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 = { kuaishou_industry: '快手电子凭证', douyin_mashida: '抖音码速达', } const CHANNEL_LABEL: Record = { 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 = { 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(null) const [keywordDraft, setKeywordDraft] = useState('') const [keyword, setKeyword] = useState('') const [providerFilter, setProviderFilter] = useState('') const [channelFilter, setChannelFilter] = useState('') const [statusFilter, setStatusFilter] = useState('') 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, ) { 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 = [ { 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) => ( updateChannel(row.provider, row.channel, { enabled })} /> ), }, { title: '触发时间', width: 220, render: (_, row) => ( { setProviderFilter(value || '') setPage(1) }} style={{ width: 150 }} /> { setStatusFilter(value || '') setPage(1) }} style={{ width: 130 }} /> { setPage(nextPageSize !== pageSize ? 1 : nextPage) setPageSize(nextPageSize) }, })} scroll={{ x: 1420 }} /> ) } 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], }), ), ) }