columns.tsx 11.6 KB
import { UTag } from '@uj/ui';
import type { TableProps } from 'antd';
import type { PolicyConfirmRow } from '../../tools/policyTableData';

const storeInfoHeaderStyle: React.CSSProperties = {
  background: '#EFF4FF',
  color: '#2B5CE6',
  fontWeight: 600,
  fontSize: 13,
};

const bizPolicyHeaderStyle: React.CSSProperties = {
  background: '#ECFDF5',
  color: '#047857',
  fontWeight: 600,
  fontSize: 13,
};

const costHeaderStyle: React.CSSProperties = {
  background: '#FFF7ED',
  color: '#C2410C',
  fontWeight: 600,
  fontSize: 13,
};

const cellStyle: React.CSSProperties = {
  fontSize: 12.5,
  color: '#374151',
  padding: '10px 12px',
};

function SceneTag({ text }: { text: string }) {
  return (
    <UTag
      color="processing"
      bordered={false}
      title={text}
      style={{
        fontSize: 12,
        maxWidth: '100%',
        whiteSpace: 'nowrap',
        overflow: 'hidden',
        textOverflow: 'ellipsis',
      }}
    >
      {text}
    </UTag>
  );
}

function CategoryTag({ text }: { text: string }) {
  return (
    <UTag
      color="processing"
      bordered={false}
      title={text}
      style={{
        fontSize: 12,
        maxWidth: '100%',
        whiteSpace: 'nowrap',
        overflow: 'hidden',
        textOverflow: 'ellipsis',
      }}
    >
      {text}
    </UTag>
  );
}

/** 金额列渲染(加粗深灰) */
function MoneyCell({ value }: { value: string }) {
  return <span style={{ fontWeight: 600, color: '#1F2937' }}>{value}</span>;
}

/** 激励费列(数值加粗) */
function FeeValueCell({ value }: { value: string }) {
  return <span style={{ fontWeight: 600, color: '#1F2937' }}>{value}</span>;
}

