refactor: 发送通知查询手机邮箱优化

This commit is contained in:
Captain.B 2021-08-26 15:07:44 +08:00 committed by 刘瑞斌
parent 31baaaf34e
commit 93f0502c06
7 changed files with 37 additions and 28 deletions

View File

@ -57,7 +57,7 @@
</select>
<select id="queryTypeByIds" parameterType="java.lang.String" resultType="io.metersphere.notice.domain.UserDetail">
SELECT
email,phone,name
id, email, phone, name
from user
WHERE id IN
<foreach collection="list" item="id" index="index"

View File

@ -4,6 +4,7 @@ import lombok.Data;
@Data
public class UserDetail {
private String id;
private String name;
private String email;
private String phone;

View File

@ -110,21 +110,7 @@ public abstract class AbstractNoticeSender implements NoticeSender {
return template;
}
protected List<String> getUserPhones(NoticeModel noticeModel, List<String> userIds) {
List<UserDetail> list = userService.queryTypeByIds(userIds);
List<String> phoneList = new ArrayList<>();
list.forEach(u -> phoneList.add(u.getPhone()));
return phoneList.stream().distinct().collect(Collectors.toList());
}
protected List<String> getUserEmails(NoticeModel noticeModel, List<String> userIds) {
List<UserDetail> list = userService.queryTypeByIds(userIds);
List<String> phoneList = new ArrayList<>();
list.forEach(u -> phoneList.add(u.getEmail()));
return phoneList.stream().distinct().collect(Collectors.toList());
}
protected List<UserDetail> getUserDetails(NoticeModel noticeModel, List<String> userIds) {
protected List<UserDetail> getUserDetails(List<String> userIds) {
return userService.queryTypeByIds(userIds);
}

View File

@ -7,6 +7,7 @@ import com.taobao.api.ApiException;
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.notice.sender.AbstractNoticeSender;
import io.metersphere.notice.sender.NoticeModel;
import org.apache.commons.collections4.CollectionUtils;
@ -30,14 +31,21 @@ public class DingNoticeSender extends AbstractNoticeSender {
text.setContent(context);
request.setText(text);
OapiRobotSendRequest.At at = new OapiRobotSendRequest.At();
List<String> phoneList = super.getUserPhones(noticeModel, receivers.stream()
List<String> userIds = receivers.stream()
.map(Receiver::getUserId)
.distinct()
.collect(Collectors.toList()));
.collect(Collectors.toList());
List<String> phoneList = super.getUserDetails(userIds).stream()
.map(UserDetail::getPhone)
.distinct()
.collect(Collectors.toList());
if (CollectionUtils.isEmpty(phoneList)) {
return;
}
LogUtil.info("钉钉收件人地址: {}", phoneList);
LogUtil.info("钉钉收件人地址: {}", userIds);
at.setAtMobiles(phoneList);
request.setAt(at);
try {

View File

@ -23,7 +23,7 @@ public class LarkNoticeSender extends AbstractNoticeSender {
.map(Receiver::getUserId)
.distinct()
.collect(Collectors.toList());
List<UserDetail> userDetails = super.getUserDetails(noticeModel, userIds);
List<UserDetail> userDetails = super.getUserDetails(userIds);
List<String> collect = userDetails.stream()
.map(ud -> "<at email=\"" + ud.getEmail() + "\">" + ud.getName() + "</at>")
.collect(Collectors.toList());

View File

@ -3,6 +3,7 @@ package io.metersphere.notice.sender.impl;
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.notice.sender.AbstractNoticeSender;
import io.metersphere.notice.sender.NoticeModel;
import io.metersphere.notice.service.MailService;
@ -45,9 +46,13 @@ public class MailNoticeSender extends AbstractNoticeSender {
if (CollectionUtils.isEmpty(userIds)) {
return;
}
List<String> emails = super.getUserEmails(noticeModel, userIds);
String[] users = emails.toArray(new String[0]);
LogUtil.info("收件人地址: {}", emails);
String[] users = super.getUserDetails(userIds).stream()
.map(UserDetail::getEmail)
.distinct()
.toArray(String[]::new);
LogUtil.info("收件人地址: {}", userIds);
helper.setText(context, true);
helper.setTo(users);
javaMailSender.send(mimeMessage);

View File

@ -3,6 +3,7 @@ package io.metersphere.notice.sender.impl;
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.notice.message.TextMessage;
import io.metersphere.notice.sender.AbstractNoticeSender;
import io.metersphere.notice.sender.NoticeModel;
@ -24,15 +25,23 @@ public class WeComNoticeSender extends AbstractNoticeSender {
return;
}
TextMessage message = new TextMessage(context);
List<String> phoneLists = super.getUserPhones(noticeModel, receivers.stream()
List<String> userIds = receivers.stream()
.map(Receiver::getUserId)
.distinct()
.collect(Collectors.toList()));
message.setMentionedMobileList(phoneLists);
if (CollectionUtils.isEmpty(phoneLists)) {
.collect(Collectors.toList());
List<String> phoneList = super.getUserDetails(userIds).stream()
.map(UserDetail::getPhone)
.distinct()
.collect(Collectors.toList());
message.setMentionedMobileList(phoneList);
if (CollectionUtils.isEmpty(phoneList)) {
return;
}
LogUtil.info("企业微信收件人: {}", phoneLists);
LogUtil.info("企业微信收件人: {}", userIds);
try {
WxChatbotClient.send(messageDetail.getWebhook(), message);
} catch (IOException e) {