CRM-联系人:完善数据权限,新增权限关联、场景分页查询,新增权限关联批量查询

This commit is contained in:
puhui999 2023-12-08 18:08:41 +08:00
parent 490418a1fc
commit f69a18fd23
5 changed files with 62 additions and 75 deletions

View File

@ -4,7 +4,6 @@ import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Assert; import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.NumberUtil; import cn.hutool.core.util.NumberUtil;
import cn.iocoder.yudao.framework.common.pojo.CommonResult; 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.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.number.NumberUtils; import cn.iocoder.yudao.framework.common.util.number.NumberUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils; import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
@ -38,6 +37,7 @@ import java.util.stream.Stream;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; 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.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.common.pojo.PageParam.PAGE_SIZE_NONE;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertListByFlatMap; 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.common.util.collection.CollectionUtils.convertSet;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT; import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;
@ -98,7 +98,8 @@ public class CrmContactController {
List<CrmCustomerDO> customerList = customerService.getCustomerList( List<CrmCustomerDO> customerList = customerService.getCustomerList(
Collections.singletonList(contact.getCustomerId()), getLoginUserId()); Collections.singletonList(contact.getCustomerId()), getLoginUserId());
// 3. 直属上级 // 3. 直属上级
List<CrmContactDO> parentContactList = contactService.getContactList(Collections.singletonList(contact.getParentId())); List<CrmContactDO> parentContactList = contactService.getContactList(
Collections.singletonList(contact.getParentId()), getLoginUserId());
return success(ContactConvert.INSTANCE.convert(contact, userMap, customerList, parentContactList)); return success(ContactConvert.INSTANCE.convert(contact, userMap, customerList, parentContactList));
} }
@ -106,7 +107,9 @@ public class CrmContactController {
@Operation(summary = "获得联系人列表") @Operation(summary = "获得联系人列表")
@PreAuthorize("@ss.hasPermission('crm:contact:query')") @PreAuthorize("@ss.hasPermission('crm:contact:query')")
public CommonResult<List<CrmContactSimpleRespVO>> getSimpleContactList() { public CommonResult<List<CrmContactSimpleRespVO>> getSimpleContactList() {
List<CrmContactDO> list = contactService.getContactList(); CrmContactPageReqVO pageReqVO = new CrmContactPageReqVO();
pageReqVO.setPageSize(PAGE_SIZE_NONE);
List<CrmContactDO> list = contactService.getContactPage(pageReqVO, getLoginUserId()).getList();
return success(ContactConvert.INSTANCE.convertAllList(list)); return success(ContactConvert.INSTANCE.convertAllList(list));
} }
@ -114,7 +117,7 @@ public class CrmContactController {
@Operation(summary = "获得联系人分页") @Operation(summary = "获得联系人分页")
@PreAuthorize("@ss.hasPermission('crm:contact:query')") @PreAuthorize("@ss.hasPermission('crm:contact:query')")
public CommonResult<PageResult<CrmContactRespVO>> getContactPage(@Valid CrmContactPageReqVO pageVO) { public CommonResult<PageResult<CrmContactRespVO>> getContactPage(@Valid CrmContactPageReqVO pageVO) {
PageResult<CrmContactDO> pageResult = contactService.getContactPage(pageVO); PageResult<CrmContactDO> pageResult = contactService.getContactPage(pageVO, getLoginUserId());
return success(convertDetailContactPage(pageResult)); return success(convertDetailContactPage(pageResult));
} }
@ -122,7 +125,7 @@ public class CrmContactController {
@Operation(summary = "获得联系人分页,基于指定客户") @Operation(summary = "获得联系人分页,基于指定客户")
public CommonResult<PageResult<CrmContactRespVO>> getContactPageByCustomer(@Valid CrmContactPageReqVO pageVO) { public CommonResult<PageResult<CrmContactRespVO>> getContactPageByCustomer(@Valid CrmContactPageReqVO pageVO) {
Assert.notNull(pageVO.getCustomerId(), "客户编号不能为空"); Assert.notNull(pageVO.getCustomerId(), "客户编号不能为空");
PageResult<CrmContactDO> pageResult = contactService.getContactPageByCustomer(pageVO); PageResult<CrmContactDO> pageResult = contactService.getContactPage(pageVO, getLoginUserId());
return success(convertDetailContactPage(pageResult)); return success(convertDetailContactPage(pageResult));
} }
@ -132,8 +135,8 @@ public class CrmContactController {
@OperateLog(type = EXPORT) @OperateLog(type = EXPORT)
public void exportContactExcel(@Valid CrmContactPageReqVO exportReqVO, public void exportContactExcel(@Valid CrmContactPageReqVO exportReqVO,
HttpServletResponse response) throws IOException { HttpServletResponse response) throws IOException {
exportReqVO.setPageNo(PageParam.PAGE_SIZE_NONE); exportReqVO.setPageNo(PAGE_SIZE_NONE);
PageResult<CrmContactDO> pageResult = contactService.getContactPage(exportReqVO); PageResult<CrmContactDO> pageResult = contactService.getContactPage(exportReqVO, getLoginUserId());
ExcelUtils.write(response, "联系人.xls", "数据", CrmContactRespVO.class, ExcelUtils.write(response, "联系人.xls", "数据", CrmContactRespVO.class,
convertDetailContactPage(pageResult).getList()); convertDetailContactPage(pageResult).getList());
} }
@ -157,7 +160,7 @@ public class CrmContactController {
contact -> Stream.of(NumberUtils.parseLong(contact.getCreator()), contact.getOwnerUserId()))); contact -> Stream.of(NumberUtils.parseLong(contact.getCreator()), contact.getOwnerUserId())));
// 3. 直属上级 // 3. 直属上级
List<CrmContactDO> parentContactList = contactService.getContactList( List<CrmContactDO> parentContactList = contactService.getContactList(
convertSet(contactList, CrmContactDO::getParentId)); convertSet(contactList, CrmContactDO::getParentId), getLoginUserId());
return ContactConvert.INSTANCE.convertPage(pageResult, userMap, crmCustomerDOList, parentContactList); return ContactConvert.INSTANCE.convertPage(pageResult, userMap, crmCustomerDOList, parentContactList);
} }

