ValidationStats.tsx 3.76 KB
import { UCard } from '@uj/ui';
import type { StoreImportCheckVO } from '@/api/modules/store';

export interface StatItem {
  label: string;
  value: string | number;
  desc: string;
  tone: 'primary' | 'success' | 'warning' | 'default';
}

interface StatCardProps {
  item: StatItem;
}

const toneMap: Record<StatItem['tone'], { border: string; accent: string }> = {
  primary: {
    border: '1px solid #C7D2FE',
    accent: '#4F46E5',
  },
  success: {
    border: '1px solid #86EFAC',
    accent: '#16A34A',
  },
  warning: {
    border: '1px solid #FCD34D',
    accent: '#CA8A04',
  },
  default: {
    border: '1px solid #E5E7EB',
    accent: '#6B7280',
  },
};

function StatCard({ item }: StatCardProps) {
  const tone = toneMap[item.tone];
  return (
    <UCard
      styles={{ body: { padding: '18px 20px' } }}
      style={{
        borderLeft: `3px solid ${tone.accent}`,
        border: tone.border,
        borderRadius: 10,
        background: '#fff',
      }}
    >
      <div
        style={{
          fontSize: 13,
          color: '#6B7280',
          marginBottom: 10,
          fontWeight: 500,
        }}
      >
        {item.label}
      </div>
      <div
        style={{
          fontSize: 26,
          fontWeight: 700,
          color: '#1A202C',
          marginBottom: 8,
          lineHeight: 1.2,
        }}
      >
        {item.value}
      </div>
      <div
        style={{
          fontSize: 12,
          color: '#9CA3AF',
          lineHeight: 1.5,
        }}
      >
        {item.desc}
      </div>
    </UCard>
  );
}

export interface ValidationStatsProps {
  stats?: StatItem[];
  checkResult?: StoreImportCheckVO;
}

function calcOperatingCost(vo: StoreImportCheckVO): number {
  const rows = vo.previewRows ?? [];
  if (rows.length === 0) return 0;
  const first = rows[0];
  return (
    Math.round(first.costDecoration ?? 0) +
    Math.round(first.costMonthly ?? 0) +
    Math.round(first.costLabor ?? 0)
  );
}

function buildStatsFromCheckResult(vo: StoreImportCheckVO): StatItem[] {
  const rows = vo.productCategoryRowCount ?? vo.totalRows;
  const storeCount = 1;
  const sceneCount = vo.distinctBusinessSceneCount;
  const categoryCount = vo.distinctProductCategoryCount;
  const operating = calcOperatingCost(vo);

  const items: StatItem[] = [
    {
      label: '解析总行数',
      value: vo.totalRows,
      desc: `${storeCount} 家门店 · ${sceneCount} 个业务场景 · ${rows} 行`,
      tone: 'primary',
    },
    {
      label: '校验通过',
      value: vo.passRows,
      desc: vo.passRows > 0 ? '可提交审批' : '无通过数据',
      tone: 'success',
    },
  ];

  if (vo.failRows > 0) {
    items.push({
      label: '校验失败',
      value: vo.failRows,
      desc: vo.failFileUrl ? '可下载失败明细文件' : '需修正后重新上传',
      tone: 'warning',
    });
  }

  items.push({
    label: '运营成本合计',
    value: operating > 0 ? `¥ ${operating.toLocaleString('en-US')}` : '-',
    desc: '门店维度(装修设备 + 当月产生 + 人工成本)',
    tone: 'default',
  });

  return items;
}

const defaultStats: StatItem[] = [
  {
    label: '解析总行数',
    value: '-',
    desc: '上传文件后展示',
    tone: 'primary',
  },
  {
    label: '校验通过',
    value: '-',
    desc: '上传文件后展示',
    tone: 'success',
  },
];

export function ValidationStats({ stats, checkResult }: ValidationStatsProps) {
  const finalStats: StatItem[] = stats ?? (checkResult ? buildStatsFromCheckResult(checkResult) : defaultStats);
  return (
    <div
      style={{
        display: 'grid',
        gridTemplateColumns: 'repeat(4, minmax(0, 1fr))',
        gap: 16,
        marginBottom: 20,
      }}
    >
      {finalStats.map((s) => (
        <StatCard key={s.label} item={s} />
      ))}
    </div>
  );
}