diff --git a/cloud-manager-sms/src/main/java/com/cm/manager/sms/config/properties/EmailProperties.java b/cloud-manager-sms/src/main/java/com/cm/manager/sms/config/properties/EmailProperties.java new file mode 100644 index 0000000..d0d6eca --- /dev/null +++ b/cloud-manager-sms/src/main/java/com/cm/manager/sms/config/properties/EmailProperties.java @@ -0,0 +1,96 @@ +package com.cm.manager.sms.config.properties; + +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.stereotype.Component; + +import java.util.ArrayList; +import java.util.List; + +/** + * When you feel like quitting. Think about why you started + * 当你想要放弃的时候,想想当初你为何开始 + * + * @ClassName: EmailProperties + * @Description: 邮箱 + * @Author: WangGeng + * @Date: 2020/7/31 4:17 下午 + * @Version: 1.0 + **/ +@Component +@ConfigurationProperties(prefix = "email") +public class EmailProperties { + + private Boolean active = false; + private String smtp = "smtp.163.com"; + private Integer port = 25; + private String senderEmail; + private String senderPassword; + private List ccPersons = new ArrayList<>(); + + public Boolean getActive() { + return active; + } + + public void setActive(Boolean active) { + this.active = active; + } + + public String getSmtp() { + return smtp == null ? "" : smtp.trim(); + } + + public void setSmtp(String smtp) { + this.smtp = smtp; + } + + public Integer getPort() { + return port; + } + + public void setPort(Integer port) { + this.port = port; + } + + public String getSenderEmail() { + return senderEmail == null ? "" : senderEmail.trim(); + } + + public void setSenderEmail(String senderEmail) { + this.senderEmail = senderEmail; + } + + public String getSenderPassword() { + return senderPassword == null ? "" : senderPassword.trim(); + } + + public void setSenderPassword(String senderPassword) { + this.senderPassword = senderPassword; + } + + public List getCcPersons() { + return ccPersons; + } + + public void setCcPersons(List ccPersons) { + this.ccPersons = ccPersons; + } + + @Override + public String toString() { + final StringBuilder sb = new StringBuilder("{"); + sb.append("\"active\":") + .append(active); + sb.append(",\"smtp\":") + .append("\"").append(smtp).append("\""); + sb.append(",\"port\":") + .append(port); + sb.append(",\"senderEmail\":") + .append("\"").append(senderEmail).append("\""); + sb.append(",\"senderPassword\":") + .append("\"").append(senderPassword).append("\""); + sb.append(",\"ccPersons\":") + .append(ccPersons); + sb.append('}'); + return sb.toString(); + } +} diff --git a/cloud-manager-sms/src/main/java/com/cm/manager/sms/dao/email/IEmailDao.java b/cloud-manager-sms/src/main/java/com/cm/manager/sms/dao/email/IEmailDao.java new file mode 100644 index 0000000..1471d5d --- /dev/null +++ b/cloud-manager-sms/src/main/java/com/cm/manager/sms/dao/email/IEmailDao.java @@ -0,0 +1,67 @@ +package com.cm.manager.sms.dao.email; + +import com.cm.common.exception.RemoveException; +import com.cm.common.exception.SaveException; +import com.cm.common.exception.SearchException; +import com.cm.common.exception.UpdateException; +import com.cm.manager.sms.pojo.dtos.email.EmailDTO; +import org.springframework.stereotype.Repository; + +import java.util.List; +import java.util.Map; + +/** + * When you feel like quitting. Think about why you started + * 当你想要放弃的时候,想想当初你为何开始 + * + * @ClassName: IEmailDao + * @Description: 邮件 + * @Author: WangGeng + * @Date: 2020/7/31 4:20 下午 + * @Version: 1.0 + **/ +@Repository +public interface IEmailDao { + /** + * 保存Email + * + * @param params + * @throws SaveException + */ + void saveEmail(Map params) throws SaveException; + + /** + * 删除Email + * + * @param params + * @throws RemoveException + */ + void removeEmail(Map params) throws RemoveException; + + /** + * 更新邮件发送状态 + * + * @param params + * @throws UpdateException + */ + void updateEmailSendingStatus(Map params) throws UpdateException; + + /** + * Email详情 + * + * @param params + * @return + * @throws SearchException + */ + EmailDTO getEmail(Map params) throws SearchException; + + /** + * Email列表 + * + * @param params + * @return + * @throws SearchException + */ + List listEmail(Map params) throws SearchException; + +} diff --git a/cloud-manager-sms/src/main/java/com/cm/manager/sms/pojo/dtos/email/EmailDTO.java b/cloud-manager-sms/src/main/java/com/cm/manager/sms/pojo/dtos/email/EmailDTO.java new file mode 100644 index 0000000..cb30f5f --- /dev/null +++ b/cloud-manager-sms/src/main/java/com/cm/manager/sms/pojo/dtos/email/EmailDTO.java @@ -0,0 +1,134 @@ +package com.cm.manager.sms.pojo.dtos.email; + +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; + +/** + * When you feel like quitting. Think about why you started + * 当你想要放弃的时候,想想当初你为何开始 + * + * @ClassName: EmailDTO + * @Description: 邮箱 + * @Author: WangGeng + * @Date: 2020/7/31 4:37 下午 + * @Version: 1.0 + **/ +@ApiModel +public class EmailDTO { + + @ApiModelProperty(name = "emailId", value = "ID") + private String emailId; + @ApiModelProperty(name = "emailSender", value = "发送人") + private String emailSender; + @ApiModelProperty(name = "emailReceiver", value = "接收人") + private String emailReceiver; + @ApiModelProperty(name = "emailCc", value = "抄送人") + private String emailCc; + @ApiModelProperty(name = "emailType", value = "邮件类型") + private String emailType; + @ApiModelProperty(name = "emailSubject", value = "主题") + private String emailSubject; + @ApiModelProperty(name = "emailContent", value = "内容") + private String emailContent; + @ApiModelProperty(name = "sendingStatus", value = "发送状态") + private Integer sendingStatus; + @ApiModelProperty(name = "gmtCreate", value = "发送时间") + private String gmtCreate; + + public String getEmailId() { + return emailId == null ? "" : emailId.trim(); + } + + public void setEmailId(String emailId) { + this.emailId = emailId; + } + + public String getEmailSender() { + return emailSender == null ? "" : emailSender.trim(); + } + + public void setEmailSender(String emailSender) { + this.emailSender = emailSender; + } + + public String getEmailReceiver() { + return emailReceiver == null ? "" : emailReceiver.trim(); + } + + public void setEmailReceiver(String emailReceiver) { + this.emailReceiver = emailReceiver; + } + + public String getEmailCc() { + return emailCc == null ? "" : emailCc.trim(); + } + + public void setEmailCc(String emailCc) { + this.emailCc = emailCc; + } + + public String getEmailType() { + return emailType == null ? "" : emailType.trim(); + } + + public void setEmailType(String emailType) { + this.emailType = emailType; + } + + public String getEmailSubject() { + return emailSubject == null ? "" : emailSubject.trim(); + } + + public void setEmailSubject(String emailSubject) { + this.emailSubject = emailSubject; + } + + public String getEmailContent() { + return emailContent == null ? "" : emailContent.trim(); + } + + public void setEmailContent(String emailContent) { + this.emailContent = emailContent; + } + + public Integer getSendingStatus() { + return sendingStatus; + } + + public void setSendingStatus(Integer sendingStatus) { + this.sendingStatus = sendingStatus; + } + + public String getGmtCreate() { + return gmtCreate == null ? "" : gmtCreate.trim(); + } + + public void setGmtCreate(String gmtCreate) { + this.gmtCreate = gmtCreate; + } + + @Override + public String toString() { + final StringBuilder sb = new StringBuilder("{"); + sb.append("\"emailId\":") + .append("\"").append(emailId).append("\""); + sb.append(",\"emailSender\":") + .append("\"").append(emailSender).append("\""); + sb.append(",\"emailReceiver\":") + .append("\"").append(emailReceiver).append("\""); + sb.append(",\"emailCc\":") + .append("\"").append(emailCc).append("\""); + sb.append(",\"emailType\":") + .append("\"").append(emailType).append("\""); + sb.append(",\"emailSubject\":") + .append("\"").append(emailSubject).append("\""); + sb.append(",\"emailContent\":") + .append("\"").append(emailContent).append("\""); + sb.append(",\"sendingStatus\":") + .append(sendingStatus); + sb.append(",\"gmtCreate\":") + .append("\"").append(gmtCreate).append("\""); + sb.append('}'); + return sb.toString(); + } +} diff --git a/cloud-manager-sms/src/main/java/com/cm/manager/sms/pojo/vos/email/EmailVO.java b/cloud-manager-sms/src/main/java/com/cm/manager/sms/pojo/vos/email/EmailVO.java new file mode 100644 index 0000000..a6279f7 --- /dev/null +++ b/cloud-manager-sms/src/main/java/com/cm/manager/sms/pojo/vos/email/EmailVO.java @@ -0,0 +1,110 @@ +package com.cm.manager.sms.pojo.vos.email; + +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; + +/** + * When you feel like quitting. Think about why you started + * 当你想要放弃的时候,想想当初你为何开始 + * + * @ClassName: EmailVO + * @Description: 邮件 + * @Author: WangGeng + * @Date: 2020/7/31 4:31 下午 + * @Version: 1.0 + **/ +@ApiModel +public class EmailVO { + + @ApiModelProperty(name = "emailSender", value = "发送人") + private String emailSender; + @ApiModelProperty(name = "emailReceiver", value = "接收人") + private String emailReceiver; + @ApiModelProperty(name = "emailCc", value = "抄送人") + private String emailCc; + @ApiModelProperty(name = "emailType", value = "邮件类型") + private String emailType; + @ApiModelProperty(name = "emailSubject", value = "主题") + private String emailSubject; + @ApiModelProperty(name = "emailContent", value = "内容") + private String emailContent; + @ApiModelProperty(name = "sendingStatus", value = "发送状态") + private Integer sendingStatus; + + public String getEmailSender() { + return emailSender == null ? "" : emailSender.trim(); + } + + public void setEmailSender(String emailSender) { + this.emailSender = emailSender; + } + + public String getEmailReceiver() { + return emailReceiver == null ? "" : emailReceiver.trim(); + } + + public void setEmailReceiver(String emailReceiver) { + this.emailReceiver = emailReceiver; + } + + public String getEmailCc() { + return emailCc == null ? "" : emailCc.trim(); + } + + public void setEmailCc(String emailCc) { + this.emailCc = emailCc; + } + + public String getEmailType() { + return emailType == null ? "" : emailType.trim(); + } + + public void setEmailType(String emailType) { + this.emailType = emailType; + } + + public String getEmailSubject() { + return emailSubject == null ? "" : emailSubject.trim(); + } + + public void setEmailSubject(String emailSubject) { + this.emailSubject = emailSubject; + } + + public String getEmailContent() { + return emailContent == null ? "" : emailContent.trim(); + } + + public void setEmailContent(String emailContent) { + this.emailContent = emailContent; + } + + public Integer getSendingStatus() { + return sendingStatus; + } + + public void setSendingStatus(Integer sendingStatus) { + this.sendingStatus = sendingStatus; + } + + @Override + public String toString() { + final StringBuilder sb = new StringBuilder("{"); + sb.append("\"emailSender\":") + .append("\"").append(emailSender).append("\""); + sb.append(",\"emailReceiver\":") + .append("\"").append(emailReceiver).append("\""); + sb.append(",\"emailCc\":") + .append("\"").append(emailCc).append("\""); + sb.append(",\"emailType\":") + .append("\"").append(emailType).append("\""); + sb.append(",\"emailSubject\":") + .append("\"").append(emailSubject).append("\""); + sb.append(",\"emailContent\":") + .append("\"").append(emailContent).append("\""); + sb.append(",\"sendingStatus\":") + .append(sendingStatus); + sb.append('}'); + return sb.toString(); + } +} diff --git a/cloud-manager-sms/src/main/java/com/cm/manager/sms/service/IEmailService.java b/cloud-manager-sms/src/main/java/com/cm/manager/sms/service/IEmailService.java new file mode 100644 index 0000000..e0cac86 --- /dev/null +++ b/cloud-manager-sms/src/main/java/com/cm/manager/sms/service/IEmailService.java @@ -0,0 +1,83 @@ +package com.cm.manager.sms.service; + +import com.cm.common.exception.RemoveException; +import com.cm.common.exception.SearchException; +import com.cm.common.pojo.ListPage; +import com.cm.common.result.SuccessResult; +import com.cm.common.result.SuccessResultList; +import com.cm.manager.sms.pojo.dtos.email.EmailDTO; +import com.cm.manager.sms.pojo.dtos.sms.SmsDTO; +import com.cm.manager.sms.pojo.vos.email.EmailVO; +import com.cm.manager.sms.pojo.vos.sms.SmsVO; + +import javax.validation.constraints.Email; +import java.util.List; +import java.util.Map; + +/** + * When you feel like quitting. Think about why you started + * 当你想要放弃的时候,想想当初你为何开始 + * + * @ClassName: IEmailService + * @Description: 邮件服务 + * @Author: WangGeng + * @Date: 2020/7/31 4:19 下午 + * @Version: 1.0 + **/ +public interface IEmailService { + + /** + * 保存并发送系统邮件 + * + * @param emailVO + * @return + * @throws Exception + */ + SuccessResult saveAndSendSystemEmail(EmailVO emailVO) throws Exception; + + /** + * 删除邮件 + * + * @param ids + * @return + * @throws RemoveException + */ + SuccessResult removeEmail(String ids) throws RemoveException; + + /** + * 更新重新发送系统邮件 + * + * @param emailId + * @return + * @throws Exception + */ + SuccessResult updateReSendSystemEmailByEmailId(String emailId) throws Exception; + + /** + * 邮件详情(通过ID) + * + * @param emailId + * @return + * @throws SearchException + */ + EmailDTO getEmailById(String emailId) throws SearchException; + + /** + * 邮件列表 + * + * @param params + * @return + * @throws SearchException + */ + List listEmail(Map params) throws SearchException; + + /** + * 邮件分页列表 + * + * @param page + * @return + * @throws SearchException + */ + SuccessResultList> listPageEmail(ListPage page) throws SearchException; + +} diff --git a/cloud-manager-sms/src/main/java/com/cm/manager/sms/service/impl/EmailServiceImpl.java b/cloud-manager-sms/src/main/java/com/cm/manager/sms/service/impl/EmailServiceImpl.java new file mode 100644 index 0000000..ce03a08 --- /dev/null +++ b/cloud-manager-sms/src/main/java/com/cm/manager/sms/service/impl/EmailServiceImpl.java @@ -0,0 +1,124 @@ +package com.cm.manager.sms.service.impl; + +import com.cm.common.base.AbstractService; +import com.cm.common.exception.RemoveException; +import com.cm.common.exception.SaveException; +import com.cm.common.exception.SearchException; +import com.cm.common.exception.UpdateException; +import com.cm.common.pojo.ListPage; +import com.cm.common.result.SuccessResult; +import com.cm.common.result.SuccessResultList; +import com.cm.common.utils.HashMapUtil; +import com.cm.common.utils.email.SimpleMailSenderUtil; +import com.cm.manager.sms.config.properties.EmailProperties; +import com.cm.manager.sms.dao.email.IEmailDao; +import com.cm.manager.sms.pojo.dtos.email.EmailDTO; +import com.cm.manager.sms.pojo.vos.email.EmailVO; +import com.cm.manager.sms.service.IEmailService; +import com.github.pagehelper.PageHelper; +import com.github.pagehelper.PageInfo; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.Arrays; +import java.util.List; +import java.util.Map; + +/** + * When you feel like quitting. Think about why you started + * 当你想要放弃的时候,想想当初你为何开始 + * + * @ClassName: EmailServiceImpl + * @Description: 邮件业务 + * @Author: WangGeng + * @Date: 2020/7/31 4:19 下午 + * @Version: 1.0 + **/ +@Service +public class EmailServiceImpl extends AbstractService implements IEmailService { + + @Autowired + private EmailProperties emailProperties; + @Autowired + private IEmailDao emailDao; + + @Override + public SuccessResult saveAndSendSystemEmail(EmailVO emailVO) throws Exception { + if (!emailProperties.getActive()) { + throw new SaveException("邮件未激活"); + } + List ccPersons = emailProperties.getCcPersons(); + StringBuilder ccSB = new StringBuilder(); + for (String ccPerson : ccPersons) { + if (ccSB.length() > 0) { + ccSB.append(","); + } + ccSB.append(ccPerson); + } + emailVO.setEmailCc(ccSB.toString()); + LOG.debug("发送邮件"); + boolean sendSuccess = SimpleMailSenderUtil.sendEmail(emailProperties.getSmtp(), emailProperties.getPort(), + emailProperties.getSenderEmail(), emailProperties.getSenderPassword(), + emailVO.getEmailReceiver(), emailVO.getEmailCc(), emailVO.getEmailSubject(), + emailVO.getEmailContent(), emailVO.getEmailType()); + if (!sendSuccess) { + LOG.error("邮件发送失败"); + } + emailVO.setSendingStatus(sendSuccess ? 1 : 0); + Map params = HashMapUtil.beanToMap(emailVO); + setSaveInfoByUserId(params, "1"); + emailDao.saveEmail(params); + return new SuccessResult(); + } + + @Override + public SuccessResult removeEmail(String ids) throws RemoveException { + Map params = getHashMap(3); + params.put("emailIds", Arrays.asList(ids.split("_"))); + setUpdateInfo(params); + emailDao.removeEmail(params); + return new SuccessResult(); + } + + @Override + public SuccessResult updateReSendSystemEmailByEmailId(String emailId) throws Exception { + EmailDTO emailDTO = getEmailById(emailId); + if (emailDTO == null) { + throw new SearchException("邮件信息不存在"); + } + if (emailDTO.getSendingStatus() == 1) { + throw new UpdateException("邮件已发送成功,无需重新发送"); + } + LOG.debug("发送邮件"); + boolean sendSuccess = SimpleMailSenderUtil.sendEmail(emailProperties.getSmtp(), emailProperties.getPort(), + emailProperties.getSenderEmail(), emailProperties.getSenderPassword(), + emailDTO.getEmailReceiver(), emailDTO.getEmailCc(), emailDTO.getEmailSubject(), + emailDTO.getEmailContent(), emailDTO.getEmailType()); + Map params = getHashMap(6); + params.put("emailId", emailId); + params.put("sendingStatus", sendSuccess ? 1 : 0); + setUpdateInfo(params); + emailDao.updateEmailSendingStatus(params); + return new SuccessResult(); + } + + @Override + public EmailDTO getEmailById(String emailId) throws SearchException { + Map params = super.getHashMap(1); + params.put("emailId", emailId); + return emailDao.getEmail(params); + } + + @Override + public List listEmail(Map params) throws SearchException { + return emailDao.listEmail(params); + } + + @Override + public SuccessResultList> listPageEmail(ListPage page) throws SearchException { + PageHelper.startPage(page.getPage(), page.getRows()); + List emailDTOs = emailDao.listEmail(page.getParams()); + PageInfo pageInfo = new PageInfo<>(emailDTOs); + return new SuccessResultList<>(emailDTOs, pageInfo.getPageNum(), pageInfo.getTotal()); + } +} diff --git a/cloud-manager-sms/src/main/resources/mybatis/mapper/email/email-mapper.xml b/cloud-manager-sms/src/main/resources/mybatis/mapper/email/email-mapper.xml new file mode 100644 index 0000000..8035f1b --- /dev/null +++ b/cloud-manager-sms/src/main/resources/mybatis/mapper/email/email-mapper.xml @@ -0,0 +1,142 @@ + + + + + + + + + + + + + + + + + + INSERT INTO sms_email( + email_id, + email_sender, + email_receiver, + email_cc, + email_type, + email_subject, + email_content, + sending_status, + creator, + gmt_create, + modifier, + gmt_modified, + is_delete + ) VALUES( + #{emailId}, + #{emailSender}, + #{emailReceiver}, + #{emailCc}, + #{emailType}, + #{emailSubject}, + #{emailContent}, + #{sendingStatus}, + #{creator}, + #{gmtCreate}, + #{modifier}, + #{gmtModified}, + #{isDelete} + ) + + + + + UPDATE + sms_email + SET + is_delete = 1, + modifier = #{modifier}, + gmt_modified = #{gmtModified} + WHERE + email_id IN + + #{smsIds[${index}]} + + + + + + UPDATE + sms_email + SET + sending_status = #{sendingStatus} + WHERE + is_delete = 0 + AND + email_id = #{emailId} + + + + + + + + + \ No newline at end of file diff --git a/cloud-manager-sms/src/main/resources/mybatis/mapper/sms/sms-mapper.xml b/cloud-manager-sms/src/main/resources/mybatis/mapper/sms/sms-mapper.xml index 200911c..24fd4e2 100644 --- a/cloud-manager-sms/src/main/resources/mybatis/mapper/sms/sms-mapper.xml +++ b/cloud-manager-sms/src/main/resources/mybatis/mapper/sms/sms-mapper.xml @@ -106,7 +106,11 @@ WHERE t1.is_delete = 0 - + AND ( + t1.phone LIKE CONCAT('%', #{keywords}, '%') + OR + t1.content LIKE CONCAT('%', #{keywords}, '%') + ) AND