Browse Source

# 邮箱发送接口开发

# 站点新增逻辑修改
zhongwei
haungxing 1 year ago
parent
commit
63b6a6c284
  1. 66
      hzims-service-api/message-api/src/main/java/com/hnac/hzims/message/dto/MailMessageDTO.java
  2. 13
      hzims-service-api/message-api/src/main/java/com/hnac/hzims/message/entity/MessagePushRecordEntity.java
  3. 4
      hzims-service-api/message-api/src/main/java/com/hnac/hzims/message/fegin/IMessageClient.java
  4. 5
      hzims-service-api/message-api/src/main/java/com/hnac/hzims/message/fegin/MessageClientFallback.java
  5. 33
      hzims-service/message/src/main/java/com/hnac/hzims/message/fegin/MessageClient.java
  6. 2
      hzims-service/message/src/main/java/com/hnac/hzims/message/service/IMessagePushRecordService.java
  7. 33
      hzims-service/message/src/main/java/com/hnac/hzims/message/service/impl/MailMessageServiceImpl.java
  8. 17
      hzims-service/message/src/main/java/com/hnac/hzims/message/service/impl/MessagePushRecordServiceImpl.java
  9. 42
      hzims-service/operational/src/main/java/com/hnac/hzims/operational/station/service/impl/StationServiceImpl.java

66
hzims-service-api/message-api/src/main/java/com/hnac/hzims/message/dto/MailMessageDTO.java

@ -0,0 +1,66 @@
package com.hnac.hzims.message.dto;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springblade.core.mp.support.QueryField;
import org.springblade.core.mp.support.SqlCondition;
import org.springframework.web.multipart.MultipartFile;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.util.List;
@Data
@EqualsAndHashCode
@ApiModel("邮件消息DTO")
public class MailMessageDTO implements Serializable {
@ApiModelProperty(value = "机构ID",required = true)
@NotNull(message = "机构ID不能为空")
private Long deptId;
@ApiModelProperty("机构名称")
private String deptName;
@ApiModelProperty(value = "业务关键字",required = true)
@NotNull(message = "业务关键字不能为空")
private String businessKey;
@ApiModelProperty(value = "业务分类。系统通知:system,事务消息:business,日常提醒:dailyRemind,巡检消息:inspect",required = true)
@NotNull(message = "业务分类不能为空")
private String businessClassify;
@ApiModelProperty("业务任务ID")
@QueryField(condition = SqlCondition.EQUAL)
private Long taskId;
@NotNull
@ApiModelProperty(value = "内容")
private String content;
@NotNull
@ApiModelProperty(value = "主题")
private String subject;
@NotNull
@ApiModelProperty(value = "推送用户")
private String userIds;
@ApiModelProperty(value = "创建用户")
private Long createUser;
@ApiModelProperty("邮件内容是否为html")
private Boolean isHtml;
@ApiModelProperty("邮件附件")
private List<MultipartFile> appendix;
@ApiModelProperty("抄送账户")
private String ccAccount;
@ApiModelProperty(value = "租户ID")
private String tenantId;
}

13
hzims-service-api/message-api/src/main/java/com/hnac/hzims/message/entity/MessagePushRecordEntity.java

@ -13,10 +13,12 @@ import org.springblade.core.mp.support.QueryField;
import org.springblade.core.mp.support.SqlCondition;
import org.springblade.core.tool.utils.DateUtil;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.multipart.MultipartFile;
import javax.validation.constraints.NotNull;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
@Data
@ApiModel(value = "消息推送记录",description = "消息推送记录")
@ -101,4 +103,15 @@ public class MessagePushRecordEntity extends MessageTemplateEntity {
@TableField(exist = false)
private LocalDate endTime;
@ApiModelProperty("是否为html,邮件发送专用")
@TableField(exist = false)
private Boolean isHtml;
@ApiModelProperty("邮件附件")
@TableField(exist = false)
private List<MultipartFile> appendix;
@ApiModelProperty("抄送账户")
@TableField(exist = false)
private String ccAccount;
}

4
hzims-service-api/message-api/src/main/java/com/hnac/hzims/message/fegin/IMessageClient.java

