From c885344040aa322a1476a8720ca5751b9288c263 Mon Sep 17 00:00:00 2001
From: gongchunyi <deslre0381@gmail.com>
Date: 星期一, 15 六月 2026 22:09:15 +0800
Subject: [PATCH] fix: 供应商往来修改

---
 src/main/java/com/ruoyi/basic/service/impl/SupplierServiceImpl.java |  245 +++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 236 insertions(+), 9 deletions(-)

diff --git a/src/main/java/com/ruoyi/basic/service/impl/SupplierServiceImpl.java b/src/main/java/com/ruoyi/basic/service/impl/SupplierServiceImpl.java
index 613bde2..ec8bb26 100644
--- a/src/main/java/com/ruoyi/basic/service/impl/SupplierServiceImpl.java
+++ b/src/main/java/com/ruoyi/basic/service/impl/SupplierServiceImpl.java
@@ -16,32 +16,47 @@
 import com.ruoyi.purchase.pojo.PurchaseLedger;
 import com.ruoyi.purchase.vo.SupplierTransactionsDetailsVo;
 import com.ruoyi.purchase.vo.SupplierTransactionsVo;
+import com.ruoyi.quality.mapper.QualityInspectMapper;
+import com.ruoyi.quality.mapper.QualityUnqualifiedMapper;
+import com.ruoyi.quality.pojo.QualityInspect;
+import com.ruoyi.quality.pojo.QualityUnqualified;
+import com.ruoyi.sales.mapper.SalesLedgerProductMapper;
+import com.ruoyi.sales.pojo.SalesLedgerProduct;
+import com.ruoyi.stock.mapper.StockInRecordMapper;
+import com.ruoyi.stock.pojo.StockInRecord;
 import jakarta.servlet.http.HttpServletResponse;
 import lombok.RequiredArgsConstructor;
 import org.springframework.beans.BeanUtils;
 import org.springframework.stereotype.Service;
 import org.springframework.web.multipart.MultipartFile;
 
+import java.math.BigDecimal;
 import java.time.LocalDate;
 import java.util.ArrayList;
 import java.util.List;
+import java.util.stream.Collectors;
 
 @Service
 @RequiredArgsConstructor
