- make scheduler as a service

- reformat code
- fix cyrillic letter
This commit is contained in:
Anton Romanov 2018-10-27 23:25:27 +04:00
parent 7388b2cb27
commit afd854e0cb

View File

@ -3,32 +3,25 @@ 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 org.springframework.stereotype.Service;
import ru.ulstu.paper.model.Paper;
import ru.ulstu.paper.repository.PaperRepository;
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 {
@Service
public class DeadlineScheduler {
private final Logger log = LoggerFactory.getLogger(UserScheduler.class);
private final PaperRepository paperRepository;
private final MailService mailService;
public DeadlineSсheduler(PaperRepository paperRepository, MailService mailService) {
public DeadlineScheduler(PaperRepository paperRepository, MailService mailService) {
this.paperRepository = paperRepository;
this.mailService = mailService;
}
@ -37,10 +30,10 @@ public class DeadlineSсheduler {
public void sendMessageDeadlineBeforeWeek() {
log.debug("DeadlineSсheduler.sendMessageDeadlineBeforeWeek started");
List<Paper> allPapers = paperRepository.findAll();
for (Paper element: allPapers) {
for (Paper element : allPapers) {
Calendar c = Calendar.getInstance();
c.add(Calendar.DAY_OF_YEAR, 7);
if(c.getTime().compareTo(element.getDeadlineDate()) < 0 ) {
if (c.getTime().compareTo(element.getDeadlineDate()) < 0) {
List<User> users = element.getAuthors();
for (User user : users) {
mailService.sendEmail(user.getEmail(), "Приближается срок сдачи статьи",
@ -50,14 +43,15 @@ public class DeadlineSсheduler {
}
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) {
for (Paper element : allPapers) {
Calendar c = Calendar.getInstance();
c.add(Calendar.DAY_OF_YEAR, 7);
if(c.getTime().compareTo(element.getDeadlineDate()) >= 0 ) {
if (c.getTime().compareTo(element.getDeadlineDate()) >= 0) {
List<User> users = element.getAuthors();
for (User user : users) {
mailService.sendEmail(user.getEmail(), "Приближается срок сдачи статьи",