import type { QuoteQueryLogQuotePreview, QuoteQueryLogRecord, } from "@/lib/frontend/types"; import { getMotherShipRateOptionLabel, getMotherShipServiceLevelLabel, } from "@/lib/constants/mothership-ui-tiers"; function clean(value: string | null | undefined): string { const t = (value ?? "").trim(); return !t || t === "—" ? "" : t; } /** 优先联想确认地址,否则客户填写地址 */ export function preferredAdminAddress( selected?: string | null, customer?: string | null, ): string { return clean(selected) || clean(customer); } export function buildQueryLogPickupCopy( row: Pick, ): string { return preferredAdminAddress(row.pickup_selected, row.pickup_customer); } export function buildQueryLogDeliveryCopy( row: Pick, ): string { return preferredAdminAddress(row.delivery_selected, row.delivery_customer); } export function buildQueryLogCargoCopy( row: Pick, ): string { return clean(row.cargo_summary); } export function formatQueryLogQuoteLine(q: QuoteQueryLogQuotePreview): string { const carrier = q.carrier.trim() || "承运商"; const level = getMotherShipServiceLevelLabel(q.service_level); const option = getMotherShipRateOptionLabel(q.service_level, q.rate_option); const price = typeof q.final_total === "number" && Number.isFinite(q.final_total) ? `USD ${q.final_total.toFixed(2)}` : "—"; return `${carrier} · ${level}/${option} · ${price}`; } export function buildQueryLogQuotesCopy( quotes: QuoteQueryLogQuotePreview[] | undefined, ): string { if (!quotes || quotes.length === 0) return ""; return quotes.map((q, i) => `${i + 1}. ${formatQueryLogQuoteLine(q)}`).join("\n"); } export function buildQueryLogFullCopy( row: Pick< QuoteQueryLogRecord, | "quote_id" | "customer_id" | "pickup_selected" | "pickup_customer" | "delivery_selected" | "delivery_customer" | "cargo_summary" | "quotes" | "quoted_carrier" | "quoted_total" >, ): string { const pickup = buildQueryLogPickupCopy(row); const delivery = buildQueryLogDeliveryCopy(row); const cargo = buildQueryLogCargoCopy(row); const quoteLines = buildQueryLogQuotesCopy(row.quotes); const lines = [ `报价单号:${row.quote_id}`, `客户:${row.customer_id}`, pickup ? `提货地址:${pickup}` : null, delivery ? `派送地址:${delivery}` : null, cargo ? `货物参数:${cargo}` : null, ]; if (quoteLines) { lines.push(`报价列表(${row.quotes!.length} 档):`, quoteLines); } else { if (row.quoted_carrier) { lines.push(`供应商:${row.quoted_carrier}`); } if ( typeof row.quoted_total === "number" && Number.isFinite(row.quoted_total) ) { lines.push(`报价:USD ${row.quoted_total.toFixed(2)}`); } } return lines.filter(Boolean).join("\n"); }