export function buildPolicyColumns(
  rows: PolicyConfirmRow[] = [],
): TableProps<PolicyConfirmRow>['columns'] {
  /**
   * 计算当前行某列的 rowSpan(同门店 / 同业务场景合并单元格):
   * - keyFn:对每行提取分组 key(如门店名、或「门店名|业务场景」复合键)
   * - 当前行与上一行 key 相同 → rowSpan = 0(被合并)
   * - 否则向下统计连续相同 key 的行数作为 rowSpan
   */
  const getRowSpan = (index: number, keyFn: (r: PolicyConfirmRow) => string): number => {
    if (index < 0 || index >= rows.length) return 1;
    const cur = keyFn(rows[index]);
    if (index > 0 && keyFn(rows[index - 1]) === cur) return 0;
    let span = 1;
    for (let i = index + 1; i < rows.length; i++) {
      if (keyFn(rows[i]) === cur) span++;
      else break;
    }
    return span;
  };

  return [
    // ------------- 第一组:门店信息(4 列) -------------
    {
      title: '门店信息',
      align: 'center',
      children: [
        {
          title: '门店名称',
          dataIndex: 'storeName',
          key: 'storeName',
          width: 200,
          onHeaderCell: () => ({ style: storeInfoHeaderStyle }),
          onCell: (_r, index) => ({
            rowSpan: getRowSpan(index ?? 0, (r) => r.storeName),
            style: cellStyle,
          }),
          render: (v) => <span style={{ color: '#1A202C', fontWeight: 500 }}>{v}</span>,
        },
        {
          title: '门店商户号',
          dataIndex: 'storeMerchantNo',
          key: 'storeMerchantNo',
          width: 130,
          align: 'center',
          onHeaderCell: () => ({ style: storeInfoHeaderStyle }),
          onCell: (_r, index) => ({
            // 同门店的商户号必然相同,按「门店名 + 商户号」复合键合并
            rowSpan: getRowSpan(index ?? 0, (r) => `${r.storeName}|${r.storeMerchantNo}`),
            style: cellStyle,
          }),
          render: (v) => <a style={{ color: '#2563EB', fontSize: 12.5 }}>{v}</a>,
        },
        {
          title: '业务场景',
          dataIndex: 'businessScene',
          key: 'businessScene',
          width: 130,
          align: 'center',
          onHeaderCell: () => ({ style: storeInfoHeaderStyle }),
          onCell: (_r, index) => ({
            // 同门店下相同业务场景才合并(不同门店同名场景不合并)
            rowSpan: getRowSpan(index ?? 0, (r) => `${r.storeName}|${r.businessScene}`),
            style: cellStyle,
          }),
          render: (v: string) => <SceneTag text={v} />,
        },
      ],
    },
    // ------------- 第二组:商务政策(14 列) -------------
    {
      title: '商务政策',
      align: 'center',
      children: [
        {
          title: '产品分类',
          dataIndex: 'productCategory',
          key: 'productCategory',
          width: 280,
          align: 'center',
          onHeaderCell: () => ({ style: bizPolicyHeaderStyle }),
          onCell: () => ({
            style: {
              ...cellStyle,
              // 确保 tableLayout=fixed 下,Tag 内容超长时单元格不会溢出到右侧列
              overflow: 'hidden',
              whiteSpace: 'nowrap',
            },
          }),
          render: (v: string) => <CategoryTag text={v} />,
        },
        // 管理层市场费 1
        {
          title: '管理层市场费1计算模式',
          dataIndex: 'mgmtFee1Mode',
          key: 'mgmtFee1Mode',
          width: 150,
          align: 'center',
          onHeaderCell: () => ({ style: bizPolicyHeaderStyle }),
          onCell: () => ({ style: cellStyle }),
        },
        {
          title: '管理层市场费1计算方式',
          dataIndex: 'mgmtFee1CalcMethod',
          key: 'mgmtFee1CalcMethod',
          width: 150,
          align: 'center',
          onHeaderCell: () => ({ style: bizPolicyHeaderStyle }),
          onCell: () => ({ style: cellStyle }),
        },
        {
          title: '管理层市场费1',
          dataIndex: 'mgmtFee1Value',
          key: 'mgmtFee1Value',
          width: 130,
          align: 'center',
          onHeaderCell: () => ({ style: bizPolicyHeaderStyle }),
          onCell: () => ({ style: cellStyle }),
          render: (v) => <FeeValueCell value={v as string} />,
        },
        // 管理层市场费 2
        {
          title: '管理层市场费2计算模式',
          dataIndex: 'mgmtFee2Mode',
          key: 'mgmtFee2Mode',
          width: 150,
          align: 'center',
          onHeaderCell: () => ({ style: bizPolicyHeaderStyle }),
          onCell: () => ({ style: cellStyle }),
        },
        {
          title: '管理层市场费2计算方式',
          dataIndex: 'mgmtFee2CalcMethod',
          key: 'mgmtFee2CalcMethod',
          width: 150,
          align: 'center',
          onHeaderCell: () => ({ style: bizPolicyHeaderStyle }),
          onCell: () => ({ style: cellStyle }),
        },
        {
          title: '管理层市场费2',
          dataIndex: 'mgmtFee2Value',
          key: 'mgmtFee2Value',
          width: 130,
          align: 'center',
          onHeaderCell: () => ({ style: bizPolicyHeaderStyle }),
          onCell: () => ({ style: cellStyle }),
          render: (v) => <FeeValueCell value={v as string} />,
        },
        // 一线阳光 1
        {
          title: '一线阳光是否开启线上积分',
          dataIndex: 'sunshineOnlinePoints',
          key: 'sunshineOnlinePoints',
          width: 170,
          align: 'center',
          onHeaderCell: () => ({ style: bizPolicyHeaderStyle }),
          onCell: (_r, index) => ({
            // 同门店同业务场景合并(与「业务场景」列合并粒度一致)
            rowSpan: getRowSpan(index ?? 0, (r) => `${r.storeName}|${r.businessScene}`),
            style: cellStyle,
          }),
        },
        {
          title: '一线阳光计算模式',
          dataIndex: 'sunshineCalcMode',
          key: 'sunshineCalcMode',
          width: 140,
          align: 'center',
          onHeaderCell: () => ({ style: bizPolicyHeaderStyle }),
          onCell: () => ({ style: cellStyle }),
        },
        {
          title: '一线阳光计算方式',
          dataIndex: 'sunshineCalcMethod',
          key: 'sunshineCalcMethod',
          width: 140,
          align: 'center',
          onHeaderCell: () => ({ style: bizPolicyHeaderStyle }),
          onCell: () => ({ style: cellStyle }),
        },
        {
          title: '一线阳光激励费',
          dataIndex: 'sunshineValue',
          key: 'sunshineValue',
          width: 140,
          align: 'center',
          onHeaderCell: () => ({ style: bizPolicyHeaderStyle }),
          onCell: () => ({ style: cellStyle }),
          render: (v) => <FeeValueCell value={v as string} />,
        },
        // 一线阳光 2
        {
          title: '一线阳光2是否开启线上积分',
          dataIndex: 'sunshine2OnlinePoints',
          key: 'sunshine2OnlinePoints',
          width: 180,
          align: 'center',
          onHeaderCell: () => ({ style: bizPolicyHeaderStyle }),
          onCell: (_r, index) => ({
            // 同门店同业务场景合并(与「业务场景」列合并粒度一致)
            rowSpan: getRowSpan(index ?? 0, (r) => `${r.storeName}|${r.businessScene}`),
            style: cellStyle,
          }),
        },
        {
          title: '一线阳光2计算模式',
          dataIndex: 'sunshine2CalcMode',
          key: 'sunshine2CalcMode',
          width: 150,
          align: 'center',
          onHeaderCell: () => ({ style: bizPolicyHeaderStyle }),
          onCell: () => ({ style: cellStyle }),
        },
        {
          title: '一线阳光2计算方式',
          dataIndex: 'sunshine2CalcMethod',
          key: 'sunshine2CalcMethod',
          width: 150,
          align: 'center',
          onHeaderCell: () => ({ style: bizPolicyHeaderStyle }),
          onCell: () => ({ style: cellStyle }),
        },
        {
          title: '一线阳光2激励费',
          dataIndex: 'sunshine2Value',
          key: 'sunshine2Value',
          width: 150,
          align: 'center',
          onHeaderCell: () => ({ style: bizPolicyHeaderStyle }),
          onCell: () => ({ style: cellStyle }),
          render: (v) => <FeeValueCell value={v as string} />,
        },
      ],
    },
    // ------------- 第三组:运营成本(3 列) -------------
    {
      title: '运营成本',
      align: 'center',
      children: [
        {
          title: '装修、设备总投入',
          dataIndex: 'costDecoration',
          key: 'costDecoration',
          width: 160,
          align: 'center',
          onHeaderCell: () => ({ style: costHeaderStyle }),
          onCell: (_r, index) => ({
            // 运营成本按「门店 + 业务场景」维度合并(同一门店下不同业务场景的运营成本不同)
            rowSpan: getRowSpan(index ?? 0, (r) => `${r.storeName}|${r.businessScene}`),
            style: cellStyle,
          }),
          render: (v) => <MoneyCell value={v as string} />,
        },
        {
          title: '当月产生(房租、水电、耗材等)',
          dataIndex: 'costMonthly',
          key: 'costMonthly',
          width: 220,
          align: 'center',
          onHeaderCell: () => ({ style: costHeaderStyle }),
          onCell: (_r, index) => ({
            rowSpan: getRowSpan(index ?? 0, (r) => `${r.storeName}|${r.businessScene}`),
            style: cellStyle,
          }),
          render: (v) => <MoneyCell value={v as string} />,
        },
        {
          title: '人工成本',
          dataIndex: 'costLabor',
          key: 'costLabor',
          width: 140,
          align: 'center',
          onHeaderCell: () => ({ style: costHeaderStyle }),
          onCell: (_r, index) => ({
            rowSpan: getRowSpan(index ?? 0, (r) => `${r.storeName}|${r.businessScene}`),
            style: cellStyle,
          }),
          render: (v) => <MoneyCell value={v as string} />,
        },
      ],
    },
  ];
}