From 74a29854bb8ff92d2ff2fcb55fa5028609ec6a33 Mon Sep 17 00:00:00 2001
From: liyong <18434998025@163.com>
Date: 星期二, 14 七月 2026 15:26:47 +0800
Subject: [PATCH] 采购删除导致审批报错+发货台账导入关联销售台账+生成出库记录

---
 src/main/java/com/ruoyi/sales/service/impl/WeighingRecordServiceImpl.java |   71 +++++++++++++++++++++++++++++++++++
 1 files changed, 71 insertions(+), 0 deletions(-)

diff --git a/src/main/java/com/ruoyi/sales/service/impl/WeighingRecordServiceImpl.java b/src/main/java/com/ruoyi/sales/service/impl/WeighingRecordServiceImpl.java
index e1a4d06..b909f95 100644
--- a/src/main/java/com/ruoyi/sales/service/impl/WeighingRecordServiceImpl.java
+++ b/src/main/java/com/ruoyi/sales/service/impl/WeighingRecordServiceImpl.java
@@ -4,13 +4,22 @@
 import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.ruoyi.common.enums.StockOutQualifiedRecordTypeEnum;
 import com.ruoyi.common.utils.StringUtils;
+import com.ruoyi.procurementrecord.utils.StockUtils;
+import com.ruoyi.sales.mapper.SalesLedgerMapper;
+import com.ruoyi.sales.mapper.SalesLedgerProductMapper;
 import com.ruoyi.sales.mapper.WeighingRecordMapper;
+import com.ruoyi.sales.pojo.SalesLedger;
+import com.ruoyi.sales.pojo.SalesLedgerProduct;
 import com.ruoyi.sales.pojo.WeighingRecord;
 import com.ruoyi.sales.service.IWeighingRecordService;
 import lombok.RequiredArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.stereotype.Service;
+
+import java.math.BigDecimal;
+import java.util.List;
 
 @Service
 @Slf4j
@@ -18,6 +27,9 @@
 public class WeighingRecordServiceImpl extends ServiceImpl<WeighingRecordMapper, WeighingRecord> implements IWeighingRecordService {
 
     private final WeighingRecordMapper weighingRecordMapper;
+    private final SalesLedgerProductMapper salesLedgerProductMapper;
+    private final SalesLedgerMapper salesLedgerMapper;
+    private final StockUtils stockUtils;
 
     @Override
     public IPage<WeighingRecord> listPage(Page<WeighingRecord> page, WeighingRecord weighingRecord) {
@@ -43,4 +55,63 @@
                 WeighingRecord::getShipUnit, weighingRecord.getShipUnit());
         return weighingRecordMapper.selectPage(page, queryWrapper);
     }
+
+    /**
+     * 鏍规嵁瀹㈡埛銆佷骇鍝佸悕绉般�佽鏍煎瀷鍙峰尮閰嶉攢鍞彴璐︿骇鍝佸苟鍑哄簱
+     * @param receiveUnit 鏀惰揣鍗曚綅锛堝鎴峰悕绉帮級
+     * @param goodsName 璐у悕锛堜骇鍝佸悕绉帮級
+     * @param specification 瑙勬牸
+     * @param quantity 鏁伴噺锛堝噣閲嶏級
+     * @param recordId 纾呭崟璁板綍ID锛堜綔涓哄叧鑱旇褰旾D锛�
+     * @return 鍖归厤鍒扮殑閿�鍞彴璐︿骇鍝両D锛屾湭鍖归厤杩斿洖null
+     */
+    public Long matchAndOutStock(String receiveUnit, String goodsName, String specification, BigDecimal quantity, Long recordId) {
+        // 1. 鏌ヨ鎵�鏈夐攢鍞彴璐︿骇鍝侊紙鍙煡璇㈡湭鍙戣揣鐨勶級
+        LambdaQueryWrapper<SalesLedgerProduct> queryWrapper = new LambdaQueryWrapper<>();
+        queryWrapper.eq(SalesLedgerProduct::getType, 1);  // 閿�鍞被鍨�
+        List<SalesLedgerProduct> productList = salesLedgerProductMapper.selectList(queryWrapper);
+        if (productList == null || productList.isEmpty()) {
+            log.warn("鏈壘鍒版湭鍙戣揣鐨勯攢鍞彴璐︿骇鍝佹暟鎹�");
+            return null;
+        }
+
+        // 2. 閬嶅巻鍖归厤锛氬鎴峰悕绉� + 浜у搧澶х被 + 瑙勬牸鍨嬪彿
+        for (SalesLedgerProduct product : productList) {
+            // 鑾峰彇瀵瑰簲鐨勯攢鍞彴璐︿富琛ㄤ俊鎭�
+            SalesLedger salesLedger = salesLedgerMapper.selectById(product.getSalesLedgerId());
+            if (salesLedger == null) {
+                continue;
+            }
+
+            // 鍖归厤鏉′欢锛氭敹璐у崟浣�=瀹㈡埛鍚嶇О锛岃揣鍚�=浜у搧澶х被锛岃鏍�=瑙勬牸鍨嬪彿
+            boolean customerMatch = receiveUnit != null && receiveUnit.equals(salesLedger.getCustomerName());
+            boolean goodsMatch = goodsName != null && goodsName.equals(product.getProductCategory());
+            boolean specMatch = specification != null && specification.equals(product.getSpecificationModel());
+
+            if (customerMatch && goodsMatch && specMatch) {
+                // 3. 鍖归厤鎴愬姛锛屾墽琛屽嚭搴�
+                try {
+                    WeighingRecord weighingRecord = new WeighingRecord();
+                    weighingRecord.setRecordId(product.getId());
+                    weighingRecord.setId(recordId);
+                    this.updateById(weighingRecord);
+                    stockUtils.substractStock(
+                            product.getProductModelId(),
+                            quantity,
+                            StockOutQualifiedRecordTypeEnum.WEIGHBRIDGE_OUT.getCode(),  // 纾呭崟鍑哄簱绫诲瀷
+                            recordId             // 纾呭崟ID浣滀负鍏宠仈璁板綍ID
+                    );
+                    log.info("纾呭崟鍑哄簱鎴愬姛锛氬鎴�={}, 浜у搧={}, 瑙勬牸={}, 鏁伴噺={}, 纾呭崟ID={}",
+                            receiveUnit, goodsName, specification, quantity, recordId);
+                    return product.getId();
+                } catch (Exception e) {
+                    log.error("纾呭崟鍑哄簱澶辫触锛歿}", e.getMessage());
+                    throw new RuntimeException("鍑哄簱澶辫触锛�" + e.getMessage());
+                }
+            }
+        }
+
+        log.warn("鏈尮閰嶅埌閿�鍞彴璐︿骇鍝侊細瀹㈡埛={}, 浜у搧={}, 瑙勬牸={}", receiveUnit, goodsName, specification);
+        return null;
+    }
 }

--
Gitblit v1.9.3