refactor: 发送邮件代码重构

This commit is contained in:
Captain.B 2020-09-25 10:22:18 +08:00
parent 7c04ac5f92
commit b5ea0a3d50
3 changed files with 84 additions and 77 deletions

View File

@ -23,6 +23,7 @@ import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
@ -106,8 +107,64 @@ public class MailService {
return template;
}
public void sendEndNotice(List<String> userIds, SaveTestCaseReviewRequest reviewRequest) {
Map<String, String> context = getReviewContext(reviewRequest);
public void sendHtml(List<String> userIds, String type, SaveTestCaseReviewRequest reviewRequest, SaveCommentRequest request, TestCaseWithBLOBs testCaseWithBLOBs) {
try {
String endTemplate = IOUtils.toString(this.getClass().getResource("/mail/end.html"), StandardCharsets.UTF_8);
sendHtml(userIds, context, endTemplate);
} catch (Exception e) {
LogUtil.error(e);
}
}
public void sendReviewerNotice(List<String> userIds, SaveTestCaseReviewRequest reviewRequest) {
Map<String, String> context = getReviewContext(reviewRequest);
try {
String reviewerTemplate = IOUtils.toString(this.getClass().getResource("/mail/reviewer.html"), StandardCharsets.UTF_8);
sendHtml(userIds, context, reviewerTemplate);
} catch (Exception e) {
LogUtil.error(e);
}
}
private void sendHtml(List<String> userIds, Map<String, String> context, String endTemplate) throws MessagingException {
JavaMailSenderImpl javaMailSender = getMailSender();
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
helper.setFrom(javaMailSender.getUsername());
helper.setSubject(Translator.get("test_review_task_notice"));
String[] users;
List<String> emails = new ArrayList<>();
try {
emails = userService.queryEmailByIds(userIds);
} catch (Exception e) {
LogUtil.error("Recipient information is empty");
}
users = emails.toArray(new String[0]);
helper.setText(getContent(endTemplate, context), true);
helper.setTo(users);
javaMailSender.send(mimeMessage);
}
public void sendCommentNotice(List<String> userIds, SaveCommentRequest request, TestCaseWithBLOBs testCaseWithBLOBs) {
Map<String, String> context = new HashMap<>();
context.put("maintainer", testCaseWithBLOBs.getMaintainer());
context.put("testCaseName", testCaseWithBLOBs.getName());
context.put("description", request.getDescription());
try {
String commentTemplate = IOUtils.toString(this.getClass().getResource("/mail/comment.html"), StandardCharsets.UTF_8);
sendHtml(userIds, context, commentTemplate);
} catch (Exception e) {
LogUtil.error(e);
}
}
private Map<String, String> getReviewContext(SaveTestCaseReviewRequest reviewRequest) {
Long startTime = reviewRequest.getCreateTime();
Long endTime = reviewRequest.getEndTime();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
@ -121,58 +178,13 @@ public class MailService {
if (!eTime.equals("null")) {
end = sdf.format(new Date(Long.parseLong(eTime)));
}
JavaMailSenderImpl javaMailSender = getMailSender();
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
Map<String, String> context = new HashMap<>();
context.put("creator", reviewRequest.getCreator());
context.put("maintainer", testCaseWithBLOBs.getMaintainer());
context.put("testCaseName", testCaseWithBLOBs.getName());
context.put("reviewName", reviewRequest.getName());
context.put("description", request.getDescription());
context.put("start", start);
context.put("end", end);
try {
String reviewerTemplate = IOUtils.toString(this.getClass().getResource("/mail/reviewer.html"), StandardCharsets.UTF_8);
String commentTemplate = IOUtils.toString(this.getClass().getResource("/mail/comment.html"), StandardCharsets.UTF_8);
String endTemplate = IOUtils.toString(this.getClass().getResource("/mail/end.html"), StandardCharsets.UTF_8);
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
helper.setFrom(javaMailSender.getUsername());
helper.setSubject(Translator.get("test_review_task_notice"));
String[] users;
List<String> emails = new ArrayList<>();
try {
emails = userService.queryEmailByIds(userIds);
} catch (Exception e) {
LogUtil.error("Recipient information is empty");
}
users = emails.toArray(new String[0]);
switch (type) {
case "reviewer":
helper.setText(getContent(reviewerTemplate, context), true);
break;
case "comment":
helper.setText(getContent(commentTemplate, context), true);
break;
case "end":
helper.setText(getContent(endTemplate, context), true);
break;
default:
break;
}
helper.setTo(users);
} catch (Exception e) {
LogUtil.error(e);
}
try {
javaMailSender.send(mimeMessage);
} catch (MailException e) {
LogUtil.error(e);
}
return context;
}
private JavaMailSenderImpl getMailSender() {

View File

@ -1,6 +1,9 @@
package io.metersphere.track.service;
import io.metersphere.base.domain.*;
import io.metersphere.base.domain.TestCaseComment;
import io.metersphere.base.domain.TestCaseCommentExample;
import io.metersphere.base.domain.TestCaseWithBLOBs;
import io.metersphere.base.domain.User;
import io.metersphere.base.mapper.TestCaseCommentMapper;
import io.metersphere.base.mapper.TestCaseMapper;
import io.metersphere.base.mapper.TestCaseReviewMapper;
@ -8,13 +11,10 @@ import io.metersphere.base.mapper.UserMapper;
import io.metersphere.commons.utils.SessionUtils;
import io.metersphere.notice.service.MailService;
import io.metersphere.track.request.testreview.SaveCommentRequest;
import io.metersphere.track.request.testreview.SaveTestCaseReviewRequest;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
@ -45,10 +45,9 @@ public class TestCaseCommentService {
testCaseCommentMapper.insert(testCaseComment);
TestCaseWithBLOBs testCaseWithBLOBs;
testCaseWithBLOBs = testCaseMapper.selectByPrimaryKey(request.getCaseId());
SaveTestCaseReviewRequest caseReviewRequest = new SaveTestCaseReviewRequest();
List<String> userIds = new ArrayList<>();
userIds.add(testCaseWithBLOBs.getMaintainer());
mailService.sendHtml(userIds, "comment", caseReviewRequest, request, testCaseWithBLOBs);
mailService.sendCommentNotice(userIds, request, testCaseWithBLOBs);
}

View File

@ -10,14 +10,20 @@ import io.metersphere.commons.constants.TestPlanStatus;
import io.metersphere.commons.constants.TestPlanTestCaseStatus;
import io.metersphere.commons.exception.MSException;
import io.metersphere.commons.user.SessionUser;
import io.metersphere.commons.utils.LogUtil;
import io.metersphere.commons.utils.MathUtils;
import io.metersphere.commons.utils.ServiceUtils;
import io.metersphere.commons.utils.SessionUtils;
import io.metersphere.controller.request.member.QueryMemberRequest;
import io.metersphere.notice.service.MailService;
import io.metersphere.service.UserService;
import io.metersphere.track.dto.*;
import io.metersphere.track.request.testreview.*;
import io.metersphere.track.dto.TestCaseReviewDTO;
import io.metersphere.track.dto.TestReviewCaseDTO;
import io.metersphere.track.dto.TestReviewDTOWithMetric;
import io.metersphere.track.request.testreview.QueryCaseReviewRequest;
import io.metersphere.track.request.testreview.QueryTestReviewRequest;
import io.metersphere.track.request.testreview.ReviewRelevanceRequest;
import io.metersphere.track.request.testreview.SaveTestCaseReviewRequest;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.ExecutorType;
@ -28,7 +34,6 @@ import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import javax.annotation.Resource;
import java.lang.reflect.InvocationTargetException;
import java.util.*;
import java.util.stream.Collectors;
@ -91,10 +96,7 @@ public class TestCaseReviewService {
reviewRequest.setCreator(SessionUtils.getUser().getId());
reviewRequest.setStatus(TestCaseReviewStatus.Prepare.name());
testCaseReviewMapper.insert(reviewRequest);
SaveCommentRequest request = new SaveCommentRequest();
TestCaseWithBLOBs testCaseWithBLOBs = new TestCaseWithBLOBs();
mailService.sendHtml(userIds, "reviewer", reviewRequest, request, testCaseWithBLOBs);
mailService.sendReviewerNotice(userIds, reviewRequest);
}
public List<TestCaseReviewDTO> listCaseReview(QueryCaseReviewRequest request) {
@ -144,15 +146,13 @@ public class TestCaseReviewService {
return extTestCaseReviewMapper.listByWorkspaceId(currentWorkspaceId);
}
public void editCaseReview(SaveTestCaseReviewRequest testCaseReview) {
editCaseReviewer(testCaseReview);
editCaseReviewProject(testCaseReview);
testCaseReview.setUpdateTime(System.currentTimeMillis());
checkCaseReviewExist(testCaseReview);
testCaseReviewMapper.updateByPrimaryKeySelective(testCaseReview);
SaveCommentRequest request = new SaveCommentRequest();
TestCaseWithBLOBs testCaseWithBLOBs = new TestCaseWithBLOBs();
mailService.sendHtml(testCaseReview.getUserIds(), "reviewer", testCaseReview, request, testCaseWithBLOBs);
public void editCaseReview(SaveTestCaseReviewRequest reviewRequest) {
editCaseReviewer(reviewRequest);
editCaseReviewProject(reviewRequest);
reviewRequest.setUpdateTime(System.currentTimeMillis());
checkCaseReviewExist(reviewRequest);
testCaseReviewMapper.updateByPrimaryKeySelective(reviewRequest);
mailService.sendReviewerNotice(reviewRequest.getUserIds(), reviewRequest);
}
private void editCaseReviewer(SaveTestCaseReviewRequest testCaseReview) {
@ -314,7 +314,7 @@ public class TestCaseReviewService {
testCaseReviewMapper.updateByPrimaryKey(testCaseReview);
}
}
public List<String> getTestCaseReviewerIds(String reviewId) {
TestCaseReviewUsersExample testCaseReviewUsersExample = new TestCaseReviewUsersExample();
testCaseReviewUsersExample.createCriteria().andReviewIdEqualTo(reviewId);
@ -339,20 +339,16 @@ public class TestCaseReviewService {
}
}
testCaseReview.setStatus(TestPlanStatus.Completed.name());
SaveCommentRequest request = new SaveCommentRequest();
TestCaseWithBLOBs testCaseWithBLOBs = new TestCaseWithBLOBs();
SaveTestCaseReviewRequest testCaseReviewRequest = new SaveTestCaseReviewRequest();
TestCaseReview _testCaseReview = testCaseReviewMapper.selectByPrimaryKey(reviewId);
List<String> userIds = new ArrayList<>();
userIds.add(_testCaseReview.getCreator());
try {
BeanUtils.copyProperties(testCaseReviewRequest, _testCaseReview);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (Exception e) {
LogUtil.error(e);
}
mailService.sendHtml(userIds, "end", testCaseReviewRequest, request, testCaseWithBLOBs);
mailService.sendEndNotice(userIds, testCaseReviewRequest);
testCaseReviewMapper.updateByPrimaryKeySelective(testCaseReview);
}