68 lines
2.3 KiB
Java
68 lines
2.3 KiB
Java
package com.babytracker.module.summary;
|
|
|
|
import com.babytracker.module.record.CareRecord;
|
|
import com.babytracker.module.record.CareRecordService;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
import java.time.LocalDate;
|
|
import java.util.LinkedHashMap;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
|
|
@Service
|
|
public class SummaryService {
|
|
private final CareRecordService careRecordService;
|
|
|
|
public SummaryService(CareRecordService careRecordService) {
|
|
this.careRecordService = careRecordService;
|
|
}
|
|
|
|
public DailySummary daily(String babyId, LocalDate date) {
|
|
LocalDate targetDate = date == null ? LocalDate.now() : date;
|
|
List<CareRecord> timeline = careRecordService.list(babyId, targetDate);
|
|
DailySummary summary = new DailySummary();
|
|
summary.setBabyId(babyId);
|
|
summary.setDate(targetDate.toString());
|
|
summary.setTimeline(timeline);
|
|
summary.setActiveSleep(careRecordService.findActiveSleep(babyId));
|
|
summary.setActiveFeeding(careRecordService.findActiveFeeding(babyId));
|
|
|
|
Map<String, Integer> methodCounts = new LinkedHashMap<String, Integer>();
|
|
int feedingCount = 0;
|
|
int sleepTotal = 0;
|
|
int peeCount = 0;
|
|
int poopCount = 0;
|
|
CareRecord lastFeeding = null;
|
|
|
|
for (CareRecord record : timeline) {
|
|
if ("feeding".equals(record.getRecordType())) {
|
|
feedingCount++;
|
|
String method = record.getFeedingMethod() == null ? "other" : record.getFeedingMethod();
|
|
methodCounts.put(method, methodCounts.getOrDefault(method, 0) + 1);
|
|
if (lastFeeding == null || record.getStartTime().isAfter(lastFeeding.getStartTime())) {
|
|
lastFeeding = record;
|
|
}
|
|
}
|
|
if ("sleep".equals(record.getRecordType()) && record.getDurationMinutes() != null) {
|
|
sleepTotal += record.getDurationMinutes();
|
|
}
|
|
if ("excretion".equals(record.getRecordType())) {
|
|
if ("pee".equals(record.getExcretionType())) {
|
|
peeCount++;
|
|
}
|
|
if ("poop".equals(record.getExcretionType())) {
|
|
poopCount++;
|
|
}
|
|
}
|
|
}
|
|
|
|
summary.setFeedingCount(feedingCount);
|
|
summary.setFeedingMethodCounts(methodCounts);
|
|
summary.setSleepTotalMinutes(sleepTotal);
|
|
summary.setPeeCount(peeCount);
|
|
summary.setPoopCount(poopCount);
|
|
summary.setLastFeeding(lastFeeding);
|
|
return summary;
|
|
}
|
|
}
|