diff --git a/ecom-dmb-frontend/apps/web-antd/src/views/ecommerce/product/components/ProductCard.vue b/ecom-dmb-frontend/apps/web-antd/src/views/ecommerce/product/components/ProductCard.vue index ab8c645a..6cfc7ed7 100644 --- a/ecom-dmb-frontend/apps/web-antd/src/views/ecommerce/product/components/ProductCard.vue +++ b/ecom-dmb-frontend/apps/web-antd/src/views/ecommerce/product/components/ProductCard.vue @@ -6,13 +6,11 @@ import { computed, h, ref, watch } from 'vue'; import type { DropdownRender } from 'ant-design-vue/es/vc-select/BaseSelect'; import { useRouter } from 'vue-router'; -import { useVbenModal } from '@vben/common-ui'; import { createIconifyIcon } from '@vben/icons'; import { Button, Card, - DatePicker, Image, Input, InputNumber, @@ -24,16 +22,11 @@ import { 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'; import { getCategoryTreeApi } from '#/api/ecommerce/category'; +import { updateProductApi } from '#/api/ecommerce/product'; import { uploadFileToOss } from '#/utils/oss-upload'; import CategoryFormModal from '#/views/ecommerce/config/category-config/components/CategoryFormModal.vue'; +import ProductCostSyncToOrdersModal from '#/views/ecommerce/product/components/ProductCostSyncToOrdersModal.vue'; const props = defineProps(); const emit = defineEmits(); @@ -46,131 +39,26 @@ const ImageIcon = createIconifyIcon('lucide:image'); const PlusIcon = createIconifyIcon('lucide:plus'); const CheckIcon = createIconifyIcon('lucide:check'); const XIcon = createIconifyIcon('lucide:x'); +const SyncOrdersIcon = createIconifyIcon('lucide:refresh-cw'); -// 成本同步弹窗 -const [SyncModal, syncModalApi] = useVbenModal({ - title: '成本变更同步确认', - class: 'w-[500px]', - draggable: true, - contentClass: 'px-5 py-4', - cancelText: '暂不同步', - confirmText: '立即同步', - onConfirm: handleSyncConfirm, - onCancel: () => { - syncModalApi.close(); - }, -}); +const productCostSyncModalRef = ref | null>(null); -const syncFormData = ref<[string, string] | undefined>(undefined); -const pendingCostChangedSpecs = ref([]); -const selectedSyncSpecIds = ref([]); - -/** 当前时间范围内将参与同步的订单数(null 表示未请求或条件不齐) */ -const costSyncOrderCount = ref(null); -const costSyncOrderCountLoading = ref(false); -let costSyncCountTimer: ReturnType | 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(); - }, +const hasSyncableSpecs = computed(() => + (props.product.specs || []).some((s) => typeof s.id === 'number' && s.id > 0), ); -watch( - syncFormData, - () => { - scheduleFetchCostSyncOrderCount(); - }, - { deep: true }, -); - -async function openSyncModal(costChangedSpecIds: number[]) { - try { - const specs = await getProductSpecsByIdsApi(costChangedSpecIds); - pendingCostChangedSpecs.value = specs ?? []; - selectedSyncSpecIds.value = costChangedSpecIds; - syncFormData.value = undefined; - costSyncOrderCount.value = null; - syncModalApi.open(); - } catch (error) { - console.error('获取规格详情失败:', error); - message.error('获取规格详情失败,无法进行成本同步'); - } -} - -function toggleSpec(specId: number) { - const idx = selectedSyncSpecIds.value.indexOf(specId); - if (idx === -1) { - selectedSyncSpecIds.value.push(specId); - } else { - selectedSyncSpecIds.value.splice(idx, 1); - } -} - -async function handleSyncConfirm() { - if (selectedSyncSpecIds.value.length === 0) { - message.warning('请至少选择一个规格'); +/** 用户主动:将当前商品各规格成本按时间范围写入订单 */ +async function openManualCostSyncModal() { + const specIds = (props.product.specs || []) + .map((s) => s.id) + .filter((id): id is number => typeof id === 'number' && id > 0); + if (specIds.length === 0) { + message.warning('当前商品没有可同步的规格'); return; } - if (!syncFormData.value || syncFormData.value[0] == null || syncFormData.value[1] == null) { - message.warning('请选择同步时间范围'); - return; - } - try { - syncModalApi.setState?.({ confirmLoading: true }); - const [startDate, endDate] = syncFormData.value; - const totalUpdated = await batchSyncCostsToOrdersApi({ - specIds: selectedSyncSpecIds.value, - startTime: startDate, - endTime: endDate, - }); - message.success(`成本同步完成,共更新 ${totalUpdated ?? 0} 条订单`); - syncModalApi.close?.(); - } catch (error) { - console.error('成本同步失败:', error); - message.error('成本同步失败'); - } finally { - syncModalApi.setState?.({ confirmLoading: false }); - } + await productCostSyncModalRef.value?.open(specIds, { entry: 'manual' }); } interface Props { @@ -498,7 +386,9 @@ const saveEdit = async () => { emit('editing-change', false); // 如果有规格成本变更,弹出同步确认框 if (result.costChangedSpecIds?.length > 0) { - openSyncModal(result.costChangedSpecIds); + await productCostSyncModalRef.value?.open(result.costChangedSpecIds, { + entry: 'after-save', + }); } } catch (error) { console.error('保存商品失败:', error); @@ -726,6 +616,15 @@ const saveEdit = async () => { + + + @@ -1819,6 +1531,19 @@ html.dark { @include outline-btn(hsl(var(--success)), hsl(var(--success)), $accent-bg, hsl(var(--success-foreground))); } + .footer-icon-btn--sync { + @include outline-btn( + hsl(199deg 89% 48%), + hsl(199deg 89% 58%), + hsl(199deg 89% 48% / 12%), + hsl(199deg 89% 72%) + ); + } + + .footer-icon-btn--sync:disabled { + opacity: 0.45; + } + .footer-icon-btn--cancel { @include outline-btn($border-base, $muted-text, $secondary-bg); } @@ -1946,51 +1671,5 @@ html.dark { } } - /* 成本同步弹窗(Teleport 到 body,不在 .management-card 内) */ - .product-cost-sync { - .section-label, - .spec-code, - .cost-tag { - color: $muted-text; - } - - .sync-alert { - background: hsl(var(--warning) / 12%); - border-color: hsl(var(--warning) / 35%); - color: hsl(var(--warning)); - } - - .spec-item-card { - border-color: $border-base; - background: hsl(var(--muted)); - - &:hover { - border-color: hsl(var(--primary) / 50%); - background: $accent-bg; - } - - &.active { - border-color: $primary; - background: hsl(var(--primary) / 10%); - } - } - - .custom-checkbox { - background: hsl(var(--muted)); - border-color: $border-base; - } - - .spec-title { - color: $card-text; - } - - .cost-snapshot { - border-left-color: $border-base; - } - - .cost-tag span:last-child { - color: $card-text; - } - } } diff --git a/ecom-dmb-frontend/apps/web-antd/src/views/ecommerce/product/components/ProductCostSyncToOrdersModal.vue b/ecom-dmb-frontend/apps/web-antd/src/views/ecommerce/product/components/ProductCostSyncToOrdersModal.vue new file mode 100644 index 00000000..f6b67740 --- /dev/null +++ b/ecom-dmb-frontend/apps/web-antd/src/views/ecommerce/product/components/ProductCostSyncToOrdersModal.vue @@ -0,0 +1,460 @@ + + + + + + + diff --git a/ecom-dmb-frontend/apps/web-antd/src/views/ecommerce/product/components/ProductFormModal.vue b/ecom-dmb-frontend/apps/web-antd/src/views/ecommerce/product/components/ProductFormModal.vue index 001f0a1e..27d0fd92 100644 --- a/ecom-dmb-frontend/apps/web-antd/src/views/ecommerce/product/components/ProductFormModal.vue +++ b/ecom-dmb-frontend/apps/web-antd/src/views/ecommerce/product/components/ProductFormModal.vue @@ -2,14 +2,13 @@ import type { ProductDTO, ProductVO } from '#/api/ecommerce/product/types'; import type { DropdownRender } from 'ant-design-vue/es/vc-select/BaseSelect'; -import { computed, h, onUnmounted, ref, watch } from 'vue'; +import { computed, h, onUnmounted, ref } from 'vue'; import { useVbenModal } from '@vben/common-ui'; import { createIconifyIcon } from '@vben/icons'; import { Button, - DatePicker, Form, Input, InputNumber, @@ -20,16 +19,11 @@ import { } from 'ant-design-vue'; 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'; import { getCategoryTreeApi } from '#/api/ecommerce/category'; +import { createProductApi, updateProductApi } from '#/api/ecommerce/product'; import { uploadFileToOss } from '#/utils/oss-upload'; import CategoryFormModal from '#/views/ecommerce/config/category-config/components/CategoryFormModal.vue'; +import ProductCostSyncToOrdersModal from '#/views/ecommerce/product/components/ProductCostSyncToOrdersModal.vue'; interface Emits { (e: 'success'): void; @@ -63,117 +57,18 @@ const DownIcon = createIconifyIcon('lucide:chevron-down'); const PlusIcon = createIconifyIcon('lucide:plus'); const TrashIcon = createIconifyIcon('lucide:trash-2'); -// 成本同步弹窗 -const [SyncModal, syncModalApi] = useVbenModal({ - 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([]); -const selectedSyncSpecIds = ref([]); - -const costSyncOrderCount = ref(null); -const costSyncOrderCountLoading = ref(false); -let costSyncCountTimer: ReturnType | 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 }, -); +const productCostSyncModalRef = ref | null>(null); async function openSyncModal(costChangedSpecIds: number[]) { - try { - // 根据ID列表查询规格详情 - const specs = await getProductSpecsByIdsApi(costChangedSpecIds); - pendingCostChangedSpecs.value = specs ?? []; - selectedSyncSpecIds.value = costChangedSpecIds; - syncFormData.value = [ + await productCostSyncModalRef.value?.open(costChangedSpecIds, { + entry: 'after-save', + initialDateRange: [ 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('获取规格详情失败,无法进行成本同步'); - } -} - -async function handleSyncConfirm() { - if (selectedSyncSpecIds.value.length === 0 || !syncFormData.value) return; - try { - syncModalApi.setState?.({ confirmLoading: true }); - const [startDate, endDate] = syncFormData.value; - const totalUpdated = await batchSyncCostsToOrdersApi({ - specIds: selectedSyncSpecIds.value, - startTime: startDate, - endTime: endDate, - }); - message.success(`成本同步完成,共更新 ${totalUpdated ?? 0} 条订单`); - syncModalApi.close?.(); - } catch (error) { - console.error('成本同步失败:', error); - message.error('成本同步失败'); - } finally { - syncModalApi.setState?.({ confirmLoading: false }); - } + ], + }); } // 内部状态管理 @@ -253,9 +148,10 @@ const categoryTreeDropdownRender: DropdownRender = (opt) => { ]); }; -/** 表格行稳定 key(新增行无 id) */ -type SpecFormRow = (ProductDTO.Spec | ProductDTO.UpdateSpec) & { +/** 表格行:创建规格无 id,编辑已有规格带 id;_clientKey 用于行 key */ +type SpecFormRow = ProductDTO.Spec & { _clientKey?: string; + id?: number; }; // 规格数据 - 支持创建和更新 @@ -917,65 +813,7 @@ onUnmounted(() => { - - -
-

- 检测到 {{ pendingCostChangedSpecs.length }} 个规格的成本已更新, - 请勾选需要同步的规格: -

-
-
- -
-
{{ spec.name }}
-
{{ spec.specCode || '无编码' }}
-
-
-
商品成本: {{ spec.productCost }}
-
运费: {{ spec.shippingCost }}
-
其他: {{ spec.otherCost }}
-
-
-
-
- - -
- - -
-
-
-
-
+