KKSU
2024-02-01 dc67dd90c6840d0770cb5bcd3cf62c58f9e0bd14
src/main/java/cc/mrbird/febs/mall/service/impl/ApiMallOrderInfoServiceImpl.java
@@ -124,6 +124,24 @@
        this.baseMapper.insert(orderInfo);
        BigDecimal total = BigDecimal.ZERO;
        /**
         * 根据传入的优惠卷ID和商品明细集合计算出每个商品获取的实际支付金额
         */
        Map<Long, BigDecimal> couponAmountMap = new HashMap<>();
        MallMemberCoupon mallMemberCoupon = new MallMemberCoupon();
        if(ObjectUtil.isNotEmpty(addOrderDto.getMemberCouponId())){
            Long memberCouponId = addOrderDto.getMemberCouponId();
            mallMemberCoupon = mallMemberCouponMapper.selectById(memberCouponId);
            if(ObjectUtil.isEmpty(mallMemberCoupon)){
                throw new FebsException(mallMemberCoupon.getCouponName()+"无法使用");
            }
            Integer state = mallMemberCoupon.getState();
            if(1 != state){
                throw new FebsException(mallMemberCoupon.getCouponName()+"无法使用");
            }
            List<AddOrderItemDto> items = addOrderDto.getItems();
            couponAmountMap = getCouponAmountMap(memberCouponId, items);
        }
        for (AddOrderItemDto item : addOrderDto.getItems()) {
                MallOrderItem orderItem = new MallOrderItem();
@@ -201,49 +219,11 @@
                    //是否使用优惠卷
                    if(ObjectUtil.isNotEmpty(addOrderDto.getMemberCouponId())){
                        Long memberCouponId = addOrderDto.getMemberCouponId();
                        MallMemberCoupon mallMemberCoupon = mallMemberCouponMapper.selectById(memberCouponId);
                        if(ObjectUtil.isEmpty(mallMemberCoupon)){
                            throw new FebsException(mallMemberCoupon.getCouponName()+"无法使用");
                        BigDecimal skuCouponAmount = couponAmountMap.get(item.getSkuId());//使用折扣卷后的应该支付的钱
                        if(skuCouponAmount.compareTo(BigDecimal.ZERO) > 0){
                            amount = skuCouponAmount;
                        }
                        Integer state = mallMemberCoupon.getState();
                        if(1 != state){
                            throw new FebsException(mallMemberCoupon.getCouponName()+"无法使用");
                        }
                        //卷是否和商品绑定
                        Long skuGoodsId = sku.getGoodsId();
                        Long couponId = mallMemberCoupon.getCouponId();
                        MallGoodsCoupon mallGoodsCoupon = mallGoodsCouponMapper.selectById(mallMemberCoupon.getCouponId());
                        if(ObjectUtil.isEmpty(mallGoodsCoupon)){
                            throw new FebsException(mallGoodsCoupon.getName()+"无法使用");
                        }
                        Integer stateCoupon = mallGoodsCoupon.getState();
                        if(2 != stateCoupon){
                            throw new FebsException(mallGoodsCoupon.getName()+"无法使用");
                        }
                        List<CouponGoods> couponGoods = couponGoodsMapper.selectByGoodIdAndCouponId(skuGoodsId, couponId);
                        if(ObjectUtil.isEmpty(couponGoods)){
                            throw new FebsException(sku.getGoodsName()+"无法使用"+mallMemberCoupon.getCouponName());
                        }
//                        Long goodsId = mallMemberCoupon.getGoodsId();
//                        if(goodsId != sku.getGoodsId()){
//                            throw new FebsException(sku.getGoodsName()+"无法使用"+mallMemberCoupon.getCouponName());
//                        }
                        BigDecimal costAmount = mallGoodsCoupon.getCostAmount();
                        BigDecimal realAmount = mallGoodsCoupon.getRealAmount();
                        if(amount.compareTo(costAmount) >= 0){
                            Integer type = mallGoodsCoupon.getType();
                            if(MallGoodsCoupon.TYPE_TWO == type){
                                BigDecimal divideTime = amount.divide(costAmount, 0, BigDecimal.ROUND_DOWN);//累计减免次数
                                BigDecimal multiply = divideTime.multiply(realAmount);//实际减免金额
                                amount = (amount.subtract(multiply).compareTo(BigDecimal.ZERO) > 0 ? amount.subtract(multiply) : BigDecimal.ZERO);
                            }
                            if(MallGoodsCoupon.TYPE_ONE == type){
                                amount = (amount.subtract(realAmount).compareTo(BigDecimal.ZERO) > 0 ? amount.subtract(realAmount) : BigDecimal.ZERO);
                            }
                            orderItem.setMemberCouponId(memberCouponId);
                            mallMemberCoupon.setState(2);
                            mallMemberCouponMapper.updateById(mallMemberCoupon);
                        }
                        orderItem.setMemberCouponId(memberCouponId);
                    }
                    orderItem.setAmount(amount);
                    orderItem.setCnt(item.getCnt());
@@ -278,6 +258,9 @@
                }
                mallOrderItemMapper.insert(orderItem);
        }
        mallMemberCoupon.setState(2);
        mallMemberCouponMapper.updateById(mallMemberCoupon);
        //运费
        BigDecimal delivaryAmount = addOrderDto.getDeliveryAmount() == null ? BigDecimal.ZERO : addOrderDto.getDeliveryAmount();
        orderInfo.setCarriage(delivaryAmount);
