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

This commit is contained in:
YunaiV 2023-11-24 23:11:58 +08:00
parent 0d821d705d
commit ec67e63c67
4 changed files with 83 additions and 35 deletions

View File

@ -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 @xiaqing1receiveCustomer 方法名字2cIds 改成 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 @xiaqinguserid通过 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<Boolean> receiveCustomer(@RequestParam(value = "ids") List<Long> ids){
customerService.receiveCustomer(ids, getLoginUserId());
return success(true);
}
// TODO @xiaqing1distributeCustomer 方法名2cIds 同上3参数校验同上4ownerId 改成 ownerUserId和别的模块统一5返回 true 即可
@PutMapping("/distributeByIds")
@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);
}
}

View File

@ -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));
}
}

View File

@ -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);
/**
* 领取公海客户

View File

@ -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