format.ts 7.85 KB
// 通用格式化与状态映射。tone:ok 绿 / warn 橙 / err 红 / info 灰 / primary 蓝。
export type Tone = 'ok' | 'warn' | 'err' | 'info' | 'primary'

export function fmtTime(v?: string | null): string {
  if (!v) return '—'
  return String(v).replace('T', ' ').slice(0, 19)
}

export function fmtShort(v?: string | null): string {
  if (!v) return '—'
  return String(v).replace('T', ' ').slice(5, 16)
}

export function fmtAge(sec?: number | null): string {
  if (sec === null || sec === undefined) return '从未'
  if (sec < 60) return `${Math.round(sec)} 秒前`
  if (sec < 3600) return `${Math.round(sec / 60)} 分钟前`
  if (sec < 86400) return `${Math.round(sec / 3600)} 小时前`
  return `${Math.round(sec / 86400)} 天前`
}

export function fmtDuration(sec?: number | null): string {
  if (sec === null || sec === undefined) return '—'
  if (sec < 60) return `${Math.round(sec)} 秒`
  if (sec < 3600) return `${Math.round(sec / 60)} 分钟`
  return `${(sec / 3600).toFixed(1)} 小时`
}

export function fmtMoney(v?: number | null): string {
  return v === null || v === undefined ? '—' : '¥' + Number(v).toFixed(2)
}

export function fmtSize(n?: number | null): string {
  if (!n) return '—'
  if (n < 1024) return `${n} B`
  if (n < 1024 * 1024) return `${(n / 1024).toFixed(1)} KB`
  return `${(n / 1024 / 1024).toFixed(1)} MB`
}

export const SEVERITY: Record<string, { text: string; tone: Tone }> = {
  info: { text: '提示', tone: 'info' }, warn: { text: '注意', tone: 'warn' },
  error: { text: '异常', tone: 'err' }, critical: { text: '紧急', tone: 'err' },
}

export const LEVEL_TONE: Record<string, Tone> = {
  info: 'info', ok: 'ok', warn: 'warn', error: 'err', INFO: 'info', WARNING: 'warn', ERROR: 'err',
  CRITICAL: 'err', DEBUG: 'info',
}

export const TASK_STATUS: Record<string, { text: string; tone: Tone }> = {
  queued: { text: '待领取', tone: 'info' }, dispatched: { text: '执行中', tone: 'primary' },
  done: { text: '完成', tone: 'ok' }, failed: { text: '失败', tone: 'err' },
  expired: { text: '没人领取', tone: 'warn' }, unknown: { text: '结果未知', tone: 'err' },
  late_done: { text: '迟到完成', tone: 'warn' }, late_failed: { text: '迟到失败', tone: 'err' },
}

export const TASK_TYPE: Record<string, string> = {
  'sys.ping': '连通测试', 'sys.check': '自检', 'sys.upload_logs': '上传日志', 'sys.collect_diag': '收集诊断', 'restock.scan': '补货查询(旧)',
  'restock.outbound': '零件出库明细', 'restock.quote': '查库存 / 主数据 / 全国仓', 'restock.so_sync': '同步近期 SO 单',
  'restock.create_po': '创建采购单', 'restock.screenshot_po': '采购单截图', 'restock.submit_po': '提交采购单',
  'restock.export_history': '历史数据导出',
  'sys.update': '版本更新', 'sys.cleanup': '清理本地日志', 'sys.restart': '重启取数端',
}

export const STEP_CN: Record<string, string> = {
  login: '登录 DMS', query_part: '查询配件', create_po: '创建采购单', verify_po: '回查采购单', screenshot: '截图',
  query_outbound: '拉零件出库明细', so_sync: '同步 SO 单', screenshot_fallback: '截图兜底(接口数据渲染)',
  upload: '上传文件', check: '自检', export: '导出数据', confirm: '等待人工确认', env: '采集环境', netdiag: '网络诊断',
  browser_probe: '浏览器对照测试', pack: '打包数据', download: '下载更新包', verify: '校验更新包', install: '安装新版本', cleanup: '清理本地日志',
}

export const STRATEGY: Record<string, { text: string; desc: string }> = {
  ui: { text: 'ui:浏览器模拟人工登录', desc: '打开 IAM 登录页,真实点击输入框、逐字输入账号密码、点登录;开启逐步确认时只在门店电脑 tool.bat 菜单 12 观看时执行' },
  token: { text: 'token:把已有登录交给浏览器', desc: '用取数端接口登录拿到的 token 打开页面,最快' },
  sso: { text: 'sso:浏览器自己走单点登录', desc: '把 IAM 登录 Cookie 交给浏览器,让 DMS 前端自己换 token' },
}

