Skip to content

Commit

Permalink
Merge pull request #50 from Team-Haruchi/feat/4-budgetredistribution#3
Browse files Browse the repository at this point in the history
[FIX] 한 주 예산 조회 리스트에 closingState추가, API 수정
  • Loading branch information
ohu-star authored Aug 5, 2024
2 parents bd45e11 + 0de889c commit 657ce55
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 28 deletions.
2 changes: 2 additions & 0 deletions src/main/java/umc/haruchi/converter/MonthBudgetConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import umc.haruchi.domain.DayBudget;
import umc.haruchi.domain.MonthBudget;
import umc.haruchi.domain.enums.DayBudgetStatus;
import umc.haruchi.web.dto.MonthBudgetRequestDTO;
import umc.haruchi.web.dto.MonthBudgetResponseDTO;

Expand Down Expand Up @@ -56,6 +57,7 @@ public static MonthBudgetResponseDTO.GetWeekBudgetResultDTO toGetWeekBudgetResul
.day(dayBudget.getDay())
.dayBudget(dayBudget.getDayBudget())
.status(dayBudget.getDayBudgetStatus())
.closingState(dayBudget.getClosingStatus())
.build();
}

Expand Down
9 changes: 9 additions & 0 deletions src/main/java/umc/haruchi/domain/DayBudget.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import umc.haruchi.domain.common.BaseEntity;
import umc.haruchi.domain.enums.ClosingStatus;
import umc.haruchi.domain.enums.DayBudgetStatus;

import java.time.LocalDate;
Expand Down Expand Up @@ -33,6 +34,10 @@ public class DayBudget extends BaseEntity {
@ColumnDefault("0")
private Integer dayBudget;

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

@Enumerated(EnumType.STRING)
@Column(nullable = false, columnDefinition = "VARCHAR(10) DEFAULT 'ACTIVE'")
private DayBudgetStatus dayBudgetStatus;
Expand Down Expand Up @@ -104,4 +109,8 @@ public void subAmount(Integer tossAmount) {
public void changeDayBudgetStatus() {
dayBudgetStatus = DayBudgetStatus.INACTIVE;
}

public void setClosingStatus(ClosingStatus closingStatus) {
this.closingStatus = closingStatus;
}
}
5 changes: 5 additions & 0 deletions src/main/java/umc/haruchi/domain/enums/ClosingStatus.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package umc.haruchi.domain.enums;

public enum ClosingStatus {
ZERO, PLUS, MINUS, ETC
}
29 changes: 20 additions & 9 deletions src/main/java/umc/haruchi/service/BudgetRedistributionService.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import umc.haruchi.apiPayload.exception.handler.MonthBudgetHandler;
import umc.haruchi.converter.BudgetRedistributionConverter;
import umc.haruchi.domain.*;
import umc.haruchi.domain.enums.ClosingStatus;
import umc.haruchi.domain.enums.DayBudgetStatus;
import umc.haruchi.repository.*;
import umc.haruchi.web.dto.BudgetRedistributionRequestDTO;
Expand Down Expand Up @@ -347,8 +348,15 @@ public PushPlusClosing closingPlusOrZero(BudgetRedistributionRequestDTO.createCl

long totalAmount = dayBudget.getDayBudget();

if(dayBudget.getDayBudget() > 0) {
dayBudget.setClosingStatus(ClosingStatus.PLUS);
} else {
dayBudget.setClosingStatus(ClosingStatus.ZERO);
}

//분배
if (dayBudget.getDayBudget() > 0) {

//고르게 넘기기(233원씩 넘겨준다고하면 200원씩 분배하고 33원씩 * 일수 -> 세이프박스)
//dayCount가 0일때 -> 마지막 날일 때 예외처리
if(dayCount == 0) {
Expand Down Expand Up @@ -393,19 +401,19 @@ public PushPlusClosing closingPlusOrZero(BudgetRedistributionRequestDTO.createCl
budget.subAmount(budget.getDayBudget() % 100);
}
});

dayBudget.subAmount(dayBudget.getDayBudget()); //dayBudget의 amount는 0으로
member.addSafeBox(safeBoxAmount);

} else if (request.getRedistributionOption().equals(SAFEBOX)) {
// 세이프 박스에 넘기기(233원을 넘겨준다고하면 233그대로 넘김)
// 세이프박스에 저장
member.addSafeBox(totalAmount);
dayBudget.subAmount(dayBudget.getDayBudget()); //dayBudget의 amount는 0으로
}
}

