| | |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public Long createOrder(AddOrderDto addOrderDto) { |
| | | Long memberId = LoginUserUtil.getLoginUser().getId(); |
| | | MallMember member = mallMemberMapper.selectById(memberId); |
| | | 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<>(); |
| | | 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); |
| | |
| | | return couponAmountMap; |
| | | } |
| | | |
| | | Set<Long> intersection = items.stream().map(AddOrderItemDto::getSkuId).collect(Collectors.toSet());//订单中的全部skuIds |
| | | Set<Long> intersection = items.stream().map(AddOrderItemDto::getSkuId).collect(Collectors.toSet()); // 订单中的全部skuIds |
| | | |
| | | if (intersection.isEmpty()) { |
| | | return couponAmountMap; |
| | |
| | | |
| | | 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; |
| | | } |
| | | for (Map.Entry<Long,BigDecimal> entry : skuMap.entrySet()) { |
| | | |
| | | 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, RoundingMode.DOWN);//每个商品占符合满减的总额的比例 |
| | | Integer type = mallGoodsCoupon.getType(); |
| | | if(MallGoodsCoupon.TYPE_ONE == type){ |
| | | BigDecimal bigDecimalOne = realAmount.multiply(divide).setScale(2, RoundingMode.DOWN);//每个SKU的减免金额(比例*减免金额) |
| | | BigDecimal skuRealAmountOne = valueSkuAmount.subtract(bigDecimalOne.max(BigDecimal.ZERO));//实际支付金额 |
| | | couponAmountMap.put(keySkuId, skuRealAmountOne); |
| | | continue; |
| | | 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, RoundingMode.DOWN);//累计减免次数 |
| | | BigDecimal multiply = divideTime.multiply(realAmount);//实际减免金额 |
| | | BigDecimal bigDecimalTwo = multiply.multiply(divide).setScale(2, RoundingMode.DOWN);//每个SKU的减免金额(比例*实际减免金额) |
| | | BigDecimal skuRealAmountTwo = valueSkuAmount.subtract(bigDecimalTwo.max(BigDecimal.ZERO));//实际支付金额 |
| | | couponAmountMap.put(keySkuId, skuRealAmountTwo); |
| | | continue; |
| | | 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()); |