gaoluyang
2026-10-16 7081c41bc3beb0ab49a22974ea4b010ae6854afe
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
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;
    }
 
}