CRM:【线索】创建、更新线索时添加校验,改用相同vo

This commit is contained in:
min 2024-01-09 01:28:21 +08:00
parent 4d68e5e747
commit 81f838c5d2
8 changed files with 100 additions and 57 deletions

View File

@ -5,6 +5,7 @@ import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.crm.controller.admin.clue.vo.*;
import cn.iocoder.yudao.module.crm.controller.admin.contact.vo.CrmContactSaveReqVO;
import cn.iocoder.yudao.module.crm.convert.clue.CrmClueConvert;
import cn.iocoder.yudao.module.crm.dal.dataobject.clue.CrmClueDO;
import cn.iocoder.yudao.module.crm.service.clue.CrmClueService;
@ -38,14 +39,14 @@ public class CrmClueController {
@PostMapping("/create")
@Operation(summary = "创建线索")
@PreAuthorize("@ss.hasPermission('crm:clue:create')")
public CommonResult<Long> createClue(@Valid @RequestBody CrmClueCreateReqVO createReqVO) {
public CommonResult<Long> createClue(@Valid @RequestBody CrmClueSaveReqVO createReqVO) {
return success(clueService.createClue(createReqVO));
}
@PutMapping("/update")
@Operation(summary = "更新线索")
@PreAuthorize("@ss.hasPermission('crm:clue:update')")
public CommonResult<Boolean> updateClue(@Valid @RequestBody CrmClueUpdateReqVO updateReqVO) {
public CommonResult<Boolean> updateClue(@Valid @RequestBody CrmClueSaveReqVO updateReqVO) {
clueService.updateClue(updateReqVO);
return success(true);
}

View File

@ -1,14 +0,0 @@
package cn.iocoder.yudao.module.crm.controller.admin.clue.vo;
import lombok.*;
import java.util.*;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.*;
@Schema(description = "管理后台 - 线索创建 Request VO")
@Data
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
public class CrmClueCreateReqVO extends CrmClueBaseVO {
}

View File

@ -0,0 +1,58 @@
package cn.iocoder.yudao.module.crm.controller.admin.clue.vo;
import cn.iocoder.yudao.framework.common.validation.Mobile;
import cn.iocoder.yudao.framework.common.validation.Telephone;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import org.springframework.format.annotation.DateTimeFormat;
import java.time.LocalDateTime;
import static cn.iocoder.yudao.framework.common.util.date.DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND;
/**
* @author minhx
*/
@Schema(description = "管理后台 - CRM 线索 创建/更新 Request VO")
@Data
public class CrmClueSaveReqVO {
@Schema(description = "编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "10969")
private Long id;
@Schema(description = "线索名称", requiredMode = Schema.RequiredMode.REQUIRED, example = "线索xxx")
@NotEmpty(message = "线索名称不能为空")
private String name;
@Schema(description = "客户 id", requiredMode = Schema.RequiredMode.REQUIRED, example = "520")
private Long customerId;
@Schema(description = "下次联系时间", example = "2023-10-18 01:00:00")
@DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND)
private LocalDateTime contactNextTime;
@Schema(description = "电话", example = "18000000000")
@Telephone
private String telephone;
@Schema(description = "手机号", example = "18000000000")
@Mobile
private String mobile;
@Schema(description = "地址", example = "北京市海淀区")
private String address;
@Schema(description = "最后跟进时间")
@DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND)
private LocalDateTime contactLastTime;
@Schema(description = "负责人编号")
private Long ownerUserId;
@Schema(description = "备注", example = "随便")
private String remark;
}

View File

@ -1,20 +0,0 @@
package cn.iocoder.yudao.module.crm.controller.admin.clue.vo;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import jakarta.validation.constraints.NotNull;
@Schema(description = "管理后台 - 线索更新 Request VO")
@Data
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
public class CrmClueUpdateReqVO extends CrmClueBaseVO {
@Schema(description = "编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "10969")
@NotNull(message = "编号不能为空")
private Long id;
}

View File

@ -2,6 +2,7 @@ package cn.iocoder.yudao.module.crm.convert.clue;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.crm.controller.admin.clue.vo.*;
import cn.iocoder.yudao.module.crm.controller.admin.contact.vo.CrmContactSaveReqVO;
import cn.iocoder.yudao.module.crm.dal.dataobject.clue.CrmClueDO;
import cn.iocoder.yudao.module.crm.service.permission.bo.CrmPermissionTransferReqBO;
import org.mapstruct.Mapper;
@ -20,9 +21,7 @@ public interface CrmClueConvert {
CrmClueConvert INSTANCE = Mappers.getMapper(CrmClueConvert.class);
CrmClueDO convert(CrmClueCreateReqVO bean);
CrmClueDO convert(CrmClueUpdateReqVO bean);
CrmClueDO convert(CrmClueSaveReqVO bean);
CrmClueRespVO convert(CrmClueDO bean);

View File

@ -2,6 +2,7 @@ package cn.iocoder.yudao.module.crm.service.clue;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.crm.controller.admin.clue.vo.*;
import cn.iocoder.yudao.module.crm.controller.admin.contact.vo.CrmContactSaveReqVO;
import cn.iocoder.yudao.module.crm.dal.dataobject.clue.CrmClueDO;
import jakarta.validation.Valid;
@ -21,14 +22,14 @@ public interface CrmClueService {
* @param createReqVO 创建信息
* @return 编号
*/
Long createClue(@Valid CrmClueCreateReqVO createReqVO);
Long createClue(@Valid CrmClueSaveReqVO createReqVO);
/**
* 更新线索
*
* @param updateReqVO 更新信息
*/
void updateClue(@Valid CrmClueUpdateReqVO updateReqVO);
void updateClue(@Valid CrmClueSaveReqVO updateReqVO);
/**
* 删除线索

View File

@ -4,6 +4,7 @@ import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.crm.controller.admin.clue.vo.*;
import cn.iocoder.yudao.module.crm.controller.admin.contact.vo.CrmContactSaveReqVO;
import cn.iocoder.yudao.module.crm.convert.clue.CrmClueConvert;
import cn.iocoder.yudao.module.crm.convert.customer.CrmCustomerConvert;
import cn.iocoder.yudao.module.crm.dal.dataobject.clue.CrmClueDO;
@ -13,6 +14,7 @@ import cn.iocoder.yudao.module.crm.enums.permission.CrmPermissionLevelEnum;
import cn.iocoder.yudao.module.crm.framework.permission.core.annotations.CrmPermission;
import cn.iocoder.yudao.module.crm.service.customer.CrmCustomerService;
import cn.iocoder.yudao.module.crm.service.permission.CrmPermissionService;
import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@ -24,6 +26,8 @@ import java.util.Objects;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.crm.enums.ErrorCodeConstants.CLUE_NOT_EXISTS;
import static cn.iocoder.yudao.module.crm.enums.ErrorCodeConstants.CUSTOMER_NOT_EXISTS;
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.USER_NOT_EXISTS;
/**
* 线索 Service 实现类
@ -39,15 +43,18 @@ public class CrmClueServiceImpl implements CrmClueService {
@Resource
private CrmCustomerService customerService;
@Resource
private CrmPermissionService crmPermissionService;
@Resource
private AdminUserApi adminUserApi;
@Override
public Long createClue(CrmClueCreateReqVO createReqVO) {
// 如果传入客户校验客户是否存在
if (Objects.nonNull(createReqVO.getCustomerId())) {
customerService.validateCustomer(createReqVO.getCustomerId());
}
public Long createClue(CrmClueSaveReqVO createReqVO) {
// 校验关联数据
validateRelationDataExists(createReqVO);
// 插入
CrmClueDO clue = CrmClueConvert.INSTANCE.convert(createReqVO);
clueMapper.insert(clue);
@ -58,13 +65,11 @@ public class CrmClueServiceImpl implements CrmClueService {
@Override
@CrmPermission(bizType = CrmBizTypeEnum.CRM_LEADS, bizId = "#updateReqVO.id", level = CrmPermissionLevelEnum.WRITE)
public void updateClue(CrmClueUpdateReqVO updateReqVO) {
// 校验存在
public void updateClue(CrmClueSaveReqVO updateReqVO) {
// 校验线索是否存在
validateClueExists(updateReqVO.getId());
// 如果传入客户校验客户是否存在
if (Objects.nonNull(updateReqVO.getCustomerId())) {
customerService.validateCustomer(updateReqVO.getCustomerId());
}
// 校验关联数据
validateRelationDataExists(updateReqVO);
// 更新
CrmClueDO updateObj = CrmClueConvert.INSTANCE.convert(updateReqVO);
@ -137,4 +142,18 @@ public class CrmClueServiceImpl implements CrmClueService {
clueMapper.updateById(clueDO);
});
}
private void validateRelationDataExists(CrmClueSaveReqVO reqVO) {
// 校验客户
if (Objects.nonNull(reqVO.getCustomerId()) &&
Objects.isNull(customerService.getCustomer(reqVO.getCustomerId()))) {
throw exception(CUSTOMER_NOT_EXISTS);
}
// 校验负责人
// 2. 校验负责人
if (Objects.nonNull(reqVO.getOwnerUserId()) &&
Objects.isNull(adminUserApi.getUser(reqVO.getOwnerUserId()))) {
throw exception(USER_NOT_EXISTS);
}
}
}

View File

@ -2,9 +2,8 @@ package cn.iocoder.yudao.module.crm.service.clue;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest;
import cn.iocoder.yudao.module.crm.controller.admin.clue.vo.CrmClueCreateReqVO;
import cn.iocoder.yudao.module.crm.controller.admin.clue.vo.CrmCluePageReqVO;
import cn.iocoder.yudao.module.crm.controller.admin.clue.vo.CrmClueUpdateReqVO;
import cn.iocoder.yudao.module.crm.controller.admin.clue.vo.CrmClueSaveReqVO;
import cn.iocoder.yudao.module.crm.dal.dataobject.clue.CrmClueDO;
import cn.iocoder.yudao.module.crm.dal.mysql.clue.CrmClueMapper;
import jakarta.annotation.Resource;
@ -43,7 +42,7 @@ public class CrmClueServiceImplTest extends BaseDbUnitTest {
@Test
public void testCreateClue_success() {
// 准备参数
CrmClueCreateReqVO reqVO = randomPojo(CrmClueCreateReqVO.class);
CrmClueSaveReqVO reqVO = randomPojo(CrmClueSaveReqVO.class);
// 调用
Long clueId = clueService.createClue(reqVO);
@ -60,7 +59,7 @@ public class CrmClueServiceImplTest extends BaseDbUnitTest {
CrmClueDO dbClue = randomPojo(CrmClueDO.class);
clueMapper.insert(dbClue);// @Sql: 先插入出一条存在的数据
// 准备参数
CrmClueUpdateReqVO reqVO = randomPojo(CrmClueUpdateReqVO.class, o -> {
CrmClueSaveReqVO reqVO = randomPojo(CrmClueSaveReqVO.class, o -> {
o.setId(dbClue.getId()); // 设置更新的 ID
});
@ -74,7 +73,7 @@ public class CrmClueServiceImplTest extends BaseDbUnitTest {
@Test
public void testUpdateClue_notExists() {
// 准备参数
CrmClueUpdateReqVO reqVO = randomPojo(CrmClueUpdateReqVO.class);
CrmClueSaveReqVO reqVO = randomPojo(CrmClueSaveReqVO.class);
// 调用, 并断言异常
assertServiceException(() -> clueService.updateClue(reqVO), CLUE_NOT_EXISTS);