-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'develop' into feature/usePoint
- Loading branch information
Showing
14 changed files
with
408 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
...om/backoffice/upjuyanolja/domain/accommodation/dto/response/CouponStatisticsResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.backoffice.upjuyanolja.domain.accommodation.dto.response; | ||
|
||
import com.backoffice.upjuyanolja.domain.coupon.entity.CouponStatistics; | ||
import lombok.Builder; | ||
|
||
@Builder | ||
public record CouponStatisticsResponse( | ||
Long accommodationId, | ||
long total, | ||
long used, | ||
long stock | ||
) { | ||
public static CouponStatisticsResponse from(CouponStatistics statistics) { | ||
return CouponStatisticsResponse.builder() | ||
.accommodationId(statistics.getId()) | ||
.total(statistics.getTotal()) | ||
.used(statistics.getUsed()) | ||
.stock(statistics.getStock()) | ||
.build(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
src/main/java/com/backoffice/upjuyanolja/domain/coupon/dto/CouponStatisticsDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.backoffice.upjuyanolja.domain.coupon.dto; | ||
|
||
import com.backoffice.upjuyanolja.domain.accommodation.entity.Accommodation; | ||
import com.backoffice.upjuyanolja.domain.coupon.entity.CouponStatistics; | ||
import lombok.Builder; | ||
|
||
@Builder | ||
public record CouponStatisticsDto( | ||
Accommodation accommodation, | ||
long total, | ||
long used, | ||
long stock | ||
) { | ||
|
||
public static CouponStatistics toEntity( | ||
Accommodation accommodation, | ||
long total, | ||
long used, | ||
long stock | ||
) { | ||
return CouponStatistics.builder() | ||
.accommodation(accommodation) | ||
.total(total) | ||
.used(used) | ||
.stock(stock) | ||
.build(); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
src/main/java/com/backoffice/upjuyanolja/domain/coupon/dto/CouponStatisticsInterface.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.backoffice.upjuyanolja.domain.coupon.dto; | ||
|
||
public interface CouponStatisticsInterface { | ||
Long getId(); | ||
Long getTotal(); | ||
Long getUsed(); | ||
Long getStock(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
src/main/java/com/backoffice/upjuyanolja/domain/coupon/entity/CouponStatistics.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package com.backoffice.upjuyanolja.domain.coupon.entity; | ||
|
||
import com.backoffice.upjuyanolja.domain.accommodation.entity.Accommodation; | ||
import com.backoffice.upjuyanolja.global.common.entity.BaseTime; | ||
import jakarta.persistence.Column; | ||
import jakarta.persistence.ConstraintMode; | ||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.FetchType; | ||
import jakarta.persistence.ForeignKey; | ||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.GenerationType; | ||
import jakarta.persistence.Id; | ||
import jakarta.persistence.JoinColumn; | ||
import jakarta.persistence.ManyToOne; | ||
import lombok.AccessLevel; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.Setter; | ||
import org.hibernate.annotations.Comment; | ||
|
||
@Getter | ||
@Entity | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
public class CouponStatistics extends BaseTime { | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
@Comment("쿠폰 사용량 통계 식별자") | ||
private Long id; | ||
|
||
@ManyToOne(fetch = FetchType.LAZY) | ||
@JoinColumn( | ||
nullable = false, | ||
name = "accommodation_id", | ||
unique = true, | ||
foreignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT)) | ||
private Accommodation accommodation; | ||
|
||
@Setter | ||
@Column(nullable = false) | ||
@Comment("발행 쿠폰") | ||
private long total; | ||
|
||
@Setter | ||
@Column(nullable = false) | ||
@Comment("사용 완료 쿠폰") | ||
private long used; | ||
|
||
@Setter | ||
@Column(nullable = false) | ||
@Comment("현재 보유 쿠폰") | ||
private long stock; | ||
|
||
@Builder | ||
public CouponStatistics( | ||
Long id, | ||
Accommodation accommodation, | ||
long total, | ||
long used, | ||
long stock | ||
) { | ||
this.id = id; | ||
this.accommodation = accommodation; | ||
this.total = total; | ||
this.used = used; | ||
this.stock = stock; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
.../java/com/backoffice/upjuyanolja/domain/coupon/repository/CouponStatisticsRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.backoffice.upjuyanolja.domain.coupon.repository; | ||
|
||
import com.backoffice.upjuyanolja.domain.coupon.dto.CouponStatisticsInterface; | ||
import com.backoffice.upjuyanolja.domain.coupon.entity.CouponStatistics; | ||
import java.util.List; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.data.jpa.repository.Query; | ||
|
||
public interface CouponStatisticsRepository extends JpaRepository<CouponStatistics, Long> { | ||
|
||
@Query(value = | ||
"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 " | ||
+ " join room rm on ci.ROOM_ID = rm.id " | ||
+ " join ACCOMMODATION ac on rm.accommodation_id = ac.id " | ||
+ " group by ac.id) t " | ||
+ " inner join " | ||
+ " (select ac.id as id, count(*) as used " | ||
+ " from reservation_room rr " | ||
+ " left join reservation rv on rr.id = rv.RESERVATION_ROOM_ID " | ||
+ " left join coupon_redeem cr on rv.id = cr.RESERVATION_ID " | ||
+ " left join room r on rr.ROOM_ID = r.ID " | ||
+ " left join accommodation ac on r.ACCOMMODATION_ID = ac.id " | ||
+ " where rv.IS_COUPON_USED = true " | ||
+ " group by ac.id) u " | ||
+ " on t.id = u.id", nativeQuery = true) | ||
List<CouponStatisticsInterface> createStatistics(); | ||
} |
26 changes: 26 additions & 0 deletions
26
src/main/java/com/backoffice/upjuyanolja/domain/coupon/service/CouponStatisticsService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.backoffice.upjuyanolja.domain.coupon.service; | ||
|
||
import com.backoffice.upjuyanolja.domain.accommodation.dto.response.CouponStatisticsResponse; | ||
import com.backoffice.upjuyanolja.domain.accommodation.exception.AccommodationNotFoundException; | ||
import com.backoffice.upjuyanolja.domain.accommodation.repository.AccommodationRepository; | ||
import com.backoffice.upjuyanolja.domain.coupon.entity.CouponStatistics; | ||
import com.backoffice.upjuyanolja.domain.coupon.repository.CouponStatisticsRepository; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@Slf4j | ||
@Service | ||
@RequiredArgsConstructor | ||
@Transactional | ||
public class CouponStatisticsService { | ||
|
||
private final CouponStatisticsRepository couponStatisticsRepository; | ||
|
||
public CouponStatisticsResponse getCouponStatistics(Long accommodationId) { | ||
CouponStatistics couponStatistics = couponStatisticsRepository.findById(accommodationId) | ||
.orElseThrow(AccommodationNotFoundException::new); | ||
return CouponStatisticsResponse.from(couponStatistics); | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
src/main/java/com/backoffice/upjuyanolja/global/scheduler/CouponStatisticsScheduler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package com.backoffice.upjuyanolja.global.scheduler; | ||
|
||
import com.backoffice.upjuyanolja.domain.accommodation.entity.Accommodation; | ||
import com.backoffice.upjuyanolja.domain.accommodation.exception.AccommodationNotFoundException; | ||
import com.backoffice.upjuyanolja.domain.accommodation.repository.AccommodationRepository; | ||
import com.backoffice.upjuyanolja.domain.coupon.dto.CouponStatisticsDto; | ||
import com.backoffice.upjuyanolja.domain.coupon.dto.CouponStatisticsInterface; | ||
import com.backoffice.upjuyanolja.domain.coupon.entity.CouponStatistics; | ||
import com.backoffice.upjuyanolja.domain.coupon.repository.CouponStatisticsRepository; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.scheduling.annotation.EnableScheduling; | ||
import org.springframework.scheduling.annotation.Scheduled; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@Slf4j | ||
@EnableScheduling | ||
@RequiredArgsConstructor | ||
@Transactional | ||
public class CouponStatisticsScheduler { | ||
|
||
private final CouponStatisticsRepository couponStatisticsRepository; | ||
private final AccommodationRepository accommodationRepository; | ||
|
||
// 매일 새벽 2시에 통계 쿼리 실행 | ||
@Scheduled(cron = "0 0 2 * * *", zone = "Asia/Seoul") | ||
public void makeCouponStatistics() { | ||
List<CouponStatisticsInterface> result = couponStatisticsRepository.createStatistics(); | ||
List<CouponStatistics> statisticsList = new ArrayList<>(); | ||
for (CouponStatisticsInterface statistics : result) { | ||
statisticsList.add(createCouponStatistics(statistics)); | ||
} | ||
couponStatisticsRepository.saveAll(statisticsList); | ||
log.info("쿠폰 통계 생성 성공. 총 {}건.", statisticsList.size()); | ||
} | ||
|
||
private CouponStatistics createCouponStatistics(CouponStatisticsInterface statistics) { | ||
Accommodation accommodation = accommodationRepository.findById(statistics.getId()) | ||
.orElseThrow(AccommodationNotFoundException::new); | ||
return CouponStatisticsDto.toEntity( | ||
accommodation, | ||
statistics.getTotal(), | ||
statistics.getUsed(), | ||
statistics.getStock() | ||
); | ||
} | ||
|
||
} |
Oops, something went wrong.