From a96948456e0a21ab33deeb2edfdd7ef343aec5bf Mon Sep 17 00:00:00 2001
From: huminmin <mac@MacBook-Pro.local>
Date: 星期六, 25 四月 2026 16:20:34 +0800
Subject: [PATCH] Merge branch 'dev_New_pro' of http://114.132.189.42:9002/r/product-inventory-management-after into dev_New_pro

---
 src/main/java/com/ruoyi/sales/service/impl/SalesLedgerProductServiceImpl.java |  258 +++++++++++++++++++++++++++++++++++++++++++++++----
 1 files changed, 236 insertions(+), 22 deletions(-)

diff --git a/src/main/java/com/ruoyi/sales/service/impl/SalesLedgerProductServiceImpl.java b/src/main/java/com/ruoyi/sales/service/impl/SalesLedgerProductServiceImpl.java
index 2a288e4..be2c03f 100644
--- a/src/main/java/com/ruoyi/sales/service/impl/SalesLedgerProductServiceImpl.java
+++ b/src/main/java/com/ruoyi/sales/service/impl/SalesLedgerProductServiceImpl.java
@@ -3,26 +3,48 @@
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+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.framework.web.domain.R;
+import com.ruoyi.procurementrecord.utils.StockUtils;
+import com.ruoyi.production.mapper.*;
+import com.ruoyi.production.pojo.ProductionPlan;
+import com.ruoyi.production.service.ProductionOrderService;
 import com.ruoyi.purchase.mapper.PurchaseLedgerMapper;
 import com.ruoyi.purchase.pojo.PurchaseLedger;
+import com.ruoyi.quality.mapper.QualityInspectMapper;
 import com.ruoyi.sales.dto.InvoiceRegistrationProductDto;
+import com.ruoyi.sales.dto.SalesLedgerProductDto;
 import com.ruoyi.sales.mapper.InvoiceRegistrationProductMapper;
 import com.ruoyi.sales.mapper.SalesLedgerMapper;
 import com.ruoyi.sales.mapper.SalesLedgerProductMapper;
-import com.ruoyi.sales.pojo.InvoiceRegistrationProduct;
+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.ISalesLedgerProductService;
-import lombok.AllArgsConstructor;
-import org.springframework.beans.factory.annotation.Autowired;
+import com.ruoyi.stock.mapper.StockInventoryMapper;
+import com.ruoyi.stock.pojo.StockInventory;
+import com.ruoyi.technology.bean.vo.TechnologyBomStructureVo;
+import com.ruoyi.technology.mapper.TechnologyBomStructureMapper;
+import com.ruoyi.technology.mapper.TechnologyRoutingMapper;
+import com.ruoyi.technology.pojo.TechnologyRouting;
+import lombok.RequiredArgsConstructor;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 import org.springframework.util.CollectionUtils;
 
 import java.lang.reflect.Field;
 import java.math.BigDecimal;
-import java.util.*;
+import java.math.RoundingMode;
+import java.time.LocalDate;
+import java.time.LocalDateTime;
+import java.time.format.DateTimeFormatter;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Objects;
+import java.util.Set;
 import java.util.function.Function;
 import java.util.stream.Collectors;
 
@@ -33,17 +55,27 @@
  * @date 2025-05-08
  */
 @Service