View File

@ -2,11 +2,17 @@ package cn.iocoder.yudao.module.crm.dal.mysql.contact;
import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX; import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX; import cn.iocoder.yudao.framework.mybatis.core.query.MPJLambdaWrapperX;
import cn.iocoder.yudao.module.crm.controller.admin.contact.vo.CrmContactPageReqVO; import cn.iocoder.yudao.module.crm.controller.admin.contact.vo.CrmContactPageReqVO;
import cn.iocoder.yudao.module.crm.dal.dataobject.contact.CrmContactDO; import cn.iocoder.yudao.module.crm.dal.dataobject.contact.CrmContactDO;
import cn.iocoder.yudao.module.crm.enums.common.CrmBizTypeEnum;
import cn.iocoder.yudao.module.crm.util.CrmQueryWrapperUtils;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Mapper;
import java.util.Collection;
import java.util.List;
/** /**
* CRM 联系人 Mapper * CRM 联系人 Mapper
* *
@ -15,29 +21,34 @@ import org.apache.ibatis.annotations.Mapper;
@Mapper @Mapper
public interface CrmContactMapper extends BaseMapperX<CrmContactDO> { public interface CrmContactMapper extends BaseMapperX<CrmContactDO> {
// TODO @puhui999数据权限 default int updateOwnerUserIdById(Long id, Long ownerUserId) {
default PageResult<CrmContactDO> selectPage(CrmContactPageReqVO reqVO) { return update(new LambdaUpdateWrapper<CrmContactDO>()
return selectPage(reqVO, new LambdaQueryWrapperX<CrmContactDO>() .eq(CrmContactDO::getId, id)
.eqIfPresent(CrmContactDO::getMobile, reqVO.getMobile()) .set(CrmContactDO::getOwnerUserId, ownerUserId));
.eqIfPresent(CrmContactDO::getTelephone, reqVO.getTelephone())
.eqIfPresent(CrmContactDO::getEmail, reqVO.getEmail())
.eqIfPresent(CrmContactDO::getCustomerId, reqVO.getCustomerId())
.likeIfPresent(CrmContactDO::getName, reqVO.getName())
.eqIfPresent(CrmContactDO::getQq, reqVO.getQq())
.eqIfPresent(CrmContactDO::getWechat, reqVO.getWechat())
.orderByDesc(CrmContactDO::getId));
} }
default PageResult<CrmContactDO> selectPageByCustomer(CrmContactPageReqVO pageVO) { default PageResult<CrmContactDO> selectPage(CrmContactPageReqVO pageReqVO, Long userId) {
return selectPage(pageVO, new LambdaQueryWrapperX<CrmContactDO>() MPJLambdaWrapperX<CrmContactDO> mpjLambdaWrapperX = new MPJLambdaWrapperX<>();
.eq(CrmContactDO::getCustomerId, pageVO.getCustomerId()) // 必须传递 // 构建数据权限连表条件
.likeIfPresent(CrmContactDO::getName, pageVO.getName()) CrmQueryWrapperUtils.builderPageQuery(mpjLambdaWrapperX, CrmBizTypeEnum.CRM_CONTACT.getType(), CrmContactDO::getId,
.eqIfPresent(CrmContactDO::getMobile, pageVO.getMobile()) userId, pageReqVO.getSceneType(), pageReqVO.getPool());
.eqIfPresent(CrmContactDO::getTelephone, pageVO.getTelephone()) mpjLambdaWrapperX.selectAll(CrmContactDO.class)
.eqIfPresent(CrmContactDO::getEmail, pageVO.getEmail()) .eq(CrmContactDO::getCustomerId, pageReqVO.getCustomerId()) // 必须传递
.eqIfPresent(CrmContactDO::getQq, pageVO.getQq()) .likeIfPresent(CrmContactDO::getName, pageReqVO.getName())
.eqIfPresent(CrmContactDO::getWechat, pageVO.getWechat()) .eqIfPresent(CrmContactDO::getMobile, pageReqVO.getMobile())
.orderByDesc(CrmContactDO::getId)); .eqIfPresent(CrmContactDO::getTelephone, pageReqVO.getTelephone())
.eqIfPresent(CrmContactDO::getEmail, pageReqVO.getEmail())
.eqIfPresent(CrmContactDO::getQq, pageReqVO.getQq())
.eqIfPresent(CrmContactDO::getWechat, pageReqVO.getWechat())
.orderByDesc(CrmContactDO::getId);
return selectJoinPage(pageReqVO, CrmContactDO.class, mpjLambdaWrapperX);
}
default List<CrmContactDO> selectBatchIds(Collection<Long> ids, Long userId) {
MPJLambdaWrapperX<CrmContactDO> mpjLambdaWrapperX = new MPJLambdaWrapperX<>();
// 构建数据权限连表条件
CrmQueryWrapperUtils.builderListQueryBatch(mpjLambdaWrapperX, CrmBizTypeEnum.CRM_CONTACT.getType(), ids, userId);
return selectJoinList(CrmContactDO.class, mpjLambdaWrapperX);
} }
} }

View File

@ -5,9 +5,8 @@ import cn.iocoder.yudao.module.crm.controller.admin.contact.vo.CrmContactCreateR
import cn.iocoder.yudao.module.crm.controller.admin.contact.vo.CrmContactPageReqVO; import cn.iocoder.yudao.module.crm.controller.admin.contact.vo.CrmContactPageReqVO;
import cn.iocoder.yudao.module.crm.controller.admin.contact.vo.CrmContactUpdateReqVO; import cn.iocoder.yudao.module.crm.controller.admin.contact.vo.CrmContactUpdateReqVO;
import cn.iocoder.yudao.module.crm.dal.dataobject.contact.CrmContactDO; import cn.iocoder.yudao.module.crm.dal.dataobject.contact.CrmContactDO;
import cn.iocoder.yudao.module.crm.dal.dataobject.customer.CrmCustomerDO;
import jakarta.validation.Valid; import jakarta.validation.Valid;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
@ -53,9 +52,10 @@ public interface CrmContactService {
* 获得联系人列表 * 获得联系人列表
* *
* @param ids 编号 * @param ids 编号
* @param userId 用户编号
* @return 联系人列表 * @return 联系人列表
*/ */
List<CrmContactDO> getContactList(Collection<Long> ids); List<CrmContactDO> getContactList(Collection<Long> ids, Long userId);
/** /**
* 获得联系人分页 * 获得联系人分页
@ -63,25 +63,9 @@ public interface CrmContactService {
* 数据权限基于 {@link CrmContactDO} * 数据权限基于 {@link CrmContactDO}
* *
* @param pageReqVO 分页查询 * @param pageReqVO 分页查询
* @param userId 用户编号
* @return 联系人分页 * @return 联系人分页
*/ */
PageResult<CrmContactDO> getContactPage(CrmContactPageReqVO pageReqVO); PageResult<CrmContactDO> getContactPage(CrmContactPageReqVO pageReqVO, Long userId);
/**
* 获得联系人分页基于指定客户
*
* 数据权限基于 {@link CrmCustomerDO} 读取
*
* @param pageReqVO 分页查询
* @return 联系人分页
*/
PageResult<CrmContactDO> getContactPageByCustomer(CrmContactPageReqVO pageReqVO);
/**
* 获取所有联系人列表
*
* @return 所有联系人列表
*/
List<CrmContactDO> getContactList();
} }

