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

Compare commits

..

No commits in common. "792ec5aaf05d172cc2aea72a77aab14ec9ab170d" and "efba2278b0e8e815ad1812abfc8d8a697e869029" have entirely different histories.

2 changed files with 23 additions and 21 deletions

View File

@ -733,14 +733,13 @@ export class OrderService {
if (!orderItem.sku) return;
// 从数据库查询产品,关联查询组件
const componentDetails = 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
}
const orderSales: OrderSale[] = componentDetails.map(({product, parentProduct, quantity}) => {
if (!product) return null
console.log('product',product)
const attrsObj = this.productService.getAttributesObject(product.attributes)
const orderSale = plainToClass(OrderSale, {
orderId: orderItem.orderId,
@ -759,7 +758,7 @@ export class OrderService {
flavor: attrsObj?.['flavor']?.name,
humidity: attrsObj?.['humidity']?.name,
size: attrsObj?.['size']?.name,
category: product.category?.name,
category: product.category.name,
});
return orderSale
}).filter(v => v !== null)

View File

@ -1,4 +1,4 @@
import { ILogger, Inject, Logger, Provide } from '@midwayjs/core';
import { Inject, Provide } from '@midwayjs/core';
import { Context } from '@midwayjs/koa';
import { InjectEntityModel } from '@midwayjs/typeorm';
import * as fs from 'fs';
@ -38,9 +38,6 @@ import { Site } from '../entity/site.entity';
@Provide()
export class ProductService {
@Logger()
logger: ILogger; // 注入 Logger 实例
@Inject()
ctx: Context;
@ -859,7 +856,7 @@ export class ProductService {
// 检查完全相同属性组合是否已存在(避免重复)
// 仅当产品类型为 'single' 且有属性时才检查重复
if (type === 'single' && resolvedAttributes.length > 0) {
const qb = this.productModel.createQueryBuilder('product')
const qb = this.productModel.createQueryBuilder('product');
resolvedAttributes.forEach((attr, index) => {
qb.innerJoin(
'product.attributes',
@ -868,12 +865,8 @@ export class ProductService {
{ [`attrId${index}`]: attr.id }
);
});
// 添加属性数量的判断,确保产品的属性数量与指定的属性数量相同
qb.andWhere('(SELECT COUNT(*) FROM product_attributes_dict_item pad WHERE pad.productId = product.id) = :attrCount', {
attrCount: resolvedAttributes.length
});
const isExist = await qb.getOne();
if (isExist) throw new Error(`相同产品属性的产品已存在,sku 为${isExist?.sku}`);
if (isExist) throw new Error('相同产品属性的产品已存在');
}
// 创建新产品实例(绑定属性与基础字段)
@ -1785,31 +1778,36 @@ 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 }, quantity: number = 1, site: Site): Promise<{ product: Product,parentProduct?: Product, quantity: number }[]> {
if (!siteProduct.sku) {
throw new Error('siteSku 不能为空')
}
const product = await this.getProductBySiteSku(siteProduct.sku, site)
const product = await this.getProductBySiteSku(siteProduct.sku, site)
if (!product) return
if(!product?.components?.length){
return [{
product,
quantity:1
quantity
}]
}
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']
where: { id: comp.productId },
}),
parentProduct: product, // 这里得记录一下他的爸爸用来记录
quantity: comp.quantity,
quantity: comp.quantity * quantity,
}
}))
}
@ -2076,7 +2074,7 @@ export class ProductService {
errors.push({ identifier: '' + rec.sku, error: `产品${rec?.sku}导入失败:${e?.message || String(e)}` });
}
}
this.logger.debug(`导入 ${records.length} 条记录,成功创建 ${created} 条,更新 ${updated} 条,失败 ${errors.length} 条,错误详情:${JSON.stringify(errors)}`);
return { total: records.length, processed: records.length - errors.length, created, updated, errors };
}
@ -2134,6 +2132,11 @@ export class ProductService {
component.sku = product.sku;
component.quantity = 1;
product.components = [component];
} else {
// 混装商品返回持久化的 SKU 组成
product.components = await this.productStockComponentModel.find({
where: { productId: product.id },
});
}
return product;