From 733b9e6837365d2817d7e8a0005b2c167c18f77e Mon Sep 17 00:00:00 2001
From: liding <756868258@qq.com>
Date: 星期四, 19 六月 2025 17:58:48 +0800
Subject: [PATCH] 1.生产加工  2.煤种id优化

---
 main-business/src/main/java/com/ruoyi/business/service/impl/ProductionMasterServiceImpl.java |  186 +++++++++++++++++++++++++++++++++++++++-------
 1 files changed, 157 insertions(+), 29 deletions(-)

diff --git a/main-business/src/main/java/com/ruoyi/business/service/impl/ProductionMasterServiceImpl.java b/main-business/src/main/java/com/ruoyi/business/service/impl/ProductionMasterServiceImpl.java
index 84bed74..ff10b10 100644
--- a/main-business/src/main/java/com/ruoyi/business/service/impl/ProductionMasterServiceImpl.java
+++ b/main-business/src/main/java/com/ruoyi/business/service/impl/ProductionMasterServiceImpl.java
@@ -2,17 +2,14 @@
 
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.metadata.IPage;
+import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.ruoyi.basic.entity.CoalInfo;
+import com.ruoyi.basic.mapper.CoalInfoMapper;
 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;
-import com.ruoyi.business.mapper.OfficialInventoryMapper;
-import com.ruoyi.business.mapper.ProductionInventoryMapper;
-import com.ruoyi.business.mapper.ProductionMapper;
-import com.ruoyi.business.mapper.ProductionMasterMapper;
+import com.ruoyi.business.entity.*;
+import com.ruoyi.business.mapper.*;
 import com.ruoyi.business.service.ProductionMasterService;
 import com.ruoyi.common.exception.base.BaseException;
 import com.ruoyi.common.utils.bean.BeanUtils;
@@ -21,9 +18,8 @@
 import org.springframework.transaction.annotation.Transactional;
 
 import java.math.BigDecimal;
