liding
3 天以前 bcbff529977768c5c80714f45d52b4f36629d236
main-business/src/main/java/com/ruoyi/business/service/impl/ProductionMasterServiceImpl.java
@@ -5,6 +5,7 @@
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.business.dto.ProductionMasterDto;
import com.ruoyi.business.entity.OfficialInventory;
import com.ruoyi.business.entity.Production;
import com.ruoyi.business.entity.ProductionInventory;
import com.ruoyi.business.entity.ProductionMaster;
@@ -13,6 +14,7 @@
import com.ruoyi.business.mapper.ProductionMapper;
import com.ruoyi.business.mapper.ProductionMasterMapper;
import com.ruoyi.business.service.ProductionMasterService;
import com.ruoyi.common.exception.base.BaseException;
import com.ruoyi.common.utils.bean.BeanUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
@@ -122,7 +124,7 @@
        BigDecimal totalTotalCost = BigDecimal.ZERO;
        BigDecimal totalEquipmentDepreciation = BigDecimal.ZERO;
        int totalProductionQuantity = 0;
        StringBuilder coalBuilder = new StringBuilder("["); // 优化字符串拼接
        StringBuilder coalBuilder = new StringBuilder();
        for (Production production : productionMasterDto.getProductionList()) {
            totalPurchasePrice = totalPurchasePrice.add(production.getPurchasePrice());
@@ -131,12 +133,12 @@
            totalTotalCost = totalTotalCost.add(production.getTotalCost());
            totalEquipmentDepreciation = totalEquipmentDepreciation.add(production.getEquipmentDepreciation());
            totalProductionQuantity += production.getProductionQuantity();
            coalBuilder.append(production.getCoal()).append(",");
            if (coalBuilder.length() > 0) {
                coalBuilder.append(","); // 在元素之间添加逗号
            }
            coalBuilder.append(production.getCoal());
        }
        // 处理coal字符串拼接
        String coalStr = coalBuilder.length() > 1 ?
                coalBuilder.deleteCharAt(coalBuilder.length()-1).append("]").toString() : "[]";
        String coalStr = coalBuilder.toString(); // 直接获取拼接结果
        // 2. 创建主表对象
        ProductionMaster productionMaster = new ProductionMaster();
@@ -155,7 +157,7 @@
            productionMasterMapper.insert(productionMaster);
            masterId = productionMaster.getId(); // 获取新生成的ID
        } else {
            // 删除关联子表数据(使用更高效的in删除)
            // 删除关联子表数据
            productionMapper.delete(new LambdaQueryWrapper<Production>()
                    .eq(Production::getProductionMasterId, masterId));
@@ -164,7 +166,17 @@
            productionMasterMapper.updateById(productionMaster);
        }
        //库存更新
        for (ProductionInventory productionInventory : productionMasterDto.getProductionInventoryList()) {
            OfficialInventory officialInventory = officialInventoryMapper.selectById(productionInventory.getOfficialId());
            BigDecimal subtract = officialInventory.getInventoryQuantity().subtract(new BigDecimal(productionInventory.getUsedQuantity()));
            if (subtract.compareTo(BigDecimal.ZERO) < 0) {
                throw new BaseException("库存不足");
            }
            officialInventory.setInventoryQuantity(subtract);
            officialInventoryMapper.updateById(officialInventory);
        }
        // 4. 批量插入子表数据
        batchInsertProductions(masterId, productionMasterDto.getProductionList());
        batchInsertInventories(masterId, productionMasterDto.getProductionInventoryList());