From 0b211628f3cffe04493ff0a2b05474f1e116f73b Mon Sep 17 00:00:00 2001 From: tikkhun Date: Thu, 29 Jan 2026 09:19:16 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E8=AE=A2=E5=8D=95=E6=9C=8D=E5=8A=A1):=20?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=BA=A7=E5=93=81=E5=88=86=E7=B1=BB=E5=8F=AF?= =?UTF-8?q?=E8=83=BD=E4=B8=BA=E7=A9=BA=E7=9A=84=E9=94=99=E8=AF=AF=E5=B9=B6?= =?UTF-8?q?=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;