From 8b68e5a8d8d43bcb45c9032c3eb893a2b8995acf Mon Sep 17 00:00:00 2001
From: liyong <18434998025@163.com>
Date: 星期五, 17 四月 2026 16:18:51 +0800
Subject: [PATCH] fix(customer): 修复客户私海相关查询和导入功能

---
 src/main/java/com/ruoyi/basic/service/impl/CustomerServiceImpl.java |  147 ++++++++++++++++++------------------------------
 1 files changed, 56 insertions(+), 91 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 58810c9..0f7b58d 100644
--- a/src/main/java/com/ruoyi/basic/service/impl/CustomerServiceImpl.java
+++ b/src/main/java/com/ruoyi/basic/service/impl/CustomerServiceImpl.java
@@ -1,8 +1,6 @@
 package com.ruoyi.basic.service.impl;
 
 
-import cn.hutool.core.date.DateUtil;
-import cn.hutool.core.date.LocalDateTimeUtil;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.core.metadata.IPage;
@@ -11,10 +9,13 @@
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.ruoyi.basic.dto.CustomerDto;
 import com.ruoyi.basic.dto.CustomerFollowUpDto;
+import com.ruoyi.basic.dto.CustomerPrivatePoolDto;
 import com.ruoyi.basic.mapper.CustomerMapper;
+import com.ruoyi.basic.mapper.CustomerPrivatePoolMapper;
 import com.ruoyi.basic.pojo.Customer;
 import com.ruoyi.basic.pojo.CustomerFollowUp;
 import com.ruoyi.basic.pojo.CustomerFollowUpFile;
+import com.ruoyi.basic.pojo.CustomerPrivatePool;
 import com.ruoyi.basic.service.CustomerFollowUpFileService;
 import com.ruoyi.basic.service.CustomerFollowUpService;
 import com.ruoyi.basic.service.CustomerReturnVisitService;
@@ -30,6 +31,7 @@
 import lombok.AllArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.BeanUtils;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 import org.springframework.util.CollectionUtils;
