diff --git a/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/controller/admin/customer/CrmCustomerController.java b/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/controller/admin/customer/CrmCustomerController.java index 10b6b5ee8..0354afbb5 100644 --- a/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/controller/admin/customer/CrmCustomerController.java +++ b/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/controller/admin/customer/CrmCustomerController.java @@ -1,22 +1,21 @@ package cn.iocoder.yudao.module.crm.controller.admin.customer; -import cn.hutool.core.collection.CollUtil; -import cn.hutool.core.util.NumberUtil; -import cn.hutool.core.util.ObjectUtil; import cn.iocoder.yudao.framework.common.pojo.CommonResult; import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils; -import cn.iocoder.yudao.framework.ip.core.utils.AreaUtils; import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog; import cn.iocoder.yudao.module.crm.controller.admin.customer.vo.*; import cn.iocoder.yudao.module.crm.convert.customer.CrmCustomerConvert; import cn.iocoder.yudao.module.crm.dal.dataobject.customer.CrmCustomerDO; +import cn.iocoder.yudao.module.crm.dal.dataobject.permission.CrmPermissionDO; +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.system.api.dept.DeptApi; import cn.iocoder.yudao.module.system.api.dept.dto.DeptRespDTO; import cn.iocoder.yudao.module.system.api.user.AdminUserApi; import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO; -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; @@ -28,11 +27,14 @@ import javax.annotation.Resource; import javax.servlet.http.HttpServletResponse; import javax.validation.Valid; import java.io.IOException; -import java.util.*; -import java.util.stream.Collectors; -import java.util.stream.Stream; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.Set; import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success; +import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertMap; +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; @@ -48,6 +50,8 @@ public class CrmCustomerController { private DeptApi deptApi; @Resource private AdminUserApi adminUserApi; + @Resource + private CrmPermissionService permissionService; @PostMapping("/create") @Operation(summary = "创建客户") @@ -78,49 +82,57 @@ public class CrmCustomerController { @Parameter(name = "id", description = "编号", required = true, example = "1024") @PreAuthorize("@ss.hasPermission('crm:customer:query')") public CommonResult getCustomer(@RequestParam("id") Long id) { + // 1. 获取客户 CrmCustomerDO customer = customerService.getCustomer(id); - CrmCustomerRespVO customerRespVO = CrmCustomerConvert.INSTANCE.convert(customer); - if (ObjectUtil.isAllNotEmpty(customer, customer.getAreaId())) { - customerRespVO.setAreaName(AreaUtils.format(customer.getAreaId())); + if (customer == null) { + return success(null); } - Map userMap = adminUserApi.getUserMap(CollUtil.removeNull(Lists.newArrayList(NumberUtil.parseLong(customerRespVO.getCreator()), customerRespVO.getOwnerUserId()))); - customerRespVO.setCreatorName(Optional.ofNullable(userMap.get(NumberUtil.parseLong(customerRespVO.getCreator()))).map(AdminUserRespDTO::getNickname).orElse(null)); - AdminUserRespDTO ownerUser = userMap.get(customer.getOwnerUserId()); - if (Objects.nonNull(ownerUser)) { - customerRespVO.setOwnerUserName(ownerUser.getNickname()); - DeptRespDTO dept = deptApi.getDept(ownerUser.getDeptId()); - if (Objects.nonNull(dept)) { - customerRespVO.setOwnerUserDept(dept.getName()); - } - } - return success(customerRespVO); + + // 2. 拼接数据 + // 2.1 获取负责人 + List ownerList = permissionService.getPermissionByBizTypeAndBizIdsAndLevel( + CrmBizTypeEnum.CRM_CUSTOMER.getType(), Collections.singletonList(customer.getId()), + CrmPermissionLevelEnum.OWNER.getLevel()); + Map ownerMap = convertMap(ownerList, CrmPermissionDO::getBizId); + // 2.2 获取负责人详情 + Set userIds = convertSet(ownerList, CrmPermissionDO::getUserId); + userIds.add(Long.parseLong(customer.getCreator())); // 加入创建者 + List userList = adminUserApi.getUserList(userIds); + Map userMap = convertMap(userList, AdminUserRespDTO::getId); + // 2.3 获取部门详情 + Map deptMap = deptApi.getDeptMap(convertSet(userList, AdminUserRespDTO::getDeptId)); + return success(CrmCustomerConvert.INSTANCE.convert(customer, ownerMap, userMap, deptMap)); } @GetMapping("/page") @Operation(summary = "获得客户分页") @PreAuthorize("@ss.hasPermission('crm:customer:query')") public CommonResult> getCustomerPage(@Valid CrmCustomerPageReqVO pageVO) { - PageResult pageResult = customerService.getCustomerPage(pageVO); - PageResult pageVo = CrmCustomerConvert.INSTANCE.convertPage(pageResult); - // TODO @wanwan: 可以参考 CollectionUtils.convertListByFlatMap(),目的是简洁 - Set userSet = pageVo.getList().stream().flatMap(i -> Stream.of(NumberUtil.parseLong(i.getCreator()), i.getOwnerUserId())).collect(Collectors.toSet()); - Map userMap = adminUserApi.getUserMap(userSet); - Map deptMap = deptApi.getDeptMap(userMap.values().stream().map(AdminUserRespDTO::getDeptId).collect(Collectors.toSet())); - // TODO @wanwan:这块可以形成一个 convertPage 方法,default 实现; - pageVo.getList().forEach(customerRespVO -> { - customerRespVO.setAreaName(AreaUtils.format(customerRespVO.getAreaId())); - customerRespVO.setCreatorName(Optional.ofNullable(userMap.get(NumberUtil.parseLong(customerRespVO.getCreator()))).map(AdminUserRespDTO::getNickname).orElse(null)); - // TODO @wanwan:可以使用 MapUtils.findAndThen - AdminUserRespDTO ownerUser = userMap.get(customerRespVO.getOwnerUserId()); - if (Objects.nonNull(ownerUser)) { - customerRespVO.setOwnerUserName(ownerUser.getNickname()); - DeptRespDTO dept = deptMap.get(ownerUser.getDeptId()); - if (Objects.nonNull(dept)) { - customerRespVO.setOwnerUserDept(dept.getName()); - } - } - }); - return success(pageVo); + return convertPage(customerService.getCustomerPage(pageVO, getLoginUserId())); + } + + @GetMapping("/pool-page") + @Operation(summary = "获得公海客户分页") + @PreAuthorize("@ss.hasPermission('crm:customer:query')") + public CommonResult> getPoolCustomerPage(@Valid CrmCustomerPageReqVO pageVO) { + return convertPage(customerService.getCustomerPage(pageVO, CrmPermissionDO.POOL_USER_ID)); + } + + private CommonResult> convertPage(PageResult pageResult) { + // 2. 拼接数据 + Set ids = convertSet(pageResult.getList(), CrmCustomerDO::getId); + // 2.1 获取负责人 + List ownerList = permissionService.getPermissionByBizTypeAndBizIdsAndLevel( + CrmBizTypeEnum.CRM_CUSTOMER.getType(), ids, CrmPermissionLevelEnum.OWNER.getLevel()); + Map ownerMap = convertMap(ownerList, CrmPermissionDO::getBizId); + // 2.2 获取负责人详情 + Set userIds = convertSet(ownerList, CrmPermissionDO::getUserId); + userIds.addAll(convertSet(pageResult.getList(), item -> Long.parseLong(item.getCreator()))); // 加入创建者 + List userList = adminUserApi.getUserList(userIds); + Map userMap = convertMap(userList, AdminUserRespDTO::getId); + // 2.3 获取部门详情 + Map deptMap = deptApi.getDeptMap(convertSet(userList, AdminUserRespDTO::getDeptId)); + return success(CrmCustomerConvert.INSTANCE.convertPage(pageResult, ownerMap, userMap, deptMap)); } @GetMapping("/export-excel") diff --git a/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/controller/admin/customer/vo/CrmCustomerRespVO.java b/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/controller/admin/customer/vo/CrmCustomerRespVO.java index f37740f26..2cbd85dd3 100644 --- a/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/controller/admin/customer/vo/CrmCustomerRespVO.java +++ b/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/controller/admin/customer/vo/CrmCustomerRespVO.java @@ -33,7 +33,7 @@ public class CrmCustomerRespVO extends CrmCustomerBaseVO { @Schema(description = "负责人名字", example = "25682") private String ownerUserName; @Schema(description = "负责人部门") - private String ownerUserDept; + private String ownerUserDeptName; @Schema(description = "地区名称", example = "北京市") private String areaName; diff --git a/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/convert/business/CrmBusinessConvert.java b/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/convert/business/CrmBusinessConvert.java index 4256a6463..8b7f7e83b 100644 --- a/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/convert/business/CrmBusinessConvert.java +++ b/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/convert/business/CrmBusinessConvert.java @@ -3,7 +3,6 @@ package cn.iocoder.yudao.module.crm.convert.business; import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.module.crm.controller.admin.business.vo.*; import cn.iocoder.yudao.module.crm.dal.dataobject.business.CrmBusinessDO; -import cn.iocoder.yudao.module.crm.service.permission.bo.CrmPermissionPageReqBO; import cn.iocoder.yudao.module.crm.service.permission.bo.CrmPermissionTransferReqBO; import org.mapstruct.Mapper; import org.mapstruct.Mapping; @@ -38,6 +37,4 @@ public interface CrmBusinessConvert { }) CrmPermissionTransferReqBO convert(CrmBusinessTransferReqVO reqVO, Long userId); - CrmPermissionPageReqBO convert(CrmBusinessPageReqVO pageReqVO); - } diff --git a/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/convert/customer/CrmCustomerConvert.java b/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/convert/customer/CrmCustomerConvert.java index f8b47ef19..dbcc0a606 100644 --- a/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/convert/customer/CrmCustomerConvert.java +++ b/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/convert/customer/CrmCustomerConvert.java @@ -1,15 +1,22 @@ package cn.iocoder.yudao.module.crm.convert.customer; import cn.iocoder.yudao.framework.common.pojo.PageResult; +import cn.iocoder.yudao.framework.ip.core.utils.AreaUtils; import cn.iocoder.yudao.module.crm.controller.admin.customer.vo.*; import cn.iocoder.yudao.module.crm.dal.dataobject.customer.CrmCustomerDO; +import cn.iocoder.yudao.module.crm.dal.dataobject.permission.CrmPermissionDO; import cn.iocoder.yudao.module.crm.service.permission.bo.CrmPermissionTransferReqBO; +import cn.iocoder.yudao.module.system.api.dept.dto.DeptRespDTO; +import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO; 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 static cn.iocoder.yudao.framework.common.util.collection.MapUtils.findAndThen; /** * 客户 Convert @@ -27,7 +34,24 @@ public interface CrmCustomerConvert { CrmCustomerRespVO convert(CrmCustomerDO bean); - PageResult convertPage(PageResult page); + default CrmCustomerRespVO convert(CrmCustomerDO customer, Map ownerMap, + Map userMap, Map deptMap) { + CrmCustomerRespVO customerResp = convert(customer); + findAndThen(ownerMap, customerResp.getId(), owner -> { + customerResp.setOwnerUserId(owner.getUserId()); + customerResp.setAreaName(AreaUtils.format(customerResp.getAreaId())); + findAndThen(userMap, owner.getUserId(), user -> { + customerResp.setOwnerUserName(user.getNickname()); + }); + findAndThen(userMap, Long.parseLong(customerResp.getCreator()), user -> { + customerResp.setCreatorName(user.getNickname()); + }); + findAndThen(deptMap, customerResp.getOwnerUserId(), dept -> { + customerResp.setOwnerUserDeptName(dept.getName()); + }); + }); + return customerResp; + } List convertList02(List list); @@ -37,4 +61,27 @@ public interface CrmCustomerConvert { }) CrmPermissionTransferReqBO convert(CrmCustomerTransferReqVO reqVO, Long userId); + PageResult convertPage(PageResult page); + + default PageResult convertPage(PageResult pageResult, Map ownerMap, + Map userMap, Map deptMap) { + PageResult result = convertPage(pageResult); + result.getList().forEach(item -> { + findAndThen(ownerMap, item.getId(), owner -> { + item.setOwnerUserId(owner.getUserId()); + item.setAreaName(AreaUtils.format(item.getAreaId())); + findAndThen(userMap, owner.getUserId(), user -> { + item.setOwnerUserName(user.getNickname()); + }); + findAndThen(userMap, Long.parseLong(item.getCreator()), user -> { + item.setCreatorName(user.getNickname()); + }); + findAndThen(deptMap, item.getOwnerUserId(), dept -> { + item.setOwnerUserDeptName(dept.getName()); + }); + }); + }); + return result; + } + } diff --git a/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/dal/dataobject/customer/CrmCustomerDO.java b/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/dal/dataobject/customer/CrmCustomerDO.java index 3e00b1cc5..f463d309e 100644 --- a/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/dal/dataobject/customer/CrmCustomerDO.java +++ b/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/dal/dataobject/customer/CrmCustomerDO.java @@ -1,6 +1,7 @@ package cn.iocoder.yudao.module.crm.dal.dataobject.customer; import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO; +import cn.iocoder.yudao.module.crm.enums.DictTypeConstants; import com.baomidou.mybatisplus.annotation.KeySequence; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; @@ -49,19 +50,19 @@ public class CrmCustomerDO extends BaseDO { /** * 所属行业 * - * 对应字典 {@link cn.iocoder.yudao.module.crm.enums.DictTypeConstants#CRM_CUSTOMER_INDUSTRY} + * 对应字典 {@link DictTypeConstants#CRM_CUSTOMER_INDUSTRY} */ private Integer industryId; /** * 客户等级 * - * 对应字典 {@link cn.iocoder.yudao.module.crm.enums.DictTypeConstants#CRM_CUSTOMER_LEVEL} + * 对应字典 {@link DictTypeConstants#CRM_CUSTOMER_LEVEL} */ private Integer level; /** * 客户来源 * - * 对应字典 {@link cn.iocoder.yudao.module.crm.enums.DictTypeConstants#CRM_CUSTOMER_SOURCE} + * 对应字典 {@link DictTypeConstants#CRM_CUSTOMER_SOURCE} */ private Integer source; /** @@ -96,21 +97,6 @@ public class CrmCustomerDO extends BaseDO { * 备注 */ private String remark; - /** - * 负责人的用户编号 - * - * 关联 AdminUserDO 的 id 字段 - */ - private Long ownerUserId; - // TODO @puhui999:这块抽到 permission 里; - /** - * 只读权限的用户编号数组 - */ - private String roUserIds; - /** - * 读写权限的用户编号数组 - */ - private String rwUserIds; /** * 地区编号 */ diff --git a/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/dal/mysql/business/CrmBusinessMapper.java b/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/dal/mysql/business/CrmBusinessMapper.java index 0ae83c236..f8f856104 100644 --- a/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/dal/mysql/business/CrmBusinessMapper.java +++ b/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/dal/mysql/business/CrmBusinessMapper.java @@ -1,5 +1,6 @@ package cn.iocoder.yudao.module.crm.dal.mysql.business; +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.module.crm.controller.admin.business.vo.CrmBusinessExportReqVO; @@ -18,14 +19,14 @@ import java.util.List; @Mapper public interface CrmBusinessMapper extends BaseMapperX { - default List selectList(CrmBusinessPageReqVO reqVO, Collection ids) { - return selectList(new LambdaQueryWrapperX() + default PageResult selectPage(CrmBusinessPageReqVO reqVO, Collection ids) { + return selectPage(reqVO, new LambdaQueryWrapperX() .in(CrmBusinessDO::getId, ids) .likeIfPresent(CrmBusinessDO::getName, reqVO.getName()) .orderByDesc(CrmBusinessDO::getId)); } - default List selectList(CrmBusinessExportReqVO reqVO) { + default List selectPage(CrmBusinessExportReqVO reqVO) { return selectList(new LambdaQueryWrapperX() .likeIfPresent(CrmBusinessDO::getName, reqVO.getName()) .eqIfPresent(CrmBusinessDO::getStatusTypeId, reqVO.getStatusTypeId()) diff --git a/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/dal/mysql/customer/CrmCustomerMapper.java b/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/dal/mysql/customer/CrmCustomerMapper.java index 74cdfe5dd..683df3581 100644 --- a/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/dal/mysql/customer/CrmCustomerMapper.java +++ b/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/dal/mysql/customer/CrmCustomerMapper.java @@ -3,12 +3,11 @@ package cn.iocoder.yudao.module.crm.dal.mysql.customer; 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.module.crm.controller.admin.customer.vo.CrmCustomerExportReqVO; import cn.iocoder.yudao.module.crm.controller.admin.customer.vo.CrmCustomerPageReqVO; import cn.iocoder.yudao.module.crm.dal.dataobject.customer.CrmCustomerDO; import org.apache.ibatis.annotations.Mapper; -import java.util.List; +import java.util.Collection; /** * 客户 Mapper @@ -18,21 +17,14 @@ import java.util.List; @Mapper public interface CrmCustomerMapper extends BaseMapperX { - default PageResult selectPage(CrmCustomerPageReqVO reqVO) { - return selectPage(reqVO, new LambdaQueryWrapperX() - .likeIfPresent(CrmCustomerDO::getName, reqVO.getName()) - .eqIfPresent(CrmCustomerDO::getMobile, reqVO.getMobile()) - .eqIfPresent(CrmCustomerDO::getIndustryId, reqVO.getIndustryId()) - .eqIfPresent(CrmCustomerDO::getLevel, reqVO.getLevel()) - .eqIfPresent(CrmCustomerDO::getSource, reqVO.getSource()) - .orderByDesc(CrmCustomerDO::getId)); - } - - default List selectList(CrmCustomerExportReqVO reqVO) { - return selectList(new LambdaQueryWrapperX() - .likeIfPresent(CrmCustomerDO::getName, reqVO.getName()) - .eqIfPresent(CrmCustomerDO::getMobile, reqVO.getMobile()) - .orderByDesc(CrmCustomerDO::getId)); + default PageResult selectPage(CrmCustomerPageReqVO pageReqVO, Collection ids) { + return selectPage(pageReqVO, new LambdaQueryWrapperX() + .inIfPresent(CrmCustomerDO::getId, ids) + .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())); } } diff --git a/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/dal/mysql/permission/CrmPermissionMapper.java b/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/dal/mysql/permission/CrmPermissionMapper.java index 3b9a220fc..34a80c2c4 100644 --- a/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/dal/mysql/permission/CrmPermissionMapper.java +++ b/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/dal/mysql/permission/CrmPermissionMapper.java @@ -1,12 +1,11 @@ package cn.iocoder.yudao.module.crm.dal.mysql.permission; -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.module.crm.dal.dataobject.permission.CrmPermissionDO; -import cn.iocoder.yudao.module.crm.service.permission.bo.CrmPermissionPageReqBO; import org.apache.ibatis.annotations.Mapper; +import java.util.Collection; import java.util.List; /** @@ -30,10 +29,17 @@ public interface CrmPermissionMapper extends BaseMapperX { .eq(CrmPermissionDO::getBizId, bizId)); } - default PageResult selectPage(CrmPermissionPageReqBO pageReqBO) { - return selectPage(pageReqBO, new LambdaQueryWrapperX() - .eq(CrmPermissionDO::getBizType, pageReqBO.getBizType()) - .eq(CrmPermissionDO::getUserId, pageReqBO.getUserId())); // 只要是团队成员都有读取的权限 + default List selectListByBizTypeAndUserId(Integer bizType, Long userId) { + return selectList(new LambdaQueryWrapperX() + .eq(CrmPermissionDO::getBizType, bizType) + .eq(CrmPermissionDO::getUserId, userId)); + } + + default List selectListByBizTypeAndBizIdsAndLevel(Integer bizType, Collection bizIds, Integer level) { + return selectList(new LambdaQueryWrapperX() + .eq(CrmPermissionDO::getBizType, bizType) + .in(CrmPermissionDO::getBizId, bizIds) + .eq(CrmPermissionDO::getLevel, level)); } } diff --git a/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/service/business/CrmBusinessServiceImpl.java b/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/service/business/CrmBusinessServiceImpl.java index d19d8de46..bb154d056 100644 --- a/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/service/business/CrmBusinessServiceImpl.java +++ b/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/service/business/CrmBusinessServiceImpl.java @@ -103,21 +103,20 @@ public class CrmBusinessServiceImpl implements CrmBusinessService { @Override public PageResult getBusinessPage(CrmBusinessPageReqVO pageReqVO, Long userId) { // 1. 获取当前用户能看的分页数据 - PageResult permissionPage = crmPermissionService.getPermissionPage( - CrmBusinessConvert.INSTANCE.convert(pageReqVO).setBizType(CrmBizTypeEnum.CRM_BUSINESS.getType()).setUserId(userId)); - Set ids = convertSet(permissionPage.getList(), CrmPermissionDO::getBizId); + List permissions = crmPermissionService.getPermissionListByBizTypeAndUserId( + CrmBizTypeEnum.CRM_BUSINESS.getType(), userId); + Set ids = convertSet(permissions, CrmPermissionDO::getBizId); if (CollUtil.isEmpty(ids)) { // 没得说明没有什么给他看的 return PageResult.empty(); } // 2. 获取商机分页数据 - List businessList = businessMapper.selectList(pageReqVO, ids); - return new PageResult<>(businessList, (long) businessList.size()); + return businessMapper.selectPage(pageReqVO, ids); } @Override public List getBusinessList(CrmBusinessExportReqVO exportReqVO) { - return businessMapper.selectList(exportReqVO); + return businessMapper.selectPage(exportReqVO); } @Override diff --git a/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/service/customer/CrmCustomerService.java b/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/service/customer/CrmCustomerService.java index 503027e5f..057de7bf9 100644 --- a/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/service/customer/CrmCustomerService.java +++ b/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/service/customer/CrmCustomerService.java @@ -5,7 +5,6 @@ import cn.iocoder.yudao.module.crm.controller.admin.customer.vo.*; import cn.iocoder.yudao.module.crm.dal.dataobject.customer.CrmCustomerDO; import javax.validation.Valid; -import java.util.Collection; import java.util.List; /** @@ -50,9 +49,10 @@ public interface CrmCustomerService { * 获得客户分页 * * @param pageReqVO 分页查询 + * @param userId 用户编号 * @return 客户分页 */ - PageResult getCustomerPage(CrmCustomerPageReqVO pageReqVO); + PageResult getCustomerPage(CrmCustomerPageReqVO pageReqVO, Long userId); /** * 获得客户列表, 用于 Excel 导出 diff --git a/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/service/customer/CrmCustomerServiceImpl.java b/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/service/customer/CrmCustomerServiceImpl.java index 2580bc854..56e958334 100644 --- a/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/service/customer/CrmCustomerServiceImpl.java +++ b/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/service/customer/CrmCustomerServiceImpl.java @@ -1,9 +1,12 @@ package cn.iocoder.yudao.module.crm.service.customer; +import cn.hutool.core.collection.CollUtil; +import cn.hutool.core.util.ObjUtil; import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.module.crm.controller.admin.customer.vo.*; import cn.iocoder.yudao.module.crm.convert.customer.CrmCustomerConvert; import cn.iocoder.yudao.module.crm.dal.dataobject.customer.CrmCustomerDO; +import cn.iocoder.yudao.module.crm.dal.dataobject.permission.CrmPermissionDO; import cn.iocoder.yudao.module.crm.dal.mysql.customer.CrmCustomerMapper; import cn.iocoder.yudao.module.crm.framework.core.annotations.CrmPermission; import cn.iocoder.yudao.module.crm.framework.enums.CrmBizTypeEnum; @@ -15,10 +18,13 @@ import org.springframework.transaction.annotation.Transactional; import org.springframework.validation.annotation.Validated; import javax.annotation.Resource; +import java.util.Collections; import java.util.List; import java.util.Objects; +import java.util.Set; import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; +import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet; import static cn.iocoder.yudao.module.crm.enums.ErrorCodeConstants.CUSTOMER_NOT_EXISTS; /** @@ -51,11 +57,10 @@ public class CrmCustomerServiceImpl implements CrmCustomerService { @Override @Transactional(rollbackFor = Exception.class) - @CrmPermission(bizType = CrmBizTypeEnum.CRM_CUSTOMER, level = CrmPermissionLevelEnum.WRITE) + @CrmPermission(bizType = CrmBizTypeEnum.CRM_CUSTOMER, bizId = "#updateReqVO.id", level = CrmPermissionLevelEnum.WRITE) public void updateCustomer(CrmCustomerUpdateReqVO updateReqVO) { // 校验存在 validateCustomerExists(updateReqVO.getId()); - // TODO 芋艿:数据权限,校验是否可以操作 // 更新 CrmCustomerDO updateObj = CrmCustomerConvert.INSTANCE.convert(updateReqVO); @@ -64,11 +69,10 @@ public class CrmCustomerServiceImpl implements CrmCustomerService { @Override @Transactional(rollbackFor = Exception.class) - @CrmPermission(bizType = CrmBizTypeEnum.CRM_CUSTOMER, level = CrmPermissionLevelEnum.WRITE) + @CrmPermission(bizType = CrmBizTypeEnum.CRM_CUSTOMER, bizId = "#id", level = CrmPermissionLevelEnum.OWNER) public void deleteCustomer(Long id) { // 校验存在 validateCustomerExists(id); - // TODO 芋艿:数据权限,校验是否可以操作 // 删除 customerMapper.deleteById(id); @@ -81,20 +85,34 @@ public class CrmCustomerServiceImpl implements CrmCustomerService { } @Override - @CrmPermission(bizType = CrmBizTypeEnum.CRM_CUSTOMER, level = CrmPermissionLevelEnum.READ) + @CrmPermission(bizType = CrmBizTypeEnum.CRM_CUSTOMER, bizId = "#id", level = CrmPermissionLevelEnum.READ) public CrmCustomerDO getCustomer(Long id) { return customerMapper.selectById(id); } @Override - public PageResult getCustomerPage(CrmCustomerPageReqVO pageReqVO) { - // TODO 芋艿:数据权限,是否可以查询到; - return customerMapper.selectPage(pageReqVO); + public PageResult getCustomerPage(CrmCustomerPageReqVO pageReqVO, Long userId) { + // 1.1 TODO 如果是超级管理员 + boolean admin = false; + if (admin && ObjUtil.notEqual(userId, CrmPermissionDO.POOL_USER_ID)) { + return customerMapper.selectPage(pageReqVO, Collections.emptyList()); + } + // 1.2 获取当前用户能看的分页数据 + List permissions = crmPermissionService.getPermissionListByBizTypeAndUserId( + CrmBizTypeEnum.CRM_CUSTOMER.getType(), userId); + Set ids = convertSet(permissions, CrmPermissionDO::getBizId); + if (CollUtil.isEmpty(ids)) { // 没得说明没有什么给他看的 + return PageResult.empty(); + } + + // 2. 获取客户分页数据 + return customerMapper.selectPage(pageReqVO, ids); } @Override public List getCustomerList(CrmCustomerExportReqVO exportReqVO) { - return customerMapper.selectList(exportReqVO); + //return customerMapper.selectList(exportReqVO); + return Collections.emptyList(); } /** diff --git a/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/service/permission/CrmPermissionService.java b/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/service/permission/CrmPermissionService.java index 18c7beffc..9bb424305 100644 --- a/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/service/permission/CrmPermissionService.java +++ b/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/service/permission/CrmPermissionService.java @@ -1,15 +1,14 @@ package cn.iocoder.yudao.module.crm.service.permission; -import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.module.crm.dal.dataobject.permission.CrmPermissionDO; import cn.iocoder.yudao.module.crm.framework.enums.CrmBizTypeEnum; import cn.iocoder.yudao.module.crm.service.permission.bo.CrmPermissionCreateReqBO; -import cn.iocoder.yudao.module.crm.service.permission.bo.CrmPermissionPageReqBO; import cn.iocoder.yudao.module.crm.service.permission.bo.CrmPermissionTransferReqBO; import cn.iocoder.yudao.module.crm.service.permission.bo.CrmPermissionUpdateReqBO; import javax.validation.Valid; +import java.util.Collection; import java.util.List; /** @@ -60,6 +59,16 @@ public interface CrmPermissionService { */ List getPermissionByBizTypeAndBizId(Integer bizType, Long bizId); + /** + * 获取数据权限列表,通过 数据类型 x 某个数据 + * + * @param bizType 数据类型,关联 {@link CrmBizTypeEnum} + * @param bizIds 数据编号,关联 {@link CrmBizTypeEnum} 对应模块 DO#getId() + * @param level 权限级别 + * @return Crm 数据权限列表 + */ + List getPermissionByBizTypeAndBizIdsAndLevel(Integer bizType, Collection bizIds, Integer level); + /** * 数据权限转移 * @@ -68,11 +77,12 @@ public interface CrmPermissionService { void transferPermission(@Valid CrmPermissionTransferReqBO crmPermissionTransferReqBO); /** - * 获取数据权限分页数据 + * 获取用户参与的模块数据列表 * - * @param pageReqBO 分页请求 - * @return 数据权限分页数据 + * @param bizType 模块类型 + * @param userId 用户编号 + * @return 模块数据列表 */ - PageResult getPermissionPage(CrmPermissionPageReqBO pageReqBO); + List getPermissionListByBizTypeAndUserId(Integer bizType, Long userId); } diff --git a/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/service/permission/CrmPermissionServiceImpl.java b/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/service/permission/CrmPermissionServiceImpl.java index 4265545a4..b24ae4156 100644 --- a/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/service/permission/CrmPermissionServiceImpl.java +++ b/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/service/permission/CrmPermissionServiceImpl.java @@ -2,14 +2,12 @@ package cn.iocoder.yudao.module.crm.service.permission; import cn.hutool.core.collection.CollUtil; import cn.hutool.core.util.ObjUtil; -import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.module.crm.convert.permission.CrmPermissionConvert; import cn.iocoder.yudao.module.crm.dal.dataobject.permission.CrmPermissionDO; import cn.iocoder.yudao.module.crm.dal.mysql.permission.CrmPermissionMapper; 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.permission.bo.CrmPermissionCreateReqBO; -import cn.iocoder.yudao.module.crm.service.permission.bo.CrmPermissionPageReqBO; import cn.iocoder.yudao.module.crm.service.permission.bo.CrmPermissionTransferReqBO; import cn.iocoder.yudao.module.crm.service.permission.bo.CrmPermissionUpdateReqBO; import cn.iocoder.yudao.module.system.api.user.AdminUserApi; @@ -18,6 +16,7 @@ import org.springframework.transaction.annotation.Transactional; import org.springframework.validation.annotation.Validated; import javax.annotation.Resource; +import java.util.Collection; import java.util.Collections; import java.util.List; @@ -85,6 +84,11 @@ public class CrmPermissionServiceImpl implements CrmPermissionService { return crmPermissionMapper.selectByBizTypeAndBizId(bizType, bizId); } + @Override + public List getPermissionByBizTypeAndBizIdsAndLevel(Integer bizType, Collection bizIds, Integer level) { + return crmPermissionMapper.selectListByBizTypeAndBizIdsAndLevel(bizType, bizIds, level); + } + private void validateCrmPermissionExists(Long id) { if (crmPermissionMapper.selectById(id) == null) { throw exception(CRM_PERMISSION_NOT_EXISTS); @@ -133,8 +137,8 @@ public class CrmPermissionServiceImpl implements CrmPermissionService { } @Override - public PageResult getPermissionPage(CrmPermissionPageReqBO pageReqBO) { - return crmPermissionMapper.selectPage(pageReqBO); + public List getPermissionListByBizTypeAndUserId(Integer bizType, Long userId) { + return crmPermissionMapper.selectListByBizTypeAndUserId(bizType, userId); } } diff --git a/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/service/permission/bo/CrmPermissionPageReqBO.java b/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/service/permission/bo/CrmPermissionPageReqBO.java deleted file mode 100644 index 736171c1e..000000000 --- a/yudao-module-crm/yudao-module-crm-biz/src/main/java/cn/iocoder/yudao/module/crm/service/permission/bo/CrmPermissionPageReqBO.java +++ /dev/null @@ -1,32 +0,0 @@ -package cn.iocoder.yudao.module.crm.service.permission.bo; - -import cn.iocoder.yudao.framework.common.pojo.PageParam; -import cn.iocoder.yudao.framework.common.validation.InEnum; -import cn.iocoder.yudao.module.crm.framework.enums.CrmBizTypeEnum; -import io.swagger.v3.oas.annotations.media.Schema; -import lombok.Data; -import lombok.EqualsAndHashCode; -import lombok.ToString; - -import javax.validation.constraints.NotNull; - -@Schema(description = "管理后台 - CRM 数据权限分页 Request BO") -@Data -@EqualsAndHashCode(callSuper = true) -@ToString(callSuper = true) -public class CrmPermissionPageReqBO extends PageParam { - - /** - * 当前登录用户编号 - */ - @NotNull(message = "用户编号不能为空") - private Long userId; - - /** - * Crm 类型 - */ - @NotNull(message = "Crm 类型不能为空") - @InEnum(CrmBizTypeEnum.class) - private Integer bizType; - -} diff --git a/yudao-module-crm/yudao-module-crm-biz/src/test/java/cn/iocoder/yudao/module/crm/service/customer/CrmCustomerServiceImplTest.java b/yudao-module-crm/yudao-module-crm-biz/src/test/java/cn/iocoder/yudao/module/crm/service/customer/CrmCustomerServiceImplTest.java index 41dd14f92..4908f79e2 100644 --- a/yudao-module-crm/yudao-module-crm-biz/src/test/java/cn/iocoder/yudao/module/crm/service/customer/CrmCustomerServiceImplTest.java +++ b/yudao-module-crm/yudao-module-crm-biz/src/test/java/cn/iocoder/yudao/module/crm/service/customer/CrmCustomerServiceImplTest.java @@ -130,7 +130,7 @@ public class CrmCustomerServiceImplTest extends BaseDbUnitTest { //reqVO.setWebsite(null); // 调用 - PageResult pageResult = customerService.getCustomerPage(reqVO); + PageResult pageResult = customerService.getCustomerPage(reqVO, 1L); // 断言 assertEquals(1, pageResult.getTotal()); assertEquals(1, pageResult.getList().size());