From 382b1b105abacd983061a000674ba7da446407d2 Mon Sep 17 00:00:00 2001
From: gongchunyi <deslre0381@gmail.com>
Date: 星期二, 21 四月 2026 09:26:12 +0800
Subject: [PATCH] fix: 添加@EqualsAndHashCode注解

---
 src/main/java/com/ruoyi/purchase/service/impl/PurchaseLedgerServiceImpl.java |  272 ++++++++++++++++++++++++++++++++++++++++++++++++++----
 1 files changed, 251 insertions(+), 21 deletions(-)

diff --git a/src/main/java/com/ruoyi/purchase/service/impl/PurchaseLedgerServiceImpl.java b/src/main/java/com/ruoyi/purchase/service/impl/PurchaseLedgerServiceImpl.java
index 2d0ceae..935cb78 100644
--- a/src/main/java/com/ruoyi/purchase/service/impl/PurchaseLedgerServiceImpl.java
+++ b/src/main/java/com/ruoyi/purchase/service/impl/PurchaseLedgerServiceImpl.java
@@ -51,7 +51,13 @@
 import com.ruoyi.sales.pojo.SalesLedgerProduct;
 import com.ruoyi.sales.service.impl.CommonFileServiceImpl;
 import com.ruoyi.stock.dto.StockInventoryDto;
+import com.ruoyi.stock.mapper.StockInRecordMapper;
+import com.ruoyi.stock.mapper.StockOutRecordMapper;
+import com.ruoyi.stock.pojo.StockInRecord;
+import com.ruoyi.stock.pojo.StockOutRecord;
+import com.ruoyi.stock.service.StockInRecordService;
 import com.ruoyi.stock.service.StockInventoryService;
+import com.ruoyi.stock.service.StockOutRecordService;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.io.FilenameUtils;
 import org.springframework.beans.BeanUtils;
@@ -135,6 +141,8 @@
     @Autowired
     private QualityInspectMapper qualityInspectMapper;
     @Autowired
+    private QualityUnqualifiedMapper qualityUnqualifiedMapper;
+    @Autowired
     private CommonFileServiceImpl commonFileService;
     @Autowired
     private QualityTestStandardBindingMapper qualityTestStandardBindingMapper;
@@ -154,6 +162,14 @@
     private SalesLedgerProductTemplateMapper salesLedgerProductTemplateMapper;
     @Autowired
     private StockInventoryService stockInventoryService;
+    @Autowired
+    private StockInRecordMapper stockInRecordMapper;
+    @Autowired
+    private StockOutRecordMapper stockOutRecordMapper;
+    @Autowired
+    private StockInRecordService stockInRecordService;
+    @Autowired
+    private StockOutRecordService stockOutRecordService;
     @Autowired
     private StockUtils stockUtils;
     @Value("${file.upload-dir}")
@@ -198,6 +214,7 @@
         purchaseLedger.setRecorderName(sysUser.getNickName());
         purchaseLedger.setPhoneNumber(sysUser.getPhonenumber());
         purchaseLedger.setApprovalStatus(1);
