index.tsx
6.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import { useState } from 'react';
import { useMutation } from '@tanstack/react-query';
import { UAlert, UButton, UCard, UResult, UUpload, useUMessage } from '@uj/ui';
import { DataTable, SearchForm, type FieldSchema, type RowActionSchema } from '@/components';
import { pointsApi, type WriteOffOrder } from '@/api';
import { PP } from '@/permissions/codes';
import { usePointsAccess } from '@/features/shared/access';
import { SETTLE_STATUS, withAll } from '@/features/shared/dictionaries';
import { compactParams, errorMessage, expandDateRanges } from '@/features/shared/format';
import { isExcel, readLegacyFile, sizeError } from '@/features/shared/files';
import { firstPage, useExport, useList, usePageClamp, useRefresh } from '@/features/shared/queries';
import { PageShell } from '@/features/shared/PageShell';
import { useConfirmAction } from '@/features/shared/useConfirmAction';
import { buildOrderColumns } from './tools/columns';
import { OrderDetailDrawer } from './components/OrderDetailDrawer';
const SCOPE = ['points', 'orders'] as const;
/** 核销日期在表单里是一个区间,提交前展开成接口的起止两个字段(老 getParams 的等价物)。 */
const DATE_RANGES = [
{ from: 'verificationTimeStr', start: 'verificationStartTime', end: 'verificationEndTime' },
];
const FIELDS: FieldSchema<Record<string, unknown>>[] = [
{
name: 'bizRelName',
label: '项目名称',
type: 'input',
placeholder: '请输入项目名称',
inputProps: { maxLength: 100 },
essential: true,
},
{
name: 'merchantName',
label: '商户名称',
type: 'input',
placeholder: '请输入商户名称',
inputProps: { maxLength: 100 },
essential: true,
},
{
name: 'settlementStatus',
label: '结算状态',
type: 'select',
options: withAll(SETTLE_STATUS),
essential: true,
},
{ name: 'verificationTimeStr', label: '核销日期', type: 'dateRange' },
];
export default function OrderList() {
const { can } = usePointsAccess();
const message = useUMessage();
const allowed = can(PP.ORDER_VIEW);
const [filters, setFilters] = useState<Record<string, unknown>>({});
const [page, setPage] = useState(firstPage);
const [detailId, setDetailId] = useState<string>();
const refresh = useRefresh([...SCOPE]);
const list = useList<WriteOffOrder>({
scope: [...SCOPE],
filters,
page,
fetcher: pointsApi.pageOrders,
enabled: allowed,
});
usePageClamp(list.total, page, setPage);
const exportAction = useExport(pointsApi.exportOrders);
const settle = useConfirmAction(refresh);
const importAction = useMutation({
mutationFn: async (file: File) => {
const tooLarge = sizeError(file);
if (tooLarge) throw new Error(tooLarge);
if (!isExcel(file.name)) throw new Error('请选择 .xls 或 .xlsx 文件');
const payload = await readLegacyFile(file);
if (!payload) throw new Error('文件读取失败,请重新选择');
return pointsApi.importSettlement(payload);
},
onSuccess: async () => {
void message.success('导入成功,结算状态已更新');
await refresh();
},
onError: (error) => void message.error(errorMessage(error)),
});
if (!allowed) {
return (
<UResult status="403" title="无积分核销订单访问权限" subTitle="请联系管理员确认账号权限。" />
);
}
const columns = buildOrderColumns();
const rowActions: RowActionSchema<WriteOffOrder>[] = [
{ label: '查看', onClick: (row) => setDetailId(row.id) },
{
label: (row) => (Number(row.settlementStatus) === 1 ? '取消结算' : '去结算'),
visible: () => can(PP.ORDER_SETTLE),
onClick: (row) =>
settle.confirm({
title: Number(row.settlementStatus) === 1 ? '取消结算' : '结算',
content: `确定要${Number(row.settlementStatus) === 1 ? '取消结算' : '把该笔核销订单标记为已结算'}吗?`,
run: () =>
pointsApi.settle({
id: row.id,
settlementStatus: Number(row.settlementStatus) === 1 ? 0 : 1,
}),
}),
},
];
return (
<PageShell
fill
title="积分核销订单"
description="按项目与商户查询核销记录,逐笔结算或取消结算,也可用表格批量导入结算结果。"
actions={
<>
{can(PP.ORDER_IMPORT) && (
<UUpload
accept=".xls,.xlsx"
showUploadList={false}
maxCount={1}
disabled={importAction.isPending}
beforeUpload={(file) => {
importAction.mutate(file);
return false;
}}
>
<UButton loading={importAction.isPending}>批量结算导入</UButton>
</UUpload>
)}
{can(PP.ORDER_EXPORT) && (
<UButton
loading={exportAction.isPending}
onClick={() => exportAction.mutate({ ...filters })}
>
导出
</UButton>
)}
</>
}
>
<UCard className="uj-filter-card" size="small">
<SearchForm
fields={FIELDS}
initialValues={{
bizRelName: '',
merchantName: '',
settlementStatus: '',
}}
onSearch={(values) => {
setFilters(compactParams(expandDateRanges({ ...values }, DATE_RANGES)));
setPage(firstPage);
}}
onReset={() => {
setFilters({});
setPage(firstPage);
}}
/>
</UCard>
{list.query.isError ? (
<UAlert
type="error"
showIcon
title="核销订单加载失败"
description={list.error}
action={
<UButton type="link" onClick={() => void list.query.refetch()}>
重试
</UButton>
}
/>
) : null}
<UCard size="small" fill>
<DataTable<WriteOffOrder>
columns={columns}
rowKey="id"
fillHeight
actionColumnWidth={150}
dataSource={list.records}
loading={list.query.isPending || list.query.isFetching}
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>
{settle.modal}
<OrderDetailDrawer
open={!!detailId}
id={detailId ?? ''}
onClose={() => setDetailId(undefined)}
/>
</PageShell>
);
}