From 3bd3767aeb905dd8a560e251658d5b272a51ccf4 Mon Sep 17 00:00:00 2001
From: gongchunyi <deslre0381@gmail.com>
Date: 星期五, 03 四月 2026 11:17:13 +0800
Subject: [PATCH] fix: 新增台账没有状态

---
 src/main/java/com/ruoyi/sales/service/impl/ShippingInfoServiceImpl.java |  148 ++++++++++++++++++++++++++++++++++++++++++------
 1 files changed, 128 insertions(+), 20 deletions(-)

diff --git a/src/main/java/com/ruoyi/sales/service/impl/ShippingInfoServiceImpl.java b/src/main/java/com/ruoyi/sales/service/impl/ShippingInfoServiceImpl.java
index 9d98126..4cade95 100644
--- a/src/main/java/com/ruoyi/sales/service/impl/ShippingInfoServiceImpl.java
+++ b/src/main/java/com/ruoyi/sales/service/impl/ShippingInfoServiceImpl.java
@@ -7,17 +7,26 @@
 import com.ruoyi.approve.pojo.ApproveProcess;
 import com.ruoyi.approve.service.impl.ApproveProcessServiceImpl;
 import com.ruoyi.common.enums.FileNameType;
-import com.ruoyi.common.enums.StockQualifiedRecordTypeEnum;
-import com.ruoyi.framework.web.domain.AjaxResult;
+import com.ruoyi.common.enums.StockOutQualifiedRecordTypeEnum;
 import com.ruoyi.other.service.impl.TempFileServiceImpl;
 import com.ruoyi.procurementrecord.utils.StockUtils;
+import com.ruoyi.sales.dto.SalesLedgerProductDto;
 import com.ruoyi.sales.dto.ShippingInfoDto;
 import com.ruoyi.sales.mapper.SalesLedgerProductMapper;
 import com.ruoyi.sales.mapper.ShippingInfoMapper;
+import com.ruoyi.sales.pojo.SalesLedger;
 import com.ruoyi.sales.pojo.SalesLedgerProduct;
 import com.ruoyi.sales.pojo.ShippingInfo;
-import com.ruoyi.sales.service.ICommonFileService;
 import com.ruoyi.sales.service.ShippingInfoService;
+import com.ruoyi.sales.mapper.SalesLedgerMapper;
+import com.ruoyi.stock.mapper.StockInventoryMapper;
+import com.ruoyi.stock.pojo.StockInventory;
+import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
+import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
+import org.springframework.transaction.annotation.Transactional;
+import java.math.BigDecimal;
+import java.util.HashMap;
+import java.util.Map;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.collections4.CollectionUtils;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -50,43 +59,131 @@
     @Autowired
     private ApproveProcessServiceImpl approveProcessService;
 
