|
@@ -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;
|
|
@@ -55,6 +54,7 @@ import static cn.iocoder.yudao.module.crm.util.CrmAuditStatusUtils.convertBpmRes
|
|
|
@Slf4j
|
|
|
public class CrmReceivableServiceImpl implements CrmReceivableService {
|
|
|
|
|
|
+ // TODO @芋艿:改个名字
|
|
|
/**
|
|
|
* BPM 回款审批流程标识
|
|
|
*/
|
|
@@ -64,9 +64,10 @@ public class CrmReceivableServiceImpl implements CrmReceivableService {
|
|
|
private CrmReceivableMapper receivableMapper;
|
|
|
|
|
|
@Resource
|
|
|
- private CrmContractService contractService;
|
|
|
+ private CrmNoRedisDAO noRedisDAO;
|
|
|
+
|
|
|
@Resource
|
|
|
- private CrmCustomerService customerService;
|
|
|
+ private CrmContractService contractService;
|
|
|
@Resource
|
|
|
@Lazy // 延迟加载,避免循环依赖
|
|
|
private CrmReceivablePlanService receivablePlanService;
|
|
@@ -83,41 +84,54 @@ public class CrmReceivableServiceImpl implements CrmReceivableService {
|
|
|
@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);
|
|
|
+ }
|
|
|
}
|
|
|
-
|
|
|
}
|
|
|
|
|
|
@Override
|
|
@@ -127,6 +141,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 +150,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 +175,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 所使用,则不允许删除
|
|
|
+ // 1.2 如果被 CrmReceivablePlanDO 所使用,则不允许删除
|
|
|
if (Objects.nonNull(receivable.getPlanId()) && 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);
|
|
|
}
|
|
|
|
|
@@ -245,10 +258,4 @@ public class CrmReceivableServiceImpl implements CrmReceivableService {
|
|
|
return receivableMapper.selectCheckReceivablesCount(userId);
|
|
|
}
|
|
|
|
|
|
- // TODO @芋艿:不确定这个方法还要不要;
|
|
|
- @Override
|
|
|
- public Long getReceivableCountByContractId(Long contractId) {
|
|
|
- return receivableMapper.selectCount(CrmReceivableDO::getContractId, contractId);
|
|
|
- }
|
|
|
-
|
|
|
}
|