From 91c179029f0b017ce7e6ae7feaaf9906207d3b97 Mon Sep 17 00:00:00 2001 From: YunaiV Date: Sun, 25 Feb 2024 16:09:16 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20CRM=EF=BC=9A=E5=AE=8C=E5=96=84?= =?UTF-8?q?=E5=9B=9E=E6=AC=BE=20plan=20=E6=96=B0=E5=A2=9E/=E4=BF=AE?= =?UTF-8?q?=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../module/crm/enums/ErrorCodeConstants.java | 2 +- .../admin/contract/CrmContractController.java | 15 ++-- .../CrmReceivablePlanController.java | 12 +++- .../vo/plan/CrmReceivablePlanSaveReqVO.java | 9 +-- .../receivable/CrmReceivablePlanMapper.java | 10 +++ .../service/contract/CrmContractService.java | 8 +++ .../contract/CrmContractServiceImpl.java | 5 ++ .../CrmReceivablePlanServiceImpl.java | 70 ++++++++++--------- .../receivable/CrmReceivableServiceImpl.java | 1 + 9 files changed, 83 insertions(+), 49 deletions(-) diff --git a/yudao-module-crm/yudao-module-crm-api/src/main/java/cn/iocoder/yudao/module/crm/enums/ErrorCodeConstants.java b/yudao-module-crm/yudao-module-crm-api/src/main/java/cn/iocoder/yudao/module/crm/enums/ErrorCodeConstants.java index b9c3898ce..f78750fd6 100644 --- a/yudao-module-crm/yudao-module-crm-api/src/main/java/cn/iocoder/yudao/module/crm/enums/ErrorCodeConstants.java +++ b/yudao-module-crm/yudao-module-crm-api/src/main/java/cn/iocoder/yudao/module/crm/enums/ErrorCodeConstants.java @@ -41,7 +41,7 @@ public interface ErrorCodeConstants { // ========== 回款计划 1-020-005-000 ========== ErrorCode RECEIVABLE_PLAN_NOT_EXISTS = new ErrorCode(1_020_005_000, "回款计划不存在"); - ErrorCode RECEIVABLE_PLAN_UPDATE_FAIL = new ErrorCode(1_020_006_000, "更想回款计划失败,原因:{}"); + ErrorCode RECEIVABLE_PLAN_UPDATE_FAIL = new ErrorCode(1_020_006_000, "更想回款计划失败,原因:已经有对应的还款"); // ========== 客户管理 1_020_006_000 ========== ErrorCode CUSTOMER_NOT_EXISTS = new ErrorCode(1_020_006_000, "客户不存在"); diff --git a/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/controller/admin/contract/CrmContractController.java b/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/controller/admin/contract/CrmContractController.java index 4267a3281..b5d96e96e 100644 --- a/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/controller/admin/contract/CrmContractController.java +++ b/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/controller/admin/contract/CrmContractController.java @@ -3,6 +3,7 @@ package cn.iocoder.yudao.module.crm.controller.admin.contract; import cn.hutool.core.collection.CollUtil; import cn.hutool.core.lang.Assert; import cn.iocoder.yudao.framework.common.pojo.CommonResult; +import cn.iocoder.yudao.framework.common.pojo.PageParam; import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.framework.common.util.collection.MapUtils; import cn.iocoder.yudao.framework.common.util.number.NumberUtils; @@ -229,14 +230,16 @@ public class CrmContractController { return success(contractService.getRemindContractCount(getLoginUserId())); } - // TODO @芋艿:需要看下; - @GetMapping("/list-all-simple-by-customer") - @Operation(summary = "获得合同精简列表", description = "只包含有读权限的客户的合同,主要用于前端的下拉选项") + @GetMapping("/simple-list") + @Operation(summary = "获得合同精简列表", description = "只包含的合同,主要用于前端的下拉选项") @Parameter(name = "customerId", description = "客户编号", required = true) @PreAuthorize("@ss.hasPermission('crm:contract:query')") - public CommonResult> getListAllSimpleByCustomer(@RequestParam("customerId") Long customerId) { - PageResult result = contractService.getContractPageByCustomerId(new CrmContractPageReqVO().setCustomerId(customerId)); - return success(BeanUtils.toBean(result.getList(), CrmContractRespVO.class)); + public CommonResult> getContractSimpleList(@RequestParam("customerId") Long customerId) { + CrmContractPageReqVO pageReqVO = new CrmContractPageReqVO().setCustomerId(customerId); + pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE); // 不分页 + PageResult pageResult = contractService.getContractPageByCustomerId(pageReqVO); + return success(convertList(pageResult.getList(), contract -> new CrmContractRespVO() // 只返回 id、name 等精简字段 + .setId(contract.getId()).setName(contract.getName()))); } } diff --git a/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/controller/admin/receivable/CrmReceivablePlanController.java b/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/controller/admin/receivable/CrmReceivablePlanController.java index 880e165e0..f68ce1561 100644 --- a/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/controller/admin/receivable/CrmReceivablePlanController.java +++ b/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/controller/admin/receivable/CrmReceivablePlanController.java @@ -41,8 +41,7 @@ import java.util.stream.Stream; import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success; import static cn.iocoder.yudao.framework.common.pojo.PageParam.PAGE_SIZE_NONE; -import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertListByFlatMap; -import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet; +import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.*; import static cn.iocoder.yudao.framework.common.util.collection.MapUtils.findAndThen; import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT; import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId; @@ -95,7 +94,14 @@ public class CrmReceivablePlanController { @PreAuthorize("@ss.hasPermission('crm:receivable-plan:query')") public CommonResult getReceivablePlan(@RequestParam("id") Long id) { CrmReceivablePlanDO receivablePlan = receivablePlanService.getReceivablePlan(id); - return success(BeanUtils.toBean(receivablePlan, CrmReceivablePlanRespVO.class)); + return success(buildReceivablePlanDetail(receivablePlan)); + } + + private CrmReceivablePlanRespVO buildReceivablePlanDetail(CrmReceivablePlanDO receivablePlan) { + if (receivablePlan == null) { + return null; + } + return buildReceivableDetailList(Collections.singletonList(receivablePlan)).get(0); } @GetMapping("/page") diff --git a/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/controller/admin/receivable/vo/plan/CrmReceivablePlanSaveReqVO.java b/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/controller/admin/receivable/vo/plan/CrmReceivablePlanSaveReqVO.java index 51b688b02..470734c65 100644 --- a/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/controller/admin/receivable/vo/plan/CrmReceivablePlanSaveReqVO.java +++ b/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/controller/admin/receivable/vo/plan/CrmReceivablePlanSaveReqVO.java @@ -14,9 +14,8 @@ public class CrmReceivablePlanSaveReqVO { @Schema(description = "编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "2") private Long id; - @Schema(description = "客户编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "2") - @NotNull(message = "客户编号不能为空") - private Long customerId; + @Schema(description = "客户编号", hidden = true, example = "2") + private Long customerId; // 该字段不通过前端传递,而是 contractId 查询出来设置进去 @Schema(description = "合同编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "2") @NotNull(message = "合同编号不能为空") @@ -37,10 +36,6 @@ public class CrmReceivablePlanSaveReqVO { @NotNull(message = "计划回款金额不能为空") private BigDecimal price; - @Schema(description = "提醒日期", requiredMode = Schema.RequiredMode.REQUIRED, example = "1") - @NotNull(message = "提醒日期不能为空") - private LocalDateTime remindTime; - @Schema(description = "提前几天提醒", example = "1") private Integer remindDays; diff --git a/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/dal/mysql/receivable/CrmReceivablePlanMapper.java b/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/dal/mysql/receivable/CrmReceivablePlanMapper.java index e253e2536..2cce6233e 100644 --- a/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/dal/mysql/receivable/CrmReceivablePlanMapper.java +++ b/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/dal/mysql/receivable/CrmReceivablePlanMapper.java @@ -25,6 +25,13 @@ import java.util.Objects; @Mapper public interface CrmReceivablePlanMapper extends BaseMapperX { + default CrmReceivablePlanDO selectMaxPeriodByContractId(Long contractId) { + return selectOne(new MPJLambdaWrapperX() + .eq(CrmReceivablePlanDO::getContractId, contractId) + .orderByDesc(CrmReceivablePlanDO::getPeriod) + .last("LIMIT 1")); + } + default PageResult selectPageByCustomerId(CrmReceivablePlanPageReqVO reqVO) { MPJLambdaWrapperX query = new MPJLambdaWrapperX<>(); if (Objects.nonNull(reqVO.getContractNo())) { // 根据合同编号检索 @@ -90,6 +97,9 @@ public interface CrmReceivablePlanMapper extends BaseMapperX getContractList(Collection ids) { if (CollUtil.isEmpty(ids)) { diff --git a/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/service/receivable/CrmReceivablePlanServiceImpl.java b/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/service/receivable/CrmReceivablePlanServiceImpl.java index 35748d85d..5df708d90 100644 --- a/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/service/receivable/CrmReceivablePlanServiceImpl.java +++ b/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/service/receivable/CrmReceivablePlanServiceImpl.java @@ -2,20 +2,17 @@ package cn.iocoder.yudao.module.crm.service.receivable; import cn.hutool.core.collection.CollUtil; import cn.hutool.core.collection.ListUtil; -import cn.hutool.core.util.ObjectUtil; import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.framework.common.util.object.BeanUtils; import cn.iocoder.yudao.module.crm.controller.admin.receivable.vo.plan.CrmReceivablePlanPageReqVO; import cn.iocoder.yudao.module.crm.controller.admin.receivable.vo.plan.CrmReceivablePlanSaveReqVO; 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.CrmReceivablePlanDO; import cn.iocoder.yudao.module.crm.dal.mysql.receivable.CrmReceivablePlanMapper; 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; @@ -33,7 +30,8 @@ import java.util.List; import java.util.Objects; import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; -import static cn.iocoder.yudao.module.crm.enums.ErrorCodeConstants.*; +import static cn.iocoder.yudao.module.crm.enums.ErrorCodeConstants.RECEIVABLE_PLAN_NOT_EXISTS; +import static cn.iocoder.yudao.module.crm.enums.ErrorCodeConstants.RECEIVABLE_PLAN_UPDATE_FAIL; import static cn.iocoder.yudao.module.crm.enums.LogRecordConstants.*; /** @@ -54,8 +52,6 @@ public class CrmReceivablePlanServiceImpl implements CrmReceivablePlanService { @Resource private CrmContractService contractService; @Resource - private CrmCustomerService customerService; - @Resource private CrmPermissionService permissionService; @Resource @@ -66,15 +62,16 @@ public class CrmReceivablePlanServiceImpl implements CrmReceivablePlanService { @LogRecord(type = CRM_RECEIVABLE_PLAN_TYPE, subType = CRM_RECEIVABLE_PLAN_CREATE_SUB_TYPE, bizNo = "{{#receivablePlan.id}}", success = CRM_RECEIVABLE_PLAN_CREATE_SUCCESS) public Long createReceivablePlan(CrmReceivablePlanSaveReqVO createReqVO) { - // 1.1 校验关联数据是否存在 + // 1. 校验关联数据是否存在 validateRelationDataExists(createReqVO); - // 1.2 查验关联合同回款数量 - Long count = receivableService.getReceivableCountByContractId(createReqVO.getContractId()); - int period = (int) (count + 1); // 2. 插入还款计划 - CrmReceivablePlanDO receivablePlan = BeanUtils.toBean(createReqVO, CrmReceivablePlanDO.class) - .setPeriod(period).setFinishStatus(false); + CrmReceivablePlanDO maxPeriodReceivablePlan = receivablePlanMapper.selectMaxPeriodByContractId(createReqVO.getContractId()); + int period = maxPeriodReceivablePlan == null ? 1 : maxPeriodReceivablePlan.getPeriod() + 1; + CrmReceivablePlanDO receivablePlan = BeanUtils.toBean(createReqVO, CrmReceivablePlanDO.class).setPeriod(period); + if (createReqVO.getReturnTime() != null && createReqVO.getRemindDays() != null) { + receivablePlan.setRemindTime(createReqVO.getReturnTime().minusDays(createReqVO.getRemindDays())); + } receivablePlanMapper.insert(receivablePlan); // 3. 创建数据权限 @@ -93,15 +90,21 @@ public class CrmReceivablePlanServiceImpl implements CrmReceivablePlanService { success = CRM_RECEIVABLE_PLAN_UPDATE_SUCCESS) @CrmPermission(bizType = CrmBizTypeEnum.CRM_RECEIVABLE_PLAN, bizId = "#updateReqVO.id", level = CrmPermissionLevelEnum.WRITE) public void updateReceivablePlan(CrmReceivablePlanSaveReqVO updateReqVO) { - // 1. 校验存在 + updateReqVO.setOwnerUserId(null).setCustomerId(null).setContractId(null); // 防止修改这些字段 + // 1.1 校验存在 validateRelationDataExists(updateReqVO); + // 1.2 校验关联数据是否存在 CrmReceivablePlanDO oldReceivablePlan = validateReceivablePlanExists(updateReqVO.getId()); - if (Objects.nonNull(oldReceivablePlan.getReceivableId())) { // 如果已经有对应的还款,则不允许编辑; - throw exception(RECEIVABLE_PLAN_UPDATE_FAIL, "已经有对应的还款"); + // 1.3 如果已经有对应的还款,则不允许编辑 + if (Objects.nonNull(oldReceivablePlan.getReceivableId())) { + throw exception(RECEIVABLE_PLAN_UPDATE_FAIL); } - // 2. 更新 + // 2. 更新还款计划 CrmReceivablePlanDO updateObj = BeanUtils.toBean(updateReqVO, CrmReceivablePlanDO.class); + if (updateReqVO.getReturnTime() != null && updateReqVO.getRemindDays() != null) { + updateObj.setRemindTime(updateReqVO.getReturnTime().minusDays(updateReqVO.getRemindDays())); + } receivablePlanMapper.updateById(updateObj); // 3. 记录操作日志上下文 @@ -109,6 +112,19 @@ public class CrmReceivablePlanServiceImpl implements CrmReceivablePlanService { LogRecordContext.putVariable("receivablePlan", oldReceivablePlan); } + private void validateRelationDataExists(CrmReceivablePlanSaveReqVO reqVO) { + // 校验负责人存在 + if (reqVO.getOwnerUserId() != null) { + adminUserApi.validateUser(reqVO.getOwnerUserId()); + } + // 校验合同存在 + if (reqVO.getContractId() != null) { + CrmContractDO contract = contractService.getContract(reqVO.getContractId()); + reqVO.setCustomerId(contract.getCustomerId()); + } + } + + // TODO @芋艿:优化下这个方法的命名 @Override public void updateReceivableId(Long id, Long receivableId) { // 校验存在 @@ -117,31 +133,21 @@ public class CrmReceivablePlanServiceImpl implements CrmReceivablePlanService { receivablePlanMapper.updateById(new CrmReceivablePlanDO().setReceivableId(receivableId).setFinishStatus(true)); } - private void validateRelationDataExists(CrmReceivablePlanSaveReqVO 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); - } - } - @Override @Transactional(rollbackFor = Exception.class) @LogRecord(type = CRM_RECEIVABLE_PLAN_TYPE, subType = CRM_RECEIVABLE_PLAN_DELETE_SUB_TYPE, bizNo = "{{#id}}", success = CRM_RECEIVABLE_PLAN_DELETE_SUCCESS) @CrmPermission(bizType = CrmBizTypeEnum.CRM_RECEIVABLE_PLAN, bizId = "#id", level = CrmPermissionLevelEnum.OWNER) public void deleteReceivablePlan(Long id) { - // 校验存在 + // 1. 校验存在 CrmReceivablePlanDO receivablePlan = validateReceivablePlanExists(id); - // 删除 + + // 2. 删除 receivablePlanMapper.deleteById(id); - // 删除数据权限 + // 3. 删除数据权限 permissionService.deletePermission(CrmBizTypeEnum.CRM_CUSTOMER.getType(), id); - // 记录操作日志上下文 + + // 4. 记录操作日志上下文 LogRecordContext.putVariable("receivablePlan", receivablePlan); } diff --git a/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/service/receivable/CrmReceivableServiceImpl.java b/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/service/receivable/CrmReceivableServiceImpl.java index fb807c699..1bf5667b9 100644 --- a/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/service/receivable/CrmReceivableServiceImpl.java +++ b/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/service/receivable/CrmReceivableServiceImpl.java @@ -245,6 +245,7 @@ public class CrmReceivableServiceImpl implements CrmReceivableService { return receivableMapper.selectCheckReceivablesCount(userId); } + // TODO @芋艿:不确定这个方法还要不要; @Override public Long getReceivableCountByContractId(Long contractId) { return receivableMapper.selectCount(CrmReceivableDO::getContractId, contractId);