xiaoyi
3 天以前 abf1c0a35d85d38239ff5d2ed746a4e4b797c9d1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
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);
    }
 
}