package cn.iocoder.yudao.module.crm.service.credit;
|
|
import cn.hutool.core.collection.CollUtil;
|
import cn.hutool.core.util.StrUtil;
|
import cn.iocoder.yudao.framework.common.exception.ServiceException;
|
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
|
import cn.iocoder.yudao.module.crm.controller.admin.credit.vo.CrmCreditRatingImportExcelVO;
|
import cn.iocoder.yudao.module.crm.controller.admin.credit.vo.CrmCreditRatingImportReqVO;
|
import cn.iocoder.yudao.module.crm.controller.admin.credit.vo.CrmCreditRatingImportRespVO;
|
import cn.iocoder.yudao.module.crm.controller.admin.credit.vo.CrmCreditRatingPageReqVO;
|
import cn.iocoder.yudao.module.crm.controller.admin.credit.vo.CrmCreditRatingRespVO;
|
import cn.iocoder.yudao.module.crm.controller.admin.credit.vo.CrmCreditRatingSaveReqVO;
|
import cn.iocoder.yudao.module.crm.dal.dataobject.credit.CrmCreditRatingDO;
|
import cn.iocoder.yudao.module.crm.dal.dataobject.customer.CrmCustomerDO;
|
import cn.iocoder.yudao.module.crm.dal.mysql.credit.CrmCreditRatingMapper;
|
import cn.iocoder.yudao.module.crm.dal.mysql.customer.CrmCustomerMapper;
|
import cn.iocoder.yudao.module.crm.enums.ErrorCodeConstants;
|
import jakarta.annotation.Resource;
|
import org.springframework.stereotype.Service;
|
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.validation.annotation.Validated;
|
|
import java.time.LocalDateTime;
|
import java.util.ArrayList;
|
import java.util.LinkedHashMap;
|
import java.util.List;
|
|
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
|
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.filterList;
|
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
|
import static cn.iocoder.yudao.module.crm.enums.ErrorCodeConstants.*;
|
|
@Service
|
@Validated
|
public class CrmCreditRatingServiceImpl implements CrmCreditRatingService {
|
|
@Resource
|
private CrmCreditRatingMapper creditRatingMapper;
|
@Resource
|
private CrmCustomerMapper customerMapper;
|
|
@Override
|
@Transactional(rollbackFor = Exception.class)
|
public Long createCreditRating(CrmCreditRatingSaveReqVO createReqVO) {
|
CrmCustomerDO customer = customerMapper.selectById(createReqVO.getCustomerId());
|
if (customer == null) {
|
throw exception(ErrorCodeConstants.CUSTOMER_NOT_EXISTS);
|
}
|
if (StrUtil.isBlank(createReqVO.getOperatorName())) {
|
createReqVO.setOperatorName(customer.getName());
|
}
|
if (createReqVO.getOperateTime() == null) {
|
createReqVO.setOperateTime(LocalDateTime.now());
|
}
|
CrmCreditRatingDO entity = BeanUtils.toBean(createReqVO, CrmCreditRatingDO.class, vo -> vo.setCustomerName(customer.getName()));
|
creditRatingMapper.insert(entity);
|
customer.setCreditLevel(entity.getLevel());
|
customer.setCreditScore(entity.getScore());
|
customerMapper.updateById(customer);
|
return entity.getId();
|
}
|
|
@Override
|
public void updateCreditRating(CrmCreditRatingSaveReqVO updateReqVO) {
|
CrmCreditRatingDO exists = creditRatingMapper.selectById(updateReqVO.getId());
|
if (exists == null) {
|
throw exception(ErrorCodeConstants.CRM_CREDIT_RATING_NOT_EXISTS);
|
}
|
CrmCreditRatingDO entity = BeanUtils.toBean(updateReqVO, CrmCreditRatingDO.class);
|
creditRatingMapper.updateById(entity);
|
CrmCreditRatingDO latest = creditRatingMapper.selectLatestByCustomerId(exists.getCustomerId());
|
CrmCustomerDO customer = customerMapper.selectById(exists.getCustomerId());
|
if (customer != null && latest != null && latest.getId().equals(entity.getId())) {
|
customer.setCreditLevel(latest.getLevel());
|
customer.setCreditScore(latest.getScore());
|
customerMapper.updateById(customer);
|
}
|
}
|
|
@Override
|
public void deleteCreditRating(Long id) {
|
CrmCreditRatingDO exists = creditRatingMapper.selectById(id);
|
if (exists == null) {
|
throw exception(ErrorCodeConstants.CRM_CREDIT_RATING_NOT_EXISTS);
|
}
|
creditRatingMapper.deleteById(id);
|
CrmCreditRatingDO latest = creditRatingMapper.selectLatestByCustomerId(exists.getCustomerId());
|
CrmCustomerDO customer = customerMapper.selectById(exists.getCustomerId());
|
if (customer != null) {
|
customer.setCreditLevel(latest == null ? null : latest.getLevel());
|
customer.setCreditScore(latest == null ? null : latest.getScore());
|
customerMapper.updateById(customer);
|
}
|
}
|
|
@Override
|
public CrmCreditRatingRespVO getCreditRating(Long id) {
|
CrmCreditRatingDO entity = creditRatingMapper.selectById(id);
|
return entity == null ? null : BeanUtils.toBean(entity, CrmCreditRatingRespVO.class);
|
}
|
|
@Override
|
public PageResult<CrmCreditRatingRespVO> getCreditRatingPage(CrmCreditRatingPageReqVO pageReqVO) {
|
return BeanUtils.toBean(creditRatingMapper.selectPage(pageReqVO), CrmCreditRatingRespVO.class);
|
}
|
|
@Override
|
public List<CrmCreditRatingRespVO> getCreditRatingList(CrmCreditRatingPageReqVO reqVO) {
|
return BeanUtils.toBean(creditRatingMapper.selectList(reqVO), CrmCreditRatingRespVO.class);
|
}
|
|
@Override
|
public CrmCreditRatingRespVO getLatestByCustomerId(Long customerId) {
|
CrmCreditRatingDO entity = creditRatingMapper.selectLatestByCustomerId(customerId);
|
return entity == null ? null : BeanUtils.toBean(entity, CrmCreditRatingRespVO.class);
|
}
|
|
@Override
|
@Transactional(rollbackFor = Exception.class)
|
public CrmCreditRatingImportRespVO importCreditRatingList(List<CrmCreditRatingImportExcelVO> importCreditRatings,
|
CrmCreditRatingImportReqVO importReqVO) {
|
// 1. 校验非空
|
importCreditRatings = filterList(importCreditRatings, item -> StrUtil.isNotBlank(item.getCustomerName()));
|
if (CollUtil.isEmpty(importCreditRatings)) {
|
throw exception(CRM_CREDIT_RATING_IMPORT_LIST_IS_EMPTY);
|
}
|
// 2. 操作人:为空则默认当前操作人
|
Long operatorId = importReqVO.getOperatorId() != null ? importReqVO.getOperatorId() : getLoginUserId();
|
|
// 3. 逐条处理
|
CrmCreditRatingImportRespVO respVO = CrmCreditRatingImportRespVO.builder()
|
.createCreditRatingNames(new ArrayList<>())
|
.updateCreditRatingNames(new ArrayList<>())
|
.failureCreditRatingNames(new LinkedHashMap<>())
|
.build();
|
importCreditRatings.forEach(importCreditRating -> {
|
try {
|
// 3.1 解析客户
|
CrmCustomerDO customer = customerMapper.selectByCustomerName(importCreditRating.getCustomerName());
|
if (customer == null) {
|
throw exception(CRM_CREDIT_RATING_IMPORT_CUSTOMER_NOT_EXISTS, importCreditRating.getCustomerName());
|
}
|
// 3.2 判断该客户是否已存在评级
|
CrmCreditRatingDO existRating = creditRatingMapper.selectLatestByCustomerId(customer.getId());
|
if (existRating == null) {
|
// 3.2.1 新增
|
if (StrUtil.isBlank(importCreditRating.getOperatorName())) {
|
importCreditRating.setOperatorName(customer.getName());
|
}
|
if (importCreditRating.getOperateTime() == null) {
|
importCreditRating.setOperateTime(LocalDateTime.now());
|
}
|
CrmCreditRatingDO entity = BeanUtils.toBean(importCreditRating, CrmCreditRatingDO.class,
|
vo -> vo.setCustomerId(customer.getId()).setCustomerName(customer.getName())
|
.setOperatorId(operatorId));
|
creditRatingMapper.insert(entity);
|
customer.setCreditLevel(entity.getLevel());
|
customer.setCreditScore(entity.getScore());
|
customerMapper.updateById(customer);
|
respVO.getCreateCreditRatingNames().add(importCreditRating.getCustomerName());
|
return;
|
}
|
// 3.2.2 已存在,判断是否允许更新
|
if (!importReqVO.getUpdateSupport()) {
|
respVO.getFailureCreditRatingNames().put(importCreditRating.getCustomerName(),
|
StrUtil.format(CRM_CREDIT_RATING_NAME_EXISTS.getMsg(), importCreditRating.getCustomerName()));
|
return;
|
}
|
CrmCreditRatingDO entity = BeanUtils.toBean(importCreditRating, CrmCreditRatingDO.class,
|
vo -> vo.setId(existRating.getId()).setCustomerId(customer.getId())
|
.setCustomerName(customer.getName()).setOperatorId(operatorId));
|
creditRatingMapper.updateById(entity);
|
CrmCreditRatingDO latest = creditRatingMapper.selectLatestByCustomerId(customer.getId());
|
if (latest != null && latest.getId().equals(entity.getId())) {
|
customer.setCreditLevel(latest.getLevel());
|
customer.setCreditScore(latest.getScore());
|
customerMapper.updateById(customer);
|
}
|
respVO.getUpdateCreditRatingNames().add(importCreditRating.getCustomerName());
|
} catch (ServiceException ex) {
|
respVO.getFailureCreditRatingNames().put(importCreditRating.getCustomerName(), ex.getMessage());
|
}
|
});
|
return respVO;
|
}
|
|
}
|