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.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;
@ -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.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.convertSet;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;
@ -98,7 +98,8 @@ public class CrmContactController {
List<CrmCustomerDO> customerList = customerService.getCustomerList(
Collections.singletonList(contact.getCustomerId()), getLoginUserId());
// 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));
}
@ -106,7 +107,9 @@ public class CrmContactController {
@Operation(summary = "获得联系人列表")
@PreAuthorize("@ss.hasPermission('crm:contact:query')")
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));
}
@ -114,7 +117,7 @@ public class CrmContactController {
@Operation(summary = "获得联系人分页")
@PreAuthorize("@ss.hasPermission('crm:contact:query')")
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));
}
@ -122,7 +125,7 @@ public class CrmContactController {
@Operation(summary = "获得联系人分页,基于指定客户")
public CommonResult<PageResult<CrmContactRespVO>> getContactPageByCustomer(@Valid CrmContactPageReqVO pageVO) {
Assert.notNull(pageVO.getCustomerId(), "客户编号不能为空");
PageResult<CrmContactDO> pageResult = contactService.getContactPageByCustomer(pageVO);
PageResult<CrmContactDO> pageResult = contactService.getContactPage(pageVO, getLoginUserId());
return success(convertDetailContactPage(pageResult));
}
@ -132,8 +135,8 @@ public class CrmContactController {
@OperateLog(type = EXPORT)
public void exportContactExcel(@Valid CrmContactPageReqVO exportReqVO,
HttpServletResponse response) throws IOException {
exportReqVO.setPageNo(PageParam.PAGE_SIZE_NONE);
PageResult<CrmContactDO> pageResult = contactService.getContactPage(exportReqVO);
exportReqVO.setPageNo(PAGE_SIZE_NONE);
PageResult<CrmContactDO> pageResult = contactService.getContactPage(exportReqVO, getLoginUserId());
ExcelUtils.write(response, "联系人.xls", "数据", CrmContactRespVO.class,
convertDetailContactPage(pageResult).getList());
}
@ -157,7 +160,7 @@ public class CrmContactController {
contact -> Stream.of(NumberUtils.parseLong(contact.getCreator()), contact.getOwnerUserId())));
// 3. 直属上级
List<CrmContactDO> parentContactList = contactService.getContactList(
convertSet(contactList, CrmContactDO::getParentId));
convertSet(contactList, CrmContactDO::getParentId), getLoginUserId());
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.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.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 java.util.Collection;
import java.util.List;
/**
* CRM 联系人 Mapper
*
@ -15,29 +21,34 @@ import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface CrmContactMapper extends BaseMapperX<CrmContactDO> {
// TODO @puhui999数据权限
default PageResult<CrmContactDO> selectPage(CrmContactPageReqVO reqVO) {
return selectPage(reqVO, new LambdaQueryWrapperX<CrmContactDO>()
.eqIfPresent(CrmContactDO::getMobile, reqVO.getMobile())
.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 int updateOwnerUserIdById(Long id, Long ownerUserId) {
return update(new LambdaUpdateWrapper<CrmContactDO>()
.eq(CrmContactDO::getId, id)
.set(CrmContactDO::getOwnerUserId, ownerUserId));
}
default PageResult<CrmContactDO> selectPageByCustomer(CrmContactPageReqVO pageVO) {
return selectPage(pageVO, new LambdaQueryWrapperX<CrmContactDO>()
.eq(CrmContactDO::getCustomerId, pageVO.getCustomerId()) // 必须传递
.likeIfPresent(CrmContactDO::getName, pageVO.getName())
.eqIfPresent(CrmContactDO::getMobile, pageVO.getMobile())
.eqIfPresent(CrmContactDO::getTelephone, pageVO.getTelephone())
.eqIfPresent(CrmContactDO::getEmail, pageVO.getEmail())
.eqIfPresent(CrmContactDO::getQq, pageVO.getQq())
.eqIfPresent(CrmContactDO::getWechat, pageVO.getWechat())
.orderByDesc(CrmContactDO::getId));
default PageResult<CrmContactDO> selectPage(CrmContactPageReqVO pageReqVO, Long userId) {
MPJLambdaWrapperX<CrmContactDO> mpjLambdaWrapperX = new MPJLambdaWrapperX<>();
// 构建数据权限连表条件
CrmQueryWrapperUtils.builderPageQuery(mpjLambdaWrapperX, CrmBizTypeEnum.CRM_CONTACT.getType(), CrmContactDO::getId,
userId, pageReqVO.getSceneType(), pageReqVO.getPool());
mpjLambdaWrapperX.selectAll(CrmContactDO.class)
.eq(CrmContactDO::getCustomerId, pageReqVO.getCustomerId()) // 必须传递
.likeIfPresent(CrmContactDO::getName, pageReqVO.getName())
.eqIfPresent(CrmContactDO::getMobile, pageReqVO.getMobile())
.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.CrmContactUpdateReqVO;
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 java.util.Collection;
import java.util.List;
@ -22,7 +21,7 @@ public interface CrmContactService {
* 创建联系人
*
* @param createReqVO 创建信息
* @param userId 用户编号
* @param userId 用户编号
* @return 编号
*/
Long createContact(@Valid CrmContactCreateReqVO createReqVO, Long userId);
@ -52,10 +51,11 @@ public interface CrmContactService {
/**
* 获得联系人列表
*
* @param ids 编号
* @param ids 编号
* @param userId 用户编号
* @return 联系人列表
*/
List<CrmContactDO> getContactList(Collection<Long> ids);
List<CrmContactDO> getContactList(Collection<Long> ids, Long userId);
/**
* 获得联系人分页
@ -63,25 +63,9 @@ public interface CrmContactService {
* 数据权限基于 {@link CrmContactDO}
*
* @param pageReqVO 分页查询
* @param userId 用户编号
* @return 联系人分页
*/
PageResult<CrmContactDO> getContactPage(CrmContactPageReqVO pageReqVO);
/**
* 获得联系人分页基于指定客户
*
* 数据权限基于 {@link CrmCustomerDO} 读取
*
* @param pageReqVO 分页查询
* @return 联系人分页
*/
PageResult<CrmContactDO> getContactPageByCustomer(CrmContactPageReqVO pageReqVO);
/**
* 获取所有联系人列表
*
* @return 所有联系人列表
*/
List<CrmContactDO> getContactList();
PageResult<CrmContactDO> getContactPage(CrmContactPageReqVO pageReqVO, Long userId);
}

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

View File

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