@@ -51,13 +53,18 @@
 @AllArgsConstructor
 @Slf4j
 public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, Customer> implements ICustomerService {
-    private final SalesLedgerMapper salesLedgerMapper;
+    @Autowired
+    private  SalesLedgerMapper salesLedgerMapper;
+    @Autowired
+    private CustomerPrivatePoolMapper customerPrivatePoolMapper;
+    @Autowired
     private CustomerMapper customerMapper;
 
+    @Autowired
     private CustomerFollowUpService customerFollowUpService;
-
+    @Autowired
     private CustomerFollowUpFileService customerFollowUpFileService;
-
+    @Autowired
     private CustomerReturnVisitService customerReturnVisitService;
 
     /**
@@ -78,41 +85,8 @@
      * @return 瀹㈡埛璇︽儏DTO
      */
     @Override
-    public CustomerDto selectCustomerDetailById(Long id) {
-        Customer customer = customerMapper.selectById(id);
-        if (customer == null) {
-            return null;
-        }
-
-        CustomerDto dto = new CustomerDto();
-        BeanUtils.copyProperties(customer, dto);
-
-        // 鏌ヨ璺熻繘璁板綍
-        List<CustomerFollowUp> followUpList = customerFollowUpService.list(
-                new LambdaQueryWrapper<CustomerFollowUp>()
-                        .eq(CustomerFollowUp::getCustomerId, id)
-                        .orderByDesc(CustomerFollowUp::getFollowUpTime)
-        );
-
-        if (!CollectionUtils.isEmpty(followUpList)) {
-            List<CustomerFollowUpDto> followUpDtoList = followUpList.stream().map(followUp -> {
-                CustomerFollowUpDto followUpDto = new CustomerFollowUpDto();
-                BeanUtils.copyProperties(followUp, followUpDto);
-
-                // 鏌ヨ闄勪欢
-                List<CustomerFollowUpFile> fileList = customerFollowUpFileService.list(
-                        new LambdaQueryWrapper<CustomerFollowUpFile>()
-                                .eq(CustomerFollowUpFile::getFollowUpId, followUp.getId())
-                );
-                followUpDto.setFileList(fileList);
-
-                return followUpDto;
-            }).collect(Collectors.toList());
-
-            dto.setFollowUpList(followUpDtoList);
-        }
-
-        return dto;
+    public Customer selectCustomerDetailById(Long id) {
+        return this.getById( id);
     }
 
     /**
@@ -122,62 +96,53 @@
      * @return 瀹㈡埛妗f
      */
     @Override
-    public IPage<Customer> selectCustomerList(Page<Customer> page, Customer customer) {
-        // 1. 澶勭悊绌哄�煎満鏅紙鍙傛暟鏍¢獙锛�
-        if (page == null) {
-            page = Page.of(1, 10); // 榛樿绗�1椤碉紝姣忛〉10鏉℃暟鎹�
-        }
-        if (customer == null) {
-            customer = new Customer(); // 閬垮厤绌哄璞″鑷寸殑NPE
+    public IPage<CustomerDto> selectCustomerList(Page<CustomerDto> page, CustomerDto customer) {
+        IPage<CustomerDto> customerPage = customerMapper.listPage(page, customer);
+
+        List<CustomerDto> records = customerPage.getRecords();
+        if (CollectionUtils.isEmpty(records)) {
+            return customerPage;
         }
 
-        // 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);
-        }
-
-        // 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 + ")");
-
-                    // 鏌ヨ鏈�鏂扮殑璺熻繘璁板綍
-                    CustomerFollowUp followUp = customerFollowUpService.getOne(
-                            new LambdaQueryWrapper<CustomerFollowUp>()
-                                    .eq(CustomerFollowUp::getCustomerId, c.getId())
-                                    .orderByDesc(CustomerFollowUp::getFollowUpTime)
-                                    .last("LIMIT 1")
-                    );
-
-                    if (followUp != null) {
-                        c.setFollowUpLevel(followUp.getFollowUpLevel());
-                        c.setFollowUpTime(
-                                Date.from(
-                                        followUp.getFollowUpTime().atZone(ZoneId.systemDefault()).toInstant()
-                                )
-                        );
-                    }
-                })
+        List<Long> customerIds = records.stream()
+                .map(CustomerDto::getId)
+                .filter(Objects::nonNull)
                 .collect(Collectors.toList());
 
-        // 5. 鏇存柊鍒嗛〉缁撴灉涓殑鏁版嵁锛堜繚鎸佸垎椤典俊鎭畬鏁达級
-        IPage<Customer> resultPage = new Page<>(customerPage.getCurrent(), customerPage.getSize(), customerPage.getTotal());
-        resultPage.setRecords(processedList);
+        if (!CollectionUtils.isEmpty(customerIds)) {
+            Map<Long, CustomerFollowUp> latestFollowUpMap = getLatestFollowUpMap(customerIds);
 
-        return customerPage; // 杩斿洖鍖呭惈鍒嗛〉淇℃伅鐨処Page瀵硅薄
+            records.forEach(c -> {
+                String address = StringUtils.defaultString(c.getCompanyAddress(), "");
+                String phone = StringUtils.defaultString(c.getCompanyPhone(), "");
+                c.setAddressPhone(address + "(" + phone + ")");
+
+                CustomerFollowUp followUp = latestFollowUpMap.get(c.getId());
+                if (followUp != null) {
+                    c.setFollowUpLevel(followUp.getFollowUpLevel());
+                    c.setFollowUpTime(Date.from(
+                            followUp.getFollowUpTime().atZone(ZoneId.systemDefault()).toInstant()
+                    ));
+                }
+            });
+        }
+
+        return customerPage;
+    }
+
+    private Map<Long, CustomerFollowUp> getLatestFollowUpMap(List<Long> customerIds) {
+        List<CustomerFollowUp> followUps = customerFollowUpService.list(
+                new LambdaQueryWrapper<CustomerFollowUp>()
+                        .in(CustomerFollowUp::getCustomerPrivatePoolId, customerIds)
+                        .orderByDesc(CustomerFollowUp::getFollowUpTime)
+        );
+
+        return followUps.stream()
+                .collect(Collectors.toMap(
+                        CustomerFollowUp::getCustomerPrivatePoolId,
+                        followUp -> followUp,
+                        (existing, replacement) -> existing
+                ));
     }
 
     /**

--
Gitblit v1.9.3