Commit 1842549634911731bfa84d4d02b2dc6055a947cc

Authored by YunaiV
1 parent 9a5f6f13

crm:code review 客户的公海领取和分配

yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/controller/admin/customer/CrmCustomerController.java
1 package cn.iocoder.yudao.module.crm.controller.admin.customer; 1 package cn.iocoder.yudao.module.crm.controller.admin.customer;
2 2
3 import cn.hutool.core.collection.CollUtil; 3 import cn.hutool.core.collection.CollUtil;
4 -import cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants;  
5 import cn.iocoder.yudao.framework.common.pojo.CommonResult; 4 import cn.iocoder.yudao.framework.common.pojo.CommonResult;
6 import cn.iocoder.yudao.framework.common.pojo.PageResult; 5 import cn.iocoder.yudao.framework.common.pojo.PageResult;
7 import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils; 6 import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
8 import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog; 7 import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
9 -import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;  
10 import cn.iocoder.yudao.module.crm.controller.admin.customer.vo.*; 8 import cn.iocoder.yudao.module.crm.controller.admin.customer.vo.*;
11 import cn.iocoder.yudao.module.crm.convert.customer.CrmCustomerConvert; 9 import cn.iocoder.yudao.module.crm.convert.customer.CrmCustomerConvert;
12 import cn.iocoder.yudao.module.crm.dal.dataobject.customer.CrmCustomerDO; 10 import cn.iocoder.yudao.module.crm.dal.dataobject.customer.CrmCustomerDO;
@@ -24,7 +22,6 @@ import io.swagger.v3.oas.annotations.Parameter; @@ -24,7 +22,6 @@ import io.swagger.v3.oas.annotations.Parameter;
24 import io.swagger.v3.oas.annotations.Parameters; 22 import io.swagger.v3.oas.annotations.Parameters;
25 import io.swagger.v3.oas.annotations.tags.Tag; 23 import io.swagger.v3.oas.annotations.tags.Tag;
26 import org.springframework.security.access.prepost.PreAuthorize; 24 import org.springframework.security.access.prepost.PreAuthorize;
27 -import org.springframework.util.CollectionUtils;  
28 import org.springframework.validation.annotation.Validated; 25 import org.springframework.validation.annotation.Validated;
29 import org.springframework.web.bind.annotation.*; 26 import org.springframework.web.bind.annotation.*;
30 27
@@ -37,7 +34,6 @@ import java.util.List; @@ -37,7 +34,6 @@ import java.util.List;
37 import java.util.Map; 34 import java.util.Map;
38 import java.util.Set; 35 import java.util.Set;
39 36
40 -import static cn.iocoder.yudao.framework.common.pojo.CommonResult.error;  
41 import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success; 37 import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
42 import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertMap; 38 import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertMap;
43 import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet; 39 import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet;
@@ -185,24 +181,23 @@ public class CrmCustomerController { @@ -185,24 +181,23 @@ public class CrmCustomerController {
185 181
186 @PutMapping("/receive") 182 @PutMapping("/receive")
187 @Operation(summary = "领取公海客户") 183 @Operation(summary = "领取公海客户")
188 - @Parameter(name = "ids", description = "批量领取公海的客户id集合", required = true,example = "1,2,3") 184 + @Parameter(name = "ids", description = "编号数组", required = true,example = "1,2,3")
189 @PreAuthorize("@ss.hasPermission('crm:customer:receive')") 185 @PreAuthorize("@ss.hasPermission('crm:customer:receive')")
190 public CommonResult<Boolean> receiveCustomer(@RequestParam(value = "ids") List<Long> ids){ 186 public CommonResult<Boolean> receiveCustomer(@RequestParam(value = "ids") List<Long> ids){
191 - // 领取公海任务  
192 - customerService.receiveCustomer(ids, SecurityFrameworkUtils.getLoginUserId()); 187 + customerService.receiveCustomer(ids, getLoginUserId());
193 return success(true); 188 return success(true);
194 } 189 }
195 190
196 - @PutMapping("/distributeByIds") 191 + @PutMapping("/distribute")
197 @Operation(summary = "分配公海给对应负责人") 192 @Operation(summary = "分配公海给对应负责人")
198 @Parameters({ 193 @Parameters({
199 - @Parameter(name = "ownerUserId", description = "分配的负责人id", required = true,example = "12345"),  
200 - @Parameter(name = "ids", description = "批量分配的公海的客户id集合", required = true,example = "1,2,3") 194 + @Parameter(name = "ownerUserId", description = "分配的负责人编号", required = true, example = "12345"),
  195 + @Parameter(name = "ids", description = "客户编号数组", required = true, example = "1,2,3")
201 }) 196 })
202 @PreAuthorize("@ss.hasPermission('crm:customer:distribute')") 197 @PreAuthorize("@ss.hasPermission('crm:customer:distribute')")
203 public CommonResult<Boolean> distributeCustomer(@RequestParam(value = "ownerUserId") Long ownerUserId, 198 public CommonResult<Boolean> distributeCustomer(@RequestParam(value = "ownerUserId") Long ownerUserId,
204 - @RequestParam(value = "ids") List<Long>ids){  
205 - customerService.distributeCustomer(ids,ownerUserId); 199 + @RequestParam(value = "ids") List<Long> ids){
  200 + customerService.distributeCustomer(ids, ownerUserId);
206 return success(true); 201 return success(true);
207 } 202 }
208 203
yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/dal/mysql/customer/CrmCustomerMapper.java
@@ -28,10 +28,10 @@ public interface CrmCustomerMapper extends BaseMapperX&lt;CrmCustomerDO&gt; { @@ -28,10 +28,10 @@ public interface CrmCustomerMapper extends BaseMapperX&lt;CrmCustomerDO&gt; {
28 .eqIfPresent(CrmCustomerDO::getSource, pageReqVO.getSource())); 28 .eqIfPresent(CrmCustomerDO::getSource, pageReqVO.getSource()));
29 } 29 }
30 30
31 - default void updateCustomerOwnerUser(Long id,CrmCustomerDO customerDO){  
32 - update(customerDO,new LambdaUpdateWrapper <CrmCustomerDO>()  
33 - .eq(CrmCustomerDO::getId,id)  
34 - .isNull(CrmCustomerDO::getOwnerUserId)  
35 - ); 31 + default void updateCustomerByOwnerUserIdIsNull(Long id, CrmCustomerDO updateObj) {
  32 + update(updateObj, new LambdaUpdateWrapper<CrmCustomerDO>()
  33 + .eq(CrmCustomerDO::getId, id)
  34 + .isNull(CrmCustomerDO::getOwnerUserId));
36 } 35 }
  36 +