-import java.util.Collections;
-import java.util.List;
-import java.util.Map;
+import java.util.*;
+import java.util.function.Function;
 import java.util.stream.Collectors;
 
 /**
@@ -45,6 +41,10 @@
     private final ProductionMapper productionMapper;
 
     private final OfficialInventoryMapper officialInventoryMapper;
+
+    private final CoalInfoMapper coalInfoMapper;
+
+    private final PendingInventoryMapper pendingInventoryMapper;
 
     @Override
     public IPage<ProductionMasterDto> selectPMList(Page page, ProductionMasterDto productionMasterDto) {
@@ -123,8 +123,7 @@
         BigDecimal totalEnergyConsumptionCost = BigDecimal.ZERO;
         BigDecimal totalTotalCost = BigDecimal.ZERO;
         BigDecimal totalEquipmentDepreciation = BigDecimal.ZERO;
-        int totalProductionQuantity = 0;
-        StringBuilder coalBuilder = new StringBuilder();
+        BigDecimal totalProductionQuantity = BigDecimal.ZERO;
 
         for (Production production : productionMasterDto.getProductionList()) {
             totalPurchasePrice = totalPurchasePrice.add(production.getPurchasePrice());
@@ -132,13 +131,14 @@
             totalEnergyConsumptionCost = totalEnergyConsumptionCost.add(production.getEnergyConsumptionCost());
             totalTotalCost = totalTotalCost.add(production.getTotalCost());
             totalEquipmentDepreciation = totalEquipmentDepreciation.add(production.getEquipmentDepreciation());
-            totalProductionQuantity += production.getProductionQuantity();
-            if (coalBuilder.length() > 0) {
-                coalBuilder.append(","); // 鍦ㄥ厓绱犱箣闂存坊鍔犻�楀彿
-            }
-            coalBuilder.append(production.getCoal());
+            totalProductionQuantity = production.getProductionQuantity().add(totalProductionQuantity);
         }
-        String coalStr = coalBuilder.toString(); // 鐩存帴鑾峰彇鎷兼帴缁撴灉
+        //鐓ょ瀛楁
+        List<Long> coalIds = productionMasterDto.getProductionList().stream()
+                .map(Production::getCoalId)
+                .collect(Collectors.toList());
+
+        List<CoalInfo> coalInfos = coalInfoMapper.selectList(new LambdaQueryWrapper<CoalInfo>().in(CoalInfo::getId, coalIds));
 
         // 2. 鍒涘缓涓昏〃瀵硅薄
         ProductionMaster productionMaster = new ProductionMaster();
@@ -147,7 +147,8 @@
         productionMaster.setEquipmentDepreciation(totalEquipmentDepreciation);
         productionMaster.setEnergyConsumptionCost(totalEnergyConsumptionCost);
         productionMaster.setLaborCost(totalLaborCost);
-        productionMaster.setCoal(coalStr);
+        productionMaster.setCoal(coalInfos.stream().map(CoalInfo::getCoal).collect(Collectors.joining(",")));
+        productionMaster.setCoalId(coalIds.stream().map(String::valueOf).collect(Collectors.joining(",")));
 
         Long masterId = productionMasterDto.getId();
         productionMaster.setId(masterId);
@@ -181,22 +182,54 @@
         batchInsertProductions(masterId, productionMasterDto.getProductionList());
         batchInsertInventories(masterId, productionMasterDto.getProductionInventoryList());
 
+        //5. 鎻掑叆鍒板緟鍏ュ簱
+        for (Production production : productionMasterDto.getProductionList()) {
+            PendingInventory pendingInventory = new PendingInventory();
+            pendingInventory.setCoalId(production.getCoalId());
+            pendingInventory.setInventoryQuantity(production.getProductionQuantity());
+            pendingInventory.setSupplierName("鐢熶骇鍔犲伐鍏ュ簱");
+            pendingInventory.setTotalPriceIncludingTax(production.getTotalCost());
+            pendingInventory.setPriceIncludingTax(production.getPurchasePrice());
+        }
         return 1;
     }
 
     // 鎵归噺鎻掑叆鐢熶骇鏁版嵁
     private void batchInsertProductions(Long masterId, List<Production> productions) {
-        List<Production> insertList = productions.stream()
-                .peek(p -> {
-                    p.setId(null);
-                    p.setProductionMasterId(masterId);
-                })
+        if (productions.isEmpty()) {
+            return;
+        }
+        // 1. 鏀堕泦鎵�鏈夐渶瑕佹煡璇㈢殑coalId
+        List<Long> coalIds = productions.stream()
+                .map(Production::getCoalId)
+                .filter(Objects::nonNull)
+                .distinct()
                 .collect(Collectors.toList());
 
-        if (!insertList.isEmpty()) {
-            for (Production production : productions) {
+        // 2. 鎵归噺鏌ヨcoalInfo鏁版嵁
+        Map<Long, CoalInfo> coalInfoMap = coalIds.isEmpty() ?
+                Collections.emptyMap() :
+                coalInfoMapper.selectList(new LambdaQueryWrapper<CoalInfo>().in(CoalInfo::getId, coalIds))
+                        .stream()
+                        .collect(Collectors.toMap(CoalInfo::getId, Function.identity()));
+        if (coalInfoMap.isEmpty()){
+            throw new BaseException("鐓ょ淇℃伅涓嶅瓨鍦�");
+        }
+
+        // 3. 鍑嗗鎵归噺鎻掑叆鏁版嵁
+        List<Production> batchInsertList = productions.stream()
+                .map(production -> {
+                    Production p = new Production(); // 鍒涘缓鏂板璞¢伩鍏嶅壇浣滅敤
+                    BeanUtils.copyProperties(production, p);
+                    // 澶嶅埗蹇呰瀛楁
+                    p.setProductionMasterId(masterId);
+                    p.setCoalId(production.getCoalId());
+                    return p;
+                })
+                .collect(Collectors.toList());
+        if (!batchInsertList.isEmpty()) {
+            for (Production production : batchInsertList) {
                 production.setId(null);
-                production.setProductionMasterId(masterId);
                 productionMapper.insert(production);
             }
         }
@@ -221,7 +254,102 @@
     }
 
     @Override
+    @Transactional(rollbackFor = Exception.class)
     public int delByIds(Long[] ids) {
-        return 0;
+        if (ids == null || ids.length == 0) {
+            return 0;
+        }
+
+        List<Long> idList = Arrays.asList(ids);
+
+        // 1. 棰勫姞杞芥墍鏈夊叧鑱旀暟鎹�
+        List<ProductionInventory> allInventoryList = productionInventoryMapper.selectList(
+                new LambdaQueryWrapper<ProductionInventory>()
+                        .in(ProductionInventory::getProductionMasterId, idList)
+        );
+
+        // 2. 鎸夊畼鏂瑰簱瀛業D鍒嗙粍骞惰绠楀簱瀛樿皟鏁撮噺
+        Map<Long, BigDecimal> inventoryAdjustMap = allInventoryList.stream()
+                .collect(Collectors.groupingBy(
+                        ProductionInventory::getOfficialId,
+                        Collectors.reducing(
+                                BigDecimal.ZERO,
+                                inv -> new BigDecimal(inv.getUsedQuantity()),
+                                BigDecimal::add
+                        )
+                ));
+
+        // 3. 鎵归噺鏇存柊瀹樻柟搴撳瓨 (浣跨敤SQL鐩存帴鏇存柊)
+        if (!inventoryAdjustMap.isEmpty()) {
+            inventoryAdjustMap.forEach((officialId, adjustAmount) ->
+                    officialInventoryMapper.addInventoryQuantity(officialId, adjustAmount)
+            );
+        }
+
+        // 4. 鎵归噺鍒犻櫎鍏宠仈鏁版嵁
+        if (!allInventoryList.isEmpty()) {
+            List<Long> inventoryIds = allInventoryList.stream()
+                    .map(ProductionInventory::getId)
+                    .collect(Collectors.toList());
+            productionInventoryMapper.deleteBatchIds(inventoryIds);
+        }
+
+        // 鍒犻櫎鐢熶骇鏄庣粏
+        productionMapper.delete(
+                new LambdaQueryWrapper<Production>()
+                        .in(Production::getProductionMasterId, idList)
+        );
+
+        // 5. 鍒犻櫎涓昏褰�
+        return productionMasterMapper.deleteBatchIds(idList);
+    }
+
+    @Override
+    @Transactional(rollbackFor = Exception.class)
+    public int deleteProductionInventory(ProductionMasterDto productionMasterDto) {
+        List<ProductionInventory> inventories = productionMasterDto.getProductionInventoryList();
+        if (CollectionUtils.isEmpty(inventories)) {
+            return 0;
+        }
+
+        // 棰勬敹闆嗘暟鎹敤浜庢壒閲忔搷浣�
+        Map<Long, BigDecimal> inventoryAdjustMap = new HashMap<>();
+        List<Long> productionIdsToDelete = new ArrayList<>(inventories.size());
+
+        for (ProductionInventory inventory : inventories) {
+            // 鏀堕泦闇�瑕佸垹闄ょ殑鐢熶骇搴撳瓨ID
+            productionIdsToDelete.add(inventory.getId());
+
+            // 绱搴撳瓨璋冩暣閲忥紙鐩稿悓officialId鐨勭敤閲忕疮鍔狅級
+            BigDecimal adjustment = new BigDecimal(inventory.getUsedQuantity());
+            inventoryAdjustMap.merge(
+                    inventory.getOfficialId(),
+                    adjustment,
+                    BigDecimal::add
+            );
+        }
+
+        // 鎵归噺鏇存柊瀹樻柟搴撳瓨
+        for (Map.Entry<Long, BigDecimal> entry : inventoryAdjustMap.entrySet()) {
+            OfficialInventory official = officialInventoryMapper.selectById(entry.getKey());
+            if (official == null) {
+                throw new BaseException("瀹樻柟搴撳瓨涓嶅瓨鍦紝ID: " + entry.getKey());
+            }
+
+            // 浣跨敤绾跨▼瀹夊叏鐨凚igDecimal鎿嶄綔
+            official.setInventoryQuantity(
+                    Optional.ofNullable(official.getInventoryQuantity())
+                            .orElse(BigDecimal.ZERO)
+                            .add(entry.getValue())
+            );
+            officialInventoryMapper.updateById(official);
+        }
+
+        // 鎵归噺鍒犻櫎鐢熶骇搴撳瓨
+        if (!productionIdsToDelete.isEmpty()) {
+            productionInventoryMapper.deleteBatchIds(productionIdsToDelete);
+        }
+
+        return productionIdsToDelete.size();
     }
 }

--
Gitblit v1.9.3