From 3bd356cdf09bc2e67dfe0ce9e6dc1b4dfe0e1bec Mon Sep 17 00:00:00 2001
From: liyong <18434998025@163.com>
Date: 星期二, 20 一月 2026 13:16:14 +0800
Subject: [PATCH] feat(production): 新增入库添加产品规格型号id,销售台账添加产品校验库存是否充足

---
 src/main/java/com/ruoyi/sales/service/impl/SalesLedgerProductServiceImpl.java |   97 ++++++++++++++++++++++++++++++++++++++++++++----
 1 files changed, 89 insertions(+), 8 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 b34d193..d0e7ea8 100644
--- a/src/main/java/com/ruoyi/sales/service/impl/SalesLedgerProductServiceImpl.java
+++ b/src/main/java/com/ruoyi/sales/service/impl/SalesLedgerProductServiceImpl.java
@@ -1,25 +1,34 @@
 package com.ruoyi.sales.service.impl;
 
 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.extension.service.impl.ServiceImpl;
+import com.ruoyi.framework.web.domain.R;
+import com.ruoyi.procurementrecord.utils.StockUtils;
+import com.ruoyi.production.dto.ProductStructureDto;
+import com.ruoyi.production.mapper.ProcessRouteMapper;
+import com.ruoyi.production.mapper.ProductBomMapper;
+import com.ruoyi.production.mapper.ProductStructureMapper;
+import com.ruoyi.production.pojo.ProcessRoute;
 import com.ruoyi.purchase.mapper.PurchaseLedgerMapper;
 import com.ruoyi.purchase.pojo.PurchaseLedger;
+import com.ruoyi.sales.dto.InvoiceRegistrationProductDto;
+import com.ruoyi.sales.mapper.InvoiceRegistrationProductMapper;
 import com.ruoyi.sales.mapper.SalesLedgerMapper;
 import com.ruoyi.sales.mapper.SalesLedgerProductMapper;
 import com.ruoyi.sales.pojo.SalesLedger;
 import com.ruoyi.sales.pojo.SalesLedgerProduct;
 import com.ruoyi.sales.service.ISalesLedgerProductService;
 import lombok.AllArgsConstructor;
+import org.springframework.beans.factory.annotation.Autowired;
 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.Arrays;
-import java.util.List;
-import java.util.Objects;
-import java.util.Set;
+import java.util.*;
 import java.util.function.Function;
 import java.util.stream.Collectors;
 
@@ -38,6 +47,17 @@
     private SalesLedgerMapper salesLedgerMapper;
 
     private PurchaseLedgerMapper purchaseLedgerMapper;
+    private StockUtils stockUtils;
+
+    @Autowired
+    private InvoiceRegistrationProductMapper invoiceRegistrationProductMapper;
+
+    @Autowired
+    private ProcessRouteMapper processRouteMapper;
+    @Autowired
+    private ProductBomMapper productBomMapper;
+    @Autowired
+    private ProductStructureMapper productStructureMapper;
 
     @Override
     public SalesLedgerProduct selectSalesLedgerProductById(Long id) {
@@ -46,10 +66,32 @@
 
     @Override
     public List<SalesLedgerProduct> selectSalesLedgerProductList(SalesLedgerProduct salesLedgerProduct) {
-        LambdaQueryWrapper<SalesLedgerProduct> queryWrapper = new LambdaQueryWrapper<>();
-        queryWrapper.eq(SalesLedgerProduct::getSalesLedgerId, salesLedgerProduct.getSalesLedgerId())
-                .eq(SalesLedgerProduct::getType, salesLedgerProduct.getType());
-        return 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)) {
+            InvoiceRegistrationProductDto invoiceRegistrationProductDto = new InvoiceRegistrationProductDto();
+            invoiceRegistrationProductDto.setSalesLedgerId(salesLedgerProduct.getSalesLedgerId().intValue());
+            List<InvoiceRegistrationProductDto> invoiceRegistrationProductDtoList = invoiceRegistrationProductMapper.invoiceRegistrationProductList(invoiceRegistrationProductDto);
+            // 缁熻寮�绁ㄧ櫥璁颁骇鍝佺殑宸插紑绁ㄦ暟/宸插紑绁ㄩ噾棰�
+            if (!CollectionUtils.isEmpty(invoiceRegistrationProductDtoList)) {
+                for (SalesLedgerProduct ledgerProduct : salesLedgerProducts) {
+                    BigDecimal invoiceNum = BigDecimal.ZERO;
+                    BigDecimal invoiceAmount = BigDecimal.ZERO;
+                    for (InvoiceRegistrationProductDto registrationProductDto : invoiceRegistrationProductDtoList) {
+                        if (ledgerProduct.getId().intValue() == registrationProductDto.getSalesLedgerProductId()) {
+                            invoiceNum = invoiceNum.add(registrationProductDto.getInvoiceNum());
+                            invoiceAmount = invoiceAmount.add(registrationProductDto.getInvoiceAmount());
+                        }
+                    }
+                    ledgerProduct.setInvoiceNum(invoiceNum);
+                    ledgerProduct.setInvoiceAmount(invoiceAmount);
+                }
+            }
+
+        }
+        return salesLedgerProducts;
     }
 
     @Override
