approval-fields.ts 3.47 KB
import type { FieldSchema } from '@/components';
import type { DictOption } from '@/api';
import { compactParams, expandDateRanges } from '@/features/shared/format';
import { APPROVAL_STATUS, statusOptions, withAll } from '@/features/shared/dictionaries';

/**
 * 积分提现审批的筛选区(字段名与顺序对齐老系统 approve.vue 的 showFormData)。
 *
 * 老系统「付款渠道」是一个三层级联(渠道 → 付款主体 → 代征主体),
 * 提交的是级联第 2、3 层的 id:paymentSubjectId 与 paymentChannelId(后者存的是代征主体,字段名沿用接口)。
 * 现系统的 SearchForm 还不支持级联懒加载,因此拆成三个下拉,选了上一层才拉下一层,提交值与老系统逐字相同。
 */

const DATE_RANGES = [
  { from: 'approvalTime', start: 'approvalTimeStart', end: 'approvalTimeEnd' },
  { from: 'oaApprovalTime', start: 'oaApprovalTimeStart', end: 'oaApprovalTimeEnd' },
  { from: 'userSettleTime', start: 'userSettleTimeStart', end: 'userSettleTimeEnd' },
];

/** `channel` 只是第一个下拉的联动用字段,接口没有这个入参,提交前去掉。 */
export function toApprovalParams(values: Record<string, unknown>): Record<string, unknown> {
  const params = expandDateRanges({ ...values }, DATE_RANGES);
  delete params.channel;
  return compactParams(params);
}

export interface ApprovalFieldDeps {
  channels: DictOption[];
  entities: DictOption[];
  levies: DictOption[];
  /** 已选中的上层,用于禁用还没到手的下层(老级联框也是选完上一层才展开下一层)。 */
  channel?: string;
  entity?: string;
  /** 换上层要清掉下层的选择,否则提交的是「老主体 + 新渠道」这种组合。 */
  onChannelChange: (channel?: string) => void;
  onEntityChange: (entity?: string) => void;
}

export function buildApprovalFields(
  deps: ApprovalFieldDeps,
): FieldSchema<Record<string, unknown>>[] {
  return [
    {
      name: 'batchNo',
      label: '批次号',
      type: 'input',
      placeholder: '请输入批次号',
      inputProps: { maxLength: 20 },
      essential: true,
    },
    {
      name: 'oaSpNo',
      label: 'OA审批编号',
      type: 'input',
      placeholder: '请输入OA审批编号',
      inputProps: { maxLength: 20 },
      essential: true,
    },
    {
      name: 'channel',
      label: '付款渠道',
      type: 'select',
      options: withAll(deps.channels),
      selectProps: {
        onChange: (value) => {
          deps.onChannelChange(value === undefined || value === '' ? undefined : String(value));
          deps.onEntityChange(undefined);
        },
      },
    },
    {
      name: 'paymentSubjectId',
      label: '付款主体',
      type: 'select',
      options: withAll(deps.entities),
      disabled: !deps.channel,
      selectProps: {
        onChange: (value) =>
          deps.onEntityChange(value === undefined || value === '' ? undefined : String(value)),
      },
    },
    {
      name: 'paymentChannelId',
      label: '代征主体',
      type: 'select',
      options: withAll(deps.levies),
      disabled: !deps.entity,
    },
    {
      name: 'status',
      label: '状态',
      type: 'select',
      options: withAll(statusOptions(APPROVAL_STATUS)),
      essential: true,
    },
    { name: 'approvalTime', label: '审批时间', type: 'dateRange' },
    { name: 'oaApprovalTime', label: 'OA审批时间', type: 'dateRange' },
    { name: 'userSettleTime', label: '付款时间', type: 'dateRange' },
  ];
}