+        purchaseLedger.setStockStatus(0);
         // 3. 鏂板鎴栨洿鏂颁富琛�
         if (purchaseLedger.getId() == null) {
             purchaseLedgerMapper.insert(purchaseLedger);
@@ -322,6 +339,7 @@
         if (!updateList.isEmpty()) {
             for (SalesLedgerProduct product : updateList) {
                 product.setType(type);
+                product.setProductStockStatus(calculateProductStockStatus(product));
                 product.fillRemainingQuantity();
                 salesLedgerProductMapper.updateById(product);
             }
@@ -337,6 +355,7 @@
                 salesLedgerProduct.setFutureTickets(salesLedgerProduct.getQuantity());
                 salesLedgerProduct.setFutureTicketsAmount(salesLedgerProduct.getTaxInclusiveTotalPrice());
                 salesLedgerProduct.setPendingTicketsTotal(salesLedgerProduct.getTaxInclusiveTotalPrice());
+                salesLedgerProduct.setProductStockStatus(calculateProductStockStatus(salesLedgerProduct));
                 salesLedgerProduct.fillRemainingQuantity();
                 salesLedgerProductMapper.insert(salesLedgerProduct);
             }
@@ -447,6 +466,35 @@
                 .eq(SalesLedgerProduct::getType, 2);
         List<SalesLedgerProduct> salesLedgerProducts = salesLedgerProductMapper.selectList(salesLedgerProductQueryWrapper);
         if (CollectionUtils.isNotEmpty(salesLedgerProducts)) {
+            List<Long> productIds = salesLedgerProducts.stream().map(SalesLedgerProduct::getId).filter(Objects::nonNull).collect(Collectors.toList());
+            // 鍒犻櫎鍙拌处鍓嶅厛鍥為��搴撳瓨璁板綍锛堝厛鍒犲嚭搴撳啀鍒犲叆搴擄級锛岄伩鍏嶅簱瀛樺洖閫�鏍¢獙琚鎷�
+            if (CollectionUtils.isNotEmpty(productIds)) {
+                List<Long> stockOutRecordIds = stockOutRecordMapper.selectList(new LambdaQueryWrapper<StockOutRecord>()
+                                .and(w -> w
+                                        .in(StockOutRecord::getSalesLedgerProductId, productIds)
+                                        .or(q -> q.in(StockOutRecord::getRecordId, productIds)
+                                                .in(StockOutRecord::getRecordType, Arrays.asList(
+                                                        StockOutUnQualifiedRecordTypeEnum.PURCHASE_SCAN_UNSTOCK_OUT.getCode()
+                                                ))))
+                                .select(StockOutRecord::getId))
+                        .stream().map(StockOutRecord::getId).collect(Collectors.toList());
+                if (CollectionUtils.isNotEmpty(stockOutRecordIds)) {
+                    stockOutRecordService.batchDelete(stockOutRecordIds);
+                }
+                List<Long> stockInRecordIds = stockInRecordMapper.selectList(new LambdaQueryWrapper<StockInRecord>()
+                                .and(w -> w
+                                        .in(StockInRecord::getSalesLedgerProductId, productIds)
+                                        .or(q -> q.in(StockInRecord::getRecordId, productIds)
+                                                .in(StockInRecord::getRecordType, Arrays.asList(
+                                                        StockInUnQualifiedRecordTypeEnum.PURCHASE_SCAN_UNSTOCK_IN.getCode(),
+                                                        StockInUnQualifiedRecordTypeEnum.PURCHASE_SCAN_QUALITY_UNSTOCK_IN.getCode()
+                                                ))))
+                                .select(StockInRecord::getId))
+                        .stream().map(StockInRecord::getId).collect(Collectors.toList());
+                if (CollectionUtils.isNotEmpty(stockInRecordIds)) {
+                    stockInRecordService.batchDelete(stockInRecordIds);
+                }
+            }
             salesLedgerProducts.stream().forEach(salesLedgerProduct -> {
                 // 鎵归噺鍒犻櫎鍏宠仈鐨勯噰璐彴璐︿骇鍝�
                 LambdaQueryWrapper<ProcurementRecordStorage> queryWrapper = new LambdaQueryWrapper<>();
@@ -525,7 +573,7 @@
         productWrapper.eq(SalesLedgerProduct::getSalesLedgerId, purchaseLedger.getId())
                 .eq(SalesLedgerProduct::getType, purchaseLedgerDto.getType());
         List<SalesLedgerProduct> products = salesLedgerProductMapper.selectList(productWrapper);
-        products.forEach(SalesLedgerProduct::fillRemainingQuantity);
+        applyQualityInboundToProducts(purchaseLedger.getId(), products);
 
         // 3.鏌ヨ涓婁紶鏂囦欢
         LambdaQueryWrapper<CommonFile> salesLedgerFileWrapper = new LambdaQueryWrapper<>();
@@ -795,7 +843,7 @@
         productWrapper.eq(SalesLedgerProduct::getSalesLedgerId, purchaseLedger.getId())
                 .eq(SalesLedgerProduct::getType, 2);
         List<SalesLedgerProduct> products = salesLedgerProductMapper.selectList(productWrapper);
-        products.forEach(SalesLedgerProduct::fillRemainingQuantity);
+        applyQualityInboundToProducts(purchaseLedger.getId(), products);
 
         // 4. 杞崲 DTO
         PurchaseLedgerDto resultDto = new PurchaseLedgerDto();
@@ -827,6 +875,9 @@
         PurchaseLedger purchaseLedger = purchaseLedgerMapper.selectById(dto.getPurchaseLedgerId());
         if (purchaseLedger == null) {
             throw new ServiceException("鍏ュ簱澶辫触,閲囪喘鍙拌处涓嶅瓨鍦�");
+        }
+        if (!Objects.equals(purchaseLedger.getApprovalStatus(), 3)) {
+            throw new ServiceException("鍏ュ簱澶辫触,閲囪喘璁㈠崟鏈鎵归�氳繃,涓嶅厑璁告壂鐮佸叆搴�");
         }
         if (CollectionUtils.isEmpty(dto.getSalesLedgerProductList())) {
             throw new ServiceException("閲囪喘鍏ュ簱澶辫触,鍏ュ簱浜у搧涓嶈兘涓虹┖");
@@ -863,7 +914,57 @@
             if (dbProduct.getProductModelId() == null) {
                 throw new ServiceException("鍏ュ簱澶辫触,浜у搧瑙勬牸鏈淮鎶�,鏃犳硶鍏ュ簱");
             }
+            BigDecimal orderQty = dbProduct.getQuantity() == null ? BigDecimal.ZERO : dbProduct.getQuantity();
+            if (orderQty.compareTo(BigDecimal.ZERO) <= 0) {
+                throw new ServiceException("鍏ュ簱澶辫触,閲囪喘浜у搧鏁伴噺寮傚父");
+            }
+            //  闇�瑕佽川妫�锛氭壂鐮佸叆搴撹繘鍏ュ師鏉愭枡妫�楠岋紝涓嶇洿鎺ュ叆鍚堟牸搴撳瓨
+            if (Boolean.TRUE.equals(dbProduct.getIsChecked())) {
+                //  瀛樺湪鏈�氳繃/鏈鐞嗙殑鍘熸潗鏂欐楠屽崟锛屽垯绂佹缁х画鎵爜鍏ュ簱
+                Long pendingInspectCount = qualityInspectMapper.selectCount(new LambdaQueryWrapper<QualityInspect>()
+                        .eq(QualityInspect::getInspectType, 0)
+                        .eq(QualityInspect::getPurchaseLedgerId, purchaseId)
+                        .eq(QualityInspect::getProductModelId, dbProduct.getProductModelId())
+                        .and(w -> w
+                                .isNull(QualityInspect::getInspectState)
+                                .or(q0 -> q0.eq(QualityInspect::getInspectState, 0))
+                                // inspect_state=1 涔熻涓衡�滄湭澶勭悊鈥�
+                                .or(q1 -> q1.eq(QualityInspect::getInspectState, 1)
+                                        .isNull(QualityInspect::getCheckResult))));
+                if (pendingInspectCount != null && pendingInspectCount > 0) {
+                    throw new ServiceException("鍏ュ簱澶辫触,瀛樺湪鏈�氳繃鎴栨湭澶勭悊鐨勮川妫�璁板綍,璇峰厛澶勭悊鍚庡啀鎵爜鍏ュ簱");
+                }
+                //  闇�瑕佽川妫�鏃讹紝鎸夆�滃緟妫�/宸插悎鏍尖�濈殑妫�楠屾暟閲忔帶鍒舵壂鐮佷笂闄�
+                BigDecimal inspectQty = qualityInspectMapper.selectList(new LambdaQueryWrapper<QualityInspect>()
+                                .eq(QualityInspect::getInspectType, 0)
+                                .eq(QualityInspect::getPurchaseLedgerId, purchaseId)
+                                .eq(QualityInspect::getProductModelId, dbProduct.getProductModelId())
+                                .and(w -> w
+                                        .isNull(QualityInspect::getInspectState)
+                                        .or(q0 -> q0.eq(QualityInspect::getInspectState, 0))
+                                        .or(q1 -> q1.eq(QualityInspect::getInspectState, 1)
+                                                .and(r -> r.isNull(QualityInspect::getCheckResult)
+                                                        .or()
+                                                        .eq(QualityInspect::getCheckResult, "鍚堟牸")))))
+                        .stream()
+                        .map(QualityInspect::getQuantity)
+                        .filter(Objects::nonNull)
+                        .reduce(BigDecimal.ZERO, BigDecimal::add);
+                if (inspectQty.add(inboundThisLine).compareTo(orderQty) > 0) {
+                    throw new ServiceException("鍏ュ簱澶辫触,鎵爜鍚堟牸鍏ュ簱鏁伴噺涓嶈兘瓒呰繃閲囪喘浜у搧鏁伴噺");
+                }
+                SalesLedgerProduct scanInspectProduct = new SalesLedgerProduct();
+                BeanUtils.copyProperties(dbProduct, scanInspectProduct);
+                scanInspectProduct.setQuantity(inboundThisLine);
+                addQualityInspect(purchaseLedger, scanInspectProduct);
+                continue;
+            }
+
+            // 涓嶉渶瑕佽川妫�锛氭壂鐮佺洿鎺ュ叆搴擄紙鍏佽澶氬叆搴擄紝涓嶅仛涓婇檺闄愬埗锛�
             BigDecimal oldStocked = dbProduct.getStockedQuantity() == null ? BigDecimal.ZERO : dbProduct.getStockedQuantity();
+            if (oldStocked.add(inboundThisLine).compareTo(orderQty) > 0) {
+                throw new ServiceException("鍏ュ簱澶辫触,鎵爜鍚堟牸鍏ュ簱鏁伴噺涓嶈兘瓒呰繃閲囪喘浜у搧鏁伴噺");
+            }
             BigDecimal newStocked = oldStocked.add(inboundThisLine);
 
             StockInventoryDto stockInventoryDto = new StockInventoryDto();
@@ -875,7 +976,6 @@
             stockInventoryDto.setSalesLedgerProductId(dbProduct.getId());
             stockInventoryService.addstockInventory(stockInventoryDto);
 
-            BigDecimal orderQty = dbProduct.getQuantity() == null ? BigDecimal.ZERO : dbProduct.getQuantity();
             int lineStockStatus;
             if (newStocked.compareTo(BigDecimal.ZERO) <= 0) {
                 lineStockStatus = 0;
@@ -889,6 +989,7 @@
             dbProduct.fillRemainingQuantity();
             salesLedgerProductMapper.updateById(dbProduct);
         }
+        refreshPurchaseLedgerStockStatus(purchaseLedger.getId());
     }
 
     @Override
@@ -900,6 +1001,9 @@
         PurchaseLedger purchaseLedger = purchaseLedgerMapper.selectById(dto.getPurchaseLedgerId());
         if (purchaseLedger == null) {
             throw new ServiceException("鍑哄簱澶辫触,閲囪喘鍙拌处涓嶅瓨鍦�");
+        }
+        if (!Objects.equals(purchaseLedger.getApprovalStatus(), 3)) {
+            throw new ServiceException("鍑哄簱澶辫触,閲囪喘璁㈠崟鏈鎵归�氳繃,涓嶅厑璁告壂鐮佸嚭搴�");
         }
         if (CollectionUtils.isEmpty(dto.getSalesLedgerProductList())) {
             throw new ServiceException("閲囪喘鍑哄簱澶辫触,鍑哄簱浜у搧涓嶈兘涓虹┖");
@@ -938,12 +1042,6 @@
             }
             stockUtils.assertQualifiedAvailable(dbProduct.getProductModelId(), outboundThisLine);
 
-            BigDecimal oldStocked = dbProduct.getStockedQuantity() == null ? BigDecimal.ZERO : dbProduct.getStockedQuantity();
-            BigDecimal newStocked = oldStocked.subtract(outboundThisLine);
-            if (newStocked.compareTo(BigDecimal.ZERO) < 0) {
-                newStocked = BigDecimal.ZERO;
-            }
-
             StockInventoryDto stockInventoryDto = new StockInventoryDto();
             stockInventoryDto.setRecordId(dbProduct.getId());
             stockInventoryDto.setRecordType(StockOutQualifiedRecordTypeEnum.PURCHASE_SCAN_STOCK_OUT.getCode());
@@ -953,17 +1051,8 @@
             stockInventoryDto.setSalesLedgerProductId(dbProduct.getId());
             stockInventoryService.subtractStockInventory(stockInventoryDto);
 
-            BigDecimal orderQty = dbProduct.getQuantity() == null ? BigDecimal.ZERO : dbProduct.getQuantity();
-            int lineStockStatus;
-            if (newStocked.compareTo(BigDecimal.ZERO) <= 0) {
-                lineStockStatus = 0;
-            } else if (orderQty.compareTo(BigDecimal.ZERO) > 0 && newStocked.compareTo(orderQty) < 0) {
-                lineStockStatus = 1;
-            } else {
-                lineStockStatus = 2;
-            }
-            dbProduct.setStockedQuantity(newStocked);
-            dbProduct.setProductStockStatus(lineStockStatus);
+            BigDecimal oldShipped = dbProduct.getShippedQuantity() == null ? BigDecimal.ZERO : dbProduct.getShippedQuantity();
+            dbProduct.setShippedQuantity(oldShipped.add(outboundThisLine));
             dbProduct.fillRemainingQuantity();
             salesLedgerProductMapper.updateById(dbProduct);
         }
@@ -978,6 +1067,9 @@
         PurchaseLedger purchaseLedger = purchaseLedgerMapper.selectById(dto.getPurchaseLedgerId());
         if (purchaseLedger == null) {
             throw new ServiceException("涓嶅悎鏍煎叆搴撳け璐�,閲囪喘鍙拌处涓嶅瓨鍦�");
+        }
+        if (!Objects.equals(purchaseLedger.getApprovalStatus(), 3)) {
+            throw new ServiceException("涓嶅悎鏍煎叆搴撳け璐�,閲囪喘璁㈠崟鏈鎵归�氳繃,涓嶅厑璁告壂鐮佸叆搴�");
         }
         if (CollectionUtils.isEmpty(dto.getSalesLedgerProductList())) {
             throw new ServiceException("閲囪喘涓嶅悎鏍煎叆搴撳け璐�,鍏ュ簱浜у搧涓嶈兘涓虹┖");
@@ -1015,7 +1107,33 @@
                 throw new ServiceException("涓嶅悎鏍煎叆搴撳け璐�,浜у搧瑙勬牸鏈淮鎶�,鏃犳硶鍏ュ簱");
             }
             stockUtils.addUnStock(null, null, dbProduct.getProductModelId(), inboundThisLine,
-                    StockInUnQualifiedRecordTypeEnum.PURCHASE_SCAN_UNSTOCK_IN.getCode(), dbProduct.getId());
+                    Boolean.TRUE.equals(dbProduct.getIsChecked())
+                            ? StockInUnQualifiedRecordTypeEnum.PURCHASE_SCAN_QUALITY_UNSTOCK_IN.getCode()
+                            : StockInUnQualifiedRecordTypeEnum.PURCHASE_SCAN_UNSTOCK_IN.getCode(),
+                    dbProduct.getId());
+
+            // 閲囪喘涓嶅悎鏍煎叆搴撳悗锛岃嚜鍔ㄨ繘鍏ヤ笉鍚堟牸绠$悊锛岀瓑寰呯敤鎴峰鐞�
+            QualityUnqualified qualityUnqualified = new QualityUnqualified();
+            qualityUnqualified.setInspectType(0); // 鍘熸潗鏂欎笉鍚堟牸
+            qualityUnqualified.setInspectState(0); // 寰呭鐞�
+            qualityUnqualified.setCheckTime(new Date());
+            LoginUser loginUser = SecurityUtils.getLoginUser();
+            if (loginUser != null && loginUser.getUser() != null) {
+                qualityUnqualified.setCheckName(loginUser.getUser().getNickName());
+            }
+            qualityUnqualified.setProductId(dbProduct.getProductId());
+            qualityUnqualified.setProductModelId(dbProduct.getProductModelId());
+            qualityUnqualified.setProductName(dbProduct.getProductCategory());
+            qualityUnqualified.setModel(dbProduct.getSpecificationModel());
+            qualityUnqualified.setUnit(dbProduct.getUnit());
+            qualityUnqualified.setQuantity(inboundThisLine);
+            qualityUnqualified.setDefectivePhenomena("閲囪喘璁㈠崟鎵爜涓嶅悎鏍煎叆搴擄紝寰呭鐞�");
+            qualityUnqualifiedMapper.insert(qualityUnqualified);
+
+            BigDecimal oldUnStocked = dbProduct.getUnqualifiedStockedQuantity() == null ? BigDecimal.ZERO : dbProduct.getUnqualifiedStockedQuantity();
+            dbProduct.setUnqualifiedStockedQuantity(oldUnStocked.add(inboundThisLine));
+            dbProduct.fillRemainingQuantity();
+            salesLedgerProductMapper.updateById(dbProduct);
         }
     }
 
