From 0dac006116a9b26112b9e9afe6bf69abf8bf0123 Mon Sep 17 00:00:00 2001 From: tikkhun Date: Tue, 27 Jan 2026 10:29:50 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E4=BA=A7=E5=93=81=E6=9C=8D=E5=8A=A1):=20?= =?UTF-8?q?=E9=87=8D=E6=9E=84=E4=BA=A7=E5=93=81=E6=9F=A5=E8=AF=A2=E9=80=BB?= =?UTF-8?q?=E8=BE=91=E5=B9=B6=E6=B7=BB=E5=8A=A0=E4=BB=B7=E6=A0=BC=E5=AD=97?= =?UTF-8?q?=E6=AE=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 重构 getProductBySiteSku 方法以支持更灵活的查询条件 在 site-product 实体中添加 price 字段 新增 site-product 控制器和服务用于管理站点商品 修改订单服务以支持站点参数传递 --- src/service/order.service.ts | 21 +++++++++++++------- src/service/product.service.ts | 35 +++++++++++++--------------------- 2 files changed, 27 insertions(+), 29 deletions(-) diff --git a/src/service/order.service.ts b/src/service/order.service.ts index fb3baf1..94afb97 100644 --- a/src/service/order.service.ts +++ b/src/service/order.service.ts @@ -733,14 +733,21 @@ export class OrderService { if (!orderItem.sku) return; // 从数据库查询产品,关联查询组件 - const componentDetails = await this.productService.getComponentDetailFromSiteSku({ sku: orderItem.sku, name: orderItem.name }, site); - if(!componentDetails?.length){ - return - } + const productDetail = await this.productService.getComponentDetailFromSiteSku({ sku: orderItem.sku, name: orderItem.name },site); - const orderSales: OrderSale[] = componentDetails.map(({product, parentProduct, quantity}) => { - if (!product) return null - console.log('product',product) + 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: { id: comp.productId }, + }), + quantity: comp.quantity * orderItem.quantity, + } + })) : [{ product, quantity }] + + const orderSales: OrderSale[] = componentDetails.map(componentDetail => { + if (!componentDetail.product) return null const attrsObj = this.productService.getAttributesObject(product.attributes) const orderSale = plainToClass(OrderSale, { orderId: orderItem.orderId, diff --git a/src/service/product.service.ts b/src/service/product.service.ts index 7c14d37..830c7c2 100644 --- a/src/service/product.service.ts +++ b/src/service/product.service.ts @@ -1785,33 +1785,24 @@ export class ProductService { attributes: attributes.length > 0 ? attributes : undefined, } } - // 获取库存单品列表 - async getComponentDetailFromSiteSku(siteProduct: { sku: string, name?: string }, site: Site): Promise<{ product: Product,parentProduct?: Product, quantity: number }[]> { + 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 }, site: Site) { if (!siteProduct.sku) { throw new Error('siteSku 不能为空') } - const product = await this.getProductBySiteSku(siteProduct.sku, site) + let product = await this.getProductBySiteSku(siteProduct.sku, site) - if (!product) return - - if(!product?.components?.length){ - return [{ - product, - quantity:1 - }] + return { + product, + quantity: 1, } - - return await Promise.all(product.components.map(async comp => { - return { - product: await this.productModel.findOne({ - where: { sku: comp.sku }, - relations: ['category', 'attributes', 'attributes.dict', 'components'] - }), - parentProduct: product, // 这里得记录一下他的爸爸用来记录 - quantity: comp.quantity, - } - })) } // 准备创建产品的 DTO, 处理类型转换和默认值 @@ -2150,7 +2141,7 @@ export class ProductService { .leftJoinAndSelect('product.components', 'components') .leftJoinAndSelect('product.siteSkus', 'siteSku') .where('siteSku.sku LIKE :siteSku', { siteSku: `%${siteSku}%` }) - .orWhere('product.sku = :siteSku', { siteSku }) + .orWhere('product.sku = :siteSku', { siteSku }); if (site) { queryBuilder.orWhere('product.sku = :processedSku', {