crm联系人review修改

This commit is contained in:
zyna 2023-11-27 23:42:04 +08:00
parent fd2af50202
commit 1580789334
8 changed files with 97 additions and 117 deletions

View File

@ -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<ContactRespVO> 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<ContactDO> list = contactService.getContactList(exportReqVO);
// TODO @zya可以改成直接调用 getContactPage 方法只要把 ContactPageReqVO 设置成不分页PAGE_SIZE_NONE
// 导出 Excel
List<ContactRespVO> contactRespVOList = convertFieldValue2Name(list);
ExcelUtils.write(response, "crm 联系人.xls", "数据", ContactRespVO.class, contactRespVOList);
exportReqVO.setPageNo(PageParam.PAGE_SIZE_NONE);
PageResult<ContactDO> pageResult = contactService.getContactPage(exportReqVO);
PageResult<ContactRespVO> exportPage = convertFieldValue2Name(pageResult);
ExcelUtils.write(response, "crm 联系人.xls", "数据", ContactRespVO.class, exportPage.getList());
}
// TODO 芋艿后续会合并下
/**
* 翻译字段名称
* @param contactDOList 联系人List
*
* @param pageResult 联系人分页参数
* @return List<ContactRespVO>
*/
private List<ContactRespVO> convertFieldValue2Name(List<ContactDO> contactDOList){
private PageResult<ContactRespVO> convertFieldValue2Name(PageResult<ContactDO> pageResult) {
List<ContactDO> contactDOList = pageResult.getList();
// 1. 获取客户列表
// TODO @zyna简单的转换可以使用 CollectionUtils.convertSet
List<Long> customerIdList = contactDOList.stream().map(ContactDO::getCustomerId).distinct().collect(Collectors.toList());
List<CrmCustomerDO> crmCustomerDOList = crmCustomerService.getCustomerList(customerIdList);
List<CrmCustomerDO> crmCustomerDOList = crmCustomerService.getCustomerList(convertSet(contactDOList, ContactDO::getCustomerId));
// 2. 获取创建人责任人列表
// TODO @zyna简单的转换可以使用 CollectionUtils.convertSetByFlatMap
List<Long> userIdsList = contactDOList.stream().flatMap(item-> Stream.of(Long.parseLong(item.getCreator()),item.getOwnerUserId()).distinct()).collect(Collectors.toList());
List<Long> userIdsList = convertListByFlatMap(contactDOList, item -> Stream.of(NumberUtils.parseLong(item.getCreator()), item.getOwnerUserId())
.filter(Objects::nonNull));
Map<Long, AdminUserRespDTO> userMap = adminUserApi.getUserMap(userIdsList);
// 3. 直属上级
List<Long> contactIdsList = contactDOList.stream().map(ContactDO::getParentId).distinct().collect(Collectors.toList());
Set<Long> contactIdsList = convertSet(contactDOList, ContactDO::getParentId);
List<ContactDO> contactList = contactService.getContactList(contactIdsList);
return ContactConvert.INSTANCE.converList(contactDOList,userMap,crmCustomerDOList,contactList);
return ContactConvert.INSTANCE.convertPage(pageResult, userMap, crmCustomerDOList, contactList);
}
}

View File

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

View File

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

View File

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

View File

@ -8,10 +8,12 @@ import lombok.ToString;
@Data
@ToString(callSuper = true)
public class ContactSimpleRespVO {
@Schema(description = "姓名", example = "芋艿") // TODO @zynarequiredMode = 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;
}

View File

@ -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<ContactRespVO> convertPage(PageResult<ContactDO> page);
default PageResult<ContactRespVO> convertPage(PageResult<ContactDO> pageResult, Map<Long, AdminUserRespDTO> userMap,
List<CrmCustomerDO> customerList, List<ContactDO> parentContactList) {
List<ContactRespVO> list = converList(pageResult.getList(), userMap, customerList, parentContactList);
return convertPage(pageResult).setList(list);
}
List<ContactSimpleRespVO> convertAllList(List<ContactDO> list);
@Mappings({
@ -63,16 +70,20 @@ public interface ContactConvert {
findAndThen(contactMap, contactDO.getParentId(), contact -> contactVO.setParentName(contact.getName()));
return contactVO;
}
default List<ContactRespVO> converList(List<ContactDO> contactList, Map<Long, AdminUserRespDTO> userMap,
List<CrmCustomerDO> customerList, List<ContactDO> parentContactList) {
List<ContactRespVO> result = convertList(contactList);
// TODO @zyna简单的转换可以使用 CollectionUtils.convertMap
Map<Long, ContactDO> parentContactMap = parentContactList.stream().collect(Collectors.toMap(ContactDO::getId,v->v));
Map<Long, CrmCustomerDO> customerMap = customerList.stream().collect(Collectors.toMap(CrmCustomerDO::getId,v->v));
Map<Long, ContactDO> parentContactMap = convertMap(parentContactList, ContactDO::getId);
Map<Long, CrmCustomerDO> 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;
}

View File

@ -19,45 +19,25 @@ public interface ContactMapper extends BaseMapperX<ContactDO> {
default PageResult<ContactDO> selectPage(ContactPageReqVO reqVO) {
return selectPage(reqVO, new LambdaQueryWrapperX<ContactDO>()
.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<ContactDO> selectList(ContactPageReqVO reqVO) {
return selectList(new LambdaQueryWrapperX<ContactDO>()
.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));
}

View File

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