CRM: 完善 code review 数据权限提到的问题,重新封装 CrmQueryWrapperUtils

This commit is contained in:
puhui999 2023-12-08 15:27:58 +08:00
parent db62d70f3f
commit 5c395fc1c4
21 changed files with 223 additions and 194 deletions

View File

@ -21,7 +21,7 @@
<!-- <module>yudao-module-mp</module>-->
<!-- <module>yudao-module-pay</module>-->
<!-- <module>yudao-module-mall</module>-->
<!-- <module>yudao-module-crm</module>-->
<module>yudao-module-crm</module>
<!-- 示例项目 -->
<!-- <module>yudao-example</module>-->
</modules>

View File

@ -13,6 +13,7 @@ import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.toolkit.Db;
import com.github.yulichang.base.MPJBaseMapper;
import com.github.yulichang.interfaces.MPJBaseJoin;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.apache.ibatis.annotations.Param;
import java.util.Collection;
@ -40,6 +41,18 @@ public interface BaseMapperX<T> extends MPJBaseMapper<T> {
return new PageResult<>(mpPage.getRecords(), mpPage.getTotal());
}
default <D> PageResult<D> selectJoinPage(PageParam pageParam, Class<D> clazz, MPJLambdaWrapper<T> lambdaWrapper) {
// 特殊不分页直接查询全部
if (PageParam.PAGE_SIZE_NONE.equals(pageParam.getPageNo())) {
List<D> list = selectJoinList(clazz, lambdaWrapper);
return new PageResult<>(list, (long) list.size());
}
IPage<D> mpPage = MyBatisUtils.buildPage(pageParam);
mpPage = selectJoinPage(mpPage, clazz, lambdaWrapper);
return new PageResult<>(mpPage.getRecords(), mpPage.getTotal());
}
default <DTO> PageResult<DTO> selectJoinPage(PageParam pageParam, Class<DTO> resultTypeClass, MPJBaseJoin<T> joinQueryWrapper) {
IPage<DTO> mpPage = MyBatisUtils.buildPage(pageParam);
selectJoinPage(mpPage, resultTypeClass, joinQueryWrapper);

View File

@ -7,7 +7,6 @@ import lombok.Getter;
import java.util.Arrays;
// TODO @puhui999这个枚举要不改成 CrmSceneTypeEnum
/**
* CRM 列表检索场景
*
@ -15,14 +14,14 @@ import java.util.Arrays;
*/
@Getter
@AllArgsConstructor
public enum CrmSceneEnum implements IntArrayValuable {
public enum CrmSceneTypeEnum implements IntArrayValuable {
OWNER(1, "我负责的"),
FOLLOW(2, "我关注的"),
// TODO @puhui999还有一个我参与的
SUBORDINATE(3, "下属负责的");
INVOLVED(3, "我参与的"),
SUBORDINATE(4, "下属负责的");
public static final int[] ARRAYS = Arrays.stream(values()).mapToInt(CrmSceneEnum::getType).toArray();
public static final int[] ARRAYS = Arrays.stream(values()).mapToInt(CrmSceneTypeEnum::getType).toArray();
/**
* 场景类型
@ -41,6 +40,10 @@ public enum CrmSceneEnum implements IntArrayValuable {
return ObjUtil.equal(FOLLOW.getType(), type);
}
public static boolean isInvolved(Integer type) {
return ObjUtil.equal(INVOLVED.getType(), type);
}
public static boolean isSubordinate(Integer type) {
return ObjUtil.equal(SUBORDINATE.getType(), type);
}

View File

@ -22,13 +22,13 @@ import cn.iocoder.yudao.module.crm.service.customer.CrmCustomerService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import java.io.IOException;
import java.util.List;
import java.util.Objects;
@ -102,7 +102,7 @@ public class CrmBusinessController {
// TODO @ljlleo可以使用 CollectionUtils.convertSet 替代常用的 stream 操作更简洁一点下面几个也是哈
Set<Long> customerIds = pageResult.getList().stream()
.map(CrmBusinessDO::getCustomerId).filter(Objects::nonNull).collect(Collectors.toSet());
List<CrmCustomerDO> customerList = customerService.getCustomerList(customerIds);
List<CrmCustomerDO> customerList = customerService.getCustomerList(customerIds, getLoginUserId());
// 处理商机状态类型名称回显
Set<Long> statusTypeIds = pageResult.getList().stream()
.map(CrmBusinessDO::getStatusTypeId).filter(Objects::nonNull).collect(Collectors.toSet());
@ -127,7 +127,7 @@ public class CrmBusinessController {
// TODO @ljlleo可以使用 CollectionUtils.convertSet 替代常用的 stream 操作更简洁一点下面几个也是哈
Set<Long> customerIds = pageResult.getList().stream()
.map(CrmBusinessDO::getCustomerId).filter(Objects::nonNull).collect(Collectors.toSet());
List<CrmCustomerDO> customerList = customerService.getCustomerList(customerIds);
List<CrmCustomerDO> customerList = customerService.getCustomerList(customerIds, getLoginUserId());
// 处理商机状态类型名称回显
Set<Long> statusTypeIds = pageResult.getList().stream()
.map(CrmBusinessDO::getStatusTypeId).filter(Objects::nonNull).collect(Collectors.toSet());

View File

@ -22,14 +22,14 @@ import com.google.common.collect.Lists;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
@ -95,7 +95,8 @@ public class CrmContactController {
Map<Long, AdminUserRespDTO> userMap = adminUserApi.getUserMap(CollUtil.removeNull(Lists.newArrayList(
NumberUtil.parseLong(contact.getCreator()), contact.getOwnerUserId())));
// 2. 获取客户信息
List<CrmCustomerDO> customerList = customerService.getCustomerList(Collections.singletonList(contact.getCustomerId()));
List<CrmCustomerDO> customerList = customerService.getCustomerList(
Collections.singletonList(contact.getCustomerId()), getLoginUserId());
// 3. 直属上级
List<CrmContactDO> parentContactList = contactService.getContactList(Collections.singletonList(contact.getParentId()));
return success(ContactConvert.INSTANCE.convert(contact, userMap, customerList, parentContactList));
@ -150,7 +151,7 @@ public class CrmContactController {
}
// 1. 获取客户列表
List<CrmCustomerDO> crmCustomerDOList = customerService.getCustomerList(
convertSet(contactList, CrmContactDO::getCustomerId));
convertSet(contactList, CrmContactDO::getCustomerId), getLoginUserId());
// 2. 获取创建人负责人列表
Map<Long, AdminUserRespDTO> userMap = adminUserApi.getUserMap(convertListByFlatMap(contactList,
contact -> Stream.of(NumberUtils.parseLong(contact.getCreator()), contact.getOwnerUserId())));

View File

@ -18,13 +18,13 @@ import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import java.io.IOException;
import java.util.List;
import java.util.Map;
@ -124,7 +124,7 @@ public class CrmContractController {
}
// 1. 获取客户列表
List<CrmCustomerDO> customerList = customerService.getCustomerList(
convertSet(contactList, CrmContractDO::getCustomerId));
convertSet(contactList, CrmContractDO::getCustomerId), getLoginUserId());
// 2. 获取创建人负责人列表
Map<Long, AdminUserRespDTO> userMap = adminUserApi.getUserMap(convertListByFlatMap(contactList,
contact -> Stream.of(NumberUtils.parseLong(contact.getCreator()), contact.getOwnerUserId())));

View File

@ -1,6 +1,8 @@
package cn.iocoder.yudao.module.crm.controller.admin.customer.vo;
import cn.iocoder.yudao.module.crm.framework.vo.CrmBasePageReqVO;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.validation.InEnum;
import cn.iocoder.yudao.module.crm.enums.common.CrmSceneTypeEnum;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;
@ -10,7 +12,7 @@ import lombok.ToString;
@Data
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
public class CrmCustomerPageReqVO extends CrmBasePageReqVO {
public class CrmCustomerPageReqVO extends PageParam {
@Schema(description = "客户名称", example = "赵六")
private String name;
@ -27,4 +29,14 @@ public class CrmCustomerPageReqVO extends CrmBasePageReqVO {
@Schema(description = "客户来源", example = "1")
private Integer source;
/**
* 场景类型 null 时则表示全部
*/
@Schema(description = "场景类型", example = "1")
@InEnum(CrmSceneTypeEnum.class)
private Integer sceneType;
@Schema(description = "是否为公海数据", requiredMode = Schema.RequiredMode.REQUIRED, example = "false")
private Boolean pool; // null 则表示为不是公海数据
}

View File

@ -23,13 +23,13 @@ import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import java.io.IOException;
import java.util.List;
import java.util.Map;
@ -39,6 +39,7 @@ 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;
@Tag(name = "管理后台 - CRM 回款")
@RestController
@ -131,7 +132,7 @@ public class CrmReceivableController {
}
// 1. 获取客户列表
List<CrmCustomerDO> customerList = customerService.getCustomerList(
convertSet(receivableList, CrmReceivableDO::getCustomerId));
convertSet(receivableList, CrmReceivableDO::getCustomerId), getLoginUserId());
// 2. 获取创建人负责人列表
Map<Long, AdminUserRespDTO> userMap = adminUserApi.getUserMap(convertListByFlatMap(receivableList,
contact -> Stream.of(NumberUtils.parseLong(contact.getCreator()), contact.getOwnerUserId())));

View File

@ -25,13 +25,13 @@ import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import java.io.IOException;
import java.util.List;
import java.util.Map;
@ -41,6 +41,7 @@ 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;
@Tag(name = "管理后台 - CRM 回款计划")
@RestController
@ -135,7 +136,7 @@ public class CrmReceivablePlanController {
}
// 1. 获取客户列表
List<CrmCustomerDO> customerList = customerService.getCustomerList(
convertSet(receivablePlanList, CrmReceivablePlanDO::getCustomerId));
convertSet(receivablePlanList, CrmReceivablePlanDO::getCustomerId), getLoginUserId());
// 2. 获取创建人负责人列表
Map<Long, AdminUserRespDTO> userMap = adminUserApi.getUserMap(convertListByFlatMap(receivablePlanList,
contact -> Stream.of(NumberUtils.parseLong(contact.getCreator()), contact.getOwnerUserId())));

