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 98918f97..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 @@ -51,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..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 @@ -4,13 +4,15 @@ 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 + boolean discountActive, + List productIdList ) { public void updateEntity(Discount discount) { 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 2bd9f9f8..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 @@ -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 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); } }