columns.tsx 5.74 KB
import type { DataTableColumnsType } from '@/components/DataTable';
import { UTag, UTooltip } from '@uj/ui';
import { StatusTag } from '@/components/StatusTag';
import type { ApprovalRow, ApproveTab } from '@/api';
import type { StoreDictionaries } from '@/features/store/dictionaries';
import { dictionaryLabel, formatDateTime, formatMoney } from '@/features/store/formatters';
import '../approveTable.css';

function formatSubmitTime(value?: string | null): string {
  const text = formatDateTime(value);
  const match =
    /^\d{4}-(\d{2}-\d{2}) (\d{2}:\d{2})(?::\d{2}(?:\.\d+)?)?(?:Z|[+-]\d{2}:?\d{2})?$/.exec(text);
  return match ? `${match[1]} ${match[2]}` : text;
}

export function buildApproveColumns(
  tab: ApproveTab,
  spec?: StoreDictionaries,
): DataTableColumnsType<ApprovalRow> {
  const columns: DataTableColumnsType<ApprovalRow> = [
    {
      title: '门店名称',
      dataIndex: 'storeName',
      width: tab === 'mine' ? 230 : 220,
      fixed: 'left',
      render: (value) => <strong>{value}</strong>,
    },
    {
      title: '门店商户号',
      dataIndex: 'merchantNo',
      width: tab === 'mine' ? 120 : 110,
      render: (value) => <span className="store-approve-merchant">{value || '—'}</span>,
    },
    {
      title: '产品分类',
      key: 'categories',
      width: tab === 'todo' ? 170 : 200,
      render: (_, row) => {
        const categories = row.productCategories ?? [];
        const count = row.categoryCount ?? categories.length;
        const categoryNames = categories
          .filter((category) => category.trim())
          .map((category) => dictionaryLabel(spec?.productCategories, category));
        return (
          <UTooltip
            trigger={['hover', 'focus']}
            title={
              categoryNames.length ? (
                <ul className="store-approve-category-list" aria-label="全部产品分类">
                  {categoryNames.map((name, index) => (
                    <li key={`${name}-${index}`}>{name}</li>
                  ))}
                </ul>
              ) : undefined
            }
          >
            <span
              className="store-approve-categories"
              tabIndex={categoryNames.length ? 0 : undefined}
            >
              {categoryNames[0] ? (
                <span className="store-approve-tag">{categoryNames[0]}</span>
              ) : (
                '—'
              )}
              {count > 1 && <span className="store-approve-category-count">等 {count} 个分类</span>}
            </span>
          </UTooltip>
        );
      },
    },
    {
      title: '业务场景',
      dataIndex: 'businessScene',
      width: tab === 'todo' ? 200 : tab === 'done' ? 150 : 160,
      render: (value) => (
        <span className="store-approve-tag">{dictionaryLabel(spec?.businessScenes, value)}</span>
      ),
    },
  ];
  if (tab === 'todo')
    columns.push(
      {
        title: '运营成本合计',
        dataIndex: 'costTotal',
        width: 110,
        align: 'right',
        render: (value) => (
          <span className="store-approve-money">
            {formatMoney(value).replace(/^¥/, '').replace(/\.00$/, '')}
          </span>
        ),
      },
      {
        title: '提交人 / 时间',
        width: 150,
        render: (_, row) => (
          <>
            {row.submitUserName || '—'}
            <span className="store-approve-submit-time">{formatSubmitTime(row.submitTime)}</span>
          </>
        ),
      },
      {
        title: '停留时长',
        width: 86,
        render: (_, row) => {
          const hours = row.pendingHours;
          if (typeof hours !== 'number' || !Number.isFinite(hours) || hours < 0) return '—';
          const level =
            row.timeoutLevel ?? (hours > 48 ? 'DANGER' : hours > 24 ? 'WARNING' : 'NORMAL');
          const tone =
            level === 'DANGER'
              ? 'danger'
              : level === 'WARNING'
                ? 'warning'
                : level === 'NORMAL'
                  ? 'normal'
                  : 'unknown';
          return (
            <span className={`store-approve-stay store-approve-stay--${tone}`}>{hours} 小时</span>
          );
        },
      },
    );
  if (tab === 'done')
    columns.push(
      {
        title: '我的动作',
        width: 110,
        render: (_, row) => (
          <UTag
            color={row.myAction === 'PASS' ? 'blue' : row.myAction === 'REJECT' ? 'red' : undefined}
          >
            {row.myActionLabel ||
              ({ PASS: '通过', REJECT: '打回' }[row.myAction ?? ''] ?? row.myAction ?? '—')}
          </UTag>
        ),
      },
      { title: '处理时间', dataIndex: 'handleTime', width: 150, render: formatDateTime },
      {
        title: '处理意见',
        key: 'handleOpinion',
        width: 220,
        line: 2,
        render: (_, row) => row.handleOpinion || row.rejectReason || '—',
      },
      { title: '记录状态', width: 100, render: (_, row) => <StatusTag status={row.status} /> },
    );
  if (tab === 'mine')
    columns.push(
      { title: '状态', width: 130, render: (_, row) => <StatusTag status={row.status} /> },
      {
        title: '审批进度',
        width: 150,
        render: (_, row) =>
          row.status === 'PENDING'
            ? (row.totalLevel ?? 2) === 1 || row.currentLevel === 2
              ? '待优加财务经理审批'
              : '待优加商务经理审批'
            : row.status === 'REJECTED'
              ? '退回发起人'
              : row.status === 'REVOKED'
                ? '发起人已撤回'
                : row.status === 'APPROVED'
                  ? '已完成'
                  : '—',
      },
      { title: '提交时间', dataIndex: 'submitTime', width: 170, render: formatDateTime },
    );
  return columns;
}