From e73b56eda97aaa42200d71b2c0496a9ea59309ee Mon Sep 17 00:00:00 2001 From: CaptainB Date: Tue, 11 Oct 2022 15:32:24 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E5=8F=91=E9=80=9A=E7=9F=A5?= =?UTF-8?q?=E6=97=B6=E6=9F=A5=E8=AF=A2=E5=85=B3=E6=B3=A8=E4=BA=BA=E5=88=97?= =?UTF-8?q?=E8=A1=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../commons/utils/HttpHeaderUtils.java | 12 +- .../notice/sender/AbstractNoticeSender.java | 126 +++++++++++------- 2 files changed, 81 insertions(+), 57 deletions(-) diff --git a/framework/sdk-parent/sdk/src/main/java/io/metersphere/commons/utils/HttpHeaderUtils.java b/framework/sdk-parent/sdk/src/main/java/io/metersphere/commons/utils/HttpHeaderUtils.java index e835dc7e23..53f454373a 100644 --- a/framework/sdk-parent/sdk/src/main/java/io/metersphere/commons/utils/HttpHeaderUtils.java +++ b/framework/sdk-parent/sdk/src/main/java/io/metersphere/commons/utils/HttpHeaderUtils.java @@ -1,11 +1,10 @@ package io.metersphere.commons.utils; +import io.metersphere.base.domain.User; import io.metersphere.base.domain.UserKey; import io.metersphere.commons.constants.ApiKeyConstants; import io.metersphere.commons.constants.SessionConstants; import io.metersphere.commons.exception.MSException; -import io.metersphere.commons.user.SessionUser; -import io.metersphere.dto.UserDTO; import io.metersphere.service.UserKeyService; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.StringUtils; @@ -14,14 +13,13 @@ import org.springframework.http.MediaType; import java.util.List; import java.util.Optional; -import java.util.UUID; /** * 服务之间调用,需要添加HttpHeader,获取的时候注意当前线程的位置 */ public class HttpHeaderUtils { - private static final ThreadLocal sessionUserThreadLocal = new ThreadLocal<>(); + private static final ThreadLocal sessionUserThreadLocal = new ThreadLocal<>(); public static HttpHeaders getHttpHeaders() { HttpHeaders headers = new HttpHeaders(); @@ -55,7 +53,7 @@ public class HttpHeaderUtils { } - UserDTO user = sessionUserThreadLocal.get(); + User user = sessionUserThreadLocal.get(); if (user != null) { UserKey userKey = getUserKey(user); accessKey = userKey.getAccessKey(); @@ -68,7 +66,7 @@ public class HttpHeaderUtils { return headers; } - private static UserKey getUserKey(UserDTO user) { + private static UserKey getUserKey(User user) { UserKeyService userKeyService = CommonBeanFactory.getBean(UserKeyService.class); List userKeys = userKeyService.getUserKeysInfo(user.getId()); UserKey userKey; @@ -84,7 +82,7 @@ public class HttpHeaderUtils { return userKey; } - public static void runAsUser(UserDTO user) { + public static void runAsUser(User user) { if (user != null) { if (StringUtils.isBlank(user.getId())) { throw new IllegalArgumentException("User ID can't be null or empty."); diff --git a/framework/sdk-parent/sdk/src/main/java/io/metersphere/notice/sender/AbstractNoticeSender.java b/framework/sdk-parent/sdk/src/main/java/io/metersphere/notice/sender/AbstractNoticeSender.java index 650c455530..357dbd728e 100644 --- a/framework/sdk-parent/sdk/src/main/java/io/metersphere/notice/sender/AbstractNoticeSender.java +++ b/framework/sdk-parent/sdk/src/main/java/io/metersphere/notice/sender/AbstractNoticeSender.java @@ -1,13 +1,19 @@ package io.metersphere.notice.sender; +import io.metersphere.base.domain.TestCaseReview; +import io.metersphere.base.domain.User; +import io.metersphere.base.mapper.UserMapper; import io.metersphere.base.mapper.ext.BaseUserMapper; +import io.metersphere.commons.constants.MicroServiceName; import io.metersphere.commons.constants.NoticeConstants; import io.metersphere.commons.constants.NotificationConstants; +import io.metersphere.commons.utils.HttpHeaderUtils; import io.metersphere.commons.utils.JSON; import io.metersphere.commons.utils.LogUtil; import io.metersphere.notice.domain.MessageDetail; import io.metersphere.notice.domain.Receiver; import io.metersphere.notice.domain.UserDetail; +import io.metersphere.service.MicroService; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.time.DateFormatUtils; @@ -23,6 +29,10 @@ import java.util.stream.Collectors; public abstract class AbstractNoticeSender implements NoticeSender { @Resource private BaseUserMapper baseUserMapper; + @Resource + private MicroService microService; + @Resource + private UserMapper userMapper; protected String getContext(MessageDetail messageDetail, NoticeModel noticeModel) { // 如果有自定义字段 @@ -151,7 +161,12 @@ public abstract class AbstractNoticeSender implements NoticeSender { } break; case NoticeConstants.RelatedUser.FOLLOW_PEOPLE: - toUsers.addAll(handleFollows(messageDetail, noticeModel)); + try { + List follows = handleFollows(messageDetail, noticeModel); + toUsers.addAll(follows); + } catch (Exception e) { + LogUtil.error("查询关注人失败: ", e); + } break; case NoticeConstants.RelatedUser.PROCESSOR: Object value = paramMap.get("processor"); // 处理人 @@ -172,55 +187,66 @@ public abstract class AbstractNoticeSender implements NoticeSender { private List handleFollows(MessageDetail messageDetail, NoticeModel noticeModel) { List receivers = new ArrayList<>(); - // todo -// String id = (String) noticeModel.getParamMap().get("id"); -// String taskType = messageDetail.getTaskType(); -// switch (taskType) { -// case NoticeConstants.TaskType.TEST_PLAN_TASK: -// receivers = testPlanService.getPlanFollow(id) -// .stream() -// .map(user -> new Receiver(user.getId(), NotificationConstants.Type.SYSTEM_NOTICE.name())) -// .collect(Collectors.toList()); -// break; -// case NoticeConstants.TaskType.REVIEW_TASK: -// TestCaseReview request = new TestCaseReview(); -// request.setId(id); -// receivers = testCaseReviewService.getFollowByReviewId(request).stream() -// .map(user -> new Receiver(user.getId(), NotificationConstants.Type.SYSTEM_NOTICE.name())) -// .collect(Collectors.toList()); -// break; -// case NoticeConstants.TaskType.API_AUTOMATION_TASK: -// receivers = apiAutomationService.getFollows(id) -// .stream() -// .map(userId -> new Receiver(userId, NotificationConstants.Type.SYSTEM_NOTICE.name())) -// .collect(Collectors.toList()); -// break; -// case NoticeConstants.TaskType.API_DEFINITION_TASK: -// receivers = apiDefinitionService.getFollows(id) -// .stream() -// .map(userId -> new Receiver(userId, NotificationConstants.Type.SYSTEM_NOTICE.name())) -// .collect(Collectors.toList()); -// List caseFollows = apiTestCaseService.getFollows(id) -// .stream() -// .map(userId -> new Receiver(userId, NotificationConstants.Type.SYSTEM_NOTICE.name())) -// .collect(Collectors.toList()); -// receivers.addAll(caseFollows); -// break; -// case NoticeConstants.TaskType.PERFORMANCE_TEST_TASK: -// receivers = performanceTestService.getFollows(id) -// .stream() -// .map(userId -> new Receiver(userId, NotificationConstants.Type.SYSTEM_NOTICE.name())) -// .collect(Collectors.toList()); -// break; -// case NoticeConstants.TaskType.TRACK_TEST_CASE_TASK: -// receivers = testCaseService.getFollows(id) -// .stream() -// .map(userId -> new Receiver(userId, NotificationConstants.Type.SYSTEM_NOTICE.name())) -// .collect(Collectors.toList()); -// break; -// default: -// break; -// } + List follows; + List followUsers; + String id = (String) noticeModel.getParamMap().get("id"); + String taskType = messageDetail.getTaskType(); + String operator = noticeModel.getOperator(); + HttpHeaderUtils.runAsUser(userMapper.selectByPrimaryKey(operator)); + switch (taskType) { + case NoticeConstants.TaskType.TEST_PLAN_TASK: + followUsers = microService.getForDataArray(MicroServiceName.TEST_TRACK, "/test/plan/follow/" + id, User.class); + receivers = followUsers + .stream() + .map(user -> new Receiver(user.getId(), NotificationConstants.Type.SYSTEM_NOTICE.name())) + .collect(Collectors.toList()); + break; + case NoticeConstants.TaskType.REVIEW_TASK: + TestCaseReview request = new TestCaseReview(); + request.setId(id); + followUsers = microService.postForDataArray(MicroServiceName.TEST_TRACK, "/test/case/review/follow", request, User.class); + receivers = followUsers + .stream() + .map(user -> new Receiver(user.getId(), NotificationConstants.Type.SYSTEM_NOTICE.name())) + .collect(Collectors.toList()); + break; + case NoticeConstants.TaskType.API_AUTOMATION_TASK: + follows = microService.getForDataArray(MicroServiceName.API_TEST, "/api/automation/follow/" + id, String.class); + receivers = follows + .stream() + .map(userId -> new Receiver(userId, NotificationConstants.Type.SYSTEM_NOTICE.name())) + .collect(Collectors.toList()); + break; + case NoticeConstants.TaskType.API_DEFINITION_TASK: + follows = microService.getForDataArray(MicroServiceName.API_TEST, "/api/definition/follow/" + id, String.class); + receivers = follows + .stream() + .map(userId -> new Receiver(userId, NotificationConstants.Type.SYSTEM_NOTICE.name())) + .collect(Collectors.toList()); + follows = microService.getForDataArray(MicroServiceName.API_TEST, "/api/testcase/follow/" + id, String.class); + List caseFollows = follows + .stream() + .map(userId -> new Receiver(userId, NotificationConstants.Type.SYSTEM_NOTICE.name())) + .collect(Collectors.toList()); + receivers.addAll(caseFollows); + break; + case NoticeConstants.TaskType.PERFORMANCE_TEST_TASK: + follows = microService.getForDataArray(MicroServiceName.PERFORMANCE_TEST, "/performance/test/follow/" + id, String.class); + receivers = follows + .stream() + .map(userId -> new Receiver(userId, NotificationConstants.Type.SYSTEM_NOTICE.name())) + .collect(Collectors.toList()); + break; + case NoticeConstants.TaskType.TRACK_TEST_CASE_TASK: + follows = microService.getForDataArray(MicroServiceName.TEST_TRACK, "/test/case/follow/" + id, String.class); + receivers = follows + .stream() + .map(userId -> new Receiver(userId, NotificationConstants.Type.SYSTEM_NOTICE.name())) + .collect(Collectors.toList()); + break; + default: + break; + } LogUtil.info("FOLLOW_PEOPLE: {}", receivers); return receivers; }