fix(product): 修复产品属性重复检查逻辑并添加日志记录
修复产品属性重复检查时未考虑属性数量的问题,确保只有当属性数量完全匹配时才认为重复 添加导入结果的调试日志记录
This commit is contained in:
parent
efba2278b0
commit
5530c3220f
|
|
@ -1,4 +1,4 @@
|
||||||
import { Inject, Provide } from '@midwayjs/core';
|
import { ILogger, Inject, Logger, Provide } from '@midwayjs/core';
|
||||||
import { Context } from '@midwayjs/koa';
|
import { Context } from '@midwayjs/koa';
|
||||||
import { InjectEntityModel } from '@midwayjs/typeorm';
|
import { InjectEntityModel } from '@midwayjs/typeorm';
|
||||||
import * as fs from 'fs';
|
import * as fs from 'fs';
|
||||||
|
|
@ -38,6 +38,9 @@ import { Site } from '../entity/site.entity';
|
||||||
|
|
||||||
@Provide()
|
@Provide()
|
||||||
export class ProductService {
|
export class ProductService {
|
||||||
|
@Logger()
|
||||||
|
logger: ILogger; // 注入 Logger 实例
|
||||||
|
|
||||||
@Inject()
|
@Inject()
|
||||||
ctx: Context;
|
ctx: Context;
|
||||||
|
|
||||||
|
|
@ -856,7 +859,7 @@ export class ProductService {
|
||||||
// 检查完全相同属性组合是否已存在(避免重复)
|
// 检查完全相同属性组合是否已存在(避免重复)
|
||||||
// 仅当产品类型为 'single' 且有属性时才检查重复
|
// 仅当产品类型为 'single' 且有属性时才检查重复
|
||||||
if (type === 'single' && resolvedAttributes.length > 0) {
|
if (type === 'single' && resolvedAttributes.length > 0) {
|
||||||
const qb = this.productModel.createQueryBuilder('product');
|
const qb = this.productModel.createQueryBuilder('product')
|
||||||
resolvedAttributes.forEach((attr, index) => {
|
resolvedAttributes.forEach((attr, index) => {
|
||||||
qb.innerJoin(
|
qb.innerJoin(
|
||||||
'product.attributes',
|
'product.attributes',
|
||||||
|
|
@ -865,8 +868,12 @@ export class ProductService {
|
||||||
{ [`attrId${index}`]: attr.id }
|
{ [`attrId${index}`]: attr.id }
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
// 添加属性数量的判断,确保产品的属性数量与指定的属性数量相同
|
||||||
|
qb.andWhere('(SELECT COUNT(*) FROM product_attributes_dict_item pad WHERE pad.productId = product.id) = :attrCount', {
|
||||||
|
attrCount: resolvedAttributes.length
|
||||||
|
});
|
||||||
const isExist = await qb.getOne();
|
const isExist = await qb.getOne();
|
||||||
if (isExist) throw new Error('相同产品属性的产品已存在');
|
if (isExist) throw new Error(`相同产品属性的产品已存在,sku 为${isExist?.sku}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 创建新产品实例(绑定属性与基础字段)
|
// 创建新产品实例(绑定属性与基础字段)
|
||||||
|
|
@ -1778,13 +1785,7 @@ export class ProductService {
|
||||||
attributes: attributes.length > 0 ? attributes : undefined,
|
attributes: attributes.length > 0 ? attributes : undefined,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
isMixedSku(sku: string) {
|
// 获取库存单品列表
|
||||||
const splitSKu = sku.split('-')
|
|
||||||
const last = splitSKu[splitSKu.length - 1]
|
|
||||||
const second = splitSKu[splitSKu.length - 2]
|
|
||||||
// 这里判断 second 是否是数字
|
|
||||||
return sku.includes('-MX-') || sku.includes('-Mixed-') || /^\d+$/.test(second) && /^\d+$/.test(last)
|
|
||||||
}
|
|
||||||
async getComponentDetailFromSiteSku(siteProduct: { sku: string, name: string }, quantity: number = 1, site: Site): Promise<{ product: Product,parentProduct?: Product, quantity: number }[]> {
|
async getComponentDetailFromSiteSku(siteProduct: { sku: string, name: string }, quantity: number = 1, site: Site): Promise<{ product: Product,parentProduct?: Product, quantity: number }[]> {
|
||||||
if (!siteProduct.sku) {
|
if (!siteProduct.sku) {
|
||||||
throw new Error('siteSku 不能为空')
|
throw new Error('siteSku 不能为空')
|
||||||
|
|
@ -2074,7 +2075,7 @@ export class ProductService {
|
||||||
errors.push({ identifier: '' + rec.sku, error: `产品${rec?.sku}导入失败:${e?.message || String(e)}` });
|
errors.push({ identifier: '' + rec.sku, error: `产品${rec?.sku}导入失败:${e?.message || String(e)}` });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
this.logger.debug(`导入 ${records.length} 条记录,成功创建 ${created} 条,更新 ${updated} 条,失败 ${errors.length} 条,错误详情:${JSON.stringify(errors)}`);
|
||||||
return { total: records.length, processed: records.length - errors.length, created, updated, errors };
|
return { total: records.length, processed: records.length - errors.length, created, updated, errors };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue