add deadlineSсheduler

This commit is contained in:
Alyona 2018-10-27 22:32:51 +04:00
parent 43740981f2
commit 61c19e5b87

View File

@ -0,0 +1,70 @@
package ru.ulstu.paper.service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import ru.ulstu.core.util.DateUtils;
import ru.ulstu.paper.model.PaperDto;
import ru.ulstu.paper.repository.PaperRepository;
import ru.ulstu.paper.service.PaperService;
import ru.ulstu.paper.model.Paper;
import ru.ulstu.user.model.User;
import ru.ulstu.user.model.UserDto;
import ru.ulstu.user.repository.UserRepository;
import ru.ulstu.user.scheduler.UserScheduler;
import ru.ulstu.user.service.MailService;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Calendar;
import java.util.List;
public class DeadlineSсheduler {
private final Logger log = LoggerFactory.getLogger(UserScheduler.class);
private final PaperRepository paperRepository;
private final MailService mailService;
public DeadlineSсheduler(PaperRepository paperRepository, MailService mailService) {
this.paperRepository = paperRepository;
this.mailService = mailService;
}
@Scheduled(cron = "0 0 8 * 1 ?")
public void sendMessageDeadlineBeforeWeek() {
log.debug("DeadlineSсheduler.sendMessageDeadlineBeforeWeek started");
List<Paper> allPapers = paperRepository.findAll();
for (Paper element: allPapers) {
Calendar c = Calendar.getInstance();
c.add(Calendar.DAY_OF_YEAR, 7);
if(c.getTime().compareTo(element.getDeadlineDate()) < 0 ) {
List<User> users = element.getAuthors();
for (User user : users) {
mailService.sendEmail(user.getEmail(), "Приближается срок сдачи статьи",
"Срок сдачи статьи " + element.getTitle() + " " + element.getDeadlineDate().toString());
}
}
}
log.debug("DeadlineSсheduler.sendMessageDeadlineBeforeWeek finished");
}
@Scheduled(cron = "0 0 8 * * ?")
public void sendMessageDeadlineAfterWeek() {
log.debug("DeadlineSсheduler.sendMessageDeadlineAfterWeek started");
List<Paper> allPapers = paperRepository.findAll();
for (Paper element: allPapers) {
Calendar c = Calendar.getInstance();
c.add(Calendar.DAY_OF_YEAR, 7);
if(c.getTime().compareTo(element.getDeadlineDate()) >= 0 ) {
List<User> users = element.getAuthors();
for (User user : users) {
mailService.sendEmail(user.getEmail(), "Приближается срок сдачи статьи",
"Срок сдачи статьи " + element.getTitle() + " " + element.getDeadlineDate().toString());
}
}
}
log.debug("DeadlineSсheduler.sendMessageDeadlineAfterWeek finished");
}
}