import { NextResponse } from 'next/server' import { cookies } from 'next/headers' import db from '@/lib/db' import { verifyJwt } from '@/lib/auth' async function getSession() { const cookieStore = await cookies() const token = cookieStore.get('session_assets')?.value if (!token) return null return verifyJwt(token) } export async function GET() { const session = await getSession() if (!session) return NextResponse.json({ error: '未登录' }, { status: 401 }) const total = (db.prepare('SELECT COUNT(*) as c FROM assets').get() as any).c const byStatus = db.prepare( 'SELECT status, COUNT(*) as count FROM assets GROUP BY status ORDER BY count DESC' ).all() const byDeviceType = db.prepare( 'SELECT device_type, COUNT(*) as count FROM assets GROUP BY device_type ORDER BY count DESC' ).all() const byManufacturer = db.prepare( "SELECT manufacturer, COUNT(*) as count FROM assets WHERE manufacturer IS NOT NULL AND manufacturer != '' GROUP BY manufacturer ORDER BY count DESC" ).all() const warrantySoon = (db.prepare( "SELECT COUNT(*) as c FROM assets WHERE warranty_date IS NOT NULL AND warranty_date != '' AND date(warranty_date) <= date('now', '+90 days') AND date(warranty_date) >= date('now')" ).get() as any).c const warrantyExpired = (db.prepare( "SELECT COUNT(*) as c FROM assets WHERE warranty_date IS NOT NULL AND warranty_date != '' AND date(warranty_date) < date('now')" ).get() as any).c const byRoom = db.prepare( "SELECT room, COUNT(*) as count FROM assets WHERE room IS NOT NULL AND room != '' GROUP BY room ORDER BY count DESC LIMIT 10" ).all() return NextResponse.json({ total, byStatus, byDeviceType, byManufacturer, byRoom, warrantySoon, warrantyExpired, }) }