From 3b02ada59b793fe7db195bbb39dbc0e533f036d6 Mon Sep 17 00:00:00 2001
From: yuan <123@>
Date: 星期四, 25 六月 2026 10:26:04 +0800
Subject: [PATCH] fix: 售后查询按照反馈日期排序

---
 src/main/java/com/ruoyi/quality/service/impl/QualityInspectServiceImpl.java |   85 ++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 85 insertions(+), 0 deletions(-)

diff --git a/src/main/java/com/ruoyi/quality/service/impl/QualityInspectServiceImpl.java b/src/main/java/com/ruoyi/quality/service/impl/QualityInspectServiceImpl.java
index f9a1d42..04a921e 100644
--- a/src/main/java/com/ruoyi/quality/service/impl/QualityInspectServiceImpl.java
+++ b/src/main/java/com/ruoyi/quality/service/impl/QualityInspectServiceImpl.java
@@ -32,6 +32,10 @@
 import com.ruoyi.purchase.pojo.PurchaseLedger;
 import com.ruoyi.sales.mapper.SalesLedgerProductMapper;
 import com.ruoyi.sales.pojo.SalesLedgerProduct;
+import com.ruoyi.sales.pojo.SalesLedger;
+import com.ruoyi.sales.mapper.SalesLedgerMapper;
+import com.ruoyi.stock.service.StockInventoryService;
+import com.ruoyi.stock.dto.StockInventoryDto;
 import com.ruoyi.framework.security.LoginUser;
 import lombok.AllArgsConstructor;
 import org.springframework.beans.BeanUtils;
@@ -69,6 +73,9 @@
 
     private ProcurementRecordService procurementRecordService;
     private IApproveProcessService approveProcessService;
+
+    private StockInventoryService stockInventoryService;
+    private SalesLedgerMapper salesLedgerMapper;
 
     @Override
     public int add(QualityInspectDto qualityInspectDto) {
@@ -148,6 +155,18 @@
                     );
                     syncQualifiedInboundToPurchaseProducts(qualityInspect, qualifiedQty);
                 }
+            } else if (Objects.equals(qualityInspect.getInspectType(), 2) && qualityInspect.getSalesLedgerId() != null) {
+                // 閿�鍞鍗曠殑鎴愬搧鍑哄巶妫�楠岋紝鎻愪氦鍚庣洿鎺ュ叆搴�
+                StockInventoryDto stockInventoryDto = new StockInventoryDto();
+                stockInventoryDto.setRecordId(qualityInspect.getSalesLedgerProductId());
+                stockInventoryDto.setRecordType(StockInQualifiedRecordTypeEnum.SALE_STOCK_IN.getCode());
+                stockInventoryDto.setQualitity(qualifiedQty);
+                stockInventoryDto.setProductModelId(qualityInspect.getProductModelId());
+                stockInventoryDto.setSalesLedgerId(qualityInspect.getSalesLedgerId());
+                stockInventoryDto.setSalesLedgerProductId(qualityInspect.getSalesLedgerProductId());
+                stockInventoryService.addstockInventory(stockInventoryDto);
+
+                syncQualifiedInboundToSalesProducts(qualityInspect, qualifiedQty);
             } else {
                 stockUtils.addStock(
                         qualityInspect.getPurchaseLedgerId() == null ? null : qualityInspect.getPurchaseLedgerId(),
@@ -165,6 +184,9 @@
         qualityInspect.setInspectState(1);
         int updated = qualityInspectMapper.updateById(qualityInspect);
         refreshPurchaseLedgerStockStatusByInspect(qualityInspect.getPurchaseLedgerId());
+        if (qualityInspect.getSalesLedgerId() != null) {
+            refreshSalesLedgerStockStatusByInspect(qualityInspect.getSalesLedgerId());
+        }
         return updated;
     }
 
@@ -397,6 +419,7 @@
             qualityInspectParamService.remove(Wrappers.<QualityInspectParam>lambdaQuery().eq(QualityInspectParam::getInspectId, qualityInspectDto.getId()));
             for (QualityInspectParam qualityInspectParam : qualityInspectDto.getQualityInspectParams()) {
                 qualityInspectParam.setInspectId(qualityInspectDto.getId());
+                qualityInspectParam.setId(null);
             }
             qualityInspectParamService.saveBatch(qualityInspectDto.getQualityInspectParams());
         }
@@ -554,5 +577,67 @@
         }
     }
 
+    private void syncQualifiedInboundToSalesProducts(QualityInspect qualityInspect, BigDecimal inboundQty) {
+        if (qualityInspect == null || qualityInspect.getSalesLedgerProductId() == null || inboundQty == null) {
+            return;
+        }
+        if (inboundQty.compareTo(BigDecimal.ZERO) <= 0) {
+            return;
+        }
+
+        SalesLedgerProduct line = salesLedgerProductMapper.selectById(qualityInspect.getSalesLedgerProductId());
+        if (line == null) {
+            return;
+        }
+
+        BigDecimal orderQty = line.getQuantity() == null ? BigDecimal.ZERO : line.getQuantity();
+        BigDecimal stocked = line.getStockedQuantity() == null ? BigDecimal.ZERO : line.getStockedQuantity();
+        BigDecimal newStocked = stocked.add(inboundQty);
+        
+        int status;
+        if (newStocked.compareTo(BigDecimal.ZERO) <= 0) {
+            status = 0;
+        } else if (orderQty.compareTo(BigDecimal.ZERO) > 0 && newStocked.compareTo(orderQty) < 0) {
+            status = 1;
+        } else {
+            status = 2;
+        }
+        
+        line.setStockedQuantity(newStocked);
+        line.setProductStockStatus(status);
+        line.fillRemainingQuantity();
+        salesLedgerProductMapper.updateById(line);
+    }
+
+    private void refreshSalesLedgerStockStatusByInspect(Long salesLedgerId) {
+        if (salesLedgerId == null) {
+            return;
+        }
+        List<SalesLedgerProduct> products = salesLedgerProductMapper.selectList(new LambdaQueryWrapper<SalesLedgerProduct>()
+                .eq(SalesLedgerProduct::getSalesLedgerId, salesLedgerId));
+        if (products == null || products.isEmpty()) {
+            return;
+        }
+        
+        boolean hasStocked = false;
+        boolean allStocked = true;
+        for (SalesLedgerProduct product : products) {
+            BigDecimal orderQty = product.getQuantity() == null ? BigDecimal.ZERO : product.getQuantity();
+            BigDecimal sq = product.getStockedQuantity() == null ? BigDecimal.ZERO : product.getStockedQuantity();
+            if (sq.compareTo(BigDecimal.ZERO) > 0) {
+                hasStocked = true;
+            }
+            if (orderQty.compareTo(BigDecimal.ZERO) <= 0 || sq.compareTo(orderQty) < 0) {
+                allStocked = false;
+            }
+        }
+        
+        SalesLedger ledger = salesLedgerMapper.selectById(salesLedgerId);
+        if (ledger != null) {
+            ledger.setStockStatus(allStocked ? 2 : (hasStocked ? 1 : 0));
+            salesLedgerMapper.updateById(ledger);
+        }
+    }
+
 
 }

--
Gitblit v1.9.3