From bb302afba7c479d313d513370ea7dd6ac4da3c21 Mon Sep 17 00:00:00 2001
From: huminmin <mac@MacBook-Pro.local>
Date: 星期五, 03 七月 2026 15:39:46 +0800
Subject: [PATCH] Merge branch 'dev_New_pro' of http://114.132.189.42:9002/r/product-inventory-management-after into dev_New_pro

---
 src/main/java/com/ruoyi/stock/service/impl/StockInRecordServiceImpl.java |  191 ++++++++++++++++++++++++++++++++++++++++++++---
 1 files changed, 176 insertions(+), 15 deletions(-)

diff --git a/src/main/java/com/ruoyi/stock/service/impl/StockInRecordServiceImpl.java b/src/main/java/com/ruoyi/stock/service/impl/StockInRecordServiceImpl.java
index 7051075..00161b5 100644
--- a/src/main/java/com/ruoyi/stock/service/impl/StockInRecordServiceImpl.java
+++ b/src/main/java/com/ruoyi/stock/service/impl/StockInRecordServiceImpl.java
@@ -13,10 +13,22 @@
 import com.ruoyi.common.utils.StringUtils;
 import com.ruoyi.common.utils.bean.BeanUtils;
 import com.ruoyi.common.utils.poi.ExcelUtil;
+
+import java.time.LocalDateTime;
 import com.ruoyi.stock.dto.StockInRecordDto;
 import com.ruoyi.stock.dto.StockInventoryDto;
 import com.ruoyi.stock.dto.StockUninventoryDto;
 import com.ruoyi.stock.execl.StockInRecordExportData;
+import com.ruoyi.production.mapper.ProductionOrderPickMapper;
+import com.ruoyi.production.pojo.ProductionOrderPick;
+import com.ruoyi.procurementrecord.mapper.ReturnManagementMapper;
+import com.ruoyi.procurementrecord.mapper.ReturnSaleProductMapper;
+import com.ruoyi.procurementrecord.pojo.ReturnManagement;
+import com.ruoyi.procurementrecord.pojo.ReturnSaleProduct;
+import com.ruoyi.sales.mapper.SalesLedgerMapper;
+import com.ruoyi.sales.mapper.ShippingInfoMapper;
+import com.ruoyi.sales.pojo.SalesLedger;
+import com.ruoyi.sales.pojo.ShippingInfo;
 import com.ruoyi.stock.mapper.StockInRecordMapper;
 import com.ruoyi.stock.mapper.StockInventoryMapper;
 import com.ruoyi.stock.mapper.StockUninventoryMapper;
@@ -30,6 +42,7 @@
 import org.springframework.transaction.annotation.Transactional;
 import org.springframework.util.CollectionUtils;
 
+import java.math.BigDecimal;
 import java.util.List;
 
 @Service
@@ -39,6 +52,11 @@
     private StockInRecordMapper stockInRecordMapper;
     private StockInventoryMapper stockInventoryMapper;
     private StockUninventoryMapper stockUninventoryMapper;
