From 4d1bf3c200ec64700559912b010970b71df2c8a3 Mon Sep 17 00:00:00 2001 From: chengpengxiang <15503679582@163.com> Date: Mon, 16 Dec 2024 17:32:25 +0800 Subject: [PATCH] fix: fix course and share conflict --- .../dl/officialsite/mail/EmailService.java | 25 ++++- .../com/dl/officialsite/sharing/Share.java | 4 + .../officialsite/sharing/SharingService.java | 91 +++++++++++++------ 3 files changed, 93 insertions(+), 27 deletions(-) diff --git a/src/main/java/com/dl/officialsite/mail/EmailService.java b/src/main/java/com/dl/officialsite/mail/EmailService.java index 6cf213c..aef9ac8 100644 --- a/src/main/java/com/dl/officialsite/mail/EmailService.java +++ b/src/main/java/com/dl/officialsite/mail/EmailService.java @@ -9,6 +9,7 @@ import javax.mail.internet.MimeBodyPart; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMultipart; +import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.mail.SimpleMailMessage; @@ -18,6 +19,7 @@ @Component +@Slf4j public class EmailService { @@ -28,7 +30,7 @@ public class EmailService { private String from; public void sendSimpleMessage( - String to, String subject, String text) { + String to, String subject, String text) { SimpleMailMessage message = new SimpleMailMessage(); message.setFrom(from); @@ -60,6 +62,27 @@ public void sendMail(String to, String subject, String text) { emailSender.send(message); } + public void sendMail(List to, String subject, String text, List cc) { + SimpleMailMessage message = new SimpleMailMessage(); + message.setFrom(from); + + // 将 List 转换为 String[] 并设置多个收件人 + message.setTo(to.toArray(new String[0])); + + // 如果 cc 不为空,将其转换为 String[] 并设置多个抄送人 + if (cc != null && !cc.isEmpty()) { + message.setCc(cc.toArray(new String[0])); + } + + message.setSubject(subject); + message.setText(text); + + // 发送邮件 + log.info("Sending email to: {}, tile: {}, content: {}", to, subject, text); + emailSender.send(message); + } + + public void sendMailWithFile(String to, String subject, String text, MultipartFile file) throws Exception { //1.创建一封邮件的实例对象 diff --git a/src/main/java/com/dl/officialsite/sharing/Share.java b/src/main/java/com/dl/officialsite/sharing/Share.java index 428d5ea..ab57aad 100644 --- a/src/main/java/com/dl/officialsite/sharing/Share.java +++ b/src/main/java/com/dl/officialsite/sharing/Share.java @@ -154,4 +154,8 @@ public class Share { @Transient private String shareCount; + + + @Column(columnDefinition = "MEDIUMTEXT") + private String outline; } diff --git a/src/main/java/com/dl/officialsite/sharing/SharingService.java b/src/main/java/com/dl/officialsite/sharing/SharingService.java index 5432b39..1aa76b8 100644 --- a/src/main/java/com/dl/officialsite/sharing/SharingService.java +++ b/src/main/java/com/dl/officialsite/sharing/SharingService.java @@ -1,6 +1,5 @@ package com.dl.officialsite.sharing; -import cn.hutool.core.lang.Assert; import com.dl.officialsite.bot.constant.BotEnum; import com.dl.officialsite.bot.constant.ChannelEnum; import com.dl.officialsite.bot.event.EventNotify; @@ -22,6 +21,18 @@ import com.dl.officialsite.sharing.model.resp.ShareTagResp; import com.dl.officialsite.team.TeamService; import com.google.common.collect.Lists; +import java.math.BigInteger; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Date; +import java.util.LinkedList; +import java.util.List; +import java.util.Objects; +import java.util.Optional; +import java.util.stream.Collectors; +import java.util.stream.Stream; +import javax.persistence.criteria.Predicate; +import javax.servlet.http.HttpServletRequest; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; @@ -32,16 +43,6 @@ import org.springframework.data.jpa.domain.Specification; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; - -import javax.persistence.criteria.Predicate; -import javax.servlet.http.HttpServletRequest; -import java.math.BigInteger; -import java.text.SimpleDateFormat; -import java.util.ArrayList; -import java.util.LinkedList; -import java.util.List; -import java.util.Objects; -import java.util.Optional; import org.springframework.util.StringUtils; @Service @@ -75,24 +76,62 @@ public SharingService(EmailService emailService, ApplicationContext applicationC @Transactional(rollbackFor = Exception.class) public Share createSharing(Share share, String address) { - /** - * 登陆用户转member - */ -// SessionUserInfo userInfo = HttpSessionUtils.getMember(request.getSession()); -// Preconditions.checkState(userInfo != null, "User info not null"); -// Optional memberOpt = this.memberRepository.findByAddress(userInfo.getAddress()); -// Member member = memberOpt.get(); -// if(member.getId() != req.) + // 保存分享记录 share = sharingRepository.save(share); - Member creatorInfo = memberRepository.findByAddress(address).orElse(null); - Assert.isNull(creatorInfo, "not found member by address"); + + // 查找分享创建者的信息 + Member creatorInfo = memberRepository.findByAddress(address) + .orElseThrow(() -> new IllegalArgumentException("Member not found by address: " + address)); + + // 格式化日期 + String formattedDate = formatDate(share.getDate()); + + // 发布事件通知 + publishShareCreationEvent(creatorInfo, share, formattedDate); + + // 准备邮件内容 + String emailTitle = "New Sharing Created"; + String emailContent = createEmailContent(share, creatorInfo); + + // 定义邮件收件人列表 + List toAddressList = Stream.of( + "0xstan.com@gmail.com", "yanyanho126@gmail.com", "longdacao2@gmail.com" + ).collect(Collectors.toList()); + + // 发送邮件 + emailService.sendMail(toAddressList, emailTitle, emailContent, null); + + return share; + } + + private String formatDate(Date date) { + // 格式化日期为字符串 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); - String formatDate = format.format(share.getDate()); - applicationContext.publishEvent(new EventNotify(Member.class, BotEnum.TELEGRAM, + return format.format(date); + } + + private void publishShareCreationEvent(Member creatorInfo, Share share, String formattedDate) { + // 发布创建分享事件的通知 + applicationContext.publishEvent(new EventNotify( + Member.class, + BotEnum.TELEGRAM, ChannelEnum.SHARING, - NotifyMessageFactory.sharingMessage("👏Create New Share👏", creatorInfo.getNickName(), share.getTheme(), - formatDate))); - return share; + NotifyMessageFactory.sharingMessage( + "👏Create New Share👏", + creatorInfo.getNickName(), + share.getTheme(), + formattedDate + ) + )); + } + + private String createEmailContent(Share share, Member creatorInfo) { + // 构造邮件内容 + return String.format( + "Have a new sharing\n👉👉👉Theme: %s👈👈👈\nCreator: %s", + share.getTheme(), + creatorInfo.getNickName() + ); }