forked from yoone/API
refactor: 移除未使用的导入和注释掉的生命周期钩子
This commit is contained in:
parent
64c1d1afe5
commit
b2ee61e47d
|
|
@ -1,8 +1,8 @@
|
||||||
import { ApiProperty } from '@midwayjs/swagger';
|
import { ApiProperty } from '@midwayjs/swagger';
|
||||||
import { Exclude, Expose } from 'class-transformer';
|
import { Exclude, Expose } from 'class-transformer';
|
||||||
import {
|
import {
|
||||||
BeforeInsert,
|
// BeforeInsert,
|
||||||
BeforeUpdate,
|
// BeforeUpdate,
|
||||||
Column,
|
Column,
|
||||||
CreateDateColumn,
|
CreateDateColumn,
|
||||||
Entity,
|
Entity,
|
||||||
|
|
|
||||||
|
|
@ -39,6 +39,7 @@ import * as path from 'path';
|
||||||
import * as os from 'os';
|
import * as os from 'os';
|
||||||
import { UnifiedOrderDTO } from '../dto/site-api.dto';
|
import { UnifiedOrderDTO } from '../dto/site-api.dto';
|
||||||
import { CustomerService } from './customer.service';
|
import { CustomerService } from './customer.service';
|
||||||
|
import { ProductService } from './product.service';
|
||||||
@Provide()
|
@Provide()
|
||||||
export class OrderService {
|
export class OrderService {
|
||||||
|
|
||||||
|
|
@ -140,7 +141,7 @@ export class OrderService {
|
||||||
updated: 0,
|
updated: 0,
|
||||||
errors: []
|
errors: []
|
||||||
};
|
};
|
||||||
console.log('开始进入循环同步订单', result.length, '个订单')
|
|
||||||
// 遍历每个订单进行同步
|
// 遍历每个订单进行同步
|
||||||
for (const order of result) {
|
for (const order of result) {
|
||||||
try {
|
try {
|
||||||
|
|
@ -164,7 +165,6 @@ export class OrderService {
|
||||||
} else {
|
} else {
|
||||||
syncResult.created++;
|
syncResult.created++;
|
||||||
}
|
}
|
||||||
// console.log('updated', syncResult.updated, 'created:', syncResult.created)
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
// 记录错误但不中断整个同步过程
|
// 记录错误但不中断整个同步过程
|
||||||
syncResult.errors.push({
|
syncResult.errors.push({
|
||||||
|
|
@ -174,8 +174,6 @@ export class OrderService {
|
||||||
syncResult.processed++;
|
syncResult.processed++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
console.log('同步完成', syncResult.updated, 'created:', syncResult.created)
|
|
||||||
|
|
||||||
this.logger.debug('syncOrders result', syncResult)
|
this.logger.debug('syncOrders result', syncResult)
|
||||||
return syncResult;
|
return syncResult;
|
||||||
}
|
}
|
||||||
|
|
@ -311,7 +309,6 @@ export class OrderService {
|
||||||
* @param order 订单数据
|
* @param order 订单数据
|
||||||
* @param forceUpdate 是否强制更新
|
* @param forceUpdate 是否强制更新
|
||||||
*/
|
*/
|
||||||
async syncSingleOrder(siteId: number, order: UnifiedOrderDTO, forceUpdate = false) {
|
|
||||||
async syncSingleOrder(siteId: number, order: UnifiedOrderDTO, forceUpdate = false) {
|
async syncSingleOrder(siteId: number, order: UnifiedOrderDTO, forceUpdate = false) {
|
||||||
// 从订单数据中解构出各个子项
|
// 从订单数据中解构出各个子项
|
||||||
let {
|
let {
|
||||||
|
|
@ -353,14 +350,14 @@ export class OrderService {
|
||||||
await this.saveOrderItems({
|
await this.saveOrderItems({
|
||||||
siteId,
|
siteId,
|
||||||
orderId,
|
orderId,
|
||||||
externalOrderId: String(externalOrderId),
|
externalOrderId,
|
||||||
orderItems: line_items,
|
orderItems: line_items,
|
||||||
});
|
});
|
||||||
// 保存退款信息
|
// 保存退款信息
|
||||||
await this.saveOrderRefunds({
|
await this.saveOrderRefunds({
|
||||||
siteId,
|
siteId,
|
||||||
orderId,
|
orderId,
|
||||||
externalOrderId ,
|
externalOrderId,
|
||||||
refunds,
|
refunds,
|
||||||
});
|
});
|
||||||
// 保存费用信息
|
// 保存费用信息
|
||||||
|
|
@ -452,7 +449,7 @@ export class OrderService {
|
||||||
* @returns 保存后的订单实体
|
* @returns 保存后的订单实体
|
||||||
*/
|
*/
|
||||||
// 这里 omit 是因为处理在外头了 其实 saveOrder 应该包括 savelineitems 等
|
// 这里 omit 是因为处理在外头了 其实 saveOrder 应该包括 savelineitems 等
|
||||||
async saveOrder(siteId: number, order: Omit<UnifiedOrderDTO, 'line_items'|'refunds'>): Promise<Order> {
|
async saveOrder(siteId: number, order: Omit<UnifiedOrderDTO, 'line_items' | 'refunds'>): Promise<Order> {
|
||||||
// 将外部订单ID转换为字符串
|
// 将外部订单ID转换为字符串
|
||||||
const externalOrderId = String(order.id)
|
const externalOrderId = String(order.id)
|
||||||
delete order.id
|
delete order.id
|
||||||
|
|
@ -725,12 +722,14 @@ export class OrderService {
|
||||||
return {
|
return {
|
||||||
product: await this.productModel.findOne({
|
product: await this.productModel.findOne({
|
||||||
where: { sku: comp.sku },
|
where: { sku: comp.sku },
|
||||||
|
relations: ['components','attributes'],
|
||||||
}),
|
}),
|
||||||
quantity: comp.quantity * orderItem.quantity,
|
quantity: comp.quantity * orderItem.quantity,
|
||||||
}
|
}
|
||||||
})) : [{ product, quantity: orderItem.quantity }];
|
})) : [{ product, quantity: orderItem.quantity }]
|
||||||
|
|
||||||
const orderSales: OrderSale[] = componentDetails.map(componentDetail => {
|
const orderSales: OrderSale[] = componentDetails.map(componentDetail => {
|
||||||
|
if(!componentDetail.product) return null
|
||||||
const attrsObj = this.productService.getAttributesObject(product.attributes)
|
const attrsObj = this.productService.getAttributesObject(product.attributes)
|
||||||
const orderSale = plainToClass(OrderSale, {
|
const orderSale = plainToClass(OrderSale, {
|
||||||
orderId: orderItem.orderId,
|
orderId: orderItem.orderId,
|
||||||
|
|
@ -743,11 +742,12 @@ export class OrderService {
|
||||||
isPackage: componentDetail.product.type === 'bundle',
|
isPackage: componentDetail.product.type === 'bundle',
|
||||||
isYoone: attrsObj?.['brand']?.name === 'yoone',
|
isYoone: attrsObj?.['brand']?.name === 'yoone',
|
||||||
isZyn: attrsObj?.['brand']?.name === 'zyn',
|
isZyn: attrsObj?.['brand']?.name === 'zyn',
|
||||||
|
isZex: attrsObj?.['brand']?.name === 'zex',
|
||||||
isYooneNew: attrsObj?.['brand']?.name === 'yoone' && attrsObj?.['version']?.name === 'new',
|
isYooneNew: attrsObj?.['brand']?.name === 'yoone' && attrsObj?.['version']?.name === 'new',
|
||||||
size: this.extractNumberFromString(attrsObj?.['strength']?.name) || null,
|
size: this.extractNumberFromString(attrsObj?.['strength']?.name) || null,
|
||||||
});
|
});
|
||||||
return orderSale
|
return orderSale
|
||||||
})
|
}).filter(v => v !== null)
|
||||||
|
|
||||||
if (orderSales.length > 0) {
|
if (orderSales.length > 0) {
|
||||||
await this.orderSaleModel.save(orderSales);
|
await this.orderSaleModel.save(orderSales);
|
||||||
|
|
@ -1185,53 +1185,7 @@ export class OrderService {
|
||||||
) END
|
) END
|
||||||
),
|
),
|
||||||
JSON_ARRAY()
|
JSON_ARRAY()
|
||||||
) as fulfillments,
|
) 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
|
|
||||||
FROM \`order\` o
|
FROM \`order\` o
|
||||||
LEFT JOIN (
|
LEFT JOIN (
|
||||||
SELECT
|
SELECT
|
||||||
|
|
@ -1273,13 +1227,13 @@ export class OrderService {
|
||||||
parameters.push(siteId);
|
parameters.push(siteId);
|
||||||
}
|
}
|
||||||
if (startDate) {
|
if (startDate) {
|
||||||
sqlQuery += ` AND o.date_paid >= ?`;
|
sqlQuery += ` AND o.date_created >= ?`;
|
||||||
totalQuery += ` AND o.date_paid >= ?`;
|
totalQuery += ` AND o.date_created >= ?`;
|
||||||
parameters.push(startDate);
|
parameters.push(startDate);
|
||||||
}
|
}
|
||||||
if (endDate) {
|
if (endDate) {
|
||||||
sqlQuery += ` AND o.date_paid <= ?`;
|
sqlQuery += ` AND o.date_created <= ?`;
|
||||||
totalQuery += ` AND o.date_paid <= ?`;
|
totalQuery += ` AND o.date_created <= ?`;
|
||||||
parameters.push(endDate);
|
parameters.push(endDate);
|
||||||
}
|
}
|
||||||
// 支付方式筛选(使用参数化,避免SQL注入)
|
// 支付方式筛选(使用参数化,避免SQL注入)
|
||||||
|
|
@ -1367,7 +1321,7 @@ export class OrderService {
|
||||||
// 添加分页到主查询
|
// 添加分页到主查询
|
||||||
sqlQuery += `
|
sqlQuery += `
|
||||||
GROUP BY o.id
|
GROUP BY o.id
|
||||||
ORDER BY o.date_paid DESC
|
ORDER BY o.date_created DESC
|
||||||
LIMIT ? OFFSET ?
|
LIMIT ? OFFSET ?
|
||||||
`;
|
`;
|
||||||
parameters.push(pageSize, (current - 1) * pageSize);
|
parameters.push(pageSize, (current - 1) * pageSize);
|
||||||
|
|
@ -2585,7 +2539,7 @@ export class OrderService {
|
||||||
'姓名地址': nameAddress,
|
'姓名地址': nameAddress,
|
||||||
'邮箱': order.customer_email || '',
|
'邮箱': order.customer_email || '',
|
||||||
'号码': phone,
|
'号码': phone,
|
||||||
'订单内容': this.removeLastParenthesesContent(orderContent),
|
'订单内容': orderContent,
|
||||||
'盒数': boxCount,
|
'盒数': boxCount,
|
||||||
'换盒数': exchangeBoxCount,
|
'换盒数': exchangeBoxCount,
|
||||||
'换货内容': exchangeContent,
|
'换货内容': 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;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue