NfttController.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. package modules.nftt;
  2. import com.alibaba.fastjson.JSONArray;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.jfinal.aop.Before;
  5. import com.jfinal.aop.Inject;
  6. import com.jfinal.kit.StrKit;
  7. import common.interceptor.LoginInterceptor;
  8. import common.interceptor.empty.EmptyInterface;
  9. import common.interceptor.role.RequiredRoleInterface;
  10. import common.model.Nftt;
  11. import common.utils.http.MyController;
  12. import common.utils.http.MyRet;
  13. import modules.user.UserController;
  14. import java.math.BigDecimal;
  15. import java.util.HashMap;
  16. import java.util.List;
  17. import java.util.Map;
  18. public class NfttController extends MyController {
  19. @Inject
  20. NfttService service;
  21. public void hello() {
  22. renderJson(MyRet.ok(service.hello()));
  23. }
  24. @Before(LoginInterceptor.class)
  25. @EmptyInterface({"id"})
  26. public void findById() {
  27. JSONObject requestBodyJson = MyController.getJsonModelByRequestAndType(getRequest(), JSONObject.class);
  28. String id = requestBodyJson.getString("id");
  29. Nftt nftt = service.findNfttById(id);
  30. if (nftt != null) {
  31. renderJson(MyRet.ok("获取成功").setData(nftt));
  32. } else {
  33. renderJson(MyRet.fail("获取失败,非法请求可能会导致ip封禁!" + id));
  34. }
  35. }
  36. @Before(LoginInterceptor.class)
  37. @RequiredRoleInterface({UserController.ROLE_CHECK_ADMIN, UserController.ROLE_SUPER_ADMIN})
  38. @EmptyInterface({"price", "name", "presale_start_time", "presale_end_time", "buying_start_time", "author", "max_quantity", "detail_image_list"})
  39. public void create() {
  40. JSONObject requestBodyJson = MyController.getJsonModelByRequestAndType(getRequest(), JSONObject.class);
  41. Nftt nftt = new Nftt(); // 创建 Model 实例
  42. // 获取当前用户ID作为 creator_id (需要从Session或LoginInterceptor中获取)
  43. // 假设你在登录成功后将用户ID存储在Session中,键名为 "user_id"
  44. String currentUserIdStr = getSessionAttr("user_id");
  45. if (StrKit.isBlank(currentUserIdStr)) {
  46. renderJson(MyRet.fail("创建失败: 未获取到用户信息,请重新登录"));
  47. return;
  48. }
  49. nftt.set("creator_id", new Integer(currentUserIdStr));
  50. try {
  51. // ** price (decimal) **
  52. // JSON 可能是 Double、Integer 或 String,统一转为 BigDecimal
  53. BigDecimal price = requestBodyJson.getBigDecimal("price");
  54. if (price == null) {
  55. // 虽然 EmptyInterface 确保了不为空字符串,但可能为 null
  56. renderJson(MyRet.fail("售价(price)不能为空或格式不正确"));
  57. return;
  58. }
  59. // 可以添加额外的业务校验,例如 price 必须大于 0
  60. if (price.compareTo(BigDecimal.ZERO) < 0) {
  61. renderJson(MyRet.fail("售价(price)不能为负数"));
  62. return;
  63. }
  64. nftt.set("price", price);
  65. // ** name (varchar) **
  66. String name = requestBodyJson.getString("name");
  67. nftt.set("name", name);
  68. // ** presale_start_time (bigint) **
  69. Long presaleStartTime = requestBodyJson.getLong("presale_start_time");
  70. // 业务校验:时间戳必须是正数或非负数
  71. if (presaleStartTime <= 0) {
  72. renderJson(MyRet.fail("预售开始时间(presale_start_time)必须是有效的Unix时间戳"));
  73. return;
  74. }
  75. nftt.set("presale_start_time", presaleStartTime);
  76. // ** presale_end_time (bigint) **
  77. Long presaleEndTime = requestBodyJson.getLong("presale_end_time");
  78. if (presaleEndTime <= 0) {
  79. renderJson(MyRet.fail("预售结束时间(presale_end_time)必须是有效的Unix时间戳"));
  80. return;
  81. }
  82. nftt.set("presale_end_time", presaleEndTime);
  83. // ** buying_start_time (bigint) **
  84. Long buyingStartTime = requestBodyJson.getLong("buying_start_time");
  85. if (buyingStartTime <= 0) {
  86. renderJson(MyRet.fail("开放购买时间(buying_start_time)必须是有效的Unix时间戳"));
  87. return;
  88. }
  89. nftt.set("buying_start_time", buyingStartTime);
  90. // ** author (varchar) **
  91. String author = requestBodyJson.getString("author");
  92. nftt.set("author", author);
  93. // ** max_quantity (int) **
  94. Integer maxQuantity = requestBodyJson.getInteger("max_quantity");
  95. if (maxQuantity == null || maxQuantity <= 0) {
  96. renderJson(MyRet.fail("最大份数(max_quantity)必须是大于0的整数"));
  97. return;
  98. }
  99. nftt.set("max_quantity", maxQuantity);
  100. // ** detail_image_list (json) **
  101. // 数据库字段是 JSON 类型,需要存储 JSON 字符串
  102. // 获取 JSON 数组
  103. JSONArray detailImageListArray = requestBodyJson.getJSONArray("detail_image_list");
  104. if (detailImageListArray == null || detailImageListArray.isEmpty()) {
  105. // EmptyInterface 应该已经保证了 non-null,但可能为空数组
  106. // 如果图片列表不能为空,这里可以返回错误
  107. renderJson(MyRet.fail("介绍图URL列表(detail_image_list)不能为空"));
  108. return;
  109. }
  110. // 转换为 JSON 字符串存储
  111. nftt.set("detail_image_list", detailImageListArray.toJSONString());
  112. // ** purchased_quantity (int) - 默认值 **
  113. // 这是一个内部字段,通常在创建时初始化为 0
  114. nftt.set("purchased_quantity", 0);
  115. // ** crate_time, update_time (bigint) - 默认时间戳 **
  116. long currentTimeMillis = System.currentTimeMillis();
  117. nftt.set("create_time", currentTimeMillis);
  118. nftt.set("update_time", currentTimeMillis);
  119. // ** is_deleted (int) - 默认值 **
  120. nftt.set("is_deleted", 0); // 0表示未删除
  121. // 打印 Nftt 对象内部实际存储的属性,进行调试验证
  122. System.out.println("Nftt Model 的内部数据 (for debug): " + nftt);
  123. if (service.save(nftt)) {
  124. renderJson(MyRet.ok("创建成功").setData(service.findNfttById(nftt.getId().toString())));
  125. } else {
  126. renderJson(MyRet.fail("创建失败,原因未知,请将此日志提供给开发者" + nftt).setData(nftt));
  127. }
  128. } catch (NumberFormatException e) {
  129. // 捕获可能发生的数字转换异常
  130. renderJson(MyRet.fail("参数格式不正确,请检查数字或时间戳字段: " + e.getMessage()));
  131. } catch (Exception e) {
  132. // 捕获其他未知异常
  133. renderJson(MyRet.fail("创建失败:" + e.getMessage()));
  134. e.printStackTrace(); // 打印堆栈跟踪,便于调试
  135. }
  136. }
  137. /**
  138. * 更新NFT模板的方法,传什么更新什么,不传就不更新,id必须传
  139. */
  140. @Before(LoginInterceptor.class)
  141. @RequiredRoleInterface({UserController.ROLE_CHECK_ADMIN, UserController.ROLE_SUPER_ADMIN})
  142. @EmptyInterface({"id"})
  143. public void updateByAdmin() {
  144. JSONObject requestBodyJson = MyController.getJsonModelByRequestAndType(getRequest(), JSONObject.class);
  145. String id = requestBodyJson.getString("id");
  146. Nftt nftt = service.findNfttById(id);
  147. if (nftt == null) {
  148. renderJson(MyRet.fail("nft模板获取不合法,该id对应的nftt不存在:" + id));
  149. return;
  150. }
  151. // 名字
  152. String name = requestBodyJson.getString("name");
  153. if (StrKit.notBlank(name)) {
  154. nftt.set("name", name);
  155. }
  156. // 价格
  157. try {
  158. BigDecimal price = requestBodyJson.getBigDecimal("price");
  159. if (price != null) {
  160. // 可以添加额外的业务校验,例如 price 必须大于 0
  161. if (price.compareTo(BigDecimal.ZERO) < 0) {
  162. renderJson(MyRet.fail("售价(price)不能为负数"));
  163. return;
  164. }
  165. nftt.set("price", price);
  166. }
  167. } catch (Exception e) {
  168. renderJson(MyRet.fail("你传入的 price 有些问题:" + e.getMessage()).setData(requestBodyJson));
  169. return;
  170. }
  171. // 预售开始时间
  172. try {
  173. Long presaleStartTime = requestBodyJson.getLong("presale_start_time");
  174. if (presaleStartTime != null) {
  175. // 业务校验:时间戳必须是正数或非负数
  176. if (presaleStartTime <= 0) {
  177. renderJson(MyRet.fail("预售开始时间(presale_start_time)必须是有效的Unix时间戳"));
  178. return;
  179. }
  180. nftt.set("presale_start_time", presaleStartTime);
  181. }
  182. } catch (Exception e) {
  183. renderJson(MyRet.fail("你传入的 presale_start_time 有些问题:" + e.getMessage()).setData(requestBodyJson));
  184. return;
  185. }
  186. // 预售结束时间
  187. try {
  188. Long presaleEndTime = requestBodyJson.getLong("presale_end_time");
  189. if (presaleEndTime != null) {
  190. if (presaleEndTime <= 0) {
  191. renderJson(MyRet.fail("预售结束时间(presale_end_time)必须是有效的Unix时间戳"));
  192. return;
  193. }
  194. nftt.set("presale_end_time", presaleEndTime);
  195. }
  196. } catch (Exception e) {
  197. renderJson(MyRet.fail("你传入的 presale_end_time 有些问题:" + e.getMessage()).setData(requestBodyJson));
  198. return;
  199. }
  200. // 开始购买时间
  201. try {
  202. Long buyingStartTime = requestBodyJson.getLong("buying_start_time");
  203. if (buyingStartTime != null) {
  204. if (buyingStartTime <= 0) {
  205. renderJson(MyRet.fail("开放购买时间(buying_start_time)必须是有效的Unix时间戳"));
  206. return;
  207. }
  208. nftt.set("buying_start_time", buyingStartTime);
  209. }
  210. } catch (Exception e) {
  211. renderJson(MyRet.fail("你传入的 buying_start_time 有些问题:" + e.getMessage()).setData(requestBodyJson));
  212. return;
  213. }
  214. // 已购入份数
  215. try {
  216. Integer purchasedQuantity = requestBodyJson.getInteger("purchased_quantity");
  217. if (purchasedQuantity != null) {
  218. if (purchasedQuantity <= 0) {
  219. renderJson(MyRet.fail("已购入份数(purchased_quantity)必须是大于0的整数"));
  220. return;
  221. }
  222. nftt.set("purchased_quantity", purchasedQuantity);
  223. }
  224. } catch (Exception e) {
  225. renderJson(MyRet.fail("你传入的 purchased_quantity 有些问题:" + e.getMessage()).setData(requestBodyJson));
  226. return;
  227. }
  228. // 最大份数
  229. try {
  230. Integer maxQuantity = requestBodyJson.getInteger("max_quantity");
  231. if (maxQuantity != null) {
  232. if (maxQuantity <= 0) {
  233. renderJson(MyRet.fail("最大份数(max_quantity)必须是大于0的整数"));
  234. return;
  235. }
  236. nftt.set("max_quantity", maxQuantity);
  237. }
  238. } catch (Exception e) {
  239. renderJson(MyRet.fail("你传入的 max_quantity 有些问题:" + e.getMessage()).setData(requestBodyJson));
  240. return;
  241. }
  242. // 作者修改
  243. String author = requestBodyJson.getString("author");
  244. if (StrKit.notBlank(author)) {
  245. nftt.set("author", author);
  246. }
  247. // 详情列表
  248. JSONArray detailImageListArray = requestBodyJson.getJSONArray("detail_image_list");
  249. if (detailImageListArray != null) {
  250. nftt.set("detail_image_list", detailImageListArray.toJSONString());
  251. }
  252. // 最后执行更新
  253. if (nftt.update()) {
  254. renderJson(MyRet.ok("更新成功").setData(nftt));
  255. } else {
  256. renderJson(MyRet.fail("更新失败,请将此日志复制给开发者" + nftt).setData(nftt));
  257. }
  258. }
  259. @Before(LoginInterceptor.class)
  260. @RequiredRoleInterface({UserController.ROLE_CHECK_ADMIN, UserController.ROLE_SUPER_ADMIN})
  261. @EmptyInterface({"page_size", "page_number"})
  262. public void nftts() {
  263. JSONObject requestBodyJson = MyController.getJsonModelByRequestAndType(getRequest(), JSONObject.class);
  264. // 页面大小
  265. String pageSizeStr = requestBodyJson.getString("page_size");
  266. int pageSizeInt;
  267. try {
  268. pageSizeInt = Integer.parseInt(pageSizeStr);
  269. if (pageSizeInt <= 0) {
  270. renderJson(MyRet.fail("页面大小(page_size)期待是正整数,你传的是: " + pageSizeStr));
  271. return;
  272. }
  273. } catch (Exception e) {
  274. renderJson(MyRet.fail("页面大小(page_size)格式不正确: " + e.getMessage()));
  275. return;
  276. }
  277. // 页码
  278. String pageNumberStr = requestBodyJson.getString("page_number");
  279. int pageNumberInt;
  280. try {
  281. pageNumberInt = Integer.parseInt(pageNumberStr);
  282. if (pageNumberInt <= 0) {
  283. renderJson(MyRet.fail("页码(page_number)期待是正整数,你传的是: " + pageNumberStr));
  284. return;
  285. }
  286. } catch (Exception e) {
  287. renderJson(MyRet.fail("页码(page_number)格式不正确: " + e.getMessage()));
  288. return;
  289. }
  290. // 最终结果封装
  291. List<Nftt> nftts = service.nftts(pageNumberInt, pageSizeInt);
  292. Integer totalNftts = service.count();
  293. Map<String, Object> response = new HashMap<>();
  294. response.put("list", nftts);
  295. response.put("total_row", nftts.toArray().length);
  296. response.put("total_page", 1 + (nftts.toArray().length / pageSizeInt));
  297. response.put("page_size", pageSizeInt);
  298. response.put("page_number", pageNumberInt);
  299. response.put("total_user_count", totalNftts);
  300. renderJson(MyRet.ok("查询成功").setData(response));
  301. }
  302. }