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] localdate 코드 로직 수정 #56

Merged
merged 1 commit into from
Aug 12, 2024
Merged
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
33 changes: 18 additions & 15 deletions src/main/java/umc/haruchi/service/BudgetRedistributionService.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,16 @@ public class BudgetRedistributionService {
private final IncomeRepository incomeRepository;
private final ExpenditureRepository expenditureRepository;

LocalDate now = LocalDate.now(); // 현재 날짜 가져오기
int year = now.getYear();
int month = now.getMonthValue(); // 현재 월 가져오기
int day = now.getDayOfMonth(); // 현재 일 가져오기

//넘기기
@Transactional
public PushPlusClosing push(BudgetRedistributionRequestDTO.createPushDTO request, Long memberId) {
LocalDate now = LocalDate.now(); // 현재 날짜 가져오기
int localNowYear = now.getYear();
int localNowMonth = now.getMonthValue(); // 현재 월 가져오기
int localNowDay = now.getDayOfMonth(); // 현재 일 가져오기
Member member = memberRepository.findById(memberId)
.orElseThrow(() -> new MemberHandler(NO_MEMBER_EXIST)); //영속화
MonthBudget monthBudget = monthBudgetRepository.findByMemberIdAndYearAndMonth(member.getId(), year, month)
MonthBudget monthBudget = monthBudgetRepository.findByMemberIdAndYearAndMonth(member.getId(), localNowYear, localNowMonth)
.orElseThrow(() -> new MonthBudgetHandler(MONTH_BUDGET_NOT_FOUND));
DayBudget sourceBudget = dayBudgetRepository.findByMonthBudgetAndDay(monthBudget, request.getSourceDay())
.orElseThrow(() -> new DayBudgetHandler(NOT_DAY_BUDGET));
Expand All @@ -60,7 +59,7 @@ public PushPlusClosing push(BudgetRedistributionRequestDTO.createPushDTO request

//이번 달 남은 일 수 알아내기(본인 제외)
long dayCount = monthBudget.getDayBudgetList().stream()
.filter(dayBudget -> dayBudget.getDay() >= day).count() - 1;
.filter(dayBudget -> dayBudget.getDay() >= localNowDay).count() - 1;

//고르게 넘기기(233원씩 넘겨준다고하면 200원씩 분배하고 33원씩 * 일수 -> 세이프박스, 금액 차감된 source도 다시 확인 후 절사)
if (request.getRedistributionOption().equals(EVENLY)) {
Expand Down Expand Up @@ -89,14 +88,14 @@ public PushPlusClosing push(BudgetRedistributionRequestDTO.createPushDTO request

// 각 DayBudget에 분배
monthBudget.getDayBudgetList().stream()
.filter(dayBudget -> dayBudget.getDay() >= day && !dayBudget.getDay().equals(sourceDay))
.filter(dayBudget -> dayBudget.getDay() >= localNowDay && !dayBudget.getDay().equals(sourceDay))
.forEach(dayBudget -> {
dayBudget.pushAmount(distributedAmount);
});

// 세이프박스에 넣을 금액(전체금액 - 분배된 총 금액, 음수에서 양수가 된 값들 절사해서 넣어줌
long safeBoxAmount = totalAmount - distributedAmount * dayCount + sourceBudget.getDayBudget() % 100 + monthBudget.getDayBudgetList().stream()
.filter(dayBudget -> dayBudget.getDay() >= day && !dayBudget.getDay().equals(sourceDay))
.filter(dayBudget -> dayBudget.getDay() >= localNowDay && !dayBudget.getDay().equals(sourceDay))
.mapToLong(dayBudget -> {
if (dayBudget.getDayBudget() > 0) {
return dayBudget.getDayBudget() % 100;
Expand All @@ -109,7 +108,7 @@ public PushPlusClosing push(BudgetRedistributionRequestDTO.createPushDTO request

// 절사한 값 반영
monthBudget.getDayBudgetList().stream()
.filter(dayBudget -> dayBudget.getDay() >= day && !dayBudget.getDay().equals(sourceDay))
.filter(dayBudget -> dayBudget.getDay() >= localNowDay && !dayBudget.getDay().equals(sourceDay))
.forEach(dayBudget -> {
if (dayBudget.getDayBudget() > 0) {
dayBudget.subAmount(dayBudget.getDayBudget() % 100);
Expand Down Expand Up @@ -173,11 +172,15 @@ public PushPlusClosing push(BudgetRedistributionRequestDTO.createPushDTO request
//당겨쓰기
@Transactional
public PullMinusClosing pull(BudgetRedistributionRequestDTO.createPullDTO request, Long memberId) {
LocalDate now = LocalDate.now(); // 현재 날짜 가져오기
int localNowYear = now.getYear();
int localNowMonth = now.getMonthValue(); // 현재 월 가져오기
int localNowDay = now.getDayOfMonth(); // 현재 일 가져오기

Member member = memberRepository.findById(memberId)
.orElseThrow(() -> new MemberHandler(NO_MEMBER_EXIST)); //영속화

MonthBudget monthBudget = monthBudgetRepository.findByMemberIdAndYearAndMonth(member.getId(), year, month)
MonthBudget monthBudget = monthBudgetRepository.findByMemberIdAndYearAndMonth(member.getId(), localNowYear, localNowMonth)
.orElseThrow(() -> new MonthBudgetHandler(MONTH_BUDGET_NOT_FOUND));

DayBudget targetBudget = dayBudgetRepository.findByMonthBudgetAndDay(monthBudget, request.getTargetDay())
Expand All @@ -202,7 +205,7 @@ public PullMinusClosing pull(BudgetRedistributionRequestDTO.createPullDTO reques
}
//이번 달 남은 일 수 알아내기
long dayCount = monthBudget.getDayBudgetList().stream()
.filter(dayBudget -> dayBudget.getDay() >= day).count() - 1;
.filter(dayBudget -> dayBudget.getDay() >= localNowDay).count() - 1;

//고르게 당겨쓰기(233원씩 당겨온다고하면 200 * 일수 당겨와서 target에 저장, 233원씩 차감, 67원씩 세이프박스로))
if (request.getRedistributionOption().equals(EVENLY)) {
Expand All @@ -229,7 +232,7 @@ public PullMinusClosing pull(BudgetRedistributionRequestDTO.createPullDTO reques

// 각 DayBudget에서 빼기
monthBudget.getDayBudgetList().stream()
.filter(dayBudget -> dayBudget.getDay() >= day && !dayBudget.getDay().equals(targetDay))
.filter(dayBudget -> dayBudget.getDay() >= localNowDay && !dayBudget.getDay().equals(targetDay))
.forEach(dayBudget -> {
if(dayBudget.getDayBudget() < splitAmount) {
throw new BudgetRedistributionHandler(LACK_OF_MONEY);
Expand All @@ -240,7 +243,7 @@ public PullMinusClosing pull(BudgetRedistributionRequestDTO.createPullDTO reques
//33원 * 일 수 + 딱 떨어지지 않았을 때의 값(totalAmount - splitAmount * dayCount) + target day가 음수에서 양수로 변했을 때의 값 -> 세이프박스
long safeBoxAmount = (splitAmount - distributedAmount) * dayCount + (totalAmount - splitAmount * dayCount) +
monthBudget.getDayBudgetList().stream()
.filter(dayBudget -> dayBudget.getDay() >= day && !dayBudget.getDay().equals(targetDay))
.filter(dayBudget -> dayBudget.getDay() >= localNowDay && !dayBudget.getDay().equals(targetDay))
.mapToLong(dayBudget -> {
if (dayBudget.getDayBudget() > 0) {
return dayBudget.getDayBudget() % 100;
Expand All @@ -251,7 +254,7 @@ public PullMinusClosing pull(BudgetRedistributionRequestDTO.createPullDTO reques

// sourceBudget에서 10 단위 이하 금액 절사 * 양수일 때 *
monthBudget.getDayBudgetList().stream()
.filter(dayBudget -> dayBudget.getDay() >= day && !dayBudget.getDay().equals(targetDay))
.filter(dayBudget -> dayBudget.getDay() >= localNowDay && !dayBudget.getDay().equals(targetDay))
.forEach(dayBudget -> {
if (dayBudget.getDayBudget() > 0) {
dayBudget.subAmount(dayBudget.getDayBudget() % 100);
Expand Down
Loading