@@ -301,10 +284,108 @@
        return orderInfo.getId();
    }
    /**
     * 根据传入的优惠卷ID和商品明细集合计算出每个商品获取的实际支付金额
     * @param memberCouponId 优惠卷ID
     * @param items 商品明细
     * @return Map<Long,BigDecimal> skuId,实际支付金额
     */
    @Override
    public Map<Long,BigDecimal> getCouponAmountMap(Long memberCouponId,List<AddOrderItemDto> items){
        HashMap<Long,BigDecimal> couponAmountMap = new HashMap<>();
        MallMemberCoupon mallMemberCoupon = mallMemberCouponMapper.selectById(memberCouponId);
        if(ObjectUtil.isEmpty(mallMemberCoupon)){
            return couponAmountMap;
        }
        Integer state = mallMemberCoupon.getState();
        if(1 != state){
            return couponAmountMap;
        }
        Long couponId = mallMemberCoupon.getCouponId();//优惠卷ID
        List<Long> goodsIdsAll = couponGoodsMapper.selectByCouponId(couponId);
        List<MallGoodsSku> skusAll = mallGoodsSkuMapper.selectSkuIdsByGoodsId(goodsIdsAll);//获取该优惠卷绑定的全部商品sku
        List<Long> skuIdsAll = skusAll.stream().map(MallGoodsSku::getId).collect(Collectors.toList());//获取该优惠卷绑定的全部商品skuIDS
        List<Long> skuIds = items.stream().map(AddOrderItemDto::getSkuId).collect(Collectors.toList());//订单中的全部skuIds
        /**
         * 获取这张优惠卷生效的skuIds
         */
        Set<Long> intersection = skuIdsAll.stream()
                .filter(item -> skuIds.contains(item))
                .collect(Collectors.toSet());//获取两个集合的交集
        if(CollUtil.isEmpty(intersection)){
            return couponAmountMap;
        }
        HashMap<Long,BigDecimal> skuMap = new HashMap<>();//每个订单详情的ID和amount的map
        List<MallGoodsSku> skus = mallGoodsSkuMapper.selectByIds(new ArrayList<>(intersection));
        BigDecimal totalAmount = BigDecimal.ZERO;//订单中的总金额
        Map<Long, AddOrderItemDto> itemMap = new HashMap<>();// 预先将items转换为Map,以skuId作为键
        for (AddOrderItemDto addOrderItemDto : items) {
            itemMap.put(addOrderItemDto.getSkuId(), addOrderItemDto);
        }
        for(MallGoodsSku mallGoodsSku : skus){
            Long id = mallGoodsSku.getId();
            BigDecimal presentPrice = mallGoodsSku.getPresentPrice();
            // 检查itemMap中是否存在该商品
            AddOrderItemDto addOrderItemDto = itemMap.get(id);
            if (addOrderItemDto != null) {
                BigDecimal skuAmount = presentPrice.multiply(BigDecimal.valueOf(addOrderItemDto.getCnt()));
                totalAmount = totalAmount.add(skuAmount);
                skuMap.put(id,skuAmount);
            }
        }
        /**
         * 计算每个商品利用优惠卷打折的具体金额
         */
        MallGoodsCoupon mallGoodsCoupon = mallGoodsCouponMapper.selectById(mallMemberCoupon.getCouponId());
        BigDecimal costAmount = mallGoodsCoupon.getCostAmount();
        BigDecimal realAmount = mallGoodsCoupon.getRealAmount();
        if(totalAmount.compareTo(costAmount) < 0){
            return couponAmountMap;
        }
        HashMap<Long,BigDecimal> skuMapPercent = new HashMap<>();//每个订单详情的ID和amount的map
        for (Map.Entry<Long,BigDecimal> entry : skuMap.entrySet()) {
            Long keySkuId = entry.getKey();
            BigDecimal valueSkuAmount = entry.getValue();
            BigDecimal divide = valueSkuAmount.divide(totalAmount, 4, BigDecimal.ROUND_DOWN);//每个商品占符合满减的总额的比例
            skuMapPercent.put(keySkuId,divide);
            Integer type = mallGoodsCoupon.getType();
            if(MallGoodsCoupon.TYPE_ONE == type){
                BigDecimal bigDecimal = realAmount.multiply(divide).setScale(2, BigDecimal.ROUND_DOWN);//每个SKU的减免金额(比例*减免金额)
                BigDecimal skuRealAmount = valueSkuAmount.subtract(bigDecimal.compareTo(BigDecimal.ZERO) > 0 ? bigDecimal : BigDecimal.ZERO);//实际支付金额
                couponAmountMap.put(keySkuId,skuRealAmount);
            }
            if(MallGoodsCoupon.TYPE_TWO == type){
                BigDecimal divideTime = totalAmount.divide(costAmount, 0, BigDecimal.ROUND_DOWN);//累计减免次数
                BigDecimal multiply = divideTime.multiply(realAmount);//实际减免金额
                BigDecimal bigDecimal = multiply.multiply(divide).setScale(2, BigDecimal.ROUND_DOWN);//每个SKU的减免金额(比例*实际减免金额)
                BigDecimal skuRealAmount = valueSkuAmount.subtract(bigDecimal.compareTo(BigDecimal.ZERO) > 0 ? bigDecimal : BigDecimal.ZERO);//实际支付金额
                couponAmountMap.put(keySkuId,skuRealAmount);
            }
        }
        return couponAmountMap;
    }
    public static void main(String[] args) {
        BigDecimal bigDecimal = new BigDecimal(980);
        BigDecimal divide = bigDecimal.divide(new BigDecimal(100), 0, BigDecimal.ROUND_DOWN);
        System.out.println(divide);
        List<Integer> a = new ArrayList<>();
        a.add(1);
        a.add(2);
        a.add(3);
        List<Integer> b = new ArrayList<>();
        b.add(3);
        b.add(4);
        b.add(5);
        boolean b1 = a.retainAll(b);
        System.out.println(b1);
        System.out.println(a);
        System.out.println(b);
        List<Integer> intersection = a.stream()
                .filter(item -> b.contains(item))
                .collect(Collectors.toList());
        System.out.println(intersection);
    }