fix(订单服务): 修复关联订单列表去重逻辑并优化查询结果
确保关联订单列表中的条目不会重复,通过使用Set来跟踪已处理的外部ID 移除不必要的orders数组填充,简化返回数据结构
This commit is contained in:
parent
27935f113d
commit
1bdae88c11
|
|
@ -1330,10 +1330,21 @@ export class OrderService {
|
||||||
let relatedList: any[] = [];
|
let relatedList: any[] = [];
|
||||||
try {
|
try {
|
||||||
const related = await this.getRelatedByOrder(id);
|
const related = await this.getRelatedByOrder(id);
|
||||||
relatedList = [
|
const subs = Array.isArray(related?.subscriptions) ? related.subscriptions : [];
|
||||||
...(Array.isArray(related?.subscriptions) ? related.subscriptions : []),
|
const ords = Array.isArray(related?.orders) ? related.orders : [];
|
||||||
...(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) {
|
} catch (error) {
|
||||||
// 关联查询失败不影响详情返回
|
// 关联查询失败不影响详情返回
|
||||||
}
|
}
|
||||||
|
|
@ -1360,15 +1371,10 @@ export class OrderService {
|
||||||
WHERE s.siteId = ? AND s.parent_id = ?
|
WHERE s.siteId = ? AND s.parent_id = ?
|
||||||
`;
|
`;
|
||||||
const subscriptions = await this.orderModel.query(subSql, [siteId, order.externalOrderId]);
|
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 {
|
return {
|
||||||
order,
|
order,
|
||||||
subscriptions,
|
subscriptions,
|
||||||
orders: Array.from(allOrdersMap.values()),
|
orders: [],
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue