diff --git a/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/controller/admin/contact/ContactController.java b/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/controller/admin/contact/ContactController.java index 32f72ddc9..87850cc7f 100644 --- a/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/controller/admin/contact/ContactController.java +++ b/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/controller/admin/contact/ContactController.java @@ -3,7 +3,9 @@ package cn.iocoder.yudao.module.crm.controller.admin.contact; import cn.hutool.core.collection.CollUtil; import cn.hutool.core.util.NumberUtil; import cn.iocoder.yudao.framework.common.pojo.CommonResult; +import cn.iocoder.yudao.framework.common.pojo.PageParam; import cn.iocoder.yudao.framework.common.pojo.PageResult; +import cn.iocoder.yudao.framework.common.util.number.NumberUtils; 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.contact.vo.*; @@ -28,14 +30,13 @@ import javax.annotation.Resource; import javax.servlet.http.HttpServletResponse; import javax.validation.Valid; import java.io.IOException; -import java.util.Collections; -import java.util.List; -import java.util.Map; -import java.util.stream.Collectors; +import java.util.*; import java.util.stream.Stream; import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success; +import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertListByFlatMap; +import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet; import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT; import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId; @@ -49,7 +50,6 @@ public class ContactController { @Resource private ContactService contactService; - // TODO @zyna:模块内,注入的变量,不用带 crm 前缀哈 @Resource private CrmCustomerService crmCustomerService; @@ -115,43 +115,41 @@ public class ContactController { if (CollUtil.isEmpty(pageResult.getList())) { return success(PageResult.empty(pageResult.getTotal())); } - List contactVOList = convertFieldValue2Name(pageResult.getList()); - return success(new PageResult<>(contactVOList, pageResult.getTotal())); + return success(convertFieldValue2Name(pageResult)); } - // TODO @zyna:可以看下新的导出写法,这里调整下 @GetMapping("/export-excel") @Operation(summary = "导出联系人 Excel") @PreAuthorize("@ss.hasPermission('crm:contact:export')") @OperateLog(type = EXPORT) public void exportContactExcel(@Valid ContactPageReqVO exportReqVO, - HttpServletResponse response) throws IOException { - List list = contactService.getContactList(exportReqVO); - // TODO @zya:可以改成直接调用 getContactPage 方法;只要把 ContactPageReqVO 设置成不分页,PAGE_SIZE_NONE - // 导出 Excel - List contactRespVOList = convertFieldValue2Name(list); - ExcelUtils.write(response, "crm 联系人.xls", "数据", ContactRespVO.class, contactRespVOList); + HttpServletResponse response) throws IOException { + exportReqVO.setPageNo(PageParam.PAGE_SIZE_NONE); + PageResult pageResult = contactService.getContactPage(exportReqVO); + PageResult exportPage = convertFieldValue2Name(pageResult); + ExcelUtils.write(response, "crm 联系人.xls", "数据", ContactRespVO.class, exportPage.getList()); } // TODO 芋艿:后续会合并下, + /** * 翻译字段名称 - * @param contactDOList 联系人List + * + * @param pageResult 联系人分页参数 * @return List */ - private List convertFieldValue2Name(List contactDOList){ + private PageResult convertFieldValue2Name(PageResult pageResult) { + List contactDOList = pageResult.getList(); // 1. 获取客户列表 - // TODO @zyna:简单的转换,可以使用 CollectionUtils.convertSet - List customerIdList = contactDOList.stream().map(ContactDO::getCustomerId).distinct().collect(Collectors.toList()); - List crmCustomerDOList = crmCustomerService.getCustomerList(customerIdList); + List crmCustomerDOList = crmCustomerService.getCustomerList(convertSet(contactDOList, ContactDO::getCustomerId)); // 2. 获取创建人、责任人列表 - // TODO @zyna:简单的转换,可以使用 CollectionUtils.convertSetByFlatMap - List userIdsList = contactDOList.stream().flatMap(item-> Stream.of(Long.parseLong(item.getCreator()),item.getOwnerUserId()).distinct()).collect(Collectors.toList()); + List userIdsList = convertListByFlatMap(contactDOList, item -> Stream.of(NumberUtils.parseLong(item.getCreator()), item.getOwnerUserId()) + .filter(Objects::nonNull)); Map userMap = adminUserApi.getUserMap(userIdsList); // 3. 直属上级 - List contactIdsList = contactDOList.stream().map(ContactDO::getParentId).distinct().collect(Collectors.toList()); + Set contactIdsList = convertSet(contactDOList, ContactDO::getParentId); List contactList = contactService.getContactList(contactIdsList); - return ContactConvert.INSTANCE.converList(contactDOList,userMap,crmCustomerDOList,contactList); + return ContactConvert.INSTANCE.convertPage(pageResult, userMap, crmCustomerDOList, contactList); } } diff --git a/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/controller/admin/contact/vo/ContactBaseVO.java b/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/controller/admin/contact/vo/ContactBaseVO.java index 6f6a57891..250e78193 100644 --- a/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/controller/admin/contact/vo/ContactBaseVO.java +++ b/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/controller/admin/contact/vo/ContactBaseVO.java @@ -5,7 +5,7 @@ import cn.iocoder.yudao.framework.common.validation.Telephone; import cn.iocoder.yudao.framework.excel.core.annotations.DictFormat; import cn.iocoder.yudao.framework.excel.core.convert.DictConvert; import cn.iocoder.yudao.module.infra.enums.DictTypeConstants; -import com.alibaba.excel.annotation.ExcelIgnore; +import com.alibaba.excel.annotation.ExcelIgnoreUnannotated; import com.alibaba.excel.annotation.ExcelProperty; import io.swagger.v3.oas.annotations.media.Schema; import lombok.Data; @@ -24,6 +24,7 @@ import static cn.iocoder.yudao.framework.common.util.date.DateUtils.FORMAT_YEAR_ * 如果子 VO 存在差异的字段,请不要添加到这里,影响 Swagger 文档生成 */ @Data +@ExcelIgnoreUnannotated public class ContactBaseVO { @ExcelProperty(value = "姓名",order = 1) @@ -32,7 +33,6 @@ public class ContactBaseVO { private String name; @Schema(description = "客户编号", example = "10795") - @ExcelIgnore private Long customerId; @ExcelProperty(value = "性别", converter = DictConvert.class, order = 3) @@ -50,7 +50,6 @@ public class ContactBaseVO { private Boolean master; @Schema(description = "直属上级", example = "23457") - @ExcelIgnore private Long parentId; @Schema(description = "手机号",example = "1387171766") @@ -97,11 +96,9 @@ public class ContactBaseVO { @Schema(description = "负责人用户编号", example = "14334") @NotNull(message = "负责人不能为空") - @ExcelIgnore // TODO @zyna:可以使用 ExcelIgnoreUnannotated private Long ownerUserId; @Schema(description = "地区编号", example = "20158") - @ExcelIgnore private Integer areaId; } diff --git a/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/controller/admin/contact/vo/ContactPageReqVO.java b/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/controller/admin/contact/vo/ContactPageReqVO.java index db408405e..3d7f0d260 100644 --- a/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/controller/admin/contact/vo/ContactPageReqVO.java +++ b/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/controller/admin/contact/vo/ContactPageReqVO.java @@ -5,26 +5,20 @@ import io.swagger.v3.oas.annotations.media.Schema; 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; @Schema(description = "管理后台 - CRM 联系人分页 Request VO") @Data @EqualsAndHashCode(callSuper = true) @ToString(callSuper = true) public class ContactPageReqVO extends PageParam { - - // TODO @zyna:筛选条件(多余的可以删除哈)还有部分 example 少了,可以补充下; // ●客户: // ●姓名: // ●手机、电话、座机、QQ、微信、邮箱 + @Schema(description = "姓名", example = "芋艿") + private String name; - @Schema(description = "下次联系时间") - @DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND) - private LocalDateTime[] nextTime; + @Schema(description = "客户编号", example = "10795") + private Long customerId; @Schema(description = "手机号", example = "13898273941") private String mobile; @@ -35,45 +29,9 @@ public class ContactPageReqVO extends PageParam { @Schema(description = "电子邮箱", example = "111@22.com") private String email; - @Schema(description = "客户编号", example = "10795") - private Long customerId; - - @Schema(description = "地址") - private String address; - - @Schema(description = "备注", example = "你说的对") - private String remark; - - @Schema(description = "创建时间") - @DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND) - private LocalDateTime[] createTime; - - @Schema(description = "最后跟进时间") - @DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND) - private LocalDateTime[] lastTime; - - @Schema(description = "直属上级", example = "23457") - private Long parentId; - - @Schema(description = "姓名", example = "芋艿") - private String name; - - @Schema(description = "职位") - private String post; - @Schema(description = "QQ", example = "3882872") private Long qq; @Schema(description = "微信", example = "zzZ98373") private String wechat; - - @Schema(description = "性别") - private Integer sex; - - @Schema(description = "是否关键决策人") - private Boolean master; - - @Schema(description = "负责人用户编号", example = "14334") - private Long ownerUserId; - } diff --git a/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/controller/admin/contact/vo/ContactRespVO.java b/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/controller/admin/contact/vo/ContactRespVO.java index 438a0b336..0e76fa398 100644 --- a/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/controller/admin/contact/vo/ContactRespVO.java +++ b/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/controller/admin/contact/vo/ContactRespVO.java @@ -1,6 +1,6 @@ package cn.iocoder.yudao.module.crm.controller.admin.contact.vo; -import com.alibaba.excel.annotation.ExcelIgnore; +import com.alibaba.excel.annotation.ExcelIgnoreUnannotated; import com.alibaba.excel.annotation.ExcelProperty; import io.swagger.v3.oas.annotations.media.Schema; import lombok.*; @@ -10,18 +10,21 @@ import java.time.LocalDateTime; @Data @EqualsAndHashCode(callSuper = true) @ToString(callSuper = true) +@ExcelIgnoreUnannotated public class ContactRespVO extends ContactBaseVO { @Schema(description = "主键", requiredMode = Schema.RequiredMode.REQUIRED, example = "3167") - @ExcelIgnore private Long id; @Schema(description = "创建时间") @ExcelProperty(value = "创建时间",order = 8) private LocalDateTime createTime; + @Schema(description = "更新时间") + @ExcelProperty(value = "更新时间",order = 8) + private LocalDateTime updateTime; + @Schema(description = "创建人", example = "25682") - @ExcelIgnore private String creator; @Schema(description = "创建人名字", example = "test") diff --git a/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/controller/admin/contact/vo/ContactSimpleRespVO.java b/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/controller/admin/contact/vo/ContactSimpleRespVO.java index 98d7da034..51342d353 100644 --- a/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/controller/admin/contact/vo/ContactSimpleRespVO.java +++ b/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/controller/admin/contact/vo/ContactSimpleRespVO.java @@ -8,10 +8,12 @@ import lombok.ToString; @Data @ToString(callSuper = true) public class ContactSimpleRespVO { - @Schema(description = "姓名", example = "芋艿") // TODO @zyna:requiredMode = Schema.RequiredMode.REQUIRED;需要空一行;字段的顺序改下,id 在 name 前面,会更干净 - private String name; @Schema(description = "主键", requiredMode = Schema.RequiredMode.REQUIRED, example = "3167") private Long id; + @Schema(description = "姓名", example = "芋艿") + private String name; + + } diff --git a/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/convert/contact/ContactConvert.java b/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/convert/contact/ContactConvert.java index c4452e076..81767422d 100644 --- a/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/convert/contact/ContactConvert.java +++ b/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/convert/contact/ContactConvert.java @@ -11,10 +11,12 @@ import org.mapstruct.Mapper; import org.mapstruct.Mapping; import org.mapstruct.Mappings; import org.mapstruct.factory.Mappers; + import java.util.List; import java.util.Map; import java.util.stream.Collectors; +import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertMap; import static cn.iocoder.yudao.framework.common.util.collection.MapUtils.findAndThen; /** @@ -37,6 +39,11 @@ public interface ContactConvert { PageResult convertPage(PageResult page); + default PageResult convertPage(PageResult pageResult, Map userMap, + List customerList, List parentContactList) { + List list = converList(pageResult.getList(), userMap, customerList, parentContactList); + return convertPage(pageResult).setList(list); + } List convertAllList(List list); @Mappings({ @@ -48,31 +55,35 @@ public interface ContactConvert { /** * 转换详情信息 * - * @param contactDO 联系人 - * @param userMap 用户列表 + * @param contactDO 联系人 + * @param userMap 用户列表 * @param crmCustomerDOList 客户 * @return ContactRespVO */ - default ContactRespVO convert(ContactDO contactDO, Map userMap, List crmCustomerDOList, - List contactList) { + default ContactRespVO convert(ContactDO contactDO, Map userMap, List crmCustomerDOList, + List contactList) { ContactRespVO contactVO = convert(contactDO); setUserInfo(contactVO, userMap); - Map ustomerMap = crmCustomerDOList.stream().collect(Collectors.toMap(CrmCustomerDO::getId,v->v)); - Map contactMap = contactList.stream().collect(Collectors.toMap(ContactDO::getId,v->v)); + Map ustomerMap = crmCustomerDOList.stream().collect(Collectors.toMap(CrmCustomerDO::getId, v -> v)); + Map contactMap = contactList.stream().collect(Collectors.toMap(ContactDO::getId, v -> v)); findAndThen(ustomerMap, contactDO.getCustomerId(), customer -> contactVO.setCustomerName(customer.getName())); findAndThen(contactMap, contactDO.getParentId(), contact -> contactVO.setParentName(contact.getName())); return contactVO; } - default List converList(List contactList, Map userMap, - List customerList, List parentContactList) { + + default List converList(List contactList, Map userMap, + List customerList, List parentContactList) { List result = convertList(contactList); - // TODO @zyna:简单的转换,可以使用 CollectionUtils.convertMap - Map parentContactMap = parentContactList.stream().collect(Collectors.toMap(ContactDO::getId,v->v)); - Map customerMap = customerList.stream().collect(Collectors.toMap(CrmCustomerDO::getId,v->v)); + Map parentContactMap = convertMap(parentContactList, ContactDO::getId); + Map customerMap = convertMap(customerList, CrmCustomerDO::getId); result.forEach(item -> { setUserInfo(item, userMap); - findAndThen(customerMap,item.getCustomerId(),customer -> {item.setCustomerName(customer.getName());}); - findAndThen(parentContactMap,item.getParentId(),contactDO -> {item.setParentName(contactDO.getName());}); + findAndThen(customerMap, item.getCustomerId(), customer -> { + item.setCustomerName(customer.getName()); + }); + findAndThen(parentContactMap, item.getParentId(), contactDO -> { + item.setParentName(contactDO.getName()); + }); }); return result; } @@ -80,13 +91,13 @@ public interface ContactConvert { /** * 设置用户信息 * - * @param contactRespVO 联系人Response VO - * @param userMap 用户信息 map + * @param contactRespVO 联系人Response VO + * @param userMap 用户信息 map */ - static void setUserInfo(ContactRespVO contactRespVO, Map userMap){ + static void setUserInfo(ContactRespVO contactRespVO, Map userMap) { contactRespVO.setAreaName(AreaUtils.format(contactRespVO.getAreaId())); findAndThen(userMap, contactRespVO.getOwnerUserId(), user -> { - contactRespVO.setOwnerUserName(user == null?"":user.getNickname()); + contactRespVO.setOwnerUserName(user == null ? "" : user.getNickname()); }); findAndThen(userMap, Long.parseLong(contactRespVO.getCreator()), user -> contactRespVO.setCreatorName(user.getNickname())); } diff --git a/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/dal/mysql/contact/ContactMapper.java b/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/dal/mysql/contact/ContactMapper.java index 9db1691cb..1ccb9e662 100644 --- a/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/dal/mysql/contact/ContactMapper.java +++ b/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/dal/mysql/contact/ContactMapper.java @@ -19,45 +19,25 @@ public interface ContactMapper extends BaseMapperX { default PageResult selectPage(ContactPageReqVO reqVO) { return selectPage(reqVO, new LambdaQueryWrapperX() - .betweenIfPresent(ContactDO::getNextTime, reqVO.getNextTime()) .eqIfPresent(ContactDO::getMobile, reqVO.getMobile()) .eqIfPresent(ContactDO::getTelephone, reqVO.getTelephone()) .eqIfPresent(ContactDO::getEmail, reqVO.getEmail()) .eqIfPresent(ContactDO::getCustomerId, reqVO.getCustomerId()) - .eqIfPresent(ContactDO::getAddress, reqVO.getAddress()) - .eqIfPresent(ContactDO::getRemark, reqVO.getRemark()) - .betweenIfPresent(ContactDO::getCreateTime, reqVO.getCreateTime()) - .betweenIfPresent(ContactDO::getLastTime, reqVO.getLastTime()) - .eqIfPresent(ContactDO::getParentId, reqVO.getParentId()) .likeIfPresent(ContactDO::getName, reqVO.getName()) - .eqIfPresent(ContactDO::getPost, reqVO.getPost()) .eqIfPresent(ContactDO::getQq, reqVO.getQq()) .eqIfPresent(ContactDO::getWechat, reqVO.getWechat()) - .eqIfPresent(ContactDO::getSex, reqVO.getSex()) - .eqIfPresent(ContactDO::getMaster, reqVO.getMaster()) - .eqIfPresent(ContactDO::getOwnerUserId, reqVO.getOwnerUserId()) .orderByDesc(ContactDO::getId)); } default List selectList(ContactPageReqVO reqVO) { return selectList(new LambdaQueryWrapperX() - .betweenIfPresent(ContactDO::getNextTime, reqVO.getNextTime()) .eqIfPresent(ContactDO::getMobile, reqVO.getMobile()) .eqIfPresent(ContactDO::getTelephone, reqVO.getTelephone()) .eqIfPresent(ContactDO::getEmail, reqVO.getEmail()) .eqIfPresent(ContactDO::getCustomerId, reqVO.getCustomerId()) - .eqIfPresent(ContactDO::getAddress, reqVO.getAddress()) - .eqIfPresent(ContactDO::getRemark, reqVO.getRemark()) - .betweenIfPresent(ContactDO::getCreateTime, reqVO.getCreateTime()) - .betweenIfPresent(ContactDO::getLastTime, reqVO.getLastTime()) - .eqIfPresent(ContactDO::getParentId, reqVO.getParentId()) .likeIfPresent(ContactDO::getName, reqVO.getName()) - .eqIfPresent(ContactDO::getPost, reqVO.getPost()) .eqIfPresent(ContactDO::getQq, reqVO.getQq()) .eqIfPresent(ContactDO::getWechat, reqVO.getWechat()) - .eqIfPresent(ContactDO::getSex, reqVO.getSex()) - .eqIfPresent(ContactDO::getMaster, reqVO.getMaster()) - .eqIfPresent(ContactDO::getOwnerUserId, reqVO.getOwnerUserId()) .orderByDesc(ContactDO::getId)); } diff --git a/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/service/contact/ContactServiceImpl.java b/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/service/contact/ContactServiceImpl.java index e7408b836..94c4d1e11 100644 --- a/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/service/contact/ContactServiceImpl.java +++ b/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/service/contact/ContactServiceImpl.java @@ -3,6 +3,7 @@ package cn.iocoder.yudao.module.crm.service.contact; 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.contact.vo.ContactBaseVO; import cn.iocoder.yudao.module.crm.controller.admin.contact.vo.ContactCreateReqVO; import cn.iocoder.yudao.module.crm.controller.admin.contact.vo.ContactPageReqVO; import cn.iocoder.yudao.module.crm.controller.admin.contact.vo.ContactUpdateReqVO; @@ -12,8 +13,10 @@ import cn.iocoder.yudao.module.crm.dal.mysql.contact.ContactMapper; import cn.iocoder.yudao.module.crm.framework.core.annotations.CrmPermission; import cn.iocoder.yudao.module.crm.framework.enums.CrmBizTypeEnum; import cn.iocoder.yudao.module.crm.framework.enums.CrmPermissionLevelEnum; +import cn.iocoder.yudao.module.crm.service.customer.CrmCustomerService; import cn.iocoder.yudao.module.crm.service.permission.CrmPermissionService; import cn.iocoder.yudao.module.crm.service.permission.bo.CrmPermissionCreateReqBO; +import cn.iocoder.yudao.module.system.api.user.AdminUserApi; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.validation.annotation.Validated; @@ -21,9 +24,12 @@ import org.springframework.validation.annotation.Validated; import javax.annotation.Resource; import java.util.Collection; import java.util.List; +import java.util.Optional; import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; import static cn.iocoder.yudao.module.crm.enums.ErrorCodeConstants.CONTACT_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; /** * crm联系人 Service 实现类 @@ -37,11 +43,20 @@ public class ContactServiceImpl implements ContactService { @Resource private ContactMapper contactMapper; + @Resource + private CrmCustomerService customerService; + + @Resource + private AdminUserApi adminUserApi; + @Resource private CrmPermissionService crmPermissionService; - @Override // TODO @zyna:新增和修改时,关联字段要校验,例如说 直属上级,是不是真的存在; + @Override public Long createContact(ContactCreateReqVO createReqVO, Long userId) { + //@todo + //校验 + validateDataExist(createReqVO); // 插入 ContactDO contact = ContactConvert.INSTANCE.convert(createReqVO); contactMapper.insert(contact); @@ -60,6 +75,7 @@ public class ContactServiceImpl implements ContactService { public void updateContact(ContactUpdateReqVO updateReqVO) { // 校验存在 validateContactExists(updateReqVO.getId()); + validateDataExist(updateReqVO); // 更新 ContactDO updateObj = ContactConvert.INSTANCE.convert(updateReqVO); contactMapper.updateById(updateObj); @@ -109,4 +125,19 @@ public class ContactServiceImpl implements ContactService { public List getContactList() { return contactMapper.selectList(); } + + private void validateDataExist(ContactBaseVO contactBaseVO){ + //1.校验客户 + if (contactBaseVO.getCustomerId() != null) { + Optional.ofNullable(customerService.getCustomer(contactBaseVO.getCustomerId())).orElseThrow(() -> exception(CUSTOMER_NOT_EXISTS)); + } + //2.校验负责人 + if (contactBaseVO.getOwnerUserId() != null) { + Optional.ofNullable(adminUserApi.getUser(contactBaseVO.getOwnerUserId())).orElseThrow(() -> exception(USER_NOT_EXISTS)); + } + //3.直属上级 + if (contactBaseVO.getParentId() != null) { + Optional.ofNullable(contactMapper.selectById(contactBaseVO.getParentId())).orElseThrow(() -> exception(CONTACT_NOT_EXISTS)); + } + } }