@@ -141,19 +141,42 @@ public ExpenseSummaryDTO getYearlyExpenseSummary(String userKey, int year) {
141141 Long userId = userService .getUserIdByUserKey (userKey );
142142 List <Expense > expenses = expenseRepository .findByUserId (userId );
143143
144- Map <Integer , RecordInfoDTO > monthlyRecords = expenses .stream ()
145- .filter (exp -> exp .getCreatedAt ().getYear () == year )
146- .collect (Collectors .groupingBy (
147- exp -> exp .getCreatedAt ().getMonthValue (),
148- Collectors .collectingAndThen (
149- Collectors .toList (),
150- list -> {
151- long recordCount = list .size ();
152- int totalDaysInMonth = YearMonth .of (year , list .get (0 ).getCreatedAt ().getMonthValue ()).lengthOfMonth ();
153- return new RecordInfoDTO (recordCount , totalDaysInMonth );
154- }
155- )
156- ));
144+ // 일 기준으로 그룹핑
145+ Set <LocalDate > dailyCounts = expenses .stream ()
146+ .filter (exp -> exp .getCreatedAt ().getYear () == year )
147+ .map (exp -> exp .getCreatedAt ().toLocalDate ())
148+ .collect (Collectors .toSet ());
149+
150+ // 월 기준으로 다시 그룹핑
151+ Map <Integer , Long > monthlyCounts = dailyCounts .stream ()
152+ .collect (Collectors .groupingBy (
153+ LocalDate ::getMonthValue ,
154+ Collectors .counting ()
155+ ));
156+
157+ // RecordInfoDTO 변환
158+ Map <Integer , RecordInfoDTO > monthlyRecords = monthlyCounts .entrySet ().stream ()
159+ .collect (Collectors .toMap (
160+ Map .Entry ::getKey , // 월(MonthValue)
161+ entry -> new RecordInfoDTO (
162+ entry .getValue (), // 해당 월의 기록이 있는 날짜 수
163+ YearMonth .of (year , entry .getKey ()).lengthOfMonth () // 해당 월의 총 날짜 수
164+ )
165+ ));
166+
167+ // Map<Integer, RecordInfoDTO> monthlyRecords = expenses.stream()
168+ // .filter(exp -> exp.getCreatedAt().getYear() == year)
169+ // .collect(Collectors.groupingBy(
170+ // exp -> exp.getCreatedAt().getMonthValue(),
171+ // Collectors.collectingAndThen(
172+ // Collectors.toList(),
173+ // list -> {
174+ // long recordCount = list.size();
175+ // int totalDaysInMonth = YearMonth.of(year, list.get(0).getCreatedAt().getMonthValue()).lengthOfMonth();
176+ // return new RecordInfoDTO(recordCount, totalDaysInMonth);
177+ // }
178+ // )
179+ // ));
157180
158181 return ExpenseSummaryDTO .builder ()
159182 .year (year )
0 commit comments