diff --git a/src/main/java/com/example/kusitmshackthon/domain/healthlog/standard/StandardHealthLog.java b/src/main/java/com/example/kusitmshackthon/domain/healthlog/standard/StandardHealthLog.java new file mode 100644 index 0000000..5b87c4a --- /dev/null +++ b/src/main/java/com/example/kusitmshackthon/domain/healthlog/standard/StandardHealthLog.java @@ -0,0 +1,16 @@ +package com.example.kusitmshackthon.domain.healthlog.standard; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@Getter +@NoArgsConstructor +public class StandardHealthLog { + // 15세 여아 표준 영양 정보 + Float protein = 45F; // 단백질(g) + Float calcium = 800F; // 칼슘(mg) + Float sodium = 1.5F; // 나트륨(mg) + Float fe = 17F; // 철(mg) + Float zinc = 9F; // 아연(mg) +} diff --git a/src/main/java/com/example/kusitmshackthon/domain/member/controller/MemberController.java b/src/main/java/com/example/kusitmshackthon/domain/member/controller/MemberController.java index 9f074e5..c9dd16c 100644 --- a/src/main/java/com/example/kusitmshackthon/domain/member/controller/MemberController.java +++ b/src/main/java/com/example/kusitmshackthon/domain/member/controller/MemberController.java @@ -3,6 +3,7 @@ import com.example.kusitmshackthon.domain.member.dto.response.MainPageResponse; import com.example.kusitmshackthon.domain.member.service.MemberService; +import io.swagger.v3.oas.annotations.Operation; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.http.ResponseEntity; @@ -18,6 +19,12 @@ public class MemberController { private final MemberService memberService; + @Operation(summary = "가장 최근에 섭취한 식단에서, 부족한 영양소 top 3 조회", description = + """ + name 은 영양소 이름이고, amount 는 섭취량, diff: 는 권장 섭취량 - amount 입니다! + + 부족한 순으로 정렬해서 나갑니다.. + """) @GetMapping("/{userId}") public ResponseEntity getMainPage(@PathVariable(name = "userId") Long userId) { MainPageResponse response = memberService.getMainPage(userId); diff --git a/src/main/java/com/example/kusitmshackthon/domain/member/dto/response/MainPageResponse.java b/src/main/java/com/example/kusitmshackthon/domain/member/dto/response/MainPageResponse.java index a7af680..9eea0f5 100644 --- a/src/main/java/com/example/kusitmshackthon/domain/member/dto/response/MainPageResponse.java +++ b/src/main/java/com/example/kusitmshackthon/domain/member/dto/response/MainPageResponse.java @@ -1,6 +1,7 @@ package com.example.kusitmshackthon.domain.member.dto.response; +import com.example.kusitmshackthon.domain.diet.dto.response.PostDietResponse; import lombok.*; import java.util.List; @@ -19,8 +20,12 @@ public class MainPageResponse { @ToString public static class NutrientInfo { private String name; - private Integer plus; - private Integer minus; + private Float amount; + private Float diff; + + public static MainPageResponse.NutrientInfo of (String name, Float amount, Float diff){ + return new MainPageResponse.NutrientInfo(name, amount, diff); + } } public static MainPageResponse of(List nutrientInfoList) { diff --git a/src/main/java/com/example/kusitmshackthon/domain/member/service/MemberService.java b/src/main/java/com/example/kusitmshackthon/domain/member/service/MemberService.java index ce2d5eb..1f4fb2a 100644 --- a/src/main/java/com/example/kusitmshackthon/domain/member/service/MemberService.java +++ b/src/main/java/com/example/kusitmshackthon/domain/member/service/MemberService.java @@ -2,6 +2,7 @@ import com.example.kusitmshackthon.domain.healthlog.entity.HealthLog; import com.example.kusitmshackthon.domain.healthlog.repository.HealthLogRepository; +import com.example.kusitmshackthon.domain.healthlog.standard.StandardHealthLog; import com.example.kusitmshackthon.domain.member.dto.response.MainPageResponse; import com.example.kusitmshackthon.domain.member.dto.response.MemberAuthResponseDto; import com.example.kusitmshackthon.domain.member.entity.Member; @@ -18,7 +19,9 @@ import java.time.LocalDate; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; import static com.example.kusitmshackthon.domain.healthlog.entity.QHealthLog.healthLog; @@ -34,7 +37,6 @@ public class MemberService { private EntityManager entityManager; public MainPageResponse getMainPage(Long userId) { - List nutrientInfoList = new ArrayList<>(); Member member = memberRepository.findById(userId) .orElseThrow(MemberNotFoundException::new); LocalDate nowDate = LocalDate.now(); @@ -54,8 +56,54 @@ public MainPageResponse getMainPage(Long userId) { .orderBy(healthLog.intakeAt.desc()) .limit(1) .fetchOne(); + // 해당 회원의 베스트 영양 정보 get 하기 + StandardHealthLog standardHealthLog = new StandardHealthLog(); + + Map diffHashMap = new HashMap<>(); + Map orignHashMap = new HashMap<>(); + + orignHashMap.put("protein", recentlyHealthLog.getProtein()); + orignHashMap.put("calcium", recentlyHealthLog.getCalcium()); + orignHashMap.put("sodium", recentlyHealthLog.getSodium()); + orignHashMap.put("fe", recentlyHealthLog.getFe()); + orignHashMap.put("zinc", recentlyHealthLog.getZinc()); + + if (recentlyHealthLog.getProtein() != 0) { + diffHashMap.put("protein", recentlyHealthLog.getProtein() - standardHealthLog.getProtein()); + } + if (recentlyHealthLog.getCalcium() != 0) { + diffHashMap.put("calcium", recentlyHealthLog.getCalcium() - standardHealthLog.getCalcium()); + } + if (recentlyHealthLog.getSodium() != 0) { + diffHashMap.put("sodium", recentlyHealthLog.getSodium() - standardHealthLog.getSodium()); + } + if (recentlyHealthLog.getFe() != 0) { + diffHashMap.put("fe", recentlyHealthLog.getFe() - standardHealthLog.getFe()); + } + if (recentlyHealthLog.getZinc() != 0) { + diffHashMap.put("zinc", recentlyHealthLog.getZinc() - standardHealthLog.getZinc()); + } - System.out.println("recentlyHealthLog = " + recentlyHealthLog); + + List> collect = diffHashMap.entrySet().stream() + .sorted(Map.Entry.comparingByValue()) + .toList(); + + List nutrientInfoList = new ArrayList<>(); + int cnt = 0; + for (Map.Entry entry : collect) { + String key = entry.getKey(); + Float diff = entry.getValue(); + if (Math.abs(diff) <= 200) { + cnt++; + nutrientInfoList.add(MainPageResponse.NutrientInfo.of( + key, orignHashMap.get(key), diff + )); + if (cnt == 3) { + break; + } + } + } return MainPageResponse.of(nutrientInfoList); }