Skip to content

Commit

Permalink
Feat: 주문/장바구니와 쿠폰 정보를 따로 받을 수 있도록 수정 [#176]
Browse files Browse the repository at this point in the history
  • Loading branch information
chd830 committed Nov 12, 2022
1 parent 8ed8dec commit 904b876
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.feelmycode.parabole.dto.CouponCreateResponseDto;
import com.feelmycode.parabole.dto.CouponDto;
import com.feelmycode.parabole.dto.CouponInfoResponseDto;
import com.feelmycode.parabole.dto.CouponRequestDto;
import com.feelmycode.parabole.dto.CouponSellerResponseDto;
import com.feelmycode.parabole.dto.CouponUseAndAssignRequestDto;
import com.feelmycode.parabole.dto.CouponUserResponseDto;
Expand Down Expand Up @@ -159,4 +160,10 @@ public Boolean setProductRemains(@PathVariable("couponId") Long couponId,
log.info("Set Coupon Remains By Event Server : {} ", couponId);
return couponService.setCouponStock(couponId, stock);
}

@GetMapping
public ResponseEntity<ParaboleResponse> getCouponGroupBySeller(@RequestAttribute Long userId, @RequestBody CouponRequestDto couponRequestDto) {
return ParaboleResponse.CommonResponse(HttpStatus.OK, true, couponRequestDto.getSellerId()+" 판매자가 사용할 수 있는 쿠폰 목록입니다.", couponService.getCouponListByDiscountValue(userId, couponRequestDto));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,12 @@ public class CartBySellerDto {
private Long sellerId;
private String storeName;
private List<CartItemDto> cartItemDtoList;
private CouponResponseDto couponDto;

public CartBySellerDto(Long sellerId, String storeName,
List<CartItemDto> cartItemDtoList, CouponResponseDto couponDto) {
List<CartItemDto> cartItemDtoList) {
this.sellerId = sellerId;
this.storeName = storeName;
this.cartItemDtoList = cartItemDtoList;
this.couponDto = couponDto;
}

public void makeNotNullResponseDto() {
this.couponDto = new CouponResponseDto();
}
}
4 changes: 3 additions & 1 deletion src/main/java/com/feelmycode/parabole/dto/CouponInfoDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ public class CouponInfoDto {
private String storeName;
private String type;
private Integer discountValue;
private Long totalFee;

public CouponInfoDto(String couponName, String serialNo, String storeName, String type, Integer discountValue) {
public CouponInfoDto(String couponName, String serialNo, String storeName, String type, Integer discountValue, Long totalFee) {
this.couponName = couponName;
this.serialNo = serialNo;
this.storeName = storeName;
this.type = type;
this.discountValue = discountValue;
this.totalFee = totalFee;
}

}
18 changes: 18 additions & 0 deletions src/main/java/com/feelmycode/parabole/dto/CouponRequestDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.feelmycode.parabole.dto;

import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor
public class CouponRequestDto {

private Long sellerId;
private Long totalFee;

public CouponRequestDto(Long sellerId, Long totalFee) {
this.sellerId = sellerId;
this.totalFee = totalFee;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,12 @@ public class OrderBySellerDto {
private Long sellerId;
private String storeName;
private List<OrderInfoResponseDto> orderInfoResponseDtos;
private CouponResponseDto couponDto;

public OrderBySellerDto(Long sellerId, String storeName,
List<OrderInfoResponseDto> orderInfoResponseDtos, CouponResponseDto couponDto) {
List<OrderInfoResponseDto> orderInfoResponseDtos) {
this.sellerId = sellerId;
this.storeName = storeName;
this.orderInfoResponseDtos = orderInfoResponseDtos;
this.couponDto = couponDto;
}

}
19 changes: 2 additions & 17 deletions src/main/java/com/feelmycode/parabole/service/CartItemService.java
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,6 @@ public CartResponseDto getCartItemGroupBySellerIdOrderByIdDesc(Long userId) {
getItemList[sellerIdMap.get(key)].add(new CartItemDto(item));
}

HashMap<Long, CouponResponseDto> couponList = couponService.getCouponMapByUserId(userId);

List<CartBySellerDto> cartBySellerDtoList = new ArrayList<>();

HashSet<Long> checkContainsSellerId = new HashSet<>();
Expand All @@ -127,24 +125,11 @@ public CartResponseDto getCartItemGroupBySellerIdOrderByIdDesc(Long userId) {
Long sellerId = Long.parseLong(key.split("\\$")[0]);
String storeName = key.split("\\$")[1];
if(checkContainsSellerId.add(sellerId)) {
if(couponList.isEmpty()) {
cartBySellerDtoList.add(
new CartBySellerDto(sellerId, storeName, getItemList[sellerIdMap.get(key)],
new CouponResponseDto()));
} else {
cartBySellerDtoList.add(
new CartBySellerDto(sellerId, storeName, getItemList[sellerIdMap.get(key)],
couponList.get(sellerId)));
}
cartBySellerDtoList.add(
new CartBySellerDto(sellerId, storeName, getItemList[sellerIdMap.get(key)]));
}
}

for(CartBySellerDto dto : cartBySellerDtoList){
if(dto.getCouponDto() == null) {
dto.makeNotNullResponseDto();
}
}

return new CartResponseDto(cart.getId(), cnt, cartBySellerDtoList);
}

Expand Down
46 changes: 44 additions & 2 deletions src/main/java/com/feelmycode/parabole/service/CouponService.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.feelmycode.parabole.dto.CouponCreateResponseDto;
import com.feelmycode.parabole.dto.CouponInfoDto;
import com.feelmycode.parabole.dto.CouponInfoResponseDto;
import com.feelmycode.parabole.dto.CouponRequestDto;
import com.feelmycode.parabole.dto.CouponResponseDto;
import com.feelmycode.parabole.dto.CouponSellerResponseDto;
import com.feelmycode.parabole.dto.CouponUserResponseDto;
Expand All @@ -23,6 +24,7 @@
import com.sun.istack.NotNull;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -150,6 +152,44 @@ public CouponInfoResponseDto getCouponInfo(String couponSNo) {
return new CouponInfoResponseDto(coupon.getType().getName(), coupon.getDiscountValue());
}

public List<CouponInfoDto> getCouponListByDiscountValue(Long userId, CouponRequestDto couponRequestDto) {

Long sellerId = couponRequestDto.getSellerId();
Long totalFee = couponRequestDto.getTotalFee();

List<UserCoupon> couponList = userCouponRepository.findAllByUserId(userId);

List<UserCoupon> getCouponListBySellerId = couponList.stream()
.filter(coupon -> coupon.getCoupon().getSeller().getId() == sellerId)
.collect(Collectors.toList());

Collections.sort(getCouponListBySellerId, new Comparator<UserCoupon>() {
@Override
public int compare(UserCoupon coupon1, UserCoupon coupon2) {
Long discountO1 = discountValue(coupon1, totalFee);
Long discountO2 = discountValue(coupon2, totalFee);
return -Long.compare(discountO1, discountO2);
}
});

List<CouponInfoDto> couponListDto = getCouponListBySellerId.stream()
.map(coupon -> new CouponInfoDto(coupon.getCoupon().getName(), coupon.getSerialNo(),
coupon.getCoupon().getSeller().getStoreName(), coupon.getCoupon().getType().getName(), coupon.getCoupon().getDiscountValue(), discountValue(coupon, totalFee)))
.collect(Collectors.toList());

return couponListDto;
}

public Long discountValue(UserCoupon userCoupon, Long value) {
Coupon coupon = userCoupon.getCoupon();
CouponType type = coupon.getType();
if(type == CouponType.RATE) {
return (value *= 100-coupon.getDiscountValue())/100;
}
return value -= coupon.getDiscountValue();
}

// userId를 기준으로 쿠폰들을 묶음
public HashMap<Long, CouponResponseDto> getCouponMapByUserId(Long userId) {

Comparator<UserCoupon> coupon = (c1, c2) -> {
Expand Down Expand Up @@ -188,7 +228,8 @@ public HashMap<Long, CouponResponseDto> getCouponMapByUserId(Long userId) {
userCoupon.getSerialNo(),
couponInfo.getSeller().getStoreName(),
couponInfo.getType().getName(),
couponInfo.getDiscountValue()
couponInfo.getDiscountValue(),
0L
));

couponMap.put(sellerId, response.setRateCoupon(rateCoupon));
Expand All @@ -200,7 +241,8 @@ else if(couponInfo.getType() == CouponType.AMOUNT){ // AMOUNT TYPE
userCoupon.getSerialNo(),
couponInfo.getSeller().getStoreName(),
couponInfo.getType().getName(),
couponInfo.getDiscountValue()
couponInfo.getDiscountValue(),
0L
));

couponMap.put(sellerId, response.setAmountCoupon(amountCoupon));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,27 +151,16 @@ public OrderResponseDto getOrderInfoGroupBySellerIdOrderByIdDesc(Long userId) {
getOrderInfoList[sellerIdMap.get(sellerId)].add(dto);
}

HashMap<Long, CouponResponseDto> couponList = couponService.getCouponMapByUserId(userId);

List<OrderBySellerDto> orderBySellerDtoList = new ArrayList<>();

HashSet<Long> checkContainsSellerId = new HashSet<>();

for (Long sellerId : sellerIdMap.keySet()) {
if (checkContainsSellerId.add(sellerId)) {
if (couponList.isEmpty()) {
orderBySellerDtoList.add(
new OrderBySellerDto(sellerId,
sellerService.getSellerBySellerId(sellerId).getStoreName(),
getOrderInfoList[sellerIdMap.get(sellerId)],
new CouponResponseDto()));
} else {
orderBySellerDtoList.add(
new OrderBySellerDto(sellerId,
sellerService.getSellerBySellerId(sellerId).getStoreName(),
getOrderInfoList[sellerIdMap.get(sellerId)],
couponList.get(sellerId)));
}
orderBySellerDtoList.add(
new OrderBySellerDto(sellerId,
sellerService.getSellerBySellerId(sellerId).getStoreName(),
getOrderInfoList[sellerIdMap.get(sellerId)]));
}
}
return new OrderResponseDto(order.getId(), cnt, orderBySellerDtoList);
Expand Down

0 comments on commit 904b876

Please sign in to comment.