From 0beb4e197c13ccce94a943e158993c503700f4da Mon Sep 17 00:00:00 2001
From: 云 <2163098428@qq.com>
Date: 星期四, 20 八月 2026 17:57:23 +0800
Subject: [PATCH] feat(sales): 实现发货模块部分发货与批量发货功能

---
 src/main/java/com/ruoyi/sales/service/impl/ShippingInfoServiceImpl.java |   81 +++++++++++++++++++++++++++++++++++++++-
 1 files changed, 79 insertions(+), 2 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 c9651a7..e7f40ed 100644
--- a/src/main/java/com/ruoyi/sales/service/impl/ShippingInfoServiceImpl.java
+++ b/src/main/java/com/ruoyi/sales/service/impl/ShippingInfoServiceImpl.java
@@ -4,10 +4,14 @@
 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.approve.bean.vo.ApproveProcessVO;
 import com.ruoyi.approve.pojo.ApproveProcess;
 import com.ruoyi.approve.service.impl.ApproveProcessServiceImpl;
 import com.ruoyi.common.enums.FileNameType;
 import com.ruoyi.common.enums.StockOutQualifiedRecordTypeEnum;
+import com.ruoyi.common.utils.OrderUtils;
+import com.ruoyi.common.utils.SecurityUtils;
+import com.ruoyi.framework.security.LoginUser;
 import com.ruoyi.other.service.impl.TempFileServiceImpl;
 import com.ruoyi.procurementrecord.utils.StockUtils;
 import com.ruoyi.sales.dto.SalesLedgerProductDto;
@@ -21,10 +25,15 @@
 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.math.BigDecimal;
+import java.time.LocalDate;
+import java.util.Arrays;
 import java.util.Collections;
 import java.util.List;
+import java.util.Objects;
 
 /**
  * @author :yys
@@ -64,10 +73,11 @@
         if (byId == null) {
             throw new RuntimeException("鍙戣揣淇℃伅涓嶅瓨鍦�");
         }
-        //鎵e噺搴撳瓨
+        //鎵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());
+            BigDecimal shipQuantity = byId.getQuantity() != null ? byId.getQuantity() : salesLedgerProduct.getQuantity();
+            stockUtils.substractStock(salesLedgerProduct.getProductModelId(), shipQuantity, StockOutQualifiedRecordTypeEnum.SALE_SHIP_STOCK_OUT.getCode(), req.getId());
         }
         byId.setExpressNumber(req.getExpressNumber());
         byId.setExpressCompany(req.getExpressCompany());
@@ -83,6 +93,73 @@
     }
 
     @Override
+    @Transactional(rollbackFor = Exception.class)
+    public boolean addShipping(ShippingInfoDto req) throws Exception {
+        // 鏍¢獙浜у搧琛�
+        SalesLedgerProduct product = salesLedgerProductMapper.selectById(req.getSalesLedgerProductId());
+        if (product == null) {
+            throw new RuntimeException("浜у搧鏄庣粏涓嶅瓨鍦�");
+        }
+        if (product.getQuantity() == null) {
+            throw new RuntimeException("浜у搧鏄庣粏鏁伴噺涓虹┖锛屾棤娉曞彂璐�");
+        }
+        // 鏈紶鏈鍙戣揣鏁伴噺鏃堕粯璁ゅ彇浜у搧琛屽叏閲�
+        if (req.getQuantity() == null) {
+            req.setQuantity(product.getQuantity());
+        }
+        validateQuantity(req.getSalesLedgerProductId(), req.getQuantity());
+        // 鍙戣揣瀹℃壒
+        LoginUser loginUser = SecurityUtils.getLoginUser();
+        String sh = OrderUtils.countTodayByCreateTime(shippingInfoMapper, "SH");
+        ApproveProcessVO approveProcessVO = new ApproveProcessVO();
+        approveProcessVO.setApproveType(7);
+        approveProcessVO.setApproveDeptId(loginUser.getCurrentDeptId());
+        approveProcessVO.setApproveReason(req.getType() + ":" + sh);
+        List<String> approveUserIds = req.getApproveUserIds();
+        approveProcessVO.setApproveUserIds(
+                approveUserIds == null || approveUserIds.isEmpty() ? "" : String.join(",", approveUserIds));
+        approveProcessVO.setApproveUser(loginUser.getUserId());
+        approveProcessVO.setApproveTime(LocalDate.now().toString());
+        approveProcessService.addApprove(approveProcessVO);
+        // 娣诲姞鍙戣揣鍗�
+        req.setShippingNo(sh);
+        req.setStatus("寰呭鏍�");
+        return this.save(req);
+    }
+
+    /**
+     * 鍓╀綑鍙彂鏁伴噺 = 浜у搧琛屾暟閲� - 鐘舵�佷负"瀹℃牳閫氳繃/宸插彂璐�"鐨勫彂璐ф暟閲忎箣鍜�
+     */
+    public BigDecimal getUnshippedQuantity(Long salesLedgerProductId) {
+        SalesLedgerProduct product = salesLedgerProductMapper.selectById(salesLedgerProductId);
+        if (product == null || product.getQuantity() == null) {
+            return BigDecimal.ZERO;
+        }
+        List<ShippingInfo> shippedList = shippingInfoMapper.selectList(new LambdaQueryWrapper<ShippingInfo>()
+                .eq(ShippingInfo::getSalesLedgerProductId, salesLedgerProductId)
+                .in(ShippingInfo::getStatus, Arrays.asList("瀹℃牳閫氳繃", "宸插彂璐�")));
+        BigDecimal shipped = shippedList.stream()
+                .map(ShippingInfo::getQuantity)
+                .filter(Objects::nonNull)
+                .reduce(BigDecimal.ZERO, BigDecimal::add);
+        BigDecimal remaining = product.getQuantity().subtract(shipped);
+        return remaining.compareTo(BigDecimal.ZERO) > 0 ? remaining : BigDecimal.ZERO;
+    }
+
+    /**
+     * 鏍¢獙鏈鍙戣揣鏁伴噺锛氬繀椤诲ぇ浜�0涓斾笉瓒呰繃鍓╀綑鍙彂鏁伴噺
+     */
+    private void validateQuantity(Long salesLedgerProductId, BigDecimal quantity) {
+        if (quantity == null || quantity.compareTo(BigDecimal.ZERO) <= 0) {
+            throw new RuntimeException("鏈鍙戣揣鏁伴噺蹇呴』澶т簬0");
+        }
+        BigDecimal remaining = getUnshippedQuantity(salesLedgerProductId);
+        if (quantity.compareTo(remaining) > 0) {
+            throw new RuntimeException("鏈鍙戣揣鏁伴噺瓒呰繃鍓╀綑鍙彂鏁伴噺锛堝墿浣欏彲鍙戯細" + remaining + "锛�");
+        }
+    }
+
+    @Override
     public boolean delete(List<Long> ids) {
         List<ShippingInfo> shippingInfos = shippingInfoMapper.selectList(new LambdaQueryWrapper<ShippingInfo>()
                 .in(ShippingInfo::getId, ids));

--
Gitblit v1.9.3