diff --git a/src/main/java/com/backoffice/upjuyanolja/domain/coupon/config/MockStatisticsInit.java b/src/main/java/com/backoffice/upjuyanolja/domain/coupon/config/MockStatisticsInit.java index f2901b7b..54a6bd9b 100644 --- a/src/main/java/com/backoffice/upjuyanolja/domain/coupon/config/MockStatisticsInit.java +++ b/src/main/java/com/backoffice/upjuyanolja/domain/coupon/config/MockStatisticsInit.java @@ -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(); } } diff --git a/src/main/java/com/backoffice/upjuyanolja/domain/coupon/repository/CouponStatisticsRepository.java b/src/main/java/com/backoffice/upjuyanolja/domain/coupon/repository/CouponStatisticsRepository.java index 6af464cd..294ec16c 100644 --- a/src/main/java/com/backoffice/upjuyanolja/domain/coupon/repository/CouponStatisticsRepository.java +++ b/src/main/java/com/backoffice/upjuyanolja/domain/coupon/repository/CouponStatisticsRepository.java @@ -10,30 +10,25 @@ public interface CouponStatisticsRepository extends JpaRepository { 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 """; diff --git a/src/main/java/com/backoffice/upjuyanolja/domain/coupon/repository/RevenueStatisticsRepository.java b/src/main/java/com/backoffice/upjuyanolja/domain/coupon/repository/RevenueStatisticsRepository.java index 36e08273..217a4421 100644 --- a/src/main/java/com/backoffice/upjuyanolja/domain/coupon/repository/RevenueStatisticsRepository.java +++ b/src/main/java/com/backoffice/upjuyanolja/domain/coupon/repository/RevenueStatisticsRepository.java @@ -11,12 +11,12 @@ public interface RevenueStatisticsRepository extends JpaRepository { 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 @@ -24,12 +24,10 @@ public interface RevenueStatisticsRepository extends JpaRepository createRevenueStatistics(