feat(ecom-dmb): 商品卡片保存时检测规格成本变更,弹出同步确认框

- updateProductApi 返回 costChangedSpecIds 时弹出成本同步弹窗
- 用户勾选规格并选择时间范围后调用 batchSyncCostsToOrdersApi 同步历史订单
This commit is contained in:
小蟑螂
2026-04-26 21:27:12 +08:00
parent d556da6aee
commit a90446dff6
@@ -4,14 +4,31 @@ import type { ProductDTO, ProductVO } from '#/api/ecommerce/product/types';
import { computed, ref, watch } from 'vue';
import { useRouter } from 'vue-router';
import { useVbenModal } from '@vben/common-ui';
import { createIconifyIcon } from '@vben/icons';
import { Button, Card, Image, Input, Popconfirm, TreeSelect, message } from 'ant-design-vue';
import {
Button,
Card,
DatePicker,
Form,
Image,
Input,
message,
Popconfirm,
TreeSelect,
} from 'ant-design-vue';
import dayjs from 'dayjs';
import TextArea from 'ant-design-vue/es/input/TextArea';
import { uploadFileToOss } from '#/utils/oss-upload';
import { getCategoryTreeApi } from '#/api/ecommerce/category';
import { updateProductApi } from '#/api/ecommerce/product';
import {
batchSyncCostsToOrdersApi,
getProductSpecsByIdsApi,
} from '#/api/ecommerce/product-spec/apis';
import type { ProductSpecVO } from '#/api/ecommerce/product-spec/types';
import { getCategoryTreeApi } from '#/api/ecommerce/category';
import { uploadFileToOss } from '#/utils/oss-upload';
const props = defineProps<Props>();
const emit = defineEmits<Emits>();
@@ -23,6 +40,59 @@ const ChartIcon = createIconifyIcon('lucide:bar-chart-2');
const ImageOffIcon = createIconifyIcon('lucide:image-off');
const PlusIcon = createIconifyIcon('lucide:plus');
// 成本同步弹窗
const [SyncModal, syncModalApi] = useVbenModal({
title: '成本变更确认',
class: 'w-[500px]',
draggable: true,
contentClass: 'px-5 py-4',
onConfirm: handleSyncConfirm,
onCancel: () => {
syncModalApi.close();
},
});
const syncFormData = ref<[string, string] | undefined>(undefined);
async function openSyncModal(costChangedSpecIds: number[]) {
try {
const specs = await getProductSpecsByIdsApi(costChangedSpecIds);
pendingCostChangedSpecs.value = specs ?? [];
selectedSyncSpecIds.value = costChangedSpecIds;
syncFormData.value = [
dayjs().startOf('day').format('YYYY-MM-DD HH:mm:ss'),
dayjs().endOf('day').format('YYYY-MM-DD HH:mm:ss'),
];
syncModalApi.open();
} 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 {
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 });
}
}
interface Props {
product: ProductVO.Info;
}
@@ -271,11 +341,15 @@ const saveEdit = async () => {
return { id: spec.id, ...base } as ProductDTO.UpdateSpec;
}),
};
await updateProductApi(props.product.id, payload);
const result = await updateProductApi(props.product.id, payload);
message.success('商品保存成功');
isEditing.value = false;
emit('updated');
emit('editing-change', false);
// 如果有规格成本变更,弹出同步确认框
if (result.costChangedSpecIds?.length > 0) {
openSyncModal(result.costChangedSpecIds);
}
} catch (error) {
console.error('保存商品失败:', error);
} finally {
@@ -496,6 +570,49 @@ const saveEdit = async () => {
</Popconfirm>
</div>
</Card>
<SyncModal>
<div class="py-4">
<p class="mb-4 text-gray-600">
检测到 <strong>{{ pendingCostChangedSpecs.length }}</strong> 个规格的成本已更新,
请勾选需要同步的规格:
</p>
<div class="mb-4 max-h-48 overflow-y-auto border border-gray-200 rounded">
<div
v-for="spec in pendingCostChangedSpecs"
:key="spec.id"
class="flex items-center gap-3 px-3 py-2 border-b border-gray-100 last:border-b-0"
>
<input
type="checkbox"
:value="spec.id"
v-model="selectedSyncSpecIds"
class="w-4 h-4 rounded border-gray-300 text-blue-500 focus:ring-blue-500"
/>
<div class="flex-1 min-w-0">
<div class="text-sm font-medium text-gray-800 truncate">{{ spec.name }}</div>
<div class="text-xs text-gray-400">{{ spec.specCode || '无编码' }}</div>
</div>
<div class="text-xs text-gray-500 text-right shrink-0">
<div>商品成本: <span class="text-orange-500 font-medium">{{ spec.productCost }}</span></div>
<div v-if="spec.shippingCost">运费: {{ spec.shippingCost }}</div>
<div v-if="spec.otherCost">其他: {{ spec.otherCost }}</div>
</div>
</div>
</div>
<Form layout="vertical" class="mt-4">
<Form.Item label="同步时间范围">
<DatePicker.RangePicker
v-model:value="syncFormData"
class="w-full"
format="YYYY-MM-DD HH:mm:ss"
value-format="YYYY-MM-DD HH:mm:ss"
show-time
/>
</Form.Item>
</Form>
</div>
</SyncModal>
</template>
<style scoped>
.management-card {