From 748eef36b5079fbe53625c46a9fc95847fb90a67 Mon Sep 17 00:00:00 2001 From: wxg0103 <727495428@qq.com> Date: Tue, 25 Jun 2024 15:49:03 +0800 Subject: [PATCH] =?UTF-8?q?refactor(=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95):?= =?UTF-8?q?=20=E4=BC=98=E5=8C=96=E8=8E=B7=E5=8F=96=E6=9C=AC=E5=9C=B0?= =?UTF-8?q?=E6=97=B6=E5=8C=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/service/ApiReportShareService.java | 2 +- .../api/service/ApiShareService.java | 2 +- .../service/TestPlanReportShareService.java | 301 +++++++++--------- 3 files changed, 156 insertions(+), 149 deletions(-) diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiReportShareService.java b/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiReportShareService.java index 2e179d0213..84e686d6ca 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiReportShareService.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiReportShareService.java @@ -107,7 +107,7 @@ public class ApiReportShareService { public ShareInfoDTO gen(ApiReportShareRequest shareRequest, String userId) { UserDTO userDTO = baseUserMapper.selectById(userId); - String lang = userDTO.getLanguage() == null ? LocaleContextHolder.getLocale().toString() : userDTO.getLanguage(); + String lang = userDTO.getLanguage() == null ? LocaleContextHolder.getLocale().toString().split("_#")[0] : userDTO.getLanguage(); ShareInfo request = new ShareInfo(); BeanUtils.copyBean(request, shareRequest); request.setLang(lang); diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiShareService.java b/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiShareService.java index adc3b5b7a9..7bb788a1b5 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiShareService.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiShareService.java @@ -47,7 +47,7 @@ public class ApiShareService { public ShareInfoDTO genApiDocShareInfo(ApiDefinitionDocRequest request, SessionUser user) { ShareInfo shareInfoRequest = new ShareInfo(); String customData = genCustomData(request, shareInfoRequest); - String lang = user.getLanguage() == null ? LocaleContextHolder.getLocale().toString() : user.getLanguage(); + String lang = user.getLanguage() == null ? LocaleContextHolder.getLocale().toString().split("_#")[0] : user.getLanguage(); Optional.ofNullable(customData) .ifPresent(data -> { diff --git a/backend/services/test-plan/src/main/java/io/metersphere/plan/service/TestPlanReportShareService.java b/backend/services/test-plan/src/main/java/io/metersphere/plan/service/TestPlanReportShareService.java index 664c277b3a..55eed6de79 100644 --- a/backend/services/test-plan/src/main/java/io/metersphere/plan/service/TestPlanReportShareService.java +++ b/backend/services/test-plan/src/main/java/io/metersphere/plan/service/TestPlanReportShareService.java @@ -34,160 +34,167 @@ import static io.metersphere.sdk.util.ShareUtil.getTimeMills; @Transactional(rollbackFor = Exception.class) public class TestPlanReportShareService { - @Resource - private BaseUserMapper baseUserMapper; - @Resource - private ShareInfoMapper shareInfoMapper; - @Resource - private TestPlanReportMapper testPlanReportMapper; - @Resource - private ProjectApplicationMapper projectApplicationMapper; + @Resource + private BaseUserMapper baseUserMapper; + @Resource + private ShareInfoMapper shareInfoMapper; + @Resource + private TestPlanReportMapper testPlanReportMapper; + @Resource + private ProjectApplicationMapper projectApplicationMapper; - private static final Long DEFAULT = 1000L * 60 * 60 * 24; + private static final Long DEFAULT = 1000L * 60 * 60 * 24; - @Transactional(propagation = Propagation.NOT_SUPPORTED) - public void validateExpired(ShareInfo shareInfo) { - String shareType = shareInfo.getShareType(); - String projectId = ""; - if (StringUtils.equals(shareType, ShareInfoType.TEST_PLAN_SHARE_REPORT.name())) { - TestPlanReport planReport = testPlanReportMapper.selectByPrimaryKey(new String(shareInfo.getCustomData())); - if (planReport != null && BooleanUtils.isFalse(planReport.getDeleted())) { - projectId = planReport.getProjectId(); - } - } - if (StringUtils.isBlank(projectId)) { - throw new MSException(Translator.get("test_plan_report_not_exist")); - } - ProjectApplicationExample example = new ProjectApplicationExample(); - example.createCriteria().andProjectIdEqualTo(projectId).andTypeEqualTo(shareType); - List projectApplications = projectApplicationMapper.selectByExample(example); - if (CollectionUtils.isEmpty(projectApplications)) { - millisCheck(System.currentTimeMillis() - shareInfo.getUpdateTime(), DEFAULT, shareInfo.getId()); - } else { - String expr = projectApplications.getFirst().getTypeValue(); - long timeMills = getTimeMills(shareInfo.getUpdateTime(), expr); - millisCheck(System.currentTimeMillis(), timeMills, shareInfo.getId()); - } - } + @Transactional(propagation = Propagation.NOT_SUPPORTED) + public void validateExpired(ShareInfo shareInfo) { + String shareType = shareInfo.getShareType(); + String projectId = ""; + if (StringUtils.equals(shareType, ShareInfoType.TEST_PLAN_SHARE_REPORT.name())) { + TestPlanReport planReport = testPlanReportMapper.selectByPrimaryKey(new String(shareInfo.getCustomData())); + if (planReport != null && BooleanUtils.isFalse(planReport.getDeleted())) { + projectId = planReport.getProjectId(); + } + } + if (StringUtils.isBlank(projectId)) { + throw new MSException(Translator.get("test_plan_report_not_exist")); + } + ProjectApplicationExample example = new ProjectApplicationExample(); + example.createCriteria().andProjectIdEqualTo(projectId).andTypeEqualTo(shareType); + List projectApplications = projectApplicationMapper.selectByExample(example); + if (CollectionUtils.isEmpty(projectApplications)) { + millisCheck(System.currentTimeMillis() - shareInfo.getUpdateTime(), DEFAULT, shareInfo.getId()); + } else { + String expr = projectApplications.getFirst().getTypeValue(); + long timeMills = getTimeMills(shareInfo.getUpdateTime(), expr); + millisCheck(System.currentTimeMillis(), timeMills, shareInfo.getId()); + } + } - /** - * 生成计划报告分享信息 - * @param shareRequest 分享请求参数 - * @param currentUser 当前用户 - * @return 计划报告分享信息 - */ - public TestPlanShareInfo gen(TestPlanReportShareRequest shareRequest, String currentUser) { - UserDTO userDTO = baseUserMapper.selectById(currentUser); - String lang = userDTO.getLanguage() == null ? LocaleContextHolder.getLocale().toString() : userDTO.getLanguage(); - ShareInfo request = new ShareInfo(); - BeanUtils.copyBean(request, shareRequest); - request.setLang(lang); - request.setCreateUser(currentUser); - request.setCustomData(shareRequest.getReportId().getBytes()); - request.setShareType(ShareInfoType.TEST_PLAN_SHARE_REPORT.name()); - ShareInfo shareInfo = createShareInfo(request); - return conversionShareInfoToDTO(shareInfo); - } + /** + * 生成计划报告分享信息 + * + * @param shareRequest 分享请求参数 + * @param currentUser 当前用户 + * @return 计划报告分享信息 + */ + public TestPlanShareInfo gen(TestPlanReportShareRequest shareRequest, String currentUser) { + UserDTO userDTO = baseUserMapper.selectById(currentUser); + String lang = userDTO.getLanguage() == null ? LocaleContextHolder.getLocale().toString().split("_#")[0] : userDTO.getLanguage(); + ShareInfo request = new ShareInfo(); + BeanUtils.copyBean(request, shareRequest); + request.setLang(lang); + request.setCreateUser(currentUser); + request.setCustomData(shareRequest.getReportId().getBytes()); + request.setShareType(ShareInfoType.TEST_PLAN_SHARE_REPORT.name()); + ShareInfo shareInfo = createShareInfo(request); + return conversionShareInfoToDTO(shareInfo); + } - /** - * 获取分享信息 - * @param id 分享ID - * @return 分享信息 - */ - public TestPlanShareResponse get(String id) { - TestPlanShareResponse dto = new TestPlanShareResponse(); - ShareInfo shareInfo = shareInfoMapper.selectByPrimaryKey(id); - // 无分享记录, 过期直接返回 - if (shareInfo == null) { - dto.setExpired(true); - return dto; - } - BeanUtils.copyBean(dto, shareInfo); - dto.setReportId(new String(shareInfo.getCustomData())); - // 检查报告ID是否存在 - TestPlanReport testPlanReport = testPlanReportMapper.selectByPrimaryKey(dto.getReportId()); - if (testPlanReport == null || testPlanReport.getDeleted()) { - dto.setDeleted(true); - } - // 报告正常, 校验分享时间是否过期 - if (!dto.isDeleted()) { - try { - validateExpired(shareInfo); - } catch (Exception e) { - dto.setExpired(true); - } - } - return dto; - } + /** + * 获取分享信息 + * + * @param id 分享ID + * @return 分享信息 + */ + public TestPlanShareResponse get(String id) { + TestPlanShareResponse dto = new TestPlanShareResponse(); + ShareInfo shareInfo = shareInfoMapper.selectByPrimaryKey(id); + // 无分享记录, 过期直接返回 + if (shareInfo == null) { + dto.setExpired(true); + return dto; + } + BeanUtils.copyBean(dto, shareInfo); + dto.setReportId(new String(shareInfo.getCustomData())); + // 检查报告ID是否存在 + TestPlanReport testPlanReport = testPlanReportMapper.selectByPrimaryKey(dto.getReportId()); + if (testPlanReport == null || testPlanReport.getDeleted()) { + dto.setDeleted(true); + } + // 报告正常, 校验分享时间是否过期 + if (!dto.isDeleted()) { + try { + validateExpired(shareInfo); + } catch (Exception e) { + dto.setExpired(true); + } + } + return dto; + } - /** - * 获取项目计划报告分享有效期 - * @param projectId 项目ID - * @return 有效期 - */ - public String getShareTime(String projectId) { - ProjectApplicationExample example = new ProjectApplicationExample(); - example.createCriteria().andProjectIdEqualTo(projectId).andTypeEqualTo(ShareInfoType.TEST_PLAN_SHARE_REPORT.name()); - List projectApplications = projectApplicationMapper.selectByExample(example); - if (CollectionUtils.isEmpty(projectApplications)) { - return "1D"; - } else { - return projectApplications.getFirst().getTypeValue(); - } - } + /** + * 获取项目计划报告分享有效期 + * + * @param projectId 项目ID + * @return 有效期 + */ + public String getShareTime(String projectId) { + ProjectApplicationExample example = new ProjectApplicationExample(); + example.createCriteria().andProjectIdEqualTo(projectId).andTypeEqualTo(ShareInfoType.TEST_PLAN_SHARE_REPORT.name()); + List projectApplications = projectApplicationMapper.selectByExample(example); + if (CollectionUtils.isEmpty(projectApplications)) { + return "1D"; + } else { + return projectApplications.getFirst().getTypeValue(); + } + } - /** - * 校验分享的资源是否存在 - * @param id 分享ID - * @return 分享资源信息 - */ - public ShareInfo checkResource(String id) { - ShareInfo shareInfo = shareInfoMapper.selectByPrimaryKey(id); - if (shareInfo == null) { - throw new RuntimeException(Translator.get("connection_expired")); - } - return shareInfo; - } + /** + * 校验分享的资源是否存在 + * + * @param id 分享ID + * @return 分享资源信息 + */ + public ShareInfo checkResource(String id) { + ShareInfo shareInfo = shareInfoMapper.selectByPrimaryKey(id); + if (shareInfo == null) { + throw new RuntimeException(Translator.get("connection_expired")); + } + return shareInfo; + } - /** - * 创建分享信息 - * @param shareInfo 分享的信息 - * @return 分享信息 - */ - private ShareInfo createShareInfo(ShareInfo shareInfo) { - long createTime = System.currentTimeMillis(); - shareInfo.setId(IDGenerator.nextStr()); - shareInfo.setCreateTime(createTime); - shareInfo.setUpdateTime(createTime); - shareInfoMapper.insert(shareInfo); - return shareInfo; - } + /** + * 创建分享信息 + * + * @param shareInfo 分享的信息 + * @return 分享信息 + */ + private ShareInfo createShareInfo(ShareInfo shareInfo) { + long createTime = System.currentTimeMillis(); + shareInfo.setId(IDGenerator.nextStr()); + shareInfo.setCreateTime(createTime); + shareInfo.setUpdateTime(createTime); + shareInfoMapper.insert(shareInfo); + return shareInfo; + } - /** - * 设置分享信息并返回 - * @param shareInfo 分享信息 - * @return 计划报告分享信息返回 - */ - private TestPlanShareInfo conversionShareInfoToDTO(ShareInfo shareInfo) { - TestPlanShareInfo testPlanShareInfo = new TestPlanShareInfo(); - if (shareInfo.getCustomData() != null) { - String url = "?shareId=" + shareInfo.getId(); - testPlanShareInfo.setId(shareInfo.getId()); - testPlanShareInfo.setShareUrl(url); - } - return testPlanShareInfo; - } + /** + * 设置分享信息并返回 + * + * @param shareInfo 分享信息 + * @return 计划报告分享信息返回 + */ + private TestPlanShareInfo conversionShareInfoToDTO(ShareInfo shareInfo) { + TestPlanShareInfo testPlanShareInfo = new TestPlanShareInfo(); + if (shareInfo.getCustomData() != null) { + String url = "?shareId=" + shareInfo.getId(); + testPlanShareInfo.setId(shareInfo.getId()); + testPlanShareInfo.setShareUrl(url); + } + return testPlanShareInfo; + } - /** - * 校验时间是否过期 - * @param compareMillis 比较时间 - * @param millis 分享时间 - * @param shareInfoId 分享ID - */ - private void millisCheck(long compareMillis, long millis, String shareInfoId) { - if (compareMillis > millis) { - shareInfoMapper.deleteByPrimaryKey(shareInfoId); - throw new MSException(Translator.get("connection_expired")); - } - } + /** + * 校验时间是否过期 + * + * @param compareMillis 比较时间 + * @param millis 分享时间 + * @param shareInfoId 分享ID + */ + private void millisCheck(long compareMillis, long millis, String shareInfoId) { + if (compareMillis > millis) { + shareInfoMapper.deleteByPrimaryKey(shareInfoId); + throw new MSException(Translator.get("connection_expired")); + } + } }