refactor(系统设置): 组织列表关联SQL拆开

This commit is contained in:
song-cc-rock 2023-09-08 16:19:22 +08:00 committed by fit2-zhao
parent 43ca83399e
commit 6e1229b332
6 changed files with 127 additions and 44 deletions

View File

@ -13,6 +13,7 @@ import io.metersphere.sdk.constants.UserRoleEnum;
import io.metersphere.sdk.constants.UserRoleType; import io.metersphere.sdk.constants.UserRoleType;
import io.metersphere.sdk.dto.LogDTO; import io.metersphere.sdk.dto.LogDTO;
import io.metersphere.sdk.dto.OptionDTO; import io.metersphere.sdk.dto.OptionDTO;
import io.metersphere.sdk.dto.UserExtend;
import io.metersphere.sdk.exception.MSException; import io.metersphere.sdk.exception.MSException;
import io.metersphere.sdk.log.constants.OperationLogModule; import io.metersphere.sdk.log.constants.OperationLogModule;
import io.metersphere.sdk.log.constants.OperationLogType; import io.metersphere.sdk.log.constants.OperationLogType;
@ -22,7 +23,6 @@ import io.metersphere.sdk.util.BeanUtils;
import io.metersphere.sdk.util.JSON; import io.metersphere.sdk.util.JSON;
import io.metersphere.sdk.util.Translator; import io.metersphere.sdk.util.Translator;
import io.metersphere.system.domain.*; import io.metersphere.system.domain.*;
import io.metersphere.sdk.dto.UserExtend;
import io.metersphere.system.mapper.UserMapper; import io.metersphere.system.mapper.UserMapper;
import io.metersphere.system.mapper.UserRoleMapper; import io.metersphere.system.mapper.UserRoleMapper;
import io.metersphere.system.mapper.UserRoleRelationMapper; import io.metersphere.system.mapper.UserRoleRelationMapper;

View File

