|
|
@@ -0,0 +1,147 @@
|
|
|
+import type { CertRead } from '@/api/pl'
|
|
|
+import { formatPlVehiclePlateDisplay } from '@/utils/pl-display'
|
|
|
+import {
|
|
|
+ PL_VEHICLE_CERT_PART_TRAILER,
|
|
|
+ normalizeVehicleCertPart
|
|
|
+} from '@/utils/pl-vehicle-cert'
|
|
|
+
|
|
|
+export type VehicleCertZipSource = {
|
|
|
+ id: string
|
|
|
+ licensePlate?: string | null
|
|
|
+ trailerNo?: string | null
|
|
|
+ certList?: CertRead[] | null
|
|
|
+}
|
|
|
+
|
|
|
+export type VehicleCertZipFile = {
|
|
|
+ fileUrl: string
|
|
|
+ baseName: string
|
|
|
+ ext: string
|
|
|
+}
|
|
|
+
|
|
|
+export type VehicleCertZipEntry = {
|
|
|
+ vehicleId: string
|
|
|
+ plateFolder: string
|
|
|
+ files: VehicleCertZipFile[]
|
|
|
+}
|
|
|
+
|
|
|
+export function sanitizeZipPathSegment(raw: string): string {
|
|
|
+ return String(raw || '')
|
|
|
+ .trim()
|
|
|
+ .replace(/[\\/:*?"<>|]+/g, '_')
|
|
|
+ .replace(/\s+/g, ' ')
|
|
|
+ .trim() || '未命名'
|
|
|
+}
|
|
|
+
|
|
|
+/** Mirrors `normalizeAttachmentUrls` in download-attachment.ts (inlined for node:test). */
|
|
|
+function normalizeAttachmentUrls(raw?: string | null): string {
|
|
|
+ if (!raw) return ''
|
|
|
+ const trimmed = String(raw).trim()
|
|
|
+ if (!trimmed) return ''
|
|
|
+ if (trimmed.startsWith('[')) {
|
|
|
+ try {
|
|
|
+ const parsed = JSON.parse(trimmed)
|
|
|
+ if (Array.isArray(parsed)) {
|
|
|
+ return parsed.map((url) => String(url).trim()).filter(Boolean).join(',')
|
|
|
+ }
|
|
|
+ } catch {
|
|
|
+ /* ignore */
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return trimmed
|
|
|
+}
|
|
|
+
|
|
|
+function parseUrls(raw?: string | null): string[] {
|
|
|
+ const normalized = normalizeAttachmentUrls(raw)
|
|
|
+ if (!normalized) return []
|
|
|
+ return normalized
|
|
|
+ .split(',')
|
|
|
+ .map((item) => item.trim())
|
|
|
+ .filter(Boolean)
|
|
|
+}
|
|
|
+
|
|
|
+function getFileExt(url: string): string {
|
|
|
+ const name =
|
|
|
+ String(url || '')
|
|
|
+ .split('?')[0]
|
|
|
+ .split('/')
|
|
|
+ .pop() || ''
|
|
|
+ const lastDot = name.lastIndexOf('.')
|
|
|
+ if (lastDot === -1) return ''
|
|
|
+ return name.slice(lastDot + 1).toLowerCase()
|
|
|
+}
|
|
|
+
|
|
|
+function partPrefix(cert: CertRead, enablePart: boolean): string {
|
|
|
+ if (!enablePart) return ''
|
|
|
+ return normalizeVehicleCertPart(cert.vehiclePart) === PL_VEHICLE_CERT_PART_TRAILER
|
|
|
+ ? '挂·'
|
|
|
+ : '主·'
|
|
|
+}
|
|
|
+
|
|
|
+export function collectVehicleCertZipEntries(
|
|
|
+ vehicles: VehicleCertZipSource[]
|
|
|
+): VehicleCertZipEntry[] {
|
|
|
+ const result: VehicleCertZipEntry[] = []
|
|
|
+
|
|
|
+ for (const vehicle of vehicles) {
|
|
|
+ const certs = vehicle.certList || []
|
|
|
+ const enablePart = certs.some(
|
|
|
+ (cert) => normalizeVehicleCertPart(cert.vehiclePart) === PL_VEHICLE_CERT_PART_TRAILER
|
|
|
+ )
|
|
|
+ const files: VehicleCertZipFile[] = []
|
|
|
+
|
|
|
+ certs.forEach((cert) => {
|
|
|
+ const certName = String(cert.certName || '').trim() || '证书'
|
|
|
+ const prefix = partPrefix(cert, enablePart)
|
|
|
+ const baseName = `${prefix}${certName}`
|
|
|
+ parseUrls(cert.attachmentUrls).forEach((fileUrl) => {
|
|
|
+ files.push({
|
|
|
+ fileUrl,
|
|
|
+ baseName,
|
|
|
+ ext: getFileExt(fileUrl)
|
|
|
+ })
|
|
|
+ })
|
|
|
+ })
|
|
|
+
|
|
|
+ if (!files.length) continue
|
|
|
+
|
|
|
+ const plateRaw = formatPlVehiclePlateDisplay(vehicle.licensePlate, vehicle.trailerNo)
|
|
|
+ result.push({
|
|
|
+ vehicleId: vehicle.id,
|
|
|
+ plateFolder: sanitizeZipPathSegment(plateRaw === '—' ? vehicle.id : plateRaw),
|
|
|
+ files
|
|
|
+ })
|
|
|
+ }
|
|
|
+
|
|
|
+ return result
|
|
|
+}
|
|
|
+
|
|
|
+export function buildVehicleCertZipFilename(
|
|
|
+ entries: Array<{ plateFolder: string }>,
|
|
|
+ yyyymmdd: string
|
|
|
+): string {
|
|
|
+ if (entries.length === 1) {
|
|
|
+ return `车辆证书_${entries[0].plateFolder}.zip`
|
|
|
+ }
|
|
|
+ return `车辆证书批量_${entries.length}辆_${yyyymmdd}.zip`
|
|
|
+}
|
|
|
+
|
|
|
+export function uniqueZipPath(used: Set<string>, relativePath: string): string {
|
|
|
+ if (!used.has(relativePath)) {
|
|
|
+ used.add(relativePath)
|
|
|
+ return relativePath
|
|
|
+ }
|
|
|
+ const lastSlash = relativePath.lastIndexOf('/')
|
|
|
+ const dir = lastSlash >= 0 ? relativePath.slice(0, lastSlash + 1) : ''
|
|
|
+ const name = lastSlash >= 0 ? relativePath.slice(lastSlash + 1) : relativePath
|
|
|
+ const lastDot = name.lastIndexOf('.')
|
|
|
+ const stem = lastDot > 0 ? name.slice(0, lastDot) : name
|
|
|
+ const ext = lastDot > 0 ? name.slice(lastDot) : ''
|
|
|
+ let i = 2
|
|
|
+ let candidate = `${dir}${stem}_${i}${ext}`
|
|
|
+ while (used.has(candidate)) {
|
|
|
+ i += 1
|
|
|
+ candidate = `${dir}${stem}_${i}${ext}`
|
|
|
+ }
|
|
|
+ used.add(candidate)
|
|
|
+ return candidate
|
|
|
+}
|