2026-10-14 09:23:29 +08:00
|
|
|
|
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'
|
2026-10-14 01:52:20 +08:00
|
|
|
|
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) {
|
2026-10-14 01:52:20 +08:00
|
|
|
|
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待核验`
|
|
|
|
|
|
}
|
2026-10-14 09:23:29 +08:00
|
|
|
|
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 {
|
2026-10-14 01:52:20 +08:00
|
|
|
|
aria: { enabled: true }, animation: typeof window === 'undefined' || !window.matchMedia?.('(prefers-reduced-motion: reduce)').matches,
|
|
|
|
|
|
animationDuration: 450, color: ['#6c7bea'],
|
2026-10-14 00:07:05 +08:00
|
|
|
|
tooltip: { trigger: 'axis', renderMode: 'richText' }, grid: { left: 12, right: 24, top: 16, bottom: 20, containLabel: true },
|
|
|
|
|
|
xAxis: owners ? { type: 'value', name: '件' } : { type: 'category', data: charts.averageDaily.map(w => `${w.days}日`) },
|
|
|
|
|
|
yAxis: owners ? { type: 'category', inverse: true, data: charts.ownerPhysicalTop.map(w => w.customerName || w.customerCode) } : { type: 'value', name: '件/日' },
|
|
|
|
|
|
series: [{ type: 'bar', barMaxWidth: 24, itemStyle: { borderRadius: 4 }, data: rows.map((_, i) => {
|
|
|
|
|
|
const value = owners ? charts.ownerPhysicalTop[i]?.physicalQuantity : charts.averageDaily[i]?.dataStatus === 'COMPLETE' ? charts.averageDaily[i]?.averageDailyOutboundQuantity : null
|
|
|
|
|
|
return value == null ? null : Number(value)
|
|
|
|
|
|
}) }],
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|