[add]余分なファイルをコミットしました

This commit is contained in:
2026-09-18 10:16:49 +09:00
parent 15185672c4
commit 571fcc6d90
7 changed files with 177 additions and 37 deletions

View File

@@ -13,7 +13,7 @@ public class CategoryAnalysisModel {
private Integer previousMonthAmount; private Integer previousMonthAmount;
private Integer previousMonthDifferenceAmount; private Integer monthDifferenceAmount;
private Integer courseLimitAmount; private Integer courseLimitAmount;

View File

@@ -1,9 +1,21 @@
package com.example.demo.app.domain.service.impl; package com.example.demo.app.domain.service.impl;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import com.example.demo.app.domain.model.AnalysisModel; import com.example.demo.app.domain.model.AnalysisModel;
import com.example.demo.app.domain.model.CategoryAnalysisModel;
import com.example.demo.app.domain.service.AnalysisService; import com.example.demo.app.domain.service.AnalysisService;
import com.example.demo.app.infra.entity.AllocationEntity;
import com.example.demo.app.infra.entity.CategoryEntity;
import com.example.demo.app.infra.entity.UserEntity;
import com.example.demo.app.infra.repository.AllocationRepository;
import com.example.demo.app.infra.repository.CategoryRepository;
import com.example.demo.app.infra.repository.TransactionRepository;
import com.example.demo.app.infra.repository.UserRepository;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
@@ -12,29 +24,119 @@ import lombok.RequiredArgsConstructor;
public class AnalysisServiceImpl implements AnalysisService { public class AnalysisServiceImpl implements AnalysisService {
//Repositoryの呼び出し //Repositoryの呼び出し
// private final TransactionRepository transactionRepository; private final TransactionRepository transactionRepository;
// private final AllocationRepository allocationRepository; private final AllocationRepository allocationRepository;
// private final UserRepository userRepository; private final UserRepository userRepository;
// private final CategoryRepository categoryRepository; private final CategoryRepository categoryRepository;
@Override @Override
public AnalysisModel getAnalysis(Long userId) { public AnalysisModel getAnalysis(Long userId) {
// ユーザー情報取得、月別使用可能資金取得 // ユーザー情報取得、月別使用可能資金取得
UserEntity userEntity = userRepository.findById(userId).orElseThrow();
// コースID取得
Long courseId = userEntity.getCourseEntity().getCourseId();
// 月別使用可能資金取得
Integer monthlyBudget =userEntity.getMonthlyBudget();
// 今月の期間取得 // 今月の期間取得
LocalDate currentMonthStartDate = LocalDate.now().withDayOfMonth(1);
LocalDate currentMonthEndDate = LocalDate.now().withDayOfMonth(LocalDate.now().lengthOfMonth());
// 前月の期間取得 // 前月の期間取得
LocalDate previousMonthStartDate = LocalDate.now().minusMonths(1).withDayOfMonth(1);
LocalDate previousMonthEndDate = LocalDate.now().minusMonths(1).withDayOfMonth(LocalDate.now().minusMonths(1).lengthOfMonth());
// 今月の支出取得 // 今月の支出取得
Integer currentMonthTotalAmount = transactionRepository.findTotalExpense(currentMonthStartDate, currentMonthEndDate, userId);
if (currentMonthTotalAmount == null) {
currentMonthTotalAmount = 0;
}
// 前月の支出取得 // 前月の支出取得
Integer previousMonthTotalAmount = transactionRepository.findTotalExpense(previousMonthStartDate, previousMonthEndDate, userId);
if (previousMonthTotalAmount == null) {
previousMonthTotalAmount = 0;
}
//総支出差額算出
Integer totalDifferenceAmount = currentMonthTotalAmount - previousMonthTotalAmount;
//カテゴリ一覧取得
List<CategoryAnalysisModel> categoryAnalysisModelList = new ArrayList<>();
List<CategoryEntity> categoryEntityList = categoryRepository.findAll();
List<AllocationEntity> allocationEntityList = allocationRepository.findByCourseEntity_CourseId(courseId);
//取得ループ
for (CategoryEntity categoryEntity : categoryEntityList) {
if (categoryEntity.getTransactionType() != 0) {
continue;
}
// カテゴリ別今月金額取得
Integer currentMonthAmount =
transactionRepository.findCategoryExpense(currentMonthStartDate,
currentMonthEndDate,
userId,
categoryEntity.getCategoryId());
// カテゴリ別前月金額取得
Integer previousMonthAmount =
transactionRepository.findCategoryExpense(previousMonthStartDate,
previousMonthEndDate,
userId,
categoryEntity.getCategoryId());
if (currentMonthAmount == null) {
currentMonthAmount = 0;
}
if (previousMonthAmount == null) {
previousMonthAmount = 0;
}
// カテゴリ別前月差額算出
Integer monthDifferenceAmount = currentMonthAmount - previousMonthAmount;
// コース配分取得 // コース配分取得
// カテゴリ別集計 AllocationEntity allocationEntity = allocationEntityList.stream().filter(allocation ->
// 前月との差額集計 allocation.getCategoryEntity()
//総支出算出 .getCategoryId()
// コース限度額算出、コース別差額算出 .equals(categoryEntity.getCategoryId()))
.findFirst()
.orElse(null);
Integer allocationRatio = 0;
if (allocationEntity != null) {
allocationRatio =
allocationEntity.getAllocationRatio();
}
// コース限度額算出
Integer courseLimitAmount = monthlyBudget * allocationRatio / 100;
// コース差額算出
Integer courseDifferenceAmount = currentMonthAmount - courseLimitAmount;
// CategoryAnalysisModel生成 // CategoryAnalysisModel生成
CategoryAnalysisModel categoryAnalysisModel = new CategoryAnalysisModel(
categoryEntity.getCategoryName(),
currentMonthAmount,
previousMonthAmount,
monthDifferenceAmount,
courseLimitAmount,
courseDifferenceAmount);
categoryAnalysisModelList.add(categoryAnalysisModel);
}
// AnalysisModelの生成 // AnalysisModelの生成
return new AnalysisModel(currentMonthTotalAmount, previousMonthTotalAmount, totalDifferenceAmount, categoryAnalysisModelList);
return null;
} }
} }

View File

@@ -16,5 +16,6 @@ public interface UserRepository extends JpaRepository<UserEntity, Long> {
String mailAddress, String mailAddress,
String password); String password);
Optional<UserEntity> findByMailAddress(String mailAddress);
} }

