Преглед изворни кода

购买时对用户余额判断

skyfffire пре 1 месец
родитељ
комит
0f66307a0f

+ 5 - 5
src/main/java/common/model/base/BaseNftt.java

@@ -35,19 +35,19 @@ public abstract class BaseNftt<M extends BaseNftt<M>> extends Model<M> implement
 		return getStr("name");
 	}
 	/**
-	 * 售价()
+	 * 售价(火花)
 
 	 */
-	public void setPrice(java.math.BigDecimal price) {
+	public void setPrice(java.lang.Integer price) {
 		set("price", price);
 	}
 	
 	/**
-	 * 售价()
+	 * 售价(火花)
 
 	 */
-	public java.math.BigDecimal getPrice() {
-		return getBigDecimal("price");
+	public java.lang.Integer getPrice() {
+		return getInt("price");
 	}
 	/**
 	 * 开启预售时间

+ 2 - 2
src/main/java/modules/nftt/NfttController.java

@@ -66,14 +66,14 @@ public class NfttController extends MyController {
         try {
             // ** price (decimal) **
             // JSON 可能是 Double、Integer 或 String,统一转为 BigDecimal
-            BigDecimal price = requestBodyJson.getBigDecimal("price");
+            Integer price = requestBodyJson.getInteger("price");
             if (price == null) {
                 // 虽然 EmptyInterface 确保了不为空字符串,但可能为 null
                 renderJson(MyRet.fail("售价(price)不能为空或格式不正确"));
                 return;
             }
             // 可以添加额外的业务校验,例如 price 必须大于 0
-            if (price.compareTo(BigDecimal.ZERO) < 0) {
+            if (price < 0) {
                 renderJson(MyRet.fail("售价(price)不能为负数"));
                 return;
             }

+ 16 - 3
src/main/java/modules/order/OrderService.java

@@ -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);

+ 1 - 1
src/test/rest/NfttControllerTest.http

@@ -21,7 +21,7 @@ Content-Type: application/json
 dl-token: {{dl_token_var}}
 
 {
-  "price": 0.99,
+  "price": 99,
   "name": "张三丰真迹",
   "presale_start_time": 1756797150932,
   "presale_end_time": 1756797150932,