From a73516297473893d456bd5722e04c26c10e6b95a Mon Sep 17 00:00:00 2001 From: n-kawahito Date: Thu, 17 Sep 2026 14:43:11 +0900 Subject: [PATCH 1/2] =?UTF-8?q?[EDIT]=E6=94=AF=E5=87=BA=E5=88=86=E6=9E=90?= =?UTF-8?q?=E3=81=AE=E3=81=9F=E3=82=81=E3=81=AE=E3=82=B5=E3=83=BC=E3=83=93?= =?UTF-8?q?=E3=82=B9=E5=AE=9F=E8=A3=85=E9=83=A8=E5=88=86=E3=82=92=E8=A8=98?= =?UTF-8?q?=E8=BF=B0=E3=81=97=E3=81=BE=E3=81=97=E3=81=9F=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/impl/AnalysisServiceImpl.java | 132 ++++++++++++++++-- 1 file changed, 117 insertions(+), 15 deletions(-) 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 66f4bbc..d0f3388 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 @@ -1,9 +1,21 @@ 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 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.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; @@ -12,29 +24,119 @@ import lombok.RequiredArgsConstructor; public class AnalysisServiceImpl implements AnalysisService { //Repositoryの呼び出し -// private final TransactionRepository transactionRepository; -// private final AllocationRepository allocationRepository; -// private final UserRepository userRepository; -// private final CategoryRepository categoryRepository; + private final TransactionRepository transactionRepository; + private final AllocationRepository allocationRepository; + private final UserRepository userRepository; + private final CategoryRepository categoryRepository; @Override public AnalysisModel getAnalysis(Long userId) { // ユーザー情報取得、月別使用可能資金取得 + UserEntity userEntity = userRepository.findById(userId).orElseThrow(); + + // コースID取得 + Long courseId = userEntity.getCourseEntity().getCourseId(); + + // 月別使用可能資金取得 + Integer monthlyBudget =userEntity.getMonthlyBudget(); // 今月の期間取得 - // 前月の期間取得 - // 今月の支出取得 - // 前月の支出取得 - // コース配分取得 - // カテゴリ別集計 - // 前月との差額集計 - //総支出算出 - // コース限度額算出、コース別差額算出 - // CategoryAnalysisModel生成 - // AnalysisModelの生成 + LocalDate currentMonthStartDate = LocalDate.now().withDayOfMonth(1); + LocalDate currentMonthEndDate = LocalDate.now().withDayOfMonth(LocalDate.now().lengthOfMonth()); - return null; + // 前月の期間取得 + 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 categoryAnalysisModelList = new ArrayList<>(); + List categoryEntityList = categoryRepository.findAll(); + List 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 = new CategoryAnalysisModel( + categoryEntity.getCategoryName(), + currentMonthAmount, + previousMonthAmount, + monthDifferenceAmount, + courseLimitAmount, + courseDifferenceAmount); + + categoryAnalysisModelList.add(categoryAnalysisModel); + } + + // AnalysisModelの生成 + return new AnalysisModel(currentMonthTotalAmount, previousMonthTotalAmount, totalDifferenceAmount, categoryAnalysisModelList); } } From ca7e93908dd38b38dbeb54a610e6768049da00e4 Mon Sep 17 00:00:00 2001 From: n-kawahito Date: Thu, 17 Sep 2026 14:43:44 +0900 Subject: [PATCH 2/2] =?UTF-8?q?[FIX]HOTFIX=E3=81=AB=E5=90=88=E3=82=8F?= =?UTF-8?q?=E3=81=9B=E3=81=A6=E3=80=81=E5=A4=89=E6=95=B0=E5=90=8D=E3=82=92?= =?UTF-8?q?=E5=A4=89=E6=9B=B4=E3=81=97=E3=81=BE=E3=81=97=E3=81=9F=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../example/demo/app/domain/model/CategoryAnalysisModel.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/example/demo/app/domain/model/CategoryAnalysisModel.java b/src/main/java/com/example/demo/app/domain/model/CategoryAnalysisModel.java index 3a94930..47cdadf 100644 --- a/src/main/java/com/example/demo/app/domain/model/CategoryAnalysisModel.java +++ b/src/main/java/com/example/demo/app/domain/model/CategoryAnalysisModel.java @@ -13,7 +13,7 @@ public class CategoryAnalysisModel { private Integer previousMonthAmount; - private Integer previousMonthDifferenceAmount; + private Integer monthDifferenceAmount; private Integer courseLimitAmount;