import { NextRequest, NextResponse } from 'next/server' import { getDb } from '@/lib/db' import { initDatabase } from '@/lib/db-schema' import { getCurrentUser } from '@/lib/auth' export async function GET(request: NextRequest) { try { initDatabase() const user = await getCurrentUser() if (!user) return NextResponse.json({ error: '未登录' }, { status: 401 }) const { searchParams } = request.nextUrl const fields = searchParams.getAll('field') const db = getDb() const result: Record = {} for (const field of fields) { const allowed = ['device_ip', 'device_name', 'fault_category', 'current_status', 'ticket_no', 'fault_subcategory'] if (!allowed.includes(field)) continue const rows = db.prepare(`SELECT DISTINCT ${field} FROM tickets WHERE ${field} IS NOT NULL AND ${field} != '' ORDER BY ${field}`).all() as Record[] result[field] = rows.map(r => r[field]) } return NextResponse.json(result) } catch (e) { const msg = e instanceof Error ? e.message : '查询失败' return NextResponse.json({ error: msg }, { status: 500 }) } }