|  | @@ -13,16 +13,15 @@ import cn.iocoder.yudao.module.bpm.api.task.dto.BpmProcessInstanceCreateReqDTO;
 | 
	
		
			
				|  |  |  import cn.iocoder.yudao.module.crm.controller.admin.receivable.vo.receivable.CrmReceivablePageReqVO;
 | 
	
		
			
				|  |  |  import cn.iocoder.yudao.module.crm.controller.admin.receivable.vo.receivable.CrmReceivableSaveReqVO;
 | 
	
		
			
				|  |  |  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.receivable.CrmReceivableDO;
 | 
	
		
			
				|  |  |  import cn.iocoder.yudao.module.crm.dal.dataobject.receivable.CrmReceivablePlanDO;
 | 
	
		
			
				|  |  |  import cn.iocoder.yudao.module.crm.dal.mysql.receivable.CrmReceivableMapper;
 | 
	
		
			
				|  |  | +import cn.iocoder.yudao.module.crm.dal.redis.no.CrmNoRedisDAO;
 | 
	
		
			
				|  |  |  import cn.iocoder.yudao.module.crm.enums.common.CrmAuditStatusEnum;
 | 
	
		
			
				|  |  |  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.contract.CrmContractService;
 | 
	
		
			
				|  |  | -import cn.iocoder.yudao.module.crm.service.customer.CrmCustomerService;
 | 
	
		
			
				|  |  |  import cn.iocoder.yudao.module.crm.service.permission.CrmPermissionService;
 | 
	
		
			
				|  |  |  import cn.iocoder.yudao.module.crm.service.permission.bo.CrmPermissionCreateReqBO;
 | 
	
		
			
				|  |  |  import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
 | 
	
	
		
			
				|  | @@ -36,9 +35,10 @@ import org.springframework.stereotype.Service;
 | 
	
		
			
				|  |  |  import org.springframework.transaction.annotation.Transactional;
 | 
	
		
			
				|  |  |  import org.springframework.validation.annotation.Validated;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +import java.math.BigDecimal;
 | 
	
		
			
				|  |  |  import java.util.Collection;
 | 
	
		
			
				|  |  |  import java.util.List;
 | 
	
		
			
				|  |  | -import java.util.Objects;
 | 
	
		
			
				|  |  | +import java.util.Map;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
 | 
	
		
			
				|  |  |  import static cn.iocoder.yudao.module.crm.enums.ErrorCodeConstants.*;
 | 
	
	
		
			
				|  | @@ -56,17 +56,18 @@ import static cn.iocoder.yudao.module.crm.util.CrmAuditStatusUtils.convertBpmRes
 | 
	
		
			
				|  |  |  public class CrmReceivableServiceImpl implements CrmReceivableService {
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      /**
 | 
	
		
			
				|  |  | -     * BPM 回款审批流程标识
 | 
	
		
			
				|  |  | +     * BPM 合同审批流程标识
 | 
	
		
			
				|  |  |       */
 | 
	
		
			
				|  |  | -    public static final String RECEIVABLE_APPROVE = "receivable-approve";
 | 
	
		
			
				|  |  | +    public static final String BPM_PROCESS_DEFINITION_KEY = "crm-receivable-audit";
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      @Resource
 | 
	
		
			
				|  |  |      private CrmReceivableMapper receivableMapper;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      @Resource
 | 
	
		
			
				|  |  | -    private CrmContractService contractService;
 | 
	
		
			
				|  |  | +    private CrmNoRedisDAO noRedisDAO;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |      @Resource
 | 
	
		
			
				|  |  | -    private CrmCustomerService customerService;
 | 
	
		
			
				|  |  | +    private CrmContractService contractService;
 | 
	
		
			
				|  |  |      @Resource
 | 
	
		
			
				|  |  |      @Lazy // 延迟加载,避免循环依赖
 | 
	
		
			
				|  |  |      private CrmReceivablePlanService receivablePlanService;
 | 
	
	
		
			
				|  | @@ -78,48 +79,63 @@ public class CrmReceivableServiceImpl implements CrmReceivableService {
 | 
	
		
			
				|  |  |      @Resource
 | 
	
		
			
				|  |  |      private BpmProcessInstanceApi bpmProcessInstanceApi;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +    // TODO @puhui999:操作日志没记录上
 | 
	
		
			
				|  |  |      @Override
 | 
	
		
			
				|  |  |      @Transactional(rollbackFor = Exception.class)
 | 
	
		
			
				|  |  |      @LogRecord(type = CRM_RECEIVABLE_TYPE, subType = CRM_RECEIVABLE_CREATE_SUB_TYPE, bizNo = "{{#receivable.id}}",
 | 
	
		
			
				|  |  |              success = CRM_RECEIVABLE_CREATE_SUCCESS)
 | 
	
		
			
				|  |  |      public Long createReceivable(CrmReceivableSaveReqVO createReqVO) {
 | 
	
		
			
				|  |  | -        // 1. 校验关联数据存在
 | 
	
		
			
				|  |  | +        // 1.1 校验关联数据存在
 | 
	
		
			
				|  |  |          validateRelationDataExists(createReqVO);
 | 
	
		
			
				|  |  | -        // 2. 插入还款
 | 
	
		
			
				|  |  | -        CrmReceivableDO receivable = BeanUtils.toBean(createReqVO, CrmReceivableDO.class).setAuditStatus(CrmAuditStatusEnum.DRAFT.getStatus());
 | 
	
		
			
				|  |  | +        // 1.2 生成回款编号
 | 
	
		
			
				|  |  | +        String no = noRedisDAO.generate(CrmNoRedisDAO.RECEIVABLE_PREFIX);
 | 
	
		
			
				|  |  | +        if (receivableMapper.selectByNo(no) != null) {
 | 
	
		
			
				|  |  | +            throw exception(RECEIVABLE_NO_EXISTS);
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        // 2. 插入回款
 | 
	
		
			
				|  |  | +        CrmReceivableDO receivable = BeanUtils.toBean(createReqVO, CrmReceivableDO.class)
 | 
	
		
			
				|  |  | +                .setNo(no).setAuditStatus(CrmAuditStatusEnum.DRAFT.getStatus());
 | 
	
		
			
				|  |  |          receivableMapper.insert(receivable);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |          // 3. 创建数据权限
 | 
	
		
			
				|  |  |          permissionService.createPermission(new CrmPermissionCreateReqBO().setBizType(CrmBizTypeEnum.CRM_RECEIVABLE.getType())
 | 
	
		
			
				|  |  | -                .setBizId(receivable.getId()).setUserId(createReqVO.getOwnerUserId()).setLevel(CrmPermissionLevelEnum.OWNER.getLevel())); // 设置当前操作的人为负责人
 | 
	
		
			
				|  |  | +                .setBizId(receivable.getId()).setUserId(createReqVO.getOwnerUserId())
 | 
	
		
			
				|  |  | +                .setLevel(CrmPermissionLevelEnum.OWNER.getLevel())); // 设置当前操作的人为负责人
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |          // 4. 更新关联的回款计划
 | 
	
		
			
				|  |  | -        if (Objects.nonNull(createReqVO.getPlanId())) {
 | 
	
		
			
				|  |  | -            receivablePlanService.updateReceivableId(receivable.getPlanId(), receivable.getId());
 | 
	
		
			
				|  |  | +        if (createReqVO.getPlanId() != null) {
 | 
	
		
			
				|  |  | +            receivablePlanService.updateReceivablePlanReceivableId(receivable.getPlanId(), receivable.getId());
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |          // 5. 记录操作日志上下文
 | 
	
		
			
				|  |  |          LogRecordContext.putVariable("receivable", receivable);
 | 
	
		
			
				|  |  |          return receivable.getId();
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      private void validateRelationDataExists(CrmReceivableSaveReqVO reqVO) {
 | 
	
		
			
				|  |  | -        adminUserApi.validateUser(reqVO.getOwnerUserId()); // 校验负责人存在
 | 
	
		
			
				|  |  | -        CrmContractDO contract = contractService.getContract(reqVO.getContractId());
 | 
	
		
			
				|  |  | -        if (ObjectUtil.isNull(contract)) {
 | 
	
		
			
				|  |  | -            throw exception(CONTRACT_NOT_EXISTS);
 | 
	
		
			
				|  |  | -        }
 | 
	
		
			
				|  |  | -        CrmCustomerDO customer = customerService.getCustomer(reqVO.getCustomerId());
 | 
	
		
			
				|  |  | -        if (ObjectUtil.isNull(customer)) {
 | 
	
		
			
				|  |  | -            throw exception(CUSTOMER_NOT_EXISTS);
 | 
	
		
			
				|  |  | +        if (reqVO.getOwnerUserId() != null) {
 | 
	
		
			
				|  |  | +            adminUserApi.validateUser(reqVO.getOwnerUserId()); // 校验负责人存在
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  | -        if (Objects.isNull(reqVO.getPlanId())) { // 没有回款计划编号则不校验
 | 
	
		
			
				|  |  | -            return;
 | 
	
		
			
				|  |  | +        if (reqVO.getContractId() != null) {
 | 
	
		
			
				|  |  | +            CrmContractDO contract = contractService.validateContract(reqVO.getContractId());
 | 
	
		
			
				|  |  | +            if (ObjectUtil.notEqual(contract.getAuditStatus(), CrmAuditStatusEnum.APPROVE.getStatus())) {
 | 
	
		
			
				|  |  | +                throw exception(RECEIVABLE_CREATE_FAIL_CONTRACT_NOT_APPROVE);
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +            reqVO.setCustomerId(contract.getCustomerId()); // 设置客户编号
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  | -        CrmReceivablePlanDO receivablePlan = receivablePlanService.getReceivablePlan(reqVO.getPlanId());
 | 
	
		
			
				|  |  | -        if (ObjectUtil.isNull(receivablePlan)) {
 | 
	
		
			
				|  |  | -            throw exception(RECEIVABLE_PLAN_NOT_EXISTS);
 | 
	
		
			
				|  |  | +        if (reqVO.getPlanId() != null) {
 | 
	
		
			
				|  |  | +            CrmReceivablePlanDO receivablePlan = receivablePlanService.getReceivablePlan(reqVO.getPlanId());
 | 
	
		
			
				|  |  | +            if (receivablePlan == null) {
 | 
	
		
			
				|  |  | +                throw exception(RECEIVABLE_PLAN_NOT_EXISTS);
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +            if (receivablePlan.getReceivableId() != null) {
 | 
	
		
			
				|  |  | +                throw exception(RECEIVABLE_PLAN_EXISTS_RECEIVABLE);
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +    // TODO @puhui999:操作日志没记录上
 | 
	
		
			
				|  |  |      @Override
 | 
	
		
			
				|  |  |      @Transactional(rollbackFor = Exception.class)
 | 
	
		
			
				|  |  |      @LogRecord(type = CRM_RECEIVABLE_TYPE, subType = CRM_RECEIVABLE_UPDATE_SUB_TYPE, bizNo = "{{#updateReqVO.id}}",
 | 
	
	
		
			
				|  | @@ -127,6 +143,7 @@ public class CrmReceivableServiceImpl implements CrmReceivableService {
 | 
	
		
			
				|  |  |      @CrmPermission(bizType = CrmBizTypeEnum.CRM_RECEIVABLE, bizId = "#updateReqVO.id", level = CrmPermissionLevelEnum.WRITE)
 | 
	
		
			
				|  |  |      public void updateReceivable(CrmReceivableSaveReqVO updateReqVO) {
 | 
	
		
			
				|  |  |          Assert.notNull(updateReqVO.getId(), "回款编号不能为空");
 | 
	
		
			
				|  |  | +        updateReqVO.setOwnerUserId(null).setCustomerId(null).setContractId(null).setPlanId(null); // 不允许修改的字段
 | 
	
		
			
				|  |  |          // 1.1 校验存在
 | 
	
		
			
				|  |  |          CrmReceivableDO receivable = validateReceivableExists(updateReqVO.getId());
 | 
	
		
			
				|  |  |          // 1.2 只有草稿、审批中,可以编辑;
 | 
	
	
		
			
				|  | @@ -135,7 +152,7 @@ public class CrmReceivableServiceImpl implements CrmReceivableService {
 | 
	
		
			
				|  |  |              throw exception(RECEIVABLE_UPDATE_FAIL_EDITING_PROHIBITED);
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -        // 2. 更新还款
 | 
	
		
			
				|  |  | +        // 2. 更新回款
 | 
	
		
			
				|  |  |          CrmReceivableDO updateObj = BeanUtils.toBean(updateReqVO, CrmReceivableDO.class);
 | 
	
		
			
				|  |  |          receivableMapper.updateById(updateObj);
 | 
	
		
			
				|  |  |  
 | 
	
	
		
			
				|  | @@ -160,27 +177,25 @@ public class CrmReceivableServiceImpl implements CrmReceivableService {
 | 
	
		
			
				|  |  |          receivableMapper.updateById(new CrmReceivableDO().setId(id).setAuditStatus(auditStatus));
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -    // TODO @liuhongfeng:缺一个取消回款的接口;只有草稿、审批中可以取消;CrmAuditStatusEnum
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  |      @Override
 | 
	
		
			
				|  |  |      @Transactional(rollbackFor = Exception.class)
 | 
	
		
			
				|  |  |      @LogRecord(type = CRM_RECEIVABLE_TYPE, subType = CRM_RECEIVABLE_DELETE_SUB_TYPE, bizNo = "{{#id}}",
 | 
	
		
			
				|  |  |              success = CRM_RECEIVABLE_DELETE_SUCCESS)
 | 
	
		
			
				|  |  |      @CrmPermission(bizType = CrmBizTypeEnum.CRM_RECEIVABLE, bizId = "#id", level = CrmPermissionLevelEnum.OWNER)
 | 
	
		
			
				|  |  |      public void deleteReceivable(Long id) {
 | 
	
		
			
				|  |  | -        // 校验存在
 | 
	
		
			
				|  |  | +        // 1.1 校验存在
 | 
	
		
			
				|  |  |          CrmReceivableDO receivable = validateReceivableExists(id);
 | 
	
		
			
				|  |  | -        // 如果被 CrmReceivablePlanDO 所使用,则不允许删除
 | 
	
		
			
				|  |  | -        if (Objects.nonNull(receivable.getPlanId()) && receivablePlanService.getReceivablePlan(receivable.getPlanId()) != null) {
 | 
	
		
			
				|  |  | +        // 1.2 如果被 CrmReceivablePlanDO 所使用,则不允许删除
 | 
	
		
			
				|  |  | +        if (receivable.getPlanId() != null && receivablePlanService.getReceivablePlan(receivable.getPlanId()) != null) {
 | 
	
		
			
				|  |  |              throw exception(RECEIVABLE_DELETE_FAIL);
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  | -        // 删除
 | 
	
		
			
				|  |  | -        receivableMapper.deleteById(id);
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -        // 删除数据权限
 | 
	
		
			
				|  |  | +        // 2. 删除
 | 
	
		
			
				|  |  | +        receivableMapper.deleteById(id);
 | 
	
		
			
				|  |  | +        // 3. 删除数据权限
 | 
	
		
			
				|  |  |          permissionService.deletePermission(CrmBizTypeEnum.CRM_RECEIVABLE.getType(), id);
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -        // 记录操作日志上下文
 | 
	
		
			
				|  |  | +        // 4. 记录操作日志上下文
 | 
	
		
			
				|  |  |          LogRecordContext.putVariable("receivable", receivable);
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |  
 | 
	
	
		
			
				|  | @@ -197,7 +212,7 @@ public class CrmReceivableServiceImpl implements CrmReceivableService {
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |          // 2. 创建回款审批流程实例
 | 
	
		
			
				|  |  |          String processInstanceId = bpmProcessInstanceApi.createProcessInstance(userId, new BpmProcessInstanceCreateReqDTO()
 | 
	
		
			
				|  |  | -                .setProcessDefinitionKey(RECEIVABLE_APPROVE).setBusinessKey(String.valueOf(id)));
 | 
	
		
			
				|  |  | +                .setProcessDefinitionKey(BPM_PROCESS_DEFINITION_KEY).setBusinessKey(String.valueOf(id)));
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |          // 3. 更新回款工作流编号
 | 
	
		
			
				|  |  |          receivableMapper.updateById(new CrmReceivableDO().setId(id).setProcessInstanceId(processInstanceId)
 | 
	
	
		
			
				|  | @@ -246,8 +261,8 @@ public class CrmReceivableServiceImpl implements CrmReceivableService {
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      @Override
 | 
	
		
			
				|  |  | -    public Long getReceivableCountByContractId(Long contractId) {
 | 
	
		
			
				|  |  | -        return receivableMapper.selectCount(CrmReceivableDO::getContractId, contractId);
 | 
	
		
			
				|  |  | +    public Map<Long, BigDecimal> getReceivablePriceMapByContractId(Collection<Long> contractIds) {
 | 
	
		
			
				|  |  | +        return receivableMapper.selectReceivablePriceMapByContractId(contractIds);
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  }
 |