routes_xunlian_codes.ts 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. import { Hono } from 'hono'
  2. import type { Variables, WithAuth } from "./middlewares.ts";
  3. export function createXunlianCodesRoutes(withAuth: WithAuth) {
  4. const xunlianCodesRoutes = new Hono<{ Variables: Variables }>()
  5. // 获取训练码列表
  6. xunlianCodesRoutes.get('/', withAuth, async (c) => {
  7. try {
  8. const apiClient = c.get('apiClient')
  9. const page = Number(c.req.query('page')) || 1
  10. const pageSize = Number(c.req.query('pageSize')) || 10
  11. const offset = (page - 1) * pageSize
  12. const search = c.req.query('search') || ''
  13. let query = apiClient.database.table('stock_xunlian_codes')
  14. .orderBy('id', 'desc')
  15. const { code, stock_name, name, type, trade_date } = c.req.query()
  16. if (code) query = query.where('code', 'like', `%${code}%`)
  17. if (stock_name) query = query.where('stock_name', 'like', `%${stock_name}%`)
  18. if (name) query = query.where('name', 'like', `%${name}%`)
  19. if (type) query = query.where('type', type)
  20. if (trade_date) query = query.where('trade_date', trade_date)
  21. if (search) query = query.where('code', 'like', `%${search}%`)
  22. const total = await query.clone().count()
  23. const codes = await query.select('*')
  24. .limit(pageSize).offset(offset)
  25. return c.json({
  26. data: codes,
  27. pagination: {
  28. total: Number(total),
  29. current: page,
  30. pageSize
  31. }
  32. })
  33. } catch (error) {
  34. console.error('获取训练码列表失败:', error)
  35. return c.json({ error: '获取训练码列表失败' }, 500)
  36. }
  37. })
  38. // 获取单个训练码详情
  39. xunlianCodesRoutes.get('/:id', withAuth, async (c) => {
  40. try {
  41. const id = Number(c.req.param('id'))
  42. if (!id || isNaN(id)) {
  43. return c.json({ error: '无效的训练码ID' }, 400)
  44. }
  45. const apiClient = c.get('apiClient')
  46. const code = await apiClient.database.table('stock_xunlian_codes')
  47. .where('id', id)
  48. .select('*')
  49. .first()
  50. if (!code) {
  51. return c.json({ error: '训练码不存在' }, 404)
  52. }
  53. return c.json({
  54. data: code,
  55. message: '获取训练码详情成功'
  56. })
  57. } catch (error) {
  58. console.error('获取训练码详情失败:', error)
  59. return c.json({ error: '获取训练码详情失败' }, 500)
  60. }
  61. })
  62. // 创建训练码
  63. xunlianCodesRoutes.post('/', withAuth, async (c) => {
  64. try {
  65. const apiClient = c.get('apiClient')
  66. const body = await c.req.json()
  67. // 验证必填字段
  68. const { code, stock_name, name, trade_date } = body
  69. if (!code || !stock_name || !name || !trade_date) {
  70. return c.json({ error: '缺少必填字段(code/stock_name/name/trade_date)' }, 400)
  71. }
  72. // 检查训练码是否已存在
  73. const existingCode = await apiClient.database.table('stock_xunlian_codes')
  74. .where('code', code)
  75. .first()
  76. if (existingCode) {
  77. return c.json({ error: '训练码已存在' }, 400)
  78. }
  79. // 创建训练码
  80. const [id] = await apiClient.database.table('stock_xunlian_codes').insert({
  81. ...body,
  82. created_at: apiClient.database.fn.now(),
  83. updated_at: apiClient.database.fn.now()
  84. })
  85. const newCode = await apiClient.database.table('stock_xunlian_codes')
  86. .where('id', id)
  87. .select('*')
  88. .first()
  89. return c.json({
  90. data: newCode,
  91. message: '创建训练码成功'
  92. })
  93. } catch (error) {
  94. console.error('创建训练码失败:', error)
  95. return c.json({ error: '创建训练码失败', message: error }, 500)
  96. }
  97. })
  98. // 更新训练码
  99. xunlianCodesRoutes.put('/:id', withAuth, async (c) => {
  100. try {
  101. const id = Number(c.req.param('id'))
  102. if (!id || isNaN(id)) {
  103. return c.json({ error: '无效的训练码ID' }, 400)
  104. }
  105. const apiClient = c.get('apiClient')
  106. const body = await c.req.json()
  107. // 验证必填字段
  108. const { code, stock_name, name, trade_date } = body
  109. if (!code || !stock_name || !name || !trade_date) {
  110. return c.json({ error: '缺少必填字段(code/stock_name/name/trade_date)' }, 400)
  111. }
  112. // 检查训练码是否存在
  113. const existingCode = await apiClient.database.table('stock_xunlian_codes')
  114. .where('id', id)
  115. .first()
  116. if (!existingCode) {
  117. return c.json({ error: '训练码不存在' }, 404)
  118. }
  119. // 如果修改了训练码,检查新训练码是否已被使用
  120. if (code !== existingCode.code) {
  121. const codeWithSameName = await apiClient.database.table('stock_xunlian_codes')
  122. .where('code', code)
  123. .whereNot('id', id.toString())
  124. .first()
  125. if (codeWithSameName) {
  126. return c.json({ error: '训练码已存在' }, 400)
  127. }
  128. }
  129. // 更新训练码信息
  130. const updateData = {
  131. ...body,
  132. updated_at: apiClient.database.fn.now()
  133. }
  134. await apiClient.database.table('stock_xunlian_codes')
  135. .where('id', id)
  136. .update(updateData)
  137. const updatedCode = await apiClient.database.table('stock_xunlian_codes')
  138. .where('id', id)
  139. .select('*')
  140. .first()
  141. return c.json({
  142. data: updatedCode,
  143. message: '更新训练码成功'
  144. })
  145. } catch (error) {
  146. console.error('更新训练码失败:', error)
  147. return c.json({ error: '更新训练码失败' }, 500)
  148. }
  149. })
  150. // 删除训练码
  151. xunlianCodesRoutes.delete('/:id', withAuth, async (c) => {
  152. try {
  153. const id = Number(c.req.param('id'))
  154. if (!id || isNaN(id)) {
  155. return c.json({ error: '无效的训练码ID' }, 400)
  156. }
  157. const apiClient = c.get('apiClient')
  158. // 检查训练码是否存在
  159. const existingCode = await apiClient.database.table('stock_xunlian_codes')
  160. .where('id', id)
  161. .first()
  162. if (!existingCode) {
  163. return c.json({ error: '训练码不存在' }, 404)
  164. }
  165. // 删除训练码
  166. await apiClient.database.table('stock_xunlian_codes')
  167. .where('id', id)
  168. .delete()
  169. return c.json({
  170. message: '删除训练码成功',
  171. id
  172. })
  173. } catch (error) {
  174. console.error('删除训练码失败:', error)
  175. return c.json({ error: '删除训练码失败' }, 500)
  176. }
  177. })
  178. return xunlianCodesRoutes
  179. }