[EDIT]達成率が画面に表示されるようにしました
This commit is contained in:
@@ -23,6 +23,8 @@ public class GoalModel {
|
|||||||
private LocalDate deadline;
|
private LocalDate deadline;
|
||||||
|
|
||||||
private Integer goalComplete;
|
private Integer goalComplete;
|
||||||
|
|
||||||
|
private Integer achievementRate; // 達成率
|
||||||
|
|
||||||
public static GoalModel convertFrom(GoalEntity goalEntity) {
|
public static GoalModel convertFrom(GoalEntity goalEntity) {
|
||||||
return new GoalModel(
|
return new GoalModel(
|
||||||
@@ -31,7 +33,8 @@ public class GoalModel {
|
|||||||
goalEntity.getGoalSubtitle(),
|
goalEntity.getGoalSubtitle(),
|
||||||
goalEntity.getTargetAmount(),
|
goalEntity.getTargetAmount(),
|
||||||
goalEntity.getDeadline(),
|
goalEntity.getDeadline(),
|
||||||
goalEntity.getGoalComplete());
|
goalEntity.getGoalComplete(),
|
||||||
|
null);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static GoalEntity convertTo(GoalModel goalModel) {
|
public static GoalEntity convertTo(GoalModel goalModel) {
|
||||||
@@ -52,7 +55,8 @@ public class GoalModel {
|
|||||||
goalForm.getGoalSubtitle(),
|
goalForm.getGoalSubtitle(),
|
||||||
goalForm.getTargetAmount(),
|
goalForm.getTargetAmount(),
|
||||||
goalForm.getDeadline(),
|
goalForm.getDeadline(),
|
||||||
goalForm.getGoalComplete());
|
goalForm.getGoalComplete()
|
||||||
|
,null);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -29,9 +29,36 @@ public class GoalServiceImpl implements GoalService {
|
|||||||
|
|
||||||
UserEntity userEntity = userRepository.findById(userId).orElseThrow();
|
UserEntity userEntity = userRepository.findById(userId).orElseThrow();
|
||||||
GoalEntity goalEntity = userEntity.getGoalEntity();
|
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
|
@Override
|
||||||
public GoalModel createGoal(GoalModel goalModel) {
|
public GoalModel createGoal(GoalModel goalModel) {
|
||||||
|
|
||||||
|
|||||||
@@ -29,9 +29,7 @@ public class GoalController {
|
|||||||
LoginModel loginUser = (LoginModel) session.getAttribute("loginUser");
|
LoginModel loginUser = (LoginModel) session.getAttribute("loginUser");
|
||||||
|
|
||||||
Long userId = loginUser.getUserId();
|
Long userId = loginUser.getUserId();
|
||||||
//テスト用
|
|
||||||
// Long userId = 1L;
|
|
||||||
|
|
||||||
GoalModel goalModel = goalService.getGoalByUserId(userId);
|
GoalModel goalModel = goalService.getGoalByUserId(userId);
|
||||||
GoalForm goalForm = GoalForm.convertFrom(goalModel);
|
GoalForm goalForm = GoalForm.convertFrom(goalModel);
|
||||||
|
|
||||||
@@ -44,9 +42,6 @@ public class GoalController {
|
|||||||
|
|
||||||
LoginModel loginUser = (LoginModel) session.getAttribute("loginUser");
|
LoginModel loginUser = (LoginModel) session.getAttribute("loginUser");
|
||||||
Long userId = loginUser.getUserId();
|
Long userId = loginUser.getUserId();
|
||||||
|
|
||||||
//テスト用
|
|
||||||
// Long userId = 1L;
|
|
||||||
|
|
||||||
GoalModel goalModel = goalService.getGoalByUserId(userId);
|
GoalModel goalModel = goalService.getGoalByUserId(userId);
|
||||||
GoalForm goalForm = GoalForm.convertFrom(goalModel);
|
GoalForm goalForm = GoalForm.convertFrom(goalModel);
|
||||||
@@ -61,9 +56,6 @@ public class GoalController {
|
|||||||
LoginModel loginUser = (LoginModel) session.getAttribute("loginUser");
|
LoginModel loginUser = (LoginModel) session.getAttribute("loginUser");
|
||||||
|
|
||||||
Long userId = loginUser.getUserId();
|
Long userId = loginUser.getUserId();
|
||||||
//テスト用
|
|
||||||
// Long userId = 1L;
|
|
||||||
|
|
||||||
|
|
||||||
// 本人の目標かどうかを確認してから更新する
|
// 本人の目標かどうかを確認してから更新する
|
||||||
GoalModel currentGoal = goalService.getGoalByUserId(userId);
|
GoalModel currentGoal = goalService.getGoalByUserId(userId);
|
||||||
|
|||||||
@@ -24,6 +24,8 @@ public class GoalForm {
|
|||||||
private LocalDate deadline;
|
private LocalDate deadline;
|
||||||
|
|
||||||
private Integer goalComplete;
|
private Integer goalComplete;
|
||||||
|
|
||||||
|
private Integer achievementRate; // 達成率
|
||||||
|
|
||||||
public static GoalForm convertFrom(GoalModel goalModel) {
|
public static GoalForm convertFrom(GoalModel goalModel) {
|
||||||
return new GoalForm(
|
return new GoalForm(
|
||||||
@@ -32,7 +34,8 @@ public class GoalForm {
|
|||||||
goalModel.getGoalSubtitle(),
|
goalModel.getGoalSubtitle(),
|
||||||
goalModel.getTargetAmount(),
|
goalModel.getTargetAmount(),
|
||||||
goalModel.getDeadline(),
|
goalModel.getDeadline(),
|
||||||
goalModel.getGoalComplete());
|
goalModel.getGoalComplete(),
|
||||||
|
goalModel.getAchievementRate());
|
||||||
}
|
}
|
||||||
|
|
||||||
// public static List<GoalForm> convertFrom(List<GoalModel> goalModelList) {
|
// public static List<GoalForm> convertFrom(List<GoalModel> goalModelList) {
|
||||||
|
|||||||
@@ -13,22 +13,22 @@
|
|||||||
|
|
||||||
<div>
|
<div>
|
||||||
<label>目標名</label>
|
<label>目標名</label>
|
||||||
<input type="text" th:field="*{goalTitle}">
|
<input type="text" th:field="*{goalTitle}" required>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div>
|
<div>
|
||||||
<label>目標説明</label>
|
<label>目標説明</label>
|
||||||
<textarea th:field="*{goalSubtitle}"></textarea>
|
<textarea th:field="*{goalSubtitle}" required></textarea>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div>
|
<div>
|
||||||
<label>目標金額</label>
|
<label>目標金額</label>
|
||||||
<input type="number" th:field="*{targetAmount}">
|
<input type="number" th:field="*{targetAmount}" required min="1">
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div>
|
<div>
|
||||||
<label>目標達成期限</label>
|
<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>
|
</div>
|
||||||
<button type="submit">更新する</button>
|
<button type="submit">更新する</button>
|
||||||
|
|
||||||
|
|||||||
@@ -27,6 +27,13 @@
|
|||||||
<th>目標達成期限</th>
|
<th>目標達成期限</th>
|
||||||
<td th:text="${goalForm.deadline}"></td>
|
<td th:text="${goalForm.deadline}"></td>
|
||||||
</tr>
|
</tr>
|
||||||
|
|
||||||
|
<tr>
|
||||||
|
<th>達成率</th>
|
||||||
|
<td th:text="${goalForm.achievementRate} + '%'"></td>
|
||||||
|
</tr>
|
||||||
|
|
||||||
|
|
||||||
</table>
|
</table>
|
||||||
|
|
||||||
<a th:href="@{/goal/edit}">編集する</a>
|
<a th:href="@{/goal/edit}">編集する</a>
|
||||||
|
|||||||
Reference in New Issue
Block a user