Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] 한 주 예산 금액 조회 로직 수정 #51

Merged
merged 1 commit into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/main/java/umc/haruchi/converter/DayBudgetConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import umc.haruchi.domain.Expenditure;
import umc.haruchi.domain.Income;
import umc.haruchi.domain.MonthBudget;
import umc.haruchi.domain.enums.ClosingStatus;
import umc.haruchi.domain.enums.DayBudgetStatus;
import umc.haruchi.web.dto.DayBudgetRequestDTO;
import umc.haruchi.web.dto.DayBudgetResponseDTO;
Expand All @@ -14,11 +15,12 @@

public class DayBudgetConverter {

public static DayBudget toDayBudget(Integer dayBudget, int day, DayBudgetStatus status, MonthBudget monthBudget) {
public static DayBudget toDayBudget(Integer dayBudget, int day, DayBudgetStatus status, ClosingStatus closingStatus, MonthBudget monthBudget) {
return DayBudget.builder()
.dayBudget(dayBudget)
.day(Long.valueOf(day))
.dayBudgetStatus(status)
.closingStatus(closingStatus)
.monthBudget(monthBudget)
.build();
}
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/umc/haruchi/converter/MonthBudgetConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ public static MonthBudgetResponseDTO.GetMonthUsedPercentResultDTO toGetMonthUsed
}

public static MonthBudgetResponseDTO.GetWeekBudgetResultDTO toGetWeekBudgetResultDTO(DayBudget dayBudget) {
if(dayBudget == null)
return null;

return MonthBudgetResponseDTO.GetWeekBudgetResultDTO.builder()
.day(dayBudget.getDay())
.dayBudget(dayBudget.getDayBudget())
Expand Down
1 change: 0 additions & 1 deletion src/main/java/umc/haruchi/domain/DayBudget.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ public class DayBudget extends BaseEntity {
private Integer dayBudget;

@Enumerated(EnumType.STRING)
@Column(nullable = false, columnDefinition = "VARCHAR(10) DEFAULT 'ETC'")
private ClosingStatus closingStatus;

@Enumerated(EnumType.STRING)
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/umc/haruchi/domain/enums/ClosingStatus.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package umc.haruchi.domain.enums;

public enum ClosingStatus {
ZERO, PLUS, MINUS, ETC
ZERO, PLUS, MINUS
}
40 changes: 23 additions & 17 deletions src/main/java/umc/haruchi/service/MonthBudgetService.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import umc.haruchi.domain.DayBudget;
import umc.haruchi.domain.Member;
import umc.haruchi.domain.MonthBudget;
import umc.haruchi.domain.enums.ClosingStatus;
import umc.haruchi.domain.enums.DayBudgetStatus;
import umc.haruchi.repository.DayBudgetRepository;
import umc.haruchi.repository.MemberRepository;
Expand Down Expand Up @@ -115,12 +116,13 @@ public List<DayBudget> distributeDayBudgets(Long memberId) {
//현재 일자의 전날까지는 status가 INACTIVE인 dayBudget을 생성하고
//현재 일자부터 말일까지는 status가 ACTIVE인 dayBudget 생성
DayBudgetStatus status = (day < nowDay) ? DayBudgetStatus.INACTIVE : DayBudgetStatus.ACTIVE;
ClosingStatus closingStatus = (day < nowDay) ? ClosingStatus.ZERO : null;
//현재 일자 전날까지는 0으로 설정, 현재 일자부터 말일까지는 distributedAmount로 설정
int nowDistributedAmount = (day < nowDay) ? 0 : distributedAmount;

//이미 생성된 dayBudget이 있으면 update, 아니면 create
DayBudget dayBudget = dayBudgetRepository.findByMonthBudgetAndDay(monthBudget, currentDay)
.orElseGet(() -> DayBudgetConverter.toDayBudget(nowDistributedAmount, currentDay, status, monthBudget));
.orElseGet(() -> DayBudgetConverter.toDayBudget(nowDistributedAmount, currentDay, status, closingStatus, monthBudget));

dayBudget.setStatus(status);
dayBudget.setDayBudget(nowDistributedAmount);
Expand Down Expand Up @@ -151,11 +153,12 @@ public List<DayBudget> createDayBudgetsWithMonth(Long memberId, Integer year, In
final int currentDay = day;
//이전 달의 dayBudget을 모두 INACTIVE로 생성
DayBudgetStatus status = DayBudgetStatus.INACTIVE;
ClosingStatus closingStatus = ClosingStatus.ZERO;
int nowDistributedAmount = 0;

//dayBudget 생성 혹은 업뎃
DayBudget dayBudget = dayBudgetRepository.findByMonthBudgetAndDay(monthBudget, day)
.orElseGet(() -> DayBudgetConverter.toDayBudget(nowDistributedAmount, currentDay, status, monthBudget));
.orElseGet(() -> DayBudgetConverter.toDayBudget(nowDistributedAmount, currentDay, status, closingStatus, monthBudget));

dayBudget.setStatus(status);
dayBudget.setDayBudget(nowDistributedAmount);
Expand Down Expand Up @@ -198,8 +201,6 @@ public double getMonthUsedPercent(Long memberId) {
return Math.round(monthUsedAmountPercent*1000000)/1000000.0;
}

//저번 달의 monthBudget이 없다면 생성해야해서 Transactional
@Transactional
public List<DayBudget> getWeekBudget(Long memberId) {
LocalDate today = LocalDate.now();

Expand All @@ -216,45 +217,50 @@ public List<DayBudget> getWeekBudget(Long memberId) {

//이번 주 월요일 구하기
int firstDayOfWeek = today.getDayOfMonth() - dayInWeek;
System.out.println("시작 날짜: " + firstDayOfWeek);

//남은 일수의 dayBudget 구하기
List<DayBudget> dayBudgets = new ArrayList<>();

//이번주에 저번달이 포함되어 있다면 저번 달의 monthBudget 생성.
//더 좋은 로직이 있는 지 고민..
//이번주에 저번달이 포함되어 있다면 저번 달의 monthBudget 가져옴
//저번 달의 monthBudget 없으면 dayBudget에 null 채우기
Integer daysInLastMonth = 0;
Integer newYear = 0;
Integer newMonth = 0;

if(firstDayOfWeek < 1) {
//헌재 1월이라면 작년 12월의 monthBudget 생성
//헌재 1월이라면 작년 12월의 monthBudget
if(monthBudget.getMonth() == 1) {
createDayBudgetsWithMonth(memberId, today.getYear()-1, 12, 0L);
daysInLastMonth = YearMonth.of(today.getYear()-1, 12).lengthOfMonth();
newYear = today.getYear() - 1;
newMonth = 12;
}
//아니라면 저번달의 monthBudget 생성
//아니라면 저번달의 monthBudget
else {
createDayBudgetsWithMonth(memberId, today.getYear(), monthBudget.getMonth()-1, 0L);
daysInLastMonth = YearMonth.of(today.getYear(), monthBudget.getMonth()-1).lengthOfMonth();
newYear = today.getYear();
newMonth = monthBudget.getMonth()-1;
}

//지난달 monthBudget 찾기
//지난달 monthBudget 찾기(없으면 null)
MonthBudget lastMonthBudget = monthBudgetRepository.findByMemberIdAndYearAndMonth(memberId, newYear, newMonth)
.orElseThrow(() -> new MonthBudgetHandler(ErrorStatus.MONTH_BUDGET_NOT_FOUND));
.orElseGet(() -> null);

//dayBudget 찾기
for(int i=0; i< 7; i++){
int plusDay = i;
//저번달 날짜라면 저번달 monthBudget에서 찾기
if(firstDayOfWeek + i < 1) {
plusDay += daysInLastMonth;
DayBudget dayBudget = dayBudgetRepository.findByMonthBudgetAndDay(lastMonthBudget, firstDayOfWeek + plusDay)
.orElseThrow(() -> new DayBudgetHandler(NOT_SOME_DAY_BUDGET));
dayBudgets.add(dayBudget);
//저번달 날짜고 lastMonthBudget이 없다면 null 저장
if(lastMonthBudget == null) {
DayBudget dayBudget = null;
dayBudgets.add(dayBudget);
}
//저번달 날짜고 lastMonthBudget이 있다면 lastMonthBudget의 dayBudget 저장
else {
DayBudget dayBudget = dayBudgetRepository.findByMonthBudgetAndDay(lastMonthBudget, firstDayOfWeek + plusDay)
.orElseThrow(() -> new DayBudgetHandler(NOT_SOME_DAY_BUDGET));
dayBudgets.add(dayBudget);
}
}
//이번달 날짜라면 이번달 monthBudget에서 찾기
else {
Expand Down
Loading