diff --git a/src/service/order.service.ts b/src/service/order.service.ts index e657df2..0cc42ed 100644 --- a/src/service/order.service.ts +++ b/src/service/order.service.ts @@ -715,21 +715,18 @@ export class OrderService { } if (!orderItem.sku) return; // 从数据库查询产品,关联查询组件 - const product = await this.productModel.findOne({ - where: { siteSkus: Like(`%${orderItem.sku}%`) }, - relations: ['components','attributes','attributes.dict'], - }); - - if (!product) return; + const productDetail = await this.productService.getComponentDetailFromSiteSku({ sku: orderItem.sku, name: orderItem.name }); + + if (!productDetail || !productDetail.quantity) return; + const {product, quantity} = productDetail const componentDetails: { product: Product, quantity: number }[] = product.components?.length > 0 ? await Promise.all(product.components.map(async comp => { return { product: await this.productModel.findOne({ - where: { sku: comp.sku }, - relations: ['components', 'attributes','attributes.dict'], + where: { id: comp.productId }, }), quantity: comp.quantity * orderItem.quantity, } - })) : [{ product, quantity: orderItem.quantity }] + })) : [{ product, quantity }] const orderSales: OrderSale[] = componentDetails.map(componentDetail => { if (!componentDetail.product) return null diff --git a/src/service/product.service.ts b/src/service/product.service.ts index 35b923d..41b168d 100644 --- a/src/service/product.service.ts +++ b/src/service/product.service.ts @@ -635,7 +635,7 @@ export class ProductService { }); } function nameToTitle(name: string) { - return name.replace('-',' '); + return name.replace('-', ' '); } if (!categoryItem && categoryName) { const category = new Category(); @@ -1453,6 +1453,59 @@ 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 }) { + if (!siteProduct.sku) { + throw new Error('siteSku 不能为空') + } + + let product = await this.productModel.findOne({ + where: { siteSkus: Like(`%${siteProduct.sku}%`) }, + relations: ['components', 'attributes', 'attributes.dict'], + }); + let quantity = 1; + // 这里处理一下特殊情况,就是无法直接通过 siteProduct.sku去获取, 但有一定规则转换成有的产品,就是 bundle 的部分 + // 考察各个站点的 bundle 规则, 会发现 + // wordpress: + // togovape YOONE Wintergreen 9MG (Moisture) - 10 cans TV-YOONE-NP-S-WG-9MG-0010 + // togovape mixed 是这样的 TV-YOONE-NP-G-12MG-MX-0003 TV-ZEX-NP-Mixed-12MG-0001 + // + // shopyy: shopyy 已经 + // 只有 bundle 做这个处理 + if (!product && !this.isMixedSku(siteProduct.sku)) { + const skuSplitArr = siteProduct.sku.split('-') + const quantityStr = skuSplitArr[skuSplitArr.length - 1] + const isBundleSku = quantityStr.startsWith('0') + if(!isBundleSku){ + return undefined + } + quantity = Number(quantityStr) + if(!isBundleSku){ + return undefined + } + // 更正为正确的站点 sku + const childSku = skuSplitArr.slice(0, skuSplitArr.length - 1).join('-') + // 重新获取匹配的商品 + product = await this.productModel.findOne({ + where: { siteSkus: Like(`%${childSku}%`) }, + relations: ['components', 'attributes', 'attributes.dict'], + }); + } + + if (!product) { + throw new Error(`产品 ${siteProduct.sku} 不存在`); + } + return { + product, + quantity, + } + } // 准备创建产品的 DTO, 处理类型转换和默认值 prepareCreateProductDTO(data: any): CreateProductDTO {