feat(ecom-dmb): 优化商品成本同步逻辑

后端:
- UpdateProductResultDTO 改回只返回成本变更的规格ID列表
- ProductServiceImpl 构建 ID 列表而非完整对象
- 新增 BatchSyncCostDTO 批量同步请求DTO
- ProductSpecService 新增 getByIds 批量查询规格方法
- ProductSpecController 新增批量同步接口 batch-sync-costs-to-orders
- ProductSpecController 新增 batch-get-by-ids 接口查询规格详情

前端:
- UpdateProductResult 类型改为返回 costChangedSpecIds: number[]
- 新增 BatchSyncCost 和 getProductSpecsByIdsApi 批量接口
- ProductFormModal 改为批量同步,修复 responseReturn .data 的误用 bug
This commit is contained in:
小蟑螂
2026-04-17 11:40:23 +08:00
parent ac39f5d850
commit 5c4ef6c990
10 changed files with 171 additions and 104 deletions
@@ -77,8 +77,25 @@ export async function getProductSpecsByProductAndSpecsApi(
}
/**
* 同步规格成本到历史订单
* 根据ID列表批量查询商品规格
*/
export async function getProductSpecsByIdsApi(specIds: number[]) {
return requestClient.post<ProductSpecVO.Info[]>(
'/ec/product-spec/batch-get-by-ids',
specIds,
);
}
/**
* 同步规格成本到历史订单(单个规格)
*/
export async function syncCostsToOrdersApi(data: ProductSpecDTO.SyncCost) {
return requestClient.post<number>('/ec/product-spec/sync-costs-to-orders', data);
}
/**
* 批量同步规格成本到历史订单
*/
export async function batchSyncCostsToOrdersApi(data: ProductSpecDTO.BatchSyncCost) {
return requestClient.post<number>('/ec/product-spec/batch-sync-costs-to-orders', data);
}
@@ -59,7 +59,7 @@ export namespace ProductSpecDTO {
specCode: string;
}
/** 同步规格成本到订单 */
/** 同步规格成本到订单(单个规格) */
export interface SyncCost {
/** 规格ID */
specId: number;
@@ -74,4 +74,14 @@ export namespace ProductSpecDTO {
/** 订单结束时间 */
endTime: string;
}
/** 批量同步规格成本到订单 */
export interface BatchSyncCost {
/** 规格ID列表 */
specIds: number[];
/** 订单开始时间 */
startTime: string;
/** 订单结束时间 */
endTime: string;
}
}
@@ -31,24 +31,8 @@ export namespace ProductVO {
/** 更新商品响应 */
export interface UpdateProductResult {
/** 本次更新中成本发生变更的规格列表 */
costChangedSpecs: CostChangedSpec[];
}
/** 成本变更的规格信息 */
export interface CostChangedSpec {
/** 规格ID */
id: number;
/** 规格名称 */
name: string;
/** 规格编码 */
specCode?: string;
/** 商品成本 */
productCost: number;
/** 运费成本 */
shippingCost?: number;
/** 其他成本 */
otherCost?: number;
/** 本次更新中成本发生变更的规格ID列表 */
costChangedSpecIds: number[];
}
/** 商品规格信息 */
@@ -19,7 +19,11 @@ import {
import dayjs from 'dayjs';
import { createProductApi, updateProductApi } from '#/api/ecommerce/product';
import { syncCostsToOrdersApi } from '#/api/ecommerce/product-spec/apis';
import {
batchSyncCostsToOrdersApi,
getProductSpecsByIdsApi,
} from '#/api/ecommerce/product-spec/apis';
import type { ProductSpecVO } from '#/api/ecommerce/product-spec/types';
import { uploadFileToOss } from '#/utils/oss-upload';
interface Emits {
@@ -67,17 +71,24 @@ const [SyncModal, syncModalApi] = useVbenModal({
});
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();
async function openSyncModal(costChangedSpecIds: number[]) {
try {
// 根据ID列表查询规格详情
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<ProductVO.CostChangedSpec[]>([]);
const pendingCostChangedSpecs = ref<ProductSpecVO.Info[]>([]);
const selectedSyncSpecIds = ref<number[]>([]);
async function handleSyncConfirm() {
@@ -85,22 +96,12 @@ async function handleSyncConfirm() {
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} 条订单`);
const totalUpdated = await batchSyncCostsToOrdersApi({
specIds: selectedSyncSpecIds.value,
startTime: startDate,
endTime: endDate,
});
message.success(`成本同步完成,共更新 ${totalUpdated ?? 0} 条订单`);
syncModalApi.close?.();
} catch (error) {
console.error('成本同步失败:', error);
@@ -242,9 +243,9 @@ async function handleSubmit() {
}),
};
const result = await updateProductApi(currentProductData.value.id, updateData);
if (specs.value.length > 0 && result.costChangedSpecs?.length > 0) {
if (specs.value.length > 0 && result.costChangedSpecIds?.length > 0) {
message.success('商品和规格更新成功');
openSyncModal(result.costChangedSpecs);
openSyncModal(result.costChangedSpecIds);
} else {
message.success('商品和规格更新成功');
}