-public class SupplierServiceImpl extends ServiceImpl<SupplierManageMapper,SupplierManage> implements ISupplierService {
+public class SupplierServiceImpl extends ServiceImpl<SupplierManageMapper, SupplierManage> implements ISupplierService {
 
     private final SupplierManageMapper supplierMapper;
     private final PurchaseLedgerMapper purchaseLedgerMapper;
+    private final SalesLedgerProductMapper salesLedgerProductMapper;
+    private final StockInRecordMapper stockInRecordMapper;
+    private final QualityInspectMapper qualityInspectMapper;
+    private final QualityUnqualifiedMapper qualityUnqualifiedMapper;
 
     /**
      * 渚涘簲鍟嗘柊澧�
+     *
      * @param supplierManage
      * @return
      */
     @Override
     public void saveSupplier(SupplierManage supplierManage) {
         LambdaQueryWrapper<SupplierManage> supplierManageLambdaQueryWrapper = new LambdaQueryWrapper<>();
-        supplierManageLambdaQueryWrapper.eq(SupplierManage::getSupplierName,supplierManage.getSupplierName());
+        supplierManageLambdaQueryWrapper.eq(SupplierManage::getSupplierName, supplierManage.getSupplierName());
         if (supplierMapper.selectCount(supplierManageLambdaQueryWrapper) > 0) {
             throw new RuntimeException("渚涘簲鍟嗗凡瀛樺湪");
         }
@@ -50,6 +65,7 @@
 
     /**
      * 渚涘簲鍟嗗垹闄�
+     *
      * @param ids
      * @return
      */
@@ -67,6 +83,7 @@
 
     /**
      * 渚涘簲鍟嗚鎯�
+     *
      * @param id
      * @return
      */
@@ -77,6 +94,7 @@
 
     /**
      * 渚涘簲鍟嗕慨鏀�
+     *
      * @param supplierManage
      * @return
      */
@@ -87,17 +105,19 @@
 
     /**
      * 渚涘簲鍟嗗垎椤垫煡璇�
+     *
      * @param page
      * @param supplierManageDto
      * @return
      */
     @Override
     public IPage<SupplierManage> supplierListPage(Page page, SupplierManageDto supplierManageDto) {
-        return supplierMapper.supplierListPage(page,supplierManageDto);
+        return supplierMapper.supplierListPage(page, supplierManageDto);
     }
 
     /**
      * 渚涘簲鍟嗗鍑�
+     *
      * @param response
      * @param supplierManageDto
      */
@@ -115,19 +135,19 @@
             ExcelUtil<SupplierManageExcelDto> util = new ExcelUtil<SupplierManageExcelDto>(SupplierManageExcelDto.class);
             List<SupplierManageExcelDto> list = util.importExcel(file.getInputStream());
             ArrayList<SupplierManage> supplierManages = new ArrayList<>();
-            list.stream().forEach(dto -> {
+            list.forEach(dto -> {
                 SupplierManage supplierManage = new SupplierManage();
-                BeanUtils.copyProperties(dto,supplierManage);
+                BeanUtils.copyProperties(dto, supplierManage);
                 supplierManage.setMaintainTime(LocalDate.now());
                 Long userId = SecurityUtils.getLoginUser().getUser().getUserId();
 
-                supplierManage.setMaintainUserId(Integer.parseInt(userId+""));
+                supplierManage.setMaintainUserId(Integer.parseInt(userId + ""));
                 supplierManages.add(supplierManage);
             });
 
             this.saveOrUpdateBatch(supplierManages);
             return true;
-        }catch (Exception e){
+        } catch (Exception e) {
             e.printStackTrace();
         }
         return false;
@@ -135,11 +155,218 @@
 
     @Override
     public IPage<SupplierTransactionsVo> supplierTransactions(Page page, String supplierName) {
-        return supplierMapper.supplierTransactions(page,supplierName);
+        IPage<SupplierTransactionsVo> plPage = supplierMapper.supplierTransactions(page, supplierName);
+        List<SupplierTransactionsVo> resultList = plPage.getRecords();
+        if (resultList.isEmpty()) {
+            return plPage;
+        }
+
+        List<Long> supplierIds = resultList.stream().map(SupplierTransactionsVo::getSupplierId).collect(Collectors.toList());
+
+        List<PurchaseLedger> purchaseLedgers = purchaseLedgerMapper.selectList(new LambdaQueryWrapper<PurchaseLedger>()
+                .in(PurchaseLedger::getSupplierId, supplierIds));
+
+        if (purchaseLedgers.isEmpty()) {
+            for (SupplierTransactionsVo vo : resultList) {
+                vo.setShippedAmount(BigDecimal.ZERO);
+                BigDecimal contractAmount = vo.getContractAmounts() == null ? BigDecimal.ZERO : vo.getContractAmounts();
+                vo.setUnshippedAmount(contractAmount);
+            }
+            return plPage;
+        }
+
+        List<Long> ledgerIds = purchaseLedgers.stream().map(PurchaseLedger::getId).collect(Collectors.toList());
+
+        List<SalesLedgerProduct> salesLedgerProducts = salesLedgerProductMapper.selectList(new LambdaQueryWrapper<SalesLedgerProduct>()
+                .in(SalesLedgerProduct::getSalesLedgerId, ledgerIds)
+                .eq(SalesLedgerProduct::getType, 2));
+
+        //  涓嶈川妫�鐩存帴鍏ュ簱鐨� - 7
+        List<StockInRecord> directInboundRecords = stockInRecordMapper.selectList(new LambdaQueryWrapper<StockInRecord>()
+                .eq(StockInRecord::getRecordType, "7")
+                .eq(StockInRecord::getApprovalStatus, 1)
+                .in(StockInRecord::getRecordId, ledgerIds));
+
+        //  閫氳繃璐ㄦ鍏ュ簱鐨�
+        List<QualityInspect> qualityInspects = qualityInspectMapper.selectList(new LambdaQueryWrapper<QualityInspect>()
+                .in(QualityInspect::getPurchaseLedgerId, ledgerIds));
+        List<Long> inspectIds = qualityInspects.stream().map(QualityInspect::getId).collect(Collectors.toList());
+
+        //  璐ㄦ鍚堟牸鍏ュ簱 - 10
+        List<StockInRecord> qualifiedInboundRecords = new ArrayList<>();
+        List<QualityUnqualified> qualityUnqualifieds = new ArrayList<>();
+        if (!inspectIds.isEmpty()) {
+            qualifiedInboundRecords = stockInRecordMapper.selectList(new LambdaQueryWrapper<StockInRecord>()
+                    .eq(StockInRecord::getRecordType, "10")
+                    .eq(StockInRecord::getApprovalStatus, 1)
+                    .in(StockInRecord::getRecordId, inspectIds));
+
+            qualityUnqualifieds = qualityUnqualifiedMapper.selectList(new LambdaQueryWrapper<QualityUnqualified>()
+                    .in(QualityUnqualified::getInspectId, inspectIds));
+        }
+        List<Long> unqualifiedIds = qualityUnqualifieds.stream().map(QualityUnqualified::getId).collect(Collectors.toList());
+
+        //  璐ㄦ涓嶅悎鏍煎叆搴� - 11
+        List<StockInRecord> unqualifiedInboundRecords = new ArrayList<>();
+        if (!unqualifiedIds.isEmpty()) {
+            unqualifiedInboundRecords = stockInRecordMapper.selectList(new LambdaQueryWrapper<StockInRecord>()
+                    .eq(StockInRecord::getRecordType, "11")
+                    .eq(StockInRecord::getApprovalStatus, 1)
+                    .in(StockInRecord::getRecordId, unqualifiedIds));
+        }
+
+        for (SupplierTransactionsVo vo : resultList) {
+            BigDecimal shippedAmount = BigDecimal.ZERO;
+
+            List<PurchaseLedger> currentLedgers = purchaseLedgers.stream().filter(pl -> pl.getSupplierId().equals(vo.getSupplierId())).toList();
+            for (PurchaseLedger ledger : currentLedgers) {
+                Long ledgerId = ledger.getId();
+
+                List<StockInRecord> currentDirectRecords = directInboundRecords.stream().filter(s -> s.getRecordId().equals(ledgerId)).toList();
+                for (StockInRecord sir : currentDirectRecords) {
+                    List<SalesLedgerProduct> currentProducts = salesLedgerProducts.stream().filter(s -> s.getSalesLedgerId().equals(ledgerId)).toList();
+                    for (SalesLedgerProduct slp : currentProducts) {
+                        if (sir.getStockInNum() != null && slp.getTaxInclusiveUnitPrice() != null) {
+                            shippedAmount = shippedAmount.add(sir.getStockInNum().multiply(slp.getTaxInclusiveUnitPrice()));
+                        }
+                    }
+                }
+
+                List<QualityInspect> currentInspects = qualityInspects.stream().filter(q -> q.getPurchaseLedgerId().equals(ledgerId)).toList();
+                for (QualityInspect inspect : currentInspects) {
+                    List<StockInRecord> currentQualifiedRecords = qualifiedInboundRecords.stream().filter(s -> s.getRecordId().equals(inspect.getId())).toList();
+                    for (StockInRecord sir : currentQualifiedRecords) {
+                        BigDecimal minPrice = getMinTaxInclusiveUnitPrice(salesLedgerProducts, ledgerId, sir.getProductModelId());
+                        if (sir.getStockInNum() != null && minPrice != null) {
+                            shippedAmount = shippedAmount.add(sir.getStockInNum().multiply(minPrice));
+                        }
+                    }
+
+                    List<QualityUnqualified> currentUnqualifieds = qualityUnqualifieds.stream().filter(q -> q.getInspectId().equals(inspect.getId())).toList();
+                    for (QualityUnqualified unqualified : currentUnqualifieds) {
+                        List<StockInRecord> currentUnqualifiedRecords = unqualifiedInboundRecords.stream().filter(s -> s.getRecordId().equals(unqualified.getId())).toList();
+                        for (StockInRecord sir : currentUnqualifiedRecords) {
+                            BigDecimal minPrice = getMinTaxInclusiveUnitPrice(salesLedgerProducts, ledgerId, sir.getProductModelId());
+                            if (sir.getStockInNum() != null && minPrice != null) {
+                                shippedAmount = shippedAmount.add(sir.getStockInNum().multiply(minPrice));
+                            }
+                        }
+                    }
+                }
+            }
+
+            BigDecimal contractAmount = vo.getContractAmounts() == null ? BigDecimal.ZERO : vo.getContractAmounts();
+            if (shippedAmount.subtract(contractAmount).compareTo(BigDecimal.ZERO) > 0) {
+                shippedAmount = contractAmount;
+            }
+            vo.setShippedAmount(shippedAmount);
+            vo.setUnshippedAmount(contractAmount.subtract(shippedAmount));
+        }
+
+        return plPage;
     }
 
     @Override
     public IPage<SupplierTransactionsDetailsVo> supplierTransactionsDetails(Page page, Long supplierId) {
-        return supplierMapper.supplierTransactionsDetails(page,supplierId);
+        IPage<SupplierTransactionsDetailsVo> plPage = supplierMapper.supplierTransactionsDetails(page, supplierId);
+
+        List<SupplierTransactionsDetailsVo> resultList = plPage.getRecords();
+        if (resultList.isEmpty()) {
+            return plPage;
+        }
+
+        List<Long> ledgerIds = resultList.stream().map(SupplierTransactionsDetailsVo::getPurchaseLedgerId).collect(Collectors.toList());
+
+        List<SalesLedgerProduct> salesLedgerProducts = salesLedgerProductMapper.selectList(new LambdaQueryWrapper<SalesLedgerProduct>()
+                .in(SalesLedgerProduct::getSalesLedgerId, ledgerIds)
+                .eq(SalesLedgerProduct::getType, 2));
+
+        //  涓嶈川妫�鐩存帴鍏ュ簱鐨� - 7
+        List<StockInRecord> directInboundRecords = stockInRecordMapper.selectList(new LambdaQueryWrapper<StockInRecord>()
+                .eq(StockInRecord::getRecordType, "7")
+                .eq(StockInRecord::getApprovalStatus, 1)
+                .in(StockInRecord::getRecordId, ledgerIds));
+
+        //  閫氳繃璐ㄦ鍏ュ簱鐨�
+        List<QualityInspect> qualityInspects = qualityInspectMapper.selectList(new LambdaQueryWrapper<QualityInspect>()
+                .in(QualityInspect::getPurchaseLedgerId, ledgerIds));
+        List<Long> inspectIds = qualityInspects.stream().map(QualityInspect::getId).collect(Collectors.toList());
+
+        //  璐ㄦ鍚堟牸鍏ュ簱 - 10
+        List<StockInRecord> qualifiedInboundRecords = new ArrayList<>();
+        List<QualityUnqualified> qualityUnqualifieds = new ArrayList<>();
+        if (!inspectIds.isEmpty()) {
+            qualifiedInboundRecords = stockInRecordMapper.selectList(new LambdaQueryWrapper<StockInRecord>()
+                    .eq(StockInRecord::getRecordType, "10")
+                    .eq(StockInRecord::getApprovalStatus, 1)
+                    .in(StockInRecord::getRecordId, inspectIds));
+
+            qualityUnqualifieds = qualityUnqualifiedMapper.selectList(new LambdaQueryWrapper<QualityUnqualified>()
+                    .in(QualityUnqualified::getInspectId, inspectIds));
+        }
+        List<Long> unqualifiedIds = qualityUnqualifieds.stream().map(QualityUnqualified::getId).collect(Collectors.toList());
+
+        //  璐ㄦ涓嶅悎鏍煎叆搴� - 11
+        List<StockInRecord> unqualifiedInboundRecords = new ArrayList<>();
+        if (!unqualifiedIds.isEmpty()) {
+            unqualifiedInboundRecords = stockInRecordMapper.selectList(new LambdaQueryWrapper<StockInRecord>()
+                    .eq(StockInRecord::getRecordType, "11")
+                    .eq(StockInRecord::getApprovalStatus, 1)
+                    .in(StockInRecord::getRecordId, unqualifiedIds));
+        }
+
+        for (SupplierTransactionsDetailsVo vo : resultList) {
+            BigDecimal shippedAmount = BigDecimal.ZERO;
+
+            List<StockInRecord> currentDirectRecords = directInboundRecords.stream().filter(s -> s.getRecordId().equals(vo.getPurchaseLedgerId())).toList();
+            for (StockInRecord sir : currentDirectRecords) {
+                List<SalesLedgerProduct> currentProducts = salesLedgerProducts.stream().filter(s -> s.getSalesLedgerId().equals(vo.getPurchaseLedgerId())).toList();
+                for (SalesLedgerProduct slp : currentProducts) {
+                    if (sir.getStockInNum() != null && slp.getTaxInclusiveUnitPrice() != null) {
+                        shippedAmount = shippedAmount.add(sir.getStockInNum().multiply(slp.getTaxInclusiveUnitPrice()));
+                    }
+                }
+            }
+
+            List<QualityInspect> currentInspects = qualityInspects.stream().filter(q -> q.getPurchaseLedgerId().equals(vo.getPurchaseLedgerId())).toList();
+            for (QualityInspect inspect : currentInspects) {
+                List<StockInRecord> currentQualifiedRecords = qualifiedInboundRecords.stream().filter(s -> s.getRecordId().equals(inspect.getId())).toList();
+                for (StockInRecord sir : currentQualifiedRecords) {
+                    BigDecimal minPrice = getMinTaxInclusiveUnitPrice(salesLedgerProducts, vo.getPurchaseLedgerId(), sir.getProductModelId());
+                    if (sir.getStockInNum() != null && minPrice != null) {
+                        shippedAmount = shippedAmount.add(sir.getStockInNum().multiply(minPrice));
+                    }
+                }
+
+                List<QualityUnqualified> currentUnqualifieds = qualityUnqualifieds.stream().filter(q -> q.getInspectId().equals(inspect.getId())).toList();
+                for (QualityUnqualified unqualified : currentUnqualifieds) {
+                    List<StockInRecord> currentUnqualifiedRecords = unqualifiedInboundRecords.stream().filter(s -> s.getRecordId().equals(unqualified.getId())).toList();
+                    for (StockInRecord sir : currentUnqualifiedRecords) {
+                        BigDecimal minPrice = getMinTaxInclusiveUnitPrice(salesLedgerProducts, vo.getPurchaseLedgerId(), sir.getProductModelId());
+                        if (sir.getStockInNum() != null && minPrice != null) {
+                            shippedAmount = shippedAmount.add(sir.getStockInNum().multiply(minPrice));
+                        }
+                    }
+                }
+            }
+
+            BigDecimal contractAmount = vo.getContractAmount() == null ? BigDecimal.ZERO : vo.getContractAmount();
+            if (shippedAmount.subtract(contractAmount).compareTo(BigDecimal.ZERO) > 0) {
+                shippedAmount = contractAmount;
+            }
+            vo.setShippedAmount(shippedAmount);
+            vo.setUnshippedAmount(contractAmount.subtract(shippedAmount));
+        }
+
+        return plPage;
+    }
+
+    private BigDecimal getMinTaxInclusiveUnitPrice(List<SalesLedgerProduct> allSlp, Long salesLedgerId, Long productModelId) {
+        return allSlp.stream()
+                .filter(s -> s.getSalesLedgerId().equals(salesLedgerId) && s.getProductModelId() != null && s.getProductModelId().equals(productModelId))
+                .map(SalesLedgerProduct::getTaxInclusiveUnitPrice)
+                .filter(java.util.Objects::nonNull)
+                .min(BigDecimal::compareTo)
+                .orElse(null);
     }
 }

--
Gitblit v1.9.3