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.dto.LogDTO;
import io.metersphere.sdk.dto.OptionDTO;
import io.metersphere.sdk.dto.UserExtend;
import io.metersphere.sdk.exception.MSException;
import io.metersphere.sdk.log.constants.OperationLogModule;
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.Translator;
import io.metersphere.system.domain.*;
import io.metersphere.sdk.dto.UserExtend;
import io.metersphere.system.mapper.UserMapper;
import io.metersphere.system.mapper.UserRoleMapper;
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
*/
@Service
@ -185,6 +186,7 @@ public class ProjectUserRoleService extends BaseUserRoleService {
/**
* 校验是否项目下用户组
*
* @param 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,38 +17,52 @@ public interface ExtOrganizationMapper {
/**
* 查询组织列表
*
* @param request 列表请求参数
* @return 组织列表数据
*/
List<OrganizationDTO> list(@Param("request") OrganizationRequest request);
/**
* 通过组织ID获取项目及成员数量
*
* @param ids 组织ID集合
* @return 项目及成员数量
*/
List<OrganizationCountDTO> getCountByIds(@Param("ids") List<String> ids);
/**
* 获取系统下所有组织
*
* @return 组织列表数据
*/
List<OrganizationDTO> listAll();
/**
* 删除组织
*
* @param organizationDeleteRequest 组织删除参数
*/
void delete(@Param("request") OrganizationDeleteRequest organizationDeleteRequest);
/**
* 恢复组织
*
* @param id 组织ID
*/
void recover(String id);
/**
* 更新组织启用/禁用状态
* @param id 组织ID
*
* @param id 组织ID
* @param enable 是否启用
*/
void updateEnable(String id, boolean enable);
/**
* 获取组织成员列表(角色)
*
* @param request 组织成员列表请求参数
* @return 组织成员列表数据
*/
@ -57,6 +71,7 @@ public interface ExtOrganizationMapper {
/**
* 获取组织成员列表(角色)
*
* @param request 组织成员列表请求参数
* @return 组织成员列表数据
*/
@ -64,6 +79,7 @@ public interface ExtOrganizationMapper {
/**
* 获取组织成员下拉列表(角色)
*
* @param keyword 组织成员列表请求参数
* @return 组织成员列表数据
*/
@ -71,6 +87,7 @@ public interface ExtOrganizationMapper {
/**
* 获取组织管理员
*
* @param orgId 组织ID
* @return 组织管理员数据
*/
@ -78,12 +95,14 @@ public interface ExtOrganizationMapper {
/**
* 获取组织列表(下拉框)
*
* @return 组织列表数据
*/
List<OrganizationProjectOptionsDTO> selectOrganizationOptions();
/**
* 获取组织下拉选项
*
* @param ids 组织ID集合
* @return 组织下拉选项
*/

View File

@ -1,55 +1,77 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="io.metersphere.system.mapper.ExtOrganizationMapper">
<select id="list" resultType="io.metersphere.system.dto.OrganizationDTO">
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,
coalesce(membercount, 0) as memberCount, coalesce(projectcount, 0) as projectCount
from organization o
left join (
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
group by source_id
) as members_group on o.id = members_group.source_id
left join (
select organization_id, count(project.id) as projectcount from project
group by organization_id
) as projects_group on o.id = projects_group.organization_id
<select id="list" resultType="io.metersphere.system.dto.OrganizationDTO">
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
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
from organization o
left join (
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
where source_id in
<foreach collection="ids" item="id" open="(" separator="," close=")">
#{id}
</foreach>
group by source_id
) as members_group on o.id = members_group.source_id
left join (
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
) as projects_group on o.id = projects_group.organization_id
</select>
<select id="listAll" resultType="io.metersphere.system.dto.OrganizationDTO">
select * from organization
select *
from organization
</select>
<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 id="recover" parameterType="java.lang.String">
update organization set deleted = false where id = #{id}
update organization
set deleted = false
where id = #{id}
</update>
<update id="updateEnable">
update organization set `enable` = #{enable} where id = #{id}
update organization
set `enable` = #{enable}
where id = #{id}
</update>
<select id="listMember" resultType="io.metersphere.sdk.dto.UserExtend">
select temp.*, max(if(temp.role_id = 'org_admin', true, false)) as adminFlag
from (
select u.*, urr.role_id
from user_role_relation urr
join `user` u on urr.user_id = u.id
<where>
u.deleted = 0
<if test="request.organizationId != null and request.organizationId != ''">
and urr.source_id = #{request.organizationId}
</if>
<if test="request.keyword != null and request.keyword != ''">
and u.name like concat('%', #{request.keyword},'%')
</if>
</where>
order by u.update_time desc
select u.*, urr.role_id
from user_role_relation urr
join `user` u on urr.user_id = u.id
<where>
u.deleted = 0
<if test="request.organizationId != null and request.organizationId != ''">
and urr.source_id = #{request.organizationId}
</if>
<if test="request.keyword != null and request.keyword != ''">
and u.name like concat('%', #{request.keyword},'%')
</if>
</where>
order by u.update_time desc
) temp
group by temp.id
order by adminFlag desc
@ -90,12 +112,16 @@
<select id="getOrgAdminList" resultType="io.metersphere.system.domain.User">
select u.*
from user_role_relation urr
join `user` u on urr.user_id = u.id
where urr.role_id = 'org_admin'and urr.source_id = #{orgId} and u.deleted = false
join `user` u on urr.user_id = u.id
where urr.role_id = 'org_admin'
and urr.source_id = #{orgId}
and u.deleted = false
</select>
<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 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.dto.LogDTO;
import io.metersphere.sdk.dto.OptionDTO;
import io.metersphere.sdk.dto.UserExtend;
import io.metersphere.sdk.exception.MSException;
import io.metersphere.sdk.log.constants.OperationLogModule;
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.Translator;
import io.metersphere.system.domain.*;
import io.metersphere.sdk.dto.UserExtend;
import io.metersphere.system.dto.*;
import io.metersphere.system.mapper.*;
import io.metersphere.system.request.*;
@ -81,7 +81,10 @@ public class OrganizationService {
*/
public List<OrganizationDTO> list(OrganizationRequest 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);
}
@ -198,6 +201,7 @@ public class OrganizationService {
userRoleRelationMapper.batchInsert(userRoleRelations);
}
}
/**
* 删除组织成员
*
@ -262,8 +266,8 @@ public class OrganizationService {
//根据关系表查询出用户的关联组织和用户组
Map<String, Set<String>> userIdRoleIdMap = new HashMap<>();
Map<String, Set<String>> userIdProjectIdMap = new HashMap<>();
Set<String>roleIdSet = new HashSet<>();
Set<String>projectIdSet = new HashSet<>();
Set<String> roleIdSet = new HashSet<>();
Set<String> projectIdSet = new HashSet<>();
for (UserRoleRelation userRoleRelationsByUser : userRoleRelationsByUsers) {
String sourceId = userRoleRelationsByUser.getSourceId();
String roleId = userRoleRelationsByUser.getRoleId();
@ -282,14 +286,14 @@ public class OrganizationService {
List<UserRole> userRoles = userRoleMapper.selectByExample(userRoleExample);
List<Project> projects = new ArrayList<>();
if (projectIdSet.size()>0) {
if (projectIdSet.size() > 0) {
ProjectExample projectExample = new ProjectExample();
projectExample.createCriteria().andIdIn(new ArrayList<>(projectIdSet));
projects = projectMapper.selectByExample(projectExample);
}
for (OrgUserExtend orgUserExtend : orgUserExtends) {
if (projects.size()>0) {
if (projects.size() > 0) {
Set<String> projectIds = userIdProjectIdMap.get(orgUserExtend.getId());
if (CollectionUtils.isNotEmpty(projectIds)) {
List<Project> projectFilters = projects.stream().filter(t -> projectIds.contains(t.getId())).toList();
@ -806,21 +810,26 @@ public class OrganizationService {
}
/**
* 设置用户信息
* 设置列表其他信息(用户信息, 项目及成员数量)
*
* @param organizationDTOS 组织集合
* @return 组织集合
*/
private List<OrganizationDTO> buildUserInfo(List<OrganizationDTO> organizationDTOS) {
private List<OrganizationDTO> buildExtraInfo(List<OrganizationDTO> organizationDTOS) {
List<String> userIds = new ArrayList<>();
userIds.addAll(organizationDTOS.stream().map(OrganizationDTO::getCreateUser).toList());
userIds.addAll(organizationDTOS.stream().map(OrganizationDTO::getUpdateUser).toList());
userIds.addAll(organizationDTOS.stream().map(OrganizationDTO::getDeleteUser).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 -> {
organizationDTO.setCreateUser(userMap.get(organizationDTO.getCreateUser()));
organizationDTO.setDeleteUser(userMap.get(organizationDTO.getDeleteUser()));
organizationDTO.setUpdateUser(userMap.get(organizationDTO.getUpdateUser()));
organizationDTO.setProjectCount(orgCountMap.get(organizationDTO.getId()).getProjectCount());
organizationDTO.setMemberCount(orgCountMap.get(organizationDTO.getId()).getMemberCount());
});
return organizationDTOS;
}