From 9d69af03b28ff0ee5306891d3670e6734d4792ad Mon Sep 17 00:00:00 2001 From: tikkhun Date: Tue, 27 Jan 2026 20:12:50 +0800 Subject: [PATCH 1/4] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E7=88=B6=E4=BA=A7?= =?UTF-8?q?=E5=93=81ID=E5=8F=AF=E8=83=BD=E4=B8=BAundefined=E6=97=B6?= =?UTF-8?q?=E7=9A=84=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/service/order.service.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/service/order.service.ts b/src/service/order.service.ts index 1a2d8a3..3a8e70b 100644 --- a/src/service/order.service.ts +++ b/src/service/order.service.ts @@ -745,7 +745,7 @@ export class OrderService { orderId: orderItem.orderId, siteId: orderItem.siteId, externalOrderItemId: orderItem.externalOrderItemId,// 原始 itemId - parentProductId: parentProduct.id, // 父产品 ID 用于统计套餐 如果是单品则不记录 + parentProductId: parentProduct?.id, // 父产品 ID 用于统计套餐 如果是单品则不记录 productId: product.id, isPackage: product.type === 'bundle',// 这里是否是套餐取决于父产品 name: product.name, -- 2.40.1 From d8a34220155fe3b0e4371245bb7c5defc4c90417 Mon Sep 17 00:00:00 2001 From: tikkhun Date: Wed, 28 Jan 2026 18:58:52 +0800 Subject: [PATCH 2/4] =?UTF-8?q?fix(product):=20=E4=BF=AE=E5=A4=8D=E4=BA=A7?= =?UTF-8?q?=E5=93=81=E5=B1=9E=E6=80=A7=E9=87=8D=E5=A4=8D=E6=A3=80=E6=9F=A5?= =?UTF-8?q?=E9=80=BB=E8=BE=91=E5=B9=B6=E6=B7=BB=E5=8A=A0=E6=97=A5=E5=BF=97?= =?UTF-8?q?=E8=AE=B0=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修复产品属性重复检查时未考虑属性数量的问题,确保只有当属性数量完全匹配时才认为重复 添加导入结果的调试日志记录 --- src/service/product.service.ts | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/src/service/product.service.ts b/src/service/product.service.ts index 362b6ca..47b9aa2 100644 --- a/src/service/product.service.ts +++ b/src/service/product.service.ts @@ -1,4 +1,4 @@ -import { Inject, Provide } from '@midwayjs/core'; +import { ILogger, Inject, Logger, Provide } from '@midwayjs/core'; import { Context } from '@midwayjs/koa'; import { InjectEntityModel } from '@midwayjs/typeorm'; import * as fs from 'fs'; @@ -38,6 +38,9 @@ import { Site } from '../entity/site.entity'; @Provide() export class ProductService { + @Logger() + logger: ILogger; // 注入 Logger 实例 + @Inject() ctx: Context; @@ -856,7 +859,7 @@ export class ProductService { // 检查完全相同属性组合是否已存在(避免重复) // 仅当产品类型为 'single' 且有属性时才检查重复 if (type === 'single' && resolvedAttributes.length > 0) { - const qb = this.productModel.createQueryBuilder('product'); + const qb = this.productModel.createQueryBuilder('product') resolvedAttributes.forEach((attr, index) => { qb.innerJoin( 'product.attributes', @@ -865,8 +868,12 @@ export class ProductService { { [`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(); - if (isExist) throw new Error('相同产品属性的产品已存在'); + if (isExist) throw new Error(`相同产品属性的产品已存在,sku 为${isExist?.sku}`); } // 创建新产品实例(绑定属性与基础字段) @@ -1778,19 +1785,13 @@ export class ProductService { 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 }[]> { if (!siteProduct.sku) { throw new Error('siteSku 不能为空') } - const product = await this.getProductBySiteSku(siteProduct.sku, site) + const product = await this.getProductBySiteSku(siteProduct.sku, site) if (!product) return @@ -2074,7 +2075,7 @@ export class ProductService { 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 }; } -- 2.40.1 From 534cc3f2c78c047e770b002aa4fc5385395eed2c Mon Sep 17 00:00:00 2001 From: tikkhun Date: Wed, 28 Jan 2026 21:27:37 +0800 Subject: [PATCH 3/4] =?UTF-8?q?refactor(=E8=AE=A2=E5=8D=95=E6=9C=8D?= =?UTF-8?q?=E5=8A=A1):=20=E7=A7=BB=E9=99=A4getComponentDetailFromSiteSku?= =?UTF-8?q?=E4=B8=AD=E4=B8=8D=E5=BF=85=E8=A6=81=E7=9A=84quantity=E5=8F=82?= =?UTF-8?q?=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 简化组件详情查询逻辑,默认数量设为1,不再需要外部传入quantity参数 --- src/service/order.service.ts | 2 +- src/service/product.service.ts | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/service/order.service.ts b/src/service/order.service.ts index 3a8e70b..2b899c2 100644 --- a/src/service/order.service.ts +++ b/src/service/order.service.ts @@ -733,7 +733,7 @@ export class OrderService { if (!orderItem.sku) return; // 从数据库查询产品,关联查询组件 - const componentDetails = await this.productService.getComponentDetailFromSiteSku({ sku: orderItem.sku, name: orderItem.name },orderItem.quantity,site); + const componentDetails = await this.productService.getComponentDetailFromSiteSku({ sku: orderItem.sku, name: orderItem.name }, site); if(!componentDetails?.length){ return } diff --git a/src/service/product.service.ts b/src/service/product.service.ts index 47b9aa2..9984dcf 100644 --- a/src/service/product.service.ts +++ b/src/service/product.service.ts @@ -1786,7 +1786,7 @@ export class ProductService { } } // 获取库存单品列表 - 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 }, site: Site): Promise<{ product: Product,parentProduct?: Product, quantity: number }[]> { if (!siteProduct.sku) { throw new Error('siteSku 不能为空') } @@ -1798,7 +1798,7 @@ export class ProductService { if(!product?.components?.length){ return [{ product, - quantity + quantity:1 }] } @@ -1808,7 +1808,7 @@ export class ProductService { where: { id: comp.productId }, }), parentProduct: product, // 这里得记录一下他的爸爸用来记录 - quantity: comp.quantity * quantity, + quantity: comp.quantity, } })) } -- 2.40.1 From f4a46bcf72bb41b3499a52a358f7aa8545bf854c Mon Sep 17 00:00:00 2001 From: tikkhun Date: Thu, 29 Jan 2026 09:19:16 +0800 Subject: [PATCH 4/4] =?UTF-8?q?fix(=E8=AE=A2=E5=8D=95=E6=9C=8D=E5=8A=A1):?= =?UTF-8?q?=20=E4=BF=AE=E5=A4=8D=E4=BA=A7=E5=93=81=E5=88=86=E7=B1=BB?= =?UTF-8?q?=E5=8F=AF=E8=83=BD=E4=B8=BA=E7=A9=BA=E7=9A=84=E9=94=99=E8=AF=AF?= =?UTF-8?q?=E5=B9=B6=E4=BC=98=E5=8C=96=E4=BA=A7=E5=93=81=E6=9F=A5=E8=AF=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修复订单服务中产品分类可能为空导致的错误,改为可选链操作 优化产品服务中组件查询逻辑,使用sku查询并添加关联关系 移除产品服务中不必要的组件查询逻辑 --- src/service/order.service.ts | 3 ++- src/service/product.service.ts | 10 +++------- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/src/service/order.service.ts b/src/service/order.service.ts index 2b899c2..477b004 100644 --- a/src/service/order.service.ts +++ b/src/service/order.service.ts @@ -740,6 +740,7 @@ export class OrderService { const orderSales: OrderSale[] = componentDetails.map(({product, parentProduct, quantity}) => { if (!product) return null + console.log('product',product) const attrsObj = this.productService.getAttributesObject(product.attributes) const orderSale = plainToClass(OrderSale, { orderId: orderItem.orderId, @@ -758,7 +759,7 @@ export class OrderService { flavor: attrsObj?.['flavor']?.name, humidity: attrsObj?.['humidity']?.name, size: attrsObj?.['size']?.name, - category: product.category.name, + category: product.category?.name, }); return orderSale }).filter(v => v !== null) diff --git a/src/service/product.service.ts b/src/service/product.service.ts index 9984dcf..c81959b 100644 --- a/src/service/product.service.ts +++ b/src/service/product.service.ts @@ -1786,7 +1786,7 @@ export class ProductService { } } // 获取库存单品列表 - async getComponentDetailFromSiteSku(siteProduct: { sku: string, name: string }, site: Site): Promise<{ product: Product,parentProduct?: Product, quantity: number }[]> { + async getComponentDetailFromSiteSku(siteProduct: { sku: string, name?: string }, site: Site): Promise<{ product: Product,parentProduct?: Product, quantity: number }[]> { if (!siteProduct.sku) { throw new Error('siteSku 不能为空') } @@ -1805,7 +1805,8 @@ export class ProductService { return await Promise.all(product.components.map(async comp => { return { product: await this.productModel.findOne({ - where: { id: comp.productId }, + where: { sku: comp.sku }, + relations: ['category', 'attributes', 'attributes.dict', 'components'] }), parentProduct: product, // 这里得记录一下他的爸爸用来记录 quantity: comp.quantity, @@ -2133,11 +2134,6 @@ export class ProductService { component.sku = product.sku; component.quantity = 1; product.components = [component]; - } else { - // 混装商品返回持久化的 SKU 组成 - product.components = await this.productStockComponentModel.find({ - where: { productId: product.id }, - }); } return product; -- 2.40.1