重构
This commit is contained in:
1 parent
dc23ea53ed
commit
8c7a9040d2
106 files changed
+5668
-9910
No files matched your search
@@ -0,0 +1,108 @@
|
||||
package com.babytracker.module.summary;
|
||||
|
||||
import com.babytracker.module.record.CareRecord;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class DailySummary {
|
||||
private String babyId;
|
||||
private String date;
|
||||
private int feedingCount;
|
||||
private Map<String, Integer> feedingMethodCounts;
|
||||
private int sleepTotalMinutes;
|
||||
private int peeCount;
|
||||
private int poopCount;
|
||||
private CareRecord lastFeeding;
|
||||
private CareRecord activeFeeding;
|
||||
private CareRecord activeSleep;
|
||||
private List<CareRecord> timeline;
|
||||
|
||||
public String getBabyId() {
|
||||
return babyId;
|
||||
}
|
||||
|
||||
public void setBabyId(String babyId) {
|
||||
this.babyId = babyId;
|
||||
}
|
||||
|
||||
public String getDate() {
|
||||
return date;
|
||||
}
|
||||
|
||||
public void setDate(String date) {
|
||||
this.date = date;
|
||||
}
|
||||
|
||||
public int getFeedingCount() {
|
||||
return feedingCount;
|
||||
}
|
||||
|
||||
public void setFeedingCount(int feedingCount) {
|
||||
this.feedingCount = feedingCount;
|
||||
}
|
||||
|
||||
public Map<String, Integer> getFeedingMethodCounts() {
|
||||
return feedingMethodCounts;
|
||||
}
|
||||
|
||||
public void setFeedingMethodCounts(Map<String, Integer> feedingMethodCounts) {
|
||||
this.feedingMethodCounts = feedingMethodCounts;
|
||||
}
|
||||
|
||||
public int getSleepTotalMinutes() {
|
||||
return sleepTotalMinutes;
|
||||
}
|
||||
|
||||
public void setSleepTotalMinutes(int sleepTotalMinutes) {
|
||||
this.sleepTotalMinutes = sleepTotalMinutes;
|
||||
}
|
||||
|
||||
public int getPeeCount() {
|
||||
return peeCount;
|
||||
}
|
||||
|
||||
public void setPeeCount(int peeCount) {
|
||||
this.peeCount = peeCount;
|
||||
}
|
||||
|
||||
public int getPoopCount() {
|
||||
return poopCount;
|
||||
}
|
||||
|
||||
public void setPoopCount(int poopCount) {
|
||||
this.poopCount = poopCount;
|
||||
}
|
||||
|
||||
public CareRecord getLastFeeding() {
|
||||
return lastFeeding;
|
||||
}
|
||||
|
||||
public void setLastFeeding(CareRecord lastFeeding) {
|
||||
this.lastFeeding = lastFeeding;
|
||||
}
|
||||
|
||||
public CareRecord getActiveSleep() {
|
||||
return activeSleep;
|
||||
}
|
||||
|
||||
public void setActiveSleep(CareRecord activeSleep) {
|
||||
this.activeSleep = activeSleep;
|
||||
}
|
||||
|
||||
public CareRecord getActiveFeeding() {
|
||||
return activeFeeding;
|
||||
}
|
||||
|
||||
public void setActiveFeeding(CareRecord activeFeeding) {
|
||||
this.activeFeeding = activeFeeding;
|
||||
}
|
||||
|
||||
public List<CareRecord> getTimeline() {
|
||||
return timeline;
|
||||
}
|
||||
|
||||
public void setTimeline(List<CareRecord> timeline) {
|
||||
this.timeline = timeline;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package com.babytracker.module.summary;
|
||||
|
||||
import com.babytracker.common.response.ApiResponse;
|
||||
import org.springframework.format.annotation.DateTimeFormat;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.time.LocalDate;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/v1/summary")
|
||||
public class SummaryController {
|
||||
private final SummaryService summaryService;
|
||||
|
||||
public SummaryController(SummaryService summaryService) {
|
||||
this.summaryService = summaryService;
|
||||
}
|
||||
|
||||
@GetMapping("/daily")
|
||||
public ApiResponse<DailySummary> daily(
|
||||
@RequestParam String babyId,
|
||||
@RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate date
|
||||
) {
|
||||
return ApiResponse.ok(summaryService.daily(babyId, date));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user