Commit c356a5a0 by weijiguang

fix coupon bug

parent a7139be8
......@@ -52,6 +52,8 @@ public class GoodsSku extends BaseEntity
private BigDecimal discount;
private BigDecimal origDiscount;
@Override
public String toString() {
return new ToStringBuilder(this,ToStringStyle.MULTI_LINE_STYLE)
......
package com.soss.system.service.impl;
import com.alibaba.fastjson.JSON;
import com.soss.common.enums.CouponCategoryType;
import com.soss.common.enums.CouponState;
import com.soss.common.enums.CouponUserType;
......@@ -23,7 +22,6 @@ import org.springframework.util.CollectionUtils;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.*;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Function;
import java.util.stream.Collectors;
......@@ -189,12 +187,9 @@ public class CouponUserServiceImpl implements ICouponUserService {
List<Goods> goods = goodsMapper.selectGoodsByIds(goodsIds);
Assert.notEmpty(goods, "未查询到匹配的商品信息[id=" + goodsIds + "]");
// 订单总额
BigDecimal orderTotalAmount = orderDetails.stream().map(x -> x.getAmountShould().multiply(new BigDecimal(x.getNum()))).reduce(BigDecimal.ZERO, BigDecimal::add);
BigDecimal orderTotalOrigDiscount = orderDetails.stream().map(x -> x.getOrigDiscount().multiply(new BigDecimal(x.getNum()))).reduce(BigDecimal.ZERO, BigDecimal::add);
BigDecimal orderTotalAmount = orderDetails.stream().map(OrderDetail::getAmountShould).reduce(BigDecimal.ZERO, BigDecimal::add);
BigDecimal orderTotalOrigDiscount = orderDetails.stream().map(OrderDetail::getOrigDiscount).reduce(BigDecimal.ZERO, BigDecimal::add);
log.info("orderTotalAmount:{}, orderTotalOrigDiscount:{}", orderTotalAmount, orderTotalOrigDiscount);
if (!CollectionUtils.isEmpty(couponVos)) {
orderDetails.forEach(x -> x.setAmountShould(x.getOrigDiscount().multiply(new BigDecimal(x.getNum()))));
}
couponVos.stream().filter(couponVo -> StringUtils.isEmpty(couponVo.getNotFitableDesc())).forEach(couponVo -> {
/** 可用饮品范围的判断 */
List<Long> fitCouponGoodsIdList = goods.stream().filter(good ->
......@@ -211,31 +206,15 @@ public class CouponUserServiceImpl implements ICouponUserService {
}
/** 券价值的判断 */
if (couponVo.getType().equals(CouponCategoryType.DEDUCTION.getType())) { // 抵扣
if (couponVo.getPriceLimit() != null && couponVo.getPriceLimit().compareTo(BigDecimal.ZERO) != 0 && couponVo.getPriceLimit().compareTo(orderTotalOrigDiscount) > 0) {
if (couponVo.getPriceLimit() != null && couponVo.getPriceLimit().compareTo(BigDecimal.ZERO) != 0 && couponVo.getPriceLimit().compareTo(orderTotalAmount) > 0) {
couponVo.setNotFitableDesc("未达到满减要求");
return;
}
couponVo.setCouponAmount(couponVo.getPriceDiscount().min(orderTotalOrigDiscount));
AtomicReference<Integer> nums = new AtomicReference<>(0);
orderDetails.forEach(x -> nums.updateAndGet(v -> v + x.getNum()));
log.info("nums:{}", nums);
if (nums.get() <= 1) {
OrderDetail orderDetail = orderDetails.get(0);
orderDetail.setCouponAmount(couponVo.getCouponAmount().negate());
orderDetail.setRealAmount(orderDetail.getAmountShould().add(orderDetail.getCouponAmount()));
} else {
orderDetails.forEach(orderDetail -> {
BigDecimal rate = orderDetail.getAmountShould().divide(orderTotalOrigDiscount);
orderDetail.setCouponAmount(couponVo.getCouponAmount().negate().multiply(rate));
orderDetail.setRealAmount(orderDetail.getAmountShould().add(orderDetail.getCouponAmount()));
log.info("rate:{},detail:{}", rate, JSON.toJSON(orderDetail));
});
}
} else {
List<OrderDetail> fitOrderDetails = orderDetails.stream().filter(orderDetail -> fitCouponGoodsIdList.contains(orderDetail.getGoodsId())).collect(Collectors.toList());
BigDecimal fitGoodsAmount = fitOrderDetails.stream().map(x -> x.getAmountShould().multiply(new BigDecimal(x.getNum()))).reduce(BigDecimal.ZERO, BigDecimal::add);
BigDecimal fitGoodsOrigDiscount = fitOrderDetails.stream().map(x -> x.getOrigDiscount().multiply(new BigDecimal(x.getNum()))).reduce(BigDecimal.ZERO, BigDecimal::add);
BigDecimal fitGoodsAmount = fitOrderDetails.stream().map(OrderDetail::getAmountShould).reduce(BigDecimal.ZERO, BigDecimal::add);
BigDecimal fitGoodsOrigDiscount = fitOrderDetails.stream().map(OrderDetail::getOrigDiscount).reduce(BigDecimal.ZERO, BigDecimal::add);
log.info("fitGoodsOrigDiscount:{}, fitGoodsAmount:{}", fitGoodsOrigDiscount, fitGoodsAmount);
if (couponVo.getPriceLimit() != null && couponVo.getPriceLimit().compareTo(BigDecimal.ZERO) != 0 && couponVo.getPriceLimit().compareTo(fitGoodsAmount) > 0) {
if (couponVo.getType().equals(CouponCategoryType.DISCOUNT.getType())) { // 折扣
......@@ -282,6 +261,7 @@ public class CouponUserServiceImpl implements ICouponUserService {
orderDetail.setOriAmount(skuNum.multiply(sku.getPrice()));
orderDetail.setUnitPrice(sku.getDiscount());
orderDetail.setAmountShould(skuNum.multiply(sku.getDiscount()));
orderDetail.setOrigDiscount(sku.getOrigDiscount());
});
return goodsSkus;
}
......
......@@ -21,7 +21,9 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<select id="selectSkuListForOrder" resultMap="GoodsSkuResult">
select gs.id, gs.goods_id, gs.price,
case when sgs.discount>=0 then sgs.discount else gs.discount end as discount, gs.is_deleted
case when sgs.discount>=0 then sgs.discount else gs.discount end as discount,
case when sgs.orig_discount>0 then sgs.orig_discount else gs.discount end orig_discount,
gs.is_deleted
from goods_sku gs
left join shop_goods_sku sgs on gs.goods_id = sgs.goods_id and gs.id = sgs.sku_id
where sgs.shop_id = #{shopId}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment