修改下单时记录用户虚拟币、积分消费操作

This commit is contained in:
wuchunlei
2024-12-04 10:35:14 +08:00
parent 86cecf3b6a
commit 76730d890d
8 changed files with 86 additions and 102 deletions

View File

@@ -5,20 +5,24 @@ import com.github.promeg.pinyinhelper.Pinyin;
import com.peanut.common.utils.R;
import com.peanut.common.utils.ShiroUtils;
import com.peanut.config.Constants;
import com.peanut.config.DelayQueueConfig;
import com.peanut.modules.book.service.BuyOrderService;
import com.peanut.modules.book.service.TransactionDetailsService;
import com.peanut.modules.book.service.UserEbookBuyService;
import com.peanut.modules.common.entity.BookEntity;
import com.peanut.modules.common.entity.BuyOrder;
import com.peanut.modules.common.entity.MyUserEntity;
import com.peanut.modules.common.service.BookService;
import com.peanut.modules.common.service.MyUserService;
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.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
@Slf4j
@RestController("bookAbroadOrder")
@@ -28,22 +32,27 @@ public class OrderController {
@Autowired
private BookService bookService;
@Autowired
private MyUserService myUserService;
@Autowired
private BuyOrderService buyOrderService;
@Autowired
private RabbitTemplate rabbitTemplate;
private TransactionDetailsService transactionDetailsService;
@Autowired
private UserEbookBuyService userEbookBuyService;
//海外读书下单
@RequestMapping("/placeOrder")
@Transactional
public R createOrder(@RequestBody BuyOrder buyOrder){
buyOrder.setUserId(ShiroUtils.getUId());
buyOrder.setCome(3);//3 海外读书
buyOrder.setPaymentMethod("5"); //5 paypal
BookEntity bookEntity = bookService.getById(buyOrder.getAbroadBookId());
BigDecimal orderMoney = buyOrder.getOrderMoney();
BigDecimal totalPrice = buyOrder.getOrderMoney();
//校验价格
if (orderMoney.compareTo(bookEntity.getAbroadPrice())!=0){
if (totalPrice.compareTo(bookEntity.getAbroadPrice())!=0){
return R.error("订单价格异常");
}
buyOrder.setRealMoney(orderMoney);
buyOrder.setRealMoney(totalPrice);
buyOrder.setRemark("Purchase the e-book '"+ Pinyin.toPinyin(bookEntity.getName(), " ").toLowerCase()+"'");
buyOrder.setOrderStatus("0");
buyOrder.setOrderType("abroadBook");
@@ -52,22 +61,25 @@ public class OrderController {
buyOrderService.save(buyOrder);
// 1. 虚拟币支付
if (Constants.PAYMENT_METHOD_VIRTUAL.equals(buyOrder.getPaymentMethod())) {
MyUserEntity user = ShiroUtils.getUser();
if (user.getPeanutCoin().compareTo(totalPrice)>=0){
//更新虚拟币
user.setPeanutCoin(user.getPeanutCoin().subtract(totalPrice));
myUserService.updateById(user);
//更新订单状态
buyOrderService.updateOrderStatus(user.getId(), buyOrder.getOrderSn(), "2");
//记录用户虚拟币消费
transactionDetailsService.recordTransaction(buyOrder, user, totalPrice);
//开通电子书权限
userEbookBuyService.addBookForUser(user.getId(), Arrays.asList(buyOrder.getAbroadBookId()));
}else {
return R.error(500, "天医币余额不足!");
}
}
rabbitTemplate.convertAndSend(
DelayQueueConfig.ORDER_TO_BE_PAY_EXCHANGE,
DelayQueueConfig.ORDER_TO_BE_PAY_ROUTING_KEY,
buyOrder.getOrderId(),
messagePostProcessor()
);
return R.ok();
}
private MessagePostProcessor messagePostProcessor() {
return message -> {
//设置有效期30分钟
message.getMessageProperties().setExpiration(String.valueOf(30 * 60 * 1000));
return message;
};
Map<String, Object> result = new HashMap<>();
result.put("orderSn", buyOrder.getOrderSn());
result.put("money", totalPrice);
return R.ok(result);
}
}