66 lines
2.4 KiB
TypeScript
66 lines
2.4 KiB
TypeScript
import pool from '../../db.js';
|
||
import type { VehicleInfoRow } from './types.js';
|
||
|
||
/** 车辆关联信息 SQL(客户名、部门、经理、租赁状态、主体、项目) */
|
||
export const VEHICLE_INFO_SQL = `SELECT
|
||
vi.plate_number AS plate,
|
||
COALESCE(c.customer_name, vor.customer_name, ci.customer_name) AS customer,
|
||
COALESCE(c.business_department_name, vor.business_dept) AS department,
|
||
COALESCE(c.business_manager_name, vor.business_manager) AS manager,
|
||
CAST(COALESCE(c.business_manager_id, vi.business_id) AS CHAR) AS manager_id,
|
||
CASE vs.operation_status
|
||
WHEN '1' THEN '租赁'
|
||
WHEN '2' THEN '自营'
|
||
WHEN '3' THEN '可运营'
|
||
WHEN '4' THEN '待运营'
|
||
WHEN '5' THEN '退出运营'
|
||
ELSE vs.operation_status
|
||
END AS rent_status,
|
||
NULLIF(vi.registered_ownership, '') AS entity,
|
||
COALESCE(c.project_name, vor.project_name) AS project
|
||
FROM vehicle_info vi
|
||
LEFT JOIN vehicle_status vs
|
||
ON vs.vehicle_id = vi.id
|
||
AND vs.del_flag = 0
|
||
LEFT JOIN vehicle_lease_order_record vor
|
||
ON vor.vehicle_id = vi.id
|
||
AND vor.del_flag = '0'
|
||
AND vor.id = (
|
||
SELECT MAX(vor2.id)
|
||
FROM vehicle_lease_order_record vor2
|
||
WHERE vor2.vehicle_id = vi.id
|
||
AND vor2.del_flag = '0'
|
||
)
|
||
LEFT JOIN vehicle_lease_contract_info c
|
||
ON c.order_id = vor.contract_id
|
||
AND c.del_flag = '0'
|
||
LEFT JOIN customer_info ci
|
||
ON ci.id = vi.customer_id
|
||
AND ci.del_flag = '0'
|
||
WHERE vi.del_flag = '0'
|
||
AND COALESCE(vs.operation_status, '') <> '5'`;
|
||
|
||
/** 查询所有车辆关联信息,返回 plate→info 的 Map */
|
||
export async function fetchVehicleInfoMap(): Promise<Map<string, VehicleInfoRow>> {
|
||
const [rows] = await pool.execute(VEHICLE_INFO_SQL) as [VehicleInfoRow[], unknown];
|
||
const map = new Map<string, VehicleInfoRow>();
|
||
for (const row of rows) {
|
||
map.set(row.plate, row);
|
||
}
|
||
return map;
|
||
}
|
||
|
||
/** 查询指定车牌的关联信息 */
|
||
export async function fetchVehicleInfoByPlates(plates: string[]): Promise<Map<string, VehicleInfoRow>> {
|
||
if (plates.length === 0) return new Map();
|
||
const [rows] = await pool.execute(
|
||
`${VEHICLE_INFO_SQL} AND vi.plate_number IN (${plates.map(() => '?').join(',')})`,
|
||
plates
|
||
) as [VehicleInfoRow[], unknown];
|
||
const map = new Map<string, VehicleInfoRow>();
|
||
for (const row of rows) {
|
||
map.set(row.plate, row);
|
||
}
|
||
return map;
|
||
}
|