index.tsx 4.61 KB
import { useMemo, useState } from 'react';
import { useMutation } from '@tanstack/react-query';
import { UAlert, UButton, UCard, UResult } from '@uj/ui';
import { DataTable, SearchForm, type RowActionSchema } from '@/components';
import { withdrawApi, type WithdrawRow } from '@/api';
import { PP } from '@/permissions/codes';
import { usePointsAccess } from '@/features/shared/access';
import { compactParams, errorMessage } from '@/features/shared/format';
import {
  firstPage,
  useActionMessage,
  useList,
  usePageClamp,
  useRefresh,
} from '@/features/shared/queries';
import { PageShell } from '@/features/shared/PageShell';
import {
  APPROVAL_TRANSFER_FIELDS,
  buildApprovalTransferColumns,
} from '@/features/shared/approvalTransfer';
import type { PageParams } from '@/routes';

/** 只有「受理失败」这一状态的记录能再次打款(老系统在到账情况那一格里放按钮)。 */
const PAYABLE_AGAIN = '6';

/**
 * 到账明细:一个批次里每一笔提现的到账情况。
 *
 * 「再次打款」在老系统是直接调用、只给一条提示,这里保持同样的动作强度,
 * 成功后照老系统那样清掉筛选条件重新取数,避免刚发起的记录被旧条件挡住。
 */
export default function WithdrawArrival({ params }: { params: PageParams }) {
  const { can } = usePointsAccess();
  const allowed = can(PP.APPROVAL_VIEW);
  const id = params.id ?? '';
  const notices = useActionMessage();
  const [filters, setFilters] = useState<Record<string, unknown>>({});
  const [page, setPage] = useState(firstPage);
  const refresh = useRefresh(['points', 'arrival', id]);
  const list = useList<WithdrawRow>({
    scope: ['points', 'arrival', id],
    filters: { ...filters, approvalId: id },
    page,
    fetcher: withdrawApi.pageTransfers,
    enabled: allowed && !!id,
  });
  usePageClamp(list.total, page, setPage);

  const payAgain = useMutation({
    mutationFn: (row: WithdrawRow) => withdrawApi.payAgain(row.id),
    onSuccess: async () => {
      void notices.success('再次打款成功');
      setFilters({});
      setPage(firstPage);
      await refresh();
    },
    onError: (error) => notices.fail(error),
  });

  const columns = useMemo(() => buildApprovalTransferColumns('arrival'), []);

  if (!allowed) {
    return (
      <UResult status="403" title="无积分提现审批访问权限" subTitle="请联系管理员确认账号权限。" />
    );
  }
  if (!id) {
    return (
      <UResult
        status="warning"
        title="缺少批次标识"
        subTitle="请从积分提现审批列表点击进入到账明细。"
      />
    );
  }

  const rowActions: RowActionSchema<WithdrawRow>[] = [
    {
      label: '再次打款',
      visible: (row) => can(PP.ARRIVAL_PAY_AGAIN) && String(row.status ?? '') === PAYABLE_AGAIN,
      onClick: (row) => payAgain.mutate(row),
    },
  ];

  return (
    <PageShell
      fill
      title="到账明细"
      description="这一个批次逐笔提现的到账情况;受理失败的记录可以重新发起打款。"
      back="withdraw"
    >
      <UCard className="uj-filter-card" size="small">
        <SearchForm
          fields={APPROVAL_TRANSFER_FIELDS}
          initialValues={{ customerName: '', customerPhone: '' }}
          onSearch={(values) => {
            setFilters(compactParams(values));
            setPage(firstPage);
          }}
          onReset={() => {
            setFilters({});
            setPage(firstPage);
          }}
        />
      </UCard>
      {list.query.isError ? (
        <UAlert
          type="error"
          showIcon
          title="到账明细加载失败"
          description={errorMessage(list.error)}
          action={
            <UButton type="link" onClick={() => void list.query.refetch()}>
              重试
            </UButton>
          }
        />
      ) : null}
      <UCard size="small" fill>
        <DataTable<WithdrawRow>
          columns={columns}
          rowKey="id"
          fillHeight
          actionColumnWidth={110}
          dataSource={list.records}
          loading={list.query.isPending || list.query.isFetching || payAgain.isPending}
          rowActions={rowActions}
          toolbarTitle={`共 ${list.total} 条到账记录`}
          pagination={{
            current: page.current,
            pageSize: page.size,
            total: list.total,
            showSizeChanger: true,
            pageSizeOptions: [10, 20, 50, 100],
            showTotal: (total) => `共 ${total} 条`,
            onChange: (current, size) =>
              setPage({ current: size === page.size ? current : 1, size }),
          }}
        />
      </UCard>
    </PageShell>
  );
}