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.extension.plugins.pagination.Page;
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
import com.ruoyi.basic.dto.CustomerFollowUpDto;
|
import com.ruoyi.basic.dto.CustomerPrivatePoolDto;
|
import com.ruoyi.basic.mapper.CustomerPrivateMapper;
|
import com.ruoyi.basic.mapper.CustomerPrivatePoolMapper;
|
import com.ruoyi.basic.pojo.*;
|
import com.ruoyi.basic.service.*;
|
import com.ruoyi.common.utils.StringUtils;
|
import lombok.RequiredArgsConstructor;
|
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)
|
@RequiredArgsConstructor
|
public class CustomerPrivatePoolServiceImpl extends ServiceImpl<CustomerPrivatePoolMapper, CustomerPrivatePool> implements CustomerPrivatePoolService {
|
|
private final CustomerPrivatePoolMapper customerPrivatePoolMapper;
|
|
private final CustomerFollowUpService customerFollowUpService;
|
|
private final CustomerReturnVisitService customerReturnVisitService;
|
|
private final ICustomerService customerService;
|
|
private final CustomerFollowUpFileService customerFollowUpFileService;
|
private final 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();
|
}
|
}
|