refactor(系统设置): 优化定时任务批量删除项目

This commit is contained in:
wxg0103 2023-09-05 19:42:51 +08:00 committed by wxg0103
parent 3af3fd8a4f
commit 2a90d2b8de
30 changed files with 80 additions and 61 deletions

View File

@ -1,7 +1,6 @@
package io.metersphere.sdk.dto;
import io.metersphere.project.domain.Project;
import io.metersphere.system.domain.User;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;
@ -17,7 +16,7 @@ public class ProjectDTO extends Project implements Serializable {
@Schema(description = "所属组织", requiredMode = Schema.RequiredMode.NOT_REQUIRED)
private String organizationName;
@Schema(description = "管理员", requiredMode = Schema.RequiredMode.NOT_REQUIRED)
private List<User> adminList;
private List<UserExtend> adminList;
@Schema(description = "创建人是否是管理员", requiredMode = Schema.RequiredMode.NOT_REQUIRED)
private Boolean projectCreateUserIsAdmin;
@Schema(description = "模块设置", requiredMode = Schema.RequiredMode.NOT_REQUIRED)

View File

@ -1,4 +1,4 @@
package io.metersphere.system.dto;
package io.metersphere.sdk.dto;
import io.metersphere.system.domain.User;
import io.swagger.v3.oas.annotations.media.Schema;
@ -33,4 +33,7 @@ public class UserExtend extends User implements Serializable {
*/
@Schema(description = "是否属于用户组, 是: 勾选禁用, 否: 勾选启用")
private boolean checkRoleFlag;
@Schema(description = "组织ID")
private String sourceId;
}

View File

@ -109,5 +109,5 @@ permission.project_application_test_plan.read=测试计划-查询
permission.project_application_test_plan.update=测试计划-编辑
permission.project_application_ui.read=UI测试-查询
permission.project_application_ui.update=UI测试-编辑
permission.project_base_info.name=信息
permission.project_base_info.name=信息
permission.project_log.name=日志

View File

@ -109,5 +109,5 @@ permission.project_application_test_plan.read=測試計劃-查詢
permission.project_application_test_plan.update=測試計劃-編輯
permission.project_application_ui.read=UI測試-查詢
permission.project_application_ui.update=UI測試-編輯
permission.project_base_info.name=信息
permission.project_base_info.name=信息
permission.project_log.name=日誌

View File

@ -13,7 +13,7 @@ import io.metersphere.sdk.dto.OptionDTO;
import io.metersphere.sdk.util.PageUtils;
import io.metersphere.sdk.util.Pager;
import io.metersphere.sdk.util.SessionUtils;
import io.metersphere.system.dto.UserExtend;
import io.metersphere.sdk.dto.UserExtend;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;

View File

@ -9,6 +9,7 @@ import io.metersphere.project.service.ProjectUserRoleLogService;
import io.metersphere.project.service.ProjectUserRoleService;
import io.metersphere.sdk.constants.PermissionConstants;
import io.metersphere.sdk.dto.PermissionDefinitionItem;
import io.metersphere.sdk.dto.UserExtend;
import io.metersphere.sdk.dto.request.PermissionSettingUpdateRequest;
import io.metersphere.sdk.log.annotation.Log;
import io.metersphere.sdk.log.constants.OperationLogType;
@ -18,7 +19,6 @@ import io.metersphere.sdk.util.Pager;
import io.metersphere.sdk.util.SessionUtils;
import io.metersphere.system.domain.User;
import io.metersphere.system.domain.UserRole;
import io.metersphere.system.dto.UserExtend;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;

View File

@ -1,7 +1,7 @@
package io.metersphere.project.mapper;
import io.metersphere.project.request.ProjectMemberRequest;
import io.metersphere.system.dto.UserExtend;
import io.metersphere.sdk.dto.UserExtend;
import org.apache.ibatis.annotations.Param;
import java.util.List;

View File

@ -16,7 +16,7 @@
</where>
</select>
<select id="getMemberByOrg" resultType="io.metersphere.system.dto.UserExtend">
<select id="getMemberByOrg" resultType="io.metersphere.sdk.dto.UserExtend">
select distinct u.* from user_role_relation urr join `user` u on urr.user_id = u.id
where
u.deleted = 0

View File

@ -22,7 +22,7 @@ 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.system.dto.UserExtend;
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

@ -7,13 +7,13 @@ import io.metersphere.sdk.constants.InternalUserRole;
import io.metersphere.sdk.constants.UserRoleEnum;
import io.metersphere.sdk.constants.UserRoleType;
import io.metersphere.sdk.dto.PermissionDefinitionItem;
import io.metersphere.sdk.dto.UserExtend;
import io.metersphere.sdk.dto.request.PermissionSettingUpdateRequest;
import io.metersphere.sdk.exception.MSException;
import io.metersphere.sdk.service.BaseUserRoleService;
import io.metersphere.sdk.util.BeanUtils;
import io.metersphere.sdk.util.Translator;
import io.metersphere.system.domain.*;
import io.metersphere.system.dto.UserExtend;
import io.metersphere.system.mapper.UserMapper;
import io.metersphere.system.mapper.UserRoleMapper;
import io.metersphere.system.mapper.UserRoleRelationMapper;

View File

@ -14,7 +14,7 @@ import io.metersphere.sdk.log.constants.OperationLogType;
import io.metersphere.sdk.util.PageUtils;
import io.metersphere.sdk.util.Pager;
import io.metersphere.sdk.util.SessionUtils;
import io.metersphere.system.dto.UserExtend;
import io.metersphere.sdk.dto.UserExtend;
import io.metersphere.system.request.OrganizationProjectRequest;
import io.metersphere.system.request.ProjectAddMemberBatchRequest;
import io.metersphere.system.request.ProjectAddMemberRequest;

View File

@ -13,7 +13,7 @@ import io.metersphere.sdk.util.Pager;
import io.metersphere.sdk.util.SessionUtils;
import io.metersphere.system.domain.User;
import io.metersphere.system.domain.UserRole;
import io.metersphere.system.dto.UserExtend;
import io.metersphere.sdk.dto.UserExtend;
import io.metersphere.system.request.OrganizationUserRoleEditRequest;
import io.metersphere.system.request.OrganizationUserRoleMemberEditRequest;
import io.metersphere.system.request.OrganizationUserRoleMemberRequest;

View File

@ -10,7 +10,7 @@ import io.metersphere.sdk.util.PageUtils;
import io.metersphere.sdk.util.Pager;
import io.metersphere.sdk.util.SessionUtils;
import io.metersphere.system.dto.OrganizationDTO;
import io.metersphere.system.dto.UserExtend;
import io.metersphere.sdk.dto.UserExtend;
import io.metersphere.system.request.OrganizationMemberRequest;
import io.metersphere.system.request.OrganizationProjectRequest;
import io.metersphere.system.request.OrganizationRequest;

View File

@ -12,7 +12,7 @@ import io.metersphere.sdk.util.PageUtils;
import io.metersphere.sdk.util.Pager;
import io.metersphere.sdk.util.SessionUtils;
import io.metersphere.system.domain.User;
import io.metersphere.system.dto.UserExtend;
import io.metersphere.sdk.dto.UserExtend;
import io.metersphere.system.request.ProjectAddMemberBatchRequest;
import io.metersphere.system.request.ProjectAddMemberRequest;
import io.metersphere.system.request.ProjectMemberRequest;

View File

@ -6,9 +6,9 @@ import io.metersphere.project.domain.Project;
import io.metersphere.project.domain.ProjectExample;
import io.metersphere.project.mapper.ProjectMapper;
import io.metersphere.sdk.util.LogUtils;
import io.metersphere.system.mapper.ExtSystemProjectMapper;
import io.metersphere.system.service.CommonProjectService;
import jakarta.annotation.Resource;
import org.apache.shiro.util.CollectionUtils;
import org.springframework.stereotype.Component;
import java.time.LocalDate;
@ -21,6 +21,8 @@ public class CleanProjectJob {
@Resource
private ProjectMapper projectMapper;
@Resource
private ExtSystemProjectMapper extSystemProjectMapper;
@Resource
private CommonProjectService commonProjectService;
/**
@ -29,26 +31,22 @@ public class CleanProjectJob {
@QuartzScheduled(cron = "0 0 3 * * ?")
public void cleanupProject() {
LogUtils.info("clean up project start.");
try {
LocalDate date = LocalDate.now().minusMonths(1);
long timestamp = date.atStartOfDay(ZoneId.systemDefault()).toInstant().toEpochMilli();
this.doCleanupProject(timestamp);
} catch (Exception e) {
LogUtils.error("clean up project error.", e);
}
LogUtils.info("clean up project end.");
}
private void doCleanupProject(long timestamp) {
//获取所有删除状态的项目总量
ProjectExample example = new ProjectExample();
example.createCriteria().andDeletedEqualTo(true).andDeleteTimeLessThanOrEqualTo(timestamp);
List<Project> projects = projectMapper.selectByExample(example);
if (!CollectionUtils.isEmpty(projects)) {
for (int i = 0; i < projects.size(); i++) {
long count = projectMapper.countByExample(example);
for (int i = 0; i < count; i++) {
//对项目进行分批处理
if (i % 100 == 0) {
commonProjectService.deleteProject(projects.subList(i, Math.min(i + 100, projects.size())));
}
List<Project> deleteProjectIds = extSystemProjectMapper.getDeleteProjectIds(timestamp, i);
commonProjectService.deleteProject(deleteProjectIds);
}
}
}

View File

@ -1,6 +1,7 @@
package io.metersphere.system.mapper;
import io.metersphere.sdk.dto.OptionDTO;
import io.metersphere.sdk.dto.UserExtend;
import io.metersphere.system.domain.User;
import io.metersphere.system.dto.*;
import io.metersphere.system.request.OrganizationDeleteRequest;

View File

@ -34,7 +34,7 @@
update organization set `enable` = #{enable} where id = #{id}
</update>
<select id="listMember" resultType="io.metersphere.system.dto.UserExtend">
<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

View File

@ -1,9 +1,9 @@
package io.metersphere.system.mapper;
import io.metersphere.project.domain.Project;
import io.metersphere.sdk.dto.ProjectDTO;
import io.metersphere.system.domain.User;
import io.metersphere.sdk.dto.UserExtend;
import io.metersphere.system.dto.OrganizationProjectOptionsDTO;
import io.metersphere.system.dto.UserExtend;
import io.metersphere.system.request.ProjectMemberRequest;
import io.metersphere.system.request.ProjectRequest;
import org.apache.ibatis.annotations.Param;
@ -16,11 +16,13 @@ public interface ExtSystemProjectMapper {
List<ProjectDTO> getProjectList(@Param("request") ProjectRequest request);
List<User> getProjectAdminList(String projectId);
List<UserExtend> getProjectAdminList(@Param("projectIds") List<String> projectIds);
List<OrganizationProjectOptionsDTO> selectProjectOptions(@Param("organizationId") String organizationId);
List<UserExtend> getUserAdminList(@Param("organizationId") String organizationId);
List<UserExtend> getUserMemberList(@Param("userIds") List<String> userIds, @Param("projectId") String projectId);
List<Project> getDeleteProjectIds(@Param("time") long timestamp, @Param("offset") long offset);
}

View File

@ -3,7 +3,7 @@
<mapper namespace="io.metersphere.system.mapper.ExtSystemProjectMapper">
<select id="getProjectMemberList" resultType="io.metersphere.system.dto.UserExtend">
<select id="getProjectMemberList" resultType="io.metersphere.sdk.dto.UserExtend">
SELECT temp.* , MAX( if (temp.role_id = 'project_admin', true, false)) as adminFlag
FROM (
SELECT `user`.* , user_role_relation.role_id
@ -45,13 +45,18 @@
<include refid="queryWhereCondition"/>
group by p.id
</select>
<select id="getProjectAdminList" resultType="io.metersphere.system.dto.UserExtend">
SELECT `user`.*
<select id="getProjectAdminList" resultType="io.metersphere.sdk.dto.UserExtend">
SELECT `user`.*, user_role_relation.source_id
FROM user_role_relation
LEFT JOIN `user` ON user_role_relation.user_id = `user`.id
<where>
`user`.deleted = 0
and user_role_relation.source_id = #{projectId}
<if test="projectIds != null and projectIds.size > 0 ">
and user_role_relation.source_id
<foreach collection="projectIds" item="projectId" open=" in (" separator="," close=")">
#{projectId}
</foreach>
</if>
and user_role_relation.role_id = 'project_admin'
</where>
</select>
@ -112,14 +117,14 @@
</if>
</sql>
<select id="getUserAdminList" resultType="io.metersphere.system.dto.UserExtend">
<select id="getUserAdminList" resultType="io.metersphere.sdk.dto.UserExtend">
select distinct u.*
from `user` u left join user_role_relation urr on urr.user_id = u.id
where u.deleted = 0 and urr.source_id = #{organizationId}
</select>
<select id="getUserMemberList" resultType="io.metersphere.system.dto.UserExtend">
<select id="getUserMemberList" resultType="io.metersphere.sdk.dto.UserExtend">
select distinct u.*, count(temp.id) > 0 as memberFlag from
user u left join (select * from user_role_relation urr where urr.source_id = #{projectId}) temp on temp.user_id = u.id
<where>
@ -136,7 +141,9 @@
</select>
<select id="getDeleteProjectIds" resultType="io.metersphere.project.domain.Project">
select * from project where deleted = 1 and delete_time &lt;= #{time} limit #{offset}, 100
</select>
</mapper>

View File

@ -1,7 +1,7 @@
package io.metersphere.system.mapper;
import io.metersphere.system.domain.User;
import io.metersphere.system.dto.UserExtend;
import io.metersphere.sdk.dto.UserExtend;
import org.apache.ibatis.annotations.Param;
import java.util.List;

View File

@ -1,7 +1,7 @@
<?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.ExtUserMapper">
<select id="getMemberOption" resultType="io.metersphere.system.dto.UserExtend">
<select id="getMemberOption" resultType="io.metersphere.sdk.dto.UserExtend">
select distinct u.*, count(urr.id) > 0 as memberFlag
from `user` u left join user_role_relation urr on urr.user_id = u.id and urr.source_id = #{sourceId}
where u.deleted = 0

View File

@ -7,6 +7,7 @@ import io.metersphere.sdk.constants.HttpMethodConstants;
import io.metersphere.sdk.constants.InternalUserRole;
import io.metersphere.sdk.constants.OperationLogConstants;
import io.metersphere.sdk.constants.UserRoleType;
import io.metersphere.sdk.dto.UserExtend;
import io.metersphere.sdk.dto.*;
import io.metersphere.sdk.exception.MSException;
import io.metersphere.sdk.invoker.ProjectServiceInvoker;
@ -28,6 +29,7 @@ import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
@ -183,16 +185,23 @@ public class CommonProjectService {
userIds.addAll(projectList.stream().map(ProjectDTO::getUpdateUser).toList());
userIds.addAll(projectList.stream().map(ProjectDTO::getDeleteUser).toList());
Map<String, String> userMap = baseUserService.getUserNameMap(userIds.stream().distinct().toList());
// 获取项目id
List<String> projectIds = projectList.stream().map(ProjectDTO::getId).toList();
List<UserExtend> users = extSystemProjectMapper.getProjectAdminList(projectIds);
//根据sourceId分组
Map<String, List<UserExtend>> userMapList = users.stream().collect(Collectors.groupingBy(UserExtend::getSourceId));
projectList.forEach(projectDTO -> {
if (StringUtils.isNotBlank(projectDTO.getModuleSetting())) {
projectDTO.setModuleIds(JSON.parseArray(projectDTO.getModuleSetting(), String.class));
}
List<User> users = extSystemProjectMapper.getProjectAdminList(projectDTO.getId());
projectDTO.setAdminList(users);
List<String> userIdList = users.stream().map(User::getId).collect(Collectors.toList());
List<UserExtend> userExtends = userMapList.get(projectDTO.getId());
if (CollectionUtils.isNotEmpty(userExtends)) {
projectDTO.setAdminList(userExtends);
List<String> userIdList = userExtends.stream().map(User::getId).collect(Collectors.toList());
if (CollectionUtils.isNotEmpty(userIdList) && userIdList.contains(projectDTO.getCreateUser())) {
projectDTO.setProjectCreateUserIsAdmin(true);
}
}
projectDTO.setCreateUser(userMap.get(projectDTO.getCreateUser()));
projectDTO.setUpdateUser(userMap.get(projectDTO.getUpdateUser()));
projectDTO.setDeleteUser(userMap.get(projectDTO.getDeleteUser()));
@ -422,6 +431,7 @@ public class CommonProjectService {
*
* @param projects
*/
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void deleteProject(List<Project> projects) {
// 删除项目
List<LogDTO> logDTOList = new ArrayList<>();

View File

@ -11,7 +11,7 @@ import io.metersphere.sdk.util.BeanUtils;
import io.metersphere.sdk.util.Translator;
import io.metersphere.system.domain.UserRoleRelation;
import io.metersphere.system.domain.UserRoleRelationExample;
import io.metersphere.system.dto.UserExtend;
import io.metersphere.sdk.dto.UserExtend;
import io.metersphere.system.mapper.ExtSystemProjectMapper;
import io.metersphere.system.mapper.OrganizationMapper;
import io.metersphere.system.mapper.UserRoleRelationMapper;

View File

@ -16,7 +16,7 @@ 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.system.dto.UserExtend;
import io.metersphere.sdk.dto.UserExtend;
import io.metersphere.system.dto.*;
import io.metersphere.system.mapper.*;
import io.metersphere.system.request.*;

View File

@ -11,7 +11,7 @@ import io.metersphere.sdk.uid.UUID;
import io.metersphere.sdk.util.BeanUtils;
import io.metersphere.sdk.util.Translator;
import io.metersphere.system.domain.*;
import io.metersphere.system.dto.UserExtend;
import io.metersphere.sdk.dto.UserExtend;
import io.metersphere.system.mapper.ExtUserRoleMapper;
import io.metersphere.system.mapper.UserMapper;
import io.metersphere.system.mapper.UserRoleMapper;

View File

@ -9,7 +9,7 @@ import io.metersphere.sdk.log.constants.OperationLogModule;
import io.metersphere.sdk.log.constants.OperationLogType;
import io.metersphere.sdk.util.Translator;
import io.metersphere.system.dto.OrganizationProjectOptionsDTO;
import io.metersphere.system.dto.UserExtend;
import io.metersphere.sdk.dto.UserExtend;
import io.metersphere.system.mapper.ExtSystemProjectMapper;
import io.metersphere.system.request.ProjectAddMemberBatchRequest;
import io.metersphere.system.request.ProjectMemberRequest;

View File

@ -13,7 +13,7 @@ import io.metersphere.sdk.util.*;
import io.metersphere.system.domain.*;
import io.metersphere.system.dto.UserBatchCreateDTO;
import io.metersphere.system.dto.UserCreateInfo;
import io.metersphere.system.dto.UserExtend;
import io.metersphere.sdk.dto.UserExtend;
import io.metersphere.system.dto.excel.UserExcel;
import io.metersphere.system.dto.excel.UserExcelRowDTO;
import io.metersphere.system.dto.request.UserInviteRequest;

View File

@ -16,7 +16,7 @@ import io.metersphere.system.domain.User;
import io.metersphere.system.domain.UserRoleRelation;
import io.metersphere.system.domain.UserRoleRelationExample;
import io.metersphere.system.dto.OrganizationDTO;
import io.metersphere.system.dto.UserExtend;
import io.metersphere.sdk.dto.UserExtend;
import io.metersphere.system.mapper.UserMapper;
import io.metersphere.system.mapper.UserRoleRelationMapper;
import io.metersphere.system.request.OrganizationProjectRequest;

View File

@ -9,7 +9,7 @@ import io.metersphere.sdk.log.constants.OperationLogType;
import io.metersphere.sdk.util.JSON;
import io.metersphere.sdk.util.Pager;
import io.metersphere.system.dto.OrganizationDTO;
import io.metersphere.system.dto.UserExtend;
import io.metersphere.sdk.dto.UserExtend;
import io.metersphere.system.request.OrganizationMemberRequest;
import io.metersphere.system.request.OrganizationRequest;
import io.metersphere.system.request.ProjectRequest;

View File

@ -15,7 +15,6 @@ import io.metersphere.sdk.util.Pager;
import io.metersphere.system.domain.User;
import io.metersphere.system.domain.UserRoleRelation;
import io.metersphere.system.domain.UserRoleRelationExample;
import io.metersphere.system.dto.UserExtend;
import io.metersphere.system.mapper.UserMapper;
import io.metersphere.system.mapper.UserRoleRelationMapper;
import io.metersphere.system.request.ProjectAddMemberRequest;