|
@@ -0,0 +1,142 @@
|
|
|
|
|
+package modules.nftt;
|
|
|
|
|
+
|
|
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
|
|
+import com.jfinal.aop.Before;
|
|
|
|
|
+import com.jfinal.aop.Inject;
|
|
|
|
|
+import com.jfinal.kit.StrKit;
|
|
|
|
|
+import common.interceptor.LoginInterceptor;
|
|
|
|
|
+import common.interceptor.empty.EmptyInterface;
|
|
|
|
|
+import common.interceptor.role.RequiredRoleInterface;
|
|
|
|
|
+import common.model.Nftt;
|
|
|
|
|
+import common.utils.http.MyController;
|
|
|
|
|
+import common.utils.http.MyRet;
|
|
|
|
|
+import modules.user.UserController;
|
|
|
|
|
+
|
|
|
|
|
+import java.math.BigDecimal;
|
|
|
|
|
+
|
|
|
|
|
+public class NfttController extends MyController {
|
|
|
|
|
+ @Inject
|
|
|
|
|
+ NfttService service;
|
|
|
|
|
+
|
|
|
|
|
+ public void hello() {
|
|
|
|
|
+ renderJson(MyRet.ok(service.hello()));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Before(LoginInterceptor.class)
|
|
|
|
|
+ @RequiredRoleInterface({UserController.ROLE_CHECK_ADMIN, UserController.ROLE_SUPER_ADMIN})
|
|
|
|
|
+ @EmptyInterface({"price", "name", "presale_start_time", "presale_end_time", "buying_start_time", "author", "max_quantity", "detail_image_list"})
|
|
|
|
|
+ public void create() {
|
|
|
|
|
+ JSONObject requestBodyJson = MyController.getJsonModelByRequestAndType(getRequest(), JSONObject.class);
|
|
|
|
|
+
|
|
|
|
|
+ Nftt nftt = new Nftt(); // 创建 Model 实例
|
|
|
|
|
+
|
|
|
|
|
+ // 获取当前用户ID作为 creater_id (需要从Session或LoginInterceptor中获取)
|
|
|
|
|
+ // 假设你在登录成功后将用户ID存储在Session中,键名为 "user_id"
|
|
|
|
|
+ String currentUserIdStr = getSessionAttr("user_id");
|
|
|
|
|
+ if (StrKit.isBlank(currentUserIdStr)) {
|
|
|
|
|
+ renderJson(MyRet.fail("创建失败: 未获取到用户信息,请重新登录"));
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ nftt.set("creater_id", new Integer(currentUserIdStr));
|
|
|
|
|
+
|
|
|
|
|
+ try {
|
|
|
|
|
+ // ** price (decimal) **
|
|
|
|
|
+ // JSON 可能是 Double、Integer 或 String,统一转为 BigDecimal
|
|
|
|
|
+ BigDecimal price = requestBodyJson.getBigDecimal("price");
|
|
|
|
|
+ if (price == null) {
|
|
|
|
|
+ // 虽然 EmptyInterface 确保了不为空字符串,但可能为 null
|
|
|
|
|
+ renderJson(MyRet.fail("售价(price)不能为空或格式不正确"));
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ // 可以添加额外的业务校验,例如 price 必须大于 0
|
|
|
|
|
+ if (price.compareTo(BigDecimal.ZERO) < 0) {
|
|
|
|
|
+ renderJson(MyRet.fail("售价(price)不能为负数"));
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ nftt.set("price", price);
|
|
|
|
|
+
|
|
|
|
|
+ // ** name (varchar) **
|
|
|
|
|
+ String name = requestBodyJson.getString("name");
|
|
|
|
|
+ nftt.set("name", name);
|
|
|
|
|
+
|
|
|
|
|
+ // ** presale_start_time (bigint) **
|
|
|
|
|
+ Long presaleStartTime = requestBodyJson.getLong("presale_start_time");
|
|
|
|
|
+ // 业务校验:时间戳必须是正数或非负数
|
|
|
|
|
+ if (presaleStartTime <= 0) {
|
|
|
|
|
+ renderJson(MyRet.fail("预售开始时间(presale_start_time)必须是有效的Unix时间戳"));
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ nftt.set("presale_start_time", presaleStartTime);
|
|
|
|
|
+
|
|
|
|
|
+ // ** presale_end_time (bigint) **
|
|
|
|
|
+ Long presaleEndTime = requestBodyJson.getLong("presale_end_time");
|
|
|
|
|
+ if (presaleEndTime <= 0) {
|
|
|
|
|
+ renderJson(MyRet.fail("预售结束时间(presale_end_time)必须是有效的Unix时间戳"));
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ nftt.set("presale_end_time", presaleEndTime);
|
|
|
|
|
+
|
|
|
|
|
+ // ** buying_start_time (bigint) **
|
|
|
|
|
+ Long buyingStartTime = requestBodyJson.getLong("buying_start_time");
|
|
|
|
|
+ if (buyingStartTime <= 0) {
|
|
|
|
|
+ renderJson(MyRet.fail("开放购买时间(buying_start_time)必须是有效的Unix时间戳"));
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ nftt.set("buying_start_time", buyingStartTime);
|
|
|
|
|
+
|
|
|
|
|
+ // ** author (varchar) **
|
|
|
|
|
+ String author = requestBodyJson.getString("author");
|
|
|
|
|
+ nftt.set("author", author);
|
|
|
|
|
+
|
|
|
|
|
+ // ** max_quantity (int) **
|
|
|
|
|
+ Integer maxQuantity = requestBodyJson.getInteger("max_quantity");
|
|
|
|
|
+ if (maxQuantity == null || maxQuantity <= 0) {
|
|
|
|
|
+ renderJson(MyRet.fail("最大份数(max_quantity)必须是大于0的整数"));
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ nftt.set("max_quantity", maxQuantity);
|
|
|
|
|
+
|
|
|
|
|
+ // ** detail_image_list (json) **
|
|
|
|
|
+ // 数据库字段是 JSON 类型,需要存储 JSON 字符串
|
|
|
|
|
+ // 获取 JSON 数组
|
|
|
|
|
+ JSONArray detailImageListArray = requestBodyJson.getJSONArray("detail_image_list");
|
|
|
|
|
+ if (detailImageListArray == null || detailImageListArray.isEmpty()) {
|
|
|
|
|
+ // EmptyInterface 应该已经保证了 non-null,但可能为空数组
|
|
|
|
|
+ // 如果图片列表不能为空,这里可以返回错误
|
|
|
|
|
+ renderJson(MyRet.fail("介绍图URL列表(detail_image_list)不能为空"));
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ // 转换为 JSON 字符串存储
|
|
|
|
|
+ nftt.set("detail_image_list", detailImageListArray.toJSONString());
|
|
|
|
|
+
|
|
|
|
|
+ // ** purchased_quantity (int) - 默认值 **
|
|
|
|
|
+ // 这是一个内部字段,通常在创建时初始化为 0
|
|
|
|
|
+ nftt.set("purchased_quantity", 0);
|
|
|
|
|
+
|
|
|
|
|
+ // ** crate_time, update_time (bigint) - 默认时间戳 **
|
|
|
|
|
+ long currentTimeMillis = System.currentTimeMillis();
|
|
|
|
|
+ nftt.set("create_time", currentTimeMillis);
|
|
|
|
|
+ nftt.set("update_time", currentTimeMillis);
|
|
|
|
|
+
|
|
|
|
|
+ // ** is_deleted (int) - 默认值 **
|
|
|
|
|
+ nftt.set("is_deleted", 0); // 0表示未删除
|
|
|
|
|
+
|
|
|
|
|
+ // 打印 Nftt 对象内部实际存储的属性,进行调试验证
|
|
|
|
|
+ System.out.println("Nftt Model 的内部数据 (for debug): " + nftt);
|
|
|
|
|
+
|
|
|
|
|
+ if (service.save(nftt)) {
|
|
|
|
|
+ renderJson(MyRet.ok("创建成功").setData(service.findNfttById(nftt.getId())));
|
|
|
|
|
+ } else {
|
|
|
|
|
+ renderJson(MyRet.fail("创建失败,原因未知,请将此日志提供给开发者" + nftt).setData(nftt));
|
|
|
|
|
+ }
|
|
|
|
|
+ } catch (NumberFormatException e) {
|
|
|
|
|
+ // 捕获可能发生的数字转换异常
|
|
|
|
|
+ renderJson(MyRet.fail("参数格式不正确,请检查数字或时间戳字段: " + e.getMessage()));
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ // 捕获其他未知异常
|
|
|
|
|
+ renderJson(MyRet.fail("创建失败:" + e.getMessage()));
|
|
|
|
|
+ e.printStackTrace(); // 打印堆栈跟踪,便于调试
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+}
|