-    @Override
-    public IPage<ShippingInfo> listPage(Page page, ShippingInfo req) {
-        IPage<ShippingInfo> listPage = shippingInfoMapper.listPage(page, req);
+    @Autowired
+    private SalesLedgerMapper salesLedgerMapper;
 
+    @Autowired
+    private StockInventoryMapper stockInventoryMapper;
+
+    @Override
+    public IPage<ShippingInfoDto> listPage(Page page, ShippingInfo req) {
+        IPage<ShippingInfoDto> listPage = shippingInfoMapper.listPage(page, req);
+        listPage.getRecords().forEach(item ->{
+            item.setCommonFileList(commonFileService.getFileListByBusinessId(item.getId(), FileNameType.SHIP.getValue()));
+        });
         return listPage;
     }
 
     @Override
+    @Transactional(rollbackFor = Exception.class)
     public boolean deductStock(ShippingInfoDto req) throws IOException {
         ShippingInfo byId = this.getById(req.getId());
         if (byId == null) {
             throw new RuntimeException("鍙戣揣淇℃伅涓嶅瓨鍦�");
         }
-        byId.setExpressNumber(req.getExpressNumber());
-        byId.setExpressCompany(req.getExpressCompany());
-        byId.setStatus("宸插彂璐�");
-        byId.setShippingCarNumber(req.getShippingCarNumber());
-        boolean update = this.updateById(req);
-        //鎵e噺搴撳瓨
-        SalesLedgerProduct salesLedgerProduct = salesLedgerProductMapper.selectById(req.getSalesLedgerProductId());
-        stockUtils.substractStock(salesLedgerProduct.getProductModelId(), salesLedgerProduct.getQuantity(), StockQualifiedRecordTypeEnum.SALE_SHIP_STOCK_OUT.getCode(), req.getId());
-        // 杩佺Щ鏂囦欢
-        if(CollectionUtils.isNotEmpty(req.getTempFileIds())){
+        return deductStockByOrder(byId.getSalesLedgerId(), req);
+    }
+
+    @Override
+    @Transactional(rollbackFor = Exception.class)
+    public boolean deductStockByOrder(Long salesLedgerId, ShippingInfoDto req) throws IOException {
+        if (salesLedgerId == null) {
+            throw new RuntimeException("鍏宠仈璁㈠崟涓嶅彲涓虹┖");
+        }
+
+        SalesLedger salesLedger = salesLedgerMapper.selectById(salesLedgerId);
+        if (salesLedger == null) {
+            throw new RuntimeException("鍏宠仈璁㈠崟涓嶅瓨鍦�");
+        }
+
+        // 妫�鏌ヨ璁㈠崟涓嬫槸鍚﹁繕鏈夋湭鍙戣揣鐨勮褰�
+        List<ShippingInfo> unsentShippings = this.list(new LambdaQueryWrapper<ShippingInfo>()
+                .eq(ShippingInfo::getSalesLedgerId, salesLedgerId)
+                .ne(ShippingInfo::getStatus, "宸插彂璐�"));
+
+        // 浠呭湪瀛樺湪鏈彂璐ц褰曟椂鎵ц搴撳瓨鎵e噺
+        if (CollectionUtils.isNotEmpty(unsentShippings)) {
+            // 鑾峰彇璇ヨ鍗曚笅鎵�鏈夌殑浜у搧淇℃伅杩涜姹囨�绘墸鍑�
+            List<SalesLedgerProduct> products = salesLedgerProductMapper.selectList(new LambdaQueryWrapper<SalesLedgerProduct>()
+                    .eq(SalesLedgerProduct::getSalesLedgerId, salesLedgerId));
+
+            if (CollectionUtils.isEmpty(products)) {
+                throw new RuntimeException("璇ヨ鍗曚笅鏃犱骇鍝佷俊鎭紝鏃犳硶鎵e噺搴撳瓨");
+            }
+
+            //  姹囨�婚渶姹傚苟鏍¢獙搴撳瓨
+            Map<Long, BigDecimal> modelQtyMap = new HashMap<>();
+            for (SalesLedgerProduct p : products) {
+                if (p.getProductModelId() == null) continue;
+                modelQtyMap.put(p.getProductModelId(), modelQtyMap.getOrDefault(p.getProductModelId(), BigDecimal.ZERO).add(p.getQuantity()));
+            }
+
+            for (Map.Entry<Long, BigDecimal> entry : modelQtyMap.entrySet()) {
+                Long modelId = entry.getKey();
+                BigDecimal totalNeeded = entry.getValue();
+
+                StockInventory stock = stockInventoryMapper.selectOne(new LambdaQueryWrapper<StockInventory>()
+                        .eq(StockInventory::getProductModelId, modelId));
+
+                if (stock == null) {
+                    throw new RuntimeException("浜у搧瑙勬牸ID:" + modelId + " 搴撳瓨璁板綍涓嶅瓨鍦�");
+                }
+
+                BigDecimal locked = stock.getLockedQuantity() == null ? BigDecimal.ZERO : stock.getLockedQuantity();
+                BigDecimal available = stock.getQualitity().subtract(locked);
+
+                if (totalNeeded.compareTo(available) > 0) {
+                    throw new RuntimeException("浜у搧瑙勬牸ID:" + modelId + " 鎬昏闇�姹� " + totalNeeded + "锛屽彲鐢ㄥ簱瀛� " + available + "锛屽簱瀛樺厖瓒虫牎楠屾湭閫氳繃");
+                }
+            }
+
+            //  鎵ц璁㈠崟涓嬫墍鏈変骇鍝佺殑搴撳瓨鎵e噺
+            for (SalesLedgerProduct p : products) {
+                if (p.getProductModelId() == null) continue;
+                // 浣跨敤 businessId = salesLedgerId 鎴栧綋鍓� req.getId()
+                stockUtils.substractStock(p.getProductModelId(), p.getQuantity(), StockOutQualifiedRecordTypeEnum.SALE_SHIP_STOCK_OUT.getCode(), salesLedgerId);
+            }
+        }
+
+        //  鏇存柊璇ヨ鍗曚笅鎵�鏈夌殑鍙戣揣璁板綍鐘舵�佷负宸插彂璐�
+        LambdaUpdateWrapper<ShippingInfo> updateWrapper = new LambdaUpdateWrapper<>();
+        updateWrapper.set(ShippingInfo::getStatus, "宸插彂璐�")
+                .eq(ShippingInfo::getSalesLedgerId, salesLedgerId);
+        
+        if (req != null) {
+            if (req.getExpressNumber() != null) updateWrapper.set(ShippingInfo::getExpressNumber, req.getExpressNumber());
+            if (req.getExpressCompany() != null) updateWrapper.set(ShippingInfo::getExpressCompany, req.getExpressCompany());
+            if (req.getShippingCarNumber() != null) updateWrapper.set(ShippingInfo::getShippingCarNumber, req.getShippingCarNumber());
+            if (req.getShippingDate() != null) updateWrapper.set(ShippingInfo::getShippingDate, req.getShippingDate());
+        }
+        this.update(updateWrapper);
+
+        //  鏇存柊璁㈠崟鐘舵�佷负 4-宸插彂璐�
+        if (!Integer.valueOf(4).equals(salesLedger.getDeliveryStatus())) {
+            salesLedger.setDeliveryStatus(4);
+            salesLedgerMapper.updateById(salesLedger);
+        }
+
+        //  杩佺Щ褰撳墠璁板綍娑夊強鐨勬枃浠�
+        if (req != null && req.getId() != null && CollectionUtils.isNotEmpty(req.getTempFileIds())) {
             tempFileService.migrateTempFilesToFormal(req.getId(), req.getTempFileIds(), FileNameType.SHIP.getValue());
         }
-        return update ;
+
+        return true;
     }
 
     @Override
     public boolean delete(List<Long> ids) {
         List<ShippingInfo> shippingInfos = shippingInfoMapper.selectList(new LambdaQueryWrapper<ShippingInfo>()
                 .in(ShippingInfo::getId, ids));
+        if(CollectionUtils.isEmpty(shippingInfos)) return false;
         // 鍒犻櫎闄勪欢
         commonFileService.deleteByBusinessIds(ids, FileNameType.SHIP.getValue());
-        // 鎵e簱瀛�
-        for (Long id : ids) {
-            stockUtils.deleteStockRecord(id, StockQualifiedRecordTypeEnum.SALE_SHIP_STOCK_OUT.getCode());
+        // 鎵e凡鍙戣揣搴撳瓨
+        for (ShippingInfo shippingInfo : shippingInfos) {
+            if("宸插彂璐�".equals(shippingInfo.getStatus())) {
+                stockUtils.deleteStockOutRecord(shippingInfo.getId(), StockOutQualifiedRecordTypeEnum.SALE_SHIP_STOCK_OUT.getCode());
+            }
         }
         // 鍒犻櫎鍙戣揣瀹℃壒
         if(CollectionUtils.isNotEmpty(shippingInfos)){
@@ -101,4 +198,15 @@
 
         return this.removeBatchByIds(ids);
     }
+
+    @Override
+    public List<SalesLedgerProductDto> getReturnManagementDtoById(Long shippingId) {
+        return shippingInfoMapper.getReturnManagementDtoById(shippingId );
+
+    }
+
+    @Override
+    public List<ShippingInfo> getShippingInfoByCustomerName(String customerName) {
+        return shippingInfoMapper.getShippingInfoByCustomerName(customerName);
+    }
 }

--
Gitblit v1.9.3