Ver código fonte

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

YunaiV 1 ano atrás
pai
commit
ec67e63c67

+ 20 - 23
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<String> receiveByIds(List<Long> 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("领取成功");
-    //}
-
-    // TODO @xiaqing:1)distributeCustomer 方法名;2)cIds 同上;3)参数校验,同上;4)ownerId 改成 ownerUserId,和别的模块统一;5)返回 true 即可;
-    @PutMapping("/distributeByIds")
+    @PutMapping("/receive")
+    @Operation(summary = "领取公海客户")
+    @Parameter(name = "ids", description = "编号数组", required = true,example = "1,2,3")
+    @PreAuthorize("@ss.hasPermission('crm:customer:receive')")
+    public CommonResult<Boolean> receiveCustomer(@RequestParam(value = "ids") List<Long> ids){
+        customerService.receiveCustomer(ids, getLoginUserId());
+        return success(true);
+    }
+
+    @PutMapping("/distribute")
     @Operation(summary = "分配公海给对应负责人")
-    @PreAuthorize("@ss.hasPermission('crm:customer:distributeByIds')")
-    public CommonResult<String> distributeByIds(Long ownerId, List<Long> 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<Boolean> distributeCustomer(@RequestParam(value = "ownerUserId") Long ownerUserId,
+                                                    @RequestParam(value = "ids") List<Long> ids){
+        customerService.distributeCustomer(ids, ownerUserId);
+        return success(true);
     }
 
 }

+ 7 - 0
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<CrmCustomerDO> {
         return new PageResult<>(mpPage.getRecords(), mpPage.getTotal());
     }
 
+    default void updateCustomerByOwnerUserIdIsNull(Long id, CrmCustomerDO updateObj) {
+        update(updateObj, new LambdaUpdateWrapper<CrmCustomerDO>()
+                .eq(CrmCustomerDO::getId, id)
+                .isNull(CrmCustomerDO::getOwnerUserId));
+    }
+
 }

+ 5 - 9
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<Long> ids);
-
-    // TODO @xiaqing:根据 controller 的建议,改下
+    void receiveCustomer(List<Long>ids, Long ownerUserId);
 
     /**
      * 分配公海客户
      *
-     * @param cIds    要分配的客户 id
-     * @param ownerId 分配的负责人id
+     * @param ids 要分配的客户编号数组
+     * @param ownerUserId 分配的负责人编号
      * @author xiaqing
      */
-    void distributeByIds(List<Long> cIds, Long ownerId);
+    void distributeCustomer(List<Long>ids, Long ownerUserId);
 
     /**
      * 领取公海客户

+ 52 - 4
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<Long> ids) {
-        transferCustomerOwner(ids, SecurityFrameworkUtils.getLoginUserId());
+    public void receiveCustomer(List <Long> ids,Long ownerUserId) {
+        transferCustomerOwner(ids, ownerUserId);
     }
 
     @Override
-    public void distributeByIds(List<Long> cIds, Long ownerId) {
-        transferCustomerOwner(cIds, ownerId);
+    @Transactional(rollbackFor = Exception.class)
+    public void distributeCustomer(List <Long> ids, Long ownerUserId) {
+        transferCustomerOwner(ids, ownerUserId);
+    }
+
+    /**
+     * 转移客户负责人
+     *
+     * @param ids 客户编号数组
+     * @param ownerUserId 负责人编号
+     */
+    private void transferCustomerOwner(List <Long> ids, Long ownerUserId) {
+        // 先一次性加载所有数据,校验客户是否可用
+        List <CrmCustomerDO> 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