|
|
@@ -4,6 +4,7 @@ import com.jfinal.plugin.activerecord.Db;
|
|
|
import com.jfinal.plugin.activerecord.Record;
|
|
|
import common.model.Order;
|
|
|
import common.model.OrderLog;
|
|
|
+import common.model.User;
|
|
|
import common.utils.http.MyRet;
|
|
|
|
|
|
import java.math.BigDecimal;
|
|
|
@@ -25,14 +26,24 @@ public class OrderService {
|
|
|
Db.tx(() -> {
|
|
|
// 1. SELECT ... FOR UPDATE: 查询商品库存,并对该商品行加排他锁
|
|
|
Record item = Db.findFirst("SELECT purchased_quantity, max_quantity, price FROM t_nftt WHERE id = ? FOR UPDATE", nfttId);
|
|
|
-
|
|
|
+
|
|
|
if (item == null) {
|
|
|
- throw new RuntimeException("商品不存在: " + nfttId); // 抛出异常触发回滚
|
|
|
+ throw new RuntimeException("商品不存在: " + nfttId);
|
|
|
}
|
|
|
|
|
|
int stock = item.getInt("max_quantity") - item.getInt("purchased_quantity");
|
|
|
if (stock <= 0) {
|
|
|
- throw new RuntimeException("库存不足"); // 抛出异常触发回滚
|
|
|
+ throw new RuntimeException("库存不足");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 判断用户余额并预扣
|
|
|
+ User user = User.dao.findById(userId);
|
|
|
+ if (user.getBalance() < item.getInt("price")) {
|
|
|
+ throw new RuntimeException("花火余额不足,请充值。需要: " + item.getInt("price") + ",你有:" + user.getBalance());
|
|
|
+ }
|
|
|
+ user.setBalance(user.getBalance() - item.getInt("price"));
|
|
|
+ if (!user.update()) {
|
|
|
+ throw new RuntimeException("创建信息时,用户信息更新失败 user id: " + user.getId());
|
|
|
}
|
|
|
|
|
|
// 2. 更新库存(在锁定的行上进行)
|
|
|
@@ -144,6 +155,8 @@ public class OrderService {
|
|
|
if (orderList.isEmpty()) {
|
|
|
return MyRet.ok("查询成功").setData(new ArrayList<Map<String, Object>>());
|
|
|
}
|
|
|
+
|
|
|
+ // TODO 这里对未划转NFT的订单进行NFT划转
|
|
|
|
|
|
// 调用重用方法封装日志
|
|
|
List<Map<String, Object>> resultList = encapsulateOrdersWithLogs(orderList);
|