@ -31,6 +31,7 @@ import static io.metersphere.system.controller.result.SystemResultCode.NO_PROJEC
/** /**
* 项目-用户组与权限 * 项目-用户组与权限
*
* @author song-cc-rock * @author song-cc-rock
*/ */
@Service @Service
@ -185,6 +186,7 @@ public class ProjectUserRoleService extends BaseUserRoleService {
/** /**
* 校验是否项目下用户组 * 校验是否项目下用户组
*
* @param userRole 用户组 * @param userRole 用户组
*/ */
private void checkProjectUserRole(UserRole userRole) { private void checkProjectUserRole(UserRole userRole) {

View File

@ -0,0 +1,27 @@
package io.metersphere.system.dto;
import lombok.Data;
import lombok.EqualsAndHashCode;
/**
* @author song-cc-rock
*/
@Data
@EqualsAndHashCode(callSuper = false)
public class OrganizationCountDTO {
/**
* 成员数量
*/
private Integer memberCount;
/**
* 项目数量
*/
private Integer projectCount;
/**
* 组织ID
*/
private String id;
}

View File

@ -17,31 +17,44 @@ public interface ExtOrganizationMapper {
/** /**
* 查询组织列表 * 查询组织列表
*
* @param request 列表请求参数 * @param request 列表请求参数
* @return 组织列表数据 * @return 组织列表数据
*/ */
List<OrganizationDTO> list(@Param("request") OrganizationRequest request); List<OrganizationDTO> list(@Param("request") OrganizationRequest request);
/**
* 通过组织ID获取项目及成员数量
*
* @param ids 组织ID集合
* @return 项目及成员数量
*/
List<OrganizationCountDTO> getCountByIds(@Param("ids") List<String> ids);
/** /**
* 获取系统下所有组织 * 获取系统下所有组织
*
* @return 组织列表数据 * @return 组织列表数据
*/ */
List<OrganizationDTO> listAll(); List<OrganizationDTO> listAll();
/** /**
* 删除组织 * 删除组织
*
* @param organizationDeleteRequest 组织删除参数 * @param organizationDeleteRequest 组织删除参数
*/ */
void delete(@Param("request") OrganizationDeleteRequest organizationDeleteRequest); void delete(@Param("request") OrganizationDeleteRequest organizationDeleteRequest);
/** /**
* 恢复组织 * 恢复组织
*
* @param id 组织ID * @param id 组织ID
*/ */
void recover(String id); void recover(String id);
/** /**
* 更新组织启用/禁用状态 * 更新组织启用/禁用状态
*
* @param id 组织ID * @param id 组织ID
* @param enable 是否启用 * @param enable 是否启用
*/ */
@ -49,6 +62,7 @@ public interface ExtOrganizationMapper {
/** /**
* 获取组织成员列表(角色) * 获取组织成员列表(角色)
*
* @param request 组织成员列表请求参数 * @param request 组织成员列表请求参数
* @return 组织成员列表数据 * @return 组织成员列表数据
*/ */
@ -57,6 +71,7 @@ public interface ExtOrganizationMapper {
/** /**
* 获取组织成员列表(角色) * 获取组织成员列表(角色)
*
* @param request 组织成员列表请求参数 * @param request 组织成员列表请求参数
* @return 组织成员列表数据 * @return 组织成员列表数据
*/ */
@ -64,6 +79,7 @@ public interface ExtOrganizationMapper {
/** /**
* 获取组织成员下拉列表(角色) * 获取组织成员下拉列表(角色)
*
* @param keyword 组织成员列表请求参数 * @param keyword 组织成员列表请求参数
* @return 组织成员列表数据 * @return 组织成员列表数据
*/ */
@ -71,6 +87,7 @@ public interface ExtOrganizationMapper {
/** /**
* 获取组织管理员 * 获取组织管理员
*
* @param orgId 组织ID * @param orgId 组织ID
* @return 组织管理员数据 * @return 组织管理员数据
*/ */
@ -78,12 +95,14 @@ public interface ExtOrganizationMapper {
/** /**
* 获取组织列表(下拉框) * 获取组织列表(下拉框)
*
* @return 组织列表数据 * @return 组织列表数据
*/ */
List<OrganizationProjectOptionsDTO> selectOrganizationOptions(); List<OrganizationProjectOptionsDTO> selectOrganizationOptions();
/** /**
* 获取组织下拉选项 * 获取组织下拉选项
*
* @param ids 组织ID集合 * @param ids 组织ID集合
* @return 组织下拉选项 * @return 组织下拉选项
*/ */

View File

@ -3,35 +3,57 @@
<mapper namespace="io.metersphere.system.mapper.ExtOrganizationMapper"> <mapper namespace="io.metersphere.system.mapper.ExtOrganizationMapper">
<select id="list" resultType="io.metersphere.system.dto.OrganizationDTO"> <select id="list" resultType="io.metersphere.system.dto.OrganizationDTO">
select o.id, o.num, o.name, o.description, o.create_time, select o.id, o.num, o.name, o.description, o.create_time,
o.update_time, o.create_user, o.deleted, o.delete_user, o.delete_time, o.enable, o.update_time, o.create_user, o.deleted, o.delete_user, o.delete_time, o.enable
from organization o
<include refid="queryWhereCondition"/>
</select>
<select id="getCountByIds" resultType="io.metersphere.system.dto.OrganizationCountDTO">
select o.id,
coalesce(membercount, 0) as memberCount, coalesce(projectcount, 0) as projectCount coalesce(membercount, 0) as memberCount, coalesce(projectcount, 0) as projectCount
from organization o from organization o
left join ( left join (
select source_id, count(distinct u.id) as membercount from user_role_relation ur select source_id, count(distinct u.id) as membercount from user_role_relation ur
join user u on ur.user_id = u.id and u.deleted = 0 join user u on ur.user_id = u.id and u.deleted = 0
where source_id in
<foreach collection="ids" item="id" open="(" separator="," close=")">
#{id}
</foreach>
group by source_id group by source_id
) as members_group on o.id = members_group.source_id ) as members_group on o.id = members_group.source_id
left join ( left join (
select organization_id, count(project.id) as projectcount from project select organization_id, count(project.id) as projectcount from project
where organization_id in
<foreach collection="ids" item="id" open="(" separator="," close=")">
#{id}
</foreach>
group by organization_id group by organization_id
) as projects_group on o.id = projects_group.organization_id ) as projects_group on o.id = projects_group.organization_id
<include refid="queryWhereCondition"/>
</select> </select>
<select id="listAll" resultType="io.metersphere.system.dto.OrganizationDTO"> <select id="listAll" resultType="io.metersphere.system.dto.OrganizationDTO">
select * from organization select *
from organization
</select> </select>
<update id="delete" parameterType="io.metersphere.system.request.OrganizationDeleteRequest"> <update id="delete" parameterType="io.metersphere.system.request.OrganizationDeleteRequest">
update organization set deleted = true , delete_time = #{request.deleteTime} , delete_user = #{request.deleteUserId} where id = #{request.organizationId} update organization
set deleted = true,
delete_time = #{request.deleteTime},
delete_user = #{request.deleteUserId}
where id = #{request.organizationId}
</update> </update>
<update id="recover" parameterType="java.lang.String"> <update id="recover" parameterType="java.lang.String">
update organization set deleted = false where id = #{id} update organization
set deleted = false
where id = #{id}
</update> </update>
<update id="updateEnable"> <update id="updateEnable">
update organization set `enable` = #{enable} where id = #{id} update organization
set `enable` = #{enable}
where id = #{id}
</update> </update>
<select id="listMember" resultType="io.metersphere.sdk.dto.UserExtend"> <select id="listMember" resultType="io.metersphere.sdk.dto.UserExtend">
@ -91,11 +113,15 @@
select u.* select u.*
from user_role_relation urr from user_role_relation urr
join `user` u on urr.user_id = u.id join `user` u on urr.user_id = u.id
where urr.role_id = 'org_admin'and urr.source_id = #{orgId} and u.deleted = false where urr.role_id = 'org_admin'
and urr.source_id = #{orgId}
and u.deleted = false
</select> </select>
<select id="selectOrganizationOptions" resultType="io.metersphere.system.dto.OrganizationProjectOptionsDTO"> <select id="selectOrganizationOptions" resultType="io.metersphere.system.dto.OrganizationProjectOptionsDTO">
select id, name from organization order by create_time desc select id, name
from organization
order by create_time desc
</select> </select>
<select id="getOptionsByIds" resultType="io.metersphere.sdk.dto.OptionDTO"> <select id="getOptionsByIds" resultType="io.metersphere.sdk.dto.OptionDTO">

View File

@ -6,6 +6,7 @@ import io.metersphere.project.mapper.ProjectMapper;
import io.metersphere.sdk.constants.*; import io.metersphere.sdk.constants.*;
import io.metersphere.sdk.dto.LogDTO; import io.metersphere.sdk.dto.LogDTO;
import io.metersphere.sdk.dto.OptionDTO; import io.metersphere.sdk.dto.OptionDTO;
import io.metersphere.sdk.dto.UserExtend;
import io.metersphere.sdk.exception.MSException; import io.metersphere.sdk.exception.MSException;
import io.metersphere.sdk.log.constants.OperationLogModule; import io.metersphere.sdk.log.constants.OperationLogModule;
import io.metersphere.sdk.log.constants.OperationLogType; import io.metersphere.sdk.log.constants.OperationLogType;
@ -17,7 +18,6 @@ import io.metersphere.sdk.util.JSON;
import io.metersphere.sdk.util.ServiceUtils; import io.metersphere.sdk.util.ServiceUtils;
import io.metersphere.sdk.util.Translator; import io.metersphere.sdk.util.Translator;
import io.metersphere.system.domain.*; import io.metersphere.system.domain.*;
import io.metersphere.sdk.dto.UserExtend;
import io.metersphere.system.dto.*; import io.metersphere.system.dto.*;
import io.metersphere.system.mapper.*; import io.metersphere.system.mapper.*;
import io.metersphere.system.request.*; import io.metersphere.system.request.*;
@ -81,7 +81,10 @@ public class OrganizationService {
*/ */
public List<OrganizationDTO> list(OrganizationRequest organizationRequest) { public List<OrganizationDTO> list(OrganizationRequest organizationRequest) {
List<OrganizationDTO> organizationDTOS = extOrganizationMapper.list(organizationRequest); List<OrganizationDTO> organizationDTOS = extOrganizationMapper.list(organizationRequest);
List<OrganizationDTO> organizations = buildUserInfo(organizationDTOS); if (CollectionUtils.isEmpty(organizationDTOS)) {
return new ArrayList<>();
}
List<OrganizationDTO> organizations = buildExtraInfo(organizationDTOS);
return buildOrgAdminInfo(organizations); return buildOrgAdminInfo(organizations);
} }
@ -198,6 +201,7 @@ public class OrganizationService {
userRoleRelationMapper.batchInsert(userRoleRelations); userRoleRelationMapper.batchInsert(userRoleRelations);
} }
} }
/** /**
* 删除组织成员 * 删除组织成员
* *
@ -262,8 +266,8 @@ public class OrganizationService {
//根据关系表查询出用户的关联组织和用户组 //根据关系表查询出用户的关联组织和用户组
Map<String, Set<String>> userIdRoleIdMap = new HashMap<>(); Map<String, Set<String>> userIdRoleIdMap = new HashMap<>();
Map<String, Set<String>> userIdProjectIdMap = new HashMap<>(); Map<String, Set<String>> userIdProjectIdMap = new HashMap<>();
Set<String>roleIdSet = new HashSet<>(); Set<String> roleIdSet = new HashSet<>();
Set<String>projectIdSet = new HashSet<>(); Set<String> projectIdSet = new HashSet<>();
for (UserRoleRelation userRoleRelationsByUser : userRoleRelationsByUsers) { for (UserRoleRelation userRoleRelationsByUser : userRoleRelationsByUsers) {
String sourceId = userRoleRelationsByUser.getSourceId(); String sourceId = userRoleRelationsByUser.getSourceId();
String roleId = userRoleRelationsByUser.getRoleId(); String roleId = userRoleRelationsByUser.getRoleId();
@ -282,14 +286,14 @@ public class OrganizationService {
List<UserRole> userRoles = userRoleMapper.selectByExample(userRoleExample); List<UserRole> userRoles = userRoleMapper.selectByExample(userRoleExample);
List<Project> projects = new ArrayList<>(); List<Project> projects = new ArrayList<>();
if (projectIdSet.size()>0) { if (projectIdSet.size() > 0) {
ProjectExample projectExample = new ProjectExample(); ProjectExample projectExample = new ProjectExample();
projectExample.createCriteria().andIdIn(new ArrayList<>(projectIdSet)); projectExample.createCriteria().andIdIn(new ArrayList<>(projectIdSet));
projects = projectMapper.selectByExample(projectExample); projects = projectMapper.selectByExample(projectExample);
} }
for (OrgUserExtend orgUserExtend : orgUserExtends) { for (OrgUserExtend orgUserExtend : orgUserExtends) {
if (projects.size()>0) { if (projects.size() > 0) {
Set<String> projectIds = userIdProjectIdMap.get(orgUserExtend.getId()); Set<String> projectIds = userIdProjectIdMap.get(orgUserExtend.getId());
if (CollectionUtils.isNotEmpty(projectIds)) { if (CollectionUtils.isNotEmpty(projectIds)) {
List<Project> projectFilters = projects.stream().filter(t -> projectIds.contains(t.getId())).toList(); List<Project> projectFilters = projects.stream().filter(t -> projectIds.contains(t.getId())).toList();
@ -806,21 +810,26 @@ public class OrganizationService {
} }
/** /**
* 设置用户信息 * 设置列表其他信息(用户信息, 项目及成员数量)
* *
* @param organizationDTOS 组织集合 * @param organizationDTOS 组织集合
* @return 组织集合 * @return 组织集合
*/ */
private List<OrganizationDTO> buildUserInfo(List<OrganizationDTO> organizationDTOS) { private List<OrganizationDTO> buildExtraInfo(List<OrganizationDTO> organizationDTOS) {
List<String> userIds = new ArrayList<>(); List<String> userIds = new ArrayList<>();
userIds.addAll(organizationDTOS.stream().map(OrganizationDTO::getCreateUser).toList()); userIds.addAll(organizationDTOS.stream().map(OrganizationDTO::getCreateUser).toList());
userIds.addAll(organizationDTOS.stream().map(OrganizationDTO::getUpdateUser).toList()); userIds.addAll(organizationDTOS.stream().map(OrganizationDTO::getUpdateUser).toList());
userIds.addAll(organizationDTOS.stream().map(OrganizationDTO::getDeleteUser).toList()); userIds.addAll(organizationDTOS.stream().map(OrganizationDTO::getDeleteUser).toList());
Map<String, String> userMap = baseUserService.getUserNameMap(userIds.stream().distinct().toList()); Map<String, String> userMap = baseUserService.getUserNameMap(userIds.stream().distinct().toList());
List<String> ids = organizationDTOS.stream().map(OrganizationDTO::getId).toList();
List<OrganizationCountDTO> orgCountList = extOrganizationMapper.getCountByIds(ids);
Map<String, OrganizationCountDTO> orgCountMap = orgCountList.stream().collect(Collectors.toMap(OrganizationCountDTO::getId, count -> count));
organizationDTOS.forEach(organizationDTO -> { organizationDTOS.forEach(organizationDTO -> {
organizationDTO.setCreateUser(userMap.get(organizationDTO.getCreateUser())); organizationDTO.setCreateUser(userMap.get(organizationDTO.getCreateUser()));
organizationDTO.setDeleteUser(userMap.get(organizationDTO.getDeleteUser())); organizationDTO.setDeleteUser(userMap.get(organizationDTO.getDeleteUser()));
organizationDTO.setUpdateUser(userMap.get(organizationDTO.getUpdateUser())); organizationDTO.setUpdateUser(userMap.get(organizationDTO.getUpdateUser()));
organizationDTO.setProjectCount(orgCountMap.get(organizationDTO.getId()).getProjectCount());
organizationDTO.setMemberCount(orgCountMap.get(organizationDTO.getId()).getMemberCount());
}); });
return organizationDTOS; return organizationDTOS;
} }