feat: 成本同步功能完整实现

- ProductSpecController 新增 POST /ec/product-spec/sync-costs-to-orders 接口
- OrderMapper.updateCostFields() 批量更新订单成本字段
- OrderService 新增 syncCostsToOrders 方法
- UpdateProductResultDTO 返回完整成本变更规格列表(含 id/name/specCode/成本字段)
- 前端 ProductFormModal 同步弹窗:勾选规格 + 选择时间范围 + 显示更新条数
This commit is contained in:
小蟑螂
2026-04-14 22:04:38 +08:00
parent 26cb1a9a41
commit 83ed7478d2
11 changed files with 341 additions and 13 deletions
@@ -8,6 +8,7 @@ import { createIconifyIcon } from '@vben/icons';
import {
Button,
DatePicker,
Form,
Input,
InputNumber,
@@ -15,8 +16,10 @@ import {
Space,
Table,
} from 'ant-design-vue';
import dayjs from 'dayjs';
import { createProductApi, updateProductApi } from '#/api/ecommerce/product';
import { syncCostsToOrdersApi } from '#/api/ecommerce/product-spec/apis';
import { uploadFileToOss } from '#/utils/oss-upload';
interface Emits {
@@ -51,6 +54,62 @@ 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',
onConfirm: handleSyncConfirm,
onCancel: () => {
syncModalApi.close();
},
});
const syncFormData = ref<[string, string] | null>(null);
function openSyncModal(costChangedSpecs: ProductVO.CostChangedSpec[]) {
pendingCostChangedSpecs.value = costChangedSpecs;
selectedSyncSpecIds.value = costChangedSpecs.map((s) => s.id);
syncFormData.value = [
dayjs().startOf('day').format('YYYY-MM-DD HH:mm:ss'),
dayjs().endOf('day').format('YYYY-MM-DD HH:mm:ss'),
];
syncModalApi.open();
}
const pendingCostChangedSpecs = ref<ProductVO.CostChangedSpec[]>([]);
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;
let totalUpdated = 0;
for (const specId of selectedSyncSpecIds.value) {
const spec = pendingCostChangedSpecs.value.find((s) => s.id === specId);
if (spec) {
const res = await syncCostsToOrdersApi({
specId,
productCost: spec.productCost,
shippingCost: spec.shippingCost ?? 0,
otherCost: spec.otherCost ?? 0,
startTime: startDate,
endTime: endDate,
});
totalUpdated += res.data ?? 0;
}
}
message.success(`成本同步完成,共更新 ${totalUpdated} 条订单`);
syncModalApi.close?.();
} catch (error) {
console.error('成本同步失败:', error);
message.error('成本同步失败');
} finally {
syncModalApi.setState?.({ confirmLoading: false });
}
}
// 内部状态管理
const isEdit = ref(false);
const currentProductData = ref<null | Partial<ProductVO.Info> | ProductVO.Info>(
@@ -182,11 +241,12 @@ async function handleSubmit() {
specs: specs.value as ProductDTO.UpdateSpec[],
}),
};
await updateProductApi(currentProductData.value.id, updateData);
if (specs.value.length > 0) {
const result = await updateProductApi(currentProductData.value.id, updateData);
if (specs.value.length > 0 && result.costChangedSpecs?.length > 0) {
message.success('商品和规格更新成功');
openSyncModal(result.costChangedSpecs);
} else {
message.success('更新成功');
message.success('商品和规格更新成功');
}
} else {
// 创建模式:统一使用创建接口,规格信息作为可选字段
@@ -654,4 +714,48 @@ onUnmounted(() => {
</Form.Item>
</Form>
</Modal>
<!-- 成本同步确认弹窗 -->
<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>