feat(产品服务): 重构产品查询逻辑并添加价格字段
重构 getProductBySiteSku 方法以支持更灵活的查询条件 在 site-product 实体中添加 price 字段 新增 site-product 控制器和服务用于管理站点商品 修改订单服务以支持站点参数传递
This commit is contained in:
parent
2cc434bb19
commit
0dac006116
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -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', {
|
||||
|
|
|
|||
Loading…
Reference in New Issue