From 2c6c5f90374dd9de7a795246112e701e443c52e0 Mon Sep 17 00:00:00 2001 From: cogns <99cogns@naver.com> Date: Tue, 29 Oct 2024 11:53:29 +0900 Subject: [PATCH 1/2] =?UTF-8?q?chore:=20=EB=94=94=ED=85=8C=EC=9D=BC=20?= =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../discount/api/DiscountController.java | 3 +- .../discount/api/DiscountScheduler.java | 11 +++- .../discount/dto/DiscountDetailResponse.java | 28 +++++++++ .../discount/dto/DiscountUpdateRequest.java | 5 +- .../domain/discount/entity/Discount.java | 4 +- .../repository/DiscountRepository.java | 4 ++ .../discount/service/DiscountService.java | 58 ++++++++++++++++--- 7 files changed, 99 insertions(+), 14 deletions(-) create mode 100644 backend/product/src/main/java/org/samtuap/inong/domain/discount/dto/DiscountDetailResponse.java diff --git a/backend/product/src/main/java/org/samtuap/inong/domain/discount/api/DiscountController.java b/backend/product/src/main/java/org/samtuap/inong/domain/discount/api/DiscountController.java index 21e0026a..8b19581b 100644 --- a/backend/product/src/main/java/org/samtuap/inong/domain/discount/api/DiscountController.java +++ b/backend/product/src/main/java/org/samtuap/inong/domain/discount/api/DiscountController.java @@ -2,6 +2,7 @@ import lombok.RequiredArgsConstructor; import org.samtuap.inong.domain.discount.dto.DiscountCreateRequest; +import org.samtuap.inong.domain.discount.dto.DiscountDetailResponse; import org.samtuap.inong.domain.discount.dto.DiscountResponse; import org.samtuap.inong.domain.discount.dto.DiscountUpdateRequest; import org.samtuap.inong.domain.discount.service.DiscountService; @@ -54,7 +55,7 @@ public ResponseEntity> getDiscountList( // 할인 디테일 조회 @GetMapping("/{discountId}/detail") - public ResponseEntity getDiscountDetail(@PathVariable("discountId") Long discountId) { + public ResponseEntity getDiscountDetail(@PathVariable("discountId") Long discountId) { return new ResponseEntity<>(discountService.getDiscountDetail(discountId), HttpStatus.OK); } } diff --git a/backend/product/src/main/java/org/samtuap/inong/domain/discount/api/DiscountScheduler.java b/backend/product/src/main/java/org/samtuap/inong/domain/discount/api/DiscountScheduler.java index 766a7c30..f1461f22 100644 --- a/backend/product/src/main/java/org/samtuap/inong/domain/discount/api/DiscountScheduler.java +++ b/backend/product/src/main/java/org/samtuap/inong/domain/discount/api/DiscountScheduler.java @@ -23,15 +23,22 @@ public class DiscountScheduler { private final CacheManager cacheManager; // 자정마다 실행되도록 스케줄러 설정 (매일 0시 0분 0초에 실행) - @Scheduled(cron = "0 0 0 * * ?") + @Scheduled(cron = "0 0 0 * * *", zone = "Asia/Seoul") +// @Scheduled(cron = "0 */1 * * * ?") // 1분마다 @Transactional public void applyDiscounts() { LocalDate now = LocalDate.now(); // 현재 날짜와 startAt이 "같은" 할인 목록을 가져와 활성화 처리 List discountsToActivate = discountRepository.findAllByStartAt(now); + log.info(">>>현재 날짜 : {}", now); + log.info(">>>>>반환값 : {} ", discountsToActivate); for (Discount discount : discountsToActivate) { + log.info(">>>>discount: {}", discount); + + log.info(">>>>>active 변경전1: {}", discount.isDiscountActive()); DiscountUtil.activateDiscount(discount); // 할인 활성화 (discountActive 필드를 true로 변경) + log.info(">>>>>active 변경후1: {}", discount.isDiscountActive()); discountRepository.save(discount); // 변경된 할인 정보 저장 } @@ -44,4 +51,4 @@ public void applyDiscounts() { Objects.requireNonNull(cacheManager.getCache("PackageDetail")).clear(); } -} +} \ No newline at end of file diff --git a/backend/product/src/main/java/org/samtuap/inong/domain/discount/dto/DiscountDetailResponse.java b/backend/product/src/main/java/org/samtuap/inong/domain/discount/dto/DiscountDetailResponse.java new file mode 100644 index 00000000..0cfa25d6 --- /dev/null +++ b/backend/product/src/main/java/org/samtuap/inong/domain/discount/dto/DiscountDetailResponse.java @@ -0,0 +1,28 @@ +package org.samtuap.inong.domain.discount.dto; + +import lombok.Builder; +import org.samtuap.inong.domain.discount.entity.Discount; + +import java.time.LocalDate; +import java.util.List; + +@Builder +public record DiscountDetailResponse( + Long id, + Integer discount, + LocalDate startAt, + LocalDate endAt, + boolean discountActive, + ListproductIdList +) { + public static DiscountDetailResponse fromEntity(Discount discount, List productIdList) { + return DiscountDetailResponse.builder() + .id(discount.getId()) + .discount(discount.getDiscount()) + .startAt(discount.getStartAt()) + .endAt(discount.getEndAt()) + .discountActive(discount.isDiscountActive()) + .productIdList(productIdList) + .build(); + } +} diff --git a/backend/product/src/main/java/org/samtuap/inong/domain/discount/dto/DiscountUpdateRequest.java b/backend/product/src/main/java/org/samtuap/inong/domain/discount/dto/DiscountUpdateRequest.java index fec2ab66..d0984708 100644 --- a/backend/product/src/main/java/org/samtuap/inong/domain/discount/dto/DiscountUpdateRequest.java +++ b/backend/product/src/main/java/org/samtuap/inong/domain/discount/dto/DiscountUpdateRequest.java @@ -4,15 +4,16 @@ import org.samtuap.inong.domain.discount.entity.Discount; import java.time.LocalDate; +import java.util.List; @Builder public record DiscountUpdateRequest( Integer discount, LocalDate startAt, LocalDate endAt, - boolean discountActive + List productIdList ) { public void updateEntity(Discount discount) { - discount.updateDiscount(this.discount, this.startAt, this.endAt, this.discountActive); + discount.updateDiscount(this.discount, this.startAt, this.endAt); } } diff --git a/backend/product/src/main/java/org/samtuap/inong/domain/discount/entity/Discount.java b/backend/product/src/main/java/org/samtuap/inong/domain/discount/entity/Discount.java index 2bd9f9f8..9da751e1 100644 --- a/backend/product/src/main/java/org/samtuap/inong/domain/discount/entity/Discount.java +++ b/backend/product/src/main/java/org/samtuap/inong/domain/discount/entity/Discount.java @@ -6,6 +6,7 @@ import org.hibernate.annotations.SQLRestriction; import org.samtuap.inong.domain.common.BaseEntity; import java.time.LocalDate; +import java.util.List; @Getter @Entity @@ -27,11 +28,10 @@ public class Discount extends BaseEntity { private boolean discountActive; // 할인 활성화 상태 - public void updateDiscount(Integer discount, LocalDate startAt, LocalDate endAt, boolean discountActive) { + public void updateDiscount(Integer discount, LocalDate startAt, LocalDate endAt) { this.discount = discount; this.startAt = startAt; this.endAt = endAt; - this.discountActive = discountActive; } public void updateDiscountActive(boolean discountActive) { diff --git a/backend/product/src/main/java/org/samtuap/inong/domain/discount/repository/DiscountRepository.java b/backend/product/src/main/java/org/samtuap/inong/domain/discount/repository/DiscountRepository.java index 819fb77e..aa2aa874 100644 --- a/backend/product/src/main/java/org/samtuap/inong/domain/discount/repository/DiscountRepository.java +++ b/backend/product/src/main/java/org/samtuap/inong/domain/discount/repository/DiscountRepository.java @@ -14,6 +14,10 @@ public interface DiscountRepository extends JpaRepository { List findAllByEndAtBefore(LocalDate now); + List findAllByStartAtLessThanEqualAndDiscountActiveFalse(LocalDate now); + + List findAllByEndAtLessThanEqualAndDiscountActiveTrue(LocalDate now); + List findAll(); default Discount findByIdThrow(Long id) { diff --git a/backend/product/src/main/java/org/samtuap/inong/domain/discount/service/DiscountService.java b/backend/product/src/main/java/org/samtuap/inong/domain/discount/service/DiscountService.java index c528e00f..292fd054 100644 --- a/backend/product/src/main/java/org/samtuap/inong/domain/discount/service/DiscountService.java +++ b/backend/product/src/main/java/org/samtuap/inong/domain/discount/service/DiscountService.java @@ -4,6 +4,7 @@ import lombok.extern.slf4j.Slf4j; import org.samtuap.inong.common.exception.BaseCustomException; import org.samtuap.inong.domain.discount.dto.DiscountCreateRequest; +import org.samtuap.inong.domain.discount.dto.DiscountDetailResponse; import org.samtuap.inong.domain.discount.dto.DiscountResponse; import org.samtuap.inong.domain.discount.dto.DiscountUpdateRequest; import org.samtuap.inong.domain.discount.entity.Discount; @@ -18,6 +19,8 @@ import java.time.LocalDate; import java.util.List; +import java.util.stream.Collectors; + import static org.samtuap.inong.common.exceptionType.ProductExceptionType.*; @Slf4j @@ -69,13 +72,47 @@ public void updateDiscount(Long discountId, DiscountUpdateRequest request) { log.info(">>>>날짜 : " + existingDiscount.getStartAt() + " >>>> 오늘보다 이후 => 비활성화"); existingDiscount.updateDiscountActive(false); } - // 변경된 할인이 적용된 상품의 캐시 삭제 처리 - List discountProducts = packageProductRepository.findAllByDiscountId(discountId); - if(!discountProducts.isEmpty()){ - for (PackageProduct product : discountProducts) { - cacheManager.getCache("PackageDetail").evict(product.getId()); + + // 기존에 할인에 적용된 상품들의 ID 리스트 + List existingProducts = packageProductRepository.findAllByDiscountId(discountId); + List existingProductIds = existingProducts.stream() + .map(PackageProduct::getId) + .collect(Collectors.toList()); + + // 요청으로 받은 새로운 상품 ID 리스트 + List newProductIds = request.productIdList(); + + // 할인에서 제거할 상품들 (기존에 있었지만 새 리스트에는 없는 상품들) + List productsToRemoveDiscount = existingProductIds.stream() + .filter(id -> !newProductIds.contains(id)) + .collect(Collectors.toList()); + for (Long productId : productsToRemoveDiscount) { + PackageProduct product = packageProductRepository.findByIdOrThrow(productId); + product.updateDiscountId(null); + cacheManager.getCache("PackageDetail").evict(productId); + } + + // 할인에 새로 추가할 상품들 (새 리스트에는 있지만 기존에 없었던 상품들) + List productsToAddDiscount = newProductIds.stream() + .filter(id -> !existingProductIds.contains(id)) + .collect(Collectors.toList()); + for (Long productId : productsToAddDiscount) { + PackageProduct product = packageProductRepository.findByIdOrThrow(productId); + if (product.getDiscountId() == null) { + product.updateDiscountId(discountId); + } else { + throw new BaseCustomException(DISCOUNT_ALREADY_EXISTS); } + cacheManager.getCache("PackageDetail").evict(productId); } + +// // 변경된 할인이 적용된 상품의 캐시 삭제 처리 +// List discountProducts = packageProductRepository.findAllByDiscountId(discountId); +// if(!discountProducts.isEmpty()){ +// for (PackageProduct product : discountProducts) { +// cacheManager.getCache("PackageDetail").evict(product.getId()); +// } +// } discountRepository.save(existingDiscount); } @@ -101,9 +138,16 @@ public Page getDiscountList(Pageable pageable) { } // 할인 디테일 조회 - public DiscountResponse getDiscountDetail(Long discountId) { + public DiscountDetailResponse getDiscountDetail(Long discountId) { Discount discount = discountRepository.findById(discountId) .orElseThrow(() -> new BaseCustomException(DISCOUNT_NOT_FOUND)); - return DiscountResponse.fromEntity(discount); + + // 해당 할인에 적용된 상품들의 ID 리스트 조회 + List productList = packageProductRepository.findAllByDiscountId(discountId); + List productIdList = productList.stream() + .map(PackageProduct::getId) + .collect(Collectors.toList()); + + return DiscountDetailResponse.fromEntity(discount, productIdList); } } From 67bf174469a4d04eec3805b8c6d0169e9204e97e Mon Sep 17 00:00:00 2001 From: cogns <99cogns@naver.com> Date: Tue, 29 Oct 2024 12:07:32 +0900 Subject: [PATCH 2/2] =?UTF-8?q?fix:=20=EC=97=94=ED=8B=B0=ED=8B=B0=20?= =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../inong/domain/discount/dto/DiscountUpdateRequest.java | 3 ++- .../org/samtuap/inong/domain/discount/entity/Discount.java | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/backend/product/src/main/java/org/samtuap/inong/domain/discount/dto/DiscountUpdateRequest.java b/backend/product/src/main/java/org/samtuap/inong/domain/discount/dto/DiscountUpdateRequest.java index d0984708..003681ea 100644 --- a/backend/product/src/main/java/org/samtuap/inong/domain/discount/dto/DiscountUpdateRequest.java +++ b/backend/product/src/main/java/org/samtuap/inong/domain/discount/dto/DiscountUpdateRequest.java @@ -11,9 +11,10 @@ public record DiscountUpdateRequest( Integer discount, LocalDate startAt, LocalDate endAt, + boolean discountActive, List productIdList ) { public void updateEntity(Discount discount) { - discount.updateDiscount(this.discount, this.startAt, this.endAt); + discount.updateDiscount(this.discount, this.startAt, this.endAt, this.discountActive); } } diff --git a/backend/product/src/main/java/org/samtuap/inong/domain/discount/entity/Discount.java b/backend/product/src/main/java/org/samtuap/inong/domain/discount/entity/Discount.java index 9da751e1..6db086e4 100644 --- a/backend/product/src/main/java/org/samtuap/inong/domain/discount/entity/Discount.java +++ b/backend/product/src/main/java/org/samtuap/inong/domain/discount/entity/Discount.java @@ -28,10 +28,11 @@ public class Discount extends BaseEntity { private boolean discountActive; // 할인 활성화 상태 - public void updateDiscount(Integer discount, LocalDate startAt, LocalDate endAt) { + public void updateDiscount(Integer discount, LocalDate startAt, LocalDate endAt, boolean discountActive) { this.discount = discount; this.startAt = startAt; this.endAt = endAt; + this.discountActive = discountActive; } public void updateDiscountActive(boolean discountActive) {