View File

@ -1,6 +0,0 @@
/**
* 提供 RESTful API 给前端
* 1. admin 提供给管理后台 yudao-ui-admin 前端项目
* 2. app 提供给用户 APP yudao-ui-app 前端项目它的 Controller VO 都要添加 App 前缀用于和管理后台进行区分
*/
package cn.iocoder.yudao.module.crm.controller;

View File

@ -55,8 +55,7 @@ public interface CrmCustomerConvert {
List<CrmCustomerExcelVO> convertList02(List<CrmCustomerDO> list);
@Mappings({
@Mapping(target = "bizId", source = "reqVO.id"),
@Mapping(target = "newOwnerUserId", source = "reqVO.id")
@Mapping(target = "bizId", source = "reqVO.id")
})
CrmPermissionTransferReqBO convert(CrmCustomerTransferReqVO reqVO, Long userId);

View File

@ -1,16 +1,13 @@
package cn.iocoder.yudao.module.crm.dal.mysql.customer;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
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.MPJLambdaWrapperX;
import cn.iocoder.yudao.framework.mybatis.core.util.MyBatisUtils;
import cn.iocoder.yudao.module.crm.controller.admin.customer.vo.CrmCustomerPageReqVO;
import cn.iocoder.yudao.module.crm.dal.dataobject.customer.CrmCustomerDO;
import cn.iocoder.yudao.module.crm.enums.common.CrmBizTypeEnum;
import cn.iocoder.yudao.module.crm.util.CrmQueryPageUtils;
import cn.iocoder.yudao.module.crm.util.CrmQueryWrapperUtils;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.apache.ibatis.annotations.Mapper;
import java.util.Collection;
@ -30,35 +27,25 @@ public interface CrmCustomerMapper extends BaseMapperX<CrmCustomerDO> {
.set(CrmCustomerDO::getOwnerUserId, ownerUserId));
}
/**
* 获取客户分页
*
* @param pageReqVO 请求
* @param userId 用户编号
* @param subordinateIds 下属用户编号
* @param isAdmin 是否为管理
* @return 客户分页数据
*/
default PageResult<CrmCustomerDO> selectPage(CrmCustomerPageReqVO pageReqVO, Long userId, Collection<Long> subordinateIds, Boolean isAdmin) {
IPage<CrmCustomerDO> mpPage = MyBatisUtils.buildPage(pageReqVO);
default PageResult<CrmCustomerDO> selectPage(CrmCustomerPageReqVO pageReqVO, Long userId) {
MPJLambdaWrapperX<CrmCustomerDO> mpjLambdaWrapperX = new MPJLambdaWrapperX<>();
// 构建数据权限连表条件
CrmQueryPageUtils.builderQuery(mpjLambdaWrapperX, pageReqVO, userId,
CrmBizTypeEnum.CRM_CUSTOMER.getType(), CrmCustomerDO::getId, subordinateIds, isAdmin);
CrmQueryWrapperUtils.builderPageQuery(mpjLambdaWrapperX, CrmBizTypeEnum.CRM_CUSTOMER.getType(), CrmCustomerDO::getId,
userId, pageReqVO.getSceneType(), pageReqVO.getPool());
mpjLambdaWrapperX.selectAll(CrmCustomerDO.class)
.likeIfPresent(CrmCustomerDO::getName, pageReqVO.getName())
.eqIfPresent(CrmCustomerDO::getMobile, pageReqVO.getMobile())
.eqIfPresent(CrmCustomerDO::getIndustryId, pageReqVO.getIndustryId())
.eqIfPresent(CrmCustomerDO::getLevel, pageReqVO.getLevel())
.eqIfPresent(CrmCustomerDO::getSource, pageReqVO.getSource());
// 特殊不分页直接查询全部
// TODO @puhui999下面这个封装一个方法 56 61 这里哈
if (PageParam.PAGE_SIZE_NONE.equals(pageReqVO.getPageNo())) {
List<CrmCustomerDO> list = selectJoinList(CrmCustomerDO.class, mpjLambdaWrapperX);
return new PageResult<>(list, (long) list.size());
return selectJoinPage(pageReqVO, CrmCustomerDO.class, mpjLambdaWrapperX);
}
mpPage = selectJoinPage(mpPage, CrmCustomerDO.class, mpjLambdaWrapperX);
return new PageResult<>(mpPage.getRecords(), mpPage.getTotal());
default List<CrmCustomerDO> selectBatchIds(Collection<Long> ids, Long userId) {
MPJLambdaWrapperX<CrmCustomerDO> mpjLambdaWrapperX = new MPJLambdaWrapperX<>();
// 构建数据权限连表条件
CrmQueryWrapperUtils.builderListQueryBatch(mpjLambdaWrapperX, CrmBizTypeEnum.CRM_CUSTOMER.getType(), ids, userId);
return selectJoinList(CrmCustomerDO.class, mpjLambdaWrapperX);
}
}

View File

@ -1,25 +0,0 @@
package cn.iocoder.yudao.module.crm.framework.vo;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.validation.InEnum;
import cn.iocoder.yudao.module.crm.enums.common.CrmSceneEnum;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;
@Schema(description = "管理后台 - CRM 分页 Base Request VO")
@Data
@EqualsAndHashCode(callSuper = true)
public class CrmBasePageReqVO extends PageParam {
/**
* 场景类型 null 时则表示全部
*/
@Schema(description = "场景类型", example = "1")
@InEnum(CrmSceneEnum.class)
private Integer sceneType;
@Schema(description = "是否为公海数据", requiredMode = Schema.RequiredMode.REQUIRED, example = "false")
private Boolean pool; // null 则表示为不是公海数据
}

View File

@ -6,8 +6,8 @@ import cn.iocoder.yudao.module.crm.controller.admin.customer.vo.CrmCustomerPageR
import cn.iocoder.yudao.module.crm.controller.admin.customer.vo.CrmCustomerTransferReqVO;
import cn.iocoder.yudao.module.crm.controller.admin.customer.vo.CrmCustomerUpdateReqVO;
import cn.iocoder.yudao.module.crm.dal.dataobject.customer.CrmCustomerDO;
import jakarta.validation.Valid;
import java.util.Collection;
import java.util.List;
@ -56,7 +56,7 @@ public interface CrmCustomerService {
* @return 客户列表
* @author ljlleo
*/
List<CrmCustomerDO> getCustomerList(Collection<Long> ids);
List<CrmCustomerDO> getCustomerList(Collection<Long> ids, Long userId);
/**
* 获得客户分页

View File

@ -15,11 +15,11 @@ import cn.iocoder.yudao.module.crm.framework.core.annotations.CrmPermission;
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.*;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
@ -93,17 +93,16 @@ public class CrmCustomerServiceImpl implements CrmCustomerService {
}
@Override
public List<CrmCustomerDO> getCustomerList(Collection<Long> ids) {
public List<CrmCustomerDO> getCustomerList(Collection<Long> ids, Long userId) {
if (CollUtil.isEmpty(ids)) {
return Collections.emptyList();
}
return customerMapper.selectBatchIds(ids);
return customerMapper.selectBatchIds(ids, userId);
}
@Override
public PageResult<CrmCustomerDO> getCustomerPage(CrmCustomerPageReqVO pageReqVO, Long userId) {
boolean admin = false; // TODO 如果是管理员
return customerMapper.selectPage(pageReqVO, userId, adminUserApi.getSubordinateIds(userId), admin);
return customerMapper.selectPage(pageReqVO, userId);
}
/**
@ -128,9 +127,11 @@ public class CrmCustomerServiceImpl implements CrmCustomerService {
// 1. 校验客户是否存在
validateCustomer(reqVO.getId());
// 2. 数据权限转移
// 2.1 数据权限转移
crmPermissionService.transferPermission(
CrmCustomerConvert.INSTANCE.convert(reqVO, userId).setBizType(CrmBizTypeEnum.CRM_CUSTOMER.getType()));
// 2.2 转移后重新设置负责人
customerMapper.updateOwnerUserIdById(reqVO.getId(), reqVO.getNewOwnerUserId());
// 3. TODO 记录转移日志
}

View File

@ -1,66 +0,0 @@
package cn.iocoder.yudao.module.crm.util;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjUtil;
import cn.iocoder.yudao.module.crm.dal.dataobject.permission.CrmPermissionDO;
import cn.iocoder.yudao.module.crm.enums.common.CrmBizTypeEnum;
import cn.iocoder.yudao.module.crm.enums.common.CrmSceneEnum;
import cn.iocoder.yudao.module.crm.framework.vo.CrmBasePageReqVO;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import jakarta.annotation.Nullable;
import java.util.Collection;
/**
* CRM 分页查询工具类
*
* @author HUIHUI
*/
public class CrmQueryPageUtils {
// TODO @puhui999是不是弱化 CrmBasePageReqVO sceneType 作为参数传入默认其实 pool 不一定要传递的
/**
* 构造 CRM 数据类型数据分页查询条件
*
* @param queryMapper 连表查询对象
* @param reqVO 查询条件
* @param userId 用户编号
* @param bizType 数据类型 {@link CrmBizTypeEnum}
* @param bizId 数据编号
* @param subordinateIds 下属用户编号可为空
*/
public static <T extends MPJLambdaWrapper<?>, V extends CrmBasePageReqVO, S> void builderQuery(
T queryMapper, V reqVO, Long userId,
Integer bizType, SFunction<S, ?> bizId,
@Nullable Collection<Long> subordinateIds, // TODO @puhui999subordinateIds 可以优化成 subordinateUserIds
Boolean isAdmin) {
// TODO @puhui999是不是特殊处理让这个 util 有状态这样 isAdmin 直接从这里读取subordinateIds 也是这样可以简化参数
// 1. 构建数据权限连表条件
if (ObjUtil.notEqual(isAdmin, Boolean.TRUE)) { // 管理员不需要数据权限
queryMapper.innerJoin(CrmPermissionDO.class, on ->
on.eq(CrmPermissionDO::getBizType, bizType).eq(CrmPermissionDO::getBizId, bizId)
.eq(CrmPermissionDO::getUserId, userId));
}
// 2. 拼接公海的查询条件
if (ObjUtil.equal(reqVO.getPool(), Boolean.TRUE)) { // 情况一公海
queryMapper.isNull("owner_user_id");
} else { // 情况二不是公海
queryMapper.isNotNull("owner_user_id");
}
// 3. 拼接场景的查询条件
// TODO @puhui999:1 处的数据权限应该和 3 处的场景是结合的
// null 一种处理
// 1一种条件
// 2一种条件
// 3一种条件
if (CrmSceneEnum.isOwner(reqVO.getSceneType())) { // 场景一我负责的数据
queryMapper.eq("owner_user_id", userId);
}
// TODO puhui999: 这里有一个疑问如果下属负责的数据权限中没有自己的话还能看吗回复不能
if (CrmSceneEnum.isSubordinate(reqVO.getSceneType()) && CollUtil.isNotEmpty(subordinateIds)) { // 场景三下属负责的数据
queryMapper.in("owner_user_id", subordinateIds);
}
}
}

View File

@ -0,0 +1,100 @@
package cn.iocoder.yudao.module.crm.util;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.extra.spring.SpringUtil;
import cn.iocoder.yudao.module.crm.dal.dataobject.permission.CrmPermissionDO;
import cn.iocoder.yudao.module.crm.enums.common.CrmBizTypeEnum;
import cn.iocoder.yudao.module.crm.enums.common.CrmSceneTypeEnum;
import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import java.util.Collection;
import java.util.List;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet;
/**
* CRM 分页查询工具类
*
* @author HUIHUI
*/
public class CrmQueryWrapperUtils {
/**
* 构造 CRM 数据类型数据分页查询条件
*
* @param queryMapper 连表查询对象
* @param bizType 数据类型 {@link CrmBizTypeEnum}
* @param bizId 数据编号
* @param userId 用户编号
* @param sceneType 场景类型
* @param pool 公海
*/
public static <T extends MPJLambdaWrapper<?>, S> void builderPageQuery(
T queryMapper, Integer bizType, SFunction<S, ?> bizId, Long userId, Integer sceneType, Boolean pool) {
// 1. 构建数据权限连表条件
if (ObjUtil.notEqual(validateAdminUser(userId), Boolean.TRUE)) { // 管理员不需要数据权限
queryMapper.innerJoin(CrmPermissionDO.class, on ->
on.eq(CrmPermissionDO::getBizType, bizType).eq(CrmPermissionDO::getBizId, bizId)
.eq(CrmPermissionDO::getUserId, userId));
}
// 1.2 场景一我负责的数据
if (CrmSceneTypeEnum.isOwner(sceneType)) {
queryMapper.eq("owner_user_id", userId);
}
// 1.3 场景一我参与的数据
if (CrmSceneTypeEnum.isInvolved(sceneType)) {
queryMapper.ne("owner_user_id", userId);
}
// 1.4 场景二下属负责的数据
if (CrmSceneTypeEnum.isSubordinate(sceneType)) {
List<AdminUserRespDTO> subordinateUsers = getAdminUserApi().getUserListBySubordinate(userId);
if (CollUtil.isNotEmpty(subordinateUsers)) {
queryMapper.in("owner_user_id", convertSet(subordinateUsers, AdminUserRespDTO::getId));
}
}
// 2. 拼接公海的查询条件
if (ObjUtil.equal(pool, Boolean.TRUE)) { // 情况一公海
queryMapper.isNull("owner_user_id");
} else { // 情况二不是公海
queryMapper.isNotNull("owner_user_id");
}
}
/**
* 构造 CRM 数据类型批量数据查询条件
*
* @param queryMapper 连表查询对象
* @param bizType 数据类型 {@link CrmBizTypeEnum}
* @param bizIds 数据编号
* @param userId 用户编号
*/
public static <T extends MPJLambdaWrapper<?>, S> void builderListQueryBatch(
T queryMapper, Integer bizType, Collection<Long> bizIds, Long userId) {
// 1. 构建数据权限连表条件
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));
}
}
private static AdminUserApi getAdminUserApi() {
return SpringUtil.getBean(AdminUserApi.class);
}
/**
* 校验用户是否是管理员
*
* @param userId 用户编号
* @return /
*/
private static boolean validateAdminUser(Long userId) {
return false;
}
}

