Files
admin-web/src/modules/self-operated/turnoverPresentation.ts
T

59 lines
4.3 KiB
TypeScript
Raw Normal View History

import type { Quantity, WarehouseOverview } from './types'
2026-10-14 00:07:05 +08:00
import type { TurnoverCharts, TurnoverTotal, TurnoverWindowMetric } from './turnoverTypes'
import type { EChartsOption } from 'echarts'
import { reasonsText } from './state'
const turnoverLabels: Record<string, string> = {
INSUFFICIENT: '合格日库存样本不足', ZERO_AVERAGE_STOCK: '每日观测未捕获存量,无法计算周转',
INVENTORY_UNKNOWN_OR_STALE: '当前库存未知、异常或已过期', NOT_APPLICABLE: '货主不计算整体可售天数',
SALES_OR_STOCK_UNASSESSABLE: '部分商品的销量或库存不足以判断',
SALES_WINDOW_UNAVAILABLE: '同期销量或商品观察证据不足',
}
export function turnoverReasonsText(reasons: readonly string[] | null | undefined) {
return reasons?.map(reason => turnoverLabels[reason] ?? reasonsText([reason])).join('、') || '—'
}
2026-10-14 00:07:05 +08:00
export function turnoverNumber(value: Quantity | undefined, decimals = 2) {
if (value == null || value === '' || !Number.isFinite(Number(value))) return '—'
const n = Number(value)
if (n > 0 && n < 10 ** -decimals) return `<${(10 ** -decimals).toFixed(decimals)}`
return n.toLocaleString('zh-CN', { maximumFractionDigits: decimals })
}
export function totalText(total: TurnoverTotal) {
if (total.value == null && total.knownSubtotal == null && total.unknownSkuCount === 0) return turnoverReasonsText(total.reasonCodes)
2026-10-14 00:07:05 +08:00
return total.value != null ? turnoverNumber(total.value) : `已知 ${turnoverNumber(total.knownSubtotal)};${total.unknownSkuCount} SKU待核验`
}
export function turnoverTotalNote(total: TurnoverTotal) {
if (total.value != null) return '已确认'
const known = total.knownSubtotal == null ? '暂无已知小计' : `已知小计 ${turnoverNumber(total.knownSubtotal)}`
return `${known} · ${total.unknownSkuCount} SKU待核验`
}
export function inventoryCollectionNotice(warehouse: WarehouseOverview | undefined) {
const task = warehouse?.currentTask
if (!task || !['DEAD', 'RETRY_WAIT'].includes(task.taskStatus) || task.latestAttempt?.status !== 'FAILED') return ''
const reasons: Record<string, string> = {
SELFOP_STOCK_DUPLICATE: '库存分页出现重复记录',
SELFOP_STOCK_COUNT_MISMATCH: '库存扫描条数与来源总数不符',
SELFOP_PAGE_TOTAL_CHANGED: '库存扫描过程中分页总数变化',
}
const reason = reasons[task.latestAttempt.errorCode ?? ''] ?? '库存采集未成功'
return `${task.taskStatus === 'RETRY_WAIT' ? '库存采集失败,等待重试' : '库存采集失败,已停止自动重试'}:${reason}。任务 ${task.taskNo}`
}
2026-10-14 00:07:05 +08:00
export function windowText(metric: TurnoverWindowMetric | undefined) {
if (!metric) return '—'
return metric.dataStatus === 'COMPLETE' ? turnoverNumber(metric.outboundQuantity) : `已采集 ${turnoverNumber(metric.observedQuantity)},覆盖不足`
}
export function turnoverChart(charts: TurnoverCharts, kind: 'owners' | 'daily'): EChartsOption {
const owners = kind === 'owners'
const rows = owners ? charts.ownerPhysicalTop : charts.averageDaily
return {
aria: { enabled: true }, animation: typeof window === 'undefined' || !window.matchMedia?.('(prefers-reduced-motion: reduce)').matches,
animationDuration: 450, color: ['#6c7bea'],
tooltip: { trigger: 'axis', renderMode: 'richText', valueFormatter: value => `${turnoverNumber(typeof value === 'number' || typeof value === 'string' ? value : null)} ${owners ? '件' : '件/日'}` }, grid: { left: 12, right: 40, top: owners ? 12 : 48, bottom: 12, containLabel: true },
2026-10-14 00:07:05 +08:00
xAxis: owners ? { type: 'value', name: '件' } : { type: 'category', data: charts.averageDaily.map(w => `${w.days}日`) },
2026-10-14 11:02:56 +08:00
yAxis: owners ? { type: 'category', inverse: true, data: charts.ownerPhysicalTop.map(w => w.customerName || w.customerCode) } : { type: 'value', name: '件/日', nameGap: 12, nameTextStyle: { align: 'left' } },
series: [{ type: 'bar', barMaxWidth: 24, itemStyle: { borderRadius: 4 }, label: { show: !owners, position: 'top', distance: 8, formatter: params => turnoverNumber(typeof params.value === 'number' ? params.value : null) }, data: rows.map((_, i) => {
2026-10-14 00:07:05 +08:00
const value = owners ? charts.ownerPhysicalTop[i]?.physicalQuantity : charts.averageDaily[i]?.dataStatus === 'COMPLETE' ? charts.averageDaily[i]?.averageDailyOutboundQuantity : null
return value == null ? null : Number(value)
}) }],
}
}