| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- package pl.sudra.service;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.scheduling.annotation.Scheduled;
- import org.springframework.stereotype.Service;
- import org.springframework.transaction.annotation.Transactional;
- import pl.sudra.domain.Bill;
- import pl.sudra.domain.Reservation;
- import pl.sudra.repository.BillRepository;
- import pl.sudra.repository.ReservationRepository;
- 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.Objects;
- @Service("reservationService")
- @Transactional
- public class ReservationServiceImpl implements ReservationService {
- private final ReservationRepository reservationRepository;
- private final BillRepository billRepository;
- @Autowired
- public ReservationServiceImpl(ReservationRepository reservationRepository, BillRepository billRepository) {
- this.reservationRepository = reservationRepository;
- this.billRepository = billRepository;
- }
- @Override
- public void addReservation(Reservation reservation) {
- this.reservationRepository.save(reservation);
- }
- @Override
- public void editReservation(Reservation reservation) {
- this.reservationRepository.save(reservation);
- }
- @Override
- public List<Reservation> getReservations() {
- return this.reservationRepository.findAll();
- }
- @Override
- public void removeReservation(long id) {
- this.reservationRepository.deleteById(id);
- }
- @Override
- public Reservation getReservation(long id) {
- return this.reservationRepository.findReservationById(id);
- }
- @Override
- public void generateReservations(int n) {
- }
- // @Override
- // public void generateReservations(int n) {
- //// Date today = new Date();
- // LocalDate today = LocalDate.now();
- // System.out.println("n: " + n);
- //
- // Random random = new Random();
- //
- // Calendar calendar = Calendar.getInstance();
- //
- // for (int i = 0; i < n; i++) {
- // byte start_hour = (byte) (random.nextInt(23 - 7) + 7);
- // byte end_hour = (byte) (random.nextInt(24 - start_hour + 1) + start_hour + 1);
- //
- //// calendar.setTime(today);
- //// calendar.add(Calendar.DAY_OF_YEAR, -1 + i);
- //// System.out.println(calendar.getTime());
- //
- //// this.reservationRepository.save(
- //// new Reservation(
- //// (long) i,
- //// (long) (i % 2),
- //// (java.sql.Date) Date.valueOf(today.plusDays(-1 + i)),
- //// start_hour, end_hour
- //// ).setDate;
- //// );
- // }
- // }
- @Override
- @Transactional
- public List<Reservation> findReservations(long boat_id, Date date) {
- return this.reservationRepository.findReservationsByBoatIdAndDate(boat_id, date);
- }
- @Override
- public Reservation findById(long id) {
- 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());
- // }
- });
- }
- }
|