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

#217 fix: 통계 쿼리 호환성 버그 수정 #218

Merged
merged 7 commits into from
Jan 29, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ public void run(ApplicationArguments args) throws Exception {
if (!couponStatisticsRepository.existsById(1L)) {
makeCouponStatistics();
}
if (!revenueStatisticsRepository.existsById(1L)) {
if (!revenueStatisticsRepository.existsById(1L) ||
!revenueTotalRepository.existsById(1L)) {
createRevenueStatistics();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,25 @@
public interface CouponStatisticsRepository extends JpaRepository<CouponStatistics, Long> {

String couponQuery = """
select t.id, t.total, u.used, t.total - u.used as stock
from (select ac.id as id, sum(ci.quantity) as total
from coupon_issuance ci
left join room rm on ci.room_id = rm.id
left join coupon cp on cp.id = ci.coupon_id
left join accommodation ac on rm.accommodation_id = ac.id
where rm.deleted_at is null
and ac.deleted_at is null
and cp.deleted_at is null
select t.id,
coalesce(t.total, 0) as total,
coalesce(u.used, 0) as used,
coalesce(t.total - u.used, t.total, 0) as stock
from (select ac.id, coalesce(sum(ci.quantity), 0) as total
from accommodation ac
join room rm on ac.id = rm.accommodation_id
left join coupon_issuance ci on rm.id = ci.room_id
left join coupon cp on ci.coupon_id = cp.id
where cp.deleted_at is null
group by ac.id) t
inner join
(select ac.id as id, count(*) as used
from room rm
left join reservation_room rr on rr.room_id = rm.id
left join
(select ac.id, coalesce(count(*), 0) as used
from accommodation ac
join room rm on ac.id = rm.accommodation_id
left join reservation_room rr on rm.id = rr.room_id
left join reservation rv on rr.id = rv.reservation_room_id
left join coupon_redeem cr on rv.id = cr.reservation_id
left join accommodation ac on rm.accommodation_id = ac.id
where rv.is_coupon_used = true
and rm.deleted_at is null
and ac.deleted_at is null
and not exists(select 1
from coupon cp
inner join coupon_redeem cr on cr.coupon_id = cp.id
where cp.deleted_at is not null)
and rv.status != 'CANCELLED'
group by ac.id) u
on t.id = u.id
""";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,23 @@
public interface RevenueStatisticsRepository extends JpaRepository<RevenueStatistics, Long> {

String revenueSQL = """
select ac.id as id,
select ac.id,
py.created_at as revenueDate,
coalesce(sum(case when py.discount_amount != 0 then py.total_amount else 0 end),
0) as couponRevenue,
coalesce(sum(case when py.discount_amount = 0 then py.total_amount else 0 end),
0) as regularRevenue
0) as regularRevenue,
coalesce(sum(case when py.discount_amount != 0 then py.total_amount else 0 end),
0) as couponRevenue
from reservation_room rr
left join reservation rv on rr.id = rv.reservation_room_id
left join payment py on rv.payment_id = py.id
left join room rm on rr.room_id = rm.id
left join accommodation ac on rm.accommodation_id = ac.id
left join coupon cp on cp.room_id = rm.id
where py.created_at between :startDate and :endDate
and rv.status = 'SERVICED'
and rm.deleted_at is null
and ac.deleted_at is null
and rv.status != 'CANCELLED'
and cp.deleted_at is null
group by ac.id, py.created_at
order by ac.id, py.created_at
group by ac.id, date(py.created_at)
order by ac.id, date(py.created_at);
""";
@Query(value = revenueSQL, nativeQuery = true)
List<RevenueStatisticsInterface> createRevenueStatistics(
Expand Down
Loading