1 | package it.finki.charitable.util;
|
---|
2 |
|
---|
3 | import it.finki.charitable.entities.AppUser;
|
---|
4 | import it.finki.charitable.entities.DonationPost;
|
---|
5 | import it.finki.charitable.entities.EmailMessage;
|
---|
6 | import it.finki.charitable.security.ConfirmationToken;
|
---|
7 | import it.finki.charitable.services.*;
|
---|
8 | import org.springframework.mail.SimpleMailMessage;
|
---|
9 | import org.springframework.scheduling.annotation.Scheduled;
|
---|
10 | import org.springframework.stereotype.Component;
|
---|
11 |
|
---|
12 | import java.time.Duration;
|
---|
13 | import java.time.LocalDate;
|
---|
14 | import java.time.LocalDateTime;
|
---|
15 | import java.util.List;
|
---|
16 | import java.util.stream.Collectors;
|
---|
17 |
|
---|
18 | @Component
|
---|
19 | public class AutomaticEvents {
|
---|
20 |
|
---|
21 | private final UserService userService;
|
---|
22 | private final ConfirmationTokenService confirmationTokenService;
|
---|
23 | private final DonationPostService donationPostService;
|
---|
24 | private final EmailMessageService emailMessageService;
|
---|
25 | private final EmailService emailService;
|
---|
26 |
|
---|
27 | public AutomaticEvents(UserService userService, ConfirmationTokenService confirmationTokenService, DonationPostService donationPostService, EmailMessageService emailMessageService, EmailService emailService) {
|
---|
28 | this.userService = userService;
|
---|
29 | this.confirmationTokenService = confirmationTokenService;
|
---|
30 | this.donationPostService = donationPostService;
|
---|
31 | this.emailMessageService = emailMessageService;
|
---|
32 | this.emailService = emailService;
|
---|
33 | }
|
---|
34 |
|
---|
35 | @Scheduled(cron = "0 0 0 * * *")
|
---|
36 | public void deleteNonEnabledUsers() {
|
---|
37 | List<ConfirmationToken> toDelete = confirmationTokenService.getAll();
|
---|
38 | for(ConfirmationToken token : toDelete) {
|
---|
39 | AppUser user = token.getUser();
|
---|
40 | confirmationTokenService.deleteConfirmationToken(token);
|
---|
41 | if(!user.getEnabled()) {
|
---|
42 | userService.deleteUser(user);
|
---|
43 | }
|
---|
44 | }
|
---|
45 | }
|
---|
46 |
|
---|
47 | @Scheduled(cron = "0 0 0 * * *")
|
---|
48 | public void setRisk() {
|
---|
49 | System.out.println("cron");
|
---|
50 | List<DonationPost> donationPosts = donationPostService.findAll();
|
---|
51 | donationPosts = donationPosts.stream().filter(post -> {
|
---|
52 | long daysToEnd = Duration.between(LocalDate.now().atTime(0, 0, 0), post.getDateDue().atTime(0, 0, 0)).toDays();
|
---|
53 | long totalDays = Duration.between(post.getCreatedAt().atTime(0, 0, 0), post.getDateDue().atTime(0, 0, 0)).toDays();
|
---|
54 | System.out.println(daysToEnd + " " + totalDays);
|
---|
55 |
|
---|
56 | if(totalDays < 10)
|
---|
57 | return true;
|
---|
58 |
|
---|
59 | return (daysToEnd * 1f/totalDays) * 100 < 75;
|
---|
60 | }).collect(Collectors.toList());
|
---|
61 |
|
---|
62 | donationPosts.forEach(post -> {
|
---|
63 | int risk;
|
---|
64 | if(post.getFundsNeeded() <= post.getTotalFundsCollected()) {
|
---|
65 | risk = 102;
|
---|
66 | }
|
---|
67 | else
|
---|
68 | {
|
---|
69 | if (LocalDate.now().isAfter(post.getDateDue()))
|
---|
70 | {
|
---|
71 | risk=0;
|
---|
72 | }
|
---|
73 | else
|
---|
74 | {
|
---|
75 | float dailyAverage = post.getTotalFundsCollected() / (Duration.between(post.getCreatedAt().atTime(0, 0, 0), LocalDate.now().atTime(0, 0, 0)).toDays() + 1);
|
---|
76 | float neededAverage = (post.getFundsNeeded() - post.getTotalFundsCollected()) / (Duration.between(LocalDate.now().atTime(0, 0, 0), post.getDateDue().atTime(0, 0, 0)).toDays()+1);
|
---|
77 |
|
---|
78 | if(Duration.between(LocalDate.now().atTime(0, 0, 0), post.getDateDue().atTime(0, 0, 0)).toDays() == 0) {
|
---|
79 | float hour=(float) LocalDateTime.now().getHour();
|
---|
80 | float mins=(float) LocalDateTime.now().getMinute();
|
---|
81 | hour=hour+(mins/(float)60);
|
---|
82 | float hourlyAverage=(dailyAverage/(float)24);
|
---|
83 | float neededhourlyAverage=(post.getFundsNeeded() - post.getTotalFundsCollected())/((float)24-hour);
|
---|
84 | risk = (int) (hourlyAverage/neededhourlyAverage*100);
|
---|
85 | if (risk>100)
|
---|
86 | {
|
---|
87 | risk=100;
|
---|
88 | }
|
---|
89 | }
|
---|
90 | else
|
---|
91 | {
|
---|
92 | System.out.println(dailyAverage + " " + neededAverage);
|
---|
93 | risk = (int) (dailyAverage / neededAverage * 100);
|
---|
94 |
|
---|
95 | if(risk > 100) {
|
---|
96 | risk = 100;
|
---|
97 | }
|
---|
98 | }
|
---|
99 | }
|
---|
100 | }
|
---|
101 | post.setRiskFactor(risk);
|
---|
102 | donationPostService.save(post);
|
---|
103 | });
|
---|
104 | }
|
---|
105 |
|
---|
106 | @Scheduled(cron = "0 0 * * * *")
|
---|
107 | public void sendMessages() {
|
---|
108 | List<EmailMessage> messages = emailMessageService.findAll();
|
---|
109 | for(EmailMessage message: messages) {
|
---|
110 | SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
|
---|
111 | simpleMailMessage.setTo(message.getSendTo());
|
---|
112 | simpleMailMessage.setSubject(message.getSubject());
|
---|
113 | simpleMailMessage.setText(message.getText());
|
---|
114 | emailService.sendMail(simpleMailMessage);
|
---|
115 | emailMessageService.delete(message);
|
---|
116 | }
|
---|
117 | }
|
---|
118 | }
|
---|