Compare commits
1 Commits
96a3e5c76d
...
5bd0eeced5
| Author | SHA1 | Date |
|---|---|---|
|
|
5bd0eeced5 |
|
|
@ -201,7 +201,7 @@ export class ProductController {
|
|||
@Get('/site-sku/:siteSku')
|
||||
async getProductBySiteSku(@Param('siteSku') siteSku: string) {
|
||||
try {
|
||||
const product = await this.productService.findProductBySiteSku(siteSku);
|
||||
const product = await this.productService.getProductBySiteSku(siteSku);
|
||||
return successResponse(product);
|
||||
} catch (error) {
|
||||
return errorResponse(error.message || '获取数据失败');
|
||||
|
|
|
|||
|
|
@ -0,0 +1,90 @@
|
|||
import {
|
||||
Body,
|
||||
Controller,
|
||||
Get,
|
||||
Inject,
|
||||
Post,
|
||||
Query,
|
||||
} from '@midwayjs/core';
|
||||
import { Context } from '@midwayjs/koa';
|
||||
import { ILogger } from '@midwayjs/logger';
|
||||
import { ApiOkResponse } from '@midwayjs/swagger';
|
||||
import { SiteProductService } from '../service/site-product.service';
|
||||
import { errorResponse, successResponse } from '../utils/response.util';
|
||||
|
||||
@Controller('/site-product')
|
||||
export class SiteProductController {
|
||||
@Inject()
|
||||
siteProductService: SiteProductService;
|
||||
|
||||
@Inject()
|
||||
ctx: Context;
|
||||
|
||||
@Inject()
|
||||
logger: ILogger;
|
||||
|
||||
@ApiOkResponse({
|
||||
description: '获取站点商品列表',
|
||||
})
|
||||
@Get('/list')
|
||||
async getSiteProductList(
|
||||
@Query('current') current: number = 1,
|
||||
@Query('pageSize') pageSize: number = 10,
|
||||
@Query('siteId') siteId: number,
|
||||
@Query('name') name: string,
|
||||
@Query('sku') sku: string
|
||||
) {
|
||||
try {
|
||||
const data = await this.siteProductService.getSiteProductList({
|
||||
current,
|
||||
pageSize,
|
||||
siteId,
|
||||
name,
|
||||
sku,
|
||||
});
|
||||
return successResponse(data);
|
||||
} catch (error) {
|
||||
this.logger.error('获取站点商品列表失败', error);
|
||||
return errorResponse(error?.message || error);
|
||||
}
|
||||
}
|
||||
|
||||
@ApiOkResponse({
|
||||
description: '同步站点商品',
|
||||
})
|
||||
@Post('/sync')
|
||||
async syncSiteProducts(@Body('siteId') siteId: number) {
|
||||
try {
|
||||
const result = await this.siteProductService.syncSiteProducts(siteId);
|
||||
return successResponse(result);
|
||||
} catch (error) {
|
||||
this.logger.error('同步站点商品失败', error);
|
||||
return errorResponse(error?.message || error);
|
||||
}
|
||||
}
|
||||
|
||||
@ApiOkResponse({
|
||||
description: '批量修改站点商品价格',
|
||||
})
|
||||
@Post('/batch-update-price')
|
||||
async batchUpdatePrice(
|
||||
@Body('siteId') siteId: number,
|
||||
@Body('productIds') productIds: string[],
|
||||
@Body('price') price: number
|
||||
) {
|
||||
try {
|
||||
const affected = await this.siteProductService.batchUpdatePrice(
|
||||
siteId,
|
||||
productIds,
|
||||
price
|
||||
);
|
||||
return successResponse({
|
||||
affected,
|
||||
message: `成功修改 ${affected} 个商品的价格`,
|
||||
});
|
||||
} catch (error) {
|
||||
this.logger.error('批量修改站点商品价格失败', error);
|
||||
return errorResponse(error?.message || error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -99,7 +99,6 @@ export class Product {
|
|||
@OneToMany(() => ProductStockComponent, (component) => component.product, { cascade: true })
|
||||
components: ProductStockComponent[];
|
||||
|
||||
|
||||
// 站点 SKU 关联
|
||||
@ApiProperty({ description: '站点 SKU关联', type: SiteSku, isArray: true })
|
||||
@OneToMany(() => SiteSku, siteSku => siteSku.product, { cascade: true })
|
||||
|
|
|
|||
|
|
@ -76,6 +76,10 @@ export class SiteProduct {
|
|||
@CreateDateColumn()
|
||||
createdAt: Date;
|
||||
|
||||
@ApiProperty({ description: '价格' })
|
||||
@Column({ type: 'decimal', precision: 10, scale: 2, nullable: true })
|
||||
price: number;
|
||||
|
||||
@ApiProperty({
|
||||
example: '2022-12-12 11:11:11',
|
||||
description: '更新时间',
|
||||
|
|
|
|||
|
|
@ -40,6 +40,7 @@ import * as os from 'os';
|
|||
import { UnifiedOrderDTO } from '../dto/site-api.dto';
|
||||
import { CustomerService } from './customer.service';
|
||||
import { ProductService } from './product.service';
|
||||
import { Site } from '../entity/site.entity';
|
||||
@Provide()
|
||||
export class OrderService {
|
||||
|
||||
|
|
@ -627,7 +628,8 @@ export class OrderService {
|
|||
// 保存订单项
|
||||
await this.saveOrderItem(entity);
|
||||
// 为每个订单项创建对应的销售项(OrderSale)
|
||||
await this.saveOrderSale(entity);
|
||||
const site = await this.siteService.get(siteId);
|
||||
await this.saveOrderSale(entity,site);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -717,7 +719,7 @@ export class OrderService {
|
|||
*/
|
||||
// TODO 这里存的是库存商品实际
|
||||
// 所以叫做 orderInventoryItems 可能更合适
|
||||
async saveOrderSale(orderItem: OrderItem) {
|
||||
async saveOrderSale(orderItem: OrderItem,site:Site) {
|
||||
const currentOrderSale = await this.orderSaleModel.find({
|
||||
where: {
|
||||
siteId: orderItem.siteId,
|
||||
|
|
@ -730,7 +732,7 @@ export class OrderService {
|
|||
if (!orderItem.sku) return;
|
||||
|
||||
// 从数据库查询产品,关联查询组件
|
||||
const productDetail = await this.productService.getComponentDetailFromSiteSku({ sku: orderItem.sku, name: orderItem.name });
|
||||
const productDetail = await this.productService.getComponentDetailFromSiteSku({ sku: orderItem.sku, name: orderItem.name },site);
|
||||
|
||||
if (!productDetail || !productDetail.quantity) return;
|
||||
const { product, quantity } = productDetail
|
||||
|
|
|
|||
|
|
@ -34,6 +34,7 @@ import { paginate } from '../utils/paginate.util';
|
|||
import { SiteApiService } from './site-api.service';
|
||||
import { StockService } from './stock.service';
|
||||
import { TemplateService } from './template.service';
|
||||
import { Site } from '../entity/site.entity';
|
||||
|
||||
@Provide()
|
||||
export class ProductService {
|
||||
|
|
@ -1784,50 +1785,16 @@ export class ProductService {
|
|||
// 这里判断 second 是否是数字
|
||||
return sku.includes('-MX-') || sku.includes('-Mixed-') || /^\d+$/.test(second) && /^\d+$/.test(last)
|
||||
}
|
||||
async getComponentDetailFromSiteSku(siteProduct: { sku: string, name: string }) {
|
||||
async getComponentDetailFromSiteSku(siteProduct: { sku: string, name: string }, site: Site) {
|
||||
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'],
|
||||
});
|
||||
}
|
||||
let product = await this.getProductBySiteSku(siteProduct.sku, site)
|
||||
|
||||
if (!product) {
|
||||
throw new Error(`产品 ${siteProduct.sku} 不存在`);
|
||||
}
|
||||
return {
|
||||
product,
|
||||
quantity,
|
||||
quantity: 1,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -2162,18 +2129,26 @@ export class ProductService {
|
|||
}
|
||||
|
||||
// 根据站点SKU查询产品
|
||||
async findProductBySiteSku(siteSku: string): Promise<Product> {
|
||||
const product = await this.productModel.findOne({
|
||||
where: { siteSkus: Like(`%${siteSku}%`) },
|
||||
relations: ['category', 'attributes', 'attributes.dict', 'components']
|
||||
});
|
||||
async getProductBySiteSku(siteSku: string, site?: Site): Promise<Product> {
|
||||
// 使用查询构建器来正确查询关联表
|
||||
const queryBuilder = this.productModel
|
||||
.createQueryBuilder('product')
|
||||
.leftJoinAndSelect('product.category', 'category')
|
||||
.leftJoinAndSelect('product.attributes', 'attributes')
|
||||
.leftJoinAndSelect('attributes.dict', 'dict')
|
||||
.leftJoinAndSelect('product.components', 'components')
|
||||
.leftJoinAndSelect('product.siteSkus', 'siteSku')
|
||||
.where('siteSku.sku LIKE :siteSku', { siteSku: `%${siteSku}%` })
|
||||
.orWhere('product.sku = :siteSku', { siteSku });
|
||||
|
||||
if (!product) {
|
||||
throw new Error(`站点SKU ${siteSku} 不存在`);
|
||||
if (site) {
|
||||
queryBuilder.orWhere('product.sku = :processedSku', {
|
||||
processedSku: siteSku.replace(new RegExp(site.skuPrefix + '-'), '')
|
||||
});
|
||||
}
|
||||
|
||||
// 获取完整的产品信息,包含所有关联数据
|
||||
return this.getProductById(product.id);
|
||||
const product = await queryBuilder.getOne();
|
||||
return product
|
||||
}
|
||||
|
||||
// 获取产品的站点SKU列表
|
||||
|
|
|
|||
|
|
@ -60,7 +60,7 @@ export class SiteApiService {
|
|||
|
||||
try {
|
||||
// 使用站点SKU查询对应的ERP产品
|
||||
const erpProduct = await this.productService.findProductBySiteSku(siteProduct.sku);
|
||||
const erpProduct = await this.productService.getProductBySiteSku(siteProduct.sku);
|
||||
|
||||
// 将ERP产品信息合并到站点商品中
|
||||
return {
|
||||
|
|
|
|||
|
|
@ -0,0 +1,102 @@
|
|||
import { Inject, Provide } from '@midwayjs/core';
|
||||
import { ILogger } from '@midwayjs/logger';
|
||||
import { InjectEntityModel } from '@midwayjs/typeorm';
|
||||
import { Repository } from 'typeorm';
|
||||
import { SiteProduct } from '../entity/site-product.entity';
|
||||
|
||||
@Provide()
|
||||
export class SiteProductService {
|
||||
@InjectEntityModel(SiteProduct)
|
||||
siteProductModel: Repository<SiteProduct>;
|
||||
|
||||
@Inject()
|
||||
logger: ILogger;
|
||||
|
||||
async getSiteProductList(params: {
|
||||
current?: number;
|
||||
pageSize?: number;
|
||||
siteId?: number;
|
||||
name?: string;
|
||||
sku?: string;
|
||||
}) {
|
||||
const {
|
||||
current = 1,
|
||||
pageSize = 10,
|
||||
siteId,
|
||||
name,
|
||||
sku,
|
||||
} = params;
|
||||
|
||||
const queryBuilder = this.siteProductModel.createQueryBuilder('siteProduct');
|
||||
|
||||
// 根据 siteId 筛选
|
||||
if (siteId) {
|
||||
queryBuilder.where('siteProduct.siteId = :siteId', { siteId });
|
||||
}
|
||||
|
||||
// 根据 name 或 sku 模糊搜索
|
||||
if (name || sku) {
|
||||
queryBuilder.andWhere(
|
||||
'(siteProduct.name LIKE :keyword OR siteProduct.sku LIKE :keyword)',
|
||||
{ keyword: `%${name || sku}%` }
|
||||
);
|
||||
}
|
||||
|
||||
// 计算总数
|
||||
const total = await queryBuilder.getCount();
|
||||
|
||||
// 分页查询
|
||||
const items = await queryBuilder
|
||||
.skip((current - 1) * pageSize)
|
||||
.take(pageSize)
|
||||
.orderBy('siteProduct.updatedAt', 'DESC')
|
||||
.getMany();
|
||||
|
||||
return {
|
||||
total,
|
||||
items,
|
||||
current,
|
||||
pageSize,
|
||||
};
|
||||
}
|
||||
|
||||
async getSiteProductById(id: string) {
|
||||
return await this.siteProductModel.findOne({ where: { id } });
|
||||
}
|
||||
|
||||
async createSiteProduct(data: Partial<SiteProduct>) {
|
||||
const siteProduct = this.siteProductModel.create(data);
|
||||
return await this.siteProductModel.save(siteProduct);
|
||||
}
|
||||
|
||||
async updateSiteProduct(id: string, data: Partial<SiteProduct>) {
|
||||
await this.siteProductModel.update(id, data);
|
||||
return await this.getSiteProductById(id);
|
||||
}
|
||||
|
||||
async deleteSiteProduct(id: string) {
|
||||
await this.siteProductModel.delete(id);
|
||||
return true;
|
||||
}
|
||||
|
||||
async batchUpdatePrice(siteId: number, productIds: string[], price: number) {
|
||||
const result = await this.siteProductModel
|
||||
.createQueryBuilder()
|
||||
.update()
|
||||
.set({ price })
|
||||
.where('siteId = :siteId AND id IN (:...productIds)', { siteId, productIds })
|
||||
.execute();
|
||||
|
||||
return result.affected || 0;
|
||||
}
|
||||
|
||||
async syncSiteProducts(siteId: number) {
|
||||
// 这里实现同步逻辑,暂时返回成功
|
||||
// 实际实现时需要调用对应的站点适配器进行同步
|
||||
this.logger.info(`Syncing products for site ${siteId}`);
|
||||
return {
|
||||
success: true,
|
||||
message: '同步成功',
|
||||
};
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue