2525
2626@ Service
2727@ RequiredArgsConstructor
28- public class HomeServiceImpl implements HomeCommandService {
28+ public class HomeServiceImpl {
2929
3030 private final UserRepository userRepository ;
3131 private final RecommendMealRepository recommendMealRepository ;
3232 private final UserMealRepository userMealRepository ;
3333 private final DiagnosisRepository diagnosisRepository ;
3434 private final DailyExerciseRecordRepository dailyExerciseRecordRepository ;
35- private final WeightRecordRepository weightRecordRepository ;
3635 private final UserExerciseRepository userExerciseRepository ;
3736 private final DiagnosisRepository diagnosesRepository ;
3837 private final ExerciseSetRepository exerciseSetRepository ;
3938
4039 //추천 운동 소모 칼로리
4140 @ Transactional (readOnly = true )
42- public int getBurnedCaloriesOnDate (Long user , LocalDate date ) {
43- List <UserExercise > exercises = userExerciseRepository .findWithExerciseByUserIdAndDate (user , date );
41+ public int getBurnedCaloriesOnDate (Long user , LocalDate date , Long goalPeriodId ) {
42+ List <UserExercise > exercises = userExerciseRepository .findWithExerciseByUserIdAndDate (user , date , goalPeriodId );
4443
4544 return exercises .stream ()
4645 .filter (userExercise -> userExercise .getExercise () != null && !userExercise .getExercise ().getId ().equals (999999L )) // 커스텀 운동 제외
@@ -61,8 +60,8 @@ public int getBurnedCaloriesOnDate(Long user, LocalDate date) {
6160 .sum ();
6261 }
6362 //추천 칼로리
64- public double getRecommendedCaloriesByDate (Long userId ,LocalDate date ) {
65- List <RecommendMeal > recommandedMeals = recommendMealRepository .findWithFoodByUserIdAndDate (userId ,date );
63+ public double getRecommendedCaloriesByDate (Long userId ,LocalDate date , Long goalPeriodId ) {
64+ List <RecommendMeal > recommandedMeals = recommendMealRepository .findWithFoodByUserIdAndDate (userId ,date , goalPeriodId );
6665
6766 double totalCalories = recommandedMeals .stream ()
6867 .mapToDouble (recommendMeal -> recommendMeal .getFood ().getCalorie () != null ? recommendMeal .getFood ().getCalorie () : 0 )
@@ -72,8 +71,8 @@ public double getRecommendedCaloriesByDate(Long userId,LocalDate date) {
7271 }
7372
7473 //추천 단백질 합계
75- public double getRecommendedProteinByDate (Long userId ,LocalDate date ) {
76- List <RecommendMeal > recommandedMeals = recommendMealRepository .findWithFoodByUserIdAndDate (userId ,date );
74+ public double getRecommendedProteinByDate (Long userId ,LocalDate date , Long goalPeriodId ) {
75+ List <RecommendMeal > recommandedMeals = recommendMealRepository .findWithFoodByUserIdAndDate (userId ,date , goalPeriodId );
7776
7877 double totalCalories = recommandedMeals .stream ()
7978 .mapToDouble (recommendMeal -> recommendMeal .getFood ().getProtein () != null ? recommendMeal .getFood ().getProtein () : 0 )
@@ -83,8 +82,8 @@ public double getRecommendedProteinByDate(Long userId,LocalDate date) {
8382 }
8483
8584 //추천 탄수화물 합계
86- public double getRecommendedCarbByDate (Long userId ,LocalDate date ) {
87- List <RecommendMeal > recommandedMeals = recommendMealRepository .findWithFoodByUserIdAndDate (userId ,date );
85+ public double getRecommendedCarbByDate (Long userId ,LocalDate date , Long goalPeriodId ) {
86+ List <RecommendMeal > recommandedMeals = recommendMealRepository .findWithFoodByUserIdAndDate (userId ,date , goalPeriodId );
8887
8988 double totalCalories = recommandedMeals .stream ()
9089 .mapToDouble (recommendMeal -> recommendMeal .getFood ().getCarb () != null ? recommendMeal .getFood ().getCarb () : 0 )
@@ -93,8 +92,8 @@ public double getRecommendedCarbByDate(Long userId,LocalDate date) {
9392 return totalCalories ;
9493 }
9594 //추천 지방 합계
96- public double getRecommendedFatByDate (Long userId ,LocalDate date ) {
97- List <RecommendMeal > recommandedMeals = recommendMealRepository .findWithFoodByUserIdAndDate (userId ,date );
95+ public double getRecommendedFatByDate (Long userId ,LocalDate date , Long goalPeriodId ) {
96+ List <RecommendMeal > recommandedMeals = recommendMealRepository .findWithFoodByUserIdAndDate (userId ,date , goalPeriodId );
9897
9998 double totalCalories = recommandedMeals .stream ()
10099 .mapToDouble (recommendMeal -> recommendMeal .getFood ().getFat () != null ? recommendMeal .getFood ().getFat () : 0 )
@@ -231,17 +230,14 @@ public String todayComment(LocalDate date){
231230 }
232231
233232
234- public HomeResultDTO .HomeMainDTO getHomeData (Long userId ) {
233+ public HomeResultDTO .HomeMainDTO getHomeData (Long userId , Long goalPeriodId ) {
235234 User user = userRepository .getReferenceById (userId );
236235 // 기존 진단이 있는지 확인
237236 Optional <Diagnosis > existingDiagnosis = diagnosisRepository .findByUserId (userId );
238- Optional <WeightRecord > recordedWeight = weightRecordRepository .findById (userId );
239-
240-
241237 LocalDate date = LocalDate .now ();
242238
243- double recommendedCalories = getRecommendedCaloriesByDate (userId , date );//추천 섭취 칼로리
244- int recommendedBurnedCalories = getBurnedCaloriesOnDate (userId , date );//추천 소비 칼로리
239+ double recommendedCalories = getRecommendedCaloriesByDate (userId , date , goalPeriodId );//추천 섭취 칼로리
240+ int recommendedBurnedCalories = getBurnedCaloriesOnDate (userId , date , goalPeriodId );//추천 소비 칼로리
245241 double todayConsumedCalories = getTodayConsumedCaloriesByDate (userId , date );// 오늘 섭취한 칼로리
246242 int todayBurnedCalories = getTodayCaloriesBurnedByUser (userId , date );//오늘 소비한 칼로리
247243
@@ -250,18 +246,9 @@ public HomeResultDTO.HomeMainDTO getHomeData(Long userId) {
250246 //추천 식단의 총 칼로리 - 추천 운동의 총 소비 칼로리
251247 double targetCalories = recommendedCalories -recommendedBurnedCalories ;
252248
253- //목표 몸무게 (현재 몸무게는 생성일자 기준으로 가장 빠른 데이터로 출력 추가로 입력 api기능 구현해야함)
254- BigDecimal currentWeight = recordedWeight
255- .map (WeightRecord ::getWeight )
256- .orElse (BigDecimal .ZERO );
257249 BigDecimal targetWeight = existingDiagnosis
258250 .map (Diagnosis ::getTargetWeight )
259251 .orElse (BigDecimal .ZERO ); // 값이 없으면 0으로 기본 처리
260-
261- int caloriePercentage = (int )(todayConsumedCalories /recommendedCalories );
262- int exercisePercentage = (int )(todayBurnedCalories /recommendedCalories );
263- String exerciseStatus = HomeExercisePercentageStatus (exercisePercentage );
264- String caloriestatus = HomeMealPercentageStatus (caloriePercentage );
265252 int presentWeek = calculateWeek (user .getCreated_at ().toLocalDate (), date );//유저 생성시간 기준으로 구현
266253 String comment = todayComment (date );
267254
@@ -270,51 +257,62 @@ public HomeResultDTO.HomeMainDTO getHomeData(Long userId) {
270257 .userId (userId )
271258 .targetCalories (targetCalories )
272259 .todayCalories (todayCalories )
273- .caloriePercentage (caloriePercentage )
274- .exercisePercentage (exercisePercentage )
275- .caloriestatus (caloriestatus )
276- .exercisestatus (exerciseStatus )
277260 .date (date )
278261 .presentWeek (presentWeek )
279262 .targetWeight (targetWeight )
280- .currentWeight (currentWeight )
281263 .comment (comment )
282264 .build ();
283265 }
284266
285- public HomeResultDTO .HomeExerciseDTO getHomeExercise (Long userId ) {
267+ public HomeResultDTO .HomeExerciseDTO getHomeExercise (Long userId , Long goalPeriodId ) {
286268 String focusedBodyPart = "하체" ; // 더미 데이터 집중 부위
287269 int anaerobicExerciseTime =getTodayAnaerobicExerciseTimeByDate (userId ); // 무산소 시간
288270 int aerobicExerciseTime = getTodayAerobicExerciseTimeByDate (userId ); // 유산소 시간
271+ int todayBurnedCalories = getTodayCaloriesBurnedByUser (userId , LocalDate .now ());//오늘 칼로리 소비량
272+ int todayRecommanedBurnedCalories = getBurnedCaloriesOnDate (userId , LocalDate .now (),goalPeriodId );//추천 칼로리 소비량
273+ int exercisePercentage = (todayBurnedCalories /todayRecommanedBurnedCalories );//현재 byzero 문제 발생
274+ String exerciseStatus = HomeExercisePercentageStatus (exercisePercentage );
289275
290276 return HomeResultDTO .HomeExerciseDTO .builder ()
277+ .todayBurnedCalories (todayBurnedCalories )
278+ .todayRecommanedBurnedCalories (todayRecommanedBurnedCalories )
291279 .anaerobicExerciseTime (anaerobicExerciseTime )
292280 .aerobicExerciseTime (aerobicExerciseTime )
293281 .focusedBodyPart (focusedBodyPart )
282+ .exerciseStatus (exerciseStatus )
283+ .exercisePercentage (exercisePercentage )
294284 .build ();
295285 }
296286
297- public HomeResultDTO .HomeMealPlanDTO getHomeMealPlan (Long userId ) {
287+ public HomeResultDTO .HomeMealPlanDTO getHomeMealPlan (Long userId , Long goalPeriodId ) {
298288 LocalDate date = LocalDate .now ();
299289
300- double calorie = getTodayConsumedCaloriesByDate (userId , date );
301- double carb = getRecommendedCarbByDate (userId , date );
302- double protein = getRecommendedProteinByDate (userId ,date );
303- double fat = getRecommendedFatByDate (userId ,date );
290+ double recommendedCalories = getRecommendedCaloriesByDate (userId , date , goalPeriodId );//추천 칼로리 섭취량
291+ double calorie = getTodayConsumedCaloriesByDate (userId , date );//오늘 섭취한 총 칼로리
292+ double carb = getRecommendedCarbByDate (userId , date , goalPeriodId );//추천된 탄수화물 그램수
293+ double protein = getRecommendedProteinByDate (userId ,date , goalPeriodId );//오늘 추천된 단백질 그램수
294+ double fat = getRecommendedFatByDate (userId ,date , goalPeriodId );//오늘 추천된 지방 그램수
295+ int caloriePercentage = (int )(calorie /recommendedCalories );
296+ String calorieStatus = HomeMealPercentageStatus (caloriePercentage );
297+
298+
304299
305300 return HomeResultDTO .HomeMealPlanDTO .builder ()
306- .calorie (calorie )
301+ .todayRecommendedCalories (recommendedCalories )
302+ .todayConsumedCalorie (calorie )
303+ .calorieStatus (calorieStatus )
304+ .caloriePercentage (caloriePercentage )
307305 .carb (carb )
308306 .fat (fat )
309307 .protein (protein )
310308 .build ();
311309 }
312310
313- public HomeFullResponseDTO getHomeFullResponse (Long userId ) {
311+ public HomeFullResponseDTO getHomeFullResponse (Long userId , Long goalPeriodId ) {
314312 return HomeFullResponseDTO .builder ()
315- .main (getHomeData (userId ))
316- .exercise (getHomeExercise (userId ))
317- .meal (getHomeMealPlan (userId ))
313+ .main (getHomeData (userId , goalPeriodId ))
314+ .exercise (getHomeExercise (userId , goalPeriodId ))
315+ .meal (getHomeMealPlan (userId , goalPeriodId ))
318316 .build ();
319317 }
320318}
0 commit comments