@ -29,6 +29,7 @@ public interface IMessageClient {
String GET_TEMPLATE_BY_ID = API_PREFIX + "/getMsgTemplateById";
String SEND_APP_AND_WS_MSG = API_PREFIX + "/sendAppAndWsMsgByUsers";
String SEND_SMS_IMMEDIATELY_MSG = API_PREFIX + "/sendSmsImmediatelyMsg";
String SEND_MAIL_MSG_BY_USERS = API_PREFIX + "/sendMailMsgByUsers";
/**
* 推送消息短信
@ -67,6 +68,9 @@ public interface IMessageClient {
@PostMapping(value = SEND_APP_AND_WS_MSG,consumes = "application/json; charset=UTF-8")
R<Boolean> sendAppAndWsMsgByUsers(@RequestBody BusinessMessageDTO request);
@PostMapping(SEND_MAIL_MSG_BY_USERS)
R<Boolean> sendMailMsgByUsers(@RequestBody MailMessageDTO request);
/**
* 发送即时推送短信消息

5
hzims-service-api/message-api/src/main/java/com/hnac/hzims/message/fegin/MessageClientFallback.java

@ -43,6 +43,11 @@ public class MessageClientFallback implements IMessageClient {
}
@Override
public R<Boolean> sendMailMsgByUsers(MailMessageDTO request) {
return R.fail("调用失败!");
}
@Override
public R<Boolean> sendSmsImmediatelyMsg(SmsImmediatelyPushDTO smsImmediatelyPushDTO) {
return R.fail("调用失败!");
}

33
hzims-service/message/src/main/java/com/hnac/hzims/message/fegin/MessageClient.java

@ -167,12 +167,33 @@ public class MessageClient extends BladeController implements IMessageClient{
}).collect(Collectors.toList());
boolean saveResult = recordService.saveBatch(pushRecords);
if(saveResult) {
List<MessagePushRecordEntity> appRecords = pushRecords.stream().filter(record -> MessageConstants.APP_PUSH.equals(record.getType())).collect(Collectors.toList());
// 推送消息 - app
Boolean appFlag = recordService.sendAppMsgByUsers(request, appRecords);
// 推送消息 - web
Boolean wsFlag = recordService.sendWsMsgByUsers(new ArrayList<>(CollectionUtils.subtract(pushRecords, appRecords)));
return R.data(appFlag && wsFlag);
return R.status(recordService.sendMailMsgByUsers(pushRecords));
}
else {
return R.data(false);
}
}
@Override
@PostMapping(SEND_MAIL_MSG_BY_USERS)
public R<Boolean> sendMailMsgByUsers(@RequestBody MailMessageDTO request) {
// 保存消息记录
List<MessagePushRecordEntity> pushRecords = Func.toLongList(request.getUserIds()).stream().map(userId -> {
MessagePushRecordEntity record = BeanUtil.copy(request, MessagePushRecordEntity.class);
long messageId = IdWorker.getId();
record.setDeptName(Func.isNotEmpty(record.getDeptName()) ? record.getDeptName() : this.getDeptNameById(record.getDeptId()));
record.setMessageId(messageId);
record.setPusher(userId.toString());
record.setPusherName(Optional.ofNullable(UserCache.getUser(userId)).map(User::getName).orElse(null));
record.setType(MessageConstants.MAIL_PUSH);
record.setPushType(MessageConstants.IMMEDIATELY);
record.setPlanTime(LocalDateTime.now());
record.setCreateDept(record.getDeptId());
record.setStatus(MessageConstants.NOT_PUSH);
return record;
}).collect(Collectors.toList());
if(recordService.saveBatch(pushRecords)) {
return R.status(recordService.sendMailMsgByUsers(pushRecords));
}
else {
return R.data(false);

2
hzims-service/message/src/main/java/com/hnac/hzims/message/service/IMessagePushRecordService.java

@ -120,6 +120,8 @@ public interface IMessagePushRecordService extends BaseService<MessagePushRecord
*/
Boolean sendWsMsgByUsers(List<MessagePushRecordEntity> request);
Boolean sendMailMsgByUsers(List<MessagePushRecordEntity> request);
/**
* 新人员发送统计

33
hzims-service/message/src/main/java/com/hnac/hzims/message/service/impl/MailMessageServiceImpl.java

@ -14,6 +14,7 @@ import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.CollectionUtil;
import org.springblade.core.tool.utils.ObjectUtil;
import org.springblade.core.tool.utils.StringUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
@ -78,7 +79,37 @@ public class MailMessageServiceImpl implements IMessageService {
@Override
public boolean send(MessagePushRecordEntity request) {
return false;
// 创建复杂有限发送对象
MimeMessage mimeMessage = mailSender.createMimeMessage();
MimeMessageHelper messageHelper;
try {
messageHelper = new MimeMessageHelper(mimeMessage, true);
// 设置发件人邮箱(若配置默认邮箱则不用再设置)
messageHelper.setFrom(fromAccount);
// 设置收件人邮箱
messageHelper.setTo(getAccountByPusher(Long.parseLong(request.getPusher()),request.getType()));
// 设置抄报人邮箱(可以不填写)
if(StringUtil.isNotBlank(request.getCcAccount())) {
messageHelper.setCc(request.getCcAccount());
}
// 设置密送人邮箱(可以不填写)
//messageHelper.setBcc("575814158@qq.com");
// 设置邮件主题
messageHelper.setSubject(request.getSubject());
messageHelper.setText(request.getContent(),request.getIsHtml());
//填充附件
if(CollectionUtil.isNotEmpty(request.getAppendix())){
for (MultipartFile appendix : request.getAppendix()) {
messageHelper.addAttachment(appendix.getName(),appendix);
}
}
//发送邮件
mailSender.send(mimeMessage);
} catch (MessagingException e) {
e.printStackTrace();
throw new ServiceException(e.getMessage());
}
return true;
}
}

17
hzims-service/message/src/main/java/com/hnac/hzims/message/service/impl/MessagePushRecordServiceImpl.java

@ -441,6 +441,23 @@ public class MessagePushRecordServiceImpl extends BaseServiceImpl<MessagePushRec
return true;
}
@Override
public Boolean sendMailMsgByUsers(List<MessagePushRecordEntity> request) {
MailMessageServiceImpl service = SpringUtil.getBean(MailMessageServiceImpl.class);
request.forEach(record -> {
boolean sendFlag = service.send(record);
if (sendFlag) {
this.update(Wrappers.<MessagePushRecordEntity>lambdaUpdate()
.set(MessagePushRecordEntity::getPushTime, LocalDateTime.now())
.set(MessagePushRecordEntity::getStatus, MessageConstants.PUSH_SUCCESS)
.set(MessagePushRecordEntity::getAccount,service.getAccountByPusher(Long.parseLong(record.getPusher()), record.getType()))
.in(MessagePushRecordEntity::getId, record.getId())
);
}
});
return true;
}
/**
* 状态 ===> 状态名称

42
hzims-service/operational/src/main/java/com/hnac/hzims/operational/station/service/impl/StationServiceImpl.java

@ -51,6 +51,9 @@ import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import java.util.*;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.FutureTask;
import java.util.stream.Collectors;
/**
@ -71,6 +74,7 @@ public class StationServiceImpl extends BaseServiceImpl<StationMapper, StationEn
private final IStationPushConfigService stationPushConfigService;
@Override
@Transactional(rollbackFor = Exception.class)
public void buildStation(StationEntity req) {
LambdaQueryWrapper lwq;
if (Func.isEmpty(req.getId())) {
@ -105,17 +109,37 @@ public class StationServiceImpl extends BaseServiceImpl<StationMapper, StationEn
*/
private void pushStation(StationEntity req) {
// 读取站点类型配置确定是否推送FDP
StationPushConfigEntity config = StationPushConfigEntity.builder().stationType(req.getType()).build();
StationPushConfigVO configVO = stationPushConfigService.getOne(BeanUtil.copy(config, StationPushConfigDTO.class));
if (ObjectUtil.isNotEmpty(configVO) && configVO.getIsPush()) {
log.info("{}站点开始智能诊断站点推送", req.getCode());
new Thread(() -> questionClient.submitStationInfo(configVO.getFdpStationType(), req.getCode(), "运维平台推送"), "智能诊断站点推送").start();
}
FutureTask<R> fdpPush = new FutureTask<>(() -> {
StationPushConfigEntity config = StationPushConfigEntity.builder().stationType(req.getType()).build();
StationPushConfigVO configVO = stationPushConfigService.getOne(BeanUtil.copy(config, StationPushConfigDTO.class));
if (ObjectUtil.isNotEmpty(configVO) && configVO.getIsPush()) {
log.info("{}站点开始智能诊断站点推送", req.getCode());
return questionClient.submitStationInfo(configVO.getFdpStationType(), req.getCode(), "运维平台推送");
}
return R.success("推送配置未找到相应配置,暂不推送至FDP");
});
// 如果设备数据来源未选定其他 则进行站点初始化操作
if (StationConstants.GATHER.equals(req.getDataOrigin()) && !StationConstants.HZ3000_OTHER.equals(req.getDataSource())) {
log.info("{}站点开始数据平台新建项目", req.getCode());
new Thread(() -> this.instanceProject(req), "数据平台新建项目").start();
FutureTask<R> dataPush = new FutureTask<>(() -> {
if (StationConstants.GATHER.equals(req.getDataOrigin()) && !StationConstants.HZ3000_OTHER.equals(req.getDataSource())) {
log.info("{}站点开始数据平台新建项目", req.getCode());
return R.status(this.instanceProject(req));
}
return R.success("不满足数据平台推送要求,暂不推送至数据平台");
});
new Thread(fdpPush,"智能诊断站点推送").start();
new Thread(dataPush,"数据平台站点推送").start();
boolean flag;
try {
flag = fdpPush.get().isSuccess() && dataPush.get().isSuccess();
}
catch (ExecutionException e) {
throw new RuntimeException(e);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
Assert.isTrue(flag,() -> {
throw new ServiceException("站点推送数据平台/FDP失败!");
});
}
@Override

Loading…
Cancel
Save