From acaf0e69a594f36ef9be6749c2b8d66e4ef6123b Mon Sep 17 00:00:00 2001
From: buhuazhen <hua100783@gmail.com>
Date: 星期五, 29 五月 2026 15:54:33 +0800
Subject: [PATCH] feat: 发货审批根据合同号合并到第一条(未审批)的记录中

---
 src/main/java/com/ruoyi/sales/service/impl/ShippingInfoServiceImpl.java |  160 +++++++++++++++++++++++++++++++++++++++++++++++++----
 1 files changed, 147 insertions(+), 13 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..f2b9555 100644
--- a/src/main/java/com/ruoyi/sales/service/impl/ShippingInfoServiceImpl.java
+++ b/src/main/java/com/ruoyi/sales/service/impl/ShippingInfoServiceImpl.java
@@ -7,23 +7,24 @@
 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.SalesLedgerProduct;
 import com.ruoyi.sales.pojo.ShippingInfo;
-import com.ruoyi.sales.service.ICommonFileService;
 import com.ruoyi.sales.service.ShippingInfoService;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.collections4.CollectionUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
 
 import java.io.IOException;
+import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
 
@@ -51,9 +52,11 @@
     private ApproveProcessServiceImpl approveProcessService;
 
     @Override
-    public IPage<ShippingInfo> listPage(Page page, ShippingInfo req) {
-        IPage<ShippingInfo> listPage = shippingInfoMapper.listPage(page, req);
-
+    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;
     }
 
@@ -63,14 +66,17 @@
         if (byId == null) {
             throw new RuntimeException("鍙戣揣淇℃伅涓嶅瓨鍦�");
         }
+        //鎵e噺搴撳瓨
+        if(!"宸插彂璐�".equals(byId.getStatus())){
+            SalesLedgerProduct salesLedgerProduct = salesLedgerProductMapper.selectById(byId.getSalesLedgerProductId());
+            stockUtils.substractStock(salesLedgerProduct.getProductModelId(), salesLedgerProduct.getQuantity(), StockOutQualifiedRecordTypeEnum.SALE_SHIP_STOCK_OUT.getCode(), req.getId());
+        }
         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());
+        byId.setShippingDate(req.getShippingDate());
+        boolean update = this.updateById(byId);
         // 杩佺Щ鏂囦欢
         if(CollectionUtils.isNotEmpty(req.getTempFileIds())){
             tempFileService.migrateTempFilesToFormal(req.getId(), req.getTempFileIds(), FileNameType.SHIP.getValue());
@@ -82,11 +88,14 @@
     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 +110,129 @@
 
         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);
