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; private final WeighbridgeDocGenerator weighbridgeDocGenerator; @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.setStockInNum(consumablesInventoryDto.getQualitity()); consumablesInRecordDto.setWeighingDate(consumablesInventoryDto.getWeighingDate()); consumablesInRecordDto.setProductModelId(consumablesInventoryDto.getProductModelId()); consumablesInRecordDto.setProductId(consumablesInventoryDto.getProductId()); consumablesInRecordDto.setPurchaser(consumablesInventoryDto.getPurchaser()); 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.getQualitity()); newConsumablesInventory.setVersion(1); newConsumablesInventory.setRemark(consumablesInventoryDto.getRemark()); newConsumablesInventory.setLockedQuantity(consumablesInventoryDto.getLockedQuantity()); newConsumablesInventory.setWarnNum(consumablesInventoryDto.getWarnNum()); newConsumablesInventory.setProductId(consumablesInventoryDto.getProductId()); newConsumablesInventory.setPurchaser(consumablesInventoryDto.getPurchaser()); consumablesInventoryMapper.insert(newConsumablesInventory); } else { consumablesInventoryDto.setQualitity(consumablesInventoryDto.getQualitity()); 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.setStockOutNum(consumablesInventoryDto.getQualitity()); consumablesOutRecordDto.setWeighingOperator(consumablesInventoryDto.getWeighingOperator()); consumablesOutRecordDto.setProductModelId(consumablesInventoryDto.getProductModelId()); consumablesOutRecordDto.setLicensePlateNo(consumablesInventoryDto.getLicensePlateNo()); consumablesOutRecordDto.setProductId(consumablesInventoryDto.getProductId()); consumablesOutRecordDto.setPurchaser(consumablesInventoryDto.getPurchaser()); 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.setNetWeight(dto.getNetWeight()); 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); } }