@@ -1028,6 +1146,9 @@
         PurchaseLedger purchaseLedger = purchaseLedgerMapper.selectById(dto.getPurchaseLedgerId());
         if (purchaseLedger == null) {
             throw new ServiceException("涓嶅悎鏍煎嚭搴撳け璐�,閲囪喘鍙拌处涓嶅瓨鍦�");
+        }
+        if (!Objects.equals(purchaseLedger.getApprovalStatus(), 3)) {
+            throw new ServiceException("涓嶅悎鏍煎嚭搴撳け璐�,閲囪喘璁㈠崟鏈鎵归�氳繃,涓嶅厑璁告壂鐮佸嚭搴�");
         }
         if (CollectionUtils.isEmpty(dto.getSalesLedgerProductList())) {
             throw new ServiceException("閲囪喘涓嶅悎鏍煎嚭搴撳け璐�,鍑哄簱浜у搧涓嶈兘涓虹┖");
@@ -1064,12 +1185,121 @@
             if (dbProduct.getProductModelId() == null) {
                 throw new ServiceException("涓嶅悎鏍煎嚭搴撳け璐�,浜у搧瑙勬牸鏈淮鎶�,鏃犳硶鍑哄簱");
             }
+            BigDecimal unStocked = dbProduct.getUnqualifiedStockedQuantity() == null ? BigDecimal.ZERO : dbProduct.getUnqualifiedStockedQuantity();
+            BigDecimal unShipped = dbProduct.getUnqualifiedShippedQuantity() == null ? BigDecimal.ZERO : dbProduct.getUnqualifiedShippedQuantity();
+            BigDecimal canUnShip = unStocked.subtract(unShipped);
+            if (outboundThisLine.compareTo(canUnShip) > 0) {
+                throw new ServiceException("涓嶅悎鏍煎嚭搴撳け璐�,鍑哄簱鏁伴噺涓嶈兘澶т簬涓嶅悎鏍煎叆搴撴暟閲�");
+            }
             stockUtils.assertUnqualifiedAvailable(dbProduct.getProductModelId(), outboundThisLine);
             stockUtils.subtractUnStock(null, null, dbProduct.getProductModelId(), outboundThisLine,
                     StockOutUnQualifiedRecordTypeEnum.PURCHASE_SCAN_UNSTOCK_OUT.getCode(), dbProduct.getId());
+
+            dbProduct.setUnqualifiedShippedQuantity(unShipped.add(outboundThisLine));
+            dbProduct.fillRemainingQuantity();
+            salesLedgerProductMapper.updateById(dbProduct);
         }
     }
 
