ReservationServiceImpl.java 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. package pl.sudra.service;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.scheduling.annotation.Scheduled;
  4. import org.springframework.stereotype.Service;
  5. import org.springframework.transaction.annotation.Transactional;
  6. import pl.sudra.domain.Bill;
  7. import pl.sudra.domain.Reservation;
  8. import pl.sudra.repository.BillRepository;
  9. import pl.sudra.repository.ReservationRepository;
  10. import java.sql.Date;
  11. import java.text.SimpleDateFormat;
  12. import java.time.LocalDate;
  13. import java.time.LocalTime;
  14. import java.time.format.DateTimeFormatter;
  15. import java.time.temporal.ChronoUnit;
  16. import java.util.List;
  17. import java.util.Objects;
  18. @Service("reservationService")
  19. @Transactional
  20. public class ReservationServiceImpl implements ReservationService {
  21. private final ReservationRepository reservationRepository;
  22. private final BillRepository billRepository;
  23. @Autowired
  24. public ReservationServiceImpl(ReservationRepository reservationRepository, BillRepository billRepository) {
  25. this.reservationRepository = reservationRepository;
  26. this.billRepository = billRepository;
  27. }
  28. @Override
  29. public void addReservation(Reservation reservation) {
  30. this.reservationRepository.save(reservation);
  31. }
  32. @Override
  33. public void editReservation(Reservation reservation) {
  34. this.reservationRepository.save(reservation);
  35. }
  36. @Override
  37. public List<Reservation> getReservations() {
  38. return this.reservationRepository.findAll();
  39. }
  40. @Override
  41. public void removeReservation(long id) {
  42. this.reservationRepository.deleteById(id);
  43. }
  44. @Override
  45. public Reservation getReservation(long id) {
  46. return this.reservationRepository.findReservationById(id);
  47. }
  48. @Override
  49. public void generateReservations(int n) {
  50. }
  51. // @Override
  52. // public void generateReservations(int n) {
  53. //// Date today = new Date();
  54. // LocalDate today = LocalDate.now();
  55. // System.out.println("n: " + n);
  56. //
  57. // Random random = new Random();
  58. //
  59. // Calendar calendar = Calendar.getInstance();
  60. //
  61. // for (int i = 0; i < n; i++) {
  62. // byte start_hour = (byte) (random.nextInt(23 - 7) + 7);
  63. // byte end_hour = (byte) (random.nextInt(24 - start_hour + 1) + start_hour + 1);
  64. //
  65. //// calendar.setTime(today);
  66. //// calendar.add(Calendar.DAY_OF_YEAR, -1 + i);
  67. //// System.out.println(calendar.getTime());
  68. //
  69. //// this.reservationRepository.save(
  70. //// new Reservation(
  71. //// (long) i,
  72. //// (long) (i % 2),
  73. //// (java.sql.Date) Date.valueOf(today.plusDays(-1 + i)),
  74. //// start_hour, end_hour
  75. //// ).setDate;
  76. //// );
  77. // }
  78. // }
  79. @Override
  80. @Transactional
  81. public List<Reservation> findReservations(long boat_id, Date date) {
  82. return this.reservationRepository.findReservationsByBoatIdAndDate(boat_id, date);
  83. }
  84. @Override
  85. public Reservation findById(long id) {
  86. return this.reservationRepository.findById(id).get();
  87. }
  88. @Override
  89. public List<Reservation> findReservationsByUserId(Long user_id) {
  90. return this.reservationRepository.findReservationsByUserId(user_id);
  91. }
  92. // every hour at 0 minutes
  93. // @Scheduled(cron = "0 0 * * * *")
  94. @Scheduled(cron = "0 * * * * *")
  95. public void updateBill() {
  96. java.util.Date currentDate = new java.util.Date();
  97. SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
  98. String formattedDate = dateFormat.format(currentDate);
  99. LocalTime date_now = LocalTime.now();
  100. int hour_now = date_now.getHour();
  101. // System.out.println("Updating reservation's Bill: ");
  102. // System.out.println("Date: " + formattedDate);
  103. // System.out.println("Hour: " + hour_now);
  104. List<Reservation> reservations = reservationRepository.findAll();
  105. reservations.forEach(reservation -> {
  106. // System.out.println("Hour: " + hour_now);
  107. // System.out.println("Start: " + reservation.getStartHour());
  108. // System.out.println("End: " + reservation.getEndHour());
  109. // System.out.println("Date: " + reservation.getDate().toString());
  110. // System.out.println("Status: " + reservation.getBill().getStatus());
  111. if (formattedDate.equals(reservation.getDate().toString()) &&
  112. hour_now == reservation.getStartHour() &&
  113. Objects.equals(reservation.getBill().getStatus(), "Confirmed")) {
  114. System.out.println("Conf -> Act");
  115. reservation.getBill().setStatus("Active");
  116. reservationRepository.saveAndFlush(reservation);
  117. } else if (hour_now == reservation.getEndHour() &&
  118. Objects.equals(reservation.getBill().getStatus(), "Active")) {
  119. System.out.println("Act -> Comp");
  120. reservation.getBill().setStatus("Completed");
  121. reservationRepository.saveAndFlush(reservation);
  122. }
  123. // LocalDate date1 = LocalDate.parse(bill.getIssueDate().toString());
  124. // LocalDate date2 = LocalDate.parse(LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")).toString());
  125. //
  126. // System.out.println(date1.toString() + ">" + date2.toString());
  127. //
  128. // if (ChronoUnit.DAYS.between(
  129. // LocalDate.parse(bill.getIssueDate().toString()),
  130. // LocalDate.parse(LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")).toString())) > 30
  131. // && date1.isBefore(date2)) {
  132. // bill.setStatus("CANCELLED");
  133. // billRepository.saveAndFlush(bill);
  134. // System.out.println(bill.getId());
  135. // }
  136. });
  137. }
  138. }