From 6c5c8cd6a091ce6877402bb122920076e1d13309 Mon Sep 17 00:00:00 2001
From: gongchunyi <deslre0381@gmail.com>
Date: 星期六, 28 三月 2026 13:58:05 +0800
Subject: [PATCH] feat: 入库管理新增"缺货字段"和"缺货情况"字段

---
 src/main/java/com/ruoyi/basic/service/impl/CustomerServiceImpl.java |   95 +++++++++++++++++++++++++++++++++++++++++++----
 1 files changed, 87 insertions(+), 8 deletions(-)

diff --git a/src/main/java/com/ruoyi/basic/service/impl/CustomerServiceImpl.java b/src/main/java/com/ruoyi/basic/service/impl/CustomerServiceImpl.java
index 07431ac..137af19 100644
--- a/src/main/java/com/ruoyi/basic/service/impl/CustomerServiceImpl.java
+++ b/src/main/java/com/ruoyi/basic/service/impl/CustomerServiceImpl.java
@@ -2,6 +2,7 @@
 
 
 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.core.toolkit.Wrappers;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
@@ -13,16 +14,20 @@
 import com.ruoyi.common.utils.StringUtils;
 import com.ruoyi.common.utils.poi.ExcelUtil;
 import com.ruoyi.framework.security.LoginUser;
+import com.ruoyi.framework.web.domain.AjaxResult;
 import com.ruoyi.project.system.domain.SysUser;
+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 lombok.AllArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.stereotype.Service;
+import org.springframework.util.CollectionUtils;
 import org.springframework.web.multipart.MultipartFile;
 
-import java.util.Arrays;
-import java.util.List;
-import java.util.Map;
-import java.util.Objects;
+import java.math.BigDecimal;
+import java.util.*;
 import java.util.stream.Collectors;
 
 
@@ -36,6 +41,8 @@
 @AllArgsConstructor
 @Slf4j
 public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, Customer> implements ICustomerService {
+    private final SalesLedgerMapper salesLedgerMapper;
+    private final SalesLedgerProductMapper salesLedgerProductMapper;
     private CustomerMapper customerMapper;
 
     /**
@@ -83,6 +90,71 @@
                     String address = StringUtils.defaultString(c.getCompanyAddress(), "");
                     String phone = StringUtils.defaultString(c.getCompanyPhone(), "");
                     c.setAddressPhone(address + "(" + phone + ")"); // 浼樺寲瀛楃涓叉嫾鎺�
+                    // 鏌ヨ璇ュ鎴峰叧鑱旂殑閿�鍞彴璐︿腑锛岃喘涔版鏁般�佸钩鍧囬噾棰濄�佹渶杩戣喘涔版椂闂�
+                    List<SalesLedger> salesLedgers = salesLedgerMapper.selectList(new QueryWrapper<SalesLedger>().lambda().eq(SalesLedger::getCustomerId, c.getId()));
+                    if (!CollectionUtils.isEmpty(salesLedgers)) {
+                        // 璁$畻璐拱娆℃暟
+                        int purchaseCount = salesLedgers.size();
+                        c.setPurchaseCount(purchaseCount);
+
+                        // 璁$畻骞冲潎閲戦
+                        if (purchaseCount > 0) {
+                            BigDecimal totalAmount = salesLedgers.stream()
+                                    .map(SalesLedger::getContractAmount)
+                                    .filter(Objects::nonNull)
+                                    .reduce(BigDecimal.ZERO, BigDecimal::add);
+                            BigDecimal averageAmount = totalAmount.divide(BigDecimal.valueOf(purchaseCount), 2, BigDecimal.ROUND_HALF_UP);
+                            c.setAverageAmount(averageAmount);
+                        } else {
+                            c.setAverageAmount(BigDecimal.ZERO);
+                        }
+
+                        // 璁$畻鏈�杩戣喘涔版椂闂�
+                        SalesLedger latestLedger = salesLedgers.stream()
+                                .max((l1, l2) -> l1.getExecutionDate().compareTo(l2.getExecutionDate()))
+                                .orElse(null);
+                        if (latestLedger != null) {
+                            c.setLatestPurchaseTime(latestLedger.getExecutionDate());
+                        }
+
+                        // 璁$畻甯歌喘浜у搧锛坱op 3锛�
+                        List<SalesLedgerProduct> allProducts = new ArrayList<>();
+                        for (SalesLedger ledger : salesLedgers) {
+                            List<SalesLedgerProduct> products = salesLedgerProductMapper.selectList(
+                                    new QueryWrapper<SalesLedgerProduct>().lambda()
+                                            .eq(SalesLedgerProduct::getSalesLedgerId, ledger.getId())
+                            );
+                            if (!CollectionUtils.isEmpty(products)) {
+                                allProducts.addAll(products);
+                            }
+                        }
+
+                        if (!CollectionUtils.isEmpty(allProducts)) {
+                            // 鎸変骇鍝佺被鍒拰瑙勬牸鍨嬪彿鍒嗙粍锛岀粺璁¤喘涔版鏁�
+                            Map<String, Long> productCountMap = allProducts.stream()
+                                    .collect(Collectors.groupingBy(
+                                            p -> p.getProductCategory() + "-" + p.getSpecificationModel(),
+                                            Collectors.counting()
+                                    ));
+
+                            // 鎺掑簭骞跺彇鍓�3
+                            List<String> topProducts = productCountMap.entrySet().stream()
+                                    .sorted(Map.Entry.<String, Long>comparingByValue().reversed())
+                                    .limit(3)
+                                    .map(Map.Entry::getKey)
+                                    .collect(Collectors.toList());
+
+                            c.setTopProducts(topProducts);
+                        } else {
+                            c.setTopProducts(new ArrayList<>());
+                        }
+                    } else {
+                        // 娌℃湁閿�鍞褰曟椂璁剧疆榛樿鍊�
+                        c.setPurchaseCount(0);
+                        c.setAverageAmount(BigDecimal.ZERO);
+                        c.setLatestPurchaseTime(null);
+                        c.setTopProducts(new ArrayList<>());
+                    }
                 })
                 .collect(Collectors.toList());
 
@@ -129,6 +201,10 @@
     @Override
     public int deleteCustomerByIds(Long[] ids) {
         List<Long> idList = Arrays.asList(ids);
+        List<SalesLedger> salesLedgers = salesLedgerMapper.selectList(new QueryWrapper<SalesLedger>().lambda().in(SalesLedger::getCustomerId, idList));
+        if (!salesLedgers.isEmpty()) {
+            throw new RuntimeException("瀹㈡埛妗f涓嬫湁閿�鍞悎鍚岋紝璇峰厛鍒犻櫎閿�鍞悎鍚�");
+        }
         return customerMapper.deleteBatchIds(idList);
     }
 
@@ -145,17 +221,20 @@
     }
 
     @Override
-    public Boolean importData(MultipartFile file) {
+    public AjaxResult importData(MultipartFile file) {
         try {
             ExcelUtil<Customer> util = new ExcelUtil<Customer>(Customer.class);
             List<Customer> userList = util.importExcel(file.getInputStream());
-
+            if(CollectionUtils.isEmpty(userList)){
+                return AjaxResult.warn("妯℃澘閿欒鎴栧鍏ユ暟鎹负绌�");
+            }
             this.saveOrUpdateBatch(userList);
-            return true;
+            return AjaxResult.success(true);
         }catch (Exception e){
             e.printStackTrace();
+            return AjaxResult.error("瀵煎叆澶辫触");
         }
-        return false;
+
     }
 
     @Override

--
Gitblit v1.9.3