Skip to content

Commit

Permalink
feat: core-module type 및 field 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeongh00 committed Oct 8, 2024
1 parent 7ac3da8 commit 828acfd
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

Expand All @@ -14,6 +13,6 @@ public interface ReportRepository extends JpaRepository<Report, Long> {

// 이번 주 총 레포트 조회
@Query("SELECT r FROM Report r WHERE r.user.id = :userId AND r.createdAt >= :startOfWeek AND r.createdAt < :endOfWeek")
List<Report> findReportsByUserAndWeek(Long userId, LocalDate startOfWeek, LocalDate endOfWeek);
List<Report> findReportsByUserAndCreatedAtBetween(Long userId, LocalDateTime startOfWeek, LocalDateTime endOfWeek);

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,29 +26,29 @@ public class Report extends BaseEntity {
private MealType type;

@Column(name = "report_total", nullable = false)
private Integer total;
private Double total;

@Column(name = "report_carb", nullable = false)
private Integer carb;
private Double carb;

@Column(name = "report_protein", nullable = false)
private Integer protein;
private Double protein;

@Column(name = "report_fat", nullable = false)
private Integer fat;
private Double fat;

@Column(name = "report_sugar", nullable = false)
private Integer sugar; // 당류
private Double sugar; // 당류

@Column(name = "report_sodium", nullable = false)
private Integer sodium; // 나트륨
private Double sodium; // 나트륨

@Column(name = "report_cholesterol", nullable = false)
private Integer cholesterol; // 콜레스테롤
private Double cholesterol; // 콜레스테롤

@Column(name = "report_saturated_fat", nullable = false)
private Integer saturatedFat; // 포화지방산
private Double saturatedFat; // 포화지방산

@Column(name = "report_trans_fat", nullable = false)
private Integer transFat; // 트랜스지방
private Double transFat; // 트랜스지방
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
package com.foodgo.coremodule.report.dto;

public record ReportComparisonDTO(
int lastWeekTotal,
int thisWeekTotal,
int lastWeekCarbs,
int thisWeekCarbs,
int lastWeekProteins,
int thisWeekProteins,
int lastWeekFats,
int thisWeekFats
Double lastWeekTotal,
Double thisWeekTotal,
Double lastWeekCarbs,
Double thisWeekCarbs,
Double lastWeekProteins,
Double thisWeekProteins,
Double lastWeekFats,
Double thisWeekFats,
Double lastWeekSugar,
Double thisWeekSugar,
Double lastWeekSodium,
Double thisWeekSodium,
Double lastWeekCholesterol,
Double thisWeekCholesterol,
Double lastWeekSaturatedFat,
Double thisWeekSaturatedFat,
Double lastWeekTransFat,
Double thisWeekTransFat
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ private LocalDate getEndOfWeek(LocalDate date) {
return date.with(TemporalAdjusters.nextOrSame(java.time.DayOfWeek.SUNDAY));
}

// 이번 주와 저번 주의 칼로리 리포트 비교
public ReportComparisonDTO getWeeklyReportComparison(Long userId) {
LocalDate today = LocalDate.now();

Expand All @@ -44,49 +43,100 @@ public ReportComparisonDTO getWeeklyReportComparison(Long userId) {
LocalDate startOfLastWeek = startOfThisWeek.minusWeeks(1);
LocalDate endOfLastWeek = endOfThisWeek.minusWeeks(1);

// 이번 주와 저번 주 레포트 가져오기
List<Report> thisWeekReports = reportRepository.findReportsByUserAndWeek(userId, startOfThisWeek, endOfThisWeek);
List<Report> lastWeekReports = reportRepository.findReportsByUserAndWeek(userId, startOfLastWeek, endOfLastWeek);
// 이번 주와 저번 주의 칼로리 합산
double thisWeekTotal = calculateTotalForWeek(userId, startOfThisWeek, endOfThisWeek);
double lastWeekTotal = calculateTotalForWeek(userId, startOfLastWeek, endOfLastWeek);

// 데이터가 있을 때만 합계를 계산하고, 없으면 0으로 처리
int thisWeekTotal = thisWeekReports.isEmpty() ? 0 : calculateTotalCalories(thisWeekReports);
int lastWeekTotal = lastWeekReports.isEmpty() ? 0 : calculateTotalCalories(lastWeekReports);
// 영양소별 합산 (탄수화물, 단백질, 지방, 당류, 나트륨 등)
double thisWeekCarbs = calculateCarbsForWeek(userId, startOfThisWeek, endOfThisWeek);
double lastWeekCarbs = calculateCarbsForWeek(userId, startOfLastWeek, endOfLastWeek);

int thisWeekCarbs = thisWeekReports.isEmpty() ? 0 : calculateTotalCarbs(thisWeekReports);
int lastWeekCarbs = lastWeekReports.isEmpty() ? 0 : calculateTotalCarbs(lastWeekReports);
double thisWeekProteins = calculateProteinsForWeek(userId, startOfThisWeek, endOfThisWeek);
double lastWeekProteins = calculateProteinsForWeek(userId, startOfLastWeek, endOfLastWeek);

int thisWeekProteins = thisWeekReports.isEmpty() ? 0 : calculateTotalProteins(thisWeekReports);
int lastWeekProteins = lastWeekReports.isEmpty() ? 0 : calculateTotalProteins(lastWeekReports);
double thisWeekFats = calculateFatsForWeek(userId, startOfThisWeek, endOfThisWeek);
double lastWeekFats = calculateFatsForWeek(userId, startOfLastWeek, endOfLastWeek);

int thisWeekFats = thisWeekReports.isEmpty() ? 0 : calculateTotalFats(thisWeekReports);
int lastWeekFats = lastWeekReports.isEmpty() ? 0 : calculateTotalFats(lastWeekReports);
double thisWeekSugar = calculateSugarForWeek(userId, startOfThisWeek, endOfThisWeek);
double lastWeekSugar = calculateSugarForWeek(userId, startOfLastWeek, endOfLastWeek);

double thisWeekSodium = calculateSodiumForWeek(userId, startOfThisWeek, endOfThisWeek);
double lastWeekSodium = calculateSodiumForWeek(userId, startOfLastWeek, endOfLastWeek);

double thisWeekCholesterol = calculateCholesterolForWeek(userId, startOfThisWeek, endOfThisWeek);
double lastWeekCholesterol = calculateCholesterolForWeek(userId, startOfLastWeek, endOfLastWeek);

double thisWeekSaturatedFat = calculateSaturatedFatForWeek(userId, startOfThisWeek, endOfThisWeek);
double lastWeekSaturatedFat = calculateSaturatedFatForWeek(userId, startOfLastWeek, endOfLastWeek);

double thisWeekTransFat = calculateTransFatForWeek(userId, startOfThisWeek, endOfThisWeek);
double lastWeekTransFat = calculateTransFatForWeek(userId, startOfLastWeek, endOfLastWeek);

// 결과 반환
return new ReportComparisonDTO(
lastWeekTotal, thisWeekTotal,
lastWeekCarbs, thisWeekCarbs,
lastWeekProteins, thisWeekProteins,
lastWeekFats, thisWeekFats
lastWeekFats, thisWeekFats,
lastWeekSugar, thisWeekSugar,
lastWeekSodium, thisWeekSodium,
lastWeekCholesterol, thisWeekCholesterol,
lastWeekSaturatedFat, thisWeekSaturatedFat,
lastWeekTransFat, thisWeekTransFat
);
}

// 칼로리 합계 계산
private int calculateTotalCalories(List<Report> reports) {
return reports.stream().mapToInt(Report::getTotal).sum();
// 주간 칼로리 합계 계산
private double calculateTotalForWeek(Long userId, LocalDate start, LocalDate end) {
List<Report> reports = reportRepository.findReportsByUserAndCreatedAtBetween(userId, start.atStartOfDay(), end.atTime(23, 59, 59));
return reports.stream().mapToDouble(Report::getTotal).sum();
}

// 주간 탄수화물 합계 계산
private double calculateCarbsForWeek(Long userId, LocalDate start, LocalDate end) {
List<Report> reports = reportRepository.findReportsByUserAndCreatedAtBetween(userId, start.atStartOfDay(), end.atTime(23, 59, 59));
return reports.stream().mapToDouble(Report::getCarb).sum();
}

// 주간 단백질 합계 계산
private double calculateProteinsForWeek(Long userId, LocalDate start, LocalDate end) {
List<Report> reports = reportRepository.findReportsByUserAndCreatedAtBetween(userId, start.atStartOfDay(), end.atTime(23, 59, 59));
return reports.stream().mapToDouble(Report::getProtein).sum();
}

// 주간 지방 합계 계산
private double calculateFatsForWeek(Long userId, LocalDate start, LocalDate end) {
List<Report> reports = reportRepository.findReportsByUserAndCreatedAtBetween(userId, start.atStartOfDay(), end.atTime(23, 59, 59));
return reports.stream().mapToDouble(Report::getFat).sum();
}

// 주간 당류 합계 계산
private double calculateSugarForWeek(Long userId, LocalDate start, LocalDate end) {
List<Report> reports = reportRepository.findReportsByUserAndCreatedAtBetween(userId, start.atStartOfDay(), end.atTime(23, 59, 59));
return reports.stream().mapToDouble(Report::getSugar).sum();
}

// 주간 나트륨 합계 계산
private double calculateSodiumForWeek(Long userId, LocalDate start, LocalDate end) {
List<Report> reports = reportRepository.findReportsByUserAndCreatedAtBetween(userId, start.atStartOfDay(), end.atTime(23, 59, 59));
return reports.stream().mapToDouble(Report::getSodium).sum();
}

// 탄수화물 합계 계산
private int calculateTotalCarbs(List<Report> reports) {
return reports.stream().mapToInt(Report::getCarb).sum();
// 주간 콜레스테롤 합계 계산
private double calculateCholesterolForWeek(Long userId, LocalDate start, LocalDate end) {
List<Report> reports = reportRepository.findReportsByUserAndCreatedAtBetween(userId, start.atStartOfDay(), end.atTime(23, 59, 59));
return reports.stream().mapToDouble(Report::getCholesterol).sum();
}

// 단백질 합계 계산
private int calculateTotalProteins(List<Report> reports) {
return reports.stream().mapToInt(Report::getProtein).sum();
// 주간 포화지방산 합계 계산
private double calculateSaturatedFatForWeek(Long userId, LocalDate start, LocalDate end) {
List<Report> reports = reportRepository.findReportsByUserAndCreatedAtBetween(userId, start.atStartOfDay(), end.atTime(23, 59, 59));
return reports.stream().mapToDouble(Report::getSaturatedFat).sum();
}

// 지방 합계 계산
private int calculateTotalFats(List<Report> reports) {
return reports.stream().mapToInt(Report::getFat).sum();
// 주간 트랜스지방 합계 계산
private double calculateTransFatForWeek(Long userId, LocalDate start, LocalDate end) {
List<Report> reports = reportRepository.findReportsByUserAndCreatedAtBetween(userId, start.atStartOfDay(), end.atTime(23, 59, 59));
return reports.stream().mapToDouble(Report::getTransFat).sum();
}
}

0 comments on commit 828acfd

Please sign in to comment.