feat: 完善履约路由、订单服务与自动核销
This commit is contained in:
@@ -0,0 +1,377 @@
|
||||
import { ReloadOutlined, SaveOutlined } from '@ant-design/icons'
|
||||
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
|
||||
import {
|
||||
Button,
|
||||
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 { formatDateTime } from '@/utils/date-time'
|
||||
|
||||
const PROVIDER_LABEL: Record<VoucherConsumeProvider, string> = {
|
||||
kuaishou_industry: '快手电子凭证',
|
||||
douyin_mashida: '抖音码速达',
|
||||
}
|
||||
|
||||
const CHANNEL_LABEL: Record<VoucherConsumeChannel, string> = {
|
||||
affiliate_dash: '自动发货',
|
||||
work_order: '接单工单',
|
||||
manual: '人工客服',
|
||||
third_party_voucher: '第三方卡券',
|
||||
}
|
||||
|
||||
const MODE_OPTIONS: Record<
|
||||
VoucherConsumeChannel,
|
||||
Array<{ value: VoucherConsumeMode; label: string }>
|
||||
> = {
|
||||
affiliate_dash: [
|
||||
{ value: 'after_link_generated', label: '领取链接生成后' },
|
||||
{ value: 'after_task_created', label: '履约任务创建后' },
|
||||
{ value: 'manual', label: '人工核销' },
|
||||
],
|
||||
work_order: [
|
||||
{ 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_task_created', label: '人工任务创建后' },
|
||||
{ value: 'manual', label: '人工核销' },
|
||||
],
|
||||
third_party_voucher: [{ 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: '已取消' },
|
||||
}
|
||||
|
||||
export default function VoucherConsumePolicyPanel() {
|
||||
const queryClient = useQueryClient()
|
||||
const [draft, setDraft] = useState<VoucherConsumePolicy | null>(null)
|
||||
const policyQuery = useQuery({
|
||||
queryKey: ['voucher-consume-policy'],
|
||||
queryFn: fetchVoucherConsumePolicy,
|
||||
})
|
||||
const jobsQuery = useQuery({
|
||||
queryKey: ['voucher-consume-jobs'],
|
||||
queryFn: () => fetchVoucherConsumeJobs({ page: 1, pageSize: 20 }),
|
||||
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])
|
||||
|
||||
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}
|
||||
disabled={row.channel === 'third_party_voucher'}
|
||||
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.channel === 'third_party_voucher' || !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.channel === 'third_party_voucher' || !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: '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' }}>
|
||||
<Typography.Title level={5} style={{ margin: 0 }}>
|
||||
最近核销任务
|
||||
</Typography.Title>
|
||||
<Button
|
||||
icon={<ReloadOutlined />}
|
||||
onClick={() => void queryClient.invalidateQueries({ queryKey: ['voucher-consume-jobs'] })}
|
||||
>
|
||||
刷新
|
||||
</Button>
|
||||
</Space>
|
||||
<PercentTable
|
||||
rowKey="id"
|
||||
size="small"
|
||||
columns={jobColumns}
|
||||
dataSource={jobsQuery.data?.data.items || []}
|
||||
loading={jobsQuery.isLoading}
|
||||
pagination={false}
|
||||
scroll={{ x: 1100 }}
|
||||
/>
|
||||
</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],
|
||||
}),
|
||||
),
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user