diff --git a/src/main/java/org/ntlab/citrusserver/repositories/ScheduleManager.java b/src/main/java/org/ntlab/citrusserver/repositories/ScheduleManager.java index 1734911..401dd29 100644 --- a/src/main/java/org/ntlab/citrusserver/repositories/ScheduleManager.java +++ b/src/main/java/org/ntlab/citrusserver/repositories/ScheduleManager.java @@ -6,8 +6,9 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Repository; +import java.text.ParseException; +import java.text.SimpleDateFormat; import java.time.LocalDate; -import java.time.format.DateTimeFormatter; import java.util.*; @Repository @@ -23,12 +24,6 @@ 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", 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); - } //管理 @@ -156,40 +151,45 @@ if (!schedules.get(accountId).get(year).containsKey(month)) { schedules.get(accountId).get(year).put(month, 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); - 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); + SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm"); + + Schedule lastCreatedSchedule = null; + System.out.println(day); + + try { + Date startDate = sdf.parse(startTime); + Date endDate = sdf.parse(endTime); + + Calendar scheduleCalendar = Calendar.getInstance(); + scheduleCalendar.setTime(startDate); + + while (!scheduleCalendar.getTime().after(endDate)) { + int currentYear = scheduleCalendar.get(Calendar.YEAR); + int currentMonth = scheduleCalendar.get(Calendar.MONTH) + 1; // +1で調整 + int currentDay = scheduleCalendar.get(Calendar.DAY_OF_MONTH); + + // スケジュールの初期化 + schedules.get(accountId).computeIfAbsent(currentYear, k -> new HashMap<>()).computeIfAbsent(currentMonth, k -> new HashMap<>()) + .computeIfAbsent(currentDay, k -> new HashMap<>()); + + String accountSchedule = accountId + currentYear + currentMonth + currentDay; + int newScheduleId = nextScheduleId.computeIfAbsent(accountSchedule, k -> 0); + Schedule newSchedule = new Schedule(title, startTime, endTime, bookId, newScheduleId); - schedules.get(accountId).get(year).get(month).get(day+i).put(newScheduleId, newSchedule); + schedules.get(accountId).get(currentYear).get(currentMonth).get(currentDay).put(newScheduleId, newSchedule); nextScheduleId.put(accountSchedule, newScheduleId + 1); - if(i==count)return newSchedule; + lastCreatedSchedule = newSchedule; + + // 次の日に進む + scheduleCalendar.add(Calendar.DAY_OF_MONTH, 1); } - }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; + } catch (ParseException e) { + e.printStackTrace(); // より具体的なエラーハンドリングを検討 + return null; } - return null; + + return lastCreatedSchedule; } public Schedule getScheduleId(String accountId, int year, int month, int day, int scheduleId, Integer bookId) {