Skip to content

Commit

Permalink
Merge pull request #309 from cheng521521/main
Browse files Browse the repository at this point in the history
fix: fix course and share conflict
  • Loading branch information
cheng521521 authored Dec 16, 2024
2 parents 935d9e0 + 4d1bf3c commit 5edc159
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 27 deletions.
25 changes: 24 additions & 1 deletion src/main/java/com/dl/officialsite/mail/EmailService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -18,6 +19,7 @@


@Component
@Slf4j
public class EmailService {


Expand All @@ -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);
Expand Down Expand Up @@ -60,6 +62,27 @@ public void sendMail(String to, String subject, String text) {
emailSender.send(message);
}

public void sendMail(List<String> to, String subject, String text, List<String> cc) {
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom(from);

// 将 List<String> 转换为 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.创建一封邮件的实例对象
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/dl/officialsite/sharing/Share.java
Original file line number Diff line number Diff line change
Expand Up @@ -154,4 +154,8 @@ public class Share {

@Transient
private String shareCount;


@Column(columnDefinition = "MEDIUMTEXT")
private String outline;
}
91 changes: 65 additions & 26 deletions src/main/java/com/dl/officialsite/sharing/SharingService.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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<Member> 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<String> toAddressList = Stream.of(
"[email protected]", "[email protected]", "[email protected]"
).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()
);
}


Expand Down

0 comments on commit 5edc159

Please sign in to comment.