|
|
@@ -15,15 +15,20 @@ export function createXunlianCodesRoutes(withAuth: WithAuth) {
|
|
|
|
|
|
const search = c.req.query('search') || ''
|
|
|
|
|
|
- let query = apiClient.database.table('xunlian_codes')
|
|
|
+ let query = apiClient.database.table('stock_xunlian_codes')
|
|
|
.orderBy('id', 'desc')
|
|
|
|
|
|
- if (search) {
|
|
|
- query = query.where('code', 'like', `%${search}%`)
|
|
|
- }
|
|
|
+ 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('id', 'code', 'status', 'created_at', 'updated_at')
|
|
|
+ const codes = await query.select('*')
|
|
|
.limit(pageSize).offset(offset)
|
|
|
|
|
|
return c.json({
|
|
|
@@ -50,9 +55,9 @@ export function createXunlianCodesRoutes(withAuth: WithAuth) {
|
|
|
}
|
|
|
|
|
|
const apiClient = c.get('apiClient')
|
|
|
- const code = await apiClient.database.table('xunlian_codes')
|
|
|
+ const code = await apiClient.database.table('stock_xunlian_codes')
|
|
|
.where('id', id)
|
|
|
- .select('id', 'code', 'status', 'created_at', 'updated_at')
|
|
|
+ .select('*')
|
|
|
.first()
|
|
|
|
|
|
if (!code) {
|
|
|
@@ -76,13 +81,13 @@ export function createXunlianCodesRoutes(withAuth: WithAuth) {
|
|
|
const body = await c.req.json()
|
|
|
|
|
|
// 验证必填字段
|
|
|
- const { code } = body
|
|
|
- if (!code) {
|
|
|
- return c.json({ error: '缺少训练码' }, 400)
|
|
|
+ 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('xunlian_codes')
|
|
|
+ const existingCode = await apiClient.database.table('stock_xunlian_codes')
|
|
|
.where('code', code)
|
|
|
.first()
|
|
|
|
|
|
@@ -91,16 +96,15 @@ export function createXunlianCodesRoutes(withAuth: WithAuth) {
|
|
|
}
|
|
|
|
|
|
// 创建训练码
|
|
|
- const [id] = await apiClient.database.table('xunlian_codes').insert({
|
|
|
- code,
|
|
|
- status: 'active', // 默认激活状态
|
|
|
- created_at: new Date(),
|
|
|
- updated_at: new Date()
|
|
|
+ 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('xunlian_codes')
|
|
|
+ const newCode = await apiClient.database.table('stock_xunlian_codes')
|
|
|
.where('id', id)
|
|
|
- .select('id', 'code', 'status', 'created_at')
|
|
|
+ .select('*')
|
|
|
.first()
|
|
|
|
|
|
return c.json({
|
|
|
@@ -109,7 +113,7 @@ export function createXunlianCodesRoutes(withAuth: WithAuth) {
|
|
|
})
|
|
|
} catch (error) {
|
|
|
console.error('创建训练码失败:', error)
|
|
|
- return c.json({ error: '创建训练码失败' }, 500)
|
|
|
+ return c.json({ error: '创建训练码失败', message: error }, 500)
|
|
|
}
|
|
|
})
|
|
|
|
|
|
@@ -125,13 +129,13 @@ export function createXunlianCodesRoutes(withAuth: WithAuth) {
|
|
|
const body = await c.req.json()
|
|
|
|
|
|
// 验证必填字段
|
|
|
- const { code } = body
|
|
|
- if (!code) {
|
|
|
- return c.json({ error: '缺少训练码' }, 400)
|
|
|
+ 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('xunlian_codes')
|
|
|
+ const existingCode = await apiClient.database.table('stock_xunlian_codes')
|
|
|
.where('id', id)
|
|
|
.first()
|
|
|
|
|
|
@@ -141,7 +145,7 @@ export function createXunlianCodesRoutes(withAuth: WithAuth) {
|
|
|
|
|
|
// 如果修改了训练码,检查新训练码是否已被使用
|
|
|
if (code !== existingCode.code) {
|
|
|
- const codeWithSameName = await apiClient.database.table('xunlian_codes')
|
|
|
+ const codeWithSameName = await apiClient.database.table('stock_xunlian_codes')
|
|
|
.where('code', code)
|
|
|
.whereNot('id', id.toString())
|
|
|
.first()
|
|
|
@@ -153,17 +157,17 @@ export function createXunlianCodesRoutes(withAuth: WithAuth) {
|
|
|
|
|
|
// 更新训练码信息
|
|
|
const updateData = {
|
|
|
- code,
|
|
|
- updated_at: new Date()
|
|
|
+ ...body,
|
|
|
+ updated_at: apiClient.database.fn.now()
|
|
|
}
|
|
|
|
|
|
- await apiClient.database.table('xunlian_codes')
|
|
|
+ await apiClient.database.table('stock_xunlian_codes')
|
|
|
.where('id', id)
|
|
|
.update(updateData)
|
|
|
|
|
|
- const updatedCode = await apiClient.database.table('xunlian_codes')
|
|
|
+ const updatedCode = await apiClient.database.table('stock_xunlian_codes')
|
|
|
.where('id', id)
|
|
|
- .select('id', 'code', 'status', 'created_at')
|
|
|
+ .select('*')
|
|
|
.first()
|
|
|
|
|
|
return c.json({
|
|
|
@@ -187,7 +191,7 @@ export function createXunlianCodesRoutes(withAuth: WithAuth) {
|
|
|
const apiClient = c.get('apiClient')
|
|
|
|
|
|
// 检查训练码是否存在
|
|
|
- const existingCode = await apiClient.database.table('xunlian_codes')
|
|
|
+ const existingCode = await apiClient.database.table('stock_xunlian_codes')
|
|
|
.where('id', id)
|
|
|
.first()
|
|
|
|
|
|
@@ -196,7 +200,7 @@ export function createXunlianCodesRoutes(withAuth: WithAuth) {
|
|
|
}
|
|
|
|
|
|
// 删除训练码
|
|
|
- await apiClient.database.table('xunlian_codes')
|
|
|
+ await apiClient.database.table('stock_xunlian_codes')
|
|
|
.where('id', id)
|
|
|
.delete()
|
|
|
|
|
|
@@ -210,49 +214,7 @@ export function createXunlianCodesRoutes(withAuth: WithAuth) {
|
|
|
}
|
|
|
})
|
|
|
|
|
|
- // 更新训练码状态
|
|
|
- xunlianCodesRoutes.put('/:id/status', 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 { status } = body
|
|
|
- if (!status || !['active', 'inactive', 'used'].includes(status)) {
|
|
|
- return c.json({ error: '无效的状态值' }, 400)
|
|
|
- }
|
|
|
-
|
|
|
- // 检查训练码是否存在
|
|
|
- const existingCode = await apiClient.database.table('xunlian_codes')
|
|
|
- .where('id', id)
|
|
|
- .first()
|
|
|
-
|
|
|
- if (!existingCode) {
|
|
|
- return c.json({ error: '训练码不存在' }, 404)
|
|
|
- }
|
|
|
-
|
|
|
- // 更新状态
|
|
|
- await apiClient.database.table('xunlian_codes')
|
|
|
- .where('id', id)
|
|
|
- .update({
|
|
|
- status,
|
|
|
- updated_at: new Date()
|
|
|
- })
|
|
|
-
|
|
|
- return c.json({
|
|
|
- message: '更新训练码状态成功',
|
|
|
- id,
|
|
|
- status
|
|
|
- })
|
|
|
- } catch (error) {
|
|
|
- console.error('更新训练码状态失败:', error)
|
|
|
- return c.json({ error: '更新训练码状态失败' }, 500)
|
|
|
- }
|
|
|
- })
|
|
|
|
|
|
return xunlianCodesRoutes
|
|
|
}
|