diff --git a/src/main/java/com/example/demo/app/domain/model/GoalModel.java b/src/main/java/com/example/demo/app/domain/model/GoalModel.java index 94a57d2..74845d1 100644 --- a/src/main/java/com/example/demo/app/domain/model/GoalModel.java +++ b/src/main/java/com/example/demo/app/domain/model/GoalModel.java @@ -23,6 +23,8 @@ public class GoalModel { private LocalDate deadline; private Integer goalComplete; + + private Integer achievementRate; // 達成率 public static GoalModel convertFrom(GoalEntity goalEntity) { return new GoalModel( @@ -31,7 +33,8 @@ public class GoalModel { goalEntity.getGoalSubtitle(), goalEntity.getTargetAmount(), goalEntity.getDeadline(), - goalEntity.getGoalComplete()); + goalEntity.getGoalComplete(), + null); } public static GoalEntity convertTo(GoalModel goalModel) { @@ -52,7 +55,8 @@ public class GoalModel { goalForm.getGoalSubtitle(), goalForm.getTargetAmount(), goalForm.getDeadline(), - goalForm.getGoalComplete()); + goalForm.getGoalComplete() + ,null); } } \ No newline at end of file diff --git a/src/main/java/com/example/demo/app/domain/service/impl/GoalServiceImpl.java b/src/main/java/com/example/demo/app/domain/service/impl/GoalServiceImpl.java index 56f4ea4..4654d05 100644 --- a/src/main/java/com/example/demo/app/domain/service/impl/GoalServiceImpl.java +++ b/src/main/java/com/example/demo/app/domain/service/impl/GoalServiceImpl.java @@ -29,9 +29,36 @@ public class GoalServiceImpl implements GoalService { UserEntity userEntity = userRepository.findById(userId).orElseThrow(); GoalEntity goalEntity = userEntity.getGoalEntity(); - return GoalModel.convertFrom(goalEntity); - } + // 収支の合計を計算(収入はプラス、支出はマイナス) + int transactionTotal = userEntity.getTransactionEntityList().stream() + .mapToInt(t -> { + int sign = t.getCategoryEntity().getTransactionType() == 1 ? 1 : -1; + return sign * t.getAmount(); + }) + .sum(); + + // 現在の資産 = 貯蓄金 + 収支合計 + int currentAmount = userEntity.getSaving() + transactionTotal; + + // 達成率(%) = 現在の資産 ÷ 目標金額 × 100 + int achievementRate = + (goalEntity.getTargetAmount() == null) + ? 0 : + (int) ((currentAmount * 100.0) / goalEntity.getTargetAmount()); + + GoalModel goalModel = GoalModel.convertFrom(goalEntity); + + return new GoalModel( + goalModel.getGoalId(), + goalModel.getGoalTitle(), + goalModel.getGoalSubtitle(), + goalModel.getTargetAmount(), + goalModel.getDeadline(), + goalModel.getGoalComplete(), + achievementRate); + } + @Override public GoalModel createGoal(GoalModel goalModel) { diff --git a/src/main/java/com/example/demo/app/presentation/controller/GoalController.java b/src/main/java/com/example/demo/app/presentation/controller/GoalController.java index 827e05b..216fd73 100644 --- a/src/main/java/com/example/demo/app/presentation/controller/GoalController.java +++ b/src/main/java/com/example/demo/app/presentation/controller/GoalController.java @@ -29,9 +29,7 @@ public class GoalController { LoginModel loginUser = (LoginModel) session.getAttribute("loginUser"); Long userId = loginUser.getUserId(); - //テスト用 -// Long userId = 1L; - + GoalModel goalModel = goalService.getGoalByUserId(userId); GoalForm goalForm = GoalForm.convertFrom(goalModel); @@ -44,9 +42,6 @@ public class GoalController { LoginModel loginUser = (LoginModel) session.getAttribute("loginUser"); Long userId = loginUser.getUserId(); - - //テスト用 -// Long userId = 1L; GoalModel goalModel = goalService.getGoalByUserId(userId); GoalForm goalForm = GoalForm.convertFrom(goalModel); @@ -61,9 +56,6 @@ public class GoalController { LoginModel loginUser = (LoginModel) session.getAttribute("loginUser"); Long userId = loginUser.getUserId(); - //テスト用 -// Long userId = 1L; - // 本人の目標かどうかを確認してから更新する GoalModel currentGoal = goalService.getGoalByUserId(userId); diff --git a/src/main/java/com/example/demo/app/presentation/form/GoalForm.java b/src/main/java/com/example/demo/app/presentation/form/GoalForm.java index 42e5368..651a62c 100644 --- a/src/main/java/com/example/demo/app/presentation/form/GoalForm.java +++ b/src/main/java/com/example/demo/app/presentation/form/GoalForm.java @@ -24,6 +24,8 @@ public class GoalForm { private LocalDate deadline; private Integer goalComplete; + + private Integer achievementRate; // 達成率 public static GoalForm convertFrom(GoalModel goalModel) { return new GoalForm( @@ -32,7 +34,8 @@ public class GoalForm { goalModel.getGoalSubtitle(), goalModel.getTargetAmount(), goalModel.getDeadline(), - goalModel.getGoalComplete()); + goalModel.getGoalComplete(), + goalModel.getAchievementRate()); } // public static List convertFrom(List goalModelList) { diff --git a/src/main/resources/templates/goal_edit.html b/src/main/resources/templates/goal_edit.html index 0d3601e..fad05c1 100644 --- a/src/main/resources/templates/goal_edit.html +++ b/src/main/resources/templates/goal_edit.html @@ -13,22 +13,22 @@
- +
- +
- +
- +
diff --git a/src/main/resources/templates/goal_view.html b/src/main/resources/templates/goal_view.html index 348f7e4..5ca8d3e 100644 --- a/src/main/resources/templates/goal_view.html +++ b/src/main/resources/templates/goal_view.html @@ -27,6 +27,13 @@ 目標達成期限 + + + 達成率 + + + + 編集する