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 |  321 ++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 312 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 b699e75..ec8bb26 100644
--- a/src/main/java/com/ruoyi/basic/service/impl/SupplierServiceImpl.java
+++ b/src/main/java/com/ruoyi/basic/service/impl/SupplierServiceImpl.java
@@ -1,43 +1,89 @@
 package com.ruoyi.basic.service.impl;
 
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 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.basic.dto.SupplierManageDto;
+import com.ruoyi.basic.excel.SupplierManageExcelDto;
 import com.ruoyi.basic.mapper.SupplierManageMapper;
 import com.ruoyi.basic.pojo.SupplierManage;
 import com.ruoyi.basic.service.ISupplierService;
-import org.springframework.beans.factory.annotation.Autowired;
+import com.ruoyi.common.utils.SecurityUtils;
+import com.ruoyi.common.utils.poi.ExcelUtil;
+import com.ruoyi.purchase.mapper.PurchaseLedgerMapper;
+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
-public class SupplierServiceImpl extends ServiceImpl<SupplierManageMapper,SupplierManage> implements ISupplierService {
+@RequiredArgsConstructor
+public class SupplierServiceImpl extends ServiceImpl<SupplierManageMapper, SupplierManage> implements ISupplierService {
 
-    @Autowired
-    private SupplierManageMapper supplierMapper;
+    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());
+        if (supplierMapper.selectCount(supplierManageLambdaQueryWrapper) > 0) {
+            throw new RuntimeException("渚涘簲鍟嗗凡瀛樺湪");
+        }
         supplierMapper.insert(supplierManage);
     }
 
     /**
      * 渚涘簲鍟嗗垹闄�
-     * @param id
+     *
+     * @param ids
      * @return
      */
     @Override
-    public int delSupplier(Integer id) {
-        return supplierMapper.deleteById(id);
+    public int delSupplier(List<Integer> ids) {
+        List<PurchaseLedger> purchaseLedgers = purchaseLedgerMapper.selectList(new QueryWrapper<PurchaseLedger>().lambda().in(PurchaseLedger::getSupplierId, ids));
+        if (purchaseLedgers.size() > 0) {
+            throw new RuntimeException("渚涘簲鍟嗗凡瀛樺湪閲囪喘鍚堝悓");
+        }
+        LambdaQueryWrapper<SupplierManage> delWrapper = new LambdaQueryWrapper<>();
+        delWrapper.in(SupplierManage::getId, ids);
+
+        return supplierMapper.delete(delWrapper);
     }
 
     /**
      * 渚涘簲鍟嗚鎯�
+     *
      * @param id
      * @return
      */
@@ -48,22 +94,279 @@
 
     /**
      * 渚涘簲鍟嗕慨鏀�
+     *
      * @param supplierManage
      * @return
      */
     @Override
-    public int supplierManage(SupplierManage supplierManage) {
+    public int supplierUpdate(SupplierManage supplierManage) {
         return supplierMapper.updateById(supplierManage);
     }
 
     /**
      * 渚涘簲鍟嗗垎椤垫煡璇�
+     *
      * @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
+     */
+    @Override
+    public void supplierExport(HttpServletResponse response, SupplierManageDto supplierManageDto) {
+        List<SupplierManageExcelDto> supplierManageList = supplierMapper.supplierExportList(supplierManageDto);
+        ExcelUtil<SupplierManageExcelDto> util = new ExcelUtil<SupplierManageExcelDto>(SupplierManageExcelDto.class);
+        util.exportExcel(response, supplierManageList, "渚涘簲鍟嗗鍑�");
+    }
+
+    @Override
+    public Boolean importData(MultipartFile file) {
+
+        try {
+            ExcelUtil<SupplierManageExcelDto> util = new ExcelUtil<SupplierManageExcelDto>(SupplierManageExcelDto.class);
+            List<SupplierManageExcelDto> list = util.importExcel(file.getInputStream());
+            ArrayList<SupplierManage> supplierManages = new ArrayList<>();
+            list.forEach(dto -> {
+                SupplierManage supplierManage = new SupplierManage();
+                BeanUtils.copyProperties(dto, supplierManage);
+                supplierManage.setMaintainTime(LocalDate.now());
+                Long userId = SecurityUtils.getLoginUser().getUser().getUserId();
+
+                supplierManage.setMaintainUserId(Integer.parseInt(userId + ""));
+                supplierManages.add(supplierManage);
+            });
+
+            this.saveOrUpdateBatch(supplierManages);
+            return true;
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+        return false;
+    }
+
+    @Override
+    public IPage<SupplierTransactionsVo> supplierTransactions(Page page, String 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) {
+        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