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 <noreply@anthropic.com>
This commit is contained in:
71
app/api/warehouse/filters/route.ts
Normal file
71
app/api/warehouse/filters/route.ts
Normal file
@@ -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<string, string> = {
|
||||
"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 }
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user