+    private void applyQualityInboundToProducts(Long purchaseLedgerId, List<SalesLedgerProduct> products) {
+        if (CollectionUtils.isEmpty(products)) {
+            return;
+        }
+        Map<Long, BigDecimal> qualityInboundQtyByLine = getQualifiedInspectInboundQtyByLine(purchaseLedgerId);
+        for (SalesLedgerProduct product : products) {
+            product.fillRemainingQuantity();
+            if (!Boolean.TRUE.equals(product.getIsChecked())) {
+                continue;
+            }
+            BigDecimal orderQty = product.getQuantity() == null ? BigDecimal.ZERO : product.getQuantity();
+            BigDecimal scanInboundQty = product.getStockedQuantity() == null ? BigDecimal.ZERO : product.getStockedQuantity();
+            BigDecimal qualityInboundQty = qualityInboundQtyByLine.getOrDefault(product.getId(), BigDecimal.ZERO);
+            BigDecimal totalQualifiedInbound = qualityInboundQty.add(scanInboundQty);
+            BigDecimal remainingInbound = orderQty.subtract(totalQualifiedInbound);
+            product.setRemainingQuantity(remainingInbound.compareTo(BigDecimal.ZERO) < 0 ? BigDecimal.ZERO : remainingInbound);
+            BigDecimal shippedQty = product.getShippedQuantity() == null ? BigDecimal.ZERO : product.getShippedQuantity();
+            BigDecimal remainingShipped = totalQualifiedInbound.subtract(shippedQty);
+            product.setRemainingShippedQuantity(remainingShipped.compareTo(BigDecimal.ZERO) < 0 ? BigDecimal.ZERO : remainingShipped);
+        }
+    }
+
+    private Map<Long, BigDecimal> getQualifiedInspectInboundQtyByLine(Long purchaseLedgerId) {
+        Map<Long, BigDecimal> qualityInboundByModel = qualityInspectMapper.selectList(new LambdaQueryWrapper<QualityInspect>()
+                        .eq(QualityInspect::getInspectType, 0)
+                        .eq(QualityInspect::getPurchaseLedgerId, purchaseLedgerId)
+                        .eq(QualityInspect::getInspectState, 1)
+                        .eq(QualityInspect::getCheckResult, "鍚堟牸"))
+                .stream()
+                .filter(qualityInspect -> qualityInspect.getProductModelId() != null && qualityInspect.getQuantity() != null)
+                .collect(Collectors.groupingBy(QualityInspect::getProductModelId,
+                        Collectors.reducing(BigDecimal.ZERO, QualityInspect::getQuantity, BigDecimal::add)));
+        if (qualityInboundByModel.isEmpty()) {
+            return Collections.emptyMap();
+        }
+        List<SalesLedgerProduct> purchaseProducts = salesLedgerProductMapper.selectList(new LambdaQueryWrapper<SalesLedgerProduct>()
+                .eq(SalesLedgerProduct::getSalesLedgerId, purchaseLedgerId)
+                .eq(SalesLedgerProduct::getType, PURCHASE.getCode()));
+        Map<Long, BigDecimal> qualityInboundByLine = new HashMap<>();
+        for (SalesLedgerProduct product : purchaseProducts) {
+            if (product.getId() == null || product.getProductModelId() == null) {
+                continue;
+            }
+            qualityInboundByLine.put(product.getId(), qualityInboundByModel.getOrDefault(product.getProductModelId(), BigDecimal.ZERO));
+        }
+        return qualityInboundByLine;
+    }
+
+    private void refreshPurchaseLedgerStockStatus(Long purchaseLedgerId) {
+        if (purchaseLedgerId == null) {
+            return;
+        }
+        List<SalesLedgerProduct> products = salesLedgerProductMapper.selectList(new LambdaQueryWrapper<SalesLedgerProduct>()
+                .eq(SalesLedgerProduct::getSalesLedgerId, purchaseLedgerId)
+                .eq(SalesLedgerProduct::getType, PURCHASE.getCode()));
+        if (CollectionUtils.isEmpty(products)) {
+            return;
+        }
+        Map<Long, BigDecimal> qualityInboundQtyByLine = getQualifiedInspectInboundQtyByLine(purchaseLedgerId);
+        boolean allInbound = true;
+        boolean anyInbound = false;
+        for (SalesLedgerProduct product : products) {
+            BigDecimal orderQty = product.getQuantity() == null ? BigDecimal.ZERO : product.getQuantity();
+            BigDecimal scanInboundQty = product.getStockedQuantity() == null ? BigDecimal.ZERO : product.getStockedQuantity();
+            BigDecimal qualityInboundQty = qualityInboundQtyByLine.getOrDefault(product.getId(), BigDecimal.ZERO);
+            BigDecimal totalInboundQty = scanInboundQty.add(qualityInboundQty);
+            if (totalInboundQty.compareTo(BigDecimal.ZERO) > 0) {
+                anyInbound = true;
+            }
+            if (totalInboundQty.compareTo(orderQty) < 0) {
+                allInbound = false;
+            }
+        }
+        PurchaseLedger purchaseLedger = purchaseLedgerMapper.selectById(purchaseLedgerId);
+        if (purchaseLedger == null) {
+            return;
+        }
+        int targetStockStatus = allInbound ? 2 : (anyInbound ? 1 : 0);
+        if (!Objects.equals(purchaseLedger.getStockStatus(), targetStockStatus)) {
+            purchaseLedger.setStockStatus(targetStockStatus);
+            purchaseLedgerMapper.updateById(purchaseLedger);
+        }
+    }
+
+    private Integer calculateProductStockStatus(SalesLedgerProduct product) {
+        if (product == null) {
+            return 0;
+        }
+        BigDecimal orderQty = product.getQuantity() == null ? BigDecimal.ZERO : product.getQuantity();
+        BigDecimal stockedQty = product.getStockedQuantity() == null ? BigDecimal.ZERO : product.getStockedQuantity();
+        if (stockedQty.compareTo(BigDecimal.ZERO) <= 0) {
+            return 0;
+        }
+        if (orderQty.compareTo(BigDecimal.ZERO) > 0 && stockedQty.compareTo(orderQty) < 0) {
+            return 1;
+        }
+        return 2;
+    }
+
     /**
      * 涓嬪垝绾垮懡鍚嶈浆椹煎嘲鍛藉悕
      */

--
Gitblit v1.9.3