| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252 |
- import type { PlanDelivery, PlanLoad, PlanProduct, PlanStopProduct } from '@/api/pl'
- import {
- appendAmapNavLine,
- buildAmapNavigationUrlWithFallback
- } from '@/utils/amap/navigation'
- import { formatDate } from '@/utils/format'
- import { formatPlanProductListText } from '@/utils/pl-display'
- export type DriverSummaryStopBlock = {
- label: string
- customerName?: string
- stopTime?: string
- address?: string
- lng?: number | null
- lat?: number | null
- contact?: string
- notes?: string
- productLines: string[]
- }
- type DriverSummaryPlan = {
- planNo?: string
- licensePlate?: string
- pickupTime?: string
- planDate?: string
- personnelSummary?: string
- pickupAddress?: string
- pickupLng?: number | null
- pickupLat?: number | null
- pickupContact?: string
- pickupNotes?: string
- destinationAddress?: string
- destinationName?: string
- destinationLng?: number | null
- destinationLat?: number | null
- deliveryContact?: string
- destinationNotes?: string
- startPoint?: string
- endPoint?: string
- load?: PlanLoad | null
- deliveries?: PlanDelivery[] | null
- }
- function formatDriverStopProductLine(
- product: PlanStopProduct,
- getUnitLabel?: (unit: string) => string
- ) {
- return formatPlanProductListText(
- {
- productName: product.productName,
- model: product.model,
- packageSpec: product.packageSpec,
- unit: product.unit,
- quantity: product.planQty
- },
- getUnitLabel,
- { usePlannedQuantity: true }
- )
- }
- function formatPlanLevelProductLines(
- products: PlanProduct[],
- getUnitLabel?: (unit: string) => string
- ) {
- return products
- .map((product) => formatPlanProductListText(product, getUnitLabel, { usePlannedQuantity: true }))
- .filter(Boolean)
- }
- function appendDriverStopBlockLines(lines: string[], stop: DriverSummaryStopBlock) {
- lines.push(`【${stop.label} · ${stop.customerName || '—'}】`)
- if (stop.stopTime) {
- lines.push(`时间:${stop.stopTime}`)
- }
- if (stop.address) {
- lines.push(`地址:${stop.address}`)
- }
- appendAmapNavLine(
- lines,
- '导航',
- buildAmapNavigationUrlWithFallback({
- lng: stop.lng,
- lat: stop.lat,
- address: stop.address,
- name: stop.label
- })
- )
- if (stop.contact) {
- lines.push(`联系方式:${stop.contact}`)
- }
- if (stop.notes) {
- lines.push(`注意事项:${stop.notes}`)
- }
- if (stop.productLines.length) {
- stop.productLines.forEach((line) => lines.push(`产品:${line}`))
- } else {
- lines.push('产品:—')
- }
- }
- export function buildDriverStopBlocksFromPlan(
- plan?: DriverSummaryPlan | null,
- getUnitLabel?: (unit: string) => string
- ): DriverSummaryStopBlock[] {
- if (!plan) return []
- const blocks: DriverSummaryStopBlock[] = []
- const load = plan.load
- if (load) {
- blocks.push({
- label: '装货',
- customerName: load.customerName || undefined,
- stopTime: formatDate(load.loadTime) || undefined,
- address: load.address || undefined,
- lng: load.lng,
- lat: load.lat,
- contact: load.contact || undefined,
- notes: load.notes || undefined,
- productLines: (load.products || []).map((product) =>
- formatDriverStopProductLine(product, getUnitLabel)
- )
- })
- }
- for (const delivery of plan.deliveries || []) {
- const seq = delivery.seq != null ? Number(delivery.seq) : 0
- blocks.push({
- label: seq > 0 ? `送货 #${seq}` : '送货',
- customerName: delivery.customerName || undefined,
- stopTime: formatDate(delivery.deliveryTime) || undefined,
- address: delivery.address || undefined,
- lng: delivery.lng,
- lat: delivery.lat,
- contact: delivery.contact || undefined,
- notes: delivery.notes || undefined,
- productLines: (delivery.products || []).map((product) =>
- formatDriverStopProductLine(product, getUnitLabel)
- )
- })
- }
- return blocks
- }
- function buildLegacyDriverStopBlocks(
- plan: DriverSummaryPlan,
- overrides: {
- pickupAddress?: string
- pickupLng?: number | null
- pickupLat?: number | null
- pickupContact?: string
- pickupNotes?: string
- destinationAddress?: string
- destinationLng?: number | null
- destinationLat?: number | null
- deliveryContact?: string
- destinationNotes?: string
- startPoint?: string
- endPoint?: string
- },
- productLines: string[]
- ): DriverSummaryStopBlock[] {
- const pickupAddress =
- overrides.pickupAddress || plan.pickupAddress || overrides.startPoint || plan.startPoint
- const destinationAddress =
- overrides.destinationAddress ||
- plan.destinationAddress ||
- plan.destinationName ||
- overrides.endPoint ||
- plan.endPoint
- const blocks: DriverSummaryStopBlock[] = []
- if (pickupAddress) {
- blocks.push({
- label: '取货',
- customerName: plan.load?.customerName || undefined,
- stopTime: formatDate(plan.pickupTime) || undefined,
- address: pickupAddress,
- lng: overrides.pickupLng ?? plan.pickupLng,
- lat: overrides.pickupLat ?? plan.pickupLat,
- contact: overrides.pickupContact || plan.pickupContact || undefined,
- notes: overrides.pickupNotes || plan.pickupNotes || undefined,
- productLines: productLines.length ? productLines : []
- })
- }
- if (destinationAddress) {
- blocks.push({
- label: '送货',
- customerName: undefined,
- stopTime: undefined,
- address: destinationAddress,
- lng: overrides.destinationLng ?? plan.destinationLng,
- lat: overrides.destinationLat ?? plan.destinationLat,
- contact: overrides.deliveryContact || plan.deliveryContact || undefined,
- notes: overrides.destinationNotes || plan.destinationNotes || undefined,
- productLines: []
- })
- }
- return blocks
- }
- export function buildDriverSummaryFromPlan(options: {
- plan?: DriverSummaryPlan | null
- planNo?: string
- licensePlate?: string
- pickupTime?: string
- planDate?: string
- personnelSummary?: string
- driverUrl?: string
- products?: PlanProduct[]
- stopBlocks?: DriverSummaryStopBlock[]
- getUnitLabel?: (unit: string) => string
- pickupAddress?: string
- pickupLng?: number | null
- pickupLat?: number | null
- destinationAddress?: string
- destinationLng?: number | null
- destinationLat?: number | null
- pickupContact?: string
- pickupNotes?: string
- deliveryContact?: string
- destinationNotes?: string
- startPoint?: string
- endPoint?: string
- }) {
- const plan = options.plan || {}
- const plate = options.licensePlate || plan.licensePlate || '—'
- const lines = [`【${plate} - 运输计划】`]
- lines.push(`计划号:${options.planNo || plan.planNo || '—'}`)
- lines.push(`车牌:${plate}`)
- const time = formatDate(options.pickupTime || plan.pickupTime) || options.planDate || plan.planDate
- if (time) lines.push(`时间:${time}`)
- const productLines = formatPlanLevelProductLines(options.products || [], options.getUnitLabel)
- let stopBlocks =
- options.stopBlocks ||
- buildDriverStopBlocksFromPlan(plan, options.getUnitLabel)
- if (!stopBlocks.length) {
- stopBlocks = buildLegacyDriverStopBlocks(plan, options, productLines)
- }
- if (stopBlocks.length) {
- lines.push('')
- stopBlocks.forEach((stop, index) => {
- if (index > 0) lines.push('')
- appendDriverStopBlockLines(lines, stop)
- })
- } else if (productLines.length) {
- lines.push('')
- lines.push(`产品:${productLines.join(';')}`)
- }
- lines.push('')
- lines.push(`司机链接:${options.driverUrl || '—'}`)
- return lines.join('\n')
- }
|