ValidationStats.tsx 3.25 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: '#2B5CE6',
  },
  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 buildStatsFromCheckResult(vo: StoreImportCheckVO): StatItem[] {
  const rows = vo.productCategoryRowCount ?? vo.totalRows;
  const storeCount = 1;
  const sceneCount = vo.distinctBusinessSceneCount;

  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',
    });
  }

  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);
  // 按实际卡片数量平分整行宽度
  const colCount = Math.max(1, finalStats.length);
  return (
    <div
      style={{
        display: 'grid',
        gridTemplateColumns: `repeat(${colCount}, minmax(0, 1fr))`,
        gap: 16,
        marginBottom: 20,
      }}
    >
      {finalStats.map((s) => (
        <StatCard key={s.label} item={s} />
      ))}
    </div>
  );
}