diff --git a/src/main/java/org/ntlab/citrusserver/repositories/ScheduleManager.java b/src/main/java/org/ntlab/citrusserver/repositories/ScheduleManager.java index fdaea51..1734911 100644 --- a/src/main/java/org/ntlab/citrusserver/repositories/ScheduleManager.java +++ b/src/main/java/org/ntlab/citrusserver/repositories/ScheduleManager.java @@ -6,14 +6,9 @@ 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.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{ @@ -25,10 +20,11 @@ @Autowired public ScheduleManager(AccountManager accountManager, BookManager bookManager) { this.bookManager = bookManager; + 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", 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", 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); @@ -44,20 +40,104 @@ Schedule>>>>> schedules = new HashMap<>(); - public HashMap>>> getScheduleAll(String accountId) { - return schedules.get(accountId); + private final //accountId + HashMap>>> resultSchedules + = new HashMap<>(); + + public HashMap>>> getScheduleAll(String accountId, Integer bookId) { + if (bookId != null) { + // 既存のアカウントのスケジュールを取得 + HashMap>>> accountSchedules = schedules.get(accountId); + + for (int year : accountSchedules.keySet()) { + for (int month : accountSchedules.get(year).keySet()) { + for (int day : accountSchedules.get(year).get(month).keySet()) { + for (int scheduleId : accountSchedules.get(year).get(month).get(day).keySet()) { + Schedule schedule = accountSchedules.get(year).get(month).get(day).get(scheduleId); + // bookIdが一致するスケジュールをresultSchedulesに追加 + if (schedule.getBookId().equals(bookId)) { + resultSchedules + .computeIfAbsent(year, k -> new HashMap<>()) + .computeIfAbsent(month, k -> new HashMap<>()) + .computeIfAbsent(day, k -> new HashMap<>()) + .put(scheduleId, schedule); + } + } + } + } + } + return resultSchedules; + } else return schedules.get(accountId); } - public HashMap>> getScheduleYear(String accountId, int year) { - return schedules.get(accountId).get(year); + public HashMap>> getScheduleYear(String accountId, int year, Integer bookId) { + if (bookId != null) { + // 既存のアカウントのスケジュールを取得 + HashMap>>> accountSchedules = schedules.get(accountId); + + for (int month : accountSchedules.get(year).keySet()) { + for (int day : accountSchedules.get(year).get(month).keySet()) { + for (int scheduleId : accountSchedules.get(year).get(month).get(day).keySet()) { + Schedule schedule = accountSchedules.get(year).get(month).get(day).get(scheduleId); + // bookIdが一致するスケジュールをresultSchedulesに追加 + if (schedule.getBookId().equals(bookId)) { + resultSchedules + .computeIfAbsent(year, k -> new HashMap<>()) + .computeIfAbsent(month, k -> new HashMap<>()) + .computeIfAbsent(day, k -> new HashMap<>()) + .put(scheduleId, schedule); + } + } + } + } + return resultSchedules.get(year); + } else return schedules.get(accountId).get(year); } - public HashMap> getScheduleMonth(String accountId, int year, int month) { - return schedules.get(accountId).get(year).get(month); + public HashMap> getScheduleMonth(String accountId, int year, int month, Integer bookId) { + if (bookId != null) { + // 既存のアカウントのスケジュールを取得 + HashMap>>> accountSchedules = schedules.get(accountId); + + for (int day : accountSchedules.get(year).get(month).keySet()) { + for (int scheduleId : accountSchedules.get(year).get(month).get(day).keySet()) { + Schedule schedule = accountSchedules.get(year).get(month).get(day).get(scheduleId); + // bookIdが一致するスケジュールをresultSchedulesに追加 + if (schedule.getBookId().equals(bookId)) { + resultSchedules + .computeIfAbsent(year, k -> new HashMap<>()) + .computeIfAbsent(month, k -> new HashMap<>()) + .computeIfAbsent(day, k -> new HashMap<>()) + .put(scheduleId, schedule); + } + } + } + return resultSchedules.get(year).get(month); + }else return schedules.get(accountId).get(year).get(month); } - public HashMap getScheduleDay(String accountId, int year, int month, int day) { - return schedules.get(accountId).get(year).get(month).get(day); + public HashMap getScheduleDay(String accountId, int year, int month, int day, Integer bookId) { + if (bookId != null) { + // 既存のアカウントのスケジュールを取得 + HashMap>>> accountSchedules = schedules.get(accountId); + + for (int scheduleId : accountSchedules.get(year).get(month).get(day).keySet()) { + Schedule schedule = accountSchedules.get(year).get(month).get(day).get(scheduleId); + // bookIdが一致するスケジュールをresultSchedulesに追加 + if (schedule.getBookId().equals(bookId)) { + resultSchedules + .computeIfAbsent(year, k -> new HashMap<>()) + .computeIfAbsent(month, k -> new HashMap<>()) + .computeIfAbsent(day, k -> new HashMap<>()) + .put(scheduleId, schedule); + } + } + return resultSchedules.get(year).get(month).get(day); + }else return schedules.get(accountId).get(year).get(month).get(day); } public Schedule addSchedule(String accountId, int year, int month, int day, String title, String startTime, String endTime, Integer bookId) { @@ -79,18 +159,6 @@ 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); if(count > 0){ for (int i = 0; i <= count; i++) { @@ -124,8 +192,22 @@ return null; } - public Schedule getScheduleId(String accountId, int year, int month, int day, int scheduleId) { - return schedules.get(accountId).get(year).get(month).get(day).get(scheduleId); + public Schedule getScheduleId(String accountId, int year, int month, int day, int scheduleId, Integer bookId) { + if (bookId != null) { + // 既存のアカウントのスケジュールを取得 + HashMap>>> accountSchedules = schedules.get(accountId); + + Schedule schedule = accountSchedules.get(year).get(month).get(day).get(scheduleId); + // bookIdが一致するスケジュールをresultSchedulesに追加 + if (schedule.getBookId().equals(bookId)) { + resultSchedules + .computeIfAbsent(year, k -> new HashMap<>()) + .computeIfAbsent(month, k -> new HashMap<>()) + .computeIfAbsent(day, k -> new HashMap<>()) + .put(scheduleId, schedule); + } + return resultSchedules.get(year).get(month).get(day).get(scheduleId); + }else return schedules.get(accountId).get(year).get(month).get(day).get(scheduleId); } public void deleteSchedule(String accountId, int year, int month, int day, int scheduleId) {