gaoluyang
5 天以前 eee03bbfea8b15d1a56ab63bd09a35c6c861c4cb
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
package cn.iocoder.yudao.module.crm.service.customer;
 
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.util.object.BeanUtils;
import cn.iocoder.yudao.module.crm.controller.admin.customer.vo.progress.CrmCustomerProgressSaveReqVO;
import cn.iocoder.yudao.module.crm.dal.dataobject.customer.CrmCustomerDO;
import cn.iocoder.yudao.module.crm.dal.dataobject.customer.CrmCustomerProgressDO;
import cn.iocoder.yudao.module.crm.dal.mysql.customer.CrmCustomerProgressMapper;
import cn.iocoder.yudao.module.crm.enums.common.CrmBizTypeEnum;
import cn.iocoder.yudao.module.crm.enums.permission.CrmPermissionLevelEnum;
import cn.iocoder.yudao.module.crm.framework.permission.core.annotations.CrmPermission;
import cn.iocoder.yudao.module.crm.service.permission.CrmPermissionService;
import cn.iocoder.yudao.module.crm.util.CrmPermissionUtils;
import jakarta.annotation.Resource;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
 
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
 
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
import static cn.iocoder.yudao.module.crm.enums.ErrorCodeConstants.CRM_PERMISSION_DENIED;
import static cn.iocoder.yudao.module.crm.enums.ErrorCodeConstants.CUSTOMER_PROGRESS_NOT_EXISTS;
 
/**
 * CRM 客户洽谈进度 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class CrmCustomerProgressServiceImpl implements CrmCustomerProgressService {
 
    @Resource
    private CrmCustomerProgressMapper customerProgressMapper;
 
    @Resource
    @Lazy
    private CrmPermissionService permissionService;
 
    @Resource
    private CrmCustomerService customerService;
 
    @Override
    @CrmPermission(bizType = CrmBizTypeEnum.CRM_CUSTOMER, bizId = "#createReqVO.customerId",
            level = CrmPermissionLevelEnum.WRITE)
    public Long createProgress(CrmCustomerProgressSaveReqVO createReqVO) {
        CrmCustomerProgressDO progress = BeanUtils.toBean(createReqVO, CrmCustomerProgressDO.class);
        customerProgressMapper.insert(progress);
        return progress.getId();
    }
 
    @Override
    public void updateProgress(CrmCustomerProgressSaveReqVO updateReqVO) {
        // 1. 校验存在
        CrmCustomerProgressDO oldProgress = validateProgressExists(updateReqVO.getId());
        // 2. 校验权限:洽谈进度跟随客户,客户写权限即可修改
        validateCustomerWritePermission(oldProgress.getCustomerId());
        // 3. 更新(客户编号不允许修改)
        CrmCustomerProgressDO updateObj = BeanUtils.toBean(updateReqVO, CrmCustomerProgressDO.class);
        updateObj.setCustomerId(oldProgress.getCustomerId());
        customerProgressMapper.updateById(updateObj);
    }
 
    @Override
    public void deleteProgress(Long id) {
        // 1. 校验存在
        CrmCustomerProgressDO progress = validateProgressExists(id);
        // 2. 校验权限
        validateCustomerWritePermission(progress.getCustomerId());
        // 3. 删除
        customerProgressMapper.deleteById(id);
    }
 
    @Override
    @CrmPermission(bizType = CrmBizTypeEnum.CRM_CUSTOMER, bizId = "#customerId",
            level = CrmPermissionLevelEnum.READ)
    public List<CrmCustomerProgressDO> getProgressListByCustomerId(Long customerId) {
        return customerProgressMapper.selectListByCustomerId(customerId);
    }
 
    @Override
    public Map<Long, CrmCustomerProgressDO> getLatestProgressMap(Collection<Long> customerIds) {
        List<CrmCustomerProgressDO> list = customerProgressMapper.selectListByCustomerIds(customerIds);
        if (CollUtil.isEmpty(list)) {
            return new LinkedHashMap<>();
        }
        // 已按洽谈时间倒序,同一客户保留第一条即最新
        Map<Long, CrmCustomerProgressDO> result = new LinkedHashMap<>();
        for (CrmCustomerProgressDO progress : list) {
            result.putIfAbsent(progress.getCustomerId(), progress);
        }
        return result;
    }
 
    /**
     * 异常提示里用的客户名称:解析失败时回退为 ID,避免异常分支再抛二次异常
     */
    private String getCustomerDisplayName(Long customerId) {
        try {
            CrmCustomerDO customer = customerService.getCustomer(customerId);
            return customer != null && StrUtil.isNotBlank(customer.getName())
                    ? customer.getName() : "ID=" + customerId;
        } catch (Exception ex) {
            return "ID=" + customerId;
        }
    }
 
    private CrmCustomerProgressDO validateProgressExists(Long id) {
        CrmCustomerProgressDO progress = customerProgressMapper.selectById(id);
        if (progress == null) {
            throw exception(CUSTOMER_PROGRESS_NOT_EXISTS);
        }
        return progress;
    }
 
    private void validateCustomerWritePermission(Long customerId) {
        // 超管 / CRM 管理员与 @CrmPermission 切面保持一致,直接放行
        if (CrmPermissionUtils.isCrmAdmin()) {
            return;
        }
        if (!permissionService.hasPermission(CrmBizTypeEnum.CRM_CUSTOMER.getType(), customerId,
                getLoginUserId(), CrmPermissionLevelEnum.WRITE)) {
            throw new ServiceException(CRM_PERMISSION_DENIED.getCode(),
                    "没有权限修改/删除客户[" + getCustomerDisplayName(customerId)
                            + "]的洽谈进度:您不是该客户的负责人或团队成员。请让客户负责人在客户详情「团队成员」中把您加入,"
                            + "或切换到该客户负责人账号后重试。");
        }
    }
 
}