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;
/**
*
* 服务实现类
*
*
* @author 芯导软件(江苏)有限公司
* @since 2026-04-16 04:43:00
*/
@Service
@Transactional(rollbackFor = Exception.class)
public class CustomerPrivatePoolServiceImpl extends ServiceImpl 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 listPage(Page page, CustomerPrivatePoolDto customerPrivatePoolDto) {
IPage customerPrivatePoolDtoIPage = customerPrivatePoolMapper.listPage(page, customerPrivatePoolDto);
List customerIds = customerPrivatePoolDtoIPage.getRecords().stream()
.map(CustomerPrivatePoolDto::getId )
.filter(Objects::nonNull)
.collect(Collectors.toList());
if (!org.springframework.util.CollectionUtils.isEmpty(customerIds)) {
Map 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 getLatestFollowUpMap(List customerIds) {
List followUps = customerFollowUpService.list(
new LambdaQueryWrapper()
.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 list = this.list(new QueryWrapper().lambda().eq(CustomerPrivatePool::getCustomerId, id));
for (CustomerPrivatePool customerPrivatePool : list) {
customerFollowUpService.remove(new QueryWrapper().lambda().eq(CustomerFollowUp::getCustomerPrivatePoolId, customerPrivatePool.getId()));
customerReturnVisitService.remove(new QueryWrapper().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 existingPools = this.list(new QueryWrapper().lambda().eq(CustomerPrivatePool::getCustomerId, customerPrivatePoolDto.getCustomerId()));
List existingBoundIds = existingPools.stream()
.map(CustomerPrivatePool::getBoundId)
.collect(Collectors.toList());
List 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 followUpList = customerFollowUpService.list(
new LambdaQueryWrapper()
.eq(CustomerFollowUp::getCustomerPrivatePoolId, id)
.orderByDesc(CustomerFollowUp::getFollowUpTime)
);
if (!CollectionUtils.isEmpty(followUpList)) {
List followUpDtoList = followUpList.stream().map(followUp -> {
CustomerFollowUpDto followUpDto = new CustomerFollowUpDto();
BeanUtils.copyProperties(followUp, followUpDto);
// 查询附件
List fileList = customerFollowUpFileService.list(
new LambdaQueryWrapper()
.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 selectCustomerPrivatePoolDtoListByIds(List ids) {
ArrayList customerPrivatePoolDtos = new ArrayList<>();
for (Long id : ids) {
CustomerPrivatePoolDto customerPrivatePoolDto = customerPrivatePoolMapper.selectInfo(id);
customerPrivatePoolDtos.add(customerPrivatePoolDto);
}
return customerPrivatePoolDtos;
}
@Override
public List selectCustomerPrivatePoolDtoLists(CustomerPrivatePoolDto customerPrivatePoolDto) {
return customerPrivatePoolMapper.selectInfos();
}
}