Changeset b8dc761 for src/main/java/it/finki/charitable/util
- Timestamp:
- 01/05/22 15:57:29 (3 years ago)
- Branches:
- master
- Children:
- 6fa3d09
- Parents:
- 881a233
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/main/java/it/finki/charitable/util/AutomaticEvents.java
r881a233 rb8dc761 2 2 3 3 import it.finki.charitable.entities.AppUser; 4 import it.finki.charitable.entities.DonationPost; 5 import it.finki.charitable.entities.EmailMessage; 4 6 import it.finki.charitable.security.ConfirmationToken; 5 import it.finki.charitable.services. ConfirmationTokenService;6 import it.finki.charitable.services.UserService;7 import it.finki.charitable.services.*; 8 import org.springframework.mail.SimpleMailMessage; 7 9 import org.springframework.scheduling.annotation.Scheduled; 8 10 import org.springframework.stereotype.Component; 9 11 12 import java.time.Duration; 13 import java.time.LocalDate; 10 14 import java.util.List; 15 import java.util.stream.Collectors; 11 16 12 17 @Component … … 15 20 private final UserService userService; 16 21 private final ConfirmationTokenService confirmationTokenService; 22 private final DonationPostService donationPostService; 23 private final EmailMessageService emailMessageService; 24 private final EmailService emailService; 17 25 18 public AutomaticEvents(UserService userService, ConfirmationTokenService confirmationTokenService ) {26 public AutomaticEvents(UserService userService, ConfirmationTokenService confirmationTokenService, DonationPostService donationPostService, EmailMessageService emailMessageService, EmailService emailService) { 19 27 this.userService = userService; 20 28 this.confirmationTokenService = confirmationTokenService; 29 this.donationPostService = donationPostService; 30 this.emailMessageService = emailMessageService; 31 this.emailService = emailService; 21 32 } 22 33 … … 32 43 } 33 44 } 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 } 34 97 }
Note:
See TracChangeset
for help on using the changeset viewer.