rk
昨天 7869fa712c19c351a48bfb0af11b0fe87b17c184
server/dmmall_service/src/main/java/com/doumee/service/business/impl/MemberCouponServiceImpl.java
@@ -379,7 +379,6 @@
        if(CollectionUtils.isEmpty(goodsSkuList)||!Constants.equalsInteger(goodsSkuList.size(),requestList.size())){
            throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"商品SKU信息错误,请刷新重试");
        }
        //查询商品列表
        List<Goods> goodsList = goodsMapper.selectJoinList(Goods.class,
                new MPJLambdaWrapper<Goods>()
@@ -391,7 +390,6 @@
        if(Objects.isNull(goodsList)){
            return memberCouponList;
        }
        //获取选择的商品总金额 与 单个商品金额
        BigDecimal totalAmount = BigDecimal.ZERO;
        for (Goods goods:goodsList) {
@@ -437,6 +435,73 @@
    }
    public void  couponIsValid(List<PayDetailRequest> requestList,MemberCoupon memberCoupon){
        if(Objects.isNull(memberCoupon)
            || Constants.equalsInteger(memberCoupon.getStatus(),Constants.ONE)
        ){
            throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"优惠券信息无效或已被使用");
        }
        if(memberCoupon.getStartDate().getTime() > System.currentTimeMillis()){
            throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"优惠券未生效");
        }
        if(memberCoupon.getEndDate().getTime() < System.currentTimeMillis()){
            throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"优惠券已过期");
        }
        List<GoodsSku> goodsSkuList = goodsSkuMapper.selectList(new QueryWrapper<GoodsSku>()
                .lambda()
                .eq(GoodsSku::getIsdeleted,Constants.ZERO)
                .in(GoodsSku::getId,requestList.stream().map(i->i.getGoodsSkuId()).collect(Collectors.toList()))
        );
        if(CollectionUtils.isEmpty(goodsSkuList)||!Constants.equalsInteger(goodsSkuList.size(),requestList.size())){
            throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"商品SKU信息错误,请刷新重试");
        }
        //查询商品列表
        List<Goods> goodsList = goodsMapper.selectJoinList(Goods.class,
                new MPJLambdaWrapper<Goods>()
                        .selectAs(GoodsSku::getPrice,Goods::getSkuPrice)
                        .selectAs(GoodsSku::getId,Goods::getSkuId)
                        .leftJoin(GoodsSku.class,GoodsSku::getGoodsId,Goods::getId)
                        .in(GoodsSku::getId,requestList.stream().map(i->i.getGoodsSkuId()).collect(Collectors.toList())));
        if(Objects.isNull(goodsList)){
            throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"商品SKU商品信息错误,请刷新重试");
        }
        //获取选择的商品总金额 与 单个商品金额
        BigDecimal totalAmount = BigDecimal.ZERO;
        for (Goods goods:goodsList) {
            List<PayDetailRequest> request = requestList.stream().filter(i->Constants.equalsInteger(i.getGoodsSkuId(), goods.getSkuId())).collect(Collectors.toList());
            if(CollectionUtils.isNotEmpty(request)){
                goods.setSkuAmount(goods.getSkuPrice().multiply(new BigDecimal(request.get(Constants.ZERO).getGoodsNum()+"")));
                totalAmount = totalAmount.add(goods.getSkuAmount());
            }
        }
        List<OrderGoodsCalculateResponse> goodsCalculateList = ListUtil.copyProperties(goodsList,OrderGoodsCalculateResponse::new);
        if(totalAmount.compareTo(memberCoupon.getLimitPrice())<Constants.ZERO){
            throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"当前商品金额未满足可使用门槛");
        }
        //适用类型:0=全场;1=品类;2=指定商品
        if(Constants.equalsInteger(memberCoupon.getApplyType(),Constants.ZERO)){
            memberCoupon.setValidAmount(memberCoupon.getPrice());
        }else{
            List<String> applyIdList = Arrays.asList(memberCoupon.getApplyIds().split(","));
            //获取有效可以使用优惠券的数据
            isVaildUse(applyIdList,goodsCalculateList,
                    Constants.equalsInteger(memberCoupon.getApplyType(),Constants.ONE)?Constants.ONE:Constants.ZERO);
            BigDecimal skuAmount = goodsCalculateList.stream().filter(i->i.getUseCoupon()).map(i->i.getSkuAmount()).reduce(BigDecimal.ZERO,BigDecimal::add);
            if(skuAmount.compareTo(memberCoupon.getLimitPrice())>=Constants.ZERO){
                //根据金额计算实际折扣金额
                BigDecimal couponPrice = skuAmount.multiply(memberCoupon.getPrice()).divide(new BigDecimal("100"),2, RoundingMode.HALF_UP);
                //折扣金额大于最大可折扣金额  则使用最大可折扣金额
                memberCoupon.setValidAmount(couponPrice.compareTo(memberCoupon.getMaxPrice())>Constants.ZERO?memberCoupon.getMaxPrice():couponPrice);
            }
        }
    }
    /**
     * 计算当前已选择优惠券每个商品占比金额
     * @param coupon 优惠券信息