columns.tsx 2.65 KB
import type { ColumnsType } from 'antd/es/table';
import { UButton, UTag } from '@uj/ui';
import { P, Perm } from '@/access';
import type { ListingPageParams, StoreListingRow } from '@/api';
import type { StoreDictionaries } from '@/features/store/dictionaries';
import { dictionaryLabel, formatDateTime } from '@/features/store/formatters';
import { COLUMN_LABELS, type ListingColumn } from '../tools/listing';

export function buildColumns({
  visible,
  params,
  spec,
  onDetail,
  onHistory,
}: {
  visible: ListingColumn[];
  params: ListingPageParams;
  spec?: StoreDictionaries;
  onDetail: (row: StoreListingRow) => void;
  onHistory: (row: StoreListingRow) => void;
}): ColumnsType<StoreListingRow> {
  const sortOrder = params.sortOrder === 'asc' ? 'ascend' : 'descend';
  const definitions: Record<ListingColumn, ColumnsType<StoreListingRow>[number]> = {
    storeName: {
      title: COLUMN_LABELS.storeName,
      dataIndex: 'storeName',
      key: 'storeName',
      width: 230,
      fixed: 'left',
      render: (value: string, row) => (
        <UButton type="link" className="store-listing-name" onClick={() => onDetail(row)}>
          {value || '—'}
        </UButton>
      ),
    },
    merchantNo: {
      title: COLUMN_LABELS.merchantNo,
      dataIndex: 'merchantNo',
      key: 'merchantNo',
      width: 170,
      render: (value: string) => <span className="store-code">{value || '—'}</span>,
    },
    businessScene: {
      title: COLUMN_LABELS.businessScene,
      dataIndex: 'businessScene',
      key: 'businessScene',
      width: 210,
      render: (value: string) => (
        <UTag color="blue">{dictionaryLabel(spec?.businessScenes, value)}</UTag>
      ),
    },
    effectiveTime: {
      title: COLUMN_LABELS.effectiveTime,
      dataIndex: 'effectiveTime',
      key: 'effectiveTime',
      width: 150,
      sorter: true,
      sortOrder: params.sortField === 'effectiveDate' ? sortOrder : null,
      render: (value: string | null) => formatDateTime(value).split(' ')[0],
    },
    changeCount: {
      title: COLUMN_LABELS.changeCount,
      dataIndex: 'changeCount',
      key: 'changeCount',
      width: 120,
      align: 'right',
      sorter: true,
      sortOrder: params.sortField === 'changeCount' ? sortOrder : null,
      render: (value: number | null | undefined, row) => (
        <Perm permission={P.STORE_LISTING_VIEW} fallback={value ?? '—'}>
          <UButton
            type="link"
            aria-label={`查看${row.storeName}变更历史`}
            onClick={() => onHistory(row)}
          >
            {value ?? '—'}
          </UButton>
        </Perm>
      ),
    },
  };
  return visible.map((column) => definitions[column]);
}