Explorar o código

✨ CRM:完善回款 plan 新增/修改

YunaiV hai 1 ano
pai
achega
91c179029f

+ 1 - 1
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, "客户不存在");

+ 9 - 6
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<List<CrmContractRespVO>> getListAllSimpleByCustomer(@RequestParam("customerId") Long customerId) {
-        PageResult<CrmContractDO> result = contractService.getContractPageByCustomerId(new CrmContractPageReqVO().setCustomerId(customerId));
-        return success(BeanUtils.toBean(result.getList(), CrmContractRespVO.class));
+    public CommonResult<List<CrmContractRespVO>> getContractSimpleList(@RequestParam("customerId") Long customerId) {
+        CrmContractPageReqVO pageReqVO = new CrmContractPageReqVO().setCustomerId(customerId);
+        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE); // 不分页
+        PageResult<CrmContractDO> pageResult = contractService.getContractPageByCustomerId(pageReqVO);
+        return success(convertList(pageResult.getList(), contract -> new CrmContractRespVO() // 只返回 id、name 等精简字段
+                .setId(contract.getId()).setName(contract.getName())));
     }
 
 }

+ 9 - 3
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<CrmReceivablePlanRespVO> 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")

+ 2 - 7
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;
 

+ 10 - 0
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<CrmReceivablePlanDO> {
 
+    default CrmReceivablePlanDO selectMaxPeriodByContractId(Long contractId) {
+        return selectOne(new MPJLambdaWrapperX<CrmReceivablePlanDO>()
+                .eq(CrmReceivablePlanDO::getContractId, contractId)
+                .orderByDesc(CrmReceivablePlanDO::getPeriod)
+                .last("LIMIT 1"));
+    }
+
     default PageResult<CrmReceivablePlanDO> selectPageByCustomerId(CrmReceivablePlanPageReqVO reqVO) {
         MPJLambdaWrapperX<CrmReceivablePlanDO> query = new MPJLambdaWrapperX<>();
         if (Objects.nonNull(reqVO.getContractNo())) { // 根据合同编号检索
@@ -90,6 +97,9 @@ public interface CrmReceivablePlanMapper extends BaseMapperX<CrmReceivablePlanDO
         LocalDateTime endOfToday = LocalDateTimeUtil.endOfDay(LocalDateTime.now());
         query.isNull(CrmReceivablePlanDO::getReceivableId)
                 .between(CrmReceivablePlanDO::getReturnTime, beginOfToday, endOfToday.plusDays(REMIND_DAYS));
+        // TODO return_time 小于现在;
+        // TODO 未还款
+        // TODO remind_time 大于现在;
         return selectCount(query);
     }
 

+ 8 - 0
yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/service/contract/CrmContractService.java

@@ -88,6 +88,14 @@ public interface CrmContractService {
      */
     CrmContractDO getContract(Long id);
 
+    /**
+     * 校验合同是否合法
+     *
+     * @param id 编号
+     * @return 合同
+     */
+    CrmContractDO validateContract(Long id);
+
     /**
      * 获得合同列表
      *

+ 5 - 0
yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/service/contract/CrmContractServiceImpl.java

@@ -323,6 +323,11 @@ public class CrmContractServiceImpl implements CrmContractService {
         return contractMapper.selectById(id);
     }
 
+    @Override
+    public CrmContractDO validateContract(Long id) {
+        return validateContractExists(id);
+    }
+
     @Override
     public List<CrmContractDO> getContractList(Collection<Long> ids) {
         if (CollUtil.isEmpty(ids)) {

+ 38 - 32
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);
     }
 

+ 1 - 0
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);