import type { IncomingMessage, ServerResponse } from "node:http"; import type { Plugin } from "vite"; const station = { id: "101", name: "本地验收加氢站", province: "浙江省", city: "嘉兴市", kg: 12345.67, lingniuKg: 10000, externalKg: 2345.67, cost: 23456.78, revenue: 34567.89, customerRevenue: 34567.89, customerCost: 18000, companyCost: 4000, otherCost: 1456.78, recordCount: 42, customerCount: 1, share: 100, }; const customer = { id: 1, name: "本地验收客户", kg: 12345.67, customerBearingKg: 10000, companyBearingKg: 2000, otherBearingKg: 345.67, bearer: "both", cost: 23456.78, revenue: 34567.89, customerRevenue: 34567.89, customerCost: 18000, companyCost: 4000, otherCost: 1456.78, recordCount: 42, }; const range = { startDate: "2026-01-01", endDate: "2026-08-31" }; const watermark = { ledgerAt: "2026-08-31 16:00:00", paymentAt: null }; const kpis = { totalKg: 12345.67, totalCost: 23456.78, customerBearingKg: 10000, companyBearingKg: 2000, otherBearingKg: 345.67, customerRevenue: 34567.89, customerCost: 18000, companyCost: 4000, otherCost: 1456.78, totalRevenue: 34567.89, customerGrossProfit: 16567.89, monthKg: 3456.78, monthCost: 6789.01, todayKg: 123.45, todayCost: 234.56, monthShareOfRange: 28, todayShareOfMonth: 3.57, recordCount: 42, stationCount: 1, }; const overview = { range, watermark, filters: {}, kpis, monthly: [{ month: "2026-08", totalKg: 12345.67, lingniuKg: 10000, externalKg: 2345.67, cost: 23456.78, customerCost: 18000, companyCost: 4000, otherCost: 1456.78, revenue: 34567.89, customerRevenue: 34567.89, customerGrossProfit: 16567.89, }], topStations: [station], regions: [{ region: "嘉兴市", kg: 12345.67, share: 100 }], stations: [station], customers: [customer], }; const dailyPoint = { date: "2026-08-31", kg: 123.45, lingniuKg: 100, externalKg: 23.45, cost: 234.56, recordCount: 2, stationCount: 1, }; export function devMockResponse(pathname: string) { if (pathname === "/api/health") return { status: "ok", source: "dev-mock" }; if (pathname.endsWith("/meta")) return { years: [{ value: 2026, startDate: range.startDate, endDate: range.endDate }], stations: [{ id: station.id, name: station.name }], watermark, }; if (pathname.endsWith("/overview")) return overview; if (pathname.endsWith("/daily-tree")) return { date: dailyPoint.date, stations: [{ id: station.id, name: station.name, kg: dailyPoint.kg, cost: dailyPoint.cost, recordCount: 2, customers: [{ id: customer.id, name: customer.name, kg: dailyPoint.kg, cost: dailyPoint.cost, recordCount: 2 }], }], }; if (pathname.endsWith("/daily")) return { range, watermark, filters: {}, kpis: { totalKg: 12345.67, totalCost: 23456.78, averageDailyKg: 823.04, stationCount: 1, activeDays: 15 }, trend: [dailyPoint], days: [dailyPoint], }; if (pathname.endsWith("/drill")) return { groupBy: "record", amountScope: "all", filters: {}, summary: { recordCount: 1, kg: 123.45, cost: 4000, revenue: 4320 }, groups: [{ ...station, stationCount: 1, customerCount: 1 }], records: [{ id: 1, time: "2026-08-31 12:00:00", orderNo: "DEV-001", stationId: station.id, stationName: station.name, customerId: customer.id, customerName: customer.name, plateNo: "浙FDEV01", source: "dev-mock", verifyStatus: "verified", vehicleId: 1, kg: 123.45, unitPrice: 35, cost: 4000, revenue: 4320, }], page: { page: 1, pageSize: 100, hasMore: false }, }; return undefined; } function sendJson(response: ServerResponse, body: unknown) { response.statusCode = 200; response.setHeader("Content-Type", "application/json; charset=utf-8"); response.end(JSON.stringify(body)); } export function energyDevMockApi(): Plugin { return { name: "energy-dev-mock-api", configureServer(server) { server.middlewares.use((request: IncomingMessage, response: ServerResponse, next) => { const pathname = new URL(request.url ?? "/", "http://localhost").pathname; if (pathname === "/favicon.ico") { response.statusCode = 204; return response.end(); } const body = devMockResponse(pathname); if (body === undefined) return next(); sendJson(response, body); }); }, }; }