View File

@@ -1,10 +1,15 @@
package com.example.demo.app.presentation.controller; package com.example.demo.app.presentation.controller;
import jakarta.servlet.http.HttpSession;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.ui.Model; import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import com.example.demo.app.domain.model.AnalysisModel;
import com.example.demo.app.domain.model.LoginModel;
import com.example.demo.app.domain.service.AnalysisService; import com.example.demo.app.domain.service.AnalysisService;
import com.example.demo.app.presentation.form.AnalysisForm;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
@@ -16,13 +21,18 @@ public class AnalysisController {
private final AnalysisService analysisService; private final AnalysisService analysisService;
@GetMapping("/analysis") @GetMapping("/analysis")
public String showAnalysis(Model model) { public String showAnalysis(Model model, HttpSession session) {
//ログインユーザーの取得(セッション) //ログインユーザーの取得(セッション)
LoginModel loginUser = (LoginModel) session.getAttribute("loginUser");
//分析結果の取得(Service) //分析結果の取得(Service)
AnalysisModel analysisModel = analysisService.getAnalysis(loginUser.getUserId());
AnalysisForm analysisForm = AnalysisForm.convertFrom(analysisModel);
//model格納 //model格納
model.addAttribute("analysisForm", analysisForm);
return "analysis"; return "analysis";
} }

View File

@@ -12,7 +12,7 @@
<table class="table text-center"> <table class="table text-center">
<thead> <thead>
<tr> <tr>
<th scope="col">カテゴリ別</th> <th scope="col"></th>
<th scope="col">支出</th> <th scope="col">支出</th>
<th scope="col">前月比</th> <th scope="col">前月比</th>
</tr> </tr>
@@ -20,8 +20,17 @@
<tbody> <tbody>
<tr> <tr>
<th scope="row">総支出</th> <th scope="row">総支出</th>
<td>¥234,000</td> <td th:text="'¥' + ${analysisForm.currentMonthTotalAmount}">
<td>+¥38,000</td> 0
</td>
<td th:text="${analysisForm.totalDifferenceAmount >= 0}
? '+' + ${analysisForm.totalDifferenceAmount}
: '-' + ${analysisForm.totalDifferenceAmount}"
th:class="${analysisForm.totalDifferenceAmount >= 0}
? 'color-danger;'
: 'color-primary;'">
0
</td>
</tr> </tr>
</tbody> </tbody>
</table> </table>
@@ -33,24 +42,42 @@
<tr> <tr>
<th scope="col">カテゴリ別</th> <th scope="col">カテゴリ別</th>
<th scope="col">支出</th> <th scope="col">支出</th>
<th scope="col">前月比<br>コース比</th> <th scope="col">
前月比
<br>
コース比
</th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
<tr> <tr th:each="category : ${analysisForm.categoryAnalysisFormList}">
<th scope="row">食費</th>
<td>¥34,000</td> <th scope="row"
<td>+¥8,000<br>+¥1,100</td> th:text="${category.categoryName}">
</tr> 食費
<tr> </th>
<th scope="row">交通費</th>
<td>¥14,000</td> <td th:text="'¥' + ${category.currentMonthAmount}">
<td>+¥8,000<br>+¥1,100</td> 0
</tr> </td>
<tr>
<th scope="row">日用品</th> <td>
<td>¥4,000</td> <span th:text="${category.monthDifferenceAmount >= 0}
<td>+¥8,000<br>+¥1,100</td> ? '+' + ${category.monthDifferenceAmount}
: '-' + ${category.monthDifferenceAmount}"
th:class="${category.monthDifferenceAmount >= 0}
? 'color-danger;'
: 'color-primary;'">
</span>
<br>
<span th:text="${category.courseDifferenceAmount >= 0}
? '+' + ${category.courseDifferenceAmount}
: '-' + ${category.courseDifferenceAmount}"
th:class="${category.courseDifferenceAmount >= 0}
? 'color-danger;'
: 'color-primary;'">
</span>
</td>
</tr> </tr>
</tbody> </tbody>
</table> </table>

View File

@@ -1,5 +1,5 @@
<!DOCTYPE html> <!DOCTYPE html>
<html> <html lang="ja"xmlns:th="http://www.thymeleaf.org">
<head> <head>
<meta charset="UTF-8"> <meta charset="UTF-8">
<title>新規会員登録確認</title> <title>新規会員登録確認</title>