+    private ProductionOrderPickMapper productionOrderPickMapper;
+    private ReturnSaleProductMapper returnSaleProductMapper;
+    private ReturnManagementMapper returnManagementMapper;
+    private ShippingInfoMapper shippingInfoMapper;
+    private SalesLedgerMapper salesLedgerMapper;
 
     @Override
     public IPage<StockInRecordDto> listPage(Page page, StockInRecordDto stockInRecordDto) {
@@ -49,8 +67,13 @@
     @Override
     @Transactional(rollbackFor = Exception.class)
     public int add(StockInRecordDto stockInRecordDto) {
-        String no = OrderUtils.countTodayByCreateTime(stockInRecordMapper, "RK","inbound_batches");
+        LocalDateTime createTime = stockInRecordDto.getCreateTime();
+        if (createTime == null) {
+            createTime = LocalDateTime.now();
+        }
+        String no = OrderUtils.countTodayByCreateTime(stockInRecordMapper, "RK","inbound_batches", createTime);
         stockInRecordDto.setInboundBatches(no);
+        stockInRecordDto.setCreateTime(createTime);
         StockInRecord stockInRecord = new StockInRecord();
         BeanUtils.copyProperties(stockInRecordDto, stockInRecord);
         return stockInRecordMapper.insert(stockInRecord);
@@ -152,6 +175,32 @@
         return stockUninventoryMapper.selectOne(eq);
     }
 
+    /**
+     * 鍥炴粴鐢熶骇閫�鏂欏叆搴撶殑棰嗘枡璁板綍閫�鏂欐暟閲�
+     * @param stockInRecord 鍏ュ簱璁板綍
+     */
+    private void rollbackFeedReturnQty(StockInRecord stockInRecord) {
+        ProductionOrderPick productionOrderPick = productionOrderPickMapper.selectById(stockInRecord.getRecordId());
+        if (productionOrderPick != null) {
+            BigDecimal returnQty = productionOrderPick.getReturnQty();
+            if (returnQty == null) {
+                returnQty = BigDecimal.ZERO;
+            }
+            BigDecimal newReturnQty = returnQty.subtract(stockInRecord.getStockInNum());
+            if (newReturnQty.compareTo(BigDecimal.ZERO) < 0) {
+                newReturnQty = BigDecimal.ZERO;
+            }
+            productionOrderPick.setReturnQty(newReturnQty);
+            // 閲嶆柊璁$畻瀹為檯鐢ㄩ噺
+            BigDecimal actualQty = productionOrderPick.getQuantity().add(
+                productionOrderPick.getFeedingQty() != null ? productionOrderPick.getFeedingQty() : BigDecimal.ZERO)
+                .subtract(newReturnQty);
+            productionOrderPick.setActualQty(actualQty);
+            productionOrderPick.setReturned(newReturnQty.compareTo(BigDecimal.ZERO) > 0);
+            productionOrderPickMapper.updateById(productionOrderPick);
+        }
+    }
+
     @Override
     @Transactional(rollbackFor = Exception.class)
     public int batchDeletePending(List<Long> ids) {
@@ -162,6 +211,11 @@
             }
             if (stockInRecord.getApprovalStatus() != null && !ReviewStatusEnum.PENDING_REVIEW.getCode().equals(stockInRecord.getApprovalStatus())) {
                 throw new BaseException("鍙湁寰呭鎵圭姸鎬佺殑璁板綍鎵嶈兘鍒犻櫎,鍏ュ簱鎵规:" + stockInRecord.getInboundBatches());
+            }
+
+            // 濡傛灉鏄敓浜ч��鏂欏叆搴擄紝鍒犻櫎鏃堕渶瑕佸洖婊氶鏂欒褰曠殑閫�鏂欐暟閲�
+            if (StockInQualifiedRecordTypeEnum.FEED_RETURN_IN.getCode().equals(stockInRecord.getRecordType())) {
+                rollbackFeedReturnQty(stockInRecord);
             }
         }
         return stockInRecordMapper.deleteBatchIds(ids);
@@ -186,6 +240,13 @@
             }
             stockInRecord.setApprovalStatus(approvalStatus);
             stockInRecordMapper.updateById(stockInRecord);
