From 5c5544a07c683965b86a2bfd4c878503c4c73a48 Mon Sep 17 00:00:00 2001
From: 云 <2163098428@qq.com>
Date: 星期一, 30 三月 2026 18:08:26 +0800
Subject: [PATCH] yys 1.设备备件字段补全

---
 src/main/java/com/ruoyi/basic/service/impl/CustomerServiceImpl.java |  168 +++++++++++++++++++++++++++++++++++++++++++++++++------
 1 files changed, 148 insertions(+), 20 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 3c12a6f..8539492 100644
--- a/src/main/java/com/ruoyi/basic/service/impl/CustomerServiceImpl.java
+++ b/src/main/java/com/ruoyi/basic/service/impl/CustomerServiceImpl.java
@@ -2,22 +2,32 @@
 
 
 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;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
-
 import com.ruoyi.basic.mapper.CustomerMapper;
 import com.ruoyi.basic.pojo.Customer;
 import com.ruoyi.basic.service.ICustomerService;
 import com.ruoyi.common.utils.SecurityUtils;
 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.math.BigDecimal;
+import java.util.*;
 import java.util.stream.Collectors;
 
 
@@ -31,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;
 
     /**
@@ -51,19 +63,109 @@
      * @return 瀹㈡埛妗f
      */
     @Override
-    public List<Customer> selectCustomerList(Customer customer) {
-        LambdaQueryWrapper<Customer> queryWrapper = new LambdaQueryWrapper<>();
-
-        if (StringUtils.isNotBlank(customer.getCustomerName())) {
-            queryWrapper.eq(Customer::getCustomerName, customer.getCustomerName());
+    public IPage<Customer> selectCustomerList(Page page, Customer customer) {
+        // 1. 澶勭悊绌哄�煎満鏅紙鍙傛暟鏍¢獙锛�
+        if (page == null) {
+            page = Page.of(1, 10); // 榛樿绗�1椤碉紝姣忛〉10鏉℃暟鎹�
+        }
+        if (customer == null) {
+            customer = new Customer(); // 閬垮厤绌哄璞″鑷寸殑NPE
         }
 
-        List<Customer> customerList = customerMapper.selectList(queryWrapper);
+        // 2. 鏋勫缓鏌ヨ鏉′欢锛堝寮虹┖鍊煎畨鍏級
+        LambdaQueryWrapper<Customer> queryWrapper = new LambdaQueryWrapper<>();
+        String customerName = customer.getCustomerName();
+        String customerType = customer.getCustomerType();
+        if (StringUtils.isNotBlank(customerName)) {
+            queryWrapper.like(Customer::getCustomerName, customerName);
+        }
+        if (StringUtils.isNotBlank(customerType)) {
+            queryWrapper.like(Customer::getCustomerType, customerType);
+        }
 
-        // 浣跨敤 Stream 淇敼姣忎釜 Customer 鐨� addressPhone 瀛楁
-        return customerList.stream().peek(c ->
-                c.setAddressPhone(c.getCompanyAddress() + "( " + c.getCompanyPhone() + " )")
-        ).collect(Collectors.toList());
+        // 3. 鎵ц鍒嗛〉鏌ヨ锛堜繚鐣欏垎椤靛厓鏁版嵁锛�
+        IPage<Customer> customerPage = customerMapper.selectPage(page, queryWrapper);
+
+        // 4. 鏁版嵁澶勭悊锛堝寮虹┖鍊煎畨鍏� & 浠g爜鍙鎬э級
+        List<Customer> processedList = customerPage.getRecords().stream()
+                .filter(Objects::nonNull) // 杩囨护绌哄璞★紙閬垮厤鍚庣画鎿嶄綔NPE锛�
+                .peek(c -> {
+                    // 瀹夊叏鑾峰彇瀛楁锛岄伩鍏峮ull鍊兼嫾鎺�
+                    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());
+
+        // 5. 鏇存柊鍒嗛〉缁撴灉涓殑鏁版嵁锛堜繚鎸佸垎椤典俊鎭畬鏁达級
+        customerPage.setRecords(processedList);
+
+        return customerPage; // 杩斿洖鍖呭惈鍒嗛〉淇℃伅鐨処Page瀵硅薄
     }
 
     /**
@@ -75,8 +177,8 @@
     @Override
     public int insertCustomer(Customer customer) {
         LoginUser loginUser = SecurityUtils.getLoginUser();
-        Integer tenantId = loginUser.getTenantId();
-        customer.setTenantId(Long.valueOf(tenantId));
+        Long tenantId = loginUser.getTenantId();
+        customer.setTenantId(tenantId);
         return customerMapper.insert(customer);
     }
 
@@ -89,8 +191,8 @@
     @Override
     public int updateCustomer(Customer customer) {
         LoginUser loginUser = SecurityUtils.getLoginUser();
-        Integer tenantId = loginUser.getTenantId();
-        customer.setTenantId(Long.valueOf(tenantId));
+        Long tenantId = loginUser.getTenantId();
+        customer.setTenantId(tenantId);
         return customerMapper.updateById(customer);
     }
 
@@ -103,6 +205,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);
     }
 
@@ -114,9 +220,31 @@
     }
 
     @Override
+    public List<Customer> selectCustomerLists(Customer customer) {
+        return customerMapper.selectList(null);
+    }
+
+    @Override
+    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 AjaxResult.success(true);
+        }catch (Exception e){
+            e.printStackTrace();
+            return AjaxResult.error("瀵煎叆澶辫触");
+        }
+
+    }
+
+    @Override
     public List<Map<String, Object>> customerList(Customer customer) {
         LambdaQueryWrapper<Customer> queryWrapper = Wrappers.lambdaQuery();
-        queryWrapper.select(Customer::getId, Customer::getCustomerName,Customer::getTaxpayerIdentificationNumber);
+        queryWrapper.select(Customer::getId, Customer::getCustomerName, Customer::getTaxpayerIdentificationNumber);
 
         // 鑾峰彇鍘熷鏌ヨ缁撴灉
         List<Map<String, Object>> result = customerMapper.selectMaps(queryWrapper);
@@ -150,4 +278,4 @@
         }
         return sb.toString();
     }
-}
+}
\ No newline at end of file

--
Gitblit v1.9.3