From 79b1abeac6373e19851f2901325e86bc65f4a947 Mon Sep 17 00:00:00 2001 From: n-kawahito Date: Fri, 18 Sep 2026 16:03:32 +0900 Subject: [PATCH] =?UTF-8?q?[EDIT]=E4=BA=8C=E6=AC=A1=E5=AE=9F=E8=A3=85?= =?UTF-8?q?=E3=81=A7=E3=81=82=E3=82=8B=E6=94=AF=E5=87=BA=E5=88=86=E6=9E=90?= =?UTF-8?q?=E3=81=AE=E5=B9=B4=E6=9C=88=E8=A1=A8=E7=A4=BA=E3=82=92=E5=AE=9F?= =?UTF-8?q?=E8=A3=85=E3=81=97=E3=81=BE=E3=81=97=E3=81=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/domain/service/AnalysisService.java | 5 ++- .../service/impl/AnalysisServiceImpl.java | 13 ++++--- .../controller/AnalysisController.java | 34 +++++++++++++++++-- src/main/resources/templates/analysis.html | 18 ++++++++++ 4 files changed, 62 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/example/demo/app/domain/service/AnalysisService.java b/src/main/java/com/example/demo/app/domain/service/AnalysisService.java index 9bcc70c..1f6b433 100644 --- a/src/main/java/com/example/demo/app/domain/service/AnalysisService.java +++ b/src/main/java/com/example/demo/app/domain/service/AnalysisService.java @@ -4,6 +4,9 @@ import com.example.demo.app.domain.model.AnalysisModel; public interface AnalysisService { - AnalysisModel getAnalysis(Long userId); + AnalysisModel getAnalysis( + Long userId, + Integer year, + Integer month); } diff --git a/src/main/java/com/example/demo/app/domain/service/impl/AnalysisServiceImpl.java b/src/main/java/com/example/demo/app/domain/service/impl/AnalysisServiceImpl.java index 0764d41..b0caf13 100644 --- a/src/main/java/com/example/demo/app/domain/service/impl/AnalysisServiceImpl.java +++ b/src/main/java/com/example/demo/app/domain/service/impl/AnalysisServiceImpl.java @@ -30,7 +30,7 @@ public class AnalysisServiceImpl implements AnalysisService { private final CategoryRepository categoryRepository; @Override - public AnalysisModel getAnalysis(Long userId) { + public AnalysisModel getAnalysis(Long userId, Integer year, Integer month) { // ユーザー情報取得、月別使用可能資金取得 UserEntity userEntity = userRepository.findById(userId).orElseThrow(() -> @@ -44,12 +44,15 @@ public class AnalysisServiceImpl implements AnalysisService { Integer monthlyBudget =userEntity.getMonthlyBudget(); // 今月の期間取得 - LocalDate currentMonthStartDate = LocalDate.now().withDayOfMonth(1); - LocalDate currentMonthEndDate = LocalDate.now().withDayOfMonth(LocalDate.now().lengthOfMonth()); + LocalDate targetDate = LocalDate.of(year, month, 1); + LocalDate currentMonthStartDate = targetDate.withDayOfMonth(1); + LocalDate currentMonthEndDate = targetDate.withDayOfMonth(targetDate.lengthOfMonth()); // 前月の期間取得 - LocalDate previousMonthStartDate = LocalDate.now().minusMonths(1).withDayOfMonth(1); - LocalDate previousMonthEndDate = LocalDate.now().minusMonths(1).withDayOfMonth(LocalDate.now().minusMonths(1).lengthOfMonth()); + LocalDate previousDate = targetDate.minusMonths(1); + + LocalDate previousMonthStartDate = previousDate.withDayOfMonth(1); + LocalDate previousMonthEndDate = previousDate.withDayOfMonth(previousDate.lengthOfMonth()); // 今月の支出取得 Integer currentMonthTotalAmount = transactionRepository.findTotalExpense(currentMonthStartDate, currentMonthEndDate, userId); diff --git a/src/main/java/com/example/demo/app/presentation/controller/AnalysisController.java b/src/main/java/com/example/demo/app/presentation/controller/AnalysisController.java index a3a745f..2699e3b 100644 --- a/src/main/java/com/example/demo/app/presentation/controller/AnalysisController.java +++ b/src/main/java/com/example/demo/app/presentation/controller/AnalysisController.java @@ -1,10 +1,13 @@ package com.example.demo.app.presentation.controller; +import java.time.LocalDate; + import jakarta.servlet.http.HttpSession; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestParam; import com.example.demo.app.domain.model.AnalysisModel; import com.example.demo.app.domain.model.LoginModel; @@ -21,18 +24,45 @@ public class AnalysisController { private final AnalysisService analysisService; @GetMapping("/analysis") - public String showAnalysis(Model model, HttpSession session) { + public String showAnalysis( + @RequestParam(required = false) Integer year, + @RequestParam(required = false) Integer month, + Model model, + HttpSession session) { //ログインユーザーの取得(セッション) LoginModel loginUser = (LoginModel) session.getAttribute("loginUser"); + //初期年月表示 + LocalDate nowDate = LocalDate.now(); + + if (year == null) { + year = nowDate.getYear(); + } + + if (month == null) { + month = nowDate.getMonthValue(); + } + + //前月・翌月作成 + LocalDate targetDate = LocalDate.of(year, month, 1); + + LocalDate previousDate = targetDate.minusMonths(1); + LocalDate nextDate = targetDate.plusMonths(1); + //分析結果の取得(Service) - AnalysisModel analysisModel = analysisService.getAnalysis(loginUser.getUserId()); + AnalysisModel analysisModel = analysisService.getAnalysis(loginUser.getUserId(), year, month); AnalysisForm analysisForm = AnalysisForm.convertFrom(analysisModel); //model格納 model.addAttribute("analysisForm", analysisForm); + model.addAttribute("year", year); + model.addAttribute("month", month); + model.addAttribute("previousYear", previousDate.getYear()); + model.addAttribute("previousMonth", previousDate.getMonthValue()); + model.addAttribute("nextYear", nextDate.getYear()); + model.addAttribute("nextMonth", nextDate.getMonthValue()); return "analysis"; } diff --git a/src/main/resources/templates/analysis.html b/src/main/resources/templates/analysis.html index 82ad4f8..a44b40a 100644 --- a/src/main/resources/templates/analysis.html +++ b/src/main/resources/templates/analysis.html @@ -5,6 +5,7 @@ 分析画面 +
@@ -13,6 +14,23 @@

支出分析

+
+ + + ← + + +
+ + +
+ + + → + + +
+