package cn.iocoder.yudao.module.crm.service.credit;
|
|
import cn.hutool.core.util.StrUtil;
|
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.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.List;
|
|
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
|
|
@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);
|
}
|
|
}
|