| | |
| | | import cn.iocoder.yudao.module.crm.dal.dataobject.business.CrmBusinessDO; |
| | | import cn.iocoder.yudao.module.crm.dal.dataobject.contact.CrmContactDO; |
| | | import cn.iocoder.yudao.module.crm.dal.dataobject.contract.CrmContractDO; |
| | | import cn.iocoder.yudao.module.crm.dal.dataobject.customer.CrmCustomerDO; |
| | | import cn.iocoder.yudao.module.crm.dal.dataobject.permission.CrmPermissionDO; |
| | | import cn.iocoder.yudao.module.crm.dal.mysql.business.CrmBusinessMapper; |
| | | import cn.iocoder.yudao.module.crm.dal.mysql.contact.CrmContactMapper; |
| | | import cn.iocoder.yudao.module.crm.dal.mysql.contract.CrmContractMapper; |
| | | import cn.iocoder.yudao.module.crm.dal.mysql.customer.CrmCustomerMapper; |
| | | import cn.iocoder.yudao.module.crm.dal.mysql.permission.CrmPermissionMapper; |
| | | import cn.iocoder.yudao.module.crm.enums.common.CrmBizTypeEnum; |
| | | import cn.iocoder.yudao.module.crm.enums.permission.CrmPermissionLevelEnum; |
| | |
| | | |
| | | @Resource |
| | | private CrmPermissionMapper permissionMapper; |
| | | @Resource |
| | | private CrmCustomerMapper customerMapper; |
| | | @Resource |
| | | private CrmContactMapper contactMapper; |
| | | @Resource |
| | | private CrmBusinessMapper businessMapper; |
| | | @Resource |
| | | private CrmContractMapper contractMapper; |
| | | @Resource |
| | | @Lazy // 解决依赖循环 |
| | | private CrmContactService contactService; |
| | |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void syncCustomerOwnerPermission(Long customerId, Long ownerUserId) { |
| | | if (customerId == null || ownerUserId == null) { |
| | | return; |
| | | } |
| | | // 1. 联系人 |
| | | syncCustomerOwnerPermission(CrmBizTypeEnum.CRM_CONTACT.getType(), ownerUserId, |
| | | convertSet(contactMapper.selectListByCustomerId(customerId), CrmContactDO::getId)); |
| | | // 2. 商机 |
| | | syncCustomerOwnerPermission(CrmBizTypeEnum.CRM_BUSINESS.getType(), ownerUserId, |
| | | convertSet(businessMapper.selectListByCustomerId(customerId), CrmBusinessDO::getId)); |
| | | // 3. 合同 |
| | | syncCustomerOwnerPermission(CrmBizTypeEnum.CRM_CONTRACT.getType(), ownerUserId, |
| | | convertSet(contractMapper.selectListByCustomerId(customerId), CrmContractDO::getId)); |
| | | } |
| | | |
| | | /** |
| | | * 同步某个业务类型下,客户负责人的【读写】权限 |
| | | * |
| | | * @param bizType 数据类型 |
| | | * @param ownerUserId 客户负责人编号 |
| | | * @param bizIds 数据编号集合 |
| | | */ |
| | | private void syncCustomerOwnerPermission(Integer bizType, Long ownerUserId, Set<Long> bizIds) { |
| | | if (CollUtil.isEmpty(bizIds)) { |
| | | return; |
| | | } |
| | | // 1. 查询已有的权限,避免重复插入 |
| | | Map<Long, CrmPermissionDO> permissionMap = new HashMap<>(); |
| | | for (CrmPermissionDO permission : permissionMapper.selectByBizTypeAndBizIds(bizType, bizIds)) { |
| | | if (ObjUtil.equal(permission.getUserId(), ownerUserId)) { |
| | | permissionMap.putIfAbsent(permission.getBizId(), permission); |
| | | } |
| | | } |
| | | // 2. 计算需要新增、升级的权限 |
| | | List<CrmPermissionDO> createPermissions = new ArrayList<>(); |
| | | List<CrmPermissionDO> updatePermissions = new ArrayList<>(); |
| | | for (Long bizId : bizIds) { |
| | | CrmPermissionDO permission = permissionMap.get(bizId); |
| | | if (permission == null) { |
| | | createPermissions.add(new CrmPermissionDO().setBizType(bizType).setBizId(bizId) |
| | | .setUserId(ownerUserId).setLevel(CrmPermissionLevelEnum.WRITE.getLevel())); |
| | | } else if (CrmPermissionLevelEnum.isRead(permission.getLevel())) { |
| | | // 只读升级为读写;负责人级别不降级 |
| | | updatePermissions.add(new CrmPermissionDO().setId(permission.getId()) |
| | | .setLevel(CrmPermissionLevelEnum.WRITE.getLevel())); |
| | | } |
| | | } |
| | | // 3. 批量写入 |
| | | if (CollUtil.isNotEmpty(createPermissions)) { |
| | | permissionMapper.insertBatch(createPermissions); |
| | | } |
| | | if (CollUtil.isNotEmpty(updatePermissions)) { |
| | | permissionMapper.updateBatch(updatePermissions); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public void createPermissionForCustomerOwner(Integer bizType, Long bizId, Long customerId, Long ownerUserId) { |
| | | if (bizId == null || customerId == null) { |
| | | return; |
| | | } |
| | | // 1. 查询客户负责人 |
| | | CrmCustomerDO customer = customerMapper.selectById(customerId); |
| | | if (customer == null || customer.getOwnerUserId() == null) { |
| | | return; |
| | | } |
| | | Long customerOwnerUserId = customer.getOwnerUserId(); |
| | | // 2. 数据负责人本身就是客户负责人时,已经拥有权限,无需重复授权 |
| | | if (ObjUtil.equal(customerOwnerUserId, ownerUserId)) { |
| | | return; |
| | | } |
| | | // 3. 不存在则新增读写权限,只读则升级为读写 |
| | | CrmPermissionDO permission = permissionMapper.selectByBizTypeAndBizIdByUserId(bizType, bizId, customerOwnerUserId); |
| | | if (permission == null) { |
| | | permissionMapper.insert(new CrmPermissionDO().setBizType(bizType).setBizId(bizId) |
| | | .setUserId(customerOwnerUserId).setLevel(CrmPermissionLevelEnum.WRITE.getLevel())); |
| | | } else if (CrmPermissionLevelEnum.isRead(permission.getLevel())) { |
| | | permissionMapper.updateById(new CrmPermissionDO().setId(permission.getId()) |
| | | .setLevel(CrmPermissionLevelEnum.WRITE.getLevel())); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void updatePermission(CrmPermissionUpdateReqVO updateReqVO) { |
| | | // 1. 校验存在 |
| | | validatePermissionExists(updateReqVO.getIds()); |