refactor(缺陷管理): 缺陷管理代码逻辑优化

This commit is contained in:
song-cc-rock 2024-09-02 17:59:27 +08:00 committed by Craftsman
parent 2ad6cc78d0
commit 16b4102a72
13 changed files with 94 additions and 38 deletions

View File

@ -35,6 +35,7 @@ public class BugAttachmentLogService {
* @param file 上传的文件 * @param file 上传的文件
* @return 日志 * @return 日志
*/ */
@SuppressWarnings("unused")
public LogDTO uploadLog(BugUploadFileRequest request, MultipartFile file) { public LogDTO uploadLog(BugUploadFileRequest request, MultipartFile file) {
Bug bug = bugMapper.selectByPrimaryKey(request.getBugId()); Bug bug = bugMapper.selectByPrimaryKey(request.getBugId());
List<BugFileDTO> allBugFiles = bugAttachmentService.getAllBugFiles(request.getBugId()); List<BugFileDTO> allBugFiles = bugAttachmentService.getAllBugFiles(request.getBugId());
@ -52,6 +53,7 @@ public class BugAttachmentLogService {
* @param request 请求参数 * @param request 请求参数
* @return 日志 * @return 日志
*/ */
@SuppressWarnings("unused")
public LogDTO deleteLog(BugDeleteFileRequest request) { public LogDTO deleteLog(BugDeleteFileRequest request) {
Bug bug = bugMapper.selectByPrimaryKey(request.getBugId()); Bug bug = bugMapper.selectByPrimaryKey(request.getBugId());
List<BugFileDTO> allBugFiles = bugAttachmentService.getAllBugFiles(request.getBugId()); List<BugFileDTO> allBugFiles = bugAttachmentService.getAllBugFiles(request.getBugId());

View File

@ -1,5 +1,6 @@
package io.metersphere.bug.service; package io.metersphere.bug.service;
import com.google.common.collect.Maps;
import io.metersphere.bug.domain.Bug; import io.metersphere.bug.domain.Bug;
import io.metersphere.bug.domain.BugLocalAttachment; import io.metersphere.bug.domain.BugLocalAttachment;
import io.metersphere.bug.domain.BugLocalAttachmentExample; import io.metersphere.bug.domain.BugLocalAttachmentExample;
@ -38,11 +39,10 @@ import io.metersphere.sdk.util.FileAssociationSourceUtil;
import io.metersphere.sdk.util.LogUtils; import io.metersphere.sdk.util.LogUtils;
import io.metersphere.sdk.util.MsFileUtils; import io.metersphere.sdk.util.MsFileUtils;
import io.metersphere.sdk.util.Translator; import io.metersphere.sdk.util.Translator;
import io.metersphere.system.dto.sdk.OptionDTO;
import io.metersphere.system.log.constants.OperationLogModule; import io.metersphere.system.log.constants.OperationLogModule;
import io.metersphere.system.mapper.BaseUserMapper;
import io.metersphere.system.service.CommonFileService; import io.metersphere.system.service.CommonFileService;
import io.metersphere.system.service.FileService; import io.metersphere.system.service.FileService;
import io.metersphere.system.service.UserToolService;
import io.metersphere.system.uid.IDGenerator; import io.metersphere.system.uid.IDGenerator;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
import org.apache.commons.io.FileUtils; import org.apache.commons.io.FileUtils;
@ -72,8 +72,6 @@ public class BugAttachmentService {
@Resource @Resource
private FileService fileService; private FileService fileService;
@Resource @Resource
private BaseUserMapper baseUserMapper;
@Resource
private FileMetadataMapper fileMetadataMapper; private FileMetadataMapper fileMetadataMapper;
@Resource @Resource
private BugPlatformService bugPlatformService; private BugPlatformService bugPlatformService;
@ -87,6 +85,8 @@ public class BugAttachmentService {
private BugLocalAttachmentMapper bugLocalAttachmentMapper; private BugLocalAttachmentMapper bugLocalAttachmentMapper;
@Resource @Resource
private CommonFileService commonFileService; private CommonFileService commonFileService;
@Resource
private UserToolService userToolService;
/** /**
* 查询缺陷的附件集合 * 查询缺陷的附件集合
@ -125,8 +125,7 @@ public class BugAttachmentService {
return bugFiles; return bugFiles;
} }
List<String> userIds = bugFiles.stream().map(BugFileDTO::getCreateUser).distinct().toList(); List<String> userIds = bugFiles.stream().map(BugFileDTO::getCreateUser).distinct().toList();
List<OptionDTO> userOptions = baseUserMapper.selectUserOptionByIds(userIds); Map<String, String> userMap = userToolService.getUserMapByIds(userIds);
Map<String, String> userMap = userOptions.stream().collect(Collectors.toMap(OptionDTO::getId, OptionDTO::getName));
return bugFiles.stream().peek(file -> file.setCreateUserName(userMap.get(file.getCreateUser()))).toList(); return bugFiles.stream().peek(file -> file.setCreateUserName(userMap.get(file.getCreateUser()))).toList();
} }
@ -621,7 +620,7 @@ public class BugAttachmentService {
FileRepository defaultRepository = FileCenter.getDefaultRepository(); FileRepository defaultRepository = FileCenter.getDefaultRepository();
String systemTempDir = DefaultRepositoryDir.getSystemTempDir(); String systemTempDir = DefaultRepositoryDir.getSystemTempDir();
// 添加文件与功能用例的关联关系 // 添加文件与功能用例的关联关系
Map<String, String> addFileMap = new HashMap<>(); Map<String, String> addFileMap = Maps.newHashMapWithExpectedSize(8);
LogUtils.info("开始上传富文本里的附件"); LogUtils.info("开始上传富文本里的附件");
List<BugLocalAttachment> localAttachments = fileIds.stream().map(fileId -> { List<BugLocalAttachment> localAttachments = fileIds.stream().map(fileId -> {
BugLocalAttachment localAttachment = new BugLocalAttachment(); BugLocalAttachment localAttachment = new BugLocalAttachment();

View File

@ -79,6 +79,10 @@ public class BugCommentNoticeService {
noticeSendService.send(NoticeConstants.TaskType.BUG_TASK, noticeModel); noticeSendService.send(NoticeConstants.TaskType.BUG_TASK, noticeModel);
} }
/**
* 设置本地语言
* @param language 语言
*/
private static void setLanguage(String language) { private static void setLanguage(String language) {
Locale locale = Locale.SIMPLIFIED_CHINESE; Locale locale = Locale.SIMPLIFIED_CHINESE;
if (StringUtils.containsIgnoreCase(language, "US")) { if (StringUtils.containsIgnoreCase(language, "US")) {

View File

@ -61,6 +61,14 @@ public class BugExportService {
return filesFolder; return filesFolder;
} }
/**
* 生成excel文件
* @param list 列表数据
* @param xlsxFileName excel文件名
* @param excelPath excel文件路径
* @param headerModel excel导出表头
* @throws Exception 异常信息
*/
private void generateExcelFile(List<BugDTO> list, String xlsxFileName, String excelPath, BugExportHeaderModel headerModel) throws Exception { private void generateExcelFile(List<BugDTO> list, String xlsxFileName, String excelPath, BugExportHeaderModel headerModel) throws Exception {
if (CollectionUtils.isNotEmpty(list)) { if (CollectionUtils.isNotEmpty(list)) {
// 准备数据 {评论, 内容, 关联用例数} // 准备数据 {评论, 内容, 关联用例数}

View File

@ -4,8 +4,6 @@ import io.metersphere.sdk.util.BeanUtils;
import io.metersphere.sdk.util.Translator; import io.metersphere.sdk.util.Translator;
import io.metersphere.system.domain.OperationHistory; import io.metersphere.system.domain.OperationHistory;
import io.metersphere.system.domain.OperationHistoryExample; import io.metersphere.system.domain.OperationHistoryExample;
import io.metersphere.system.domain.User;
import io.metersphere.system.domain.UserExample;
import io.metersphere.system.dto.OperationHistoryDTO; import io.metersphere.system.dto.OperationHistoryDTO;
import io.metersphere.system.dto.request.OperationHistoryRequest; import io.metersphere.system.dto.request.OperationHistoryRequest;
import io.metersphere.system.log.constants.OperationLogModule; import io.metersphere.system.log.constants.OperationLogModule;
@ -13,6 +11,7 @@ import io.metersphere.system.log.constants.OperationLogType;
import io.metersphere.system.mapper.BaseOperationHistoryMapper; import io.metersphere.system.mapper.BaseOperationHistoryMapper;
import io.metersphere.system.mapper.OperationHistoryMapper; import io.metersphere.system.mapper.OperationHistoryMapper;
import io.metersphere.system.mapper.UserMapper; import io.metersphere.system.mapper.UserMapper;
import io.metersphere.system.service.UserToolService;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
@ -21,7 +20,6 @@ import org.springframework.transaction.annotation.Transactional;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.stream.Collectors;
@Service @Service
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
@ -29,7 +27,8 @@ public class BugHistoryService {
@Resource @Resource
private UserMapper userMapper; private UserMapper userMapper;
@Resource
private UserToolService userToolService;
@Resource @Resource
private OperationHistoryMapper operationHistoryMapper; private OperationHistoryMapper operationHistoryMapper;
@Resource @Resource
@ -49,10 +48,7 @@ public class BugHistoryService {
return List.of(); return List.of();
} }
List<String> userIds = history.stream().map(OperationHistory::getCreateUser).toList(); List<String> userIds = history.stream().map(OperationHistory::getCreateUser).toList();
UserExample userExample = new UserExample(); Map<String, String> userMap = userToolService.getUserMapByIds(userIds);
userExample.createCriteria().andIdIn(userIds);
List<User> users = userMapper.selectByExample(userExample);
Map<String, String> userMap = users.stream().collect(Collectors.toMap(User::getId, User::getName));
Long latestVersionId = baseOperationHistoryMapper.selectLatestIdByOperationId(request.getSourceId()); Long latestVersionId = baseOperationHistoryMapper.selectLatestIdByOperationId(request.getSourceId());
return history.stream().map(h -> { return history.stream().map(h -> {
OperationHistoryDTO dto = new OperationHistoryDTO(); OperationHistoryDTO dto = new OperationHistoryDTO();

View File

@ -28,6 +28,7 @@ public class BugRelateCaseLogService {
* @param id 取消关联的引用ID * @param id 取消关联的引用ID
* @return 日志 * @return 日志
*/ */
@SuppressWarnings("unused")
public LogDTO getRelateLog(String id) { public LogDTO getRelateLog(String id) {
BugRelationCase bugRelationCase = bugRelationCaseMapper.selectByPrimaryKey(id); BugRelationCase bugRelationCase = bugRelationCaseMapper.selectByPrimaryKey(id);
BugRelateCaseDTO relateCase = extBugRelateCaseMapper.getRelateCase(bugRelationCase.getCaseId(), bugRelationCase.getCaseType()); BugRelateCaseDTO relateCase = extBugRelateCaseMapper.getRelateCase(bugRelationCase.getCaseId(), bugRelationCase.getCaseType());

View File

@ -24,6 +24,12 @@ public class BugScheduleServiceImpl implements BaseBugScheduleService {
@Resource @Resource
private ScheduleService scheduleService; private ScheduleService scheduleService;
/**
* 更新缺陷同步定时任务配置
* @param bugSyncConfigs 配置
* @param projectId 项目ID
* @param currentUser 当前用户
*/
@Override @Override
public void updateBugSyncScheduleConfig(List<ProjectApplication> bugSyncConfigs, String projectId, String currentUser) { public void updateBugSyncScheduleConfig(List<ProjectApplication> bugSyncConfigs, String projectId, String currentUser) {
List<ProjectApplication> syncCron = bugSyncConfigs.stream().filter(config -> config.getType().equals(ProjectApplicationType.BUG.BUG_SYNC.name() + "_" + ProjectApplicationType.BUG_SYNC_CONFIG.CRON_EXPRESSION.name())).toList(); List<ProjectApplication> syncCron = bugSyncConfigs.stream().filter(config -> config.getType().equals(ProjectApplicationType.BUG.BUG_SYNC.name() + "_" + ProjectApplicationType.BUG_SYNC_CONFIG.CRON_EXPRESSION.name())).toList();
@ -56,6 +62,12 @@ public class BugScheduleServiceImpl implements BaseBugScheduleService {
} }
} }
/**
* 启用或禁用缺陷同步定时任务
* @param projectId 项目ID
* @param currentUser 当前用户
* @param enable 开启或禁用
*/
@Override @Override
public void enableOrNotBugSyncSchedule(String projectId, String currentUser, Boolean enable) { public void enableOrNotBugSyncSchedule(String projectId, String currentUser, Boolean enable) {
Schedule schedule = scheduleService.getScheduleByResource(projectId, BugSyncJob.class.getName()); Schedule schedule = scheduleService.getScheduleByResource(projectId, BugSyncJob.class.getName());

View File

@ -2084,30 +2084,30 @@ public class BugService {
List<PlatformCustomFieldItemDTO> platformCustomFields = platformBug.getCustomFieldList(); List<PlatformCustomFieldItemDTO> platformCustomFields = platformBug.getCustomFieldList();
// 过滤出需要同步的自定义字段{默认模板时, 需要同步所有字段; 非默认模板时, 需要同步模板中映射的字段} // 过滤出需要同步的自定义字段{默认模板时, 需要同步所有字段; 非默认模板时, 需要同步模板中映射的字段}
final Map<String, String> needSyncApiFieldFilterMap = needSyncApiFieldMap; final Map<String, String> needSyncApiFieldFilterMap = needSyncApiFieldMap;
if (platformBug.getPlatformDefaultTemplate()) { List<BugCustomFieldDTO> bugCustomFieldDTOList;
if (platformBug.getPlatformDefaultTemplate()) {
// 平台默认模板创建的缺陷 // 平台默认模板创建的缺陷
List<BugCustomFieldDTO> bugCustomFieldDTOList = platformCustomFields.stream() bugCustomFieldDTOList = platformCustomFields.stream()
.map(platformField -> { .map(platformField -> {
BugCustomFieldDTO bugCustomFieldDTO = new BugCustomFieldDTO(); BugCustomFieldDTO bugCustomFieldDTO = new BugCustomFieldDTO();
bugCustomFieldDTO.setId(platformField.getId()); bugCustomFieldDTO.setId(platformField.getId());
bugCustomFieldDTO.setValue(platformField.getValue() == null ? null : platformField.getValue().toString()); bugCustomFieldDTO.setValue(platformField.getValue() == null ? null : platformField.getValue().toString());
return bugCustomFieldDTO; return bugCustomFieldDTO;
}).collect(Collectors.toList()); }).collect(Collectors.toList());
customEditRequest.setCustomFields(bugCustomFieldDTOList); } else {
} else {
// 非平台默认模板创建的缺陷(使用模板API映射字段) // 非平台默认模板创建的缺陷(使用模板API映射字段)
List<BugCustomFieldDTO> bugCustomFieldDTOList = platformCustomFields.stream() bugCustomFieldDTOList = platformCustomFields.stream()
.filter(field -> needSyncApiFieldFilterMap.containsKey(field.getId())) .filter(field -> needSyncApiFieldFilterMap.containsKey(field.getId()))
.map(platformField -> { .map(platformField -> {
BugCustomFieldDTO bugCustomFieldDTO = new BugCustomFieldDTO(); BugCustomFieldDTO bugCustomFieldDTO = new BugCustomFieldDTO();
bugCustomFieldDTO.setId(needSyncApiFieldFilterMap.get(platformField.getId())); bugCustomFieldDTO.setId(needSyncApiFieldFilterMap.get(platformField.getId()));
bugCustomFieldDTO.setValue(platformField.getValue() == null ? null : platformField.getValue().toString()); bugCustomFieldDTO.setValue(platformField.getValue() == null ? null : platformField.getValue().toString());
return bugCustomFieldDTO; return bugCustomFieldDTO;
}).collect(Collectors.toList()); }).collect(Collectors.toList());
customEditRequest.setCustomFields(bugCustomFieldDTOList); }
} customEditRequest.setCustomFields(bugCustomFieldDTOList);
// 保存缺陷 // 保存缺陷
if (originalBug == null) { if (originalBug == null) {
// 新增 // 新增
batchBugMapper.insertSelective(bug); batchBugMapper.insertSelective(bug);

View File

@ -107,6 +107,11 @@ public class BugStatusService {
return baseStatusFlowSettingService.getAllStatusOption(projectId, TemplateScene.BUG.name()); return baseStatusFlowSettingService.getAllStatusOption(projectId, TemplateScene.BUG.name());
} }
/**
* 获取当前项目最新的Jira平台缺陷Key (表头状态筛选需要)
* @param projectId 项目ID
* @return JiraKey
*/
public String getJiraPlatformBugKeyLatest(String projectId) { public String getJiraPlatformBugKeyLatest(String projectId) {
BugExample example = new BugExample(); BugExample example = new BugExample();
example.createCriteria().andPlatformEqualTo(BugPlatform.JIRA.name()).andProjectIdEqualTo(projectId); example.createCriteria().andPlatformEqualTo(BugPlatform.JIRA.name()).andProjectIdEqualTo(projectId);

View File

@ -87,6 +87,10 @@ public class BugSyncNoticeService {
inSiteNoticeSender.sendAnnouncement(messageDetail, noticeModel, MessageTemplateUtils.getContent(context, paramMap), subject); inSiteNoticeSender.sendAnnouncement(messageDetail, noticeModel, MessageTemplateUtils.getContent(context, paramMap), subject);
} }
/**
* 设置本地语言
* @param language 语言
*/
private static void setLanguage(String language) { private static void setLanguage(String language) {
Locale locale = Locale.SIMPLIFIED_CHINESE; Locale locale = Locale.SIMPLIFIED_CHINESE;
if (StringUtils.containsIgnoreCase(language, "US")) { if (StringUtils.containsIgnoreCase(language, "US")) {

View File

@ -23,6 +23,10 @@ public class CleanupBugResourceServiceImpl implements CleanupProjectResourceServ
@Resource @Resource
private BugCommonService bugCommonService; private BugCommonService bugCommonService;
/**
* 清理当前项目相关缺陷资源
* @param projectId 项目ID
*/
@Async @Async
@Override @Override
public void deleteResources(String projectId) { public void deleteResources(String projectId) {
@ -36,6 +40,11 @@ public class CleanupBugResourceServiceImpl implements CleanupProjectResourceServ
} }
} }
/**
* 获取当前项目下所有缺陷ID集合
* @param projectId 项目ID
* @return 缺陷ID集合
*/
private List<String> getBugIds(String projectId) { private List<String> getBugIds(String projectId) {
BugExample example = new BugExample(); BugExample example = new BugExample();
example.createCriteria().andProjectIdEqualTo(projectId); example.createCriteria().andProjectIdEqualTo(projectId);
@ -43,6 +52,10 @@ public class CleanupBugResourceServiceImpl implements CleanupProjectResourceServ
return bugs.stream().map(Bug::getId).toList(); return bugs.stream().map(Bug::getId).toList();
} }
/**
* 清理缺陷
* @param bugIds 缺陷ID集合
*/
private void deleteBug(List<String> bugIds) { private void deleteBug(List<String> bugIds) {
BugExample example = new BugExample(); BugExample example = new BugExample();
example.createCriteria().andIdIn(bugIds); example.createCriteria().andIdIn(bugIds);

View File

@ -11,8 +11,8 @@ import java.util.function.BiFunction;
public class ExportUtils { public class ExportUtils {
private List<BugDTO> bugs; private final List<BugDTO> bugs;
private BugExportHeaderModel headerModel; private final BugExportHeaderModel headerModel;
public ExportUtils( public ExportUtils(
List<BugDTO> bugs, List<BugDTO> bugs,

View File

@ -2,6 +2,7 @@ package io.metersphere.system.service;
import io.metersphere.system.domain.User; import io.metersphere.system.domain.User;
import io.metersphere.system.domain.UserExample; import io.metersphere.system.domain.UserExample;
import io.metersphere.system.dto.sdk.OptionDTO;
import io.metersphere.system.dto.table.TableBatchProcessDTO; import io.metersphere.system.dto.table.TableBatchProcessDTO;
import io.metersphere.system.mapper.BaseUserMapper; import io.metersphere.system.mapper.BaseUserMapper;
import io.metersphere.system.mapper.UserMapper; import io.metersphere.system.mapper.UserMapper;
@ -11,6 +12,7 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.stream.Collectors; import java.util.stream.Collectors;
/** /**
@ -42,4 +44,14 @@ public class UserToolService {
return request.getSelectIds(); return request.getSelectIds();
} }
} }
/**
* 获取用户Map集合 (复用)
* @param userIds 用户ID集合
* @return 用户 <ID, NAME> 映射集合
*/
public Map<String, String> getUserMapByIds(List<String> userIds) {
List<OptionDTO> userOptions = baseUserMapper.selectUserOptionByIds(userIds);
return userOptions.stream().collect(Collectors.toMap(OptionDTO::getId, OptionDTO::getName));
}
} }