From 71fba5328a35b449b11088e540932787220f91d8 Mon Sep 17 00:00:00 2001
From: liding <756868258@qq.com>
Date: 星期三, 18 六月 2025 17:28:50 +0800
Subject: [PATCH] 1.生产加工变更库存回滚 2.巡检,档案上传完善

---
 main-business/src/main/java/com/ruoyi/business/service/impl/ProductionMasterServiceImpl.java |  140 ++++++++++++++++++++++++++++++++++++++++++----
 1 files changed, 128 insertions(+), 12 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 28f3652..0a9dcc1 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,9 +2,11 @@
 
 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.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,15 +15,16 @@
 import com.ruoyi.business.mapper.ProductionMapper;
 import com.ruoyi.business.mapper.ProductionMasterMapper;
 import com.ruoyi.business.service.ProductionMasterService;
+import com.ruoyi.common.core.domain.entity.SysUser;
+import com.ruoyi.common.exception.base.BaseException;
 import com.ruoyi.common.utils.bean.BeanUtils;
+import com.ruoyi.system.mapper.SysUserMapper;
 import lombok.RequiredArgsConstructor;
 import org.springframework.stereotype.Service;
 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.stream.Collectors;
 
 /**
@@ -43,6 +46,8 @@
     private final ProductionMapper productionMapper;
 
     private final OfficialInventoryMapper officialInventoryMapper;
+
+    private final SysUserMapper sysUserMapper;
 
     @Override
     public IPage<ProductionMasterDto> selectPMList(Page page, ProductionMasterDto productionMasterDto) {
@@ -122,7 +127,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 +136,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();
@@ -151,11 +156,17 @@
         productionMaster.setId(masterId);
 
         // 3. 缁熶竴瀛愯〃澶勭悊閫昏緫
+        Long producerId = productionMasterDto.getProducerId();
+        if (producerId == null) {
+            throw new BaseException("璇烽�夋嫨鐢熶骇鑰�");
+        }
+        SysUser sysUser = sysUserMapper.selectUserById(producerId);
+        productionMaster.setProducer(sysUser.getUserName());
         if (masterId == null) {
             productionMasterMapper.insert(productionMaster);
             masterId = productionMaster.getId(); // 鑾峰彇鏂扮敓鎴愮殑ID
         } else {
-            // 鍒犻櫎鍏宠仈瀛愯〃鏁版嵁锛堜娇鐢ㄦ洿楂樻晥鐨刬n鍒犻櫎锛�
+            // 鍒犻櫎鍏宠仈瀛愯〃鏁版嵁
             productionMapper.delete(new LambdaQueryWrapper<Production>()
                     .eq(Production::getProductionMasterId, masterId));
 
@@ -164,7 +175,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());
@@ -209,7 +230,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