Commit ec67e63c675d1b5453e899b40e8ce802566e3ca0
1 parent
0d821d70
crm:code review 客户的公海领取和分配
Showing
4 changed files
with
84 additions
and
36 deletions
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; |
| 16 | 16 | import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO; |
| 17 | 17 | import io.swagger.v3.oas.annotations.Operation; |
| 18 | 18 | import io.swagger.v3.oas.annotations.Parameter; |
| 19 | +import io.swagger.v3.oas.annotations.Parameters; | |
| 19 | 20 | import io.swagger.v3.oas.annotations.tags.Tag; |
| 20 | 21 | import org.springframework.security.access.prepost.PreAuthorize; |
| 21 | 22 | import org.springframework.util.CollectionUtils; |
| ... | ... | @@ -164,30 +165,26 @@ public class CrmCustomerController { |
| 164 | 165 | return success(true); |
| 165 | 166 | } |
| 166 | 167 | |
| 167 | - //@PutMapping("/receive") | |
| 168 | - //@Operation(summary = "领取公海客户") | |
| 169 | - //// TODO @xiaqing:1)receiveCustomer 方法名字;2)cIds 改成 ids,要加下 @RequestParam,还有 swagger 注解;3)参数非空,使用 validator 校验;4)返回 true 即可; | |
| 170 | - //@PreAuthorize("@ss.hasPermission('crm:customer:receive')") | |
| 171 | - //public CommonResult<String> receiveByIds(List<Long> cIds) { | |
| 172 | - // // 判断是否为空 | |
| 173 | - // if (CollectionUtils.isEmpty(cIds)) | |
| 174 | - // return error(GlobalErrorCodeConstants.BAD_REQUEST.getCode(), GlobalErrorCodeConstants.BAD_REQUEST.getMsg()); | |
| 175 | - // // 领取公海任务 | |
| 176 | - // // TODO @xiaqing:userid,通过 controller 传递给 service,不要在 service 里面获取,无状态 | |
| 177 | - // customerService.receive(cIds); | |
| 178 | - // return success("领取成功"); | |
| 179 | - //} | |
| 180 | - | |
| 181 | - // TODO @xiaqing:1)distributeCustomer 方法名;2)cIds 同上;3)参数校验,同上;4)ownerId 改成 ownerUserId,和别的模块统一;5)返回 true 即可; | |
| 182 | - @PutMapping("/distributeByIds") | |
| 168 | + @PutMapping("/receive") | |
| 169 | + @Operation(summary = "领取公海客户") | |
| 170 | + @Parameter(name = "ids", description = "编号数组", required = true,example = "1,2,3") | |
| 171 | + @PreAuthorize("@ss.hasPermission('crm:customer:receive')") | |
| 172 | + public CommonResult<Boolean> receiveCustomer(@RequestParam(value = "ids") List<Long> ids){ | |
| 173 | + customerService.receiveCustomer(ids, getLoginUserId()); | |
| 174 | + return success(true); | |
| 175 | + } | |
| 176 | + | |
| 177 | + @PutMapping("/distribute") | |
| 183 | 178 | @Operation(summary = "分配公海给对应负责人") |
| 184 | - @PreAuthorize("@ss.hasPermission('crm:customer:distributeByIds')") | |
| 185 | - public CommonResult<String> distributeByIds(Long ownerId, List<Long> cIds) { | |
| 186 | - //判断参数不能为空 | |
| 187 | - if (ownerId == null || CollectionUtils.isEmpty(cIds)) | |
| 188 | - return error(GlobalErrorCodeConstants.BAD_REQUEST.getCode(), GlobalErrorCodeConstants.BAD_REQUEST.getMsg()); | |
| 189 | - customerService.distributeByIds(cIds, ownerId); | |
| 190 | - return success("分配成功"); | |
| 179 | + @Parameters({ | |
| 180 | + @Parameter(name = "ownerUserId", description = "分配的负责人编号", required = true, example = "12345"), | |
| 181 | + @Parameter(name = "ids", description = "客户编号数组", required = true, example = "1,2,3") | |
| 182 | + }) | |
| 183 | + @PreAuthorize("@ss.hasPermission('crm:customer:distribute')") | |
| 184 | + public CommonResult<Boolean> distributeCustomer(@RequestParam(value = "ownerUserId") Long ownerUserId, | |
| 185 | + @RequestParam(value = "ids") List<Long> ids){ | |
| 186 | + customerService.distributeCustomer(ids, ownerUserId); | |
| 187 | + return success(true); | |
| 191 | 188 | } |
| 192 | 189 | |
| 193 | 190 | } | ... | ... |
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; |
| 10 | 10 | import cn.iocoder.yudao.module.crm.dal.dataobject.permission.CrmPermissionDO; |
| 11 | 11 | import cn.iocoder.yudao.module.crm.enums.common.CrmSceneEnum; |
| 12 | 12 | import cn.iocoder.yudao.module.crm.framework.enums.CrmBizTypeEnum; |
| 13 | +import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; | |
| 13 | 14 | import com.baomidou.mybatisplus.core.metadata.IPage; |
| 14 | 15 | import org.apache.ibatis.annotations.Mapper; |
| 15 | 16 | |
| ... | ... | @@ -107,4 +108,10 @@ public interface CrmCustomerMapper extends BaseMapperX<CrmCustomerDO> { |
| 107 | 108 | return new PageResult<>(mpPage.getRecords(), mpPage.getTotal()); |
| 108 | 109 | } |
| 109 | 110 | |
| 111 | + default void updateCustomerByOwnerUserIdIsNull(Long id, CrmCustomerDO updateObj) { | |
| 112 | + update(updateObj, new LambdaUpdateWrapper<CrmCustomerDO>() | |
| 113 | + .eq(CrmCustomerDO::getId, id) | |
| 114 | + .isNull(CrmCustomerDO::getOwnerUserId)); | |
| 115 | + } | |
| 116 | + | |
| 110 | 117 | } | ... | ... |
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 { |
| 85 | 85 | */ |
| 86 | 86 | void lockCustomer(@Valid CrmCustomerUpdateReqVO updateReqVO); |
| 87 | 87 | |
| 88 | - // TODO @xiaqing:根据 controller 的建议,改下 | |
| 89 | - | |
| 90 | 88 | /** |
| 91 | 89 | * 领取公海客户 |
| 92 | 90 | * |
| 93 | - * @param ids 要领取的客户 id | |
| 91 | + * @param ids 要领取的客户编号数组 | |
| 94 | 92 | */ |
| 95 | - void receive(List<Long> ids); | |
| 96 | - | |
| 97 | - // TODO @xiaqing:根据 controller 的建议,改下 | |
| 93 | + void receiveCustomer(List<Long>ids, Long ownerUserId); | |
| 98 | 94 | |
| 99 | 95 | /** |
| 100 | 96 | * 分配公海客户 |
| 101 | 97 | * |
| 102 | - * @param cIds 要分配的客户 id | |
| 103 | - * @param ownerId 分配的负责人id | |
| 98 | + * @param ids 要分配的客户编号数组 | |
| 99 | + * @param ownerUserId 分配的负责人编号 | |
| 104 | 100 | * @author xiaqing |
| 105 | 101 | */ |
| 106 | - void distributeByIds(List<Long> cIds, Long ownerId); | |
| 102 | + void distributeCustomer(List<Long>ids, Long ownerUserId); | |
| 107 | 103 | |
| 108 | 104 | /** |
| 109 | 105 | * 领取公海客户 | ... | ... |
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 { |
| 149 | 149 | |
| 150 | 150 | @Override |
| 151 | 151 | @Transactional(rollbackFor = Exception.class) |
| 152 | - public void receive(List<Long> ids) { | |
| 153 | - transferCustomerOwner(ids, SecurityFrameworkUtils.getLoginUserId()); | |
| 152 | + public void receiveCustomer(List <Long> ids,Long ownerUserId) { | |
| 153 | + transferCustomerOwner(ids, ownerUserId); | |
| 154 | 154 | } |
| 155 | 155 | |
| 156 | 156 | @Override |
| 157 | - public void distributeByIds(List<Long> cIds, Long ownerId) { | |
| 158 | - transferCustomerOwner(cIds, ownerId); | |
| 157 | + @Transactional(rollbackFor = Exception.class) | |
| 158 | + public void distributeCustomer(List <Long> ids, Long ownerUserId) { | |
| 159 | + transferCustomerOwner(ids, ownerUserId); | |
| 160 | + } | |
| 161 | + | |
| 162 | + /** | |
| 163 | + * 转移客户负责人 | |
| 164 | + * | |
| 165 | + * @param ids 客户编号数组 | |
| 166 | + * @param ownerUserId 负责人编号 | |
| 167 | + */ | |
| 168 | + private void transferCustomerOwner(List <Long> ids, Long ownerUserId) { | |
| 169 | + // 先一次性加载所有数据,校验客户是否可用 | |
| 170 | + List <CrmCustomerDO> customers = customerMapper.selectBatchIds(ids); | |
| 171 | + for (CrmCustomerDO customer : customers) { | |
| 172 | + // 校验是否已有负责人 | |
| 173 | + validateCustomerOwnerExists(customer); | |
| 174 | + // 校验是否锁定 | |
| 175 | + validateCustomerIsLocked(customer); | |
| 176 | + // 校验成交状态 | |
| 177 | + validateCustomerDeal(customer); | |
| 178 | + } | |
| 179 | + | |
| 180 | + // TODO @QingX:这里是不是改成一次性更新;不然,如果有 20 个客户,就要执行 20 次 SQL 了; | |
| 181 | + // 统一修改状态 | |
| 182 | + CrmCustomerDO updateDo = new CrmCustomerDO(); | |
| 183 | + updateDo.setOwnerUserId(ownerUserId); | |
| 184 | + // TODO @QingX:如果更新的数量不对,则应该抛出异常,回滚,并错误提示; | |
| 185 | + for (Long id : ids) { | |
| 186 | + customerMapper.updateCustomerByOwnerUserIdIsNull(id,updateDo); | |
| 187 | + } | |
| 188 | + } | |
| 189 | + | |
| 190 | + // TODO @QingX:错误提示里面,可以把客户的名字带上哈;不然不知道是谁; | |
| 191 | + private void validateCustomerOwnerExists(CrmCustomerDO customer) { | |
| 192 | + if (customer.getOwnerUserId() != null) { | |
| 193 | + throw exception(CUSTOMER_OWNER_EXISTS); | |
| 194 | + } | |
| 195 | + } | |
| 196 | + | |
| 197 | + private void validateCustomerIsLocked(CrmCustomerDO customer) { | |
| 198 | + if (customer.getLockStatus()) { | |
| 199 | + throw exception(CUSTOMER_LOCKED); | |
| 200 | + } | |
| 201 | + } | |
| 202 | + | |
| 203 | + private void validateCustomerDeal(CrmCustomerDO customer) { | |
| 204 | + if (customer.getDealStatus()) { | |
| 205 | + throw exception(CUSTOMER_ALREADY_DEAL); | |
| 206 | + } | |
| 159 | 207 | } |
| 160 | 208 | |
| 161 | 209 | @Override | ... | ... |