153 lines
5.0 KiB
Java
153 lines
5.0 KiB
Java
package com.bycrm.service.impl;
|
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
import com.bycrm.common.Constants;
|
|
import com.bycrm.common.PageResult;
|
|
import com.bycrm.dto.CustomerDTO;
|
|
import com.bycrm.dto.PageQuery;
|
|
import com.bycrm.entity.Customer;
|
|
import com.bycrm.entity.User;
|
|
import com.bycrm.exception.BusinessException;
|
|
import com.bycrm.mapper.CustomerMapper;
|
|
import com.bycrm.mapper.UserMapper;
|
|
import com.bycrm.service.CustomerService;
|
|
import com.bycrm.vo.CustomerVO;
|
|
import org.springframework.beans.BeanUtils;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
import java.time.LocalDateTime;
|
|
import java.time.temporal.ChronoUnit;
|
|
import java.util.List;
|
|
import java.util.stream.Collectors;
|
|
|
|
/**
|
|
* 客户服务实现
|
|
*/
|
|
@Service
|
|
public class CustomerServiceImpl implements CustomerService {
|
|
|
|
private final CustomerMapper customerMapper;
|
|
private final UserMapper userMapper;
|
|
|
|
public CustomerServiceImpl(CustomerMapper customerMapper, UserMapper userMapper) {
|
|
this.customerMapper = customerMapper;
|
|
this.userMapper = userMapper;
|
|
}
|
|
|
|
@Override
|
|
public PageResult<CustomerVO> getCustomerPage(PageQuery query, String name, String industry, Integer status) {
|
|
// 计算偏移量
|
|
query.setOffset((query.getCurrent() - 1) * query.getSize());
|
|
|
|
List<Customer> customers = customerMapper.selectPage(query, name, industry, status);
|
|
Long total = customerMapper.countPage(name, industry, status);
|
|
|
|
List<CustomerVO> voList = customers.stream().map(customer -> {
|
|
CustomerVO vo = convertToVO(customer);
|
|
return vo;
|
|
}).collect(Collectors.toList());
|
|
|
|
return PageResult.of(total, voList, query.getCurrent(), query.getSize());
|
|
}
|
|
|
|
@Override
|
|
public CustomerVO getCustomerById(Long id) {
|
|
Customer customer = customerMapper.selectById(id);
|
|
if (customer == null) {
|
|
throw new BusinessException("客户不存在");
|
|
}
|
|
return convertToVO(customer);
|
|
}
|
|
|
|
@Override
|
|
@Transactional(rollbackFor = Exception.class)
|
|
public Customer createCustomer(CustomerDTO customerDTO, Long currentUserId) {
|
|
// 校验客户名称是否重复
|
|
checkCustomerNameDuplicate(customerDTO.getName());
|
|
|
|
Customer customer = new Customer();
|
|
BeanUtils.copyProperties(customerDTO, customer);
|
|
customer.setStatus(Constants.CUSTOMER_STATUS_AVAILABLE);
|
|
customer.setCreatedAt(LocalDateTime.now());
|
|
customer.setUpdatedAt(LocalDateTime.now());
|
|
|
|
customerMapper.insert(customer);
|
|
return customer;
|
|
}
|
|
|
|
@Override
|
|
@Transactional(rollbackFor = Exception.class)
|
|
public void updateCustomer(Long id, CustomerDTO customerDTO, Long currentUserId) {
|
|
Customer existingCustomer = customerMapper.selectById(id);
|
|
if (existingCustomer == null) {
|
|
throw new BusinessException("客户不存在");
|
|
}
|
|
|
|
// 如果修改了名称,需要检查新名称是否与其他客户重复
|
|
if (!existingCustomer.getName().equals(customerDTO.getName())) {
|
|
checkCustomerNameDuplicate(customerDTO.getName());
|
|
}
|
|
|
|
Customer customer = new Customer();
|
|
BeanUtils.copyProperties(customerDTO, customer);
|
|
customer.setId(id);
|
|
customer.setUpdatedAt(LocalDateTime.now());
|
|
|
|
customerMapper.update(customer);
|
|
}
|
|
|
|
@Override
|
|
@Transactional(rollbackFor = Exception.class)
|
|
public void deleteCustomer(Long id, Long currentUserId) {
|
|
Customer customer = customerMapper.selectById(id);
|
|
if (customer == null) {
|
|
throw new BusinessException("客户不存在");
|
|
}
|
|
|
|
if (customer.getStatus() == Constants.CUSTOMER_STATUS_PROTECTED) {
|
|
throw new BusinessException("客户处于保护期中,无法删除");
|
|
}
|
|
|
|
customerMapper.deleteById(id);
|
|
}
|
|
|
|
@Override
|
|
public List<Customer> searchByName(String name) {
|
|
return customerMapper.selectByNameLike(name);
|
|
}
|
|
|
|
@Override
|
|
public void checkCustomerNameDuplicate(String name) {
|
|
List<Customer> existingCustomers = customerMapper.selectByNameLike(name);
|
|
if (existingCustomers.stream().anyMatch(c -> c.getName().equals(name))) {
|
|
throw new BusinessException("客户名称已存在");
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 转换为 VO
|
|
*/
|
|
private CustomerVO convertToVO(Customer customer) {
|
|
CustomerVO vo = new CustomerVO();
|
|
BeanUtils.copyProperties(customer, vo);
|
|
|
|
// 设置状态描述
|
|
switch (customer.getStatus()) {
|
|
case Constants.CUSTOMER_STATUS_AVAILABLE:
|
|
vo.setStatusDesc("可报备");
|
|
break;
|
|
case Constants.CUSTOMER_STATUS_PROTECTED:
|
|
vo.setStatusDesc("保护中");
|
|
break;
|
|
case Constants.CUSTOMER_STATUS_EXPIRED:
|
|
vo.setStatusDesc("已失效");
|
|
break;
|
|
default:
|
|
vo.setStatusDesc("未知");
|
|
}
|
|
|
|
return vo;
|
|
}
|
|
}
|