| | |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | |
| | | import java.math.BigDecimal; |
| | | import java.math.RoundingMode; |
| | | import java.util.*; |
| | | import java.util.stream.Collectors; |
| | | |
| | |
| | | private final CouponGoodsMapper couponGoodsMapper; |
| | | |
| | | private final IMallVipConfigService mallVipConfigService; |
| | | private final MallActivityMapper mallActivityMapper; |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public Long createOrder(AddOrderDto addOrderDto) { |
| | | Long memberId = LoginUserUtil.getLoginUser().getId(); |
| | | MallMember member = mallMemberMapper.selectById(memberId); |
| | | |
| | | //验证用户的等级 |
| | | // if(AgentLevelEnum.ZERO_LEVEL.name().equals(member.getLevel())){ |
| | | // throw new FebsException("请先申请成为代理人"); |
| | | // } |
| | | MallMember member = ValidateEntityUtils |
| | | .ensureColumnReturnEntity(memberId, MallMember::getId, mallMemberMapper::selectOne, "用户不存在"); |
| | | |
| | | //订单范围内才允许下单 |
| | | MallAddressInfo address = mallAddressInfoMapper.selectAddressInfoByMemberIdAndId(member.getId(), addOrderDto.getAddressId()); |
| | | if(ObjectUtil.isEmpty(address)){ |
| | | throw new FebsException("请重新选择收货地址信息"); |
| | | } |
| | | MallAddressInfo address = ValidateEntityUtils |
| | | .ensureColumnReturnEntity(addOrderDto.getAddressId(), MallAddressInfo::getId, mallAddressInfoMapper::selectOne, "收货地址信息不存在"); |
| | | |
| | | String orderNo = MallUtils.getOrderNum(); |
| | | MallOrderInfo orderInfo = new MallOrderInfo(); |
| | |
| | | throw new FebsException("购买商品或sku不存在"); |
| | | } |
| | | |
| | | // if("样品".equals(sku.getSample())){ |
| | | // List<MallOrderItem> items = mallOrderItemMapper.selectItemBySkuIdUnCancel(sku.getId(), member.getId()); |
| | | // if (CollUtil.isNotEmpty(items)) { |
| | | // throw new FebsException("样品一个用户只能购买一次"); |
| | | // } |
| | | // Integer cnt = item.getCnt(); |
| | | // if(1 < cnt){ |
| | | // throw new FebsException("样品只能购买一件"); |
| | | // } |
| | | // |
| | | // } |
| | | |
| | | if (sku.getStock() < item.getCnt()) { |
| | | throw new FebsException(sku.getSkuName() + "库存不足"); |
| | | } |
| | |
| | | total = total.add(amount); |
| | | //规格的库存销量 |
| | | Integer skuResult = mallGoodsSkuMapper.upDateStockAndVolumeBySkuId(sku.getId(),item.getCnt()); |
| | | if(1 != skuResult){ |
| | | throw new FebsException(sku.getSkuName() + "库存不足"); |
| | | } |
| | | ValidateEntityUtils.ensureEqual(1,skuResult,sku.getSkuName() + "库存不足"); |
| | | if (addOrderDto.getType() == 1) { |
| | | mallShoppingCartMapper.delBySkuId(sku.getId(), member.getId()); |
| | | } |
| | |
| | | * @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 |
| | | public Map<Long, BigDecimal> getCouponAmountMap(Long memberCouponId, List<AddOrderItemDto> items) { |
| | | HashMap<Long, BigDecimal> couponAmountMap = new HashMap<>(); |
| | | // 获取优惠券信息 |
| | | Optional<MallMemberCoupon> optionalMallMemberCoupon = Optional.ofNullable(mallMemberCouponMapper.selectById(memberCouponId)); |
| | | MallMemberCoupon mallMemberCoupon = optionalMallMemberCoupon.orElse(null); |
| | | |
| | | /** |
| | | * 获取这张优惠卷生效的skuIds |
| | | */ |
| | | Set<Long> intersection = skuIdsAll.stream() |
| | | .filter(item -> skuIds.contains(item)) |
| | | .collect(Collectors.toSet());//获取两个集合的交集 |
| | | if(CollUtil.isEmpty(intersection)){ |
| | | if (mallMemberCoupon == null || mallMemberCoupon.getState() != 1) { |
| | | 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); |
| | | |
| | | Set<Long> intersection = items.stream().map(AddOrderItemDto::getSkuId).collect(Collectors.toSet()); // 订单中的全部skuIds |
| | | |
| | | if (intersection.isEmpty()) { |
| | | return couponAmountMap; |
| | | } |
| | | for(MallGoodsSku mallGoodsSku : skus){ |
| | | |
| | | // 获取订单详情的ID和amount的map |
| | | Map<Long, AddOrderItemDto> itemMap = items.stream() |
| | | .collect(Collectors.toMap(AddOrderItemDto::getSkuId, item -> item)); |
| | | |
| | | List<MallGoodsSku> skus = mallGoodsSkuMapper.selectList( |
| | | new LambdaQueryWrapper<MallGoodsSku>() |
| | | .select(MallGoodsSku::getId, MallGoodsSku::getPresentPrice) |
| | | .in(MallGoodsSku::getId, intersection) |
| | | ); |
| | | BigDecimal totalAmount = BigDecimal.ZERO; |
| | | Map<Long, BigDecimal> skuMap = new HashMap<>(); |
| | | |
| | | 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); |
| | | skuMap.put(id, skuAmount); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 计算每个商品利用优惠卷打折的具体金额 |
| | | */ |
| | | MallGoodsCoupon mallGoodsCoupon = mallGoodsCouponMapper.selectById(mallMemberCoupon.getCouponId()); |
| | | BigDecimal costAmount = mallGoodsCoupon.getCostAmount(); |
| | | BigDecimal realAmount = mallGoodsCoupon.getRealAmount(); |
| | | if(totalAmount.compareTo(costAmount) < 0){ |
| | | // 计算每个商品利用优惠卷打折的具体金额 |
| | | Optional<MallGoodsCoupon> optionalMallGoodsCoupon = Optional.ofNullable(mallGoodsCouponMapper.selectById(mallMemberCoupon.getCouponId())); |
| | | MallGoodsCoupon mallGoodsCoupon = optionalMallGoodsCoupon.orElse(null); |
| | | if (mallGoodsCoupon == null) { |
| | | return couponAmountMap; |
| | | } |
| | | HashMap<Long,BigDecimal> skuMapPercent = new HashMap<>();//每个订单详情的ID和amount的map |
| | | for (Map.Entry<Long,BigDecimal> entry : skuMap.entrySet()) { |
| | | |
| | | BigDecimal costAmount = mallGoodsCoupon.getCostAmount(); |
| | | BigDecimal realAmount = mallGoodsCoupon.getRealAmount(); |
| | | Integer type = mallGoodsCoupon.getType(); |
| | | if (MallGoodsCoupon.TYPE_ONE == type) { |
| | | realAmount = mallGoodsCoupon.getRealAmount(); |
| | | } else if (MallGoodsCoupon.TYPE_TWO == type) { |
| | | BigDecimal divideTime = totalAmount.divide(costAmount, 0, RoundingMode.DOWN); // 累计减免次数 |
| | | realAmount = divideTime.multiply(realAmount); // 实际减免金额 |
| | | } |
| | | |
| | | if (totalAmount.compareTo(costAmount) < 0) { |
| | | return couponAmountMap; |
| | | } |
| | | |
| | | BigDecimal totalDiscount = BigDecimal.ZERO; |
| | | 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(0, BigDecimal.ROUND_DOWN);//每个SKU的减免金额(比例*减免金额) |
| | | BigDecimal skuRealAmount = valueSkuAmount.subtract(bigDecimal.compareTo(BigDecimal.ZERO) > 0 ? bigDecimal : BigDecimal.ZERO);//实际支付金额 |
| | | couponAmountMap.put(keySkuId,skuRealAmount); |
| | | BigDecimal divide = valueSkuAmount.divide(totalAmount, 4, RoundingMode.DOWN); // 每个商品占符合满减的总额的比例 |
| | | BigDecimal skuRealAmount = BigDecimal.ZERO; |
| | | if (MallGoodsCoupon.TYPE_ONE == type) { |
| | | BigDecimal bigDecimalOne = realAmount.multiply(divide).setScale(2, RoundingMode.DOWN); // 每个SKU的减免金额(比例*减免金额) |
| | | skuRealAmount = valueSkuAmount.subtract(bigDecimalOne.max(BigDecimal.ZERO)).setScale(2, RoundingMode.DOWN); |
| | | totalDiscount = totalDiscount.add(bigDecimalOne); |
| | | } else if (MallGoodsCoupon.TYPE_TWO == type) { |
| | | BigDecimal divideTime = totalAmount.divide(costAmount, 0, RoundingMode.DOWN); // 累计减免次数 |
| | | BigDecimal multiply = divideTime.multiply(realAmount); // 实际减免金额 |
| | | BigDecimal bigDecimalTwo = multiply.multiply(divide).setScale(2, RoundingMode.DOWN); // 每个SKU的减免金额(比例*实际减免金额) |
| | | skuRealAmount = valueSkuAmount.subtract(bigDecimalTwo.max(BigDecimal.ZERO)).setScale(2, RoundingMode.DOWN); |
| | | totalDiscount = totalDiscount.add(bigDecimalTwo); |
| | | } |
| | | 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(0, BigDecimal.ROUND_DOWN);//每个SKU的减免金额(比例*实际减免金额) |
| | | BigDecimal skuRealAmount = valueSkuAmount.subtract(bigDecimal.compareTo(BigDecimal.ZERO) > 0 ? bigDecimal : BigDecimal.ZERO);//实际支付金额 |
| | | couponAmountMap.put(keySkuId,skuRealAmount); |
| | | couponAmountMap.put(keySkuId, skuRealAmount); |
| | | } |
| | | |
| | | // 调整最后一个商品的减免金额以确保总减免金额达到预期 |
| | | if (totalDiscount.compareTo(realAmount) < 0) { |
| | | BigDecimal difference = realAmount.subtract(totalDiscount); |
| | | for (Map.Entry<Long, BigDecimal> entry : couponAmountMap.entrySet()) { |
| | | Long keySkuId = entry.getKey(); |
| | | BigDecimal skuRealAmount = entry.getValue(); |
| | | BigDecimal newSkuRealAmount = skuRealAmount.subtract(difference).setScale(2, RoundingMode.DOWN); |
| | | couponAmountMap.put(keySkuId, newSkuRealAmount); |
| | | break; // 调整最后一个商品的减免金额 |
| | | } |
| | | } |
| | | |
| | | return couponAmountMap; |
| | | } |
| | | |
| | | |
| | | public static void main(String[] args) { |
| | | List<Integer> a = new ArrayList<>(); |
| | |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public Map<String, Object> payOrder(PayOrderDto payOrderDto) { |
| | | MallMember member = LoginUserUtil.getLoginUser(); |
| | | MallOrderInfo orderInfo = this.baseMapper.selectOrderByMemberIdAndId(member.getId(), payOrderDto.getId()); |
| | | if (orderInfo == null || AppContants.DEL_FLAG_Y == orderInfo.getDelFlag()) { |
| | | throw new FebsException("订单不存在"); |
| | | } |
| | | |
| | | if (OrderStatusEnum.WAIT_PAY.getValue() != orderInfo.getStatus()) { |
| | | throw new FebsException("订单状态不能支付"); |
| | | } |
| | | MallOrderInfo orderInfo = ValidateEntityUtils |
| | | .ensureColumnReturnEntity(payOrderDto.getId(), MallOrderInfo::getId, this.baseMapper::selectOne, "订单不存在"); |
| | | ValidateEntityUtils.ensureEqual(member.getId(),orderInfo.getMemberId(),"订单不存在"); |
| | | ValidateEntityUtils.ensureEqual(OrderStatusEnum.WAIT_PAY.getValue(),orderInfo.getStatus(),"订单状态不能支付"); |
| | | |
| | | String payResultStr = ""; |
| | | String wxResultStr = ""; |
| | |
| | | @Transactional |
| | | public FebsResponse refundOrder(Long id) { |
| | | MallMember member = LoginUserUtil.getLoginUser(); |
| | | MallOrderInfo mallOrderInfo = this.baseMapper.selectById(id); |
| | | if(ObjectUtil.isEmpty(mallOrderInfo)){ |
| | | return new FebsResponse().fail().message("订单不存在"); |
| | | } |
| | | MallOrderInfo mallOrderInfo = ValidateEntityUtils |
| | | .ensureColumnReturnEntity(id, MallOrderInfo::getId, this.baseMapper::selectOne, "订单不存在"); |
| | | Integer status = mallOrderInfo.getStatus(); |
| | | if(OrderStatusEnum.WAIT_SHIPPING.getValue() != status){ |
| | | return new FebsResponse().fail().message("订单不是待发货状态"); |
| | | } |
| | | Integer deliveryState = mallOrderInfo.getDeliveryState(); |
| | | if(1 != deliveryState){ |
| | | return new FebsResponse().fail().message("订单不是待配送状态"); |
| | | } |
| | | |
| | | ValidateEntityUtils.ensureEqual(OrderStatusEnum.WAIT_SHIPPING.getValue(),status,"订单不是待发货状态"); |
| | | ValidateEntityUtils.ensureEqual(1,deliveryState,"订单不是待配送状态"); |
| | | //根据子订单生成退款记录 |
| | | List<MallOrderItem> mallOrderItemList = mallOrderItemMapper.selectListByOrderId(id); |
| | | if(CollUtil.isEmpty(mallOrderItemList)){ |
| | | return new FebsResponse().fail().message("订单不存在"); |
| | | } |
| | | List<MallOrderItem> mallOrderItemList = ValidateEntityUtils |
| | | .ensureColumnReturnEntityList(id, MallOrderItem::getOrderId, mallOrderItemMapper::selectList, "订单不存在"); |
| | | for(MallOrderItem mallOrderItem : mallOrderItemList){ |
| | | QueryWrapper<MallRefundEntity> objectQueryWrapper = new QueryWrapper<>(); |
| | | objectQueryWrapper.eq("member_id",member.getId()); |
| | |
| | | mallRefundEntity.setItemId(mallOrderItem.getId()); |
| | | if("余额支付".equals(mallOrderInfo.getPayMethod())){ |
| | | mallRefundEntity.setType(3); |
| | | }else if("微信支付".equals(mallOrderInfo.getPayMethod())){ |
| | | }else if("FIUU支付".equals(mallOrderInfo.getPayMethod())){ |
| | | mallRefundEntity.setType(1); |
| | | }else{ |
| | | mallRefundEntity.setType(3); |
| | |
| | | mallRefundEntity.setAmount(mallOrderItem.getAmount()); |
| | | mallRefundMapper.insert(mallRefundEntity); |
| | | }else{ |
| | | if(mallRefund.getState() == 1){ |
| | | return new FebsResponse().fail().message("订单已退款"); |
| | | } |
| | | ValidateEntityUtils.ensureNotEqual(1,mallRefund.getState(),"订单已退款"); |
| | | if(mallRefund.getState() == 2 || mallRefund.getState() == 3){ |
| | | mallRefundEntity.setId(mallRefund.getId()); |
| | | mallRefundEntity.setRefundNo(mallRefund.getRefundNo()); |
| | |
| | | if (sku == null) { |
| | | return new FebsResponse().fail().message("购买商品或sku不存在"); |
| | | } |
| | | // if("样品".equals(sku.getSample())){ |
| | | // List<MallOrderItem> mallOrderItems = mallOrderItemMapper.selectItemBySkuIdUnCancel(sku.getId(), member.getId()); |
| | | // if (CollUtil.isNotEmpty(mallOrderItems)) { |
| | | // return new FebsResponse().fail().message("样品只能购买一次"); |
| | | // } |
| | | // Integer cnt = item.getCnt(); |
| | | // if(1 < cnt){ |
| | | // return new FebsResponse().fail().message("样品只能购买一件"); |
| | | // } |
| | | // }else{ |
| | | // //验证用户的等级 |
| | | // if(AgentLevelEnum.ZERO_LEVEL.name().equals(member.getLevel())){ |
| | | // return new FebsResponse().fail().message("请先申请成为合伙人"); |
| | | // } |
| | | // } |
| | | |
| | | if (sku.getStock() < item.getCnt()) { |
| | | return new FebsResponse().fail().message(sku.getSkuName() + "库存不足"); |
| | | } |
| | |
| | | if (MallGoods.ISSALE_STATUS_DISABLED.equals(mallGoods.getIsSale())) { |
| | | return new FebsResponse().fail().message(mallGoods.getGoodsName() + "已下架"); |
| | | } |
| | | if(GoodsTypeEnum.HUO_DONG.getValue() == mallGoods.getIsNormal()){ |
| | | //活动商品判断是否在活动进行中 |
| | | MallActivity mallActivity = mallActivityMapper.selectById(mallGoods.getActivityId()); |
| | | if(mallActivity == null || mallActivity.getState() != 2){ |
| | | return new FebsResponse().fail().message("活动不在进行中"); |
| | | } |
| | | } |
| | | |
| | | } |
| | | |
| | | return new FebsResponse().success(); |