View File

@ -10,18 +10,18 @@ import cn.iocoder.yudao.module.crm.controller.admin.contact.vo.CrmContactUpdateR
import cn.iocoder.yudao.module.crm.convert.contact.ContactConvert; import cn.iocoder.yudao.module.crm.convert.contact.ContactConvert;
import cn.iocoder.yudao.module.crm.dal.dataobject.contact.CrmContactDO; import cn.iocoder.yudao.module.crm.dal.dataobject.contact.CrmContactDO;
import cn.iocoder.yudao.module.crm.dal.mysql.contact.CrmContactMapper; import cn.iocoder.yudao.module.crm.dal.mysql.contact.CrmContactMapper;
import cn.iocoder.yudao.module.crm.framework.core.annotations.CrmPermission;
import cn.iocoder.yudao.module.crm.enums.common.CrmBizTypeEnum; import cn.iocoder.yudao.module.crm.enums.common.CrmBizTypeEnum;
import cn.iocoder.yudao.module.crm.enums.permission.CrmPermissionLevelEnum; import cn.iocoder.yudao.module.crm.enums.permission.CrmPermissionLevelEnum;
import cn.iocoder.yudao.module.crm.framework.core.annotations.CrmPermission;
import cn.iocoder.yudao.module.crm.service.customer.CrmCustomerService; 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.CrmPermissionService;
import cn.iocoder.yudao.module.crm.service.permission.bo.CrmPermissionCreateReqBO; import cn.iocoder.yudao.module.crm.service.permission.bo.CrmPermissionCreateReqBO;
import cn.iocoder.yudao.module.system.api.user.AdminUserApi; import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated; import org.springframework.validation.annotation.Validated;
import jakarta.annotation.Resource;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
@ -82,7 +82,7 @@ public class CrmContactServiceImpl implements CrmContactService {
* *
* @param saveReqVO 新增/修改请求 VO * @param saveReqVO 新增/修改请求 VO
*/ */
private void validateRelationDataExists(CrmContactBaseVO saveReqVO){ private void validateRelationDataExists(CrmContactBaseVO saveReqVO) {
// 1. 校验客户 // 1. 校验客户
if (saveReqVO.getCustomerId() != null && customerService.getCustomer(saveReqVO.getCustomerId()) == null) { if (saveReqVO.getCustomerId() != null && customerService.getCustomer(saveReqVO.getCustomerId()) == null) {
throw exception(CUSTOMER_NOT_EXISTS); throw exception(CUSTOMER_NOT_EXISTS);
@ -98,12 +98,14 @@ public class CrmContactServiceImpl implements CrmContactService {
} }
@Override @Override
@CrmPermission(bizType = CrmBizTypeEnum.CRM_CONTACT, bizId = "#id", level = CrmPermissionLevelEnum.WRITE) @CrmPermission(bizType = CrmBizTypeEnum.CRM_CONTACT, bizId = "#id", level = CrmPermissionLevelEnum.OWNER)
public void deleteContact(Long id) { public void deleteContact(Long id) {
// 校验存在 // 校验存在
validateContactExists(id); validateContactExists(id);
// 删除 // 删除
contactMapper.deleteById(id); contactMapper.deleteById(id);
// 删除数据权限
crmPermissionService.deletePermission(CrmBizTypeEnum.CRM_CONTACT.getType(), id);
} }
private void validateContactExists(Long id) { private void validateContactExists(Long id) {
@ -112,7 +114,6 @@ public class CrmContactServiceImpl implements CrmContactService {
} }
} }
// TODO 芋艿是否要做数据权限的校验
@Override @Override
@CrmPermission(bizType = CrmBizTypeEnum.CRM_CONTACT, bizId = "#id", level = CrmPermissionLevelEnum.READ) @CrmPermission(bizType = CrmBizTypeEnum.CRM_CONTACT, bizId = "#id", level = CrmPermissionLevelEnum.READ)
public CrmContactDO getContact(Long id) { public CrmContactDO getContact(Long id) {
@ -120,28 +121,16 @@ public class CrmContactServiceImpl implements CrmContactService {
} }
@Override @Override
public List<CrmContactDO> getContactList(Collection<Long> ids) { public List<CrmContactDO> getContactList(Collection<Long> ids, Long userId) {
if (CollUtil.isEmpty(ids)) { if (CollUtil.isEmpty(ids)) {
return ListUtil.empty(); return ListUtil.empty();
} }
return contactMapper.selectBatchIds(ids); return contactMapper.selectBatchIds(ids, userId);
} }
@Override @Override
public PageResult<CrmContactDO> getContactPage(CrmContactPageReqVO pageReqVO) { public PageResult<CrmContactDO> getContactPage(CrmContactPageReqVO pageReqVO, Long userId) {
// TODO puhui999后面要改成基于数据权限的查询 return contactMapper.selectPage(pageReqVO, userId);
return contactMapper.selectPage(pageReqVO);
}
@Override
@CrmPermission(bizType = CrmBizTypeEnum.CRM_CUSTOMER, bizId = "#pageReqVO.customerId", level = CrmPermissionLevelEnum.READ)
public PageResult<CrmContactDO> getContactPageByCustomer(CrmContactPageReqVO pageReqVO) {
return contactMapper.selectPageByCustomer(pageReqVO);
}
@Override
public List<CrmContactDO> getContactList() {
return contactMapper.selectList();
} }
} }

View File

@ -79,7 +79,7 @@ public class CrmQueryWrapperUtils {
if (ObjUtil.notEqual(validateAdminUser(userId), Boolean.TRUE)) { // 管理员不需要数据权限 if (ObjUtil.notEqual(validateAdminUser(userId), Boolean.TRUE)) { // 管理员不需要数据权限
queryMapper.innerJoin(CrmPermissionDO.class, on -> queryMapper.innerJoin(CrmPermissionDO.class, on ->
on.eq(CrmPermissionDO::getBizType, bizType).in(CrmPermissionDO::getBizId, bizIds) on.eq(CrmPermissionDO::getBizType, bizType).in(CrmPermissionDO::getBizId, bizIds)
.eq(CrmPermissionDO::getUserId, userId)); .in(CollUtil.isNotEmpty(bizIds), CrmPermissionDO::getUserId, userId));
} }
} }