diff --git a/src/main/java/org/ntlab/citrusserver/repositories/ScheduleManager.java b/src/main/java/org/ntlab/citrusserver/repositories/ScheduleManager.java index 30cf7f2..fdaea51 100644 --- a/src/main/java/org/ntlab/citrusserver/repositories/ScheduleManager.java +++ b/src/main/java/org/ntlab/citrusserver/repositories/ScheduleManager.java @@ -6,9 +6,14 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Repository; +import java.text.SimpleDateFormat; +import java.time.LocalDate; import java.time.format.DateTimeFormatter; -import java.util.Calendar; -import java.util.HashMap; +import java.time.temporal.ChronoUnit; +import java.util.*; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; @Repository public class ScheduleManager implements IAccountListener, IBookListener{ @@ -23,10 +28,10 @@ this.bookManager.addListener(this); accountManager.addListener(this); Calendar c = Calendar.getInstance(); - this.addSchedule("fish", c.get(Calendar.YEAR), c.get(Calendar.MONTH) + 1, c.get(Calendar.DATE), "cinema", "10;00", "12:00", 0); - this.addSchedule("fish", 2024, 5, 28, "test", "15;00", "18:00", 1); - this.addSchedule("bird", c.get(Calendar.YEAR), c.get(Calendar.MONTH) + 1, c.get(Calendar.DATE), "cinema", "10;00", "12:00", 0); - this.addSchedule("bird", 2024, 5, 28, "test", "15;00", "18:00", 1); +// this.addSchedule("fish", c.get(Calendar.YEAR), c.get(Calendar.MONTH) + 1, c.get(Calendar.DATE), "cinema", c.get(Calendar.YEAR)+"/"+(c.get(Calendar.MONTH)+1)+"/"+c.get(Calendar.DATE)+" 10:00", c.get(Calendar.YEAR)+"/"+(c.get(Calendar.MONTH)+1)+"/"+c.get(Calendar.DATE)+" 12:00", 0); +// this.addSchedule("fish", 2024, 5, 28, "test", "2024/05/28 15:00", "2024/05/28 18:00", 1); +// this.addSchedule("bird", c.get(Calendar.YEAR), c.get(Calendar.MONTH) + 1, c.get(Calendar.DATE), "cinema", c.get(Calendar.YEAR)+"/"+(c.get(Calendar.MONTH)+1)+"/"+c.get(Calendar.DATE)+" 10:00", c.get(Calendar.YEAR)+"/"+(c.get(Calendar.MONTH)+1)+"/"+c.get(Calendar.DATE)+" 12:00", 0); +// this.addSchedule("bird", 2024, 5, 28, "test", "2024/05/28 15:00", "2024/05/28 18:00", 1); } @@ -71,20 +76,52 @@ if (!schedules.get(accountId).get(year).containsKey(month)) { schedules.get(accountId).get(year).put(month, new HashMap<>()); } - if (!schedules.get(accountId).get(year).get(month).containsKey(day)) { - schedules.get(accountId).get(year).get(month).put(day, new HashMap<>()); - } + LocalDate stDate = LocalDate.parse(startTime, DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm")); + LocalDate edDate = LocalDate.parse(endTime, DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm")); + int count = getDateRange(stDate, edDate); +// try { +// SimpleDateFormat stDateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm"); +// SimpleDateFormat edDateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm"); +// stDate = stDateFormat.parse(startTime); +// edDate = edDateFormat.parse(endTime); +// }catch (ParseException e){ +// e.printStackTrace(); +// } +// Calendar stcalendar = Calendar.getInstance(); +// Calendar edcalendar = Calendar.getInstance(); +// stcalendar.setTime(stDate); +// edcalendar.setTime(edDate); - String accountSchedule = accountId + year + month + day;// - if (!nextScheduleId.containsKey(accountSchedule)) { - nextScheduleId.put(accountSchedule, 0); + if(count > 0){ + for (int i = 0; i <= count; i++) { + if (!schedules.get(accountId).get(year).get(month).containsKey(day+i)) { + schedules.get(accountId).get(year).get(month).put(day+i, new HashMap<>()); + } + String accountSchedule = accountId + year + month + (day+i);// + if (!nextScheduleId.containsKey(accountSchedule)) { + nextScheduleId.put(accountSchedule, 0); + } + int newScheduleId = nextScheduleId.get(accountSchedule); + Schedule newSchedule = new Schedule(title, startTime, endTime, bookId, newScheduleId); + schedules.get(accountId).get(year).get(month).get(day+i).put(newScheduleId, newSchedule); + nextScheduleId.put(accountSchedule, newScheduleId + 1); + if(i==count)return newSchedule; + } + }else { + if (!schedules.get(accountId).get(year).get(month).containsKey(day)) { + schedules.get(accountId).get(year).get(month).put(day, new HashMap<>()); + } + String accountSchedule = accountId + year + month + day;// + if (!nextScheduleId.containsKey(accountSchedule)) { + nextScheduleId.put(accountSchedule, 0); + } + int newScheduleId = nextScheduleId.get(accountSchedule); + Schedule newSchedule = new Schedule(title, startTime, endTime, bookId, newScheduleId); + schedules.get(accountId).get(year).get(month).get(day).put(newScheduleId, newSchedule); + nextScheduleId.put(accountSchedule, newScheduleId + 1); + return newSchedule; } - - int newScheduleId = nextScheduleId.get(accountSchedule); - Schedule newSchedule = new Schedule(title, startTime, endTime, bookId, newScheduleId); - schedules.get(accountId).get(year).get(month).get(day).put(newScheduleId, newSchedule); - nextScheduleId.put(accountSchedule, newScheduleId + 1); - return newSchedule; + return null; } public Schedule getScheduleId(String accountId, int year, int month, int day, int scheduleId) { @@ -164,4 +201,19 @@ public void accountDeleted(Account account) { deleteSchedules(account.getId()); } + + public static int getDateRange(LocalDate startDate, LocalDate endDate){ + List dateList = new ArrayList<>(); + LocalDate currentDate = startDate; + int count = 0; + + while (!currentDate.isAfter(endDate)) { + dateList.add(currentDate); + currentDate = currentDate.plusDays(1); + count++; + } + + return count-1; + } } +