Skip to content

Commit

Permalink
♻️ refactor : 정산결과 반환값 변경 #494
Browse files Browse the repository at this point in the history
  • Loading branch information
hobeen-kim committed Oct 2, 2023
1 parent 779b804 commit c3ec544
Show file tree
Hide file tree
Showing 6 changed files with 177 additions and 193 deletions.
13 changes: 0 additions & 13 deletions Server/src/docs/asciidoc/snippets/adjustment/adjustment.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,6 @@ include::{snippets}/adjustment/adjustment/http-response.adoc[]
==== Response Fields
include::{snippets}/adjustment/adjustment/response-fields.adoc[]

[[Adjustment-total]]
== 특정 월의 정산 총 금액 및 월별 정산 내역
=== HTTP Request
include::{snippets}/adjustment/calculateamount/http-request.adoc[]
==== Request Headers
include::{snippets}/adjustment/calculateamount/request-headers.adoc[]
==== Request Query Parameters
include::{snippets}/adjustment/calculateamount/request-parameters.adoc[]
=== HTTP Response
include::{snippets}/adjustment/calculateamount/http-response.adoc[]
==== Response Fields
include::{snippets}/adjustment/calculateamount/response-fields.adoc[]

[[Adjustment-totalyear]]
== 특정 연도의 정산 총 금액 및 월별 정산 내역
=== HTTP Request
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@

import com.server.domain.adjustment.controller.dto.request.AccountUpdateApiRequest;
import com.server.domain.adjustment.service.AdjustmentService;
import com.server.domain.adjustment.service.dto.response.AccountResponse;
import com.server.domain.adjustment.service.dto.response.ToTalAdjustmentResponse;
import com.server.domain.adjustment.service.dto.response.VideoAdjustmentResponse;
import com.server.domain.adjustment.service.dto.response.*;
import com.server.domain.order.controller.dto.request.AdjustmentSort;
import com.server.domain.adjustment.service.dto.response.AdjustmentResponse;
import com.server.global.annotation.LoginId;
import com.server.global.exception.businessexception.orderexception.AdjustmentDateException;
import com.server.global.reponse.ApiPageResponse;
Expand Down Expand Up @@ -65,16 +62,13 @@ public ResponseEntity<ApiSingleResponse<List<VideoAdjustmentResponse>>> calculat
}

