diff --git a/src/main/java/com/programmers/smrtstore/domain/coupon/application/OrderCouponDiscountCalculator.java b/src/main/java/com/programmers/smrtstore/domain/coupon/application/OrderCouponDiscountCalculator.java index 606e2c9b9..3977d7d96 100644 --- a/src/main/java/com/programmers/smrtstore/domain/coupon/application/OrderCouponDiscountCalculator.java +++ b/src/main/java/com/programmers/smrtstore/domain/coupon/application/OrderCouponDiscountCalculator.java @@ -124,7 +124,7 @@ public static SelectedCouponsWithCouponApplyResult getMaxDiscountCouponApplyResu //TODO: 메서드2. 주어진 select 대로 계산해서 반환 - public static Map> getCouponApplyResult(List orderedProducts, SelectedCouponsRequest selectedCoupons) { + public static Map> getCouponApplyResult(List orderedProducts, MapselectedProductCoupons,Coupon selectedCartCoupon) { Map> discountsByOrderedProductId = new HashMap<>(); @@ -136,8 +136,8 @@ public static Map> getCouponApplyResult(List> getCouponApplyResult(List 상품쿠폰까지 적용된 총합 //TODO: 장바구니 쿠폰을 각 OrderedProduct에 CouponApplyResult를 추가하기 - Coupon selectedCartCoupons = selectedCoupons.getSelectedCartCoupons(); - int cartCouponDiscount = selectedCartCoupons.discountProduct(productCouponAppliedSum); + int cartCouponDiscount = selectedCartCoupon.discountProduct(productCouponAppliedSum); for (OrderedProduct orderedProduct : orderedProducts) { List couponApplyResults = discountsByOrderedProductId.get(orderedProduct.getId()); @@ -170,7 +169,7 @@ public static Map> getCouponApplyResult(List list = new ArrayList<>(); list.add(new CouponApplyResult( - selectedCartCoupons.getId(), + selectedCartCoupon.getId(), CouponType.CART, orderedProduct.getProductSalePriceWithQuantity(), //baseAmount = 상품할인 안한 즉시할인(옵션o,수량o) 총합 가격 cartCouponAppliedPrice)); diff --git a/src/main/java/com/programmers/smrtstore/domain/coupon/application/OrderCouponService.java b/src/main/java/com/programmers/smrtstore/domain/coupon/application/OrderCouponService.java index a329ce634..c5d646cdf 100644 --- a/src/main/java/com/programmers/smrtstore/domain/coupon/application/OrderCouponService.java +++ b/src/main/java/com/programmers/smrtstore/domain/coupon/application/OrderCouponService.java @@ -2,6 +2,7 @@ import com.programmers.smrtstore.core.properties.ErrorCode; import com.programmers.smrtstore.domain.coupon.domain.entity.Coupon; +import com.programmers.smrtstore.domain.coupon.domain.exception.CouponException; import com.programmers.smrtstore.domain.coupon.infrastructure.CouponJpaRepository; import com.programmers.smrtstore.domain.coupon.presentation.res.CouponResponse; import com.programmers.smrtstore.domain.orderManagement.orderSheet.presentation.dto.req.SelectedCouponsRequest; @@ -16,10 +17,7 @@ import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; @Service @RequiredArgsConstructor @@ -43,15 +41,24 @@ public class OrderCouponService { */ // 1. 쿠폰을 선택해서 파라미터(selectedCoupons)로 주는 메서드 -->여기선 selectedCoupons 그대로 반환하고 public OrderSheetCouponInfo getOrderSheetCouponInfoWithSelectedCoupons(Long userId, List orderedProducts, SelectedCouponsRequest selectedCouponsRequest) { - ApplicableProductCoupons applicableProductCoupons = getApplicableProductCoupons(userId, orderedProducts); ApplicableDeliveryFeeCoupons applicableDeliveryFeeCoupons = getApplicableDeliveryFeeCoupons(); - List cartCoupons = getCartCoupons(); + List cartCoupons = getCartCoupons().stream().map(coupon -> CouponResponse.from(coupon)).toList(); + Map selectedProductCoupons = new HashMap<>(); + for (OrderedProduct orderedProduct : orderedProducts) { + if (selectedCouponsRequest.getSelectedProductCouponListsByOrderedProductId().containsKey(orderedProduct.getId())) { + selectedProductCoupons.put(orderedProduct.getId(), getCouponJpaRepositoryById(selectedCouponsRequest.getSelectedProductCouponListsByOrderedProductId().get(orderedProduct.getId()))); + } + } + + Coupon cartCoupon = couponJpaRepository.findById(selectedCouponsRequest.getSelectedCartCoupons()) + .orElseThrow(()->new CouponException(ErrorCode.COUPON_NOT_FOUND)); + SelectedCoupons selectedCoupons = getSelectedCoupons(selectedCouponsRequest); - Map> discountsByOrderedProductId = OrderCouponDiscountCalculator.getCouponApplyResult(orderedProducts, selectedCouponsRequest); - List cartCouponResponses = cartCoupons.stream().map(cartCoupon -> CouponResponse.from(cartCoupon)).toList(); - return new OrderSheetCouponInfo(discountsByOrderedProductId, selectedCoupons, applicableProductCoupons, applicableDeliveryFeeCoupons, cartCouponResponses); + Map> discountsByOrderedProductId = OrderCouponDiscountCalculator.getCouponApplyResult(orderedProducts,selectedProductCoupons ,cartCoupon); + CouponResponse cartCouponResponses = CouponResponse.from(cartCoupon); + return new OrderSheetCouponInfo(discountsByOrderedProductId, selectedCoupons, applicableProductCoupons, applicableDeliveryFeeCoupons, cartCoupons); } //2. 쿠폰 선택X, 쿠폰 서비스에서 자체적으로 최적의 쿠폰 조합을 제공해야함. -> 여기선 selectedCoupons 을 최적 알고리즘으로 반환 @@ -82,16 +89,21 @@ private SelectedCoupons getSelectedCoupons(SelectedCouponsRequest selectedCoupon Map selectedProductDuplicateCouponsByOrderedProductId = new HashMap<>(); for (Long orderedProductId : selectedCoupons.getSelectedProductCouponListsByOrderedProductId().keySet()) { - Coupon coupon = selectedCoupons.getSelectedProductCouponListsByOrderedProductId().get(orderedProductId); + Coupon coupon = getCouponJpaRepositoryById(selectedCoupons.getSelectedProductCouponListsByOrderedProductId().get(orderedProductId)); if (coupon.isDuplicationYn()) { selectedProductDuplicateCouponsByOrderedProductId.put(orderedProductId, CouponResponse.from(coupon)); } else selectedProductCouponListsByOrderedProductId.put(orderedProductId, CouponResponse.from(coupon)); } - CouponResponse cartCoupon = CouponResponse.from(selectedCoupons.getSelectedCartCoupons()); + CouponResponse cartCoupon = CouponResponse.from(getCouponJpaRepositoryById(selectedCoupons.getSelectedCartCoupons())); return new SelectedCoupons(selectedProductCouponListsByOrderedProductId, selectedProductDuplicateCouponsByOrderedProductId, cartCoupon); } + private Coupon getCouponJpaRepositoryById(Long couponId) { + return couponJpaRepository.findById(couponId) + .orElseThrow(()-> new CouponException(ErrorCode.COUPON_NOT_FOUND)); + } + private ApplicableProductCoupons getApplicableProductCoupons(Long userId, List orderedProducts) { Map> productCouponListsByOrderedProductId = new HashMap<>(); diff --git a/src/main/java/com/programmers/smrtstore/domain/coupon/presentation/res/CouponResponse.java b/src/main/java/com/programmers/smrtstore/domain/coupon/presentation/res/CouponResponse.java index 01d5a5309..f0d1c1234 100644 --- a/src/main/java/com/programmers/smrtstore/domain/coupon/presentation/res/CouponResponse.java +++ b/src/main/java/com/programmers/smrtstore/domain/coupon/presentation/res/CouponResponse.java @@ -17,6 +17,7 @@ @AllArgsConstructor(access = AccessLevel.PRIVATE) public class CouponResponse { + private final Long id; private final String name; private final String content; private final Integer benefitValue; @@ -36,6 +37,7 @@ public class CouponResponse { public static CouponResponse from(Coupon coupon) { return CouponResponse.builder() + .id(coupon.getId()) .name(coupon.getCouponValue().getName()) .content(coupon.getCouponValue().getContent()) .benefitValue(coupon.getCouponValue().getBenefitValue()) diff --git a/src/main/java/com/programmers/smrtstore/domain/orderManagement/orderSheet/presentation/dto/req/SelectedCouponsRequest.java b/src/main/java/com/programmers/smrtstore/domain/orderManagement/orderSheet/presentation/dto/req/SelectedCouponsRequest.java index 1aa23b2ad..0bd9cd0e2 100644 --- a/src/main/java/com/programmers/smrtstore/domain/orderManagement/orderSheet/presentation/dto/req/SelectedCouponsRequest.java +++ b/src/main/java/com/programmers/smrtstore/domain/orderManagement/orderSheet/presentation/dto/req/SelectedCouponsRequest.java @@ -10,6 +10,6 @@ @AllArgsConstructor public class SelectedCouponsRequest { //Long은 orderedProduct - private Map selectedProductCouponListsByOrderedProductId; //오더프로덕트당 선택된 쿠폰 - private Coupon selectedCartCoupons; + private Map selectedProductCouponListsByOrderedProductId; //오더프로덕트당 선택된 쿠폰 + private Long selectedCartCoupons; }