|
@@ -1,25 +1,33 @@
|
|
|
package pl.sudra.service;
|
|
package pl.sudra.service;
|
|
|
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
+import org.springframework.scheduling.annotation.Scheduled;
|
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
+import pl.sudra.domain.Bill;
|
|
|
import pl.sudra.domain.Reservation;
|
|
import pl.sudra.domain.Reservation;
|
|
|
|
|
+import pl.sudra.repository.BillRepository;
|
|
|
import pl.sudra.repository.ReservationRepository;
|
|
import pl.sudra.repository.ReservationRepository;
|
|
|
|
|
|
|
|
-import java.time.LocalDate;
|
|
|
|
|
-import java.util.Calendar;
|
|
|
|
|
import java.sql.Date;
|
|
import java.sql.Date;
|
|
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
|
|
+import java.time.LocalDate;
|
|
|
|
|
+import java.time.LocalTime;
|
|
|
|
|
+import java.time.format.DateTimeFormatter;
|
|
|
|
|
+import java.time.temporal.ChronoUnit;
|
|
|
import java.util.List;
|
|
import java.util.List;
|
|
|
-import java.util.Random;
|
|
|
|
|
|
|
+import java.util.Objects;
|
|
|
|
|
|
|
|
@Service("reservationService")
|
|
@Service("reservationService")
|
|
|
@Transactional
|
|
@Transactional
|
|
|
public class ReservationServiceImpl implements ReservationService {
|
|
public class ReservationServiceImpl implements ReservationService {
|
|
|
private final ReservationRepository reservationRepository;
|
|
private final ReservationRepository reservationRepository;
|
|
|
|
|
+ private final BillRepository billRepository;
|
|
|
|
|
|
|
|
@Autowired
|
|
@Autowired
|
|
|
- public ReservationServiceImpl(ReservationRepository reservationRepository) {
|
|
|
|
|
|
|
+ public ReservationServiceImpl(ReservationRepository reservationRepository, BillRepository billRepository) {
|
|
|
this.reservationRepository = reservationRepository;
|
|
this.reservationRepository = reservationRepository;
|
|
|
|
|
+ this.billRepository = billRepository;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
@@ -91,5 +99,59 @@ public class ReservationServiceImpl implements ReservationService {
|
|
|
public Reservation findById(long id) {
|
|
public Reservation findById(long id) {
|
|
|
return this.reservationRepository.findById(id).get();
|
|
return this.reservationRepository.findById(id).get();
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public List<Reservation> findReservationsByUserId(Long user_id) {
|
|
|
|
|
+ return this.reservationRepository.findReservationsByUserId(user_id);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // every hour at 0 minutes
|
|
|
|
|
+// @Scheduled(cron = "0 0 * * * *")
|
|
|
|
|
+ @Scheduled(cron = "0 * * * * *")
|
|
|
|
|
+ public void updateBill() {
|
|
|
|
|
+ java.util.Date currentDate = new java.util.Date();
|
|
|
|
|
+ SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
|
|
+ String formattedDate = dateFormat.format(currentDate);
|
|
|
|
|
+ LocalTime date_now = LocalTime.now();
|
|
|
|
|
+ int hour_now = date_now.getHour();
|
|
|
|
|
+// System.out.println("Updating reservation's Bill: ");
|
|
|
|
|
+// System.out.println("Date: " + formattedDate);
|
|
|
|
|
+// System.out.println("Hour: " + hour_now);
|
|
|
|
|
+ List<Reservation> reservations = reservationRepository.findAll();
|
|
|
|
|
+ reservations.forEach(reservation -> {
|
|
|
|
|
+// System.out.println("Hour: " + hour_now);
|
|
|
|
|
+// System.out.println("Start: " + reservation.getStartHour());
|
|
|
|
|
+// System.out.println("End: " + reservation.getEndHour());
|
|
|
|
|
+// System.out.println("Date: " + reservation.getDate().toString());
|
|
|
|
|
+// System.out.println("Status: " + reservation.getBill().getStatus());
|
|
|
|
|
+
|
|
|
|
|
+ if (formattedDate.equals(reservation.getDate().toString()) &&
|
|
|
|
|
+ hour_now == reservation.getStartHour() &&
|
|
|
|
|
+ Objects.equals(reservation.getBill().getStatus(), "Confirmed")) {
|
|
|
|
|
+ System.out.println("Conf -> Act");
|
|
|
|
|
+ reservation.getBill().setStatus("Active");
|
|
|
|
|
+ reservationRepository.saveAndFlush(reservation);
|
|
|
|
|
+ } else if (hour_now == reservation.getEndHour() &&
|
|
|
|
|
+ Objects.equals(reservation.getBill().getStatus(), "Active")) {
|
|
|
|
|
+ System.out.println("Act -> Comp");
|
|
|
|
|
+ reservation.getBill().setStatus("Completed");
|
|
|
|
|
+ reservationRepository.saveAndFlush(reservation);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+// LocalDate date1 = LocalDate.parse(bill.getIssueDate().toString());
|
|
|
|
|
+// LocalDate date2 = LocalDate.parse(LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")).toString());
|
|
|
|
|
+//
|
|
|
|
|
+// System.out.println(date1.toString() + ">" + date2.toString());
|
|
|
|
|
+//
|
|
|
|
|
+// if (ChronoUnit.DAYS.between(
|
|
|
|
|
+// LocalDate.parse(bill.getIssueDate().toString()),
|
|
|
|
|
+// LocalDate.parse(LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")).toString())) > 30
|
|
|
|
|
+// && date1.isBefore(date2)) {
|
|
|
|
|
+// bill.setStatus("CANCELLED");
|
|
|
|
|
+// billRepository.saveAndFlush(bill);
|
|
|
|
|
+// System.out.println(bill.getId());
|
|
|
|
|
+// }
|
|
|
|
|
+ });
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|