37 } 37 }
yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/service/customer/CrmCustomerService.java
@@ -88,17 +88,17 @@ public interface CrmCustomerService { @@ -88,17 +88,17 @@ public interface CrmCustomerService {
88 /** 88 /**
89 * 领取公海客户 89 * 领取公海客户
90 * 90 *
91 - * @param ids 要领取的客户 id 91 + * @param ids 要领取的客户编号数组
92 */ 92 */
93 void receiveCustomer(List<Long>ids, Long ownerUserId); 93 void receiveCustomer(List<Long>ids, Long ownerUserId);
94 94
95 /** 95 /**
96 * 分配公海客户 96 * 分配公海客户
97 * 97 *
98 - * @param ids 要分配的客户 id  
99 - * @param ownerUserId 分配的负责人id 98 + * @param ids 要分配的客户编号数组
  99 + * @param ownerUserId 分配的负责人编号
100 * @author xiaqing 100 * @author xiaqing
101 */ 101 */
102 - void distributeCustomer(List<Long>ids,Long ownerUserId); 102 + void distributeCustomer(List<Long>ids, Long ownerUserId);
103 103
104 } 104 }
yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/service/customer/CrmCustomerServiceImpl.java
@@ -4,7 +4,6 @@ import cn.hutool.core.collection.CollUtil; @@ -4,7 +4,6 @@ import cn.hutool.core.collection.CollUtil;
4 import cn.hutool.core.util.ObjUtil; 4 import cn.hutool.core.util.ObjUtil;
5 import cn.iocoder.yudao.framework.common.pojo.PageResult; 5 import cn.iocoder.yudao.framework.common.pojo.PageResult;
6 import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils; 6 import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
7 -import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;  
8 import cn.iocoder.yudao.module.crm.controller.admin.customer.vo.*; 7 import cn.iocoder.yudao.module.crm.controller.admin.customer.vo.*;
9 import cn.iocoder.yudao.module.crm.convert.customer.CrmCustomerConvert; 8 import cn.iocoder.yudao.module.crm.convert.customer.CrmCustomerConvert;
10 import cn.iocoder.yudao.module.crm.dal.dataobject.customer.CrmCustomerDO; 9 import cn.iocoder.yudao.module.crm.dal.dataobject.customer.CrmCustomerDO;
@@ -83,11 +82,6 @@ public class CrmCustomerServiceImpl implements CrmCustomerService { @@ -83,11 +82,6 @@ public class CrmCustomerServiceImpl implements CrmCustomerService {
83 throw exception(CUSTOMER_NOT_EXISTS); 82 throw exception(CUSTOMER_NOT_EXISTS);
84 } 83 }
85 } 84 }
86 - private void validateCustomerExists(CrmCustomerDO customerDO){  
87 - if (customerDO == null) {  
88 - throw exception(CUSTOMER_NOT_EXISTS);  
89 - }  
90 - }  
91 85
92 @Override 86 @Override
93 @CrmPermission(bizType = CrmBizTypeEnum.CRM_CUSTOMER, bizId = "#id", level = CrmPermissionLevelEnum.READ) 87 @CrmPermission(bizType = CrmBizTypeEnum.CRM_CUSTOMER, bizId = "#id", level = CrmPermissionLevelEnum.READ)
@@ -168,51 +162,58 @@ public class CrmCustomerServiceImpl implements CrmCustomerService { @@ -168,51 +162,58 @@ public class CrmCustomerServiceImpl implements CrmCustomerService {
168 @Override 162 @Override
169 @Transactional(rollbackFor = Exception.class) 163 @Transactional(rollbackFor = Exception.class)
170 public void receiveCustomer(List <Long> ids,Long ownerUserId) { 164 public void receiveCustomer(List <Long> ids,Long ownerUserId) {
171 - transferCustomerOwner(ids,ownerUserId); 165 + transferCustomerOwner(ids, ownerUserId);
172 } 166 }
173 167
174 @Override 168 @Override
  169 + @Transactional(rollbackFor = Exception.class)
175 public void distributeCustomer(List <Long> ids, Long ownerUserId) { 170 public void distributeCustomer(List <Long> ids, Long ownerUserId) {
176 - transferCustomerOwner(ids,ownerUserId); 171 + transferCustomerOwner(ids, ownerUserId);
177 } 172 }
178 173
  174 + /**
  175 + * 转移客户负责人
  176 + *
  177 + * @param ids 客户编号数组
  178 + * @param ownerUserId 负责人编号
  179 + */
179 private void transferCustomerOwner(List <Long> ids, Long ownerUserId) { 180 private void transferCustomerOwner(List <Long> ids, Long ownerUserId) {
180 // 先一次性加载所有数据,校验客户是否可用 181 // 先一次性加载所有数据,校验客户是否可用
181 - List <CrmCustomerDO> customerDOList = customerMapper.selectBatchIds(ids);  
182 - for (CrmCustomerDO customerDO : customerDOList) {  
183 - // 校验客户是否存在  
184 - validateCustomerExists(customerDO); 182 + List <CrmCustomerDO> customers = customerMapper.selectBatchIds(ids);
  183 + for (CrmCustomerDO customer : customers) {
185 // 校验是否已有负责人 184 // 校验是否已有负责人
186 - validCustomerOwnerExist(customerDO); 185 + validateCustomerOwnerExists(customer);
187 // 校验是否锁定 186 // 校验是否锁定
188 - validCustomerIsLocked(customerDO); 187 + validateCustomerIsLocked(customer);
189 // 校验成交状态 188 // 校验成交状态
190 - validCustomerDeal(customerDO); 189 + validateCustomerDeal(customer);
191 } 190 }
192 191
  192 + // TODO @QingX:这里是不是改成一次性更新;不然,如果有 20 个客户,就要执行 20 次 SQL 了;
193 // 统一修改状态 193 // 统一修改状态
194 CrmCustomerDO updateDo = new CrmCustomerDO(); 194 CrmCustomerDO updateDo = new CrmCustomerDO();
195 updateDo.setOwnerUserId(ownerUserId); 195 updateDo.setOwnerUserId(ownerUserId);
  196 + // TODO @QingX:如果更新的数量不对,则应该抛出异常,回滚,并错误提示;
196 for (Long id : ids) { 197 for (Long id : ids) {
197 - customerMapper.updateCustomerOwnerUser(id,updateDo); 198 + customerMapper.updateCustomerByOwnerUserIdIsNull(id,updateDo);
198 } 199 }
199 -  
200 } 200 }
201 201
202 - private void validCustomerOwnerExist(CrmCustomerDO customerDO) {  
203 - if (customerDO.getOwnerUserId()!=null) { 202 + // TODO @QingX:错误提示里面,可以把客户的名字带上哈;不然不知道是谁;
  203 + private void validateCustomerOwnerExists(CrmCustomerDO customer) {
  204 + if (customer.getOwnerUserId() != null) {
204 throw exception(CUSTOMER_OWNER_EXISTS); 205 throw exception(CUSTOMER_OWNER_EXISTS);
205 } 206 }
206 } 207 }
207 208
208 - private void validCustomerIsLocked(CrmCustomerDO customerDO) {  
209 - if (customerDO.getLockStatus() ==true) { 209 + private void validateCustomerIsLocked(CrmCustomerDO customer) {
  210 + if (customer.getLockStatus()) {
210 throw exception(CUSTOMER_LOCKED); 211 throw exception(CUSTOMER_LOCKED);
211 } 212 }
212 } 213 }
213 214
214 - private void validCustomerDeal(CrmCustomerDO customerDO) {  
215 - if (customerDO.getDealStatus() ==true) { 215 + private void validateCustomerDeal(CrmCustomerDO customer) {
  216 + if (customer.getDealStatus()) {
216 throw exception(CUSTOMER_ALREADY_DEAL); 217 throw exception(CUSTOMER_ALREADY_DEAL);
217 } 218 }
218 } 219 }