View File

@ -77,7 +77,6 @@ export function export${simpleClassName}Excel(params) {
// ==================== 子表($subTable.classComment ====================
## 情况一MASTER_ERP 时,需要分查询页子表
#if ( $table.templateType == 11 )
// 获得${subTable.classComment}分页
export function get${subSimpleClassName}Page(params) {
return request({
@ -89,20 +88,18 @@ export function export${simpleClassName}Excel(params) {
## 情况二:非 MASTER_ERP 时,需要列表查询子表
#else
#if ( $subTable.subJoinMany )
// 获得${subTable.classComment}列表
export function get${subSimpleClassName}ListBy${SubJoinColumnName}(${subJoinColumn.javaField}) {
return request({
url: `${baseURL}/${subSimpleClassName_strikeCase}/list-by-${subJoinColumn_strikeCase}?${subJoinColumn.javaField}=` + ${subJoinColumn.javaField},
url: '${baseURL}/${subSimpleClassName_strikeCase}/list-by-${subJoinColumn_strikeCase}?${subJoinColumn.javaField}=' + ${subJoinColumn.javaField},
method: 'get'
})
}
#else
// 获得${subTable.classComment}
export function get${subSimpleClassName}By${SubJoinColumnName}(${subJoinColumn.javaField}) {
return request({
url: `${baseURL}/${subSimpleClassName_strikeCase}/get-by-${subJoinColumn_strikeCase}?${subJoinColumn.javaField}=` + ${subJoinColumn.javaField},
url: '${baseURL}/${subSimpleClassName_strikeCase}/get-by-${subJoinColumn_strikeCase}?${subJoinColumn.javaField}=' + ${subJoinColumn.javaField},
method: 'get'
})
}
@ -113,7 +110,7 @@ export function export${simpleClassName}Excel(params) {
// 新增${subTable.classComment}
export function create${subSimpleClassName}(data) {
return request({
url: `${baseURL}/${subSimpleClassName_strikeCase}/create`,
url: '${baseURL}/${subSimpleClassName_strikeCase}/create',
method: 'post',
data
})
@ -122,7 +119,7 @@ export function export${simpleClassName}Excel(params) {
// 修改${subTable.classComment}
export function update${subSimpleClassName}(data) {
return request({
url: `${baseURL}/${subSimpleClassName_strikeCase}/update`,
url: '${baseURL}/${subSimpleClassName_strikeCase}/update',
method: 'post',
data
})
@ -131,7 +128,7 @@ export function export${simpleClassName}Excel(params) {
// 删除${subTable.classComment}
export function delete${subSimpleClassName}(id) {
return request({
url: `${baseURL}/${subSimpleClassName_strikeCase}/delete?id=` + id,
url: '${baseURL}/${subSimpleClassName_strikeCase}/delete?id=' + id,
method: 'delete'
})
}
@ -139,7 +136,7 @@ export function export${simpleClassName}Excel(params) {
// 获得${subTable.classComment}
export function get${subSimpleClassName}(id) {
return request({
url: `${baseURL}/${subSimpleClassName_strikeCase}/get?id=` + id,
url: '${baseURL}/${subSimpleClassName_strikeCase}/get?id=' + id,
method: 'get'
})
}

View File

@ -6,7 +6,6 @@ import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* Admin 用户 API 接口
@ -27,10 +26,10 @@ public interface AdminUserApi {
/**
* 通过用户 ID 查询用户下属
*
* @param id 用户编号
* @return 用户下属用户编号列表
* @param userId 用户编号
* @return 用户下属用户列表
*/
Set<Long> getSubordinateIds(Long id);
List<AdminUserRespDTO> getUserListBySubordinate(Long userId);
/**
* 通过用户 ID 查询用户们

View File

@ -1,5 +1,6 @@
package cn.iocoder.yudao.module.system.api.user;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjUtil;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO;
@ -7,13 +8,13 @@ import cn.iocoder.yudao.module.system.dal.dataobject.dept.DeptDO;
import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO;
import cn.iocoder.yudao.module.system.service.dept.DeptService;
import cn.iocoder.yudao.module.system.service.user.AdminUserService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet;
@ -37,21 +38,32 @@ public class AdminUserApiImpl implements AdminUserApi {
}
@Override
public Set<Long> getSubordinateIds(Long id) {
AdminUserDO user = userService.getUser(id);
public List<AdminUserRespDTO> getUserListBySubordinate(Long userId) {
// 1. 获取用户信息
AdminUserDO user = userService.getUser(userId);
if (user == null) {
return null;
return Collections.emptyList();
}
Set<Long> subordinateIds = null; // 下属用户编号
// 2.1 获取用户负责的部门
ArrayList<Long> deptIds = new ArrayList<>();
DeptDO dept = deptService.getDept(user.getDeptId());
// TODO @puhui999需要递归查询到子部门并且要排除到自己噢
// TODO @puhui999保持 if return 原则这里其实要判断不等于则返回 null最好返回 空集合上面也是
if (ObjUtil.equal(dept.getLeaderUserId(), id)) { // 校验是否是该部门的负责人
List<AdminUserDO> users = userService.getUserListByDeptIds(Collections.singletonList(dept.getId()));
subordinateIds = convertSet(users, AdminUserDO::getId);
if (dept == null) {
return Collections.emptyList();
}
return subordinateIds;
if (ObjUtil.notEqual(dept.getLeaderUserId(), userId)) { // 校验为负责人
return Collections.emptyList();
}
deptIds.add(dept.getId()); // 加入此部门
// 2.2 获取所有子部门
List<DeptDO> childDeptList = deptService.getChildDeptList(dept.getId());
if (CollUtil.isNotEmpty(childDeptList)) {
deptIds.addAll(convertSet(childDeptList, DeptDO::getId));
}
// 2.3 获取用户信息
List<AdminUserDO> users = userService.getUserListByDeptIds(deptIds);
users.removeIf(item -> ObjUtil.equal(item.getId(), userId)); // 排除自己
return BeanUtils.toBean(users, AdminUserRespDTO.class);
}
@Override

View File

@ -92,11 +92,11 @@
<!-- </dependency>-->
<!-- CRM 相关模块。默认注释,保证编译速度 -->
<!-- <dependency>-->
<!-- <groupId>cn.iocoder.boot</groupId>-->
<!-- <artifactId>yudao-module-crm-biz</artifactId>-->
<!-- <version>${revision}</version>-->
<!-- </dependency>-->
<dependency>
<groupId>cn.iocoder.boot</groupId>
<artifactId>yudao-module-crm-biz</artifactId>
<version>${revision}</version>
</dependency>
<!-- spring boot 配置所需依赖 -->
<dependency>