feat(ecom-dmb): 成本同步弹窗新增实时订单数预览,确认前显示受影响订单数量

This commit is contained in:
小蟑螂
2026-04-28 09:22:27 +08:00
parent f03e9a0e07
commit 81391d20aa
6 changed files with 220 additions and 9 deletions
@@ -24,6 +24,7 @@ import TextArea from 'ant-design-vue/es/input/TextArea';
import { updateProductApi } from '#/api/ecommerce/product';
import {
batchSyncCostsToOrdersApi,
countOrdersForCostSyncApi,
getProductSpecsByIdsApi,
} from '#/api/ecommerce/product-spec/apis';
import type { ProductSpecVO } from '#/api/ecommerce/product-spec/types';
@@ -42,10 +43,12 @@ const PlusIcon = createIconifyIcon('lucide:plus');
// 成本同步弹窗
const [SyncModal, syncModalApi] = useVbenModal({
title: '成本变更确认',
title: '成本变更同步确认',
class: 'w-[500px]',
draggable: true,
contentClass: 'px-5 py-4',
cancelText: '暂不同步',
confirmText: '立即同步',
onConfirm: handleSyncConfirm,
onCancel: () => {
syncModalApi.close();
@@ -53,6 +56,66 @@ const [SyncModal, syncModalApi] = useVbenModal({
});
const syncFormData = ref<[string, string] | undefined>(undefined);
const pendingCostChangedSpecs = ref<ProductSpecVO.Info[]>([]);
const selectedSyncSpecIds = ref<number[]>([]);
/** 当前时间范围内将参与同步的订单数(null 表示未请求或条件不齐) */
const costSyncOrderCount = ref<null | number>(null);
const costSyncOrderCountLoading = ref(false);
let costSyncCountTimer: ReturnType<typeof setTimeout> | null = null;
function scheduleFetchCostSyncOrderCount() {
if (costSyncCountTimer) {
clearTimeout(costSyncCountTimer);
}
costSyncCountTimer = setTimeout(() => {
costSyncCountTimer = null;
void fetchCostSyncOrderCount();
}, 300);
}
async function fetchCostSyncOrderCount() {
if (
!syncFormData.value ||
syncFormData.value[0] == null ||
syncFormData.value[1] == null
) {
costSyncOrderCount.value = null;
return;
}
if (selectedSyncSpecIds.value.length === 0) {
costSyncOrderCount.value = null;
return;
}
costSyncOrderCountLoading.value = true;
try {
const [startTime, endTime] = syncFormData.value;
costSyncOrderCount.value = await countOrdersForCostSyncApi({
specIds: [...selectedSyncSpecIds.value],
startTime,
endTime,
});
} catch {
costSyncOrderCount.value = null;
} finally {
costSyncOrderCountLoading.value = false;
}
}
watch(
() => [...selectedSyncSpecIds.value],
() => {
scheduleFetchCostSyncOrderCount();
},
);
watch(
syncFormData,
() => {
scheduleFetchCostSyncOrderCount();
},
{ deep: true },
);
async function openSyncModal(costChangedSpecIds: number[]) {
try {
@@ -60,6 +123,7 @@ async function openSyncModal(costChangedSpecIds: number[]) {
pendingCostChangedSpecs.value = specs ?? [];
selectedSyncSpecIds.value = costChangedSpecIds;
syncFormData.value = undefined;
costSyncOrderCount.value = null;
syncModalApi.open();
} catch (error) {
console.error('获取规格详情失败:', error);
@@ -67,9 +131,6 @@ async function openSyncModal(costChangedSpecIds: number[]) {
}
}
const pendingCostChangedSpecs = ref<ProductSpecVO.Info[]>([]);
const selectedSyncSpecIds = ref<number[]>([]);
function toggleSpec(specId: number) {
const idx = selectedSyncSpecIds.value.indexOf(specId);
if (idx === -1) {
@@ -634,6 +695,24 @@ const saveEdit = async () => {
value-format="YYYY-MM-DD HH:mm:ss"
show-time
/>
<div class="sync-order-count mt-2 text-sm text-gray-600 dark:text-gray-400">
<template v-if="costSyncOrderCountLoading">正在统计订单数…</template>
<template
v-else-if="
costSyncOrderCount !== null &&
syncFormData &&
syncFormData[0] &&
syncFormData[1] &&
selectedSyncSpecIds.length > 0
"
>
当前时间范围内共
<strong class="text-gray-900 dark:text-gray-100">{{
costSyncOrderCount
}}</strong>
条订单将更新成本
</template>
</div>
</div>
</div>
</SyncModal>
@@ -1,7 +1,7 @@
<script lang="ts" setup>
import type { ProductDTO, ProductVO } from '#/api/ecommerce/product/types';
import { computed, h, onUnmounted, ref } from 'vue';
import { computed, h, onUnmounted, ref, watch } from 'vue';
import { useVbenModal } from '@vben/common-ui';
import { createIconifyIcon } from '@vben/icons';
@@ -22,6 +22,7 @@ import dayjs from 'dayjs';
import { createProductApi, updateProductApi } from '#/api/ecommerce/product';
import {
batchSyncCostsToOrdersApi,
countOrdersForCostSyncApi,
getProductSpecsByIdsApi,
} from '#/api/ecommerce/product-spec/apis';
import type { ProductSpecVO } from '#/api/ecommerce/product-spec/types';
@@ -62,16 +63,77 @@ const TrashIcon = createIconifyIcon('lucide:trash-2');
// 成本同步弹窗
const [SyncModal, syncModalApi] = useVbenModal({
title: '成本变更确认',
title: '成本变更同步确认',
class: 'w-[500px]',
draggable: true,
contentClass: 'px-5 py-4',
cancelText: '暂不同步',
confirmText: '立即同步',
onConfirm: handleSyncConfirm,
onCancel: () => {
syncModalApi.close();
},
});
const syncFormData = ref<[string, string] | undefined>(undefined);
const pendingCostChangedSpecs = ref<ProductSpecVO.Info[]>([]);
const selectedSyncSpecIds = ref<number[]>([]);
const costSyncOrderCount = ref<null | number>(null);
const costSyncOrderCountLoading = ref(false);
let costSyncCountTimer: ReturnType<typeof setTimeout> | null = null;
function scheduleFetchCostSyncOrderCount() {
if (costSyncCountTimer) {
clearTimeout(costSyncCountTimer);
}
costSyncCountTimer = setTimeout(() => {
costSyncCountTimer = null;
void fetchCostSyncOrderCount();
}, 300);
}
async function fetchCostSyncOrderCount() {
if (
!syncFormData.value ||
syncFormData.value[0] == null ||
syncFormData.value[1] == null
) {
costSyncOrderCount.value = null;
return;
}
if (selectedSyncSpecIds.value.length === 0) {
costSyncOrderCount.value = null;
return;
}
costSyncOrderCountLoading.value = true;
try {
const [startTime, endTime] = syncFormData.value;
costSyncOrderCount.value = await countOrdersForCostSyncApi({
specIds: [...selectedSyncSpecIds.value],
startTime,
endTime,
});
} catch {
costSyncOrderCount.value = null;
} finally {
costSyncOrderCountLoading.value = false;
}
}
watch(
() => [...selectedSyncSpecIds.value],
() => {
scheduleFetchCostSyncOrderCount();
},
);
watch(
syncFormData,
() => {
scheduleFetchCostSyncOrderCount();
},
{ deep: true },
);
async function openSyncModal(costChangedSpecIds: number[]) {
try {
@@ -83,16 +145,15 @@ async function openSyncModal(costChangedSpecIds: number[]) {
dayjs().startOf('day').format('YYYY-MM-DD HH:mm:ss'),
dayjs().endOf('day').format('YYYY-MM-DD HH:mm:ss'),
];
costSyncOrderCount.value = null;
syncModalApi.open();
scheduleFetchCostSyncOrderCount();
} catch (error) {
console.error('获取规格详情失败:', error);
message.error('获取规格详情失败,无法进行成本同步');
}
}
const pendingCostChangedSpecs = ref<ProductSpecVO.Info[]>([]);
const selectedSyncSpecIds = ref<number[]>([]);
async function handleSyncConfirm() {
if (selectedSyncSpecIds.value.length === 0 || !syncFormData.value) return;
try {
@@ -785,6 +846,22 @@ onUnmounted(() => {
value-format="YYYY-MM-DD HH:mm:ss"
show-time
/>
<div class="mt-2 text-sm text-gray-500">
<template v-if="costSyncOrderCountLoading">正在统计订单数…</template>
<template
v-else-if="
costSyncOrderCount !== null &&
syncFormData &&
syncFormData[0] &&
syncFormData[1] &&
selectedSyncSpecIds.length > 0
"
>
当前时间范围内共
<strong class="text-gray-800">{{ costSyncOrderCount }}</strong>
条订单将更新成本
</template>
</div>
</Form.Item>
</Form>
</div>