import type { BoatRoutePayload, BoatWaypoint, BoatWaypointAction } from '@/services/boatRoute'; const toNumber = (value: any, fallback?: number) => { if (value === undefined || value === null || value === '') return fallback; const num = Number(value); return Number.isFinite(num) ? num : fallback; }; const normalizeAction = (action: any): BoatWaypointAction => ({ actionType: action?.actionType, duration: toNumber(action?.duration, 0), params: action?.params ?? undefined, }); export const normalizeWaypoint = (waypoint: any, index: number): BoatWaypoint => ({ waypointId: waypoint?.waypointId, waypointIndex: toNumber(waypoint?.waypointIndex, index), longitude: toNumber(waypoint?.longitude), latitude: toNumber(waypoint?.latitude), speed: toNumber(waypoint?.speed), stayDuration: toNumber(waypoint?.stayDuration, 0), headingAngle: toNumber(waypoint?.headingAngle), turnMode: waypoint?.turnMode || 'smooth', isKeyPoint: Boolean(waypoint?.isKeyPoint), pointName: waypoint?.pointName || `航点${index + 1}`, actions: Array.isArray(waypoint?.actions) ? waypoint.actions.map(normalizeAction) : [], remark: waypoint?.remark || undefined, }); export const parseBoatRouteJson = (jsonText: string, routeName: string): BoatRoutePayload => { const source = JSON.parse(jsonText); const waypoints = Array.isArray(source?.waypoints) ? source.waypoints.map((item: any, index: number) => normalizeWaypoint(item, index)) : []; if (!waypoints.length) { throw new Error('JSON 文件中未找到 waypoints 数组'); } const invalidIndex = waypoints.findIndex((item: BoatWaypoint) => item.longitude === undefined || item.latitude === undefined); if (invalidIndex >= 0) { throw new Error(`第 ${invalidIndex + 1} 个航点缺少经纬度`); } return { routeName, deviceSn: source?.deviceSn, scenicId: toNumber(source?.scenicId), cruiseSpeed: toNumber(source?.cruiseSpeed, 2), loopEnabled: Boolean(source?.loopEnabled), loopCount: toNumber(source?.loopCount, 1), returnMode: source?.returnMode || 'return_start', safeDistance: toNumber(source?.safeDistance, 5), boundaryId: source?.boundaryId, remark: source?.remark, showInModel: toNumber(source?.showInModel, 0), waypoints, }; }; export const buildUpdatePayload = (detail: any, waypoints: BoatWaypoint[]): Partial => ({ routeName: detail.routeName, cruiseSpeed: toNumber(detail.cruiseSpeed, 2), loopEnabled: Boolean(detail.loopEnabled), loopCount: toNumber(detail.loopCount, 1), returnMode: detail.returnMode || 'return_start', safeDistance: toNumber(detail.safeDistance, 5), boundaryId: detail.boundaryId, remark: detail.remark, showInModel: toNumber(detail.showInModel, 0), waypoints: waypoints.map((item, index) => normalizeWaypoint(item, index)), });