dayBudget.subAmount(dayBudget.getDayBudget()); //dayBudget의 amount는 0으로
PushPlusClosing pushPlusClosing = BudgetRedistributionConverter.toPlusClosing(request, dayBudget);
dayBudget.changeDayBudgetStatus(); //INACTIVE로 변경

member.setLastClosing();
return pushPlusClosingRepository.save(pushPlusClosing);
}
Expand Down Expand Up @@ -494,7 +502,7 @@ public PullMinusClosing closingMinus(BudgetRedistributionRequestDTO.createClosin
// 세이프박스에서 차감
member.subSafeBox(totalAmount);
}

dayBudget.setClosingStatus(ClosingStatus.MINUS);
member.addSafeBox(safeBoxAmount);
dayBudget.subAmount(dayBudget.getDayBudget()); // 양수일 때도 음수일 때도 0이 됨
PullMinusClosing pullMinusClosing = BudgetRedistributionConverter.toMinusClosing(request, dayBudget);
Expand All @@ -503,11 +511,14 @@ public PullMinusClosing closingMinus(BudgetRedistributionRequestDTO.createClosin
return pullMinusClosingRepository.save(pullMinusClosing);
}

public Long calculatingAmount(int year, int month, int day, Long amount, Long memberId) {
public Long calculatingAmount(int year, int month, int day, Long memberId) {

MonthBudget monthBudget = monthBudgetRepository.findByMemberIdAndYearAndMonth(memberId, year, month)
.orElseThrow(() -> new MonthBudgetHandler(MONTH_BUDGET_NOT_FOUND));

DayBudget dayBudget = dayBudgetRepository.findByMonthBudgetAndDay(monthBudget, day)
.orElseThrow(() -> new DayBudgetHandler(NOT_SOME_DAY_BUDGET));

//이번 달 남은 일 수 알아내기(본인 제외)
long dayCount = monthBudget.getDayBudgetList().stream()
.filter(budget -> budget.getDay() >= day).count() - 1;
Expand All @@ -517,13 +528,13 @@ public Long calculatingAmount(int year, int month, int day, Long amount, Long me
throw new BudgetRedistributionHandler(FINAL_DAY);
}

long totalAmount = amount;
long totalAmount = dayBudget.getDayBudget();

if(amount > 0) {
if(totalAmount > 0) {
return totalAmount / dayCount;
}
else if(amount < 0) {
totalAmount = -1 * amount; //양수로 변경
else if(totalAmount < 0) {
totalAmount = -1 * totalAmount; //양수로 변경
return totalAmount / dayCount;
}
else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,36 +24,22 @@ public class BudgetRedistributionController {

private final BudgetRedistributionService budgetRedistributionService;

@Operation(summary = "넘겨쓰기 API", description = "EVENLY(1/n),DATE(특정일),SAFEBOX(targetId null)")
@Operation(summary = "넘겨쓰기 API", description = "DATE(특정일)을 제외한 EVENLY(1/n) 와 SAFEBOX는 targetId를 null로 넘겨주세요")
@PostMapping("/push")
public ApiResponse<BudgetRedistributionResponseDTO.BudgetPushResultDTO> pushBudget(@Valid @RequestBody BudgetRedistributionRequestDTO.createPushDTO request,
@AuthenticationPrincipal MemberDetail memberDetail){
PushPlusClosing pushPlusClosing = budgetRedistributionService.push(request, memberDetail.getMember().getId());
return ApiResponse.onSuccess(BudgetRedistributionConverter.toBudgetPushResultDTO(pushPlusClosing));
}

@Operation(summary = "당겨쓰기 API", description = "EVENLY(1/n),DATE(특정일),SAFEBOX(sourceId null)")
@Operation(summary = "당겨쓰기 API", description = "DATE(특정일)을 제외한 EVENLY(1/n) 와 SAFEBOX는 sourceId를 null로 넘겨주세요")
@PostMapping("/pull")
public ApiResponse<BudgetRedistributionResponseDTO.BudgetPullResultDTO> pullBudget(@Valid @RequestBody BudgetRedistributionRequestDTO.createPullDTO request,
@AuthenticationPrincipal MemberDetail memberDetail){
PullMinusClosing pushMinusClosing = budgetRedistributionService.pull(request, memberDetail.getMember().getId());
return ApiResponse.onSuccess(BudgetRedistributionConverter.toBudgetPullResultDTO(pushMinusClosing));
}

// @Operation(summary = "지출 마감 API", description = "음수,양수,0으로 넘겨주시고, 0일 떄 옵션(EVENLY,SAFEBOX)은 ZERO로 넘겨주세요")
// @PostMapping("/closing")
// public ApiResponse<BudgetRedistributionResponseDTO.BudgetClosingResultDTO> closingBudget(@Valid @RequestBody BudgetRedistributionRequestDTO.createClosingDTO request,
// @AuthenticationPrincipal MemberDetail memberDetail){
// if(request.getAmount() >= 0) {
// PushPlusClosing pushPlusClosing = budgetRedistributionService.closingPlusOrZero(request, memberDetail.getMember().getId());
// return ApiResponse.onSuccess(BudgetRedistributionConverter.toBudgetClosingResultDTO(pushPlusClosing));
// }
// else {
// PullMinusClosing pullMinusClosing = budgetRedistributionService.closingMinus(request, memberDetail.getMember().getId());
// return ApiResponse.onSuccess(BudgetRedistributionConverter.toBudgetClosingResultDTO(pullMinusClosing));
// }
// }

@Operation(summary = "지출 마감 API", description = "0일떄는 옵션을 ZERO로 넘겨주시고, 마지막 날의 1/n 방식은 에러처리 되어있습니다.")
@PostMapping("/closing")
public ApiResponse<BudgetRedistributionResponseDTO.BudgetClosingResultDTO> closingBudget(@Valid @RequestBody BudgetRedistributionRequestDTO.createClosingDTO request,
Expand All @@ -69,14 +55,13 @@ public ApiResponse<BudgetRedistributionResponseDTO.BudgetClosingResultDTO> closi
}
}

@Operation(summary = "지출 마감에서 1/n경우의 하루 차감/분배 값 조회 API", description = "지출 마감 영수증에서 고르게 분배하기 선택 시 얼마씩 분배/차감할 지 알려주는 API입니다. +, - 값을 넘겨주세요.")
@Operation(summary = "지출 마감에서 1/n경우의 하루 차감/분배 값 조회 API", description = "지출 마감 영수증에서 고르게 분배하기 선택 시 얼마씩 분배/차감할 지 알려주는 API입니다.")
@GetMapping("/closing/amount")
public ApiResponse<BudgetRedistributionResponseDTO.GetCalculatedAmountResultDTO> getCalculatedAmount(@RequestParam @NotNull(message = "year 값은 필수 입력 값입니다.") int year,
@RequestParam @NotNull(message = "month 값은 필수 입력 값입니다.") int month,
@RequestParam @NotNull(message = "day 값은 필수 입력 값입니다.") int day,
@RequestParam @NotNull(message = "amount 값은 필수 입력 값입니다.") Long amount,
@AuthenticationPrincipal MemberDetail memberDetail){
Long calculatedAmount = budgetRedistributionService.calculatingAmount(year, month, day, amount, memberDetail.getMember().getId());
Long calculatedAmount = budgetRedistributionService.calculatingAmount(year, month, day, memberDetail.getMember().getId());
return ApiResponse.onSuccess(BudgetRedistributionConverter.toGetCalculatedAmountResultDTO(calculatedAmount));
}

Expand Down
2 changes: 2 additions & 0 deletions src/main/java/umc/haruchi/web/dto/MonthBudgetResponseDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import lombok.Getter;
import lombok.NoArgsConstructor;
import umc.haruchi.domain.DayBudget;
import umc.haruchi.domain.enums.ClosingStatus;
import umc.haruchi.domain.enums.DayBudgetStatus;

import java.time.LocalDate;
Expand Down Expand Up @@ -56,6 +57,7 @@ public static class GetWeekBudgetResultDTO {
Long day;
Integer dayBudget;
DayBudgetStatus status;
ClosingStatus closingState;
}

@Builder
Expand Down

0 comments on commit 657ce55

Please sign in to comment.