forked from yoone/API
1
0
Fork 0

refactor: 移除未使用的导入和注释掉的生命周期钩子

This commit is contained in:
tikkhun 2026-01-10 11:52:04 +08:00
parent 64c1d1afe5
commit b2ee61e47d
2 changed files with 19 additions and 143 deletions

View File

@ -1,8 +1,8 @@
import { ApiProperty } from '@midwayjs/swagger';
import { Exclude, Expose } from 'class-transformer';
import {
BeforeInsert,
BeforeUpdate,
// BeforeInsert,
// BeforeUpdate,
Column,
CreateDateColumn,
Entity,

View File

@ -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,7 +350,7 @@ export class OrderService {
await this.saveOrderItems({
siteId,
orderId,
externalOrderId: String(externalOrderId),
externalOrderId,
orderItems: line_items,
});
// 保存退款信息
@ -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;
}
}