product: 规格新增关联平台店铺字段,ProductSpec 支持绑定店铺列表

- ProductServiceImpl / ProductSpecServiceImpl: 规格关联平台店铺逻辑
- UpdateProductDTO: 增加 platform_shop_ids 字段
- OrderMapper: 关联查询优化
- 前端: ProductFormModal / ProductCard 适配多店铺绑定
This commit is contained in:
小蟑螂
2026-05-03 23:50:03 +08:00
parent 63e54279c0
commit 74eb45b76c
7 changed files with 279 additions and 54 deletions
@@ -188,8 +188,10 @@ const goToDetails = (product: ProductVO.Info) => {
const MAX_VISIBLE_SPECS = 4;
const visibleSpecs = computed(() => (props.product.specs || []).slice(0, MAX_VISIBLE_SPECS));
/** id 仅来自后端的已有规格;新增行不传 id,用 _clientKey 做列表 key */
type EditableSpec = {
id: number;
id?: number;
_clientKey?: string;
name: string;
otherCost?: number;
productCost: number;
@@ -198,6 +200,11 @@ type EditableSpec = {
specCode?: string;
};
let nextSpecRowKey = 0;
/** 进入编辑时商品上已有的规格 ID,用于提交时生成 removedSpecIds */
const initialEditSpecIds = ref<Set<number>>(new Set());
const isEditing = ref(false);
const isSaving = ref(false);
const originalSnapshot = ref('');
@@ -232,6 +239,7 @@ const normalizeDraft = () => ({
remark: (spec.remark || '').trim(),
shippingCost: Number(spec.shippingCost || 0),
specCode: (spec.specCode || '').trim(),
...(spec._clientKey ? { _cid: spec._clientKey } : {}),
})),
});
@@ -260,6 +268,9 @@ const initDraftFromProps = () => {
specCode: spec.specCode || '',
})),
};
initialEditSpecIds.value = new Set(
(props.product.specs || []).map((s) => s.id).filter((id) => id != null),
);
newImageFile.value = null;
originalImageUrl.value = props.product.mainImage || '';
imageLoadError.value = false;
@@ -292,7 +303,7 @@ const formatCost = (val?: number) => `¥${(val ?? 0).toFixed(2)}`;
function addSpec() {
draft.value.specs.push({
id: -Date.now(),
_clientKey: `new-${++nextSpecRowKey}`,
name: '',
otherCost: 0,
productCost: 0,
@@ -391,23 +402,37 @@ const saveEdit = async () => {
if (newImageFile.value) {
mainImageId = await uploadImageAndGetId();
}
const keptSpecIds = new Set(
draft.value.specs
.map((s) => s.id)
.filter((id): id is number => id != null && id > 0),
);
const removedSpecIds = [...initialEditSpecIds.value].filter(
(sid) => !keptSpecIds.has(sid),
);
const specsPayload = draft.value.specs.map((spec) => {
const base = {
name: spec.name.trim(),
otherCost: Number(spec.otherCost || 0),
productCost: Number(spec.productCost || 0),
remark: (spec.remark || '').trim(),
shippingCost: Number(spec.shippingCost || 0),
specCode: spec.specCode?.trim() || undefined,
};
if (spec.id != null && spec.id > 0) {
return { id: spec.id, ...base } as ProductDTO.UpdateSpec;
}
return { ...base } as ProductDTO.UpdateSpec;
});
const payload: ProductDTO.Update = {
name: draft.value.name.trim(),
productCode: draft.value.productCode.trim() || undefined,
categoryId: draft.value.categoryId,
mainImageId,
specs: draft.value.specs.map((spec) => {
const base = {
name: spec.name.trim(),
otherCost: Number(spec.otherCost || 0),
productCost: Number(spec.productCost || 0),
remark: (spec.remark || '').trim(),
shippingCost: Number(spec.shippingCost || 0),
specCode: spec.specCode?.trim() || undefined,
};
if (spec.id < 0) return base as ProductDTO.UpdateSpec;
return { id: spec.id, ...base } as ProductDTO.UpdateSpec;
}),
...(draft.value.specs.length > 0 && { specs: specsPayload }),
...(removedSpecIds.length > 0 && { removedSpecIds }),
};
const result = await updateProductApi(props.product.id, payload);
message.success('商品保存成功');
@@ -537,7 +562,7 @@ const saveEdit = async () => {
</div>
<div
v-for="(spec, index) in displaySpecs"
:key="spec.id"
:key="isEditing ? spec.id ?? spec._clientKey ?? `idx-${index}` : spec.id"
class="spec-row"
:class="{ 'editing-row': isEditing }"
>
@@ -197,10 +197,20 @@ const formData = ref({
// 类目选项列表
const categoryOptions = ref<any[]>([]);
/** 表格行稳定 key(新增行无 id) */
type SpecFormRow = (ProductDTO.Spec | ProductDTO.UpdateSpec) & {
_clientKey?: string;
};
// 规格数据 - 支持创建和更新
const specs = ref<Array<ProductDTO.Spec | ProductDTO.UpdateSpec>>([]);
const specs = ref<SpecFormRow[]>([]);
const showSpecs = ref(false);
/** 编辑打开时已有的规格 ID,用于提交时计算 removedSpecIds */
const initialEditSpecIds = ref<Set<number>>(new Set());
let nextSpecClientKey = 0;
// 当前显示的图片 URL(优先显示新选择的)
const currentImageUrl = computed(() => {
if (newImageFile.value) {
@@ -245,6 +255,7 @@ function initFormData() {
) {
const mapSpec = (spec: ProductVO.SpecInfo) => ({
name: spec.name || spec.specCode || '',
linkName: spec.linkName || '',
specCode: spec.specCode || '',
productCost: spec.productCost ?? 0,
shippingCost: spec.shippingCost ?? 0,
@@ -252,18 +263,30 @@ function initFormData() {
remark: spec.remark || '',
});
specs.value = isEdit.value
? currentProductData.value.specs.map(
(spec) =>
({
id: (spec as ProductVO.SpecInfo).id,
...mapSpec(spec as ProductVO.SpecInfo),
}) as ProductDTO.UpdateSpec,
)
: currentProductData.value.specs.map((spec) =>
mapSpec(spec as ProductVO.SpecInfo),
);
? currentProductData.value.specs.map((spec) => {
const s = spec as ProductVO.SpecInfo;
return {
id: s.id,
_clientKey: `exist-${s.id}`,
...mapSpec(s),
} as SpecFormRow;
})
: currentProductData.value.specs.map((spec) => ({
_clientKey: `seed-${++nextSpecClientKey}`,
...mapSpec(spec as ProductVO.SpecInfo),
}));
showSpecs.value = specs.value.length > 0;
}
if (isEdit.value) {
initialEditSpecIds.value = new Set(
specs.value
.map((s) => (s.id != null ? Number(s.id) : null))
.filter((x): x is number => x != null),
);
} else {
initialEditSpecIds.value = new Set();
}
}
// 重置表单(在open方法中调用)
@@ -274,6 +297,7 @@ function resetForm() {
imageLoadError.value = false;
specs.value = [];
showSpecs.value = false;
initialEditSpecIds.value = new Set();
categoryOptions.value = [];
}
@@ -304,14 +328,49 @@ async function handleSubmit() {
'id' in currentProductData.value &&
currentProductData.value.id
) {
const keptSpecIds = new Set(
specs.value
.map((s) => (s.id != null ? Number(s.id) : null))
.filter((x): x is number => x != null),
);
const removedSpecIds = [...initialEditSpecIds.value].filter(
(sid) => !keptSpecIds.has(sid),
);
const specsPayload: ProductDTO.UpdateSpec[] = specs.value.map((row) => {
const {
_clientKey: _c,
id,
name,
linkName,
specCode,
productCost,
shippingCost,
otherCost,
remark,
} = row;
const base = {
name,
linkName: linkName || undefined,
specCode: specCode ?? '',
productCost: productCost ?? 0,
shippingCost: shippingCost ?? 0,
otherCost: otherCost ?? 0,
remark: remark ?? '',
};
if (id != null && id !== undefined) {
return { ...base, id: Number(id) };
}
return { ...base };
});
const updateData: ProductDTO.Update = {
name: submitData.name,
productCode: submitData.productCode || undefined,
categoryId: submitData.categoryId,
mainImageId: submitData.mainImageId,
...(specs.value.length > 0 && {
specs: specs.value as ProductDTO.UpdateSpec[],
}),
...(specs.value.length > 0 && { specs: specsPayload }),
...(removedSpecIds.length > 0 && { removedSpecIds }),
};
const result = await updateProductApi(currentProductData.value.id, updateData);
if (specs.value.length > 0 && result.costChangedSpecIds?.length > 0) {
@@ -327,7 +386,9 @@ async function handleSubmit() {
productCode: submitData.productCode || undefined,
categoryId: submitData.categoryId,
mainImageId: submitData.mainImageId,
...(specs.value.length > 0 && { specs: specs.value }),
...(specs.value.length > 0 && {
specs: specs.value.map(({ _clientKey: _c, id: _id, ...rest }) => rest),
}),
};
await createProductApi(createData);
if (specs.value.length > 0) {
@@ -429,8 +490,10 @@ function removeNewImage() {
message.success('已重置图片');
}
const defaultSpec = (): ProductDTO.Spec => ({
const defaultSpec = (): SpecFormRow => ({
_clientKey: `new-${++nextSpecClientKey}`,
name: '',
linkName: '',
specCode: '',
productCost: 0,
shippingCost: 0,
@@ -440,11 +503,7 @@ const defaultSpec = (): ProductDTO.Spec => ({
// 添加规格
function addSpec() {
if (isEdit.value) {
specs.value.push(defaultSpec() as ProductDTO.Spec);
} else {
specs.value.push(defaultSpec());
}
specs.value.push(defaultSpec());
}
// 删除规格
@@ -462,7 +521,7 @@ const specColumns = [
customRender: ({
record,
}: {
record: ProductDTO.Spec | ProductDTO.UpdateSpec;
record: SpecFormRow;
}) => {
return h(Input, {
value: record.name,
@@ -481,7 +540,7 @@ const specColumns = [
customRender: ({
record,
}: {
record: ProductDTO.Spec | ProductDTO.UpdateSpec;
record: SpecFormRow;
}) => {
return h(Input, {
value: record.specCode,
@@ -500,7 +559,7 @@ const specColumns = [
customRender: ({
record,
}: {
record: ProductDTO.Spec | ProductDTO.UpdateSpec;
record: SpecFormRow;
}) => {
return h(InputNumber, {
value: record.productCost,
@@ -522,7 +581,7 @@ const specColumns = [
customRender: ({
record,
}: {
record: ProductDTO.Spec | ProductDTO.UpdateSpec;
record: SpecFormRow;
}) => {
return h(InputNumber, {
value: record.shippingCost ?? 0,
@@ -544,7 +603,7 @@ const specColumns = [
customRender: ({
record,
}: {
record: ProductDTO.Spec | ProductDTO.UpdateSpec;
record: SpecFormRow;
}) => {
return h(InputNumber, {
value: record.otherCost ?? 0,
@@ -566,7 +625,7 @@ const specColumns = [
customRender: ({
record,
}: {
record: ProductDTO.Spec | ProductDTO.UpdateSpec;
record: SpecFormRow;
}) => {
return h(Input, {
value: record.remark ?? '',
@@ -792,7 +851,7 @@ onUnmounted(() => {
:columns="specColumns"
:data-source="specs"
:pagination="false"
row-key="index"
:row-key="(r: SpecFormRow) => (r.id != null ? String(r.id) : r._clientKey ?? '')"
size="small"
>
<template #emptyText>