diff --git a/xdclass-order-service/src/main/java/net/jieyuu/feign/CouponFeignService.java b/xdclass-order-service/src/main/java/net/jieyuu/feign/CouponFeignService.java new file mode 100644 index 0000000..769ac6b --- /dev/null +++ b/xdclass-order-service/src/main/java/net/jieyuu/feign/CouponFeignService.java @@ -0,0 +1,18 @@ +package net.jieyuu.feign; + +import net.jieyuu.utils.JsonData; +import org.springframework.cloud.openfeign.FeignClient; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; + +@FeignClient(name = "xdclass-coupon-service") +public interface CouponFeignService { + /** + * 查询用户的优惠券是否可用,注意防止水平越权 + * @param recordId + * @return + */ + @GetMapping("/api/coupon_record/v1/detail/{record_id}") + JsonData findUserCouponRecordById(@PathVariable("record_id") long recordId); + +} diff --git a/xdclass-order-service/src/main/java/net/jieyuu/service/impl/ProductOrderServiceImpl.java b/xdclass-order-service/src/main/java/net/jieyuu/service/impl/ProductOrderServiceImpl.java index 9d75663..1346cc2 100644 --- a/xdclass-order-service/src/main/java/net/jieyuu/service/impl/ProductOrderServiceImpl.java +++ b/xdclass-order-service/src/main/java/net/jieyuu/service/impl/ProductOrderServiceImpl.java @@ -4,7 +4,9 @@ import com.alibaba.fastjson.TypeReference; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import lombok.extern.slf4j.Slf4j; import net.jieyuu.enums.BizCodeEnum; +import net.jieyuu.enums.CouponStateEnum; import net.jieyuu.exception.BizException; +import net.jieyuu.feign.CouponFeignService; import net.jieyuu.feign.ProductFeignService; import net.jieyuu.feign.UserFeignService; import net.jieyuu.interceptor.LoginInterceptor; @@ -17,10 +19,12 @@ import net.jieyuu.service.ProductOrderService; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import net.jieyuu.utils.CommonUtil; import net.jieyuu.utils.JsonData; +import net.jieyuu.vo.CouponRecordVO; import net.jieyuu.vo.ProductOrderAddressVO; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import java.math.BigDecimal; import java.util.List; /** @@ -44,6 +48,9 @@ public class ProductOrderServiceImpl extends ServiceImpl orderItemList, ConfirmOrderRequest orderRequest) { + + // 统计总价 + BigDecimal realPayAmount = new BigDecimal("0"); + if (orderItemList != null) { + for (OrderItemVO item : orderItemList) { + BigDecimal itemRealPayAmount = item.getTotalAmount(); + realPayAmount = realPayAmount.add(itemRealPayAmount); + } + } + // 获取优惠券 + CouponRecordVO couponRecordVO = getCartCouponRecord(orderRequest.getCouponRecordId()); + // 计算购物车价格, 是否满足优惠券满减条件 + + // 计算购物车价格,是否满足优惠券满减条件 + if (couponRecordVO != null) { + // 计算是否满足满减条件 + if (realPayAmount.compareTo(couponRecordVO.getConditionPrice()) < 0) {// 优惠券满足使用条件 + throw new BizException(BizCodeEnum.ORDER_CONFIRM_COUPON_FAIL); + } + + if (couponRecordVO.getPrice().compareTo(realPayAmount) > 0) { // 防止出现优惠券抵扣超过商品价格 + realPayAmount = BigDecimal.ZERO; + }else{ + realPayAmount= realPayAmount.subtract(couponRecordVO.getPrice()); + } + } + + // 验价 + if(realPayAmount.compareTo(orderRequest.getTotalAmount())!=0){ + log.error("订单验价失败:{}" , orderRequest); + throw new BizException(BizCodeEnum.ORDER_CONFIRM_PRICE_FAIL); + } + } + + /** + * 获取优惠券 + * + * @param couponRecordId + * @return + */ + private CouponRecordVO getCartCouponRecord(Long couponRecordId) { + if (couponRecordId == null || couponRecordId < 0) { + return null; + } + JsonData couponRecordData = couponFeignService.findUserCouponRecordById(couponRecordId); + if (couponRecordData.getCode() != 0) { + throw new BizException(BizCodeEnum.ORDER_CONFIRM_COUPON_FAIL); + } + + if (couponRecordData.getCode() == 0) { + CouponRecordVO couponRecordVO = couponRecordData.getData(new TypeReference() { + }); + if (!couponAvailable(couponRecordVO)) { + log.error("优惠券使用失败"); + throw new BizException(BizCodeEnum.COUPON_UNAVAILABLE); + } + return couponRecordVO; + } + return null; + } + + + /** + * 判断优惠券是否可用 + * + * @param couponRecordVO + * @return + */ + private boolean couponAvailable(CouponRecordVO couponRecordVO) { + if (couponRecordVO.getUseState().equalsIgnoreCase(CouponStateEnum.NEW.name())) { + long currentTimestamp = CommonUtil.getCurrentTimestamp(); + long end = couponRecordVO.getEndTime().getTime(); + long start = couponRecordVO.getStartTime().getTime(); + if (currentTimestamp >= start && currentTimestamp <= end) { + return true; + } + } + return false; + } + /** * 获取收货地址详情 * diff --git a/xdclass-order-service/src/main/java/net/jieyuu/vo/CouponRecordVO.java b/xdclass-order-service/src/main/java/net/jieyuu/vo/CouponRecordVO.java new file mode 100644 index 0000000..44c7c8d --- /dev/null +++ b/xdclass-order-service/src/main/java/net/jieyuu/vo/CouponRecordVO.java @@ -0,0 +1,79 @@ +package net.jieyuu.vo; + + +import com.fasterxml.jackson.annotation.JsonFormat; +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.Data; +import java.io.Serializable; +import java.math.BigDecimal; +import java.util.Date; + + +@Data +public class CouponRecordVO implements Serializable { + + private Long id; + + /** + * 优惠券id + */ + @JsonProperty("coupon_id") + private Long couponId; + + /** + * 使用状态 可用 NEW,已使用USED,过期 EXPIRED; + */ + @JsonProperty("use_state") + private String useState; + + /** + * 用户id + */ + @JsonProperty("user_id") + private Long userId; + + /** + * 用户昵称 + */ + @JsonProperty("user_name") + private String userName; + + /** + * 优惠券标题 + */ + @JsonProperty("coupon_title") + private String couponTitle; + + /** + * 优惠券开始有效时间 + */ + @JsonFormat(pattern = "yyyy-MM-dd hh:mm:ss", locale = "zh", timezone = "GMT+8") + @JsonProperty("start_time") + private Date startTime; + + /** + * 优惠券失效时间 + */ + @JsonFormat(pattern = "yyyy-MM-dd hh:mm:ss", locale = "zh", timezone = "GMT+8") + @JsonProperty("end_time") + private Date endTime; + + /** + * 订单id + */ + @JsonProperty("order_id") + private Long orderId; + + /** + * 抵扣价格 + */ + private BigDecimal price; + + /** + * 满多少才可以使用 + */ + @JsonProperty("condition_price") + private BigDecimal conditionPrice; + + +}