Ignore:
Timestamp:
01/05/22 15:57:29 (3 years ago)
Author:
NikolaCenevski <cenevskinikola@…>
Branches:
master
Children:
6fa3d09
Parents:
881a233
Message:

part 2

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/main/java/it/finki/charitable/util/AutomaticEvents.java

    r881a233 rb8dc761  
    22
    33import it.finki.charitable.entities.AppUser;
     4import it.finki.charitable.entities.DonationPost;
     5import it.finki.charitable.entities.EmailMessage;
    46import it.finki.charitable.security.ConfirmationToken;
    5 import it.finki.charitable.services.ConfirmationTokenService;
    6 import it.finki.charitable.services.UserService;
     7import it.finki.charitable.services.*;
     8import org.springframework.mail.SimpleMailMessage;
    79import org.springframework.scheduling.annotation.Scheduled;
    810import org.springframework.stereotype.Component;
    911
     12import java.time.Duration;
     13import java.time.LocalDate;
    1014import java.util.List;
     15import java.util.stream.Collectors;
    1116
    1217@Component
     
    1520    private final UserService userService;
    1621    private final ConfirmationTokenService confirmationTokenService;
     22    private final DonationPostService donationPostService;
     23    private final EmailMessageService emailMessageService;
     24    private final EmailService emailService;
    1725
    18     public AutomaticEvents(UserService userService, ConfirmationTokenService confirmationTokenService) {
     26    public AutomaticEvents(UserService userService, ConfirmationTokenService confirmationTokenService, DonationPostService donationPostService, EmailMessageService emailMessageService, EmailService emailService) {
    1927        this.userService = userService;
    2028        this.confirmationTokenService = confirmationTokenService;
     29        this.donationPostService = donationPostService;
     30        this.emailMessageService = emailMessageService;
     31        this.emailService = emailService;
    2132    }
    2233
     
    3243        }
    3344    }
     45
     46    @Scheduled(cron = "0 0 0 * * *")
     47    public void setRisk() {
     48        System.out.println("cron");
     49        List<DonationPost> donationPosts = donationPostService.findAll();
     50        donationPosts = donationPosts.stream().filter(post -> {
     51            long daysToEnd = Duration.between(LocalDate.now().atTime(0, 0, 0), post.getDateDue().atTime(0, 0, 0)).toDays();
     52            long totalDays = Duration.between(post.getCreatedAt().atTime(0, 0, 0), post.getDateDue().atTime(0, 0, 0)).toDays();
     53            System.out.println(daysToEnd + " " + totalDays);
     54
     55            if(totalDays < 10)
     56                return true;
     57
     58            return (daysToEnd * 1f/totalDays) * 100 < 75;
     59        }).collect(Collectors.toList());
     60
     61        donationPosts.forEach(post -> {
     62            float dailyAverage = post.getTotalFundsCollected() / (Duration.between(post.getCreatedAt().atTime(0, 0, 0), LocalDate.now().atTime(0, 0, 0)).toDays() + 1);
     63            float neededAverage = (post.getFundsNeeded() - post.getTotalFundsCollected()) / (Duration.between(LocalDate.now().atTime(0, 0, 0), post.getDateDue().atTime(0, 0, 0)).toDays()+1);
     64
     65            System.out.println(dailyAverage + " " + neededAverage);
     66            int risk = (int) (dailyAverage / neededAverage * 100);
     67
     68            if(risk > 100) {
     69                risk = 100;
     70            }
     71
     72            if(Duration.between(LocalDate.now().atTime(0, 0, 0), post.getDateDue().atTime(0, 0, 0)).toDays() == 0) {
     73                risk = 0;
     74            }
     75
     76            if(post.getFundsNeeded() <= post.getTotalFundsCollected()) {
     77                risk = 101;
     78            }
     79
     80            post.setRiskFactor(risk);
     81            donationPostService.save(post);
     82        });
     83    }
     84
     85    @Scheduled(cron = "0 0 * * * *")
     86    public void sendMessages() {
     87        List<EmailMessage> messages = emailMessageService.findAll();
     88        for(EmailMessage message: messages) {
     89            SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
     90            simpleMailMessage.setTo(message.getSendTo());
     91            simpleMailMessage.setSubject(message.getSubject());
     92            simpleMailMessage.setText(message.getText());
     93            emailService.sendMail(simpleMailMessage);
     94            emailMessageService.delete(message);
     95        }
     96    }
    3497}
Note: See TracChangeset for help on using the changeset viewer.