handle out of time

This commit is contained in:
Cauchy
2023-10-10 16:24:03 +08:00
parent 6ab08f7a29
commit f3df8b326b
14 changed files with 636 additions and 495 deletions

View File

@@ -1,26 +1,29 @@
package com.peanut.modules.book.controller;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.peanut.modules.book.entity.*;
import com.peanut.modules.book.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.peanut.common.utils.PageUtils;
import com.peanut.common.utils.R;
import com.peanut.config.Constants;
import com.peanut.config.DelayQueueConfig;
import com.peanut.modules.book.entity.*;
import com.peanut.modules.book.service.*;
import com.peanut.modules.pay.weChatPay.dto.WechatPaymentInfo;
import com.peanut.modules.pay.weChatPay.service.WxpayService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;
/**
@@ -54,44 +57,10 @@ public class BuyOrderController {
private UserEbookBuyService userEbookBuyService;
@Autowired
private UserRecordService userRecordService;
/**
* 订单状态 - 待支付
*/
private static final String ORDER_STATUS_TO_BE_PAID = "0";
/**
* 订单状态 - 待发货
*/
private static final String ORDER_STATUS_TO_BE_SHIPPED = "1";
/**
* 订单状态 - 待收货
*/
private static final String ORDER_STATUS_TO_BE_RECEIVED = "2";
/**
* 支付方式 - 支付宝 - 支付宝支付
*/
private static final String PAYMENT_METHOD_ALI_PAY = "1";
/**
* 支付方式 - 微信支付
*/
private static final String PAYMENT_METHOD_WECHAT_PAY = "2";
/**
* 支付方式 - IOS 内购
*/
private static final String PAYMENT_METHOD_IOS = "3";
/**
* 支付方式 - 虚拟币支付
*/
private static final String PAYMENT_METHOD_VIRTUAL = "4";
/**
* 购买方式 - 直接购买
*/
private static final String BUY_TYPE_REDIRECT = "0";
/**
* 购买方式 - 购物车
*/
private static final String BUY_TYPE_CART = "1";
@Autowired
private WxpayService wxpayService;
@Autowired
private RabbitTemplate rabbitTemplate;
@Autowired
private ShopProudictBookService shopProudictBookService;
@@ -144,7 +113,7 @@ public class BuyOrderController {
*/
@RequestMapping("/buySave")
@Transactional
public R buySave(@RequestBody BuyOrderEntity buyOrder) {
public R buySave(@RequestBody BuyOrderEntity buyOrder) throws IOException {
// 获取订单详情
List<BuyOrderDetailEntity> products = buyOrder.getProducts();
// 订单总金额
@@ -163,7 +132,7 @@ public class BuyOrderController {
buyOrderDetail.setProductPrice(product.getPrice());
buyOrderDetail.setAddressId(buyOrder.getAddressId());
buyOrderDetail.setProductUrl(product.getProductImages());
buyOrderDetail.setOrderStatus(ORDER_STATUS_TO_BE_PAID);
buyOrderDetail.setOrderStatus(Constants.ORDER_STATUS_TO_BE_PAID);
}
totalPrice = totalPrice.subtract(useCouponAmount(buyOrder));
@@ -176,21 +145,38 @@ public class BuyOrderController {
for (BuyOrderDetailEntity buyOrderDetail : products) {
buyOrderDetail.setOrderId(buyOrder.getOrderId());
buyOrderDetail.setUserId(buyOrder.getUserId());
if (BUY_TYPE_CART.equals(buyOrder.getBuyType())) {
if (Constants.BUY_TYPE_CART.equals(buyOrder.getBuyType())) {
handleBuyCart(buyOrder, buyOrderDetail);
}
}
buyOrderDetailService.saveBatch(products);
if (PAYMENT_METHOD_VIRTUAL.equals(buyOrder.getPaymentMethod())) {
buyOrder.setOrderStatus(ORDER_STATUS_TO_BE_SHIPPED);
// 1. 虚拟币支付
if (Constants.PAYMENT_METHOD_VIRTUAL.equals(buyOrder.getPaymentMethod())) {
buyOrder.setOrderStatus(Constants.ORDER_STATUS_TO_BE_SHIPPED);
MyUserEntity user = this.myUserService.getById(buyOrder.getUserId());
if (usePeanutCoin(user, totalPrice)) {
// 更新订单状态
buyOrderService.updateOrderStatus(user.getId(), buyOrder.getOrderSn(), "0");
recordTransaction(buyOrder, user, totalPrice);
addEbookToUser(products, buyOrder);
} else {
return R.error(500, "花生币余额不足!");
}
}
// 2. 微信支付
if (Constants.PAYMENT_METHOD_WECHAT_PAY.equals(buyOrder.getPaymentMethod())) {
rabbitTemplate.convertAndSend(
DelayQueueConfig.ORDER_TO_BE_PAY_EXCHANGE,
DelayQueueConfig.ORDER_TO_BE_PAY_ROUTING_KEY,
buyOrder.getOrderId(),
messagePostProcessor()
);
WechatPaymentInfo paymentInfo = new WechatPaymentInfo();
paymentInfo.setOrderSn(orderSn);
paymentInfo.setBuyOrderId(buyOrder.getOrderId());
paymentInfo.setTotalAmount(totalPrice);
wxpayService.prepay(paymentInfo);
}
Map<String, Object> result = new HashMap<>();
result.put("orderSn", buyOrder.getOrderSn());
result.put("money", totalPrice);
@@ -557,4 +543,13 @@ public class BuyOrderController {
orderCartService.removeByIds(collect);
}
}
private MessagePostProcessor messagePostProcessor() {
return message -> {
//设置有效期30分钟
//message.getMessageProperties().setExpiration("1800000");
message.getMessageProperties().setExpiration("30000");
return message;
};
}
}