export const STAGE: Record<string, { text: string; tone: Tone }> = {
  scanning: { text: '取数中', tone: 'primary' }, scan_retry: { text: '取数失败·等待重试', tone: 'warn' },
  refreshing: { text: '刷新取数中', tone: 'primary' },
  awaiting_confirm: { text: '待确认', tone: 'warn' }, creating: { text: '建单中', tone: 'primary' },
  create_unknown: { text: '建单异常·核实中', tone: 'err' },
  screenshotting: { text: '截图中', tone: 'primary' },
  awaiting_submit: { text: '待确认提交', tone: 'warn' }, submitting: { text: '提交中', tone: 'primary' },
  done: { text: '已完成', tone: 'ok' },
  cancelled: { text: '不订', tone: 'info' }, error: { text: '出错', tone: 'err' },
  superseded: { text: '已作废', tone: 'info' }, expired: { text: '已过期', tone: 'err' },
  terminated: { text: '已终止', tone: 'info' },
}

export const KIND_CN: Record<string, string> = { scheduled: '定时补货', single: '单次补货' }

// 智能助手:技能 / 工具的中文名、被统一的闸拦下的原因(与云端 features/skills.py、guards.py 一致)
export const SKILL_CN: Record<string, string> = {
  'restock.part_query': '查库存', 'restock.single': '单次补货', 'restock.scheduled': '定时补货',
}
export const TOOL_CN: Record<string, string> = {
  query_part: '查库存', single_restock: '发起单次补货', confirm_order: '建单', submit_order: '提交', cancel_order: '不订 / 不提交',
  single_status: '单次补货进度', scheduled_status: '定时补货进度', scheduled_link: '定时补货单据页链接',
}
export const BLOCKED_CN: Record<string, string> = {
  from_text: '零件号在原话和聊天记录里都没有,没有执行',
}
// 智能助手对一句话的处理结果(与云端 services/aitrace.py RESULT_CN 一致)
export const AI_RESULT: Record<string, { text: string; tone: 'ok' | 'warn' | 'err' | 'info' | 'primary' }> = {
  tool: { text: '工具已回复', tone: 'ok' }, free_reply: { text: '大模型直接回复', tone: 'ok' },
  free_reply_blocked: { text: '回复里有长数字,改发引导语', tone: 'warn' }, refused: { text: '回了固定引导语', tone: 'info' },
  llm_unavailable: { text: '大模型不可用', tone: 'err' }, tool_then_llm_error: { text: '工具已回复,之后大模型出错', tone: 'warn' },
  no_skills: { text: '没有启用技能', tone: 'warn' },
}
// 一次工具调用的简写:「查库存 5165156」
export function callText(c: any): string {
  const args = Object.values(c?.args || {}).filter((v) => v !== '' && v != null).join(' ')
  return `${TOOL_CN[c?.tool] || c?.tool}${args ? ' ' + args : ''}`
}

// DMS 全国仓库名 → 给人看的叫法(与云端 planner.wh_label 一致)
export function whLabel(name: string): string {
  const m: Record<string, string> = { GTMC广州: '广州仓', 华南GTMC: '华南仓' }
  if (m[name]) return m[name]
  for (const k of ['广州', '华南']) if (name.includes(k)) return k + '仓'
  return name
}

export const TRIGGER: Record<string, string> = { chat: '企微对话', schedule: '定时推送', console: '手动补充' }
export const GROUP: Record<string, string> = { stock: '历史常备件', other: '其他出库件', single: '单次补货' }

export const LOGIN_STATE: Record<string, { text: string; tone: Tone }> = {
  ok: { text: '登录正常', tone: 'ok' }, failed: { text: '登录失败', tone: 'err' },
  rejected: { text: '账号密码被拒', tone: 'err' }, none: { text: '未登录', tone: 'info' },
}

export function lineClass(l: any): string {
  if (l.tag === 'error') return 'row-err'
  if (l.short_after || l.tag === 'oos') return 'row-red'
  if (l.wh_short || l.tag === 'capped') return 'row-orange'
  if (!l.seq) return 'row-grey'
  return ''
}

export function errMsg(e: any): string {
  return e?.message || String(e)
}

export const WEEKDAYS = ['一', '二', '三', '四', '五', '六', '日']