mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-24 21:49:19 +08:00
fix: 规格重组按商家编码由 agent 指定对齐 SKU
This commit is contained in:
+141
-13
@@ -1621,17 +1621,20 @@ function onSkuSpecCellCommit(rowKey: number, axisKey: string) {
|
||||
/**
|
||||
* 规格槽位变化后按笛卡尔积生成 SKU;同槽位组合键保留已填价格/编码/图。
|
||||
* 改文案只改 token,槽位 id 不变,故不会丢商家编码。
|
||||
* orphanByIndex:槽位对不上时是否按原行序迁编码(人手改轴用);Agent 有 skuAssignments 时关闭。
|
||||
* 返回未纳入新表的旧行,供调用方按编码绑定或释放缩略图。
|
||||
*/
|
||||
function syncSkuRowsFromSpecs() {
|
||||
function syncSkuRowsFromSpecs(options?: { orphanByIndex?: boolean }): SkuRow[] {
|
||||
const orphanByIndex = options?.orphanByIndex !== false;
|
||||
const axes = specAxes.value;
|
||||
if (axes.length === 0) {
|
||||
clearSkuRows();
|
||||
return;
|
||||
return [];
|
||||
}
|
||||
const slotLists = axes.map((a) => effectiveSpecSlots(a));
|
||||
if (slotLists.some((list) => list.length === 0)) {
|
||||
clearSkuRows();
|
||||
return;
|
||||
return [];
|
||||
}
|
||||
|
||||
const combos = cartesianProduct(slotLists);
|
||||
@@ -1642,9 +1645,10 @@ function syncSkuRowsFromSpecs() {
|
||||
);
|
||||
}
|
||||
|
||||
const prevByKey = new Map(skuRows.value.map((r) => [r.comboKey, r]));
|
||||
const prevRows = skuRows.value;
|
||||
const prevByKey = new Map(prevRows.map((r) => [r.comboKey, r]));
|
||||
const next: SkuRow[] = [];
|
||||
const usedKeys = new Set<string>();
|
||||
const isFresh: boolean[] = [];
|
||||
|
||||
for (const combo of combos) {
|
||||
const parts = axes.map((axis, i) => ({
|
||||
@@ -1652,7 +1656,6 @@ function syncSkuRowsFromSpecs() {
|
||||
slotId: combo[i]!.id,
|
||||
}));
|
||||
const key = comboKeyFromSlots(parts);
|
||||
usedKeys.add(key);
|
||||
const saleValues: Record<string, string> = {};
|
||||
const labels: string[] = [];
|
||||
axes.forEach((axis, i) => {
|
||||
@@ -1667,6 +1670,7 @@ function syncSkuRowsFromSpecs() {
|
||||
prev.saleValues = saleValues;
|
||||
prev.comboKey = key;
|
||||
next.push(prev);
|
||||
isFresh.push(false);
|
||||
prevByKey.delete(key);
|
||||
} else {
|
||||
next.push({
|
||||
@@ -1679,14 +1683,104 @@ function syncSkuRowsFromSpecs() {
|
||||
thumbFileList: [],
|
||||
saleValues,
|
||||
});
|
||||
isFresh.push(true);
|
||||
}
|
||||
}
|
||||
|
||||
for (const orphan of prevByKey.values()) {
|
||||
revokeUploadFiles(orphan.thumbFileList);
|
||||
if (orphanByIndex) {
|
||||
const orphans = prevRows.filter((r) => prevByKey.has(r.comboKey));
|
||||
let orphanIdx = 0;
|
||||
for (let ni = 0; ni < next.length; ni++) {
|
||||
if (!isFresh[ni]) continue;
|
||||
const orphan = orphans[orphanIdx++];
|
||||
if (!orphan) break;
|
||||
adoptSkuRowBusinessFields(next[ni]!, orphan);
|
||||
prevByKey.delete(orphan.comboKey);
|
||||
}
|
||||
for (const orphan of prevByKey.values()) {
|
||||
revokeUploadFiles(orphan.thumbFileList);
|
||||
}
|
||||
skuRows.value = next;
|
||||
void scrollSkuTableToBottom();
|
||||
return [];
|
||||
}
|
||||
|
||||
const deferredOrphans = prevRows.filter((r) => prevByKey.has(r.comboKey));
|
||||
skuRows.value = next;
|
||||
void scrollSkuTableToBottom();
|
||||
return deferredOrphans;
|
||||
}
|
||||
|
||||
/** 规格重组时继承编码/价/图/内部绑定;保留目标行的 comboKey 与规格描述 */
|
||||
function adoptSkuRowBusinessFields(target: SkuRow, source: SkuRow) {
|
||||
target.key = source.key;
|
||||
target.outSkuSn = source.outSkuSn;
|
||||
target.priceYuan = source.priceYuan;
|
||||
target.multiPriceYuan = source.multiPriceYuan;
|
||||
target.quantity = source.quantity;
|
||||
target.weight = source.weight;
|
||||
target.thumbFileList = source.thumbFileList;
|
||||
target.internalSkuId = source.internalSkuId;
|
||||
target.productName = source.productName;
|
||||
target.internalSkuName = source.internalSkuName;
|
||||
target.codeMatchStatus = source.codeMatchStatus;
|
||||
}
|
||||
|
||||
/** 行规格展示值列表(与轴顺序一致,供 Agent 对齐) */
|
||||
function skuRowValueLabels(row: SkuRow) {
|
||||
return specAxes.value.map((a) =>
|
||||
labelOfAxisValue(a, row.saleValues[a.key] ?? ''),
|
||||
);
|
||||
}
|
||||
|
||||
/** 判断 SKU 行是否匹配 Agent 给出的 values(与当前轴同序,按 label/token) */
|
||||
function skuRowMatchesValueLabels(row: SkuRow, valueLabels: string[]) {
|
||||
const axes = specAxes.value;
|
||||
if (valueLabels.length !== axes.length) return false;
|
||||
return axes.every((axis, i) => {
|
||||
const want = String(valueLabels[i] ?? '').trim();
|
||||
const token = row.saleValues[axis.key] ?? '';
|
||||
const label = labelOfAxisValue(axis, token);
|
||||
const resolved = resolveSpecValueToken(axis, want);
|
||||
return (
|
||||
want === token ||
|
||||
want === label ||
|
||||
resolved === token ||
|
||||
resolved === label
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 按商家编码把旧行业务字段挂到 Agent 指定的新规格组合上。
|
||||
* outSkuSn 为唯一身份;values 与当前 axes 同序。
|
||||
*/
|
||||
function applySkuAssignmentsByOutSkuSn(
|
||||
assignments: Array<{ outSkuSn: string; values: string[] }>,
|
||||
prevRows: SkuRow[],
|
||||
) {
|
||||
const prevByCode = new Map<string, SkuRow>();
|
||||
for (const r of prevRows) {
|
||||
const code = r.outSkuSn.trim();
|
||||
if (code && !prevByCode.has(code)) prevByCode.set(code, r);
|
||||
}
|
||||
const usedCodes = new Set<string>();
|
||||
let bound = 0;
|
||||
for (const item of assignments) {
|
||||
const code = item.outSkuSn.trim();
|
||||
if (!code || usedCodes.has(code)) continue;
|
||||
const source = prevByCode.get(code);
|
||||
if (!source) continue;
|
||||
const target = skuRows.value.find(
|
||||
(r) =>
|
||||
!r.outSkuSn.trim() && skuRowMatchesValueLabels(r, item.values),
|
||||
);
|
||||
if (!target) continue;
|
||||
adoptSkuRowBusinessFields(target, source);
|
||||
usedCodes.add(code);
|
||||
bound += 1;
|
||||
}
|
||||
return bound;
|
||||
}
|
||||
|
||||
/** 将内部 SKU 绑定到某一组合行(只填编码,不新增行) */
|
||||
@@ -3122,13 +3216,13 @@ function getFormSnapshot() {
|
||||
catalogKey: a.catalogKey,
|
||||
values: effectiveSpecValues(a).map((v) => labelOfAxisValue(a, v)),
|
||||
})),
|
||||
/** outSkuSn 为 SKU 唯一身份;values 与 specAxes 同序,便于 agent 重排 */
|
||||
skuRows: skuRows.value.map((r) => ({
|
||||
skuName: r.skuName,
|
||||
outSkuSn: r.outSkuSn,
|
||||
values: skuRowValueLabels(r),
|
||||
skuName: r.skuName,
|
||||
quantity: r.quantity,
|
||||
weight: r.weight,
|
||||
saleValues: { ...r.saleValues },
|
||||
internalSkuId: r.internalSkuId,
|
||||
productName: r.productName,
|
||||
internalSkuName: r.internalSkuName,
|
||||
})),
|
||||
@@ -3151,12 +3245,29 @@ function applySetGoodsDesc(args: Record<string, unknown>) {
|
||||
return { ok: true, message: '已写入商品描述' };
|
||||
}
|
||||
|
||||
function parseSkuAssignments(raw: unknown) {
|
||||
if (!Array.isArray(raw)) return [];
|
||||
const list: Array<{ outSkuSn: string; values: string[] }> = [];
|
||||
for (const item of raw) {
|
||||
if (!item || typeof item !== 'object') continue;
|
||||
const row = item as { outSkuSn?: unknown; values?: unknown };
|
||||
const outSkuSn = String(row.outSkuSn ?? '').trim();
|
||||
const values = Array.isArray(row.values)
|
||||
? row.values.map((v) => String(v ?? '').trim())
|
||||
: [];
|
||||
if (!outSkuSn || values.length === 0) continue;
|
||||
list.push({ outSkuSn, values });
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
/** 按名称匹配类目规格目录;无目录时建自由轴。同轴按位置复用槽位 id,改文案保留 SKU。 */
|
||||
function applySetSpecAxes(args: Record<string, unknown>) {
|
||||
const raw = args.axes;
|
||||
if (!Array.isArray(raw) || raw.length === 0) {
|
||||
return { ok: false, message: '规格轴为空' };
|
||||
}
|
||||
const assignments = parseSkuAssignments(args.skuAssignments);
|
||||
const maxAxes = Math.min(2, Math.max(1, specCatalog.value.length || 2));
|
||||
const limited = raw.slice(0, maxAxes);
|
||||
const usedCatalog = new Set<string>();
|
||||
@@ -3166,6 +3277,7 @@ function applySetSpecAxes(args: Record<string, unknown>) {
|
||||
const prevByName = new Map(
|
||||
specAxes.value.map((a) => [a.name, a] as const),
|
||||
);
|
||||
const prevSkuRows = skuRows.value.map((r) => r);
|
||||
const next: SpecAxis[] = [];
|
||||
|
||||
for (const item of limited) {
|
||||
@@ -3224,10 +3336,26 @@ function applySetSpecAxes(args: Record<string, unknown>) {
|
||||
return { ok: false, message: '未能解析任何规格维' };
|
||||
}
|
||||
specAxes.value = next;
|
||||
syncSkuRowsFromSpecs();
|
||||
// 有编码绑定时关闭行序兜底,完全按 agent 的 outSkuSn 对齐
|
||||
const deferredOrphans = syncSkuRowsFromSpecs({
|
||||
orphanByIndex: assignments.length === 0,
|
||||
});
|
||||
const bound =
|
||||
assignments.length > 0
|
||||
? applySkuAssignmentsByOutSkuSn(assignments, prevSkuRows)
|
||||
: 0;
|
||||
for (const orphan of deferredOrphans) {
|
||||
const adopted = skuRows.value.some((r) => r.key === orphan.key);
|
||||
if (!adopted) {
|
||||
revokeUploadFiles(orphan.thumbFileList);
|
||||
}
|
||||
}
|
||||
return {
|
||||
ok: true,
|
||||
message: `已设置 ${next.length} 个规格维,SKU 行 ${skuRows.value.length} 条`,
|
||||
message:
|
||||
assignments.length > 0
|
||||
? `已设置 ${next.length} 个规格维,SKU ${skuRows.value.length} 条,按编码绑定 ${bound}/${assignments.length}`
|
||||
: `已设置 ${next.length} 个规格维,SKU 行 ${skuRows.value.length} 条`,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
+4
-3
@@ -9,12 +9,13 @@ export type PublishSpecAxisSnapshot = {
|
||||
};
|
||||
|
||||
export type PublishSkuRowSnapshot = {
|
||||
skuName: string;
|
||||
/** 商家编码:SKU 唯一身份 */
|
||||
outSkuSn: string;
|
||||
/** 与 specAxes 同序的规格值文案 */
|
||||
values: string[];
|
||||
skuName: string;
|
||||
quantity: number;
|
||||
weight: number;
|
||||
saleValues: Record<string, string>;
|
||||
internalSkuId?: number;
|
||||
productName?: string;
|
||||
internalSkuName?: string;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user