+
+            // 瀹℃壒椹冲洖鏃讹紝濡傛灉鏄敓浜ч��鏂欏叆搴擄紝闇�瑕佸洖婊氶鏂欒褰曠殑閫�鏂欐暟閲�
+            if (ReviewStatusEnum.REJECTED.getCode().equals(approvalStatus) &&
+                StockInQualifiedRecordTypeEnum.FEED_RETURN_IN.getCode().equals(stockInRecord.getRecordType())) {
+                rollbackFeedReturnQty(stockInRecord);
+            }
+
             // 瀹℃壒閫氳繃鏃讹紝搴撳瓨澧炲姞
             if (ReviewStatusEnum.APPROVED.getCode().equals(approvalStatus)) {
                 if ("0".equals(stockInRecord.getType())) {
@@ -197,13 +258,18 @@
                     stockInventoryDto.setQualitity(stockInRecord.getStockInNum());
                     stockInventoryDto.setRemark(stockInRecord.getRemark());
                     if (stockInventory == null) {
-                        stockInventoryMapper.insert(new StockInventory() {{
-                            setProductModelId(stockInRecord.getProductModelId());
-                            setQualitity(stockInRecord.getStockInNum());
-                            setBatchNo(stockInRecord.getBatchNo());
-                            setRemark(stockInRecord.getRemark());
-                            setVersion(1);
-                        }});
+                        try {
+                            stockInventoryMapper.insert(new StockInventory() {{
+                                setProductModelId(stockInRecord.getProductModelId());
+                                setQualitity(stockInRecord.getStockInNum());
+                                setBatchNo(stockInRecord.getBatchNo());
+                                setRemark(stockInRecord.getRemark());
+                                setWarnNum(stockInRecord.getWarnNum());
+                                setVersion(1);
+                            }});
+                        } catch (org.springframework.dao.DuplicateKeyException e) {
+                            stockInventoryMapper.updateAddStockInventory(stockInventoryDto);
+                        }
                     } else {
                         stockInventoryMapper.updateAddStockInventory(stockInventoryDto);
                     }
@@ -216,19 +282,114 @@
                     stockUninventoryDto.setQualitity(stockInRecord.getStockInNum());
                     stockUninventoryDto.setRemark(stockInRecord.getRemark());
                     if (stockUninventory == null) {
-                        stockUninventoryMapper.insert(new StockUninventory() {{
-                            setProductModelId(stockInRecord.getProductModelId());
-                            setQualitity(stockInRecord.getStockInNum());
-                            setBatchNo(stockInRecord.getBatchNo());
-                            setRemark(stockInRecord.getRemark());
-                            setVersion(1);
-                        }});
+                        try {
+                            stockUninventoryMapper.insert(new StockUninventory() {{
+                                setProductModelId(stockInRecord.getProductModelId());
+                                setQualitity(stockInRecord.getStockInNum());
+                                setBatchNo(stockInRecord.getBatchNo());
+                                setRemark(stockInRecord.getRemark());
+                                setVersion(1);
+                            }});
+                        } catch (org.springframework.dao.DuplicateKeyException e) {
+                            stockUninventoryMapper.updateAddStockUnInventory(stockUninventoryDto);
+                        }
                     } else {
                         stockUninventoryMapper.updateAddStockUnInventory(stockUninventoryDto);
                     }
                 }
+                // 閿�鍞��璐у叆搴�:鎵e噺閿�鍞彴璐﹀疄闄呭悎鍚岄噾棰�(閫�璐х敓鏁�)
+                handleSalesReturnRefund(stockInRecord);
             }
         }
         return ids.size();
     }
