2026-04-20 a02789977e52ea046fbdeb042c63c34c39d6d529
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
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.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.basic.dto.CustomerDto;
import com.ruoyi.basic.dto.CustomerFollowUpDto;
import com.ruoyi.basic.dto.CustomerPrivatePoolDto;
import com.ruoyi.basic.mapper.CustomerPrivateMapper;
import com.ruoyi.basic.pojo.*;
import com.ruoyi.basic.mapper.CustomerPrivatePoolMapper;
import com.ruoyi.basic.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.sales.mapper.SalesLedgerMapper;
import com.ruoyi.sales.pojo.SalesLedger;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.time.ZoneId;
import java.util.*;
import java.util.stream.Collectors;
 
/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 芯导软件(江苏)有限公司
 * @since 2026-04-16 04:43:00
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class CustomerPrivatePoolServiceImpl extends ServiceImpl<CustomerPrivatePoolMapper, CustomerPrivatePool> implements CustomerPrivatePoolService {
 
    @Autowired
    private CustomerPrivatePoolMapper customerPrivatePoolMapper;
 
    @Autowired
    private CustomerFollowUpService customerFollowUpService;
 
    @Autowired
    private CustomerReturnVisitService customerReturnVisitService;
 
    @Autowired
    private ICustomerService customerService;
 
    @Autowired
    private CustomerFollowUpFileService customerFollowUpFileService;
    @Autowired
    private CustomerPrivateMapper customerPrivateMapper;
 
 
    @Override
    public IPage<CustomerPrivatePoolDto> listPage(Page<CustomerPrivatePoolDto> page, CustomerPrivatePoolDto customerPrivatePoolDto) {
        IPage<CustomerPrivatePoolDto> customerPrivatePoolDtoIPage = customerPrivatePoolMapper.listPage(page, customerPrivatePoolDto);
        List<Long> customerIds = customerPrivatePoolDtoIPage.getRecords().stream()
                .map(CustomerPrivatePoolDto::getId )
                .filter(Objects::nonNull)
                .collect(Collectors.toList());
 
        if (!org.springframework.util.CollectionUtils.isEmpty(customerIds)) {
            Map<Long, CustomerFollowUp> latestFollowUpMap = getLatestFollowUpMap(customerIds);
 
            customerPrivatePoolDtoIPage.getRecords().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 customerPrivatePoolDtoIPage;
    }
 
 
    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
                ));
    }
 
    @Override
    public boolean deleteCustomerPrivatePool(Long id) {
        List<CustomerPrivatePool> list = this.list(new QueryWrapper<CustomerPrivatePool>().lambda().eq(CustomerPrivatePool::getCustomerId, id));
 
        for (CustomerPrivatePool customerPrivatePool : list) {
            customerFollowUpService.remove(new QueryWrapper<CustomerFollowUp>().lambda().eq(CustomerFollowUp::getCustomerPrivatePoolId, customerPrivatePool.getId()));
            customerReturnVisitService.remove(new QueryWrapper<CustomerReturnVisit>().lambda().eq(CustomerReturnVisit::getCustomerPrivatePoolId, customerPrivatePool.getId()));
        }
        Customer byId = customerService.getById(id);
        byId.setUsageStatus(0L);
        byId.setUsageUser(0L);
        customerService.updateById(byId);
        list.stream().forEach(customerPrivatePool -> {
            customerPrivatePool.setDeleteFlag(1);
            customerPrivatePoolMapper.updateById(customerPrivatePool);
        });
        return true;
    }
 
    @Override
    public boolean together(CustomerPrivatePoolDto customerPrivatePoolDto) {
        List<CustomerPrivatePool> existingPools = this.list(new QueryWrapper<CustomerPrivatePool>().lambda().eq(CustomerPrivatePool::getCustomerId, customerPrivatePoolDto.getCustomerId()));
 
        List<Long> existingBoundIds = existingPools.stream()
                .map(CustomerPrivatePool::getBoundId)
                .collect(Collectors.toList());
 
        List<Long> newBoundIds = customerPrivatePoolDto.getBoundIds().stream()
                .filter(boundId -> !existingBoundIds.contains(boundId))
                .collect(Collectors.toList());
 
        if (CollectionUtils.isEmpty(newBoundIds)) {
            return true;
        }
        for (Long id : customerPrivatePoolDto.getBoundIds()) {
            CustomerPrivatePool customerPrivatePool = new CustomerPrivatePool();
            customerPrivatePool.setCustomerId(customerPrivatePoolDto.getCustomerId());
            customerPrivatePool.setBoundId(id);
            customerPrivatePool.setType(1L);
            this.save(customerPrivatePool);
        }
        return true;
    }
 
    @Override
    public boolean add(CustomerPrivatePoolDto customerPrivatePool) {
        customerPrivatePool.setType(1L);
        this.save(customerPrivatePool);
        Customer byId = customerService.getById(customerPrivatePool.getCustomerId());
        if (byId != null) {
            byId.setUsageStatus(1L);
            byId.setUsageUser(customerPrivatePool.getBoundId());
            return customerService.updateById(byId);
        }
        throw new RuntimeException("客户不存在");
 
    }
 
    @Override
    public CustomerPrivatePoolDto getInfo(Long id) {
        CustomerPrivatePoolDto customerPrivatePool = customerPrivatePoolMapper.selectInfo(id);
        if (customerPrivatePool == null) {
            return null;
        }
 
        // 查询跟进记录
        List<CustomerFollowUp> followUpList = customerFollowUpService.list(
                new LambdaQueryWrapper<CustomerFollowUp>()
                        .eq(CustomerFollowUp::getCustomerPrivatePoolId, 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());
 
            customerPrivatePool.setFollowUpList(followUpDtoList);
        }
 
        return customerPrivatePool;
    }
 
    @Override
    public Boolean updateCustomerPrivatePoolDto(CustomerPrivatePoolDto customerPrivatePoolDto) {
        if (customerPrivatePoolDto.getType() == 0L) {
            CustomerPrivate byId = customerPrivateMapper.selectById(customerPrivatePoolDto.getCustomerId());
            BeanUtils.copyProperties(customerPrivatePoolDto, byId);
            byId.setId(customerPrivatePoolDto.getCustomerId());
            customerPrivateMapper.updateById(byId);
        } else if (customerPrivatePoolDto.getType() == 1L) {
            Customer customer = customerService.getById(customerPrivatePoolDto.getCustomerId());
            BeanUtils.copyProperties(customerPrivatePoolDto, customer);
            customer.setId(customerPrivatePoolDto.getCustomerId());
            customerService.updateById(customer);
        }
        return true;
    }
 
    @Override
    public CustomerPrivatePoolDto getbyId(Long id) {
        CustomerPrivatePoolDto customerPrivatePool = customerPrivatePoolMapper.selectInfo(id);
        return customerPrivatePool;
    }
 
    @Override
    public List<CustomerPrivatePoolDto> selectCustomerPrivatePoolDtoListByIds(List<Long> ids) {
        ArrayList<CustomerPrivatePoolDto> customerPrivatePoolDtos = new ArrayList<>();
        for (Long id : ids) {
            CustomerPrivatePoolDto customerPrivatePoolDto = customerPrivatePoolMapper.selectInfo(id);
            customerPrivatePoolDtos.add(customerPrivatePoolDto);
        }
        return customerPrivatePoolDtos;
    }
 
    @Override
    public List<CustomerPrivatePoolDto> selectCustomerPrivatePoolDtoLists(CustomerPrivatePoolDto customerPrivatePoolDto) {
 
        return customerPrivatePoolMapper.selectInfos();
    }
}