From 566c745bfd017ff12bf32a92f07cf474f9661cd8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=87=E4=BD=B3?= Date: Tue, 14 Sep 2021 15:27:59 +0800 Subject: [PATCH] =?UTF-8?q?=E9=82=AE=E4=BB=B6model=E5=B1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- model/pom.xml | 6 - .../model/dao/entity/EmailJob.java | 96 +++++++++++++ .../model/dao/entity/EmailSendRecord.java | 75 ++++++++++ .../dao/entity/vo/NewEmailJobParamsVo.java | 63 +++++++++ .../model/dao/entity/vo/NewEmailJobVo.java | 74 ++++++++++ .../model/dao/mapper/EmailJobsMapper.java | 23 +++ .../dao/mapper/EmailSendRecordsMapper.java | 25 ++++ .../notification/EmailJobsService.java | 45 ++++++ .../notification/EmailSendRecordsService.java | 33 +++++ .../impl/EmailJobsServiceImpl.java | 41 ++++++ .../impl/EmailSendRecordsServiceImpl.java | 41 ++++++ .../mapper/notification/EmailJobsMapper.xml | 132 ++++++++++++++++++ .../notification/EmailSendRecordsMapper.xml | 120 ++++++++++++++++ .../controller/EmailJobsController.java | 98 +++++++++++++ 14 files changed, 866 insertions(+), 6 deletions(-) create mode 100644 model/src/main/java/cn/org/gitlink/notification/model/dao/entity/EmailJob.java create mode 100644 model/src/main/java/cn/org/gitlink/notification/model/dao/entity/EmailSendRecord.java create mode 100644 model/src/main/java/cn/org/gitlink/notification/model/dao/entity/vo/NewEmailJobParamsVo.java create mode 100644 model/src/main/java/cn/org/gitlink/notification/model/dao/entity/vo/NewEmailJobVo.java create mode 100644 model/src/main/java/cn/org/gitlink/notification/model/dao/mapper/EmailJobsMapper.java create mode 100644 model/src/main/java/cn/org/gitlink/notification/model/dao/mapper/EmailSendRecordsMapper.java create mode 100644 model/src/main/java/cn/org/gitlink/notification/model/service/notification/EmailJobsService.java create mode 100644 model/src/main/java/cn/org/gitlink/notification/model/service/notification/EmailSendRecordsService.java create mode 100644 model/src/main/java/cn/org/gitlink/notification/model/service/notification/impl/EmailJobsServiceImpl.java create mode 100644 model/src/main/java/cn/org/gitlink/notification/model/service/notification/impl/EmailSendRecordsServiceImpl.java create mode 100644 model/src/main/resources/mapper/notification/EmailJobsMapper.xml create mode 100644 model/src/main/resources/mapper/notification/EmailSendRecordsMapper.xml create mode 100644 writer/src/main/java/cn/org/gitlink/notification/writer/controller/EmailJobsController.java diff --git a/model/pom.xml b/model/pom.xml index 16e0dfb..91b059a 100644 --- a/model/pom.xml +++ b/model/pom.xml @@ -79,12 +79,6 @@ 2.3.4.RELEASE test - - junit - junit - 4.12 - test - diff --git a/model/src/main/java/cn/org/gitlink/notification/model/dao/entity/EmailJob.java b/model/src/main/java/cn/org/gitlink/notification/model/dao/entity/EmailJob.java new file mode 100644 index 0000000..f53c560 --- /dev/null +++ b/model/src/main/java/cn/org/gitlink/notification/model/dao/entity/EmailJob.java @@ -0,0 +1,96 @@ +package cn.org.gitlink.notification.model.dao.entity; + + +import com.baomidou.mybatisplus.annotation.IdType; +import com.baomidou.mybatisplus.annotation.TableId; +import com.fasterxml.jackson.annotation.JsonFormat; +import com.fasterxml.jackson.annotation.JsonProperty; + +import java.util.Date; + +public class EmailJob { + @TableId(type = IdType.AUTO) + private Integer id; + + private Integer sender; + + private String emails; + + private String subject; + + private String content; + + @JsonProperty("created_at") + @JsonFormat(pattern = "yyyy-MM-dd HH:dd:ss") + private Date createdAt; + + @JsonProperty("dispatched_at") + @JsonFormat(pattern = "yyyy-MM-dd HH:dd:ss") + private Date dispatchedAt; + + private Integer dispatchedStatus; + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public Integer getSender() { + return sender; + } + + public void setSender(Integer sender) { + this.sender = sender; + } + + public String getEmails() { + return emails; + } + + public void setEmails(String emails) { + this.emails = emails == null ? null : emails.trim(); + } + + public String getSubject() { + return subject; + } + + public void setSubject(String subject) { + this.subject = subject == null ? null : subject.trim(); + } + + public String getContent() { + return content; + } + + public void setContent(String content) { + this.content = content == null ? null : content.trim(); + } + + public Date getCreatedAt() { + return createdAt; + } + + public void setCreatedAt(Date createdAt) { + this.createdAt = createdAt; + } + + public Date getDispatchedAt() { + return dispatchedAt; + } + + public void setDispatchedAt(Date dispatchedAt) { + this.dispatchedAt = dispatchedAt; + } + + public Integer getDispatchedStatus() { + return dispatchedStatus; + } + + public void setDispatchedStatus(Integer dispatchedStatus) { + this.dispatchedStatus = dispatchedStatus; + } +} diff --git a/model/src/main/java/cn/org/gitlink/notification/model/dao/entity/EmailSendRecord.java b/model/src/main/java/cn/org/gitlink/notification/model/dao/entity/EmailSendRecord.java new file mode 100644 index 0000000..22047e3 --- /dev/null +++ b/model/src/main/java/cn/org/gitlink/notification/model/dao/entity/EmailSendRecord.java @@ -0,0 +1,75 @@ +package cn.org.gitlink.notification.model.dao.entity; + +import com.baomidou.mybatisplus.annotation.IdType; +import com.baomidou.mybatisplus.annotation.TableId; +import com.fasterxml.jackson.annotation.JsonFormat; +import com.fasterxml.jackson.annotation.JsonProperty; + +import java.util.Date; + +public class EmailSendRecord { + @TableId(type = IdType.AUTO) + private Integer id; + + private String email; + + private Integer jobId; + + @JsonProperty("created_at") + @JsonFormat(pattern = "yyyy-MM-dd HH:dd:ss") + private Date createdAt; + + @JsonProperty("sent_at") + @JsonFormat(pattern = "yyyy-MM-dd HH:dd:ss") + private Date sentAt; + + private Integer status; + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email == null ? null : email.trim(); + } + + public Integer getJobId() { + return jobId; + } + + public void setJobId(Integer jobId) { + this.jobId = jobId; + } + + public Date getCreatedAt() { + return createdAt; + } + + public void setCreatedAt(Date createdAt) { + this.createdAt = createdAt; + } + + public Date getSentAt() { + return sentAt; + } + + public void setSentAt(Date sentAt) { + this.sentAt = sentAt; + } + + public Integer getStatus() { + return status; + } + + public void setStatus(Integer status) { + this.status = status; + } +} diff --git a/model/src/main/java/cn/org/gitlink/notification/model/dao/entity/vo/NewEmailJobParamsVo.java b/model/src/main/java/cn/org/gitlink/notification/model/dao/entity/vo/NewEmailJobParamsVo.java new file mode 100644 index 0000000..35fb3a2 --- /dev/null +++ b/model/src/main/java/cn/org/gitlink/notification/model/dao/entity/vo/NewEmailJobParamsVo.java @@ -0,0 +1,63 @@ +package cn.org.gitlink.notification.model.dao.entity.vo; + +import io.swagger.annotations.ApiModelProperty; +import org.hibernate.validator.constraints.Range; + +import javax.validation.constraints.NotBlank; +import javax.validation.constraints.NotNull; +import javax.validation.constraints.Pattern; +import javax.validation.constraints.Size; + +public class NewEmailJobParamsVo { + + @ApiModelProperty(value = "邮件发送者", required = true) + @NotNull(message = "邮件发送者不能为空") + @Range(min = Integer.MIN_VALUE, max = Integer.MAX_VALUE) + private Integer sender; + + @ApiModelProperty(value = "邮件地址,eg: w@163.com,j@163.com", required = true) + @Pattern(regexp = "^\\w+([-+.']\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*(,\\w+([-+.']\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*)*$", message = "邮件地址串非法") + @NotBlank(message = "邮件地址不能为空") + private String emails; + + @ApiModelProperty(value = "邮件主题", required = true) + @NotBlank(message = "邮件主题不能为空") + @Size(max = 255, message = "邮件主题长度在0~255") + private String subject; + + @ApiModelProperty(value = "邮件内容", required = true) + @NotBlank(message = "邮件内容不能为空") + private String content; + + public Integer getSender() { + return sender; + } + + public void setSender(Integer sender) { + this.sender = sender; + } + + public String getEmails() { + return emails; + } + + public void setEmails(String emails) { + this.emails = emails; + } + + public String getSubject() { + return subject; + } + + public void setSubject(String subject) { + this.subject = subject; + } + + public String getContent() { + return content; + } + + public void setContent(String content) { + this.content = content; + } +} diff --git a/model/src/main/java/cn/org/gitlink/notification/model/dao/entity/vo/NewEmailJobVo.java b/model/src/main/java/cn/org/gitlink/notification/model/dao/entity/vo/NewEmailJobVo.java new file mode 100644 index 0000000..9781bfc --- /dev/null +++ b/model/src/main/java/cn/org/gitlink/notification/model/dao/entity/vo/NewEmailJobVo.java @@ -0,0 +1,74 @@ +package cn.org.gitlink.notification.model.dao.entity.vo; + + +import io.swagger.annotations.ApiModelProperty; +import org.hibernate.validator.constraints.Range; + +import javax.validation.constraints.NotBlank; +import javax.validation.constraints.NotNull; +import javax.validation.constraints.Pattern; +import javax.validation.constraints.Size; + +public class NewEmailJobVo { + //平台编码 + private String platform; + + @ApiModelProperty(value = "邮件发送者", required = true) + @NotNull(message = "邮件发送者不能为空") + @Range(min = Integer.MIN_VALUE, max = Integer.MAX_VALUE) + private Integer sender; + + @ApiModelProperty(value = "邮件地址,eg: w@163.com,j@163.com", required = true) + @Pattern(regexp = "^(\\-|\\+?)\\d+(,\\d+)*$", message = "邮件地址串非法") + @NotBlank(message = "邮件地址不能为空") + private String emails; + + @ApiModelProperty(value = "邮件主题", required = true) + @NotBlank(message = "邮件主题不能为空") + @Size(max = 255, message = "邮件主题长度在0~255") + private String subject; + + @ApiModelProperty(value = "邮件内容", required = true) + @NotBlank(message = "邮件内容不能为空") + private String content; + + public String getPlatform() { + return platform; + } + + public void setPlatform(String platform) { + this.platform = platform; + } + + public Integer getSender() { + return sender; + } + + public void setSender(Integer sender) { + this.sender = sender; + } + + public String getEmails() { + return emails; + } + + public void setEmails(String emails) { + this.emails = emails; + } + + public String getSubject() { + return subject; + } + + public void setSubject(String subject) { + this.subject = subject; + } + + public String getContent() { + return content; + } + + public void setContent(String content) { + this.content = content; + } +} diff --git a/model/src/main/java/cn/org/gitlink/notification/model/dao/mapper/EmailJobsMapper.java b/model/src/main/java/cn/org/gitlink/notification/model/dao/mapper/EmailJobsMapper.java new file mode 100644 index 0000000..9b14e27 --- /dev/null +++ b/model/src/main/java/cn/org/gitlink/notification/model/dao/mapper/EmailJobsMapper.java @@ -0,0 +1,23 @@ +package cn.org.gitlink.notification.model.dao.mapper; + +import cn.org.gitlink.notification.model.dao.entity.EmailJob; +import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import org.apache.ibatis.annotations.Param; + +import java.util.List; + +public interface EmailJobsMapper extends BaseMapper { + int deleteByPrimaryKey(@Param("platform") String platform, Integer id); + + int insert(@Param("platform") String platform, @Param("record") EmailJob record); + + int insertSelective(@Param("platform") String platform, @Param("record") EmailJob record); + + EmailJob selectByPrimaryKey(@Param("platform") String platform, Integer id); + + int updateByPrimaryKeySelective(@Param("platform") String platform, @Param("record") EmailJob record); + + int updateByPrimaryKey(@Param("platform") String platform, @Param("record") EmailJob record); + + List getNotDispatchedEmailJobs(@Param("platform") String platform); +} diff --git a/model/src/main/java/cn/org/gitlink/notification/model/dao/mapper/EmailSendRecordsMapper.java b/model/src/main/java/cn/org/gitlink/notification/model/dao/mapper/EmailSendRecordsMapper.java new file mode 100644 index 0000000..1cd26f7 --- /dev/null +++ b/model/src/main/java/cn/org/gitlink/notification/model/dao/mapper/EmailSendRecordsMapper.java @@ -0,0 +1,25 @@ +package cn.org.gitlink.notification.model.dao.mapper; + +import cn.org.gitlink.notification.model.dao.entity.EmailSendRecord; +import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import org.apache.ibatis.annotations.Param; + +import java.util.List; + +public interface EmailSendRecordsMapper extends BaseMapper { + int deleteByPrimaryKey(@Param("platform") String platform, Integer id); + + int insert(@Param("platform") String platform, @Param("record") EmailSendRecord record); + + int insertSelective(@Param("platform") String platform, @Param("record") EmailSendRecord record); + + EmailSendRecord selectByPrimaryKey(@Param("platform") String platform, Integer id); + + int updateByPrimaryKeySelective(@Param("platform") String platform, @Param("record") EmailSendRecord record); + + int updateByPrimaryKey(@Param("platform") String platform, @Param("record") EmailSendRecord record); + + //批量插入邮件发送任务记录 + int insertEmailSendRecordBatch(@Param("platform") String platform,@Param("list") List emailSendRecordList); + +} diff --git a/model/src/main/java/cn/org/gitlink/notification/model/service/notification/EmailJobsService.java b/model/src/main/java/cn/org/gitlink/notification/model/service/notification/EmailJobsService.java new file mode 100644 index 0000000..549245c --- /dev/null +++ b/model/src/main/java/cn/org/gitlink/notification/model/service/notification/EmailJobsService.java @@ -0,0 +1,45 @@ +package cn.org.gitlink.notification.model.service.notification; + +import cn.org.gitlink.notification.model.dao.entity.EmailJob; +import cn.org.gitlink.notification.model.dao.entity.vo.NewEmailJobVo; +import com.baomidou.mybatisplus.extension.service.IService; + +import java.util.Date; +import java.util.List; + +public interface EmailJobsService extends IService { + + /** + * @Description: 添加发送邮件任务 + * + * @Param newEmailJobVo + * @return: boolean + * @Author: wanjia + * @Date: 2021/9/13 + */ + boolean sendEmail(NewEmailJobVo newEmailJobVo) throws Exception; + + /** + * @Description: 获取所有未处理邮件任务列表 + * + * @Param platform 平台编码 + * @return: List + * @Author: wanjia + * @Date: 2021/9/13 + */ + List getNotDispatchedEmailJobs(String platform) throws Exception; + + /** + * @Description: + * + * @Param platform 平台编码 + * @Param emailJobId 邮件任务id + * @Param dispatchedAt 处理时间 + * @Param dispatchedStatus 处理状态 -1 未处理,1 处理成功,2 处理失败 + * @return: int + * @Author: wanjia + * @Date: 2021/9/13 + */ + int markEmailJobAs(String platform, Integer emailJobId, Date dispatchedAt, Integer dispatchedStatus) throws Exception; + +} diff --git a/model/src/main/java/cn/org/gitlink/notification/model/service/notification/EmailSendRecordsService.java b/model/src/main/java/cn/org/gitlink/notification/model/service/notification/EmailSendRecordsService.java new file mode 100644 index 0000000..6131aec --- /dev/null +++ b/model/src/main/java/cn/org/gitlink/notification/model/service/notification/EmailSendRecordsService.java @@ -0,0 +1,33 @@ +package cn.org.gitlink.notification.model.service.notification; + +import cn.org.gitlink.notification.model.dao.entity.EmailSendRecord; +import com.baomidou.mybatisplus.extension.service.IService; + +import java.util.Date; + +public interface EmailSendRecordsService extends IService { + /** + * @Description: 新增邮件任务到邮件发送记录表中 + * @Param platform 平台编码 + * @Param emails 邮件地址,eg: w@163.com,j@163.com + * @Param jobId 邮件任务id + * @return: boolean + * @Author: wanjia + * @Date: 2021/9/13 + */ + boolean newEmailSendRecords(String platform, String emails, Integer jobId) throws Exception; + + /** + * @Description: + * + * @Param platform 平台编码 + * @Param emailSendRecordId 邮件发送记录id + * @Param sentAt 发送时间 + * @Param status 发送状态 -1 未发送,1 发送成功,2 发送失败 + * @return: int + * @Author: wanjia + * @Date: 2021/9/13 + */ + int markEmailSendRecordAs(String platform, Integer emailSendRecordId, Date sentAt, Integer status) throws Exception; + +} diff --git a/model/src/main/java/cn/org/gitlink/notification/model/service/notification/impl/EmailJobsServiceImpl.java b/model/src/main/java/cn/org/gitlink/notification/model/service/notification/impl/EmailJobsServiceImpl.java new file mode 100644 index 0000000..ee0b8ac --- /dev/null +++ b/model/src/main/java/cn/org/gitlink/notification/model/service/notification/impl/EmailJobsServiceImpl.java @@ -0,0 +1,41 @@ +package cn.org.gitlink.notification.model.service.notification.impl; + +import cn.org.gitlink.notification.model.dao.entity.EmailJob; +import cn.org.gitlink.notification.model.dao.entity.vo.NewEmailJobVo; +import cn.org.gitlink.notification.model.dao.mapper.EmailJobsMapper; +import cn.org.gitlink.notification.model.service.notification.EmailJobsService; +import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.springframework.beans.BeanUtils; +import org.springframework.beans.BeansException; +import org.springframework.stereotype.Service; + +import java.util.Date; +import java.util.List; + +@Service +public class EmailJobsServiceImpl extends ServiceImpl implements EmailJobsService { + + @Override + public boolean sendEmail(NewEmailJobVo newEmailJobVo) { + String platform = newEmailJobVo.getPlatform(); + EmailJob emailJob = new EmailJob(); + BeanUtils.copyProperties(newEmailJobVo, emailJob); + return baseMapper.insertSelective(platform, emailJob) > 0; + } + + @Override + public List getNotDispatchedEmailJobs(String platform) { + return baseMapper.getNotDispatchedEmailJobs(platform); + } + + @Override + public int markEmailJobAs(String platform, Integer emailJobId, Date dispatchedAt, Integer dispatchedStatus) throws Exception { + EmailJob emailJob = new EmailJob(); + emailJob.setId(emailJobId); + emailJob.setDispatchedAt(dispatchedAt); + emailJob.setDispatchedStatus(dispatchedStatus); + return baseMapper.updateByPrimaryKeySelective(platform, emailJob); + } +} diff --git a/model/src/main/java/cn/org/gitlink/notification/model/service/notification/impl/EmailSendRecordsServiceImpl.java b/model/src/main/java/cn/org/gitlink/notification/model/service/notification/impl/EmailSendRecordsServiceImpl.java new file mode 100644 index 0000000..31b04c9 --- /dev/null +++ b/model/src/main/java/cn/org/gitlink/notification/model/service/notification/impl/EmailSendRecordsServiceImpl.java @@ -0,0 +1,41 @@ +package cn.org.gitlink.notification.model.service.notification.impl; + +import cn.org.gitlink.notification.model.dao.entity.EmailSendRecord; +import cn.org.gitlink.notification.model.dao.mapper.EmailSendRecordsMapper; +import cn.org.gitlink.notification.model.service.notification.EmailSendRecordsService; +import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Date; +import java.util.List; + +@Service +public class EmailSendRecordsServiceImpl extends ServiceImpl implements EmailSendRecordsService { + + @Override + @Transactional + public boolean newEmailSendRecords(String platform, String emails, Integer jobId) { + EmailSendRecord emailSendRecord = new EmailSendRecord(); + emailSendRecord.setJobId(jobId); + List list = Arrays.asList(emails.split(",")); + List emailSendRecordList = new ArrayList(); + for (String email : list){ + emailSendRecord.setEmail(email); + emailSendRecordList.add(emailSendRecord); + } + int i = baseMapper.insertEmailSendRecordBatch(platform, emailSendRecordList); + return i > 0; + } + + @Override + public int markEmailSendRecordAs(String platform, Integer emailSendRecordId, Date sentAt, Integer status) throws Exception { + EmailSendRecord emailSendRecord = new EmailSendRecord(); + emailSendRecord.setId(emailSendRecordId); + emailSendRecord.setSentAt(sentAt); + emailSendRecord.setStatus(status); + return baseMapper.updateByPrimaryKeySelective(platform, emailSendRecord); + } +} diff --git a/model/src/main/resources/mapper/notification/EmailJobsMapper.xml b/model/src/main/resources/mapper/notification/EmailJobsMapper.xml new file mode 100644 index 0000000..9b4a120 --- /dev/null +++ b/model/src/main/resources/mapper/notification/EmailJobsMapper.xml @@ -0,0 +1,132 @@ + + + + + + + + + + + + + + + id, sender, emails, subject, content, created_at, dispatched_at, dispatched_status + + + + delete from ${platform}_email_jobs + where id = #{id,jdbcType=INTEGER} + + + insert into ${platform}_email_jobs (id, sender, emails, + subject, content, created_at, + dispatched_at, dispatched_status) + values (#{record.id,jdbcType=INTEGER}, #{record.sender,jdbcType=INTEGER}, #{record.emails,jdbcType=VARCHAR}, + #{record.subject,jdbcType=VARCHAR}, #{record.content,jdbcType=VARCHAR}, #{record.createdAt,jdbcType=TIMESTAMP}, + #{record.dispatchedAt,jdbcType=TIMESTAMP}, #{record.dispatchedStatus,jdbcType=INTEGER}) + + + insert into ${platform}_email_jobs + + + id, + + + sender, + + + emails, + + + subject, + + + content, + + + created_at, + + + dispatched_at, + + + dispatched_status, + + + + + #{record.id,jdbcType=INTEGER}, + + + #{record.sender,jdbcType=INTEGER}, + + + #{record.emails,jdbcType=VARCHAR}, + + + #{record.subject,jdbcType=VARCHAR}, + + + #{record.content,jdbcType=VARCHAR}, + + + #{record.createdAt,jdbcType=TIMESTAMP}, + + + #{record.dispatchedAt,jdbcType=TIMESTAMP}, + + + #{record.dispatchedStatus,jdbcType=INTEGER}, + + + + + update ${platform}_email_jobs + + + sender = #{record.sender,jdbcType=INTEGER}, + + + emails = #{record.emails,jdbcType=VARCHAR}, + + + subject = #{record.subject,jdbcType=VARCHAR}, + + + content = #{record.content,jdbcType=VARCHAR}, + + + created_at = #{record.createdAt,jdbcType=TIMESTAMP}, + + + dispatched_at = #{record.dispatchedAt,jdbcType=TIMESTAMP}, + + + dispatched_status = #{record.dispatchedStatus,jdbcType=INTEGER}, + + + where id = #{record.id,jdbcType=INTEGER} + + + update ${platform}_email_jobs + set sender = #{record.sender,jdbcType=INTEGER}, + emails = #{record.emails,jdbcType=VARCHAR}, + subject = #{record.subject,jdbcType=VARCHAR}, + content = #{record.content,jdbcType=VARCHAR}, + created_at = #{record.createdAt,jdbcType=TIMESTAMP}, + dispatched_at = #{record.dispatchedAt,jdbcType=TIMESTAMP}, + dispatched_status = #{record.dispatchedStatus,jdbcType=INTEGER} + where id = #{record.id,jdbcType=INTEGER} + + + \ No newline at end of file diff --git a/model/src/main/resources/mapper/notification/EmailSendRecordsMapper.xml b/model/src/main/resources/mapper/notification/EmailSendRecordsMapper.xml new file mode 100644 index 0000000..d141be0 --- /dev/null +++ b/model/src/main/resources/mapper/notification/EmailSendRecordsMapper.xml @@ -0,0 +1,120 @@ + + + + + + + + + + + + + id, email, job_id, created_at, sent_at, status + + + + delete from ${platform}_email_send_records + where id = #{id,jdbcType=INTEGER} + + + insert into ${platform}_email_send_records (id, email, job_id, + created_at, sent_at, status + ) + values (#{record.id,jdbcType=INTEGER}, #{record.email,jdbcType=VARCHAR}, #{record.jobId,jdbcType=INTEGER}, + #{record.createdAt,jdbcType=TIMESTAMP}, #{record.sentAt,jdbcType=TIMESTAMP}, #{record.status,jdbcType=INTEGER} + ) + + + insert into ${platform}_email_send_records + + + id, + + + email, + + + job_id, + + + created_at, + + + sent_at, + + + status, + + + + + #{record.id,jdbcType=INTEGER}, + + + #{record.email,jdbcType=VARCHAR}, + + + #{record.jobId,jdbcType=INTEGER}, + + + #{record.createdAt,jdbcType=TIMESTAMP}, + + + #{record.sentAt,jdbcType=TIMESTAMP}, + + + #{record.status,jdbcType=INTEGER}, + + + + + update ${platform}_email_send_records + + + email = #{record.email,jdbcType=VARCHAR}, + + + job_id = #{record.jobId,jdbcType=INTEGER}, + + + created_at = #{record.createdAt,jdbcType=TIMESTAMP}, + + + sent_at = #{record.sentAt,jdbcType=TIMESTAMP}, + + + status = #{record.status,jdbcType=INTEGER}, + + + where id = #{record.id,jdbcType=INTEGER} + + + update ${platform}_email_send_records + set email = #{record.email,jdbcType=VARCHAR}, + job_id = #{record.jobId,jdbcType=INTEGER}, + created_at = #{record.createdAt,jdbcType=TIMESTAMP}, + sent_at = #{record.sentAt,jdbcType=TIMESTAMP}, + status = #{record.status,jdbcType=INTEGER} + where id = #{record.id,jdbcType=INTEGER} + + + + SELECT LAST_INSERT_ID() + + insert into ${platform}_email_send_records + (id, email, job_id) + values + + ( + #{record.id,jdbcType=INTEGER}, #{record.email,jdbcType=VARCHAR}, #{record.jobId,jdbcType=INTEGER} + ) + + + \ No newline at end of file diff --git a/writer/src/main/java/cn/org/gitlink/notification/writer/controller/EmailJobsController.java b/writer/src/main/java/cn/org/gitlink/notification/writer/controller/EmailJobsController.java new file mode 100644 index 0000000..1c8a7c0 --- /dev/null +++ b/writer/src/main/java/cn/org/gitlink/notification/writer/controller/EmailJobsController.java @@ -0,0 +1,98 @@ +package cn.org.gitlink.notification.writer.controller; + +import cn.org.gitlink.notification.common.constant.NotificationSystemConstant; +import cn.org.gitlink.notification.common.response.DataPacketUtil; +import cn.org.gitlink.notification.common.response.ResponseData; +import cn.org.gitlink.notification.common.utils.KafkaUtil; +import cn.org.gitlink.notification.common.utils.ValidatorUtils; +import cn.org.gitlink.notification.model.dao.entity.EmailJob; +import cn.org.gitlink.notification.model.dao.entity.vo.NewEmailJobParamsVo; +import cn.org.gitlink.notification.model.dao.entity.vo.NewEmailJobVo; +import cn.org.gitlink.notification.model.dao.entity.vo.NewSysNotificationVo; +import cn.org.gitlink.notification.model.service.notification.EmailJobsService; +import cn.org.gitlink.notification.model.service.notification.EmailSendRecordsService; +import com.alibaba.fastjson.JSONObject; +import io.swagger.annotations.ApiOperation; +import io.swagger.annotations.ApiParam; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.springframework.beans.BeanUtils; +import org.springframework.beans.BeansException; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Configuration; +import org.springframework.validation.BindingResult; +import org.springframework.validation.annotation.Validated; +import org.springframework.web.bind.annotation.*; + +import java.util.Date; +import java.util.List; +import java.util.Map; + +@RestController +@RequestMapping(value = "/gns/email") +@Configuration +public class EmailJobsController { + + //todo yml文件中配置topic后替换 + private static final String GITLINK_EMAIL_TOPIC = "topic_gitlink_email"; + + private Logger logger = LogManager.getLogger(EmailJobsController.class); + + @Autowired + private KafkaUtil kafkaUtil; + + @ApiOperation("发送邮件任务") + @RequestMapping(path = "/{platform}", method = RequestMethod.POST) + @ResponseBody + public ResponseData sendNotification(@ApiParam(value = "平台编码", required = true) + @PathVariable(name = "platform") String platform, + + @Validated @RequestBody NewEmailJobParamsVo newEmailJobParamsVo, + + BindingResult bindingResult){ + //参数合法性验证 + Map errors = ValidatorUtils.buildValidationErrorMessageMap(bindingResult); + if (!errors.isEmpty()) { + return DataPacketUtil.jsonFailResult(errors); + } + ResponseData jsonFailResult = validatePlatformCode(platform); + if (jsonFailResult != null) return jsonFailResult; + + //platform和NewEmailJobParamsVo拼装 + NewEmailJobVo newEmailJobVo = new NewEmailJobVo(); + try { + BeanUtils.copyProperties(newEmailJobParamsVo, newEmailJobVo); + } catch (BeansException e) { + logger.error(e); + return DataPacketUtil.jsonFailResult(e.getMessage()); + } + newEmailJobVo.setPlatform(platform); + + try { + //todo creatTopic + + kafkaUtil.sendMessage(GITLINK_EMAIL_TOPIC, JSONObject.toJSONString(newEmailJobVo)); + return DataPacketUtil.jsonSuccessResult(); + } catch (Exception e) { + logger.error(e); + return DataPacketUtil.jsonFailResult(e.getMessage()); + } + } + + + /** + * 验证 platform合法性 + * + * @param platform + * @return + */ + private ResponseData validatePlatformCode(String platform) { + + //验证 {platform} 参数合法性,以判断请求来源 + if (!NotificationSystemConstant.PLATFORM_CODE_MAP.containsKey(platform)) { + logger.debug("\t 输入参数 {platform} 的值 {" + platform + "} 无效"); + return DataPacketUtil.jsonFailResult("{platform} 参数非法"); + } + return null; + } +}