package com.ruoyi.consumables.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.enums.StockInQualifiedRecordTypeEnum;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.consumables.dto.ConsumablesInRecordDto;
import com.ruoyi.consumables.dto.ConsumablesInventoryDto;
import com.ruoyi.consumables.dto.ConsumablesOutRecordDto;
import com.ruoyi.consumables.execl.ConsumablesInventoryExportData;
import com.ruoyi.consumables.mapper.ConsumablesInventoryMapper;
import com.ruoyi.consumables.pojo.ConsumablesInventory;
import com.ruoyi.consumables.service.ConsumablesInRecordService;
import com.ruoyi.consumables.service.ConsumablesInventoryService;
import com.ruoyi.consumables.service.ConsumablesOutRecordService;
import com.ruoyi.framework.web.domain.R;
import com.ruoyi.sales.mapper.SalesLedgerProductMapper;
import com.ruoyi.sales.pojo.SalesLedgerProduct;
import com.ruoyi.stock.word.WeighbridgeDocGenerator;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
/**
*
* 库存表 服务实现类
*
*
* @author 芯导软件(江苏)有限公司
* @since 2026-01-21 04:16:36
*/
@Service
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class ConsumablesInventoryServiceImpl extends ServiceImpl implements ConsumablesInventoryService {
private final ConsumablesInventoryMapper ConsumablesInventoryMapper;
private final ConsumablesInRecordService ConsumablesInRecordService;
private final ConsumablesOutRecordService ConsumablesOutRecordService;
private final SalesLedgerProductMapper salesLedgerProductMapper;
@Override
public IPage pageConsumablesInventory(Page page, ConsumablesInventoryDto ConsumablesInventoryDto) {
return ConsumablesInventoryMapper.pageConsumablesInventory(page, ConsumablesInventoryDto);
}
//入库调用
@Override
@Transactional(rollbackFor = Exception.class)
public Boolean addConsumablesInventory(ConsumablesInventoryDto consumablesInventoryDto) {
//新增入库记录再添加库存
ConsumablesInRecordDto consumablesInRecordDto = new ConsumablesInRecordDto();
consumablesInRecordDto.setRecordId(consumablesInventoryDto.getRecordId());
consumablesInRecordDto.setRecordType(consumablesInventoryDto.getRecordType());
consumablesInRecordDto.setConsumablesInNum(consumablesInventoryDto.getNetWeight());
consumablesInRecordDto.setWeighingDate(consumablesInventoryDto.getWeighingDate());
consumablesInRecordDto.setNetWeight(consumablesInventoryDto.getNetWeight());
consumablesInRecordDto.setGrossWeight(consumablesInventoryDto.getGrossWeight());
consumablesInRecordDto.setTareWeight(consumablesInventoryDto.getTareWeight());
consumablesInRecordDto.setLicensePlateNo(consumablesInventoryDto.getLicensePlateNo());
consumablesInRecordDto.setWeighingOperator(consumablesInventoryDto.getWeighingOperator());
consumablesInRecordDto.setProductModelId(consumablesInventoryDto.getProductModelId());
consumablesInRecordDto.setProductId(consumablesInventoryDto.getProductId());
consumablesInRecordDto.setType("0");
ConsumablesInRecordService.add(consumablesInRecordDto);
//再进行新增库存数量库存
//先查询库存表中的产品是否存在,不存在新增,存在更新
ConsumablesInventory oldConsumablesInventory = ConsumablesInventoryMapper.selectOne(new QueryWrapper().lambda().eq(ConsumablesInventory::getProductModelId, consumablesInventoryDto.getProductModelId()));
if (ObjectUtils.isEmpty(oldConsumablesInventory)) {
ConsumablesInventory newConsumablesInventory = new ConsumablesInventory();
newConsumablesInventory.setProductModelId(consumablesInventoryDto.getProductModelId());
newConsumablesInventory.setQualitity(consumablesInventoryDto.getNetWeight());
newConsumablesInventory.setVersion(1);
newConsumablesInventory.setRemark(consumablesInventoryDto.getRemark());
newConsumablesInventory.setLockedQuantity(consumablesInventoryDto.getLockedQuantity());
newConsumablesInventory.setWarnNum(consumablesInventoryDto.getWarnNum());
ConsumablesInventoryMapper.insert(newConsumablesInventory);
} else {
consumablesInventoryDto.setQualitity(consumablesInventoryDto.getNetWeight());
ConsumablesInventoryMapper.updateAddConsumablesInventory(consumablesInventoryDto);
}
return true;
}
//出库调用
@Override
@Transactional(rollbackFor = Exception.class)
public Boolean subtractConsumablesInventory(ConsumablesInventoryDto consumablesInventoryDto) {
// 新增出库记录
ConsumablesOutRecordDto consumablesOutRecordDto = new ConsumablesOutRecordDto();
consumablesOutRecordDto.setRecordId(consumablesInventoryDto.getRecordId());
consumablesOutRecordDto.setRecordType(consumablesInventoryDto.getRecordType());
consumablesOutRecordDto.setWeighingDate(consumablesInventoryDto.getWeighingDate());
consumablesOutRecordDto.setConsumablesOutNum(consumablesInventoryDto.getNetWeight());
consumablesOutRecordDto.setNetWeight(consumablesInventoryDto.getNetWeight());
consumablesOutRecordDto.setGrossWeight(consumablesInventoryDto.getGrossWeight());
consumablesOutRecordDto.setTareWeight(consumablesInventoryDto.getTareWeight());
consumablesOutRecordDto.setWeighingOperator(consumablesInventoryDto.getWeighingOperator());
consumablesOutRecordDto.setProductModelId(consumablesInventoryDto.getProductModelId());
consumablesOutRecordDto.setLicensePlateNo(consumablesInventoryDto.getLicensePlateNo());
consumablesOutRecordDto.setProductId(consumablesInventoryDto.getProductId());
consumablesOutRecordDto.setType("0");
ConsumablesOutRecordService.add(consumablesOutRecordDto);
ConsumablesInventory oldConsumablesInventory = ConsumablesInventoryMapper.selectOne(new QueryWrapper().lambda().eq(ConsumablesInventory::getProductModelId, consumablesInventoryDto.getProductModelId()));
if (ObjectUtils.isEmpty(oldConsumablesInventory)) {
throw new RuntimeException("产品库存不存在");
}
BigDecimal lockedQty = oldConsumablesInventory.getLockedQuantity();
if (lockedQty == null) {
lockedQty = BigDecimal.ZERO;
}
if (consumablesInventoryDto.getQualitity().compareTo(oldConsumablesInventory.getQualitity().subtract(lockedQty)) > 0) {
throw new RuntimeException("库存不足无法出库");
}
ConsumablesInventoryMapper.updateSubtractConsumablesInventory(consumablesInventoryDto);
return true;
}
@Override
public R importConsumablesInventory(MultipartFile file) {
try {
// 查询所有的产品
List salesLedgerProducts = salesLedgerProductMapper.selectProduct();
ExcelUtil util = new ExcelUtil(ConsumablesInventoryExportData.class);
List list = util.importExcel(file.getInputStream());
// 记录未找到匹配项的数据
List unmatchedRecords = new ArrayList<>();
list.forEach(dto -> {
boolean matched = false;
for (SalesLedgerProduct item : salesLedgerProducts) {
if (item.getProductCategory().equals(dto.getProductName()) &&
item.getSpecificationModel().equals(dto.getModel())) {
ConsumablesInventoryDto ConsumablesInventoryDto = new ConsumablesInventoryDto();
ConsumablesInventoryDto.setRecordId(0L);
ConsumablesInventoryDto.setRecordType(StockInQualifiedRecordTypeEnum.CUSTOMIZATION_STOCK_IN.getCode());
ConsumablesInventoryDto.setQualitity(dto.getQualitity());
ConsumablesInventoryDto.setRemark(dto.getRemark());
ConsumablesInventoryDto.setWarnNum(dto.getWarnNum());
if (ObjectUtils.isNotEmpty(dto.getLockedQuantity()) && dto.getLockedQuantity().compareTo(dto.getQualitity()) > 0) {
throw new RuntimeException("冻结数量不能超过本次导入的库存数量");
}
ConsumablesInventoryDto.setLockedQuantity(dto.getLockedQuantity());
ConsumablesInventoryDto.setProductModelId(item.getProductModelId());
this.addConsumablesInventory(ConsumablesInventoryDto);
matched = true;
break; // 找到匹配项后跳出循环
}
}
if (!matched) {
// 记录未匹配的数据
String unmatchedInfo = String.format("产品名称:%s,规格型号:%s",
dto.getProductName(), dto.getModel());
unmatchedRecords.add(unmatchedInfo);
}
});
// 构建返回信息
StringBuilder message = new StringBuilder();
if (!unmatchedRecords.isEmpty()) {
message.append("以下产品未找到匹配项:\n");
for (String record : unmatchedRecords) {
message.append(record).append("\n");
}
throw new RuntimeException(message.toString());
}
} catch (Exception e) {
e.printStackTrace();
return R.fail("导入失败:" + e.getMessage());
}
return R.ok("导入成功");
}
@Override
public void exportConsumablesInventory(HttpServletResponse response, ConsumablesInventoryDto ConsumablesInventoryDto) {
List list = ConsumablesInventoryMapper.listConsumablesInventoryExportData(ConsumablesInventoryDto);
ExcelUtil util = new ExcelUtil<>(ConsumablesInventoryExportData.class);
util.exportExcel(response, list, "库存信息");
}
@Override
public IPage consumablesInventoryPage(ConsumablesInventoryDto consumablesInventoryDto, Page page) {
return ConsumablesInventoryMapper.ConsumablesInventoryPage(consumablesInventoryDto, page);
}
@Override
public IPage consumablesInAndOutRecord(ConsumablesInventoryDto consumablesInventoryDto, Page page) {
return ConsumablesInventoryMapper.ConsumablesInAndOutRecord(consumablesInventoryDto, page);
}
@Override
public Boolean frozenConsumables(ConsumablesInventoryDto ConsumablesInventoryDto) {
ConsumablesInventory ConsumablesInventory = ConsumablesInventoryMapper.selectById(ConsumablesInventoryDto.getId());
if (ConsumablesInventory.getQualitity().compareTo(ConsumablesInventoryDto.getLockedQuantity()) < 0) {
throw new RuntimeException("冻结数量不能超过库存数量");
}
if (ObjectUtils.isEmpty(ConsumablesInventory.getLockedQuantity())) {
ConsumablesInventory.setLockedQuantity(ConsumablesInventoryDto.getLockedQuantity());
} else {
ConsumablesInventory.setLockedQuantity(ConsumablesInventory.getLockedQuantity().add(ConsumablesInventoryDto.getLockedQuantity()));
}
return this.updateById(ConsumablesInventory);
}
@Override
public Boolean thawConsumables(ConsumablesInventoryDto ConsumablesInventoryDto) {
ConsumablesInventory ConsumablesInventory = ConsumablesInventoryMapper.selectById(ConsumablesInventoryDto.getId());
if (ConsumablesInventory.getLockedQuantity().compareTo(ConsumablesInventoryDto.getLockedQuantity()) < 0) {
throw new RuntimeException("解冻数量不能超过冻结数量");
}
ConsumablesInventory.setLockedQuantity(ConsumablesInventory.getLockedQuantity().subtract(ConsumablesInventoryDto.getLockedQuantity()));
return this.updateById(ConsumablesInventory);
}
}