Skip to content

Commit

Permalink
[REFACT] CouponResponse ,Request 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
YongNyeo committed Jan 17, 2024
1 parent 865f25a commit 1e40364
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public static SelectedCouponsWithCouponApplyResult getMaxDiscountCouponApplyResu


//TODO: 메서드2. 주어진 select 대로 계산해서 반환
public static Map<Long, List<CouponApplyResult>> getCouponApplyResult(List<OrderedProduct> orderedProducts, SelectedCouponsRequest selectedCoupons) {
public static Map<Long, List<CouponApplyResult>> getCouponApplyResult(List<OrderedProduct> orderedProducts, Map<Long,Coupon>selectedProductCoupons,Coupon selectedCartCoupon) {

Map<Long, List<CouponApplyResult>> discountsByOrderedProductId = new HashMap<>();

Expand All @@ -136,8 +136,8 @@ public static Map<Long, List<CouponApplyResult>> getCouponApplyResult(List<Order
int productDiscountSum = 0;

for (OrderedProduct orderedProduct : orderedProducts) {
if (selectedCoupons.getSelectedProductCouponListsByOrderedProductId().containsKey(orderedProduct.getId())) { //쿠폰이 적용된 상품이라면
Coupon coupon = selectedCoupons.getSelectedProductCouponListsByOrderedProductId().get(orderedProduct.getId());
if (selectedProductCoupons.containsKey(orderedProduct.getId())) { //쿠폰이 적용된 상품이라면
Coupon coupon = selectedProductCoupons.get(orderedProduct.getId());

Integer baseAmount = orderedProduct.getProductSalePriceWithQuantity();
Integer saleAmount = coupon.discountProduct(baseAmount);
Expand All @@ -158,8 +158,7 @@ public static Map<Long, List<CouponApplyResult>> getCouponApplyResult(List<Order
int productCouponAppliedSum = orderedProductSalePriceSum - productDiscountSum; //즉시할인가 총합 - 상품쿠폰 적용가 -> 상품쿠폰까지 적용된 총합

//TODO: 장바구니 쿠폰을 각 OrderedProduct에 CouponApplyResult를 추가하기
Coupon selectedCartCoupons = selectedCoupons.getSelectedCartCoupons();
int cartCouponDiscount = selectedCartCoupons.discountProduct(productCouponAppliedSum);
int cartCouponDiscount = selectedCartCoupon.discountProduct(productCouponAppliedSum);

for (OrderedProduct orderedProduct : orderedProducts) {
List<CouponApplyResult> couponApplyResults = discountsByOrderedProductId.get(orderedProduct.getId());
Expand All @@ -170,7 +169,7 @@ public static Map<Long, List<CouponApplyResult>> getCouponApplyResult(List<Order
if (couponApplyResults.size() == 0) { //상품 쿠폰 적용 없었던 것들
List<CouponApplyResult> list = new ArrayList<>();
list.add(new CouponApplyResult(
selectedCartCoupons.getId(),
selectedCartCoupon.getId(),
CouponType.CART,
orderedProduct.getProductSalePriceWithQuantity(), //baseAmount = 상품할인 안한 즉시할인(옵션o,수량o) 총합 가격
cartCouponAppliedPrice));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -43,15 +41,24 @@ public class OrderCouponService {
*/
// 1. 쿠폰을 선택해서 파라미터(selectedCoupons)로 주는 메서드 -->여기선 selectedCoupons 그대로 반환하고
public OrderSheetCouponInfo getOrderSheetCouponInfoWithSelectedCoupons(Long userId, List<OrderedProduct> orderedProducts, SelectedCouponsRequest selectedCouponsRequest) {

ApplicableProductCoupons applicableProductCoupons = getApplicableProductCoupons(userId, orderedProducts);
ApplicableDeliveryFeeCoupons applicableDeliveryFeeCoupons = getApplicableDeliveryFeeCoupons();
List<Coupon> cartCoupons = getCartCoupons();
List<CouponResponse> cartCoupons = getCartCoupons().stream().map(coupon -> CouponResponse.from(coupon)).toList();
Map<Long, Coupon> 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<Long, List<CouponApplyResult>> discountsByOrderedProductId = OrderCouponDiscountCalculator.getCouponApplyResult(orderedProducts, selectedCouponsRequest);
List<CouponResponse> cartCouponResponses = cartCoupons.stream().map(cartCoupon -> CouponResponse.from(cartCoupon)).toList();

return new OrderSheetCouponInfo(discountsByOrderedProductId, selectedCoupons, applicableProductCoupons, applicableDeliveryFeeCoupons, cartCouponResponses);
Map<Long, List<CouponApplyResult>> discountsByOrderedProductId = OrderCouponDiscountCalculator.getCouponApplyResult(orderedProducts,selectedProductCoupons ,cartCoupon);
CouponResponse cartCouponResponses = CouponResponse.from(cartCoupon);
return new OrderSheetCouponInfo(discountsByOrderedProductId, selectedCoupons, applicableProductCoupons, applicableDeliveryFeeCoupons, cartCoupons);
}

//2. 쿠폰 선택X, 쿠폰 서비스에서 자체적으로 최적의 쿠폰 조합을 제공해야함. -> 여기선 selectedCoupons 을 최적 알고리즘으로 반환
Expand Down Expand Up @@ -82,16 +89,21 @@ private SelectedCoupons getSelectedCoupons(SelectedCouponsRequest selectedCoupon
Map<Long, CouponResponse> 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<OrderedProduct> orderedProducts) {

Map<Long, List<CouponResponse>> productCouponListsByOrderedProductId = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
@AllArgsConstructor
public class SelectedCouponsRequest {
//Long은 orderedProduct
private Map<Long, Coupon> selectedProductCouponListsByOrderedProductId; //오더프로덕트당 선택된 쿠폰
private Coupon selectedCartCoupons;
private Map<Long, Long> selectedProductCouponListsByOrderedProductId; //오더프로덕트당 선택된 쿠폰
private Long selectedCartCoupons;
}

0 comments on commit 1e40364

Please sign in to comment.