@@ -95,11 +137,15 @@
     @Override
     @Transactional(rollbackFor = Exception.class)
     public int addOrUpdateSalesLedgerProduct(SalesLedgerProduct salesLedgerProduct) {
+        //鏈紑绁ㄦ暟閲�+閲戦
+        salesLedgerProduct.setNoInvoiceNum(salesLedgerProduct.getQuantity());
+        salesLedgerProduct.setNoInvoiceAmount(salesLedgerProduct.getTaxInclusiveTotalPrice());
         int result;
         Long salesLedgerId = salesLedgerProduct.getSalesLedgerId();
         if (salesLedgerProduct.getId() == null) {
             result = salesLedgerProductMapper.insert(salesLedgerProduct);
         } else {
+            salesLedgerProduct.setFutureTickets(salesLedgerProduct.getQuantity());
             result = salesLedgerProductMapper.updateById(salesLedgerProduct);
         }
 
@@ -133,6 +179,41 @@
         return result;
     }
 
+    @Override
+    public R judgmentInventory(SalesLedgerProduct salesLedgerProduct) {
+        //鑾峰彇浜у搧鏈�鏂扮殑宸ヨ壓璺嚎
+        ProcessRoute processRoute = processRouteMapper.selectOne(new QueryWrapper<ProcessRoute>().lambda().eq(ProcessRoute::getProductModelId, salesLedgerProduct.getProductModelId()).orderByDesc(ProcessRoute::getCreateTime).last("LIMIT 1"));
+        if (processRoute == null) {
+            return R.fail("璇峰厛璁剧疆宸ヨ壓璺嚎");
+        }
+        List<ProductStructureDto> productStructureDtos = productStructureMapper.listBybomId(processRoute.getBomId());
+        if (productStructureDtos.isEmpty()) {
+            return R.fail("璇峰厛璁剧疆浜у搧缁撴瀯");
+        }
+        int count = 0;
+        StringBuilder stringBuffer = new StringBuilder();
+        for (ProductStructureDto productStructureDto : productStructureDtos) {
+            BigDecimal stockQuantity = stockUtils.getStockQuantity(productStructureDto.getProductModelId()).get("stockQuantity");
+            //鎵�闇�鏁伴噺
+            BigDecimal multiply = salesLedgerProduct.getQuantity().multiply(productStructureDto.getUnitQuantity());
+            BigDecimal subtract =stockQuantity.subtract(multiply);
+            if (subtract.compareTo(BigDecimal.ZERO) <= 0) {
+                count++;
+                stringBuffer.append(productStructureDto.getProductName())
+                        .append("-")
+                        .append(productStructureDto.getModel())
+                        .append("搴撳瓨涓嶈冻锛屽皯")
+                        .append(subtract)
+                        .append(System.lineSeparator());
+            }
+        }
+        if (count>0) {
+            return R.fail(stringBuffer.toString());
+        }else {
+            return R.ok();
+        }
+    }
+
     /**
      * 閫氱敤鏂规硶锛氭牴鎹富琛↖D鍜屽瓙琛ㄥ垪琛紝鏇存柊涓昏〃鐨勫悎鍚岄噾棰�
      */

--
Gitblit v1.9.3