import { Hono } from 'hono' import type { Variables, WithAuth } from "./middlewares.ts"; export function createXunlianCodesRoutes(withAuth: WithAuth) { const xunlianCodesRoutes = new Hono<{ Variables: Variables }>() // 获取训练码列表 xunlianCodesRoutes.get('/', withAuth, async (c) => { try { const apiClient = c.get('apiClient') const page = Number(c.req.query('page')) || 1 const pageSize = Number(c.req.query('pageSize')) || 10 const offset = (page - 1) * pageSize const search = c.req.query('search') || '' let query = apiClient.database.table('stock_xunlian_codes') .orderBy('id', 'desc') const { code, stock_name, name, type, trade_date } = c.req.query() if (code) query = query.where('code', 'like', `%${code}%`) if (stock_name) query = query.where('stock_name', 'like', `%${stock_name}%`) if (name) query = query.where('name', 'like', `%${name}%`) if (type) query = query.where('type', type) if (trade_date) query = query.where('trade_date', trade_date) if (search) query = query.where('code', 'like', `%${search}%`) const total = await query.clone().count() const codes = await query.select('*') .limit(pageSize).offset(offset) return c.json({ data: codes, pagination: { total: Number(total), current: page, pageSize } }) } catch (error) { console.error('获取训练码列表失败:', error) return c.json({ error: '获取训练码列表失败' }, 500) } }) // 获取单个训练码详情 xunlianCodesRoutes.get('/:id', withAuth, async (c) => { try { const id = Number(c.req.param('id')) if (!id || isNaN(id)) { return c.json({ error: '无效的训练码ID' }, 400) } const apiClient = c.get('apiClient') const code = await apiClient.database.table('stock_xunlian_codes') .where('id', id) .select('*') .first() if (!code) { return c.json({ error: '训练码不存在' }, 404) } return c.json({ data: code, message: '获取训练码详情成功' }) } catch (error) { console.error('获取训练码详情失败:', error) return c.json({ error: '获取训练码详情失败' }, 500) } }) // 创建训练码 xunlianCodesRoutes.post('/', withAuth, async (c) => { try { const apiClient = c.get('apiClient') const body = await c.req.json() // 验证必填字段 const { code, stock_name, name, trade_date } = body if (!code || !stock_name || !name || !trade_date) { return c.json({ error: '缺少必填字段(code/stock_name/name/trade_date)' }, 400) } // 检查训练码是否已存在 const existingCode = await apiClient.database.table('stock_xunlian_codes') .where('code', code) .first() if (existingCode) { return c.json({ error: '训练码已存在' }, 400) } // 创建训练码 const [id] = await apiClient.database.table('stock_xunlian_codes').insert({ ...body, created_at: apiClient.database.fn.now(), updated_at: apiClient.database.fn.now() }) const newCode = await apiClient.database.table('stock_xunlian_codes') .where('id', id) .select('*') .first() return c.json({ data: newCode, message: '创建训练码成功' }) } catch (error) { console.error('创建训练码失败:', error) return c.json({ error: '创建训练码失败', message: error }, 500) } }) // 更新训练码 xunlianCodesRoutes.put('/:id', withAuth, async (c) => { try { const id = Number(c.req.param('id')) if (!id || isNaN(id)) { return c.json({ error: '无效的训练码ID' }, 400) } const apiClient = c.get('apiClient') const body = await c.req.json() // 验证必填字段 const { code, stock_name, name, trade_date } = body if (!code || !stock_name || !name || !trade_date) { return c.json({ error: '缺少必填字段(code/stock_name/name/trade_date)' }, 400) } // 检查训练码是否存在 const existingCode = await apiClient.database.table('stock_xunlian_codes') .where('id', id) .first() if (!existingCode) { return c.json({ error: '训练码不存在' }, 404) } // 如果修改了训练码,检查新训练码是否已被使用 if (code !== existingCode.code) { const codeWithSameName = await apiClient.database.table('stock_xunlian_codes') .where('code', code) .whereNot('id', id.toString()) .first() if (codeWithSameName) { return c.json({ error: '训练码已存在' }, 400) } } // 更新训练码信息 const updateData = { ...body, updated_at: apiClient.database.fn.now() } await apiClient.database.table('stock_xunlian_codes') .where('id', id) .update(updateData) const updatedCode = await apiClient.database.table('stock_xunlian_codes') .where('id', id) .select('*') .first() return c.json({ data: updatedCode, message: '更新训练码成功' }) } catch (error) { console.error('更新训练码失败:', error) return c.json({ error: '更新训练码失败' }, 500) } }) // 删除训练码 xunlianCodesRoutes.delete('/:id', withAuth, async (c) => { try { const id = Number(c.req.param('id')) if (!id || isNaN(id)) { return c.json({ error: '无效的训练码ID' }, 400) } const apiClient = c.get('apiClient') // 检查训练码是否存在 const existingCode = await apiClient.database.table('stock_xunlian_codes') .where('id', id) .first() if (!existingCode) { return c.json({ error: '训练码不存在' }, 404) } // 删除训练码 await apiClient.database.table('stock_xunlian_codes') .where('id', id) .delete() return c.json({ message: '删除训练码成功', id }) } catch (error) { console.error('删除训练码失败:', error) return c.json({ error: '删除训练码失败' }, 500) } }) return xunlianCodesRoutes }