+    }
+
+    /**
+     * 涓�閿彂璐� - 鑷姩瀹℃壒閫氳繃骞跺嚭搴�
+     * 涓嶅垱寤哄鎵规祦绋嬶紝鐩存帴鎵e噺搴撳瓨
+     */
+    @Override
+    @Transactional
+    public boolean oneClickShipping(ShippingInfoDto req) throws IOException {
+        // 鑾峰彇閿�鍞彴璐︿骇鍝佷俊鎭�
+        SalesLedgerProduct salesLedgerProduct = salesLedgerProductMapper.selectById(req.getSalesLedgerProductId());
+        if (salesLedgerProduct == null) {
+            throw new RuntimeException("閿�鍞骇鍝佷笉瀛樺湪");
+        }
+
+        // 鐢熸垚鍙戣揣鍗曞彿
+        String shippingNo = com.ruoyi.common.utils.OrderUtils.countTodayByCreateTime(shippingInfoMapper, "SH", "shipping_no");
+
+        // 鍒涘缓鍙戣揣璁板綍锛岀姸鎬佺洿鎺ヤ负"宸插彂璐�"
+        ShippingInfo shippingInfo = new ShippingInfo();
+        shippingInfo.setShippingNo(shippingNo);
+        shippingInfo.setSalesLedgerId(req.getSalesLedgerId());
+        shippingInfo.setSalesLedgerProductId(req.getSalesLedgerProductId());
+        shippingInfo.setType(req.getType());
+        shippingInfo.setShippingDate(req.getShippingDate());
+        shippingInfo.setShippingCarNumber(req.getShippingCarNumber());
+        shippingInfo.setExpressCompany(req.getExpressCompany());
+        shippingInfo.setExpressNumber(req.getExpressNumber());
+        shippingInfo.setStatus("宸插彂璐�");
+        boolean save = this.save(shippingInfo);
+
+        if (save) {
+            // 鐩存帴鎵e噺搴撳瓨
+            stockUtils.substractStock(
+                salesLedgerProduct.getProductModelId(),
+                salesLedgerProduct.getQuantity(),
+                StockOutQualifiedRecordTypeEnum.SALE_SHIP_STOCK_OUT.getCode(),
+                shippingInfo.getId()
+            );
+
+            // 杩佺Щ闄勪欢
+            if (CollectionUtils.isNotEmpty(req.getTempFileIds())) {
+                tempFileService.migrateTempFilesToFormal(shippingInfo.getId(), req.getTempFileIds(), FileNameType.SHIP.getValue());
+            }
+        }
+
+        return save;
+    }
+
+    /**
+     * 鎵归噺涓�閿彂璐� - 灏嗛攢鍞彴璐︿笅鎵�鏈夋湭鍙戣揣鐨勪骇鍝佸叏閮ㄥ彂璐�
+     */
+    @Override
+    @Transactional
+    public boolean batchOneClickShipping(Long salesLedgerId, ShippingInfoDto req) throws IOException {
+        // 鏌ヨ璇ラ攢鍞彴璐︿笅鎵�鏈変骇鍝�
+        List<SalesLedgerProduct> products = salesLedgerProductMapper.selectList(
+            new LambdaQueryWrapper<SalesLedgerProduct>()
+                .eq(SalesLedgerProduct::getSalesLedgerId, salesLedgerId)
+                .eq(SalesLedgerProduct::getType, 1) // 閿�鍞被鍨�
+        );
+
+        if (CollectionUtils.isEmpty(products)) {
+            throw new RuntimeException("璇ラ攢鍞彴璐︿笅娌℃湁浜у搧");
+        }
+
+        // 杩囨护鍑烘湭鍙戣揣鐨勪骇鍝侊紙娌℃湁鍙戣揣璁板綍鎴栧彂璐х姸鎬佷笉鏄�"宸插彂璐�"锛�
+        List<SalesLedgerProduct> unshippedProducts = new ArrayList<>();
+        for (SalesLedgerProduct product : products) {
+            ShippingInfo existingShipping = shippingInfoMapper.selectOne(
+                new LambdaQueryWrapper<ShippingInfo>()
+                    .eq(ShippingInfo::getSalesLedgerProductId, product.getId())
+                    .eq(ShippingInfo::getStatus, "宸插彂璐�")
+                    .last("limit 1")
+            );
+            if (existingShipping == null) {
+                unshippedProducts.add(product);
+            }
+        }
+
+        if (CollectionUtils.isEmpty(unshippedProducts)) {
+            throw new RuntimeException("璇ラ攢鍞彴璐︿笅鎵�鏈変骇鍝佸凡鍙戣揣");
+        }
+
+        // 涓烘瘡涓湭鍙戣揣鐨勪骇鍝佸垱寤哄彂璐ц褰�
+        for (SalesLedgerProduct product : unshippedProducts) {
+            // 鐢熸垚鍙戣揣鍗曞彿
+            String shippingNo = com.ruoyi.common.utils.OrderUtils.countTodayByCreateTime(shippingInfoMapper, "SH", "shipping_no");
+
+            // 鍒涘缓鍙戣揣璁板綍
+            ShippingInfo shippingInfo = new ShippingInfo();
+            shippingInfo.setShippingNo(shippingNo);
+            shippingInfo.setSalesLedgerId(salesLedgerId);
+            shippingInfo.setSalesLedgerProductId(product.getId());
+            shippingInfo.setType(req.getType());
+            shippingInfo.setShippingDate(req.getShippingDate());
+            shippingInfo.setShippingCarNumber(req.getShippingCarNumber());
+            shippingInfo.setExpressCompany(req.getExpressCompany());
+            shippingInfo.setExpressNumber(req.getExpressNumber());
+            shippingInfo.setStatus("宸插彂璐�");
+
+            boolean save = this.save(shippingInfo);
+            if (save) {
+                // 鎵e噺搴撳瓨
+                stockUtils.substractStock(
+                    product.getProductModelId(),
+                    product.getQuantity(),
+                    StockOutQualifiedRecordTypeEnum.SALE_SHIP_STOCK_OUT.getCode(),
+                    shippingInfo.getId()
+                );
+            }
+        }
+
+        return true;
+    }
 }

--
Gitblit v1.9.3