mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-24 20:29:19 +08:00
feat: 完成拼多多订单同步,订单转换,bi计算三层设计
This commit is contained in:
@@ -22,3 +22,49 @@ export async function syncPddOrdersIncrementalApi(shopId: number) {
|
||||
{ params: { shopId } },
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建全量同步任务(异步按日)
|
||||
*/
|
||||
export async function createPddFullSyncTaskApi(data: {
|
||||
shopId: number;
|
||||
dateFrom: string;
|
||||
dateTo: string;
|
||||
}) {
|
||||
return requestClient.post<PddOrderSyncVO.FullTask>(
|
||||
'/channel/pdd/order/sync/full-task',
|
||||
data,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询全量同步任务
|
||||
*/
|
||||
export async function getPddFullSyncTaskApi(taskId: number) {
|
||||
return requestClient.get<PddOrderSyncVO.FullTask>(
|
||||
`/channel/pdd/order/sync/full-task/${taskId}`,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 增量同步开关
|
||||
*/
|
||||
export async function togglePddIncrementalSyncApi(data: {
|
||||
shopId: number;
|
||||
enabled: boolean;
|
||||
}) {
|
||||
return requestClient.post<PddOrderSyncVO.IncrementalStatus>(
|
||||
'/channel/pdd/order/sync/incremental/toggle',
|
||||
data,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询增量同步开关状态
|
||||
*/
|
||||
export async function getPddIncrementalSyncStatusApi(shopId: number) {
|
||||
return requestClient.get<PddOrderSyncVO.IncrementalStatus>(
|
||||
'/channel/pdd/order/sync/incremental/status',
|
||||
{ params: { shopId } },
|
||||
);
|
||||
}
|
||||
|
||||
@@ -10,4 +10,26 @@ export namespace PddOrderSyncVO {
|
||||
skippedCount: number;
|
||||
errors?: string[];
|
||||
}
|
||||
|
||||
export interface FullTask {
|
||||
id: number;
|
||||
shopId: number;
|
||||
dateFrom: string;
|
||||
dateTo: string;
|
||||
currentDate?: string;
|
||||
totalDays: number;
|
||||
doneDays: number;
|
||||
failedDays: number;
|
||||
status: string;
|
||||
lastError?: string;
|
||||
createTime?: string;
|
||||
finishTime?: string;
|
||||
}
|
||||
|
||||
export interface IncrementalStatus {
|
||||
shopId: number;
|
||||
enabled: boolean;
|
||||
cursorStatus?: number;
|
||||
lastError?: string;
|
||||
}
|
||||
}
|
||||
|
||||
+155
-13
@@ -1,17 +1,22 @@
|
||||
<script lang="ts" setup>
|
||||
import type { ShopVO } from '#/api/ecommerce/shop/types';
|
||||
import type { PddOrderSyncVO } from '#/api/ecommerce/pdd/order-sync';
|
||||
import type { Dayjs } from 'dayjs';
|
||||
|
||||
import { computed, ref } from 'vue';
|
||||
import { computed, onBeforeUnmount, ref } from 'vue';
|
||||
|
||||
import { useVbenModal } from '@vben/common-ui';
|
||||
|
||||
import { DatePicker, Form, message, Radio } from 'ant-design-vue';
|
||||
import { DatePicker, Form, message, Progress, Radio, Switch } from 'ant-design-vue';
|
||||
import dayjs from 'dayjs';
|
||||
|
||||
import {
|
||||
createPddFullSyncTaskApi,
|
||||
getPddFullSyncTaskApi,
|
||||
getPddIncrementalSyncStatusApi,
|
||||
syncPddOrdersByDayApi,
|
||||
syncPddOrdersIncrementalApi,
|
||||
togglePddIncrementalSyncApi,
|
||||
} from '#/api/ecommerce/pdd/order-sync';
|
||||
|
||||
interface Emits {
|
||||
@@ -22,13 +27,21 @@ const emit = defineEmits<Emits>();
|
||||
|
||||
const shop = ref<null | ShopVO.Info>(null);
|
||||
const loading = ref(false);
|
||||
const mode = ref<'day' | 'incremental'>('day');
|
||||
const mode = ref<'day' | 'full' | 'incremental'>('full');
|
||||
const confirmDate = ref<Dayjs>(dayjs().subtract(1, 'day'));
|
||||
const range = ref<[Dayjs, Dayjs]>([
|
||||
dayjs().subtract(90, 'day'),
|
||||
dayjs().subtract(1, 'day'),
|
||||
]);
|
||||
const incrementalEnabled = ref(false);
|
||||
const fullTask = ref<null | PddOrderSyncVO.FullTask>(null);
|
||||
let pollTimer: null | ReturnType<typeof setInterval> = null;
|
||||
|
||||
const [Modal, modalApi] = useVbenModal({
|
||||
draggable: true,
|
||||
class: 'w-[480px]',
|
||||
class: 'w-[520px]',
|
||||
onCancel() {
|
||||
stopPoll();
|
||||
modalApi.close();
|
||||
},
|
||||
onConfirm() {
|
||||
@@ -40,15 +53,99 @@ const modalTitle = computed(() =>
|
||||
shop.value ? `同步拼多多订单 - ${shop.value.name}` : '同步拼多多订单',
|
||||
);
|
||||
|
||||
const taskProgressPercent = computed(() => {
|
||||
if (!fullTask.value || !fullTask.value.totalDays) return 0;
|
||||
return Math.min(
|
||||
100,
|
||||
Math.round((fullTask.value.doneDays / fullTask.value.totalDays) * 100),
|
||||
);
|
||||
});
|
||||
|
||||
function stopPoll() {
|
||||
if (pollTimer) {
|
||||
clearInterval(pollTimer);
|
||||
pollTimer = null;
|
||||
}
|
||||
}
|
||||
|
||||
async function loadIncrementalStatus() {
|
||||
if (!shop.value) return;
|
||||
try {
|
||||
const status = await getPddIncrementalSyncStatusApi(shop.value.id);
|
||||
incrementalEnabled.value = !!status.enabled;
|
||||
} catch (error) {
|
||||
console.error('加载增量开关失败:', error);
|
||||
}
|
||||
}
|
||||
|
||||
async function handleToggleIncremental(checked: boolean | string | number) {
|
||||
if (!shop.value) return;
|
||||
const enabled = !!checked;
|
||||
try {
|
||||
const status = await togglePddIncrementalSyncApi({
|
||||
shopId: shop.value.id,
|
||||
enabled,
|
||||
});
|
||||
incrementalEnabled.value = !!status.enabled;
|
||||
message.success(enabled ? '已开启增量同步' : '已关闭增量同步');
|
||||
} catch (error) {
|
||||
incrementalEnabled.value = !enabled;
|
||||
console.error('切换增量开关失败:', error);
|
||||
message.error('切换增量开关失败');
|
||||
}
|
||||
}
|
||||
|
||||
async function pollFullTask(taskId: number) {
|
||||
stopPoll();
|
||||
pollTimer = setInterval(async () => {
|
||||
try {
|
||||
const task = await getPddFullSyncTaskApi(taskId);
|
||||
fullTask.value = task;
|
||||
if (task.status === 'SUCCESS' || task.status === 'FAILED' || task.status === 'CANCELLED') {
|
||||
stopPoll();
|
||||
modalApi.setState({ confirmLoading: false });
|
||||
loading.value = false;
|
||||
if (task.status === 'SUCCESS') {
|
||||
message.success(
|
||||
`全量同步完成:${task.doneDays}/${task.totalDays} 天`,
|
||||
);
|
||||
emit('success');
|
||||
} else {
|
||||
message.error(task.lastError || '全量同步失败');
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('轮询全量任务失败:', error);
|
||||
}
|
||||
}, 2000);
|
||||
}
|
||||
|
||||
async function handleSubmit() {
|
||||
if (!shop.value) return;
|
||||
if (mode.value === 'day' && !confirmDate.value) {
|
||||
message.warning('请选择成交日期');
|
||||
return;
|
||||
}
|
||||
if (mode.value === 'full' && (!range.value?.[0] || !range.value?.[1])) {
|
||||
message.warning('请选择全量同步日期区间');
|
||||
return;
|
||||
}
|
||||
try {
|
||||
loading.value = true;
|
||||
modalApi.setState({ confirmLoading: true });
|
||||
|
||||
if (mode.value === 'full') {
|
||||
const task = await createPddFullSyncTaskApi({
|
||||
shopId: shop.value.id,
|
||||
dateFrom: range.value[0].format('YYYY-MM-DD'),
|
||||
dateTo: range.value[1].format('YYYY-MM-DD'),
|
||||
});
|
||||
fullTask.value = task;
|
||||
message.success('全量同步任务已创建,正在后台执行');
|
||||
pollFullTask(task.id);
|
||||
return;
|
||||
}
|
||||
|
||||
const result =
|
||||
mode.value === 'day'
|
||||
? await syncPddOrdersByDayApi({
|
||||
@@ -62,7 +159,9 @@ async function handleSubmit() {
|
||||
? `;注意:${result.errors[0]}`
|
||||
: '';
|
||||
message.success(
|
||||
`同步完成:拉取 ${result.fetchedCount},入库 ${result.upsertedCount}${errHint}`,
|
||||
mode.value === 'day' || mode.value === 'incremental'
|
||||
? '已入队,后台同步中'
|
||||
: `同步完成:拉取 ${result.fetchedCount},入库 ${result.upsertedCount}${errHint}`,
|
||||
);
|
||||
emit('success');
|
||||
modalApi.close();
|
||||
@@ -70,34 +169,66 @@ async function handleSubmit() {
|
||||
console.error('同步拼多多订单失败:', error);
|
||||
message.error('同步拼多多订单失败');
|
||||
} finally {
|
||||
loading.value = false;
|
||||
modalApi.setState({ confirmLoading: false });
|
||||
if (mode.value !== 'full') {
|
||||
loading.value = false;
|
||||
modalApi.setState({ confirmLoading: false });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function open(record: ShopVO.Info) {
|
||||
async function open(record: ShopVO.Info) {
|
||||
shop.value = record;
|
||||
mode.value = 'day';
|
||||
mode.value = 'full';
|
||||
confirmDate.value = dayjs().subtract(1, 'day');
|
||||
range.value = [dayjs().subtract(90, 'day'), dayjs().subtract(1, 'day')];
|
||||
fullTask.value = null;
|
||||
stopPoll();
|
||||
await loadIncrementalStatus();
|
||||
modalApi.open();
|
||||
}
|
||||
|
||||
onBeforeUnmount(() => stopPoll());
|
||||
|
||||
defineExpose({ open });
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<Modal :title="modalTitle" content-class="px-5 py-4">
|
||||
<Form layout="vertical">
|
||||
<Form.Item label="自动增量同步">
|
||||
<div class="flex items-center gap-3">
|
||||
<Switch
|
||||
:checked="incrementalEnabled"
|
||||
checked-children="开"
|
||||
un-checked-children="关"
|
||||
@change="handleToggleIncremental"
|
||||
/>
|
||||
<span class="text-xs text-slate-500">开启后定时 Job 会拉取该店增量并转换</span>
|
||||
</div>
|
||||
</Form.Item>
|
||||
<Form.Item label="同步方式">
|
||||
<Radio.Group v-model:value="mode">
|
||||
<Radio value="full">全量同步任务(按日区间)</Radio>
|
||||
<Radio value="day">按成交日同步一天</Radio>
|
||||
<Radio value="incremental">增量同步(状态变更)</Radio>
|
||||
<Radio value="incremental">手动增量一次</Radio>
|
||||
</Radio.Group>
|
||||
</Form.Item>
|
||||
<Form.Item
|
||||
v-if="mode === 'day'"
|
||||
v-if="mode === 'full'"
|
||||
label="成交日期区间"
|
||||
extra="按天串行拉取;每天成功后自动进入转换与 BI 日汇聚"
|
||||
>
|
||||
<DatePicker.RangePicker
|
||||
v-model:value="range"
|
||||
class="w-full"
|
||||
:allow-clear="false"
|
||||
format="YYYY-MM-DD"
|
||||
/>
|
||||
</Form.Item>
|
||||
<Form.Item
|
||||
v-else-if="mode === 'day'"
|
||||
label="成交日期"
|
||||
extra="按拼多多成交时间(confirm_time)拉取该自然日订单,写入外部订单表"
|
||||
extra="按拼多多成交时间(confirm_time)拉取该自然日订单"
|
||||
>
|
||||
<DatePicker
|
||||
v-model:value="confirmDate"
|
||||
@@ -106,9 +237,20 @@ defineExpose({ open });
|
||||
format="YYYY-MM-DD"
|
||||
/>
|
||||
</Form.Item>
|
||||
<p v-else class="text-xs text-slate-500">
|
||||
<p v-else class="mb-3 text-xs text-slate-500">
|
||||
按订单更新时间拉取自上次游标以来的变更,用于发货/售后等状态同步。
|
||||
</p>
|
||||
<div v-if="fullTask" class="rounded border border-slate-200 p-3">
|
||||
<div class="mb-2 text-sm">
|
||||
任务 #{{ fullTask.id }} · {{ fullTask.status }} ·
|
||||
{{ fullTask.doneDays }}/{{ fullTask.totalDays }} 天
|
||||
<span v-if="fullTask.currentDate">(当前 {{ fullTask.currentDate }})</span>
|
||||
</div>
|
||||
<Progress :percent="taskProgressPercent" size="small" />
|
||||
<p v-if="fullTask.lastError" class="mt-2 text-xs text-red-500">
|
||||
{{ fullTask.lastError }}
|
||||
</p>
|
||||
</div>
|
||||
</Form>
|
||||
</Modal>
|
||||
</template>
|
||||
|
||||
Reference in New Issue
Block a user