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 getProgressListByCustomerId(Long customerId) { return customerProgressMapper.selectListByCustomerId(customerId); } @Override public Map getLatestProgressMap(Collection customerIds) { List list = customerProgressMapper.selectListByCustomerIds(customerIds); if (CollUtil.isEmpty(list)) { return new LinkedHashMap<>(); } // 已按洽谈时间倒序,同一客户保留第一条即最新 Map 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) + "]的洽谈进度:您不是该客户的负责人或团队成员。请让客户负责人在客户详情「团队成员」中把您加入," + "或切换到该客户负责人账号后重试。"); } } }