-@AllArgsConstructor
+@RequiredArgsConstructor
 public class SalesLedgerProductServiceImpl extends ServiceImpl<SalesLedgerProductMapper, SalesLedgerProduct> implements ISalesLedgerProductService {
 
-    private SalesLedgerProductMapper salesLedgerProductMapper;
-
-    private SalesLedgerMapper salesLedgerMapper;
-
-    private PurchaseLedgerMapper purchaseLedgerMapper;
-
-    @Autowired
-    private InvoiceRegistrationProductMapper invoiceRegistrationProductMapper;
+    private final SalesLedgerProductMapper salesLedgerProductMapper;
+    private final ProductionAccountMapper productionAccountMapper;
+    private final SalesLedgerMapper salesLedgerMapper;
+    private final PurchaseLedgerMapper purchaseLedgerMapper;
+    private final ProductionPlanMapper productionPlanMapper;
+    private final ProductionOperationTaskMapper productionOperationTaskMapper;
+    private final ProductionOrderService productionOrderService;
+    private final TechnologyRoutingMapper technologyRoutingMapper;
+    private final TechnologyBomStructureMapper technologyBomStructureMapper;
+    private final InvoiceRegistrationProductMapper invoiceRegistrationProductMapper;
+    private final ProductionProductMainMapper productionProductMainMapper;
+    private final ProductionProductOutputMapper productionProductOutputMapper;
+    private final ProductionProductInputMapper productionProductInputMapper;
+    private final QualityInspectMapper qualityInspectMapper;
+    private final ShippingInfoMapper shippingInfoMapper;
+    private final ShippingInfoServiceImpl shippingInfoService;
+    private final StockUtils stockUtils;
+    private final StockInventoryMapper stockInventoryMapper;
 
     @Override
     public SalesLedgerProduct selectSalesLedgerProductById(Long id) {
@@ -52,27 +84,50 @@
 
     @Override
     public List<SalesLedgerProduct> selectSalesLedgerProductList(SalesLedgerProduct salesLedgerProduct) {
-        LambdaQueryWrapper<SalesLedgerProduct> queryWrapper = new LambdaQueryWrapper<>();
-        queryWrapper.eq(SalesLedgerProduct::getSalesLedgerId, salesLedgerProduct.getSalesLedgerId())
-                .eq(SalesLedgerProduct::getType, salesLedgerProduct.getType());
-        List<SalesLedgerProduct> salesLedgerProducts = salesLedgerProductMapper.selectList(queryWrapper);
+//        LambdaQueryWrapper<SalesLedgerProduct> queryWrapper = new LambdaQueryWrapper<>();
+//        queryWrapper.eq(SalesLedgerProduct::getSalesLedgerId, salesLedgerProduct.getSalesLedgerId())
+//                .eq(SalesLedgerProduct::getType, salesLedgerProduct.getType());
+        List<SalesLedgerProduct> salesLedgerProducts = salesLedgerProductMapper.selectSalesLedgerProductList(salesLedgerProduct);
         if(!CollectionUtils.isEmpty(salesLedgerProducts)){
+            salesLedgerProducts.forEach(item -> {
+                // 鍙戣揣淇℃伅
+                ShippingInfo shippingInfo = shippingInfoMapper.selectOne(new LambdaQueryWrapper<ShippingInfo>()
+                        .eq(ShippingInfo::getSalesLedgerProductId, item.getId())
+                        .orderByDesc(ShippingInfo::getCreateTime)
+                        .last("limit 1"));
+                if(shippingInfo != null){
+                    item.setShippingDate(shippingInfo.getShippingDate());
+                    item.setShippingCarNumber(shippingInfo.getShippingCarNumber());
+                    item.setShippingStatus(shippingInfo.getStatus());
+                    item.setExpressCompany(shippingInfo.getExpressCompany());
+                    item.setExpressNumber(shippingInfo.getExpressNumber());
+                }
+            });
+            // 寮�绁�
             InvoiceRegistrationProductDto invoiceRegistrationProductDto = new InvoiceRegistrationProductDto();
             invoiceRegistrationProductDto.setSalesLedgerId(salesLedgerProduct.getSalesLedgerId().intValue());
             List<InvoiceRegistrationProductDto> invoiceRegistrationProductDtoList = invoiceRegistrationProductMapper.invoiceRegistrationProductList(invoiceRegistrationProductDto);
             // 缁熻寮�绁ㄧ櫥璁颁骇鍝佺殑宸插紑绁ㄦ暟/宸插紑绁ㄩ噾棰�
-            if(!CollectionUtils.isEmpty(invoiceRegistrationProductDtoList)){
+            if (!CollectionUtils.isEmpty(invoiceRegistrationProductDtoList)) {
                 for (SalesLedgerProduct ledgerProduct : salesLedgerProducts) {
                     BigDecimal invoiceNum = BigDecimal.ZERO;
                     BigDecimal invoiceAmount = BigDecimal.ZERO;
+                    BigDecimal noInvoiceNum = BigDecimal.ZERO;
+                    BigDecimal noInvoiceAmount = BigDecimal.ZERO;
                     for (InvoiceRegistrationProductDto registrationProductDto : invoiceRegistrationProductDtoList) {
                         if(ledgerProduct.getId().intValue() == registrationProductDto.getSalesLedgerProductId()){
                             invoiceNum =  invoiceNum.add(registrationProductDto.getInvoiceNum());
                             invoiceAmount = invoiceAmount.add(registrationProductDto.getInvoiceAmount());
                         }
                     }
+                    noInvoiceNum = ledgerProduct.getQuantity().subtract(invoiceNum);
+                    noInvoiceAmount = ledgerProduct.getTaxInclusiveTotalPrice().subtract(invoiceAmount);
                     ledgerProduct.setInvoiceNum(invoiceNum);
                     ledgerProduct.setInvoiceAmount(invoiceAmount);
+                    ledgerProduct.setNoInvoiceNum(noInvoiceNum);
+                    ledgerProduct.setNoInvoiceAmount(noInvoiceAmount);
+
+
                 }
             }
 
@@ -92,8 +147,14 @@
         if (deletedProducts.isEmpty()) {
             return 0; // 娌℃湁鍙垹闄ょ殑鏁版嵁
         }
+        //鍒犻櫎鍙戣揣淇℃伅
+        List<ShippingInfo> shippingInfos = shippingInfoMapper.selectList(new LambdaQueryWrapper<ShippingInfo>()
+                .in(ShippingInfo::getSalesLedgerProductId, Arrays.asList(ids)));
+        if(!CollectionUtils.isEmpty(shippingInfos)){
+            shippingInfoService.delete(shippingInfos.stream().map(ShippingInfo::getId).collect(Collectors.toList()));
+        }
 
-        // 鍙兘灞炰簬澶氫釜涓昏〃锛堜絾閫氬父涓�涓帴鍙e彧澶勭悊涓�涓富琛級
+        // 鍙兘灞炰簬澶氫釜涓昏〃
         Set<Long> mainIds = deletedProducts.stream()
                 .map(SalesLedgerProduct::getSalesLedgerId)
                 .filter(Objects::nonNull)
@@ -101,6 +162,8 @@
 
         // 2. 鎵ц鍒犻櫎鎿嶄綔
         int result = salesLedgerProductMapper.deleteBatchIds(Arrays.asList(ids));
+        //鍒犻櫎瀵瑰簲鐨勭敓浜ц鍗�
+        deleteProductionData(Arrays.asList(ids));
 
         // 3. 瀵规瘡涓富琛↖D杩涜閲戦鏇存柊
         for (Long salesLedgerId : mainIds) {
@@ -123,16 +186,33 @@
     @Override
     @Transactional(rollbackFor = Exception.class)
     public int addOrUpdateSalesLedgerProduct(SalesLedgerProduct salesLedgerProduct) {
-        //鏈紑绁ㄦ暟閲�+閲戦
-        salesLedgerProduct.setNoInvoiceNum(salesLedgerProduct.getQuantity());
-        salesLedgerProduct.setNoInvoiceAmount(salesLedgerProduct.getTaxInclusiveTotalPrice());
+        // 寰呭洖娆撅紝浠樻
+        if(salesLedgerProduct.getType().equals(1)){
+            salesLedgerProduct.setPendingInvoiceTotal(salesLedgerProduct.getTaxInclusiveTotalPrice().subtract(salesLedgerProduct.getInvoiceTotal()));
+            //鏈紑绁ㄦ暟閲�+閲戦
+            salesLedgerProduct.setNoInvoiceNum(salesLedgerProduct.getQuantity());
+            salesLedgerProduct.setNoInvoiceAmount(salesLedgerProduct.getTaxInclusiveTotalPrice());
+        }else{
+            salesLedgerProduct.setPendingTicketsTotal(salesLedgerProduct.getTaxInclusiveTotalPrice().subtract(salesLedgerProduct.getTicketsTotal()));
+            // 鏈潵绁ㄦ暟閲�+閲戦
+            salesLedgerProduct.setFutureTickets(salesLedgerProduct.getQuantity());
+            salesLedgerProduct.setFutureTicketsAmount(salesLedgerProduct.getTaxInclusiveTotalPrice());
+        }
+
         int result;
         Long salesLedgerId = salesLedgerProduct.getSalesLedgerId();
         if (salesLedgerProduct.getId() == null) {
+            salesLedgerProduct.setRegisterDate(LocalDateTime.now());
             result = salesLedgerProductMapper.insert(salesLedgerProduct);
+            addProductionData(salesLedgerProduct);
         } else {
+            //鏌ヨ鍘熸湰鐨勪骇鍝佸瀷鍙穒d
             salesLedgerProduct.setFutureTickets(salesLedgerProduct.getQuantity());
             result = salesLedgerProductMapper.updateById(salesLedgerProduct);
+            /*鍒犻櫎瀵瑰簲鐨勭敓浜ф暟鎹苟閲嶆柊鏂板*/
+            deleteProductionData(Arrays.asList(salesLedgerProduct.getId()));
+
+            addProductionData(salesLedgerProduct);
         }
 
         // 濡傛灉鎻掑叆鎴栨洿鏂版垚鍔燂紝骞朵笖鏈� salesLedgerId锛屾墠缁х画鏇存柊涓昏〃閲戦
@@ -163,6 +243,75 @@
             }
         }
         return result;
+    }
+
+    /**
+     * 鏂板鐢熶骇鏁版嵁
+     */
+    public void addProductionData(SalesLedgerProduct salesLedgerProduct) {
+        //鍏堝垽鏂浜у搧鏄惁闇�瑕佺敓浜�
+        if (!salesLedgerProduct.getIsProduction()) {
+            return;
+        }
+        SalesLedger salesLedger = salesLedgerMapper.selectById(salesLedgerProduct.getSalesLedgerId());
+        ProductionPlan productionPlan = new ProductionPlan();
+        productionPlan.setSalesLedgerId(salesLedgerProduct.getSalesLedgerId());
+        productionPlan.setSalesLedgerProductId(salesLedgerProduct.getId());
+        productionPlan.setMpsNo(generateNextPlanNo(LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"))));
+        productionPlan.setProductModelId(salesLedgerProduct.getProductModelId());
+        productionPlan.setQtyRequired(salesLedgerProduct.getQuantity());
+        productionPlan.setSource("閿�鍞�");
+        productionPlan.setStatus(0);
+        productionPlan.setRequiredDate(salesLedger.getDeliveryDate());//闇�姹傛棩鏈�=浜よ揣鏃ユ湡
+        productionPlan.setPromisedDeliveryDate(salesLedger.getDeliveryDate());//鎵胯鏃ユ湡=浜よ揣鏃ユ湡
+        productionPlanMapper.insert(productionPlan);
+
+    }
+
+    /**
+     * 鍒犻櫎鐢熶骇璁″垝
+     */
+    public void deleteProductionData(List<Long> productIds) {
+        List<ProductionPlan> productionPlans = productionPlanMapper.selectList(
+                new LambdaQueryWrapper<ProductionPlan>()
+                        .in(ProductionPlan::getSalesLedgerProductId, productIds.stream().map(Long::intValue).collect(Collectors.toList())));
+        if (CollectionUtils.isEmpty(productionPlans)) {
+            return;
+        }
+        //濡傛灉鐢熶骇璁″垝宸蹭笅鍙戝垯涓嶈兘鍒犻櫎
+        if (productionPlans.stream().anyMatch(productionPlan -> productionPlan.getStatus() != 0)) {
+            throw new RuntimeException("鐢熶骇璁″垝宸蹭笅鍙戯紝涓嶈兘鍒犻櫎璇ラ攢鍞骇鍝�");
+        }
+        List<Long> ids = productionPlans.stream().map(ProductionPlan::getId).collect(Collectors.toList());
+        productionPlanMapper.deleteByIds(ids);
+    }
+
+    @Override
+    public IPage<SalesLedgerProductDto> listPage(Page page, SalesLedgerProductDto salesLedgerProduct) {
+        IPage<SalesLedgerProductDto> salesLedgerProductDtoIPage = salesLedgerProductMapper.listPage(page, salesLedgerProduct);
+        salesLedgerProductDtoIPage.getRecords().forEach(item -> {
+            // 鍒ゆ柇鐘舵��
+            if(item.getTaxInclusiveTotalPrice().compareTo(item.getInvoiceTotal()) == 0){
+                item.setStatusName("宸插畬鎴愪粯娆�");
+            }else{
+                item.setStatusName("鏈畬鎴愪粯娆�");
+            }
+        });
+        return salesLedgerProductDtoIPage;
+    }
+
+    @Override
+    public IPage<SalesLedgerProductDto> listPagePurchaseLedger(Page page, SalesLedgerProductDto salesLedgerProduct) {
+        IPage<SalesLedgerProductDto> salesLedgerProductDtoIPage = salesLedgerProductMapper.listPagePurchaseLedger(page, salesLedgerProduct);
+        salesLedgerProductDtoIPage.getRecords().forEach(item -> {
+            // 鍒ゆ柇鐘舵��
+            if(item.getTaxInclusiveTotalPrice().compareTo(item.getTicketsTotal()) == 0){
+                item.setStatusName("宸插畬鎴愪粯娆�");
+            }else{
+                item.setStatusName("鏈畬鎴愪粯娆�");
+            }
+        });
+        return salesLedgerProductDtoIPage;
     }
 
     /**
@@ -199,4 +348,69 @@
             throw new RuntimeException("鍔ㄦ�佹洿鏂颁富琛ㄩ噾棰濆け璐�", e);
         }
     }
+    @Override
+    public R judgmentInventory(SalesLedgerProduct salesLedgerProduct) {
+        TechnologyRouting routing = technologyRoutingMapper.selectOne(
+                new QueryWrapper<TechnologyRouting>().lambda()
+                        .eq(TechnologyRouting::getProductModelId, salesLedgerProduct.getProductModelId())
+                        .orderByDesc(TechnologyRouting::getCreateTime)
+                        .last("LIMIT 1"));
+        if (routing == null) {
+            return R.fail("璇峰厛璁剧疆宸ヨ壓璺嚎");
+        }
+        List<TechnologyBomStructureVo> structureList = technologyBomStructureMapper.listByBomId(routing.getBomId().longValue());
+        if (structureList == null || structureList.isEmpty()) {
+            return R.fail("璇峰厛璁剧疆浜у搧缁撴瀯");
+        }
+        int count = 0;
+        StringBuilder stringBuffer = new StringBuilder();
+        for (TechnologyBomStructureVo structure : structureList) {
+            if (structure.getParentId() == null) {
+                continue;
+            }
+            StockInventory stockInventory = stockInventoryMapper.selectOne(
+                    new QueryWrapper<StockInventory>().lambda().eq(StockInventory::getProductModelId, structure.getProductModelId()));
+            if (stockInventory == null) {
+                count++;
+                stringBuffer.append(structure.getProductName()).append("-").append(structure.getModel()).append("搴撳瓨涓嶈冻").append(System.lineSeparator());
+                continue;
+            }
+            BigDecimal required = salesLedgerProduct.getQuantity().multiply(structure.getUnitQuantity() == null ? BigDecimal.ZERO : structure.getUnitQuantity());
+            BigDecimal remain = stockInventory.getQualitity()
+                    .subtract(stockInventory.getLockedQuantity())
+                    .subtract(required)
+                    .divide(BigDecimal.ONE, 2, RoundingMode.CEILING);
+            if (remain.compareTo(BigDecimal.ZERO) < 0) {
+                count++;
+                stringBuffer.append(structure.getProductName())
+                        .append("-")
+                        .append(structure.getModel())
+                        .append("搴撳瓨涓嶈冻锛屽皯")
+                        .append(remain.abs())
+                        .append(System.lineSeparator());
+            }
+        }
+        if (count > 0) {
+            return R.fail(stringBuffer.toString());
+        }
+        return R.ok();
+    }
+
+    private String generateNextPlanNo(String datePrefix) {
+        QueryWrapper<ProductionPlan> queryWrapper = new QueryWrapper<>();
+        queryWrapper.likeRight("mps_no", "JH" + datePrefix);
+        queryWrapper.orderByDesc("mps_no");
+        queryWrapper.last("LIMIT 1");
+        ProductionPlan latestPlan = productionPlanMapper.selectOne(queryWrapper);
+        int sequence = 1;
+        if (latestPlan != null && latestPlan.getMpsNo() != null && !latestPlan.getMpsNo().isEmpty()) {
+            String sequenceStr = latestPlan.getMpsNo().substring(("JH" + datePrefix).length());
+            try {
+                sequence = Integer.parseInt(sequenceStr) + 1;
+            } catch (NumberFormatException e) {
+                sequence = 1;
+            }
+        }
+        return "JH" + datePrefix + String.format("%04d", sequence);
+    }
 }

--
Gitblit v1.9.3