routes_submission_records.ts 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. import { Hono } from "hono";
  2. import debug from "debug";
  3. import type {
  4. SubmissionRecord,
  5. } from "../client/share/types_stock.ts";
  6. import {
  7. SubmissionRecordStatus,
  8. } from "../client/share/types_stock.ts";
  9. import {
  10. DeleteStatus,
  11. EnableStatus,
  12. } from "../client/share/types.ts";
  13. import type { Variables, WithAuth } from "./middlewares.ts";
  14. const log = {
  15. api: debug("api:submission"),
  16. };
  17. // 创建提交记录路由
  18. export function createSubmissionRoutes(withAuth: WithAuth) {
  19. const submissionRoutes = new Hono<{ Variables: Variables }>();
  20. // 创建提交记录
  21. submissionRoutes.post("/", withAuth, async (c) => {
  22. try {
  23. const recordData = (await c.req.json()) as Partial<SubmissionRecord>;
  24. const user = c.get("user");
  25. // 验证必填字段
  26. if (!recordData.code || !recordData.training_date) {
  27. return c.json({ error: "股票代码和训练日期不能为空" }, 400);
  28. }
  29. const apiClient = c.get('apiClient');
  30. // 设置提交者信息
  31. if (user) {
  32. recordData.user_id = user.id;
  33. recordData.nickname = user.nickname || user.username;
  34. }
  35. // 设置默认状态
  36. recordData.status = SubmissionRecordStatus.PENDING;
  37. // 插入提交记录
  38. const [id] = await apiClient.database.table("submission_records").insert({
  39. ...recordData,
  40. is_disabled: EnableStatus.ENABLED,
  41. is_deleted: DeleteStatus.NOT_DELETED,
  42. created_at: apiClient.database.fn.now(),
  43. updated_at: apiClient.database.fn.now(),
  44. });
  45. // 获取插入的数据
  46. const [insertedRecord] = await apiClient.database
  47. .table("submission_records")
  48. .where("id", id);
  49. return c.json({
  50. message: "提交记录创建成功",
  51. data: insertedRecord,
  52. });
  53. } catch (error) {
  54. log.api("创建提交记录失败:", error);
  55. return c.json({ error: "创建提交记录失败" }, 500);
  56. }
  57. });
  58. // 获取提交记录列表
  59. submissionRoutes.get("/list", withAuth, async (c) => {
  60. try {
  61. const page = Number(c.req.query("page")) || 1;
  62. const pageSize = Number(c.req.query("pageSize")) || 10;
  63. const status = c.req.query("status");
  64. const keyword = c.req.query("keyword");
  65. const apiClient = c.get('apiClient');
  66. let query = apiClient.database
  67. .table("submission_records")
  68. .where("is_deleted", DeleteStatus.NOT_DELETED)
  69. .orderBy("created_at", "desc");
  70. // 应用过滤条件
  71. if (status) {
  72. query = query.where("status", status);
  73. }
  74. if (keyword) {
  75. query = query.where((builder) => {
  76. builder
  77. .where("code", "like", `%${keyword}%`)
  78. .orWhere("training_date", "like", `%${keyword}%`);
  79. });
  80. }
  81. // 获取总数
  82. const total = await query.clone().count();
  83. // 分页查询
  84. const records = await query.limit(pageSize).offset((page - 1) * pageSize);
  85. return c.json({
  86. message: "获取提交记录列表成功",
  87. data: {
  88. list: records,
  89. pagination: {
  90. current: page,
  91. pageSize,
  92. total: Number(total),
  93. },
  94. },
  95. });
  96. } catch (error) {
  97. log.api("获取提交记录列表失败:", error);
  98. return c.json({ error: "获取提交记录列表失败" }, 500);
  99. }
  100. });
  101. // 获取单个提交记录
  102. submissionRoutes.get("/:id", withAuth, async (c) => {
  103. try {
  104. const id = Number(c.req.param("id"));
  105. if (!id || isNaN(id)) {
  106. return c.json({ error: "无效的提交记录ID" }, 400);
  107. }
  108. const apiClient = c.get('apiClient');
  109. const record = await apiClient.database
  110. .table("submission_records")
  111. .where("id", id)
  112. .where("is_deleted", DeleteStatus.NOT_DELETED)
  113. .first();
  114. if (!record) {
  115. return c.json({ error: "提交记录不存在" }, 404);
  116. }
  117. return c.json({
  118. message: "获取提交记录成功",
  119. data: record,
  120. });
  121. } catch (error) {
  122. log.api("获取提交记录失败:", error);
  123. return c.json({ error: "获取提交记录失败" }, 500);
  124. }
  125. });
  126. // 更新提交记录
  127. submissionRoutes.put("/:id", withAuth, async (c) => {
  128. try {
  129. const id = Number(c.req.param("id"));
  130. const recordData = (await c.req.json()) as Partial<SubmissionRecord>;
  131. if (!id || isNaN(id)) {
  132. return c.json({ error: "无效的提交记录ID" }, 400);
  133. }
  134. const apiClient = c.get('apiClient');
  135. // 查询记录是否存在
  136. const record = await apiClient.database
  137. .table("submission_records")
  138. .where("id", id)
  139. .where("is_deleted", DeleteStatus.NOT_DELETED)
  140. .first();
  141. if (!record) {
  142. return c.json({ error: "提交记录不存在" }, 404);
  143. }
  144. // 更新记录
  145. await apiClient.database
  146. .table("submission_records")
  147. .where("id", id)
  148. .update({
  149. ...recordData,
  150. updated_at: apiClient.database.fn.now(),
  151. });
  152. // 获取更新后的数据
  153. const [updatedRecord] = await apiClient.database
  154. .table("submission_records")
  155. .where("id", id);
  156. return c.json({
  157. message: "更新提交记录成功",
  158. data: updatedRecord,
  159. });
  160. } catch (error) {
  161. log.api("更新提交记录失败:", error);
  162. return c.json({ error: "更新提交记录失败" }, 500);
  163. }
  164. });
  165. // 删除提交记录
  166. submissionRoutes.delete("/:id", withAuth, async (c) => {
  167. try {
  168. const id = Number(c.req.param("id"));
  169. if (!id || isNaN(id)) {
  170. return c.json({ error: "无效的提交记录ID" }, 400);
  171. }
  172. const apiClient = c.get('apiClient');
  173. // 查询记录是否存在
  174. const record = await apiClient.database
  175. .table("submission_records")
  176. .where("id", id)
  177. .where("is_deleted", DeleteStatus.NOT_DELETED)
  178. .first();
  179. if (!record) {
  180. return c.json({ error: "提交记录不存在" }, 404);
  181. }
  182. // 软删除记录
  183. await apiClient.database.table("submission_records").where("id", id).update({
  184. is_deleted: DeleteStatus.DELETED,
  185. updated_at: apiClient.database.fn.now(),
  186. });
  187. return c.json({
  188. message: "提交记录删除成功",
  189. });
  190. } catch (error) {
  191. log.api("删除提交记录失败:", error);
  192. return c.json({ error: "删除提交记录失败" }, 500);
  193. }
  194. });
  195. // 获取提交状态
  196. submissionRoutes.get("/:id/status", withAuth, async (c) => {
  197. try {
  198. const id = Number(c.req.param("id"));
  199. if (!id || isNaN(id)) {
  200. return c.json({ error: "无效的提交记录ID" }, 400);
  201. }
  202. const apiClient = c.get('apiClient');
  203. const record = await apiClient.database
  204. .table("submission_records")
  205. .where("id", id)
  206. .where("is_deleted", DeleteStatus.NOT_DELETED)
  207. .select("status")
  208. .first();
  209. if (!record) {
  210. return c.json({ error: "提交记录不存在" }, 404);
  211. }
  212. return c.json({
  213. message: "获取提交状态成功",
  214. data: {
  215. status: record.status,
  216. },
  217. });
  218. } catch (error) {
  219. log.api("获取提交状态失败:", error);
  220. return c.json({ error: "获取提交状态失败" }, 500);
  221. }
  222. });
  223. // 更新提交状态
  224. submissionRoutes.put("/:id/status", withAuth, async (c) => {
  225. try {
  226. const id = Number(c.req.param("id"));
  227. const { status } = (await c.req.json()) as { status: SubmissionRecordStatus };
  228. if (!id || isNaN(id)) {
  229. return c.json({ error: "无效的提交记录ID" }, 400);
  230. }
  231. if (!status) {
  232. return c.json({ error: "状态不能为空" }, 400);
  233. }
  234. const apiClient = c.get('apiClient');
  235. // 查询记录是否存在
  236. const record = await apiClient.database
  237. .table("submission_records")
  238. .where("id", id)
  239. .where("is_deleted", DeleteStatus.NOT_DELETED)
  240. .first();
  241. if (!record) {
  242. return c.json({ error: "提交记录不存在" }, 404);
  243. }
  244. // 更新状态
  245. await apiClient.database
  246. .table("submission_records")
  247. .where("id", id)
  248. .update({
  249. status,
  250. updated_at: apiClient.database.fn.now(),
  251. });
  252. return c.json({
  253. message: "更新提交状态成功",
  254. });
  255. } catch (error) {
  256. log.api("更新提交状态失败:", error);
  257. return c.json({ error: "更新提交状态失败" }, 500);
  258. }
  259. });
  260. return submissionRoutes;
  261. }