@GetMapping("/total-adjustment")
public ResponseEntity<ApiSingleResponse<ToTalAdjustmentResponse>> calculateAmount(
@RequestParam(required = false) @Min(value = 1) @Max(value = 12) Integer month,
public ResponseEntity<ApiSingleResponse<List<MonthAdjustmentResponse>>> calculateAmount(
@RequestParam(required = false) @Min(value = 2020) Integer year,
@LoginId Long loginMemberId) {

checkValidDate(month, year);

ToTalAdjustmentResponse total = adjustmentService.totalAdjustment(loginMemberId, month, year);
List<MonthAdjustmentResponse> total = adjustmentService.totalAdjustment(loginMemberId, year);

return ResponseEntity.ok(ApiSingleResponse.ok(total, getAdjustmentMessage(month, year) + " 정산 내역"));
return ResponseEntity.ok(ApiSingleResponse.ok(total, getAdjustmentMessage(null, year) + " 정산 내역"));
}

@GetMapping("/account")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,100 +46,18 @@ public Page<AdjustmentResponse> adjustment(Long loginMemberId, int page, int siz
return datas.map(AdjustmentResponse::of);
}

public Integer calculateAmount(Long loginMemberId, Integer month, Integer year) {

return adjustmentRepository.calculateAmount(loginMemberId, month, year);
}

public ToTalAdjustmentResponse totalAdjustment(Long loginMemberId, Integer month, Integer year) {
public List<MonthAdjustmentResponse> totalAdjustment(Long loginMemberId, Integer year) {

List<Adjustment> monthlyData = adjustmentRepository.findMonthlyData(loginMemberId, year);

List<MonthAdjustmentResponse> monthAdjustmentResponses = monthlyData.stream()
.map(MonthAdjustmentResponse::of)
.collect(Collectors.toList());

//최근 2달 데이터 넣기
addCurrentMonthData(monthAdjustmentResponses);

//필요한 데이터 세팅
return getTotalAdjustment(monthAdjustmentResponses, monthlyData, month, year);
}

private void addCurrentMonthData(List<MonthAdjustmentResponse> monthAdjustmentResponses) {

LocalDate now = LocalDate.now();
int month = now.getMonthValue();
int year = now.getYear();

//이번달 데이터 세팅
int amount = adjustmentRepository.calculateAmount(1L, month, year);
monthAdjustmentResponses.add(MonthAdjustmentResponse.of(year, month, amount));

//지난달 데이터가 있는지 확인
if (month == 1) {
month = 12;
year -= 1;
} else {
month -= 1;
}

boolean hasLastMonth = false;

for(MonthAdjustmentResponse monthAdjustmentResponse : monthAdjustmentResponses) {
if (monthAdjustmentResponse.isSameMonthAndYear(year, month)) {
hasLastMonth = true;
break;
}
}
//없는 데이터 넣기
addAdditionalMonthData(monthAdjustmentResponses, year);

//지난달 데이터가 없다면 세팅
if (!hasLastMonth) {
amount = adjustmentRepository.calculateAmount(1L, month, year);
monthAdjustmentResponses.add(MonthAdjustmentResponse.of(year, month, amount));
}
}

private ToTalAdjustmentResponse getTotalAdjustment(List<MonthAdjustmentResponse> monthAdjustmentResponses,
List<Adjustment> monthlyData,
Integer month,
Integer year) {

int amount = 0;
AdjustmentStatus status = AdjustmentStatus.NO_ADJUSTMENT;
String reason = null;

if(year == null) {

amount = adjustmentRepository.calculateAmount(1L, null, null);
status = AdjustmentStatus.TOTAL;

return ToTalAdjustmentResponse.of(amount, status, reason, monthAdjustmentResponses);
}

if(month == null) {

amount = adjustmentRepository.calculateAmount(1L, null, year);
status = AdjustmentStatus.TOTAL;

return ToTalAdjustmentResponse.of(amount, status, reason, monthAdjustmentResponses);
}

for(MonthAdjustmentResponse monthAdjustmentResponse : monthAdjustmentResponses) {
if (monthAdjustmentResponse.isSameMonthAndYear(month, year)) {
amount = monthAdjustmentResponse.getAmount();
status = AdjustmentStatus.NOT_ADJUSTED;
}
}

for(Adjustment adjustment : monthlyData) {
if (adjustment.isSameMonthAndYear(month, year)) {
status = adjustment.getAdjustmentStatus();
reason = adjustment.getReason() == null ? status.getDescription() : adjustment.getReason();
}
}

return ToTalAdjustmentResponse.of(amount, status, reason, monthAdjustmentResponses);
return monthAdjustmentResponses;
}

public AccountResponse getAccount(Long loginMemberId) {
Expand Down Expand Up @@ -176,6 +94,138 @@ private Account getAccountOrNull(Long loginMemberId) {
return accountRepository.findByMemberId(loginMemberId).orElse(null);
}

private void addAdditionalMonthData(List<MonthAdjustmentResponse> monthAdjustmentResponses, Integer year) {

LocalDate now = LocalDate.now();
int currentMonth = now.getMonthValue();
int currentYear = now.getYear();

if(year == null) {

//이번달 데이터 세팅
int amount = adjustmentRepository.calculateAmount(1L, currentMonth, currentYear);
monthAdjustmentResponses.add(MonthAdjustmentResponse.of(currentYear, currentMonth, amount, AdjustmentStatus.NOT_ADJUSTED));

//지난달 데이터가 있는지 확인
if (currentMonth == 1) {
currentMonth = 12;
currentYear -= 1;
} else {
currentMonth -= 1;
}

boolean hasLastMonth = false;

for(MonthAdjustmentResponse monthAdjustmentResponse : monthAdjustmentResponses) {
if (monthAdjustmentResponse.isSameMonthAndYear(currentYear, currentMonth)) {
hasLastMonth = true;
break;
}
}

//지난달 데이터가 없다면 세팅
if (!hasLastMonth) {
amount = adjustmentRepository.calculateAmount(1L, currentMonth, currentYear);
monthAdjustmentResponses.add(MonthAdjustmentResponse.of(currentYear, currentMonth, amount, AdjustmentStatus.NOT_ADJUSTED));
}

//2023년부터 빈 데이터를 확인해서 0원으로 넣어주기
for(int i = 2023; i <= currentYear; i++) {
for(int j = 1; j <= 12; j++) {
boolean hasData = false;
for(MonthAdjustmentResponse monthAdjustmentResponse : monthAdjustmentResponses) {
if (monthAdjustmentResponse.isSameMonthAndYear(i, j)) {
hasData = true;
break;
}
}
if(!hasData) {
monthAdjustmentResponses.add(MonthAdjustmentResponse.of(i, j, 0, AdjustmentStatus.NO_ADJUSTMENT));
}
}
}
}else if(year.equals(currentYear)) {

//이번달 데이터 세팅
int amount = adjustmentRepository.calculateAmount(1L, currentMonth, currentYear);
monthAdjustmentResponses.add(MonthAdjustmentResponse.of(currentYear, currentMonth, amount, AdjustmentStatus.NOT_ADJUSTED));

boolean hasLastMonth = false;

//지난달 데이터가 올해인지 확인
if (currentMonth == 1) {
hasLastMonth = true;
} else {
currentMonth -= 1;
}

if(!hasLastMonth) {

for(MonthAdjustmentResponse monthAdjustmentResponse : monthAdjustmentResponses) {
if (monthAdjustmentResponse.isSameMonthAndYear(currentYear, currentMonth)) {
hasLastMonth = true;
break;
}
}
}

//지난달 데이터가 없다면 세팅
if (!hasLastMonth) {
amount = adjustmentRepository.calculateAmount(1L, currentMonth, currentYear);
monthAdjustmentResponses.add(MonthAdjustmentResponse.of(currentYear, currentMonth, amount, AdjustmentStatus.NOT_ADJUSTED));
}

//빈 데이터를 확인해서 0원으로 넣어주기
for (int j = 1; j <= 12; j++) {
boolean hasData = false;
for (MonthAdjustmentResponse monthAdjustmentResponse : monthAdjustmentResponses) {
if (monthAdjustmentResponse.isSameMonthAndYear(year, j)) {
hasData = true;
break;
}
}
if (!hasData) {
monthAdjustmentResponses.add(MonthAdjustmentResponse.of(year, j, 0, AdjustmentStatus.NO_ADJUSTMENT));
}
}
}else {
//현재 월이 1월이라면 지난달 12월 데이터 넣어주기
if(currentMonth == 1 && currentYear == year + 1) {

boolean hasLastMonth = false;

for(MonthAdjustmentResponse monthAdjustmentResponse : monthAdjustmentResponses) {
if (monthAdjustmentResponse.isSameMonthAndYear(year, 12)) {
hasLastMonth = true;
break;
}
}

//지난달 데이터가 없다면 세팅
if (!hasLastMonth) {
int amount = adjustmentRepository.calculateAmount(1L, 12, year);
monthAdjustmentResponses.add(MonthAdjustmentResponse.of(year, 12, amount, AdjustmentStatus.NOT_ADJUSTED));
}
}

//빈 데이터를 확인해서 0원으로 넣어주기
for (int j = 1; j <= 12; j++) {
boolean hasData = false;
for (MonthAdjustmentResponse monthAdjustmentResponse : monthAdjustmentResponses) {
if (monthAdjustmentResponse.isSameMonthAndYear(year, j)) {
hasData = true;
break;
}
}
if (!hasData) {
monthAdjustmentResponses.add(MonthAdjustmentResponse.of(year, j, 0, AdjustmentStatus.NO_ADJUSTMENT));
}
}
}


}

private Member verifiedMember(Long memberId) {
return memberRepository.findById(memberId)
.orElseThrow(MemberNotFoundException::new);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.server.domain.adjustment.service.dto.response;

import com.server.domain.adjustment.domain.Adjustment;
import com.server.domain.adjustment.domain.AdjustmentStatus;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
Expand All @@ -13,20 +14,26 @@ public class MonthAdjustmentResponse {
private Integer year;
private Integer month;
private Integer amount;
private String reason;
private AdjustmentStatus adjustmentStatus;

public static MonthAdjustmentResponse of(Adjustment adjustment) {
return MonthAdjustmentResponse.builder()
.year(adjustment.getAdjustmentYear())
.month(adjustment.getAdjustmentMonth())
.amount(adjustment.getAmount())
.reason(adjustment.getReason())
.adjustmentStatus(adjustment.getAdjustmentStatus())
.build();
}

public static MonthAdjustmentResponse of(int year, int month, int amount) {
public static MonthAdjustmentResponse of(int year, int month, int amount, AdjustmentStatus adjustmentStatus) {
return MonthAdjustmentResponse.builder()
.year(year)
.month(month)
.amount(amount)
.reason(adjustmentStatus.getDescription())
.adjustmentStatus(adjustmentStatus)
.build();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.server.global.batch.adjustment;

public class AdjustmentJobConfig {
}
Loading

0 comments on commit c3ec544

Please sign in to comment.