From 75056db42c2c87c37af45e1214be1cebb4eb15dd Mon Sep 17 00:00:00 2001 From: tikkhun Date: Thu, 22 Jan 2026 15:05:41 +0800 Subject: [PATCH] =?UTF-8?q?refactor(woocommerce):=20=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E5=8F=82=E6=95=B0=E5=A4=84=E7=90=86=E9=80=BB=E8=BE=91=E5=B9=B6?= =?UTF-8?q?=E6=8F=90=E5=8F=96=E5=B7=A5=E5=85=B7=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 将参数转换逻辑提取到独立的工具模块中 合并重复的include参数处理逻辑 添加对parent_exclude参数的支持 --- src/adapter/woocommerce.adapter.ts | 16 ++-------------- src/dto/woocommerce.dto.ts | 3 ++- src/utils/trans.util.ts | 11 +++++++++++ 3 files changed, 15 insertions(+), 15 deletions(-) create mode 100644 src/utils/trans.util.ts diff --git a/src/adapter/woocommerce.adapter.ts b/src/adapter/woocommerce.adapter.ts index 0246569..f14d7d3 100644 --- a/src/adapter/woocommerce.adapter.ts +++ b/src/adapter/woocommerce.adapter.ts @@ -35,6 +35,7 @@ import { import { Site } from '../entity/site.entity'; import { WPService } from '../service/wp.service'; import { BatchOperationDTO, BatchOperationResultDTO } from '../dto/batch.dto'; +import { toArray, toNumber } from '../utils/trans.util'; export class WooCommerceAdapter implements ISiteAdapter { // 构造函数接收站点配置与服务实例 @@ -332,22 +333,11 @@ export class WooCommerceAdapter implements ISiteAdapter { // } const mapped: any = { ...(params.search ? { search: params.search } : {}), - // ...(orderBy ? { orderBy } : {}), page, per_page, }; - const toArray = (value: any): any[] => { - if (Array.isArray(value)) return value; - if (value === undefined || value === null) return []; - return String(value).split(',').map(v => v.trim()).filter(Boolean); - }; - const toNumber = (value: any): number | undefined => { - if (value === undefined || value === null || value === '') return undefined; - const n = Number(value); - return Number.isFinite(n) ? n : undefined; - }; // 时间过滤参数 if (where.after ?? where.date_created_after ?? where.created_after) mapped.after = String(where.after ?? where.date_created_after ?? where.created_after); @@ -358,9 +348,7 @@ export class WooCommerceAdapter implements ISiteAdapter { // 集合过滤参数 if (where.exclude) mapped.exclude = toArray(where.exclude); - if (where.include) mapped.include = toArray(where.include); - - if (where.ids) mapped.include = toArray(where.ids); + if (where.ids || where.number || where.id || where.include) mapped.include = [...new Set([where.number,where.id,...toArray(where.ids),...toArray(where.include)])].filter(Boolean); if (toNumber(where.offset) !== undefined) mapped.offset = Number(where.offset); if (where.parent ?? where.parentId) mapped.parent = toArray(where.parent ?? where.parentId); if (where.parent_exclude ?? where.parentExclude) mapped.parent_exclude = toArray(where.parent_exclude ?? where.parentExclude); diff --git a/src/dto/woocommerce.dto.ts b/src/dto/woocommerce.dto.ts index 29245b7..c48b85f 100644 --- a/src/dto/woocommerce.dto.ts +++ b/src/dto/woocommerce.dto.ts @@ -559,7 +559,8 @@ export interface WooOrderSearchParams { order: string; orderby: string; parant: string[]; - status: (WooOrderStatusSearchParams)[]; + parent_exclude: string[]; + status: WooOrderStatusSearchParams[]; customer: number; product: number; dp: number; diff --git a/src/utils/trans.util.ts b/src/utils/trans.util.ts new file mode 100644 index 0000000..57063e3 --- /dev/null +++ b/src/utils/trans.util.ts @@ -0,0 +1,11 @@ +export const toArray = (value: any): any[] => { + if (Array.isArray(value)) return value; + if (value === undefined || value === null) return []; + return String(value).split(',').map(v => v.trim()).filter(Boolean); +}; + +export const toNumber = (value: any): number | undefined => { + if (value === undefined || value === null || value === '') return undefined; + const n = Number(value); + return Number.isFinite(n) ? n : undefined; +}; \ No newline at end of file