diff --git a/src/main/java/com/example/fiurinee/FiurineeApplication.java b/src/main/java/com/example/fiurinee/FiurineeApplication.java index a9d33ba..68329e2 100644 --- a/src/main/java/com/example/fiurinee/FiurineeApplication.java +++ b/src/main/java/com/example/fiurinee/FiurineeApplication.java @@ -5,8 +5,10 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import com.example.fiurinee.domain.flower.service.FlowerService; +import org.springframework.scheduling.annotation.EnableScheduling; @SpringBootApplication +@EnableScheduling public class FiurineeApplication implements CommandLineRunner { @Autowired diff --git a/src/main/java/com/example/fiurinee/domain/mail/AnniversarySchedular.java b/src/main/java/com/example/fiurinee/domain/mail/AnniversarySchedular.java new file mode 100644 index 0000000..5f71797 --- /dev/null +++ b/src/main/java/com/example/fiurinee/domain/mail/AnniversarySchedular.java @@ -0,0 +1,47 @@ +package com.example.fiurinee.domain.mail; + +import com.example.fiurinee.domain.anniversary.entity.Anniversary; +import com.example.fiurinee.domain.anniversary.service.AnniversaryService; +import com.example.fiurinee.domain.member.entity.Member; +import com.example.fiurinee.domain.member.service.MemberService; +import jakarta.mail.MessagingException; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Service; + +import java.util.List; +import java.util.Map; + +@Service +public class AnniversarySchedular { + @Autowired + private AnniversaryService anniversaryService; + + @Autowired + private MailService mailService; + + @Autowired + private MemberService memberService; + + @Scheduled(cron = "0 27 23 * * *") + public void sendDDayZeroAnniversaryEmails() { + List members = memberService.findAll(); + for (Member member : members) { + List anniversaries = member.getAnniversaries(); + for (Anniversary anniversary : anniversaries) { + List> allDDays = anniversaryService.calculateDDay(anniversary); + for (Map dDay : allDDays) { + for (Map.Entry entry : dDay.entrySet()) { + if (entry.getValue() == 0) { + try { + mailService.sendAnniversaryEmail(anniversary.getMember(), anniversary); + } catch (MessagingException e) { + e.printStackTrace(); + } + } + } + } + } + } + } +} diff --git a/src/main/java/com/example/fiurinee/domain/member/service/MemberService.java b/src/main/java/com/example/fiurinee/domain/member/service/MemberService.java index 596a666..8c44ede 100644 --- a/src/main/java/com/example/fiurinee/domain/member/service/MemberService.java +++ b/src/main/java/com/example/fiurinee/domain/member/service/MemberService.java @@ -9,6 +9,8 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import java.util.List; + @Service @Transactional(readOnly = true) @RequiredArgsConstructor @@ -37,5 +39,9 @@ public MemberResponseDTO getMemberDtoById(Long id) { return MemberResponseDTO.of(member); } + public List findAll() { + return memberRepository.findAll(); + } + }