From b2ee61e47d4753d0d1d85b2f9f733c9bf8a31732 Mon Sep 17 00:00:00 2001 From: tikkhun Date: Sat, 10 Jan 2026 11:52:04 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E7=A7=BB=E9=99=A4=E6=9C=AA?= =?UTF-8?q?=E4=BD=BF=E7=94=A8=E7=9A=84=E5=AF=BC=E5=85=A5=E5=92=8C=E6=B3=A8?= =?UTF-8?q?=E9=87=8A=E6=8E=89=E7=9A=84=E7=94=9F=E5=91=BD=E5=91=A8=E6=9C=9F?= =?UTF-8?q?=E9=92=A9=E5=AD=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/entity/order_sale.entity.ts | 4 +- src/service/order.service.ts | 158 ++++---------------------------- 2 files changed, 19 insertions(+), 143 deletions(-) diff --git a/src/entity/order_sale.entity.ts b/src/entity/order_sale.entity.ts index 96913d6..5ceb758 100644 --- a/src/entity/order_sale.entity.ts +++ b/src/entity/order_sale.entity.ts @@ -1,8 +1,8 @@ import { ApiProperty } from '@midwayjs/swagger'; import { Exclude, Expose } from 'class-transformer'; import { - BeforeInsert, - BeforeUpdate, + // BeforeInsert, + // BeforeUpdate, Column, CreateDateColumn, Entity, diff --git a/src/service/order.service.ts b/src/service/order.service.ts index ecaf680..f0aaca7 100644 --- a/src/service/order.service.ts +++ b/src/service/order.service.ts @@ -39,6 +39,7 @@ import * as path from 'path'; import * as os from 'os'; import { UnifiedOrderDTO } from '../dto/site-api.dto'; import { CustomerService } from './customer.service'; +import { ProductService } from './product.service'; @Provide() export class OrderService { @@ -140,7 +141,7 @@ export class OrderService { updated: 0, errors: [] }; - console.log('开始进入循环同步订单', result.length, '个订单') + // 遍历每个订单进行同步 for (const order of result) { try { @@ -164,7 +165,6 @@ export class OrderService { } else { syncResult.created++; } - // console.log('updated', syncResult.updated, 'created:', syncResult.created) } catch (error) { // 记录错误但不中断整个同步过程 syncResult.errors.push({ @@ -174,8 +174,6 @@ export class OrderService { syncResult.processed++; } } - console.log('同步完成', syncResult.updated, 'created:', syncResult.created) - this.logger.debug('syncOrders result', syncResult) return syncResult; } @@ -311,7 +309,6 @@ export class OrderService { * @param order 订单数据 * @param forceUpdate 是否强制更新 */ - async syncSingleOrder(siteId: number, order: UnifiedOrderDTO, forceUpdate = false) { async syncSingleOrder(siteId: number, order: UnifiedOrderDTO, forceUpdate = false) { // 从订单数据中解构出各个子项 let { @@ -353,14 +350,14 @@ export class OrderService { await this.saveOrderItems({ siteId, orderId, - externalOrderId: String(externalOrderId), + externalOrderId, orderItems: line_items, }); // 保存退款信息 await this.saveOrderRefunds({ siteId, orderId, - externalOrderId , + externalOrderId, refunds, }); // 保存费用信息 @@ -452,7 +449,7 @@ export class OrderService { * @returns 保存后的订单实体 */ // 这里 omit 是因为处理在外头了 其实 saveOrder 应该包括 savelineitems 等 - async saveOrder(siteId: number, order: Omit): Promise { + async saveOrder(siteId: number, order: Omit): Promise { // 将外部订单ID转换为字符串 const externalOrderId = String(order.id) delete order.id @@ -725,12 +722,14 @@ export class OrderService { return { product: await this.productModel.findOne({ where: { sku: comp.sku }, + relations: ['components','attributes'], }), quantity: comp.quantity * orderItem.quantity, } - })) : [{ product, quantity: orderItem.quantity }]; + })) : [{ product, quantity: orderItem.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, @@ -743,11 +742,12 @@ export class OrderService { isPackage: componentDetail.product.type === 'bundle', isYoone: attrsObj?.['brand']?.name === 'yoone', isZyn: attrsObj?.['brand']?.name === 'zyn', + isZex: attrsObj?.['brand']?.name === 'zex', isYooneNew: attrsObj?.['brand']?.name === 'yoone' && attrsObj?.['version']?.name === 'new', size: this.extractNumberFromString(attrsObj?.['strength']?.name) || null, }); return orderSale - }) + }).filter(v => v !== null) if (orderSales.length > 0) { await this.orderSaleModel.save(orderSales); @@ -1185,53 +1185,7 @@ export class OrderService { ) END ), JSON_ARRAY() - ) as fulfillments, - ( - SELECT COALESCE( - JSON_ARRAYAGG( - JSON_OBJECT( - 'id', oi.id, - 'name', oi.name, - 'orderId', oi.orderId, - 'siteId', oi.siteId, - 'externalOrderId', oi.externalOrderId, - 'externalOrderItemId', oi.externalOrderItemId, - 'externalProductId', oi.externalProductId, - 'externalVariationId', oi.externalVariationId, - 'quantity', oi.quantity, - 'subtotal', oi.subtotal, - 'subtotal_tax', oi.subtotal_tax, - 'total', oi.total, - 'total_tax', oi.total_tax, - 'sku', oi.sku, - 'price', oi.price - ) - ), - JSON_ARRAY() - ) - FROM order_item oi - WHERE oi.orderId = o.id - ) AS orderItems, - ( - SELECT COALESCE( - JSON_ARRAYAGG( - JSON_OBJECT( - 'id', os.id, - 'orderId', os.orderId, - 'siteId', os.siteId, - 'externalOrderItemId', os.externalOrderItemId, - 'productId', os.productId, - 'name', os.name, - 'sku', os.sku, - 'quantity', os.quantity, - 'isPackage', os.isPackage - ) - ), - JSON_ARRAY() - ) - FROM order_sale os - WHERE os.orderId = o.id - ) AS orderSales + ) as fulfillments FROM \`order\` o LEFT JOIN ( SELECT @@ -1273,13 +1227,13 @@ export class OrderService { parameters.push(siteId); } if (startDate) { - sqlQuery += ` AND o.date_paid >= ?`; - totalQuery += ` AND o.date_paid >= ?`; + sqlQuery += ` AND o.date_created >= ?`; + totalQuery += ` AND o.date_created >= ?`; parameters.push(startDate); } if (endDate) { - sqlQuery += ` AND o.date_paid <= ?`; - totalQuery += ` AND o.date_paid <= ?`; + sqlQuery += ` AND o.date_created <= ?`; + totalQuery += ` AND o.date_created <= ?`; parameters.push(endDate); } // 支付方式筛选(使用参数化,避免SQL注入) @@ -1367,7 +1321,7 @@ export class OrderService { // 添加分页到主查询 sqlQuery += ` GROUP BY o.id - ORDER BY o.date_paid DESC + ORDER BY o.date_created DESC LIMIT ? OFFSET ? `; parameters.push(pageSize, (current - 1) * pageSize); @@ -2585,7 +2539,7 @@ export class OrderService { '姓名地址': nameAddress, '邮箱': order.customer_email || '', '号码': phone, - '订单内容': this.removeLastParenthesesContent(orderContent), + '订单内容': orderContent, '盒数': boxCount, '换盒数': exchangeBoxCount, '换货内容': exchangeContent, @@ -2686,84 +2640,6 @@ export class OrderService { } } - /** - * 删除每个分号前面一个左右括号和最后一个左右括号包含的内容(包括括号本身) - * @param str 输入字符串 - * @returns 删除后的字符串 - */ - removeLastParenthesesContent(str: string): string { - if (!str || typeof str !== 'string') { - return str; - } - - // 辅助函数:删除指定位置的括号对及其内容 - const removeParenthesesAt = (s: string, leftIndex: number): string => { - if (leftIndex === -1) return s; - - let rightIndex = -1; - let parenCount = 0; - - for (let i = leftIndex; i < s.length; i++) { - const char = s[i]; - if (char === '(') { - parenCount++; - } else if (char === ')') { - parenCount--; - if (parenCount === 0) { - rightIndex = i; - break; - } - } - } - - if (rightIndex !== -1) { - return s.substring(0, leftIndex) + s.substring(rightIndex + 1); - } - - return s; - }; - - // 1. 处理每个分号前面的括号对 - let result = str; - - // 找出所有分号的位置 - const semicolonIndices: number[] = []; - for (let i = 0; i < result.length; i++) { - if (result[i] === ';') { - semicolonIndices.push(i); - } - } - - // 从后向前处理每个分号,避免位置变化影响后续处理 - for (let i = semicolonIndices.length - 1; i >= 0; i--) { - const semicolonIndex = semicolonIndices[i]; - - // 从分号位置向前查找最近的左括号 - let lastLeftParenIndex = -1; - for (let j = semicolonIndex - 1; j >= 0; j--) { - if (result[j] === '(') { - lastLeftParenIndex = j; - break; - } - } - - // 如果找到左括号,删除该括号对及其内容 - if (lastLeftParenIndex !== -1) { - result = removeParenthesesAt(result, lastLeftParenIndex); - } - } - - // 2. 处理整个字符串的最后一个括号对 - let lastLeftParenIndex = result.lastIndexOf('('); - if (lastLeftParenIndex !== -1) { - result = removeParenthesesAt(result, lastLeftParenIndex); - } - - return result; - } - - - }