refactor(product): 重构获取组件详情逻辑并支持数量参数

将获取组件详情的逻辑从order.service.ts移到product.service.ts中统一处理
新增quantity参数支持组件数量计算
返回结果中增加parentProduct信息用于追踪父产品
This commit is contained in:
tikkhun 2026-01-27 18:53:54 +08:00
parent d3d493f858
commit b879202d13
2 changed files with 32 additions and 27 deletions

View File

@ -733,33 +733,24 @@ export class OrderService {
if (!orderItem.sku) return; if (!orderItem.sku) return;
// 从数据库查询产品,关联查询组件 // 从数据库查询产品,关联查询组件
const productDetail = await this.productService.getComponentDetailFromSiteSku({ sku: orderItem.sku, name: orderItem.name },site); const componentDetails = await this.productService.getComponentDetailFromSiteSku({ sku: orderItem.sku, name: orderItem.name },orderItem.quantity,site);
if(!componentDetails?.length){
return
}
if (!productDetail || !productDetail.product || !productDetail.quantity) return; const orderSales: OrderSale[] = componentDetails.map(({product, parentProduct, quantity}) => {
const { product, quantity } = productDetail if (!product) return null
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 attrsObj = this.productService.getAttributesObject(product.attributes)
const orderSale = plainToClass(OrderSale, { const orderSale = plainToClass(OrderSale, {
orderId: orderItem.orderId, orderId: orderItem.orderId,
siteId: orderItem.siteId, siteId: orderItem.siteId,
externalOrderItemId: orderItem.externalOrderItemId,// 原始 itemId externalOrderItemId: orderItem.externalOrderItemId,// 原始 itemId
parentProductId: product.id, // 父产品 ID 用于统计套餐 如果是单品则不记录 parentProductId: parentProduct.id, // 父产品 ID 用于统计套餐 如果是单品则不记录
productId: componentDetail.product.id, productId: product.id,
isPackage: product.type === 'bundle',// 这里是否是套餐取决于父产品 isPackage: product.type === 'bundle',// 这里是否是套餐取决于父产品
name: componentDetail.product.name, name: product.name,
quantity: componentDetail.quantity * orderItem.quantity, quantity: quantity * orderItem.quantity,
sku: componentDetail.product.sku, sku: product.sku,
// 理论上直接存 product 的全部数据才是对的,因为这样我的数据才全面。 // 理论上直接存 product 的全部数据才是对的,因为这样我的数据才全面。
brand: attrsObj?.['brand']?.name, brand: attrsObj?.['brand']?.name,
version: attrsObj?.['version']?.name, version: attrsObj?.['version']?.name,
@ -767,7 +758,7 @@ export class OrderService {
flavor: attrsObj?.['flavor']?.name, flavor: attrsObj?.['flavor']?.name,
humidity: attrsObj?.['humidity']?.name, humidity: attrsObj?.['humidity']?.name,
size: attrsObj?.['size']?.name, size: attrsObj?.['size']?.name,
category: componentDetail.product.category.name, category: product.category.name,
}); });
return orderSale return orderSale
}).filter(v => v !== null) }).filter(v => v !== null)

View File

@ -1785,17 +1785,31 @@ export class ProductService {
// 这里判断 second 是否是数字 // 这里判断 second 是否是数字
return sku.includes('-MX-') || sku.includes('-Mixed-') || /^\d+$/.test(second) && /^\d+$/.test(last) return sku.includes('-MX-') || sku.includes('-Mixed-') || /^\d+$/.test(second) && /^\d+$/.test(last)
} }
async getComponentDetailFromSiteSku(siteProduct: { sku: string, name: string }, site: Site) { 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 不能为空')
} }
let product = await this.getProductBySiteSku(siteProduct.sku, site) const product = await this.getProductBySiteSku(siteProduct.sku, site)
return { if (!product) return
product,
quantity: 1, if(!product?.components?.length){
return [{
product,
quantity
}]
} }
return await Promise.all(product.components.map(async comp => {
return {
product: await this.productModel.findOne({
where: { id: comp.productId },
}),
parentProduct: product, // 这里得记录一下他的爸爸用来记录
quantity: comp.quantity * quantity,
}
}))
} }
// 准备创建产品的 DTO, 处理类型转换和默认值 // 准备创建产品的 DTO, 处理类型转换和默认值