zksu
/
API
forked from yoone/API
1
0
Fork 0

refactor(订单服务): 重构订单组件详情获取逻辑

将订单服务中的产品详情查询逻辑提取到产品服务中
处理混合SKU和bundle产品的特殊情况
This commit is contained in:
tikkhun 2026-01-17 16:57:06 +08:00
parent b7101ac866
commit b3b7ee4793
2 changed files with 60 additions and 10 deletions

View File

@ -715,21 +715,18 @@ export class OrderService {
} }
if (!orderItem.sku) return; if (!orderItem.sku) return;
// 从数据库查询产品,关联查询组件 // 从数据库查询产品,关联查询组件
const product = await this.productModel.findOne({ const productDetail = await this.productService.getComponentDetailFromSiteSku({ sku: orderItem.sku, name: orderItem.name });
where: { siteSkus: Like(`%${orderItem.sku}%`) },
relations: ['components','attributes','attributes.dict'],
});
if (!product) return; 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 => { const componentDetails: { product: Product, quantity: number }[] = product.components?.length > 0 ? await Promise.all(product.components.map(async comp => {
return { return {
product: await this.productModel.findOne({ product: await this.productModel.findOne({
where: { sku: comp.sku }, where: { id: comp.productId },
relations: ['components', 'attributes','attributes.dict'],
}), }),
quantity: comp.quantity * orderItem.quantity, quantity: comp.quantity * orderItem.quantity,
} }
})) : [{ product, quantity: orderItem.quantity }] })) : [{ product, quantity }]
const orderSales: OrderSale[] = componentDetails.map(componentDetail => { const orderSales: OrderSale[] = componentDetails.map(componentDetail => {
if (!componentDetail.product) return null if (!componentDetail.product) return null

View File

@ -635,7 +635,7 @@ export class ProductService {
}); });
} }
function nameToTitle(name: string) { function nameToTitle(name: string) {
return name.replace('-',' '); return name.replace('-', ' ');
} }
if (!categoryItem && categoryName) { if (!categoryItem && categoryName) {
const category = new Category(); const category = new Category();
@ -1453,6 +1453,59 @@ export class ProductService {
attributes: attributes.length > 0 ? attributes : undefined, 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, 处理类型转换和默认值 // 准备创建产品的 DTO, 处理类型转换和默认值
prepareCreateProductDTO(data: any): CreateProductDTO { prepareCreateProductDTO(data: any): CreateProductDTO {