+
+    @Override
+    @Transactional(rollbackFor = Exception.class)
+    public int batchReAudit(List<Long> ids) {
+        if (CollectionUtils.isEmpty(ids)) {
+            throw new BaseException("璇烽�夋嫨鑷冲皯涓�鏉℃暟鎹�");
+        }
+
+        for (Long id : ids) {
+            StockInRecord stockInRecord = stockInRecordMapper.selectById(id);
+            if (stockInRecord == null) {
+                throw new BaseException("鍏ュ簱璁板綍涓嶅瓨鍦�,鏃犳硶閲嶆柊瀹℃牳!!!");
+            }
+            // 鍙湁椹冲洖鐘舵�佹墠鑳介噸鏂板鏍�
+            if (!ReviewStatusEnum.REJECTED.getCode().equals(stockInRecord.getApprovalStatus())) {
+                throw new BaseException("鍙湁椹冲洖鐘舵�佺殑璁板綍鎵嶈兘閲嶆柊瀹℃牳,鍏ュ簱鎵规:" + stockInRecord.getInboundBatches());
+            }
+
+            // 濡傛灉鏄敓浜ч��鏂欏叆搴擄紝鎭㈠閫�鏂欐暟閲忥紙鍥犱负椹冲洖鏃跺凡鎵e噺锛�
+            if (StockInQualifiedRecordTypeEnum.FEED_RETURN_IN.getCode().equals(stockInRecord.getRecordType())) {
+                ProductionOrderPick productionOrderPick = productionOrderPickMapper.selectById(stockInRecord.getRecordId());
+                if (productionOrderPick != null) {
+                    BigDecimal returnQty = productionOrderPick.getReturnQty();
+                    if (returnQty == null) {
+                        returnQty = BigDecimal.ZERO;
+                    }
+                    // 閲嶆柊瀹℃牳鏃舵仮澶嶉��鏂欐暟閲�
+                    BigDecimal newReturnQty = returnQty.add(stockInRecord.getStockInNum());
+                    productionOrderPick.setReturnQty(newReturnQty);
+                    // 閲嶆柊璁$畻瀹為檯鐢ㄩ噺
+                    BigDecimal actualQty = productionOrderPick.getQuantity().add(
+                        productionOrderPick.getFeedingQty() != null ? productionOrderPick.getFeedingQty() : BigDecimal.ZERO)
+                        .subtract(newReturnQty);
+                    productionOrderPick.setActualQty(actualQty);
+                    productionOrderPick.setReturned(newReturnQty.compareTo(BigDecimal.ZERO) > 0);
+                    productionOrderPickMapper.updateById(productionOrderPick);
+                }
+            }
+
+            // 灏嗙姸鎬佹敼涓哄緟瀹℃牳
+            stockInRecord.setApprovalStatus(ReviewStatusEnum.PENDING_REVIEW.getCode());
+            stockInRecordMapper.updateById(stockInRecord);
+        }
+
+        return ids.size();
+    }
+
+    /**
+     * 閿�鍞��璐у叆搴撳鎵归�氳繃鏃�,鎵e噺瀵瑰簲閿�鍞彴璐︾殑瀹為檯鍚堝悓閲戦銆�
+     * 鍏宠仈閾�:stock_in_record.record_id = return_sale_product.id
+     *        鈫� return_management.shipping_id = shipping_info.id
+     *        鈫� shipping_info.sales_ledger_id = sales_ledger.id
+     * 鍙鐞� record_type 涓� 14(鍚堟牸)鎴� 15(涓嶅悎鏍�)鐨勯攢鍞��璐у叆搴撱��
+     */
+    private void handleSalesReturnRefund(StockInRecord stockInRecord) {
+        String recordType = stockInRecord.getRecordType();
+        if (!StockInQualifiedRecordTypeEnum.RETURN_HE_IN.getCode().equals(recordType)
+                && !StockInQualifiedRecordTypeEnum.RETURN_UNSTOCK_IN.getCode().equals(recordType)) {
+            return;
+        }
+        if (stockInRecord.getRecordId() == null) {
+            return;
+        }
+        ReturnSaleProduct rsp = returnSaleProductMapper.selectById(stockInRecord.getRecordId());
+        if (rsp == null || rsp.getReturnManagementId() == null) {
+            return;
+        }
+        ReturnManagement rm = returnManagementMapper.selectById(rsp.getReturnManagementId());
+        if (rm == null || rm.getShippingId() == null) {
+            return;
+        }
+        ShippingInfo shippingInfo = shippingInfoMapper.selectById(rm.getShippingId());
+        if (shippingInfo == null || shippingInfo.getSalesLedgerId() == null) {
+            return;
+        }
+        SalesLedger salesLedger = salesLedgerMapper.selectById(shippingInfo.getSalesLedgerId());
+        if (salesLedger == null || salesLedger.getContractAmount() == null) {
+            return;
+        }
+        BigDecimal refund = rsp.getAmount() != null ? rsp.getAmount() : BigDecimal.ZERO;
+        if (refund.compareTo(BigDecimal.ZERO) == 0) {
+            return;
+        }
+        BigDecimal baseAmount = salesLedger.getNetContractAmount() != null
+                ? salesLedger.getNetContractAmount()
+                : salesLedger.getContractAmount();
+        salesLedger.setNetContractAmount(baseAmount.subtract(refund));
+        salesLedgerMapper.updateById(salesLedger);
+    }
 }

--
Gitblit v1.9.3