Skip to content

Commit

Permalink
refactor : MySQL 문법 SNAKE_CASE로 변경 및 원본객체를 정렬할때는 동시성이슈를 피하기 위해 stream적용
Browse files Browse the repository at this point in the history
  • Loading branch information
codesejin committed Apr 28, 2024
1 parent c931928 commit 53b6aa4
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ public final class MonthlyOrderStatistics {
private final BigDecimal totalCouponPrice;

public MonthlyOrderStatistics(MonthlyOrderStatisticsVo vo) {
this.yearMonth = (vo != null) ? vo.yearMonth() : null;
this.totalOrderCnt = (vo != null) ? vo.totalOrderCnt() : 0;
this.totalPaymentPrice = (vo != null) ? vo.totalPaymentPrice() : BigDecimal.ZERO;
this.totalCouponUseCnt = (vo != null) ? vo.totalCouponUseCnt() : 0;
this.totalCouponPrice = (vo != null) ? vo.totalCouponPrice() : BigDecimal.ZERO;
this.yearMonth = vo.yearMonth();
this.totalOrderCnt = vo.totalOrderCnt();
this.totalPaymentPrice = vo.totalPaymentPrice();
this.totalCouponUseCnt = vo.totalCouponUseCnt();
this.totalCouponPrice = vo.totalCouponPrice();
}
}
13 changes: 8 additions & 5 deletions src/main/java/com/flab/offcoupon/service/StatisticsService.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,10 @@ public ResponseDTO<List<MonthlyOrderStatistics>> getMonthlyOrderStatistics(Stati
List<MonthlyOrderStatistics> monthlyStatisticsList = getMonthlyStatistics(startedAt, endedAt);

// 월별로 정렬
monthlyStatisticsList.sort(Comparator.comparing(MonthlyOrderStatistics::getYearMonth));

return ResponseDTO.getSuccessResult(monthlyStatisticsList);
return ResponseDTO.getSuccessResult(monthlyStatisticsList
.stream()
.sorted(Comparator.comparing(MonthlyOrderStatistics::getYearMonth))
.toList());
}

/**
Expand Down Expand Up @@ -98,8 +99,9 @@ private List<MonthlyOrderStatistics> getMonthlyStatistics(LocalDate startedAt, L

/**
* 현재 월과 대상 날짜가 같은지 확인하는 메서드입니다.
*
* @param currentYearMonth 현재 월
* @param target 대상 날짜
* @param target 대상 날짜
* @return 현재 월과 대상 날짜가 같은지 여부
*/
private boolean isSameYearMonth(YearMonth currentYearMonth, LocalDate target) {
Expand All @@ -109,8 +111,9 @@ private boolean isSameYearMonth(YearMonth currentYearMonth, LocalDate target) {
/**
* 시작일부터 종료일까지의 월 차이를 계산하는 메서드입니다.
* 병렬 스트림에서 사용하기 위해 long 타입으로 반환합니다.
*
* @param startedAt 시작일
* @param endedAt 종료일
* @param endedAt 종료일
* @return 시작일부터 종료일까지의 월 차이
*/
private long countMonthDifference(LocalDate startedAt, LocalDate endedAt) {
Expand Down
14 changes: 7 additions & 7 deletions src/main/resources/mapper/StatisticsMapper.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@

<mapper namespace="com.flab.offcoupon.repository.mysql.StatisticsRepository">
<select id="getMonthlyOrderStatistics" parameterType="MonthlyStatisticsParameterVo" resultType="MonthlyOrderStatisticsVo">
select DATE_FORMAT(od.created_at,'%Y-%m') as YearMonth,
count(od.id) as totalOrderCnt,
SUM(od.total_payment_price) as totalPaymentPrice,
count(oc.id) as totalCouponUseCnt,
SUM(od.total_discount_price) as total_discount_price
select DATE_FORMAT(od.created_at,'%Y-%m') as `year_month`,
count(od.id) as total_order_cnt,
SUM(od.total_payment_price) as 'total_payment_price',
count(oc.id) as total_coupon_cnt,
SUM(od.total_discount_price) as 'total_discount_price'
from order_detail od
left join order_coupon oc on od.id = oc.order_id
where od.created_at BETWEEN #{startedAt} and #{endedAt}
group by YearMonth
order by YearMonth;
group by `year_month`
order by `year_month`;
</select>
</mapper>

0 comments on commit 53b6aa4

Please sign in to comment.