[EDIT]達成率が画面に表示されるようにしました
This commit is contained in:
@@ -24,6 +24,8 @@ public class GoalModel {
|
||||
|
||||
private Integer goalComplete;
|
||||
|
||||
private Integer achievementRate; // 達成率
|
||||
|
||||
public static GoalModel convertFrom(GoalEntity goalEntity) {
|
||||
return new GoalModel(
|
||||
goalEntity.getGoalId(),
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -29,7 +29,34 @@ 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
|
||||
|
||||
@@ -29,8 +29,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);
|
||||
@@ -45,9 +43,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);
|
||||
|
||||
@@ -25,6 +25,8 @@ public class GoalForm {
|
||||
|
||||
private Integer goalComplete;
|
||||
|
||||
private Integer achievementRate; // 達成率
|
||||
|
||||
public static GoalForm convertFrom(GoalModel goalModel) {
|
||||
return new GoalForm(
|
||||
goalModel.getGoalId(),
|
||||
@@ -32,7 +34,8 @@ public class GoalForm {
|
||||
goalModel.getGoalSubtitle(),
|
||||
goalModel.getTargetAmount(),
|
||||
goalModel.getDeadline(),
|
||||
goalModel.getGoalComplete());
|
||||
goalModel.getGoalComplete(),
|
||||
goalModel.getAchievementRate());
|
||||
}
|
||||
|
||||
// public static List<GoalForm> convertFrom(List<GoalModel> goalModelList) {
|
||||
|
||||
@@ -13,22 +13,22 @@
|
||||
|
||||
<div>
|
||||
<label>目標名</label>
|
||||
<input type="text" th:field="*{goalTitle}">
|
||||
<input type="text" th:field="*{goalTitle}" required>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<label>目標説明</label>
|
||||
<textarea th:field="*{goalSubtitle}"></textarea>
|
||||
<textarea th:field="*{goalSubtitle}" required></textarea>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<label>目標金額</label>
|
||||
<input type="number" th:field="*{targetAmount}">
|
||||
<input type="number" th:field="*{targetAmount}" required min="1">
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<label>目標達成期限</label>
|
||||
<input type="date" th:field="*{deadline}">
|
||||
<input type="date" th:field="*{deadline}" th:min="${#temporals.format(#temporals.createNow(), 'yyyy-MM-dd')}" required>
|
||||
</div>
|
||||
<button type="submit">更新する</button>
|
||||
|
||||
|
||||
@@ -27,6 +27,13 @@
|
||||
<th>目標達成期限</th>
|
||||
<td th:text="${goalForm.deadline}"></td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<th>達成率</th>
|
||||
<td th:text="${goalForm.achievementRate} + '%'"></td>
|
||||
</tr>
|
||||
|
||||
|
||||
</table>
|
||||
|
||||
<a th:href="@{/goal/edit}">編集する</a>
|
||||
|
||||
Reference in New Issue
Block a user