145 lines
4.5 KiB
TypeScript
145 lines
4.5 KiB
TypeScript
import * as XLSX from 'xlsx';
|
||
import type { MonitoringVehicle } from './types';
|
||
|
||
interface ExportContext {
|
||
date?: string;
|
||
startDate?: string;
|
||
endDate?: string;
|
||
sortBy: 'today' | 'total';
|
||
}
|
||
|
||
const HEADERS = [
|
||
'状态', '车牌号', '客户', '业务部门', '项目', '租赁状态',
|
||
'运营区域', '区间里程(km)', '累计里程(km)',
|
||
] as const;
|
||
|
||
function statusLabel(v: MonitoringVehicle): string {
|
||
if (!v.isDataSynced) return '未对接';
|
||
return v.isOnline ? '在线' : '离线';
|
||
}
|
||
|
||
function mileageCell(v: MonitoringVehicle, kind: 'today' | 'total'): string | number {
|
||
if (kind === 'today') {
|
||
// 区间内未对接但有历史累计,视作区间 0;只有完全无数据才标「未对接」。
|
||
if (!v.isDataSynced && v.totalKm == null) return '未对接';
|
||
return Math.max(0, v.dailyKm || 0);
|
||
}
|
||
return v.totalKm != null ? v.totalKm : '未对接';
|
||
}
|
||
|
||
export function exportMileageXlsx(vehicles: MonitoringVehicle[], ctx: ExportContext): void {
|
||
const start = ctx.startDate || ctx.date || '';
|
||
const end = ctx.endDate || ctx.date || '';
|
||
const isRange = !!start && !!end && start !== end;
|
||
const summaryData: (string | number)[][] = [
|
||
[...HEADERS],
|
||
...vehicles.map(v => [
|
||
statusLabel(v),
|
||
v.plate,
|
||
v.customer || '',
|
||
v.department || '',
|
||
v.project || '',
|
||
v.rentStatus || '',
|
||
v.region || '',
|
||
mileageCell(v, 'today'),
|
||
mileageCell(v, 'total'),
|
||
]),
|
||
];
|
||
|
||
const ws = XLSX.utils.aoa_to_sheet(summaryData);
|
||
|
||
ws['!cols'] = [
|
||
{ wch: 8 }, // 状态
|
||
{ wch: 12 }, // 车牌号
|
||
{ wch: 28 }, // 客户
|
||
{ wch: 14 }, // 业务部门
|
||
{ wch: 16 }, // 项目
|
||
{ wch: 10 }, // 租赁状态
|
||
{ wch: 12 }, // 运营区域
|
||
{ wch: 14 }, // 区间里程
|
||
{ wch: 14 }, // 累计里程
|
||
];
|
||
|
||
ws['!freeze'] = { xSplit: 0, ySplit: 1 } as never;
|
||
|
||
for (let r = 1; r < summaryData.length; r++) {
|
||
for (const c of [7, 8]) {
|
||
const ref = XLSX.utils.encode_cell({ r, c });
|
||
if (ws[ref]?.t === 'n') ws[ref].z = '0.##########';
|
||
}
|
||
}
|
||
|
||
// 表头样式(在客户端 SheetJS 社区版仅基本样式生效)
|
||
for (let c = 0; c < HEADERS.length; c++) {
|
||
const ref = XLSX.utils.encode_cell({ r: 0, c });
|
||
if (ws[ref]) {
|
||
(ws[ref] as { s?: unknown }).s = {
|
||
font: { bold: true, color: { rgb: 'FFFFFF' } },
|
||
fill: { fgColor: { rgb: '2563EB' } },
|
||
alignment: { horizontal: 'center', vertical: 'center' },
|
||
};
|
||
}
|
||
}
|
||
|
||
const wb = XLSX.utils.book_new();
|
||
XLSX.utils.book_append_sheet(wb, ws, '车辆汇总');
|
||
|
||
const dayKeys = Array.from(
|
||
new Set(vehicles.flatMap(v => Object.keys(v.dailyMileage || {})))
|
||
).sort();
|
||
if (dayKeys.length > 0) {
|
||
const detailHeaders = [
|
||
'车牌号', '客户', '业务部门', '项目', '租赁状态', '运营区域',
|
||
...dayKeys.map(day => `${day}里程(km)`),
|
||
'区间合计(km)',
|
||
'累计里程(km)',
|
||
];
|
||
const detailData: (string | number)[][] = [
|
||
detailHeaders,
|
||
...vehicles.map(v => [
|
||
v.plate,
|
||
v.customer || '',
|
||
v.department || '',
|
||
v.project || '',
|
||
v.rentStatus || '',
|
||
v.region || '',
|
||
...dayKeys.map(day => v.dailyMileage?.[day] || 0),
|
||
Math.max(0, v.dailyKm || 0),
|
||
v.totalKm != null ? v.totalKm : '',
|
||
]),
|
||
];
|
||
const detailWs = XLSX.utils.aoa_to_sheet(detailData);
|
||
detailWs['!cols'] = [
|
||
{ wch: 12 },
|
||
{ wch: 28 },
|
||
{ wch: 14 },
|
||
{ wch: 16 },
|
||
{ wch: 10 },
|
||
{ wch: 12 },
|
||
...dayKeys.map(() => ({ wch: 14 })),
|
||
{ wch: 14 },
|
||
{ wch: 14 },
|
||
];
|
||
detailWs['!freeze'] = { xSplit: 6, ySplit: 1 } as never;
|
||
for (let r = 1; r < detailData.length; r++) {
|
||
for (let c = 6; c < detailHeaders.length; c++) {
|
||
const ref = XLSX.utils.encode_cell({ r, c });
|
||
if (detailWs[ref]?.t === 'n') detailWs[ref].z = '0.##########';
|
||
}
|
||
}
|
||
XLSX.utils.book_append_sheet(wb, detailWs, '每日明细');
|
||
}
|
||
|
||
const now = new Date();
|
||
const y = now.getFullYear();
|
||
const m = String(now.getMonth() + 1).padStart(2, '0');
|
||
const d = String(now.getDate()).padStart(2, '0');
|
||
const hh = String(now.getHours()).padStart(2, '0');
|
||
const mm = String(now.getMinutes()).padStart(2, '0');
|
||
const dateTag = start && end
|
||
? `${start.replace(/-/g, '')}-${end.replace(/-/g, '')}`
|
||
: `${y}${m}${d}`;
|
||
const filename = `里程看板_${dateTag}_${hh}${mm}_${ctx.sortBy === 'today' ? (isRange ? '区间' : '今日') : '累计'}.xlsx`;
|
||
XLSX.writeFile(wb, filename);
|
||
}
|