From ec67e63c675d1b5453e899b40e8ce802566e3ca0 Mon Sep 17 00:00:00 2001 From: YunaiV Date: Fri, 24 Nov 2023 23:11:58 +0800 Subject: [PATCH] =?UTF-8?q?crm=EF=BC=9Acode=20review=20=E5=AE=A2=E6=88=B7?= =?UTF-8?q?=E7=9A=84=E5=85=AC=E6=B5=B7=E9=A2=86=E5=8F=96=E5=92=8C=E5=88=86?= =?UTF-8?q?=E9=85=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../admin/customer/CrmCustomerController.java | 41 +++++++------- .../dal/mysql/customer/CrmCustomerMapper.java | 7 +++ .../service/customer/CrmCustomerService.java | 14 ++--- .../customer/CrmCustomerServiceImpl.java | 56 +++++++++++++++++-- 4 files changed, 83 insertions(+), 35 deletions(-) diff --git a/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/controller/admin/customer/CrmCustomerController.java b/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/controller/admin/customer/CrmCustomerController.java index 8c48d8df2..8d7055686 100644 --- a/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/controller/admin/customer/CrmCustomerController.java +++ b/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/controller/admin/customer/CrmCustomerController.java @@ -16,6 +16,7 @@ import cn.iocoder.yudao.module.system.api.user.AdminUserApi; import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Parameter; +import io.swagger.v3.oas.annotations.Parameters; import io.swagger.v3.oas.annotations.tags.Tag; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.util.CollectionUtils; @@ -164,30 +165,26 @@ public class CrmCustomerController { return success(true); } - //@PutMapping("/receive") - //@Operation(summary = "领取公海客户") - //// TODO @xiaqing:1)receiveCustomer 方法名字;2)cIds 改成 ids,要加下 @RequestParam,还有 swagger 注解;3)参数非空,使用 validator 校验;4)返回 true 即可; - //@PreAuthorize("@ss.hasPermission('crm:customer:receive')") - //public CommonResult receiveByIds(List cIds) { - // // 判断是否为空 - // if (CollectionUtils.isEmpty(cIds)) - // return error(GlobalErrorCodeConstants.BAD_REQUEST.getCode(), GlobalErrorCodeConstants.BAD_REQUEST.getMsg()); - // // 领取公海任务 - // // TODO @xiaqing:userid,通过 controller 传递给 service,不要在 service 里面获取,无状态 - // customerService.receive(cIds); - // return success("领取成功"); - //} + @PutMapping("/receive") + @Operation(summary = "领取公海客户") + @Parameter(name = "ids", description = "编号数组", required = true,example = "1,2,3") + @PreAuthorize("@ss.hasPermission('crm:customer:receive')") + public CommonResult receiveCustomer(@RequestParam(value = "ids") List ids){ + customerService.receiveCustomer(ids, getLoginUserId()); + return success(true); + } - // TODO @xiaqing:1)distributeCustomer 方法名;2)cIds 同上;3)参数校验,同上;4)ownerId 改成 ownerUserId,和别的模块统一;5)返回 true 即可; - @PutMapping("/distributeByIds") + @PutMapping("/distribute") @Operation(summary = "分配公海给对应负责人") - @PreAuthorize("@ss.hasPermission('crm:customer:distributeByIds')") - public CommonResult distributeByIds(Long ownerId, List cIds) { - //判断参数不能为空 - if (ownerId == null || CollectionUtils.isEmpty(cIds)) - return error(GlobalErrorCodeConstants.BAD_REQUEST.getCode(), GlobalErrorCodeConstants.BAD_REQUEST.getMsg()); - customerService.distributeByIds(cIds, ownerId); - return success("分配成功"); + @Parameters({ + @Parameter(name = "ownerUserId", description = "分配的负责人编号", required = true, example = "12345"), + @Parameter(name = "ids", description = "客户编号数组", required = true, example = "1,2,3") + }) + @PreAuthorize("@ss.hasPermission('crm:customer:distribute')") + public CommonResult distributeCustomer(@RequestParam(value = "ownerUserId") Long ownerUserId, + @RequestParam(value = "ids") List ids){ + customerService.distributeCustomer(ids, ownerUserId); + return success(true); } } diff --git a/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/dal/mysql/customer/CrmCustomerMapper.java b/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/dal/mysql/customer/CrmCustomerMapper.java index b281b12e9..c9c38d5f0 100644 --- a/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/dal/mysql/customer/CrmCustomerMapper.java +++ b/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/dal/mysql/customer/CrmCustomerMapper.java @@ -10,6 +10,7 @@ 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.enums.common.CrmSceneEnum; import cn.iocoder.yudao.module.crm.framework.enums.CrmBizTypeEnum; +import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import org.apache.ibatis.annotations.Mapper; @@ -107,4 +108,10 @@ public interface CrmCustomerMapper extends BaseMapperX { return new PageResult<>(mpPage.getRecords(), mpPage.getTotal()); } + default void updateCustomerByOwnerUserIdIsNull(Long id, CrmCustomerDO updateObj) { + update(updateObj, new LambdaUpdateWrapper() + .eq(CrmCustomerDO::getId, id) + .isNull(CrmCustomerDO::getOwnerUserId)); + } + } diff --git a/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/service/customer/CrmCustomerService.java b/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/service/customer/CrmCustomerService.java index 497196aa5..2aec682f7 100644 --- a/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/service/customer/CrmCustomerService.java +++ b/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/service/customer/CrmCustomerService.java @@ -85,25 +85,21 @@ public interface CrmCustomerService { */ void lockCustomer(@Valid CrmCustomerUpdateReqVO updateReqVO); - // TODO @xiaqing:根据 controller 的建议,改下 - /** * 领取公海客户 * - * @param ids 要领取的客户 id + * @param ids 要领取的客户编号数组 */ - void receive(List ids); - - // TODO @xiaqing:根据 controller 的建议,改下 + void receiveCustomer(Listids, Long ownerUserId); /** * 分配公海客户 * - * @param cIds 要分配的客户 id - * @param ownerId 分配的负责人id + * @param ids 要分配的客户编号数组 + * @param ownerUserId 分配的负责人编号 * @author xiaqing */ - void distributeByIds(List cIds, Long ownerId); + void distributeCustomer(Listids, Long ownerUserId); /** * 领取公海客户 diff --git a/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/service/customer/CrmCustomerServiceImpl.java b/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/service/customer/CrmCustomerServiceImpl.java index e555896f1..b4d3b3479 100644 --- a/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/service/customer/CrmCustomerServiceImpl.java +++ b/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/service/customer/CrmCustomerServiceImpl.java @@ -149,13 +149,61 @@ public class CrmCustomerServiceImpl implements CrmCustomerService { @Override @Transactional(rollbackFor = Exception.class) - public void receive(List ids) { - transferCustomerOwner(ids, SecurityFrameworkUtils.getLoginUserId()); + public void receiveCustomer(List ids,Long ownerUserId) { + transferCustomerOwner(ids, ownerUserId); } @Override - public void distributeByIds(List cIds, Long ownerId) { - transferCustomerOwner(cIds, ownerId); + @Transactional(rollbackFor = Exception.class) + public void distributeCustomer(List ids, Long ownerUserId) { + transferCustomerOwner(ids, ownerUserId); + } + + /** + * 转移客户负责人 + * + * @param ids 客户编号数组 + * @param ownerUserId 负责人编号 + */ + private void transferCustomerOwner(List ids, Long ownerUserId) { + // 先一次性加载所有数据,校验客户是否可用 + List customers = customerMapper.selectBatchIds(ids); + for (CrmCustomerDO customer : customers) { + // 校验是否已有负责人 + validateCustomerOwnerExists(customer); + // 校验是否锁定 + validateCustomerIsLocked(customer); + // 校验成交状态 + validateCustomerDeal(customer); + } + + // TODO @QingX:这里是不是改成一次性更新;不然,如果有 20 个客户,就要执行 20 次 SQL 了; + // 统一修改状态 + CrmCustomerDO updateDo = new CrmCustomerDO(); + updateDo.setOwnerUserId(ownerUserId); + // TODO @QingX:如果更新的数量不对,则应该抛出异常,回滚,并错误提示; + for (Long id : ids) { + customerMapper.updateCustomerByOwnerUserIdIsNull(id,updateDo); + } + } + + // TODO @QingX:错误提示里面,可以把客户的名字带上哈;不然不知道是谁; + private void validateCustomerOwnerExists(CrmCustomerDO customer) { + if (customer.getOwnerUserId() != null) { + throw exception(CUSTOMER_OWNER_EXISTS); + } + } + + private void validateCustomerIsLocked(CrmCustomerDO customer) { + if (customer.getLockStatus()) { + throw exception(CUSTOMER_LOCKED); + } + } + + private void validateCustomerDeal(CrmCustomerDO customer) { + if (customer.getDealStatus()) { + throw exception(CUSTOMER_ALREADY_DEAL); + } } @Override