zksu
/
API
forked from yoone/API
1
0
Fork 0

fix(订单服务): 修复关联订单列表去重逻辑并优化查询结果

确保关联订单列表中的条目不会重复,通过使用Set来跟踪已处理的外部ID
移除不必要的orders数组填充,简化返回数据结构
This commit is contained in:
tikkhun 2025-11-21 11:58:13 +08:00
parent 27935f113d
commit 1bdae88c11
1 changed files with 16 additions and 10 deletions

View File

@ -1330,10 +1330,21 @@ export class OrderService {
let relatedList: any[] = [];
try {
const related = await this.getRelatedByOrder(id);
relatedList = [
...(Array.isArray(related?.subscriptions) ? related.subscriptions : []),
...(Array.isArray(related?.orders) ? related.orders : []),
];
const subs = Array.isArray(related?.subscriptions) ? related.subscriptions : [];
const ords = Array.isArray(related?.orders) ? related.orders : [];
const seen = new Set<string>();
const merge = [...subs, ...ords];
for (const it of merge) {
const key = it?.externalSubscriptionId
? `sub:${it.externalSubscriptionId}`
: it?.externalOrderId
? `ord:${it.externalOrderId}`
: `id:${it?.id}`;
if (!seen.has(key)) {
seen.add(key);
relatedList.push(it);
}
}
} catch (error) {
// 关联查询失败不影响详情返回
}
@ -1360,15 +1371,10 @@ export class OrderService {
WHERE s.siteId = ? AND s.parent_id = ?
`;
const subscriptions = await this.orderModel.query(subSql, [siteId, order.externalOrderId]);
const allOrdersMap = new Map<number, any>();
subscriptions.forEach(o => allOrdersMap.set(o.id, o));
return {
order,
subscriptions,
orders: Array.from(allOrdersMap.values()),
orders: [],
};
}