551 lines
17 KiB
TypeScript
551 lines
17 KiB
TypeScript
import { ReloadOutlined, SaveOutlined, SearchOutlined } from '@ant-design/icons'
|
|
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
|
|
import {
|
|
Button,
|
|
Input,
|
|
InputNumber,
|
|
Select,
|
|
Space,
|
|
Switch,
|
|
Tag,
|
|
Tooltip,
|
|
Typography,
|
|
message,
|
|
type TableColumnsType,
|
|
} from 'antd'
|
|
import { useEffect, useMemo, useState } from 'react'
|
|
|
|
import PercentTable from '@/components/common/PercentTable'
|
|
import {
|
|
fetchVoucherConsumeJobs,
|
|
fetchVoucherConsumePolicy,
|
|
retryVoucherConsumeJob,
|
|
saveVoucherConsumePolicy,
|
|
type VoucherChannelConsumePolicy,
|
|
type VoucherConsumeChannel,
|
|
type VoucherConsumeJob,
|
|
type VoucherConsumeMode,
|
|
type VoucherConsumePolicy,
|
|
type VoucherConsumeProvider,
|
|
} from '@/services/admin'
|
|
import { ADMIN_DEFAULT_PAGE_SIZE, buildAdminTablePagination } from '@/utils/admin-pagination'
|
|
import { formatDateTime } from '@/utils/date-time'
|
|
|
|
const PROVIDER_LABEL: Record<VoucherConsumeProvider, string> = {
|
|
kuaishou_industry: '快手电子凭证',
|
|
douyin_mashida: '抖音码速达',
|
|
}
|
|
|
|
const CHANNEL_LABEL: Record<VoucherConsumeChannel, string> = {
|
|
order_received: '订单级触发',
|
|
affiliate_dash: '自动发货',
|
|
work_order: '接单工单',
|
|
manual: '人工客服',
|
|
on_account: '统一结算',
|
|
third_party_voucher: '第三方卡券',
|
|
unmatched: '未命中渠道兜底',
|
|
}
|
|
|
|
const MODE_OPTIONS: Record<
|
|
VoucherConsumeChannel,
|
|
Array<{ value: VoucherConsumeMode; label: string }>
|
|
> = {
|
|
order_received: [
|
|
{ value: 'after_order_received', label: '收到订单后' },
|
|
{ value: 'manual', label: '人工核销' },
|
|
],
|
|
affiliate_dash: [
|
|
{ value: 'after_order_received', label: '收到订单后' },
|
|
{ value: 'after_link_generated', label: '领取链接生成后' },
|
|
{ value: 'after_task_created', label: '履约任务创建后' },
|
|
{ value: 'manual', label: '人工核销' },
|
|
],
|
|
work_order: [
|
|
{ value: 'after_order_received', label: '收到订单后' },
|
|
{ value: 'after_work_order_published_or_assigned', label: '发布或指派后' },
|
|
{ value: 'after_work_order_published', label: '仅发布后' },
|
|
{ value: 'after_work_order_assigned', label: '仅指派后' },
|
|
{ value: 'after_task_created', label: '履约任务创建后' },
|
|
{ value: 'manual', label: '人工核销' },
|
|
],
|
|
manual: [
|
|
{ value: 'after_order_received', label: '收到订单后' },
|
|
{ value: 'after_task_created', label: '人工任务创建后' },
|
|
{ value: 'manual', label: '人工核销' },
|
|
],
|
|
third_party_voucher: [
|
|
{ value: 'after_order_received', label: '收到订单后' },
|
|
{ value: 'manual', label: '人工核销' },
|
|
],
|
|
on_account: [
|
|
{ value: 'after_order_received', label: '收到订单后' },
|
|
{ value: 'manual', label: '人工核销' },
|
|
],
|
|
unmatched: [
|
|
{ value: 'after_order_received', label: '未命中渠道后' },
|
|
{ value: 'manual', label: '人工核销' },
|
|
],
|
|
}
|
|
|
|
type PolicyRow = {
|
|
key: string
|
|
provider: VoucherConsumeProvider
|
|
channel: VoucherConsumeChannel
|
|
policy: VoucherChannelConsumePolicy
|
|
}
|
|
|
|
const JOB_STATUS: Record<string, { color: string; label: string }> = {
|
|
pending: { color: 'gold', label: '待执行' },
|
|
processing: { color: 'processing', label: '执行中' },
|
|
succeeded: { color: 'green', label: '成功' },
|
|
failed: { color: 'red', label: '失败' },
|
|
cancelled: { color: 'default', label: '已取消' },
|
|
}
|
|
|
|
const PROVIDER_FILTER_OPTIONS: Array<{ value: VoucherConsumeProvider; label: string }> = [
|
|
{ value: 'kuaishou_industry', label: PROVIDER_LABEL.kuaishou_industry },
|
|
{ value: 'douyin_mashida', label: PROVIDER_LABEL.douyin_mashida },
|
|
]
|
|
|
|
const CHANNEL_FILTER_OPTIONS: Array<{ value: VoucherConsumeChannel; label: string }> = [
|
|
{ value: 'order_received', label: CHANNEL_LABEL.order_received },
|
|
{ value: 'affiliate_dash', label: CHANNEL_LABEL.affiliate_dash },
|
|
{ value: 'work_order', label: CHANNEL_LABEL.work_order },
|
|
{ value: 'manual', label: CHANNEL_LABEL.manual },
|
|
{ value: 'third_party_voucher', label: CHANNEL_LABEL.third_party_voucher },
|
|
{ value: 'on_account', label: CHANNEL_LABEL.on_account },
|
|
{ value: 'unmatched', label: CHANNEL_LABEL.unmatched },
|
|
]
|
|
|
|
const JOB_STATUS_FILTER_OPTIONS: Array<{
|
|
value: VoucherConsumeJob['status']
|
|
label: string
|
|
}> = [
|
|
{ value: 'pending', label: JOB_STATUS.pending!.label },
|
|
{ value: 'processing', label: JOB_STATUS.processing!.label },
|
|
{ value: 'succeeded', label: JOB_STATUS.succeeded!.label },
|
|
{ value: 'failed', label: JOB_STATUS.failed!.label },
|
|
{ value: 'cancelled', label: JOB_STATUS.cancelled!.label },
|
|
]
|
|
|
|
export default function VoucherConsumePolicyPanel() {
|
|
const queryClient = useQueryClient()
|
|
const [draft, setDraft] = useState<VoucherConsumePolicy | null>(null)
|
|
const [keywordDraft, setKeywordDraft] = useState('')
|
|
const [keyword, setKeyword] = useState('')
|
|
const [providerFilter, setProviderFilter] = useState<VoucherConsumeProvider | ''>('')
|
|
const [channelFilter, setChannelFilter] = useState<VoucherConsumeChannel | ''>('')
|
|
const [statusFilter, setStatusFilter] = useState<VoucherConsumeJob['status'] | ''>('')
|
|
const [page, setPage] = useState(1)
|
|
const [pageSize, setPageSize] = useState(ADMIN_DEFAULT_PAGE_SIZE)
|
|
const policyQuery = useQuery({
|
|
queryKey: ['voucher-consume-policy'],
|
|
queryFn: fetchVoucherConsumePolicy,
|
|
})
|
|
const jobsQuery = useQuery({
|
|
queryKey: [
|
|
'voucher-consume-jobs',
|
|
keyword,
|
|
providerFilter,
|
|
channelFilter,
|
|
statusFilter,
|
|
page,
|
|
pageSize,
|
|
],
|
|
queryFn: () =>
|
|
fetchVoucherConsumeJobs({
|
|
keyword: keyword || undefined,
|
|
provider: providerFilter || undefined,
|
|
channel: channelFilter || undefined,
|
|
status: statusFilter || undefined,
|
|
page,
|
|
pageSize,
|
|
}),
|
|
refetchInterval: 10_000,
|
|
})
|
|
|
|
useEffect(() => {
|
|
const policy = policyQuery.data?.data
|
|
if (policy) {
|
|
setDraft(structuredClone(policy))
|
|
}
|
|
}, [policyQuery.data])
|
|
|
|
const saveMutation = useMutation({
|
|
mutationFn: (policy: VoucherConsumePolicy) => saveVoucherConsumePolicy(policy),
|
|
onSuccess: (response) => {
|
|
setDraft(structuredClone(response.data))
|
|
message.success('核销策略已保存')
|
|
void queryClient.invalidateQueries({ queryKey: ['voucher-consume-policy'] })
|
|
},
|
|
onError: (error) => {
|
|
message.error(error instanceof Error ? error.message : '保存核销策略失败')
|
|
},
|
|
})
|
|
|
|
const retryMutation = useMutation({
|
|
mutationFn: retryVoucherConsumeJob,
|
|
onSuccess: () => {
|
|
message.success('核销任务已重新排队')
|
|
void queryClient.invalidateQueries({ queryKey: ['voucher-consume-jobs'] })
|
|
},
|
|
onError: (error) => {
|
|
message.error(error instanceof Error ? error.message : '重试核销任务失败')
|
|
},
|
|
})
|
|
|
|
const rows = useMemo(() => buildPolicyRows(draft), [draft])
|
|
const hasJobFilters = Boolean(keyword || providerFilter || channelFilter || statusFilter)
|
|
|
|
function resetJobFilters() {
|
|
setKeywordDraft('')
|
|
setKeyword('')
|
|
setProviderFilter('')
|
|
setChannelFilter('')
|
|
setStatusFilter('')
|
|
setPage(1)
|
|
}
|
|
|
|
function updateChannel(
|
|
provider: VoucherConsumeProvider,
|
|
channel: VoucherConsumeChannel,
|
|
patch: Partial<VoucherChannelConsumePolicy>,
|
|
) {
|
|
setDraft((current) => {
|
|
if (!current) return current
|
|
return {
|
|
...current,
|
|
platforms: {
|
|
...current.platforms,
|
|
[provider]: {
|
|
...current.platforms[provider],
|
|
channels: {
|
|
...current.platforms[provider].channels,
|
|
[channel]: {
|
|
...current.platforms[provider].channels[channel],
|
|
...patch,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
}
|
|
})
|
|
}
|
|
|
|
function updatePlatformEnabled(provider: VoucherConsumeProvider, enabled: boolean) {
|
|
setDraft((current) => {
|
|
if (!current) return current
|
|
return {
|
|
...current,
|
|
platforms: {
|
|
...current.platforms,
|
|
[provider]: {
|
|
...current.platforms[provider],
|
|
enabled,
|
|
},
|
|
},
|
|
}
|
|
})
|
|
}
|
|
|
|
const policyColumns: TableColumnsType<PolicyRow> = [
|
|
{
|
|
title: '平台',
|
|
dataIndex: 'provider',
|
|
width: 150,
|
|
render: (provider: VoucherConsumeProvider) => PROVIDER_LABEL[provider],
|
|
},
|
|
{
|
|
title: '策略项',
|
|
dataIndex: 'channel',
|
|
width: 140,
|
|
render: (channel: VoucherConsumeChannel) => CHANNEL_LABEL[channel],
|
|
},
|
|
{
|
|
title: '自动核销',
|
|
width: 100,
|
|
render: (_, row) => (
|
|
<Switch
|
|
size="small"
|
|
checked={row.policy.enabled}
|
|
onChange={(enabled) => updateChannel(row.provider, row.channel, { enabled })}
|
|
/>
|
|
),
|
|
},
|
|
{
|
|
title: '触发时间',
|
|
width: 220,
|
|
render: (_, row) => (
|
|
<Select
|
|
value={row.policy.mode}
|
|
options={MODE_OPTIONS[row.channel]}
|
|
disabled={!row.policy.enabled}
|
|
onChange={(mode) => updateChannel(row.provider, row.channel, { mode })}
|
|
style={{ width: '100%' }}
|
|
/>
|
|
),
|
|
},
|
|
{
|
|
title: '延迟',
|
|
width: 130,
|
|
render: (_, row) => (
|
|
<InputNumber
|
|
min={0}
|
|
max={3600}
|
|
precision={0}
|
|
value={row.policy.delaySeconds}
|
|
disabled={!row.policy.enabled}
|
|
onChange={(value) =>
|
|
updateChannel(row.provider, row.channel, {
|
|
delaySeconds: Math.max(0, Number(value || 0)),
|
|
})
|
|
}
|
|
addonAfter="秒"
|
|
style={{ width: '100%' }}
|
|
/>
|
|
),
|
|
},
|
|
]
|
|
|
|
const jobColumns: TableColumnsType<VoucherConsumeJob> = [
|
|
{ title: '任务', dataIndex: 'id', width: 80 },
|
|
{
|
|
title: '平台',
|
|
dataIndex: 'provider',
|
|
width: 140,
|
|
render: (provider: VoucherConsumeProvider) => PROVIDER_LABEL[provider],
|
|
},
|
|
{
|
|
title: '渠道',
|
|
dataIndex: 'channel',
|
|
width: 110,
|
|
render: (channel: VoucherConsumeChannel) => CHANNEL_LABEL[channel],
|
|
},
|
|
{
|
|
title: '订单号',
|
|
dataIndex: 'platform_order_id',
|
|
width: 170,
|
|
render: (value: string) => value || '-',
|
|
},
|
|
{
|
|
title: '核销码',
|
|
dataIndex: 'voucher_codes',
|
|
width: 210,
|
|
render: (values: string[] | null | undefined) => {
|
|
const codes = Array.isArray(values)
|
|
? values.map((value) => String(value || '').trim()).filter(Boolean)
|
|
: []
|
|
if (codes.length === 0) {
|
|
return '-'
|
|
}
|
|
return (
|
|
<Space direction="vertical" size={2}>
|
|
{codes.slice(0, 3).map((code) => (
|
|
<Typography.Text key={code} copyable={{ text: code }} ellipsis>
|
|
{code}
|
|
</Typography.Text>
|
|
))}
|
|
{codes.length > 3 ? (
|
|
<Typography.Text type="secondary">另有 {codes.length - 3} 个</Typography.Text>
|
|
) : null}
|
|
</Space>
|
|
)
|
|
},
|
|
},
|
|
{
|
|
title: '计划时间',
|
|
dataIndex: 'due_at',
|
|
width: 170,
|
|
render: (value: string) => formatDateTime(value),
|
|
},
|
|
{
|
|
title: '状态',
|
|
dataIndex: 'status',
|
|
width: 100,
|
|
render: (status: string) => {
|
|
const current = JOB_STATUS[status] || { color: 'default', label: status }
|
|
return <Tag color={current.color}>{current.label}</Tag>
|
|
},
|
|
},
|
|
{
|
|
title: '尝试',
|
|
width: 80,
|
|
render: (_, row) => `${row.attempt_count}/${row.max_attempts}`,
|
|
},
|
|
{
|
|
title: '错误',
|
|
dataIndex: 'last_error',
|
|
render: (value: string) =>
|
|
value ? (
|
|
<Tooltip title={value}>
|
|
<Typography.Text ellipsis style={{ maxWidth: 260 }}>
|
|
{value}
|
|
</Typography.Text>
|
|
</Tooltip>
|
|
) : (
|
|
'-'
|
|
),
|
|
},
|
|
{
|
|
title: '',
|
|
width: 80,
|
|
render: (_, row) =>
|
|
row.status === 'failed' || row.status === 'cancelled' ? (
|
|
<Button
|
|
size="small"
|
|
onClick={() => retryMutation.mutate(row.id)}
|
|
loading={retryMutation.isPending}
|
|
>
|
|
重试
|
|
</Button>
|
|
) : null,
|
|
},
|
|
]
|
|
|
|
return (
|
|
<Space direction="vertical" size={16} className="full-width">
|
|
<Space size={20} wrap>
|
|
<Space size={8}>
|
|
<Typography.Text strong>总开关</Typography.Text>
|
|
<Switch
|
|
checked={draft?.enabled !== false}
|
|
onChange={(enabled) =>
|
|
setDraft((current) => (current ? { ...current, enabled } : current))
|
|
}
|
|
/>
|
|
</Space>
|
|
{(['kuaishou_industry', 'douyin_mashida'] as const).map((provider) => (
|
|
<Space key={provider} size={8}>
|
|
<Typography.Text>{PROVIDER_LABEL[provider]}</Typography.Text>
|
|
<Switch
|
|
checked={draft?.platforms[provider].enabled !== false}
|
|
disabled={draft?.enabled === false}
|
|
onChange={(enabled) => updatePlatformEnabled(provider, enabled)}
|
|
/>
|
|
</Space>
|
|
))}
|
|
<Button
|
|
type="primary"
|
|
icon={<SaveOutlined />}
|
|
disabled={!draft}
|
|
loading={saveMutation.isPending}
|
|
onClick={() => draft && saveMutation.mutate(draft)}
|
|
>
|
|
保存
|
|
</Button>
|
|
</Space>
|
|
|
|
<PercentTable
|
|
rowKey={(row) => row.key}
|
|
size="small"
|
|
columns={policyColumns}
|
|
dataSource={rows}
|
|
pagination={false}
|
|
loading={policyQuery.isLoading}
|
|
/>
|
|
|
|
<Space className="full-width" style={{ justifyContent: 'space-between' }} wrap>
|
|
<Typography.Title level={5} style={{ margin: 0 }}>
|
|
最近核销任务
|
|
</Typography.Title>
|
|
<Space wrap>
|
|
<Input.Search
|
|
allowClear
|
|
prefix={<SearchOutlined />}
|
|
placeholder="任务号 / 订单号 / 平台单号 / 核销码"
|
|
value={keywordDraft}
|
|
onChange={(event) => {
|
|
const value = event.target.value
|
|
setKeywordDraft(value)
|
|
if (!value) {
|
|
setKeyword('')
|
|
setPage(1)
|
|
}
|
|
}}
|
|
onSearch={(value) => {
|
|
setKeyword(String(value || '').trim())
|
|
setPage(1)
|
|
}}
|
|
style={{ width: 260 }}
|
|
/>
|
|
<Select
|
|
allowClear
|
|
placeholder="全部平台"
|
|
value={providerFilter || undefined}
|
|
options={PROVIDER_FILTER_OPTIONS}
|
|
onChange={(value: VoucherConsumeProvider | undefined) => {
|
|
setProviderFilter(value || '')
|
|
setPage(1)
|
|
}}
|
|
style={{ width: 150 }}
|
|
/>
|
|
<Select
|
|
allowClear
|
|
placeholder="全部渠道"
|
|
value={channelFilter || undefined}
|
|
options={CHANNEL_FILTER_OPTIONS}
|
|
onChange={(value: VoucherConsumeChannel | undefined) => {
|
|
setChannelFilter(value || '')
|
|
setPage(1)
|
|
}}
|
|
style={{ width: 150 }}
|
|
/>
|
|
<Select
|
|
allowClear
|
|
placeholder="全部状态"
|
|
value={statusFilter || undefined}
|
|
options={JOB_STATUS_FILTER_OPTIONS}
|
|
onChange={(value: VoucherConsumeJob['status'] | undefined) => {
|
|
setStatusFilter(value || '')
|
|
setPage(1)
|
|
}}
|
|
style={{ width: 130 }}
|
|
/>
|
|
<Button disabled={!hasJobFilters} onClick={resetJobFilters}>
|
|
重置
|
|
</Button>
|
|
<Button
|
|
icon={<ReloadOutlined />}
|
|
onClick={() =>
|
|
void queryClient.invalidateQueries({ queryKey: ['voucher-consume-jobs'] })
|
|
}
|
|
>
|
|
刷新
|
|
</Button>
|
|
</Space>
|
|
</Space>
|
|
<PercentTable
|
|
rowKey="id"
|
|
size="small"
|
|
columns={jobColumns}
|
|
dataSource={jobsQuery.data?.data.items || []}
|
|
loading={jobsQuery.isFetching}
|
|
pagination={buildAdminTablePagination({
|
|
current: jobsQuery.data?.data.page || page,
|
|
pageSize: jobsQuery.data?.data.pageSize || pageSize,
|
|
total: jobsQuery.data?.data.total || 0,
|
|
onChange: (nextPage, nextPageSize) => {
|
|
setPage(nextPageSize !== pageSize ? 1 : nextPage)
|
|
setPageSize(nextPageSize)
|
|
},
|
|
})}
|
|
scroll={{ x: 1420 }}
|
|
/>
|
|
</Space>
|
|
)
|
|
}
|
|
|
|
function buildPolicyRows(policy: VoucherConsumePolicy | null): PolicyRow[] {
|
|
if (!policy) return []
|
|
return (Object.keys(policy.platforms) as VoucherConsumeProvider[]).flatMap((provider) =>
|
|
(Object.keys(policy.platforms[provider].channels) as VoucherConsumeChannel[]).map(
|
|
(channel) => ({
|
|
key: `${provider}:${channel}`,
|
|
provider,
|
|
channel,
|
|
policy: policy.platforms[provider].channels[channel],
|
|
}),
|
|
),
|
|
)
|
|
}
|