feat: 添加少许特性 #50

Merged
zhuotianyuan merged 2 commits from zksu/API:main into main 2026-01-14 11:45:15 +00:00
6 changed files with 1106 additions and 284 deletions
Showing only changes of commit 56deb447b3 - Show all commits

View File

@ -21,13 +21,16 @@ import {
CreateReviewDTO,
CreateVariationDTO,
UpdateReviewDTO,
OrderPaymentStatus,
} from '../dto/site-api.dto';
import { UnifiedPaginationDTO, UnifiedSearchParamsDTO, } from '../dto/api.dto';
import {
ShopyyAllProductQuery,
ShopyyCustomer,
ShopyyOrder,
ShopyyOrderCreateParams,
ShopyyOrderQuery,
ShopyyOrderUpdateParams,
ShopyyProduct,
ShopyyProductQuery,
ShopyyVariant,
@ -38,15 +41,15 @@ import {
} from '../enums/base.enum';
import { BatchOperationDTO, BatchOperationResultDTO } from '../dto/batch.dto';
export class ShopyyAdapter implements ISiteAdapter {
shopyyFinancialStatusMap= {
shopyyFinancialStatusMap = {
'200': '待支付',
'210': "支付中",
'220':"部分支付",
'230':"已支付",
'240':"支付失败",
'250':"部分退款",
'260':"已退款",
'290':"已取消",
'220': "部分支付",
'230': "已支付",
'240': "支付失败",
'250': "部分退款",
'260': "已退款",
'290': "已取消",
}
constructor(private site: any, private shopyyService: ShopyyService) {
this.mapPlatformToUnifiedCustomer = this.mapPlatformToUnifiedCustomer.bind(this);
@ -123,8 +126,8 @@ export class ShopyyAdapter implements ISiteAdapter {
return data
}
async getCustomer(where: {id?: string | number,email?: string,phone?: string}): Promise<UnifiedCustomerDTO> {
if(!where.id && !where.email && !where.phone){
async getCustomer(where: { id?: string | number, email?: string, phone?: string }): Promise<UnifiedCustomerDTO> {
if (!where.id && !where.email && !where.phone) {
throw new Error('必须传入 id 或 email 或 phone')
}
const customer = await this.shopyyService.getCustomer(this.site, where.id);
@ -153,12 +156,12 @@ export class ShopyyAdapter implements ISiteAdapter {
return this.mapPlatformToUnifiedCustomer(createdCustomer);
}
async updateCustomer(where: {id: string | number}, data: Partial<UnifiedCustomerDTO>): Promise<UnifiedCustomerDTO> {
async updateCustomer(where: { id: string | number }, data: Partial<UnifiedCustomerDTO>): Promise<UnifiedCustomerDTO> {
const updatedCustomer = await this.shopyyService.updateCustomer(this.site, where.id, data);
return this.mapPlatformToUnifiedCustomer(updatedCustomer);
}
async deleteCustomer(where: {id: string | number}): Promise<boolean> {
async deleteCustomer(where: { id: string | number }): Promise<boolean> {
return await this.shopyyService.deleteCustomer(this.site, where.id);
}
@ -212,12 +215,12 @@ export class ShopyyAdapter implements ISiteAdapter {
return this.mapPlatformToUnifiedMedia(createdMedia);
}
async updateMedia(where: {id: string | number}, data: any): Promise<UnifiedMediaDTO> {
async updateMedia(where: { id: string | number }, data: any): Promise<UnifiedMediaDTO> {
const updatedMedia = await this.shopyyService.updateMedia(this.site, where.id, data);
return this.mapPlatformToUnifiedMedia(updatedMedia);
}
async deleteMedia(where: {id: string | number}): Promise<boolean> {
async deleteMedia(where: { id: string | number }): Promise<boolean> {
return await this.shopyyService.deleteMedia(this.site, where.id);
}
@ -228,10 +231,10 @@ export class ShopyyAdapter implements ISiteAdapter {
// ========== 订单映射方法 ==========
mapPlatformToUnifiedOrder(item: ShopyyOrder): UnifiedOrderDTO {
// console.log(item)
if(!item) throw new Error('订单数据不能为空')
if (!item) throw new Error('订单数据不能为空')
// 提取账单和送货地址 如果不存在则为空对象
const billing = (item).bill_address || {};
const shipping = (item as any).shipping_address || {};
const billing = item.billing_address || {};
const shipping = item.shipping_address || {};
// 构建账单地址对象
const billingObj: UnifiedAddressDTO = {
@ -309,14 +312,14 @@ export class ShopyyAdapter implements ISiteAdapter {
};
const lineItems: UnifiedOrderLineItemDTO[] = (item.products || []).map(
(p: any) => ({
id: p.id,
name: p.product_title || p.name,
product_id: p.product_id,
quantity: p.quantity,
total: String(p.price ?? ''),
sku: p.sku_code || '',
price: String(p.price ?? ''),
(product) => ({
id: product.id,
name: product.product_title || product.name,
product_id: product.product_id,
quantity: product.quantity,
total: String(product.price ?? ''),
sku: product.sku || product.sku_code || '',
price: String(product.price ?? ''),
})
);
// 货币符号
@ -334,12 +337,12 @@ export class ShopyyAdapter implements ISiteAdapter {
'SGD': 'S$'
// 可以根据需要添加更多货币代码和符号
};
// 映射订单状态,如果不存在则默认 pending
const status = this.shopyyOrderStatusMap[item.status ?? item.order_status] || OrderStatus.PENDING;
const finalcial_status = this.shopyyFinancialStatusMap[item.financial_status]
const finalcial_status = this.shopyyFinancialStatusMap[item.financial_status]
// 发货状态
const fulfillment_status = this.shopyyFulfillmentStatusMap[item.fulfillment_status];
const fulfillment_status = this.fulfillmentStatusMap[item.fulfillment_status];
return {
id: item.id || item.order_id,
number: item.order_number || item.order_sn,
@ -388,7 +391,7 @@ export class ShopyyAdapter implements ISiteAdapter {
tracking_number: f.tracking_number || '',
shipping_provider: f.tracking_company || '',
shipping_method: f.tracking_company || '',
date_created: typeof f.created_at === 'number'
? new Date(f.created_at * 1000).toISOString()
: f.created_at || '',
@ -402,11 +405,11 @@ export class ShopyyAdapter implements ISiteAdapter {
return data
}
mapCreateOrderParams(data: Partial<UnifiedOrderDTO>): any {
mapCreateOrderParams(data: Partial<UnifiedOrderDTO>): ShopyyOrderCreateParams {
return data
}
mapUpdateOrderParams(data: Partial<UnifiedOrderDTO>): any {
mapUpdateOrderParams(data: Partial<UnifiedOrderDTO>): ShopyyOrderUpdateParams {
// 构建 ShopYY 订单更新参数(仅包含传入的字段)
const params: any = {};
@ -536,9 +539,17 @@ export class ShopyyAdapter implements ISiteAdapter {
return params;
}
async getOrder(where: {id: string | number}): Promise<UnifiedOrderDTO> {
const data = await this.shopyyService.getOrder(this.site.id, String(where.id));
return this.mapPlatformToUnifiedOrder(data);
async getOrder(where: { id: string | number }): Promise<UnifiedOrderDTO> {
const data = await this.getOrders({
where: {
id: where.id,
},
page: 1,
per_page: 1,
})
return data.items[0] || null
// const data = await this.shopyyService.getOrder(this.site.id, String(where.id));
// return this.mapPlatformToUnifiedOrder(data);
}
async getOrders(
@ -565,7 +576,7 @@ export class ShopyyAdapter implements ISiteAdapter {
return data.map(this.mapPlatformToUnifiedOrder.bind(this));
}
async countOrders(where: Record<string,any>): Promise<number> {
async countOrders(where: Record<string, any>): Promise<number> {
// 使用最小分页只获取总数
const searchParams = {
where,
@ -583,13 +594,13 @@ export class ShopyyAdapter implements ISiteAdapter {
return this.mapPlatformToUnifiedOrder(createdOrder);
}
async updateOrder(where: {id: string | number}, data: Partial<UnifiedOrderDTO>): Promise<boolean> {
async updateOrder(where: { id: string | number }, data: Partial<UnifiedOrderDTO>): Promise<boolean> {
// 使用映射方法转换参数
const requestParams = this.mapUpdateOrderParams(data);
return await this.shopyyService.updateOrder(this.site, String(where.id), requestParams);
}
async deleteOrder(where: {id: string | number}): Promise<boolean> {
async deleteOrder(where: { id: string | number }): Promise<boolean> {
return await this.shopyyService.deleteOrder(this.site, where.id);
}
@ -662,7 +673,7 @@ export class ShopyyAdapter implements ISiteAdapter {
mapOrderSearchParams(params: UnifiedSearchParamsDTO): Partial<ShopyyOrderQuery> {
// 首先使用通用参数转换
const baseParams = this.mapSearchParams(params);
// 订单状态映射
const statusMap = {
'pending': '100', // 100 未完成
@ -670,7 +681,7 @@ export class ShopyyAdapter implements ISiteAdapter {
'completed': "180", // 180 已完成(确认收货)
'cancelled': '190', // 190 取消
};
// 如果有状态参数,进行特殊映射
if (baseParams.status) {
const unifiedStatus = baseParams.status
@ -678,13 +689,13 @@ export class ShopyyAdapter implements ISiteAdapter {
baseParams.status = statusMap[unifiedStatus];
}
}
// 处理ID参数
if (baseParams.id) {
baseParams.ids = baseParams.id;
delete baseParams.id;
}
return baseParams;
}
@ -699,7 +710,7 @@ export class ShopyyAdapter implements ISiteAdapter {
name: item.name || item.title,
type: String(item.product_type ?? ''),
status: mapProductStatus(item.status),
sku: item.variant?.sku || '',
sku: item.variant?.sku || item.variant?.sku_code || '',
regular_price: String(item.variant?.price ?? ''),
sale_price: String(item.special_price ?? ''),
price: String(item.price ?? ''),
@ -728,7 +739,7 @@ export class ShopyyAdapter implements ISiteAdapter {
name: c.title || '',
})),
variations: item.variants?.map(this.mapPlatformToUnifiedVariation.bind(this)) || [],
permalink: `${this.site.websiteUrl}/products/${item.handle}`,
permalink: `${this.site.websiteUrl}/products/${item.handle}`,
date_created:
typeof item.created_at === 'number'
? new Date(item.created_at * 1000).toISOString()
@ -864,8 +875,8 @@ export class ShopyyAdapter implements ISiteAdapter {
return params;
}
async getProduct(where: {id?: string | number, sku?: string}): Promise<UnifiedProductDTO> {
if(!where.id && !where.sku){
async getProduct(where: { id?: string | number, sku?: string }): Promise<UnifiedProductDTO> {
if (!where.id && !where.sku) {
throw new Error('必须传入 id 或 sku')
}
if (where.id) {
@ -901,11 +912,11 @@ export class ShopyyAdapter implements ISiteAdapter {
per_page,
};
}
mapAllProductParams(params: UnifiedSearchParamsDTO): Partial<ShopyyAllProductQuery>{
const mapped = {
mapAllProductParams(params: UnifiedSearchParamsDTO): Partial<ShopyyAllProductQuery> {
const mapped = {
...params.where,
} as any
if(params.per_page){mapped.limit = params.per_page}
} as any
if (params.per_page) { mapped.limit = params.per_page }
return mapped
}
@ -919,7 +930,7 @@ export class ShopyyAdapter implements ISiteAdapter {
null,
requestParams
);
if(response.code !==0){
if (response.code !== 0) {
throw new Error(response.msg || '获取产品列表失败')
}
const { data = [] } = response;
@ -934,7 +945,7 @@ export class ShopyyAdapter implements ISiteAdapter {
return this.mapPlatformToUnifiedProduct(res);
}
async updateProduct(where: {id?: string | number, sku?: string}, data: Partial<UnifiedProductDTO>): Promise<boolean> {
async updateProduct(where: { id?: string | number, sku?: string }, data: Partial<UnifiedProductDTO>): Promise<boolean> {
let productId: string;
if (where.id) {
productId = String(where.id);
@ -951,7 +962,7 @@ export class ShopyyAdapter implements ISiteAdapter {
return true;
}
async deleteProduct(where: {id?: string | number, sku?: string}): Promise<boolean> {
async deleteProduct(where: { id?: string | number, sku?: string }): Promise<boolean> {
let productId: string | number;
if (where.id) {
productId = where.id;
@ -966,11 +977,11 @@ export class ShopyyAdapter implements ISiteAdapter {
await this.shopyyService.batchProcessProducts(this.site, { delete: [productId] });
return true;
}
// 通过sku获取产品详情的私有方法
private async getProductBySku(sku: string): Promise<UnifiedProductDTO> {
// 使用Shopyy API的搜索功能通过sku查询产品
const response = await this.getAllProducts({ where: {sku} });
const response = await this.getAllProducts({ where: { sku } });
console.log('getProductBySku', response)
const product = response?.[0]
if (!product) {
@ -1034,12 +1045,12 @@ export class ShopyyAdapter implements ISiteAdapter {
return this.mapPlatformToUnifiedReview(createdReview);
}
async updateReview(where: {id: string | number}, data: any): Promise<UnifiedReviewDTO> {
async updateReview(where: { id: string | number }, data: any): Promise<UnifiedReviewDTO> {
const updatedReview = await this.shopyyService.updateReview(this.site, where.id, data);
return this.mapPlatformToUnifiedReview(updatedReview);
}
async deleteReview(where: {id: string | number}): Promise<boolean> {
async deleteReview(where: { id: string | number }): Promise<boolean> {
return await this.shopyyService.deleteReview(this.site, where.id);
}
@ -1101,10 +1112,11 @@ export class ShopyyAdapter implements ISiteAdapter {
// ========== 产品变体映射方法 ==========
mapPlatformToUnifiedVariation(variant: ShopyyVariant): UnifiedProductVariationDTO {
// 映射变体
console.log('ivarianttem', variant)
return {
id: variant.id,
name: variant.sku || '',
sku: variant.sku || '',
name: variant.title || '',
sku: variant.sku || variant.sku_code || '',
regular_price: String(variant.price ?? ''),
sale_price: String(variant.special_price ?? ''),
price: String(variant.price ?? ''),
@ -1195,7 +1207,7 @@ export class ShopyyAdapter implements ISiteAdapter {
return data
}
async getWebhook(where: {id: string | number}): Promise<UnifiedWebhookDTO> {
async getWebhook(where: { id: string | number }): Promise<UnifiedWebhookDTO> {
const webhook = await this.shopyyService.getWebhook(this.site, where.id);
return this.mapPlatformToUnifiedWebhook(webhook);
}
@ -1221,12 +1233,12 @@ export class ShopyyAdapter implements ISiteAdapter {
return this.mapPlatformToUnifiedWebhook(createdWebhook);
}
async updateWebhook(where: {id: string | number}, data: UpdateWebhookDTO): Promise<UnifiedWebhookDTO> {
async updateWebhook(where: { id: string | number }, data: UpdateWebhookDTO): Promise<UnifiedWebhookDTO> {
const updatedWebhook = await this.shopyyService.updateWebhook(this.site, where.id, data);
return this.mapPlatformToUnifiedWebhook(updatedWebhook);
}
async deleteWebhook(where: {id: string | number}): Promise<boolean> {
async deleteWebhook(where: { id: string | number }): Promise<boolean> {
return await this.shopyyService.deleteWebhook(this.site, where.id);
}
@ -1269,14 +1281,14 @@ export class ShopyyAdapter implements ISiteAdapter {
// 处理分页参数
const page = Number(params.page || 1);
const limit = Number(params.per_page ?? 20);
// 处理 where 条件
const query: any = {
...(params.where || {}),
page,
limit,
}
if(params.orderBy){
if (params.orderBy) {
const [field, dir] = Object.entries(params.orderBy)[0];
query.order_by = dir === 'desc' ? 'desc' : 'asc';
query.order_field = field
@ -1286,24 +1298,24 @@ export class ShopyyAdapter implements ISiteAdapter {
// 映射产品状态: publish -> 1, draft -> 0
mapStatus = (status: string) => {
return status === 'publish' ? 1 : 0;
return status === 'publish' ? 1 : 0;
};
// 映射库存状态: instock -> 1, outofstock -> 0
mapStockStatus = (stockStatus: string) => {
mapStockStatus = (stockStatus: string) => {
return stockStatus === 'instock' ? 1 : 0;
};
shopyyOrderStatusMap = {//订单状态 100 未完成110 待处理180 已完成(确认收货); 190 取消;
[100]: OrderStatus.PENDING, // 100 未完成 转为 pending
[110]: OrderStatus.PROCESSING, // 110 待处理 转为 processing
// 已发货
// 已发货
[180]: OrderStatus.COMPLETED, // 180 已完成(确认收货) 转为 completed
[190]: OrderStatus.CANCEL // 190 取消 转为 cancelled
}
shopyyFulfillmentStatusMap = {
// 物流状态 300 未发货310 部分发货320 已发货330(确认收货)
fulfillmentStatusMap = {
// 未发货
'300': OrderFulfillmentStatus.PENDING,
// 部分发货
@ -1314,4 +1326,23 @@ export class ShopyyAdapter implements ISiteAdapter {
'330': OrderFulfillmentStatus.CANCELLED,
// 确认发货
}
// 支付状态 200 待支付210 支付中220 部分支付230 已支付240 支付失败250 部分退款260 已退款 290 已取消;
financialStatusMap = {
// 待支付
'200': OrderPaymentStatus.PENDING,
// 支付中
'210': OrderPaymentStatus.PAYING,
// 部分支付
'220': OrderPaymentStatus.PARTIALLY_PAID,
// 已支付
'230': OrderPaymentStatus.PAID,
// 支付失败
'240': OrderPaymentStatus.FAILED,
// 部分退款
'250': OrderPaymentStatus.PARTIALLY_REFUNDED,
// 已退款
'260': OrderPaymentStatus.REFUNDED,
// 已取消
'290': OrderPaymentStatus.CANCELLED,
}
}

View File

@ -117,7 +117,7 @@ export class ProductController {
const file = files?.[0];
if (!file) return errorResponse('未接收到上传文件');
const result = await this.productService.importProductsCSV(file);
const result = await this.productService.importProductsFromTable(file);
return successResponse(result);
} catch (error) {
return errorResponse(error?.message || error);

File diff suppressed because it is too large Load Diff

View File

@ -18,6 +18,24 @@ export enum OrderFulfillmentStatus {
// 确认发货
CONFIRMED,
}
export enum OrderPaymentStatus {
// 待支付
PENDING,
// 支付中
PAYING,
// 部分支付
PARTIALLY_PAID,
// 已支付
PAID,
// 支付失败
FAILED,
// 部分退款
PARTIALLY_REFUNDED,
// 已退款
REFUNDED,
// 已取消
CANCELLED,
}
//
export class UnifiedProductWhere {
sku?: string;

View File

@ -1,6 +1,6 @@
import { Inject, Provide } from '@midwayjs/core';
import { parse } from 'csv-parse';
import * as fs from 'fs';
import * as xlsx from 'xlsx';
import { In, Like, Not, Repository } from 'typeorm';
import { Product } from '../entity/product.entity';
import { PaginationParams } from '../interface';
@ -238,18 +238,18 @@ export class ProductService {
// 处理分页参数(支持新旧两种格式)
const page = query.page || 1;
const pageSize = query.per_page || 10;
// 处理搜索参数
const name = query.where?.name || query.search || '';
// 处理品牌过滤
const brandId = query.where?.brandId;
const brandIds = query.where?.brandIds;
// 处理分类过滤
const categoryId = query.where?.categoryId;
const categoryIds = query.where?.categoryIds;
// 处理排序参数
const orderBy = query.orderBy;
@ -270,17 +270,17 @@ export class ProductService {
if (query.where?.id) {
qb.andWhere('product.id = :id', { id: query.where.id });
}
// 处理产品ID列表过滤
if (query.where?.ids && query.where.ids.length > 0) {
qb.andWhere('product.id IN (:...ids)', { ids: query.where.ids });
}
// 处理where对象中的id过滤
if (query.where?.id) {
qb.andWhere('product.id = :whereId', { whereId: query.where.id });
}
// 处理where对象中的ids过滤
if (query.where?.ids && query.where.ids.length > 0) {
qb.andWhere('product.id IN (:...whereIds)', { whereIds: query.where.ids });
@ -290,17 +290,17 @@ export class ProductService {
if (query.where?.sku) {
qb.andWhere('product.sku = :sku', { sku: query.where.sku });
}
// 处理SKU列表过滤
if (query.where?.skus && query.where.skus.length > 0) {
qb.andWhere('product.sku IN (:...skus)', { skus: query.where.skus });
}
// 处理where对象中的sku过滤
if (query.where?.sku) {
qb.andWhere('product.sku = :whereSku', { whereSku: query.where.sku });
}
// 处理where对象中的skus过滤
if (query.where?.skus && query.where.skus.length > 0) {
qb.andWhere('product.sku IN (:...whereSkus)', { whereSkus: query.where.skus });
@ -315,7 +315,7 @@ export class ProductService {
if (query.where?.type) {
qb.andWhere('product.type = :type', { type: query.where.type });
}
// 处理where对象中的type过滤
if (query.where?.type) {
qb.andWhere('product.type = :whereType', { whereType: query.where.type });
@ -325,16 +325,16 @@ export class ProductService {
if (query.where?.minPrice !== undefined) {
qb.andWhere('product.price >= :minPrice', { minPrice: query.where.minPrice });
}
if (query.where?.maxPrice !== undefined) {
qb.andWhere('product.price <= :maxPrice', { maxPrice: query.where.maxPrice });
}
// 处理where对象中的价格范围过滤
if (query.where?.minPrice !== undefined) {
qb.andWhere('product.price >= :whereMinPrice', { whereMinPrice: query.where.minPrice });
}
if (query.where?.maxPrice !== undefined) {
qb.andWhere('product.price <= :whereMaxPrice', { whereMaxPrice: query.where.maxPrice });
}
@ -343,16 +343,16 @@ export class ProductService {
if (query.where?.minPromotionPrice !== undefined) {
qb.andWhere('product.promotionPrice >= :minPromotionPrice', { minPromotionPrice: query.where.minPromotionPrice });
}
if (query.where?.maxPromotionPrice !== undefined) {
qb.andWhere('product.promotionPrice <= :maxPromotionPrice', { maxPromotionPrice: query.where.maxPromotionPrice });
}
// 处理where对象中的促销价格范围过滤
if (query.where?.minPromotionPrice !== undefined) {
qb.andWhere('product.promotionPrice >= :whereMinPromotionPrice', { whereMinPromotionPrice: query.where.minPromotionPrice });
}
if (query.where?.maxPromotionPrice !== undefined) {
qb.andWhere('product.promotionPrice <= :whereMaxPromotionPrice', { whereMaxPromotionPrice: query.where.maxPromotionPrice });
}
@ -361,16 +361,16 @@ export class ProductService {
if (query.where?.createdAtStart) {
qb.andWhere('product.createdAt >= :createdAtStart', { createdAtStart: new Date(query.where.createdAtStart) });
}
if (query.where?.createdAtEnd) {
qb.andWhere('product.createdAt <= :createdAtEnd', { createdAtEnd: new Date(query.where.createdAtEnd) });
}
// 处理where对象中的创建时间范围过滤
if (query.where?.createdAtStart) {
qb.andWhere('product.createdAt >= :whereCreatedAtStart', { whereCreatedAtStart: new Date(query.where.createdAtStart) });
}
if (query.where?.createdAtEnd) {
qb.andWhere('product.createdAt <= :whereCreatedAtEnd', { whereCreatedAtEnd: new Date(query.where.createdAtEnd) });
}
@ -379,16 +379,16 @@ export class ProductService {
if (query.where?.updatedAtStart) {
qb.andWhere('product.updatedAt >= :updatedAtStart', { updatedAtStart: new Date(query.where.updatedAtStart) });
}
if (query.where?.updatedAtEnd) {
qb.andWhere('product.updatedAt <= :updatedAtEnd', { updatedAtEnd: new Date(query.where.updatedAtEnd) });
}
// 处理where对象中的更新时间范围过滤
if (query.where?.updatedAtStart) {
qb.andWhere('product.updatedAt >= :whereUpdatedAtStart', { whereUpdatedAtStart: new Date(query.where.updatedAtStart) });
}
if (query.where?.updatedAtEnd) {
qb.andWhere('product.updatedAt <= :whereUpdatedAtEnd', { whereUpdatedAtEnd: new Date(query.where.updatedAtEnd) });
}
@ -407,7 +407,7 @@ export class ProductService {
return 'product.id IN ' + subQuery;
});
}
// 处理品牌ID列表过滤
if (brandIds && brandIds.length > 0) {
qb.andWhere(qb => {
@ -427,17 +427,17 @@ export class ProductService {
if (categoryId) {
qb.andWhere('product.categoryId = :categoryId', { categoryId });
}
// 处理分类ID列表过滤
if (categoryIds && categoryIds.length > 0) {
qb.andWhere('product.categoryId IN (:...categoryIds)', { categoryIds });
}
// 处理where对象中的分类ID过滤
if (query.where?.categoryId) {
qb.andWhere('product.categoryId = :whereCategoryId', { whereCategoryId: query.where.categoryId });
}
// 处理where对象中的分类ID列表过滤
if (query.where?.categoryIds && query.where.categoryIds.length > 0) {
qb.andWhere('product.categoryId IN (:...whereCategoryIds)', { whereCategoryIds: query.where.categoryIds });
@ -555,7 +555,7 @@ export class ProductService {
// 如果提供了 categoryId,设置分类
if (categoryId) {
categoryItem = await this.categoryModel.findOne({
categoryItem = await this.categoryModel.findOne({
where: { id: categoryId },
relations: ['attributes', 'attributes.attributeDict']
});
@ -566,12 +566,12 @@ export class ProductService {
// 如果属性是分类,特殊处理
if (attr.dictName === 'category') {
if (attr.id) {
categoryItem = await this.categoryModel.findOne({
categoryItem = await this.categoryModel.findOne({
where: { id: attr.id },
relations: ['attributes', 'attributes.attributeDict']
});
} else if (attr.name) {
categoryItem = await this.categoryModel.findOne({
categoryItem = await this.categoryModel.findOne({
where: { name: attr.name },
relations: ['attributes', 'attributes.attributeDict']
});
@ -637,7 +637,7 @@ export class ProductService {
if (sku) {
product.sku = sku;
} else {
product.sku = await this.templateService.render('product.sku', {product});
product.sku = await this.templateService.render('product.sku', { product });
}
const savedProduct = await this.productModel.save(product);
@ -1440,7 +1440,7 @@ export class ProductService {
// 解析属性字段(分号分隔多值)
const parseList = (v: string) => (v ? String(v).split(';').map(s => s.trim()).filter(Boolean) : []);
// 将属性解析为 DTO 输入
const attributes: any[] = [];
@ -1461,16 +1461,16 @@ export class ProductService {
return {
sku,
name: val(rec.name),
nameCn: val(rec.nameCn),
nameCn: val(rec.nameCn),
description: val(rec.description),
price: num(rec.price),
promotionPrice: num(rec.promotionPrice),
type: val(rec.type),
siteSkus: rec.siteSkus
? String(rec.siteSkus)
.split(/[;,]/) // 支持英文分号或英文逗号分隔
.map(s => s.trim())
.filter(Boolean)
.split(/[;,]/) // 支持英文分号或英文逗号分隔
.map(s => s.trim())
.filter(Boolean)
: undefined,
category, // 添加分类字段
@ -1536,10 +1536,10 @@ export class ProductService {
return dto;
}
getAttributesObject(attributes:DictItem[]){
if(!attributes) return {}
const obj:any = {}
attributes.forEach(attr=>{
getAttributesObject(attributes: DictItem[]) {
if (!attributes) return {}
const obj: any = {}
attributes.forEach(attr => {
obj[attr.dict.name] = attr
})
return obj
@ -1665,57 +1665,55 @@ export class ProductService {
return rows.join('\n');
}
// 从 CSV 导入产品;存在则更新,不存在则创建
async importProductsCSV(file: any): Promise<BatchOperationResult> {
let buffer: Buffer;
if (Buffer.isBuffer(file)) {
buffer = file;
} else if (file?.data) {
if (typeof file.data === 'string') {
buffer = fs.readFileSync(file.data);
} else {
buffer = file.data;
}
} else {
throw new Error('无效的文件输入');
}
// 解析 CSV(使用 csv-parse/sync 按表头解析)
let records: any[] = [];
async getRecordsFromTable(file: any) {
// 解析文件(使用 xlsx 包自动识别文件类型并解析)
try {
records = await new Promise((resolve, reject) => {
parse(buffer, {
columns: true,
skip_empty_lines: true,
trim: true,
bom: true,
}, (err, data) => {
if (err) {
reject(err);
} else {
resolve(data);
}
});
})
let buffer: Buffer;
// 处理文件输入,获取 buffer
if (Buffer.isBuffer(file)) {
buffer = file;
}
else if (file?.data) {
if (typeof file.data === 'string') {
buffer = fs.readFileSync(file.data);
} else {
buffer = file.data;
}
} else {
throw new Error('无效的文件输入');
}
let records: any[] = []
// xlsx 包会自动根据文件内容识别文件类型(CSV 或 XLSX)
const workbook = xlsx.read(buffer, { type: 'buffer' });
// 获取第一个工作表
const worksheet = workbook.Sheets[workbook.SheetNames[0]];
// 将工作表转换为 JSON 数组
records = xlsx.utils.sheet_to_json(worksheet);
console.log('Parsed records count:', records.length);
if (records.length > 0) {
console.log('First record keys:', Object.keys(records[0]));
}
return records;
} catch (e: any) {
throw new Error(`CSV 解析失败:${e?.message || e}`)
throw new Error(`文件解析失败:${e?.message || e}`);
}
}
// 从 CSV 导入产品;存在则更新,不存在则创建
async importProductsFromTable(file: any): Promise<BatchOperationResult> {
let created = 0;
let updated = 0;
const errors: BatchErrorItem[] = [];
const records = await this.getRecordsFromTable(file);
// 逐条处理记录
for (const rec of records) {
try {
const data = this.transformCsvRecordToData(rec);
if (!data) {
errors.push({ identifier: data.sku, error: '缺少 SKU 的记录已跳过'});
errors.push({ identifier: data.sku, error: '缺少 SKU 的记录已跳过' });
continue;
}
const { sku } = data;
@ -1735,7 +1733,7 @@ export class ProductService {
updated += 1;
}
} catch (e: any) {
errors.push({ identifier: '' + rec.sku, error: `产品${rec?.sku}导入失败:${e?.message || String(e)}`});
errors.push({ identifier: '' + rec.sku, error: `产品${rec?.sku}导入失败:${e?.message || String(e)}` });
}
}
@ -1749,7 +1747,7 @@ export class ProductService {
.createQueryBuilder('stock')
.select('DISTINCT(stock.sku)', 'sku')
.getRawMany();
const skus = stockSkus.map(s => s.sku).filter(Boolean);
let added = 0;
const errors: string[] = [];
@ -1783,7 +1781,7 @@ export class ProductService {
where: { id },
relations: ['category', 'attributes', 'attributes.dict', 'components']
});
if (!product) {
throw new Error(`产品 ID ${id} 不存在`);
}
@ -1867,7 +1865,7 @@ export class ProductService {
await this.bindSiteSkus(localProduct.id, [unifiedProduct.sku]);
return result;
} catch (error) {
throw new Error(`同步产品到站点失败: ${error?.response?.data?.message??error.message}`);
throw new Error(`同步产品到站点失败: ${error?.response?.data?.message ?? error.message}`);
}
}
@ -1893,7 +1891,7 @@ export class ProductService {
siteId,
siteSku: item.siteSku
});
results.synced++;
results.processed++;
} catch (error) {
@ -1915,18 +1913,18 @@ export class ProductService {
* @returns
*/
async syncProductFromSite(siteId: number, siteProductId: string | number, sku: string): Promise<any> {
const adapter = await this.siteApiService.getAdapter(siteId);
const siteProduct = await adapter.getProduct({ id: siteProductId });
const adapter = await this.siteApiService.getAdapter(siteId);
const siteProduct = await adapter.getProduct({ id: siteProductId });
// 从站点获取产品信息
if (!siteProduct) {
throw new Error(`站点产品 ID ${siteProductId} 不存在`);
}
// 将站点产品转换为本地产品格式
const productData = await this.mapUnifiedToLocalProduct(siteProduct);
return await this.upsertProduct({sku}, productData);
return await this.upsertProduct({ sku }, productData);
}
async upsertProduct(where: Partial<Pick<Product,'id'| 'sku'>>, productData: any) {
const existingProduct = await this.productModel.findOne({ where: where});
async upsertProduct(where: Partial<Pick<Product, 'id' | 'sku'>>, productData: any) {
const existingProduct = await this.productModel.findOne({ where: where });
if (existingProduct) {
// 更新现有产品
const updateData: UpdateProductDTO = productData;
@ -1944,7 +1942,7 @@ export class ProductService {
* @param siteProductIds ID数组
* @returns
*/
async batchSyncFromSite(siteId: number, data: Array<{siteProductId:string, sku: string}>): Promise<{ synced: number, errors: string[] }> {
async batchSyncFromSite(siteId: number, data: Array<{ siteProductId: string, sku: string }>): Promise<{ synced: number, errors: string[] }> {
const results = {
synced: 0,
errors: []
@ -2030,13 +2028,13 @@ export class ProductService {
* @param localProduct
* @returns
*/
private async mapLocalToUnifiedProduct(localProduct: Product,siteSku?: string): Promise<Partial<UnifiedProductDTO>> {
const tags = localProduct.attributes?.map(a => ({name: a.name})) || [];
private async mapLocalToUnifiedProduct(localProduct: Product, siteSku?: string): Promise<Partial<UnifiedProductDTO>> {
const tags = localProduct.attributes?.map(a => ({ name: a.name })) || [];
// 将本地产品数据转换为UnifiedProductDTO格式
const unifiedProduct: any = {
id: localProduct.id ? String(localProduct.id) : undefined, // 如果产品已存在使用现有ID
name: localProduct.name,
type: localProduct.type === 'single'? 'simple' : 'bundle', // 默认类型,可以根据实际需要调整
type: localProduct.type === 'single' ? 'simple' : 'bundle', // 默认类型,可以根据实际需要调整
status: 'publish', // 默认状态,可以根据实际需要调整
sku: siteSku || await this.templateService.render('site.product.sku', { product: localProduct, sku: localProduct.sku }),
regular_price: String(localProduct.price || 0),

View File

@ -8,7 +8,7 @@ import * as FormData from 'form-data';
import { SiteService } from './site.service';
import { Site } from '../entity/site.entity';
import { UnifiedReviewDTO } from '../dto/site-api.dto';
import { ShopyyReview } from '../dto/shopyy.dto';
import { ShopyyGetOneOrderResult, ShopyyReview } from '../dto/shopyy.dto';
import { BatchOperationDTO, BatchOperationResultDTO } from '../dto/batch.dto';
import { UnifiedSearchParamsDTO } from '../dto/api.dto';
/**
@ -366,7 +366,7 @@ export class ShopyyService {
* @param orderId ID
* @returns
*/
async getOrder(siteId: string, orderId: string): Promise<any> {
async getOrder(siteId: string, orderId: string): Promise<ShopyyGetOneOrderResult> {
const site = await this.siteService.get(Number(siteId));
// ShopYY API: GET /orders/{id}