From baa339025a8559f17f8460dad3aedb9c5b5e765c Mon Sep 17 00:00:00 2001 From: Misaka_Company Date: Thu, 4 Jun 2026 09:39:59 +0800 Subject: [PATCH] feat: add filter options API endpoint Add GET /api/warehouse/filters endpoint that returns distinct workshop codes/names and handler names from both pressure gauge and thermometer contract tables, filtering out noise values from the workshop column. Co-Authored-By: Claude Opus 4.6 --- app/api/warehouse/filters/route.ts | 71 ++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 app/api/warehouse/filters/route.ts diff --git a/app/api/warehouse/filters/route.ts b/app/api/warehouse/filters/route.ts new file mode 100644 index 0000000..8c4f0e0 --- /dev/null +++ b/app/api/warehouse/filters/route.ts @@ -0,0 +1,71 @@ +import { NextResponse } from "next/server"; +import sql from "mssql"; +import { getPool } from "@/lib/db"; +import type { FilterOptions } from "@/types/warehouse"; + +// Workshop code -> display name mapping +const WORKSHOP_MAP: Record = { + "1": "一车间", + "2": "二车间", + "3": "温度计车间", + "4": "四车间", + "5": "五车间", +}; + +export async function GET() { + try { + const pool = await getPool(); + + // Get distinct workshops from both contract tables + const workshopResult = await pool.request().query(` + SELECT DISTINCT 车间 AS code + FROM productionContractData.[26年压力表合同数据] + WHERE 车间 IS NOT NULL AND 车间 NOT IN (' ', '') + AND 车间 NOT LIKE '%取消%' AND 车间 NOT LIKE '%暂停%' + AND 车间 NOT LIKE '%变更%' AND 车间 NOT LIKE '%检%' + AND 车间 NOT LIKE '%重选%' AND 车间 NOT LIKE '%外购%' + AND 车间 NOT LIKE '%取%' + UNION + SELECT DISTINCT 车间 AS code + FROM productionContractData.[26年温度计合同数据] + WHERE 车间 IS NOT NULL AND 车间 NOT IN (' ', '') + AND 车间 NOT LIKE '%取消%' AND 车间 NOT LIKE '%暂停%' + AND 车间 NOT LIKE '%变更%' AND 车间 NOT LIKE '%检%' + AND 车间 NOT LIKE '%重选%' AND 车间 NOT LIKE '%外购%' + AND 车间 NOT LIKE '%取%' + ORDER BY code + `); + + const workshops = workshopResult.recordset.map( + (r: { code: string }) => ({ + code: r.code.trim(), + name: WORKSHOP_MAP[r.code.trim()] || r.code.trim(), + }) + ); + + // Get distinct handlers + const handlerResult = await pool.request().query(` + SELECT DISTINCT 经办人 AS handler + FROM productionContractData.[26年压力表合同数据] + WHERE 经办人 IS NOT NULL AND 经办人 <> '' + UNION + SELECT DISTINCT 经办人 AS handler + FROM productionContractData.[26年温度计合同数据] + WHERE 经办人 IS NOT NULL AND 经办人 <> '' + ORDER BY handler + `); + + const handlers = handlerResult.recordset.map( + (r: { handler: string }) => r.handler + ); + + const data: FilterOptions = { workshops, handlers }; + return NextResponse.json(data); + } catch (error) { + console.error("Failed to fetch filter options:", error); + return NextResponse.json( + { error: "Failed to fetch filter options" }, + { status: 500 } + ); + } +}