@pisell/pisellos 2.2.191 → 2.2.192
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/server/modules/order/index.d.ts +1 -68
- package/dist/server/modules/order/index.js +334 -761
- package/dist/server/modules/products/index.d.ts +0 -57
- package/dist/server/modules/products/index.js +347 -706
- package/lib/server/modules/order/index.d.ts +1 -68
- package/lib/server/modules/order/index.js +6 -376
- package/lib/server/modules/products/index.d.ts +0 -57
- package/lib/server/modules/products/index.js +0 -333
- package/package.json +1 -1
|
@@ -40,9 +40,6 @@ var INDEXDB_STORE_NAME = "orders";
|
|
|
40
40
|
var ORDER_LAST_FULL_FETCH_AT_STORAGE_KEY = "server_order_last_full_fetch_at";
|
|
41
41
|
var ORDER_SYNC_THROTTLE_MS_STORAGE_KEY = "order_sync_throttle_ms";
|
|
42
42
|
var DEFAULT_ORDER_SYNC_THROTTLE_MS = 2e3;
|
|
43
|
-
var ORDER_FLOW_LOG_WINDOW_KEY = "__orderModuleFlowLogs";
|
|
44
|
-
var ORDER_WRITE_STATS_WINDOW_KEY = "__orderModuleWriteStats";
|
|
45
|
-
var ORDER_FLOW_LOG_MAX_LENGTH = 1e3;
|
|
46
43
|
var ORDER_SQLITE_DEDUPE_MS = 15e3;
|
|
47
44
|
var ORDER_SILENT_REFRESH_MIN_INTERVAL_MS = 3e4;
|
|
48
45
|
var ORDER_BUSINESS_WRITE_SOURCES = /* @__PURE__ */ new Set([
|
|
@@ -60,8 +57,6 @@ var OrderModule = class extends import_BaseModule.BaseModule {
|
|
|
60
57
|
// Map<resource_id, OrderId[]> 资源到订单的倒排索引
|
|
61
58
|
this.resourceIdIndex = /* @__PURE__ */ new Map();
|
|
62
59
|
this.orderSQLiteSaveQueue = Promise.resolve();
|
|
63
|
-
/** 全局递增的 SQLite 写入序号,便于日志关联同一次操作 */
|
|
64
|
-
this.orderFlowWriteSeq = 0;
|
|
65
60
|
/** 按 storageKey 记录最近一次 SQLite 写入来源与时间,用于去重 */
|
|
66
61
|
this.recentSqliteWriteByStorageKey = /* @__PURE__ */ new Map();
|
|
67
62
|
/** 最近一次 SSE 全量拉取完成时间 */
|
|
@@ -128,15 +123,8 @@ var OrderModule = class extends import_BaseModule.BaseModule {
|
|
|
128
123
|
this.store.list = [];
|
|
129
124
|
this.store.map = /* @__PURE__ */ new Map();
|
|
130
125
|
}
|
|
131
|
-
this.initOrderFlowLogs();
|
|
132
126
|
this.initOrderDataSource();
|
|
133
127
|
this.setupOrderSync();
|
|
134
|
-
this.pushOrderFlowLog("initialize.complete", {
|
|
135
|
-
hasDbManager: !!this.dbManager,
|
|
136
|
-
hasLogger: !!this.logger,
|
|
137
|
-
initialOrderCount: this.store.list.length,
|
|
138
|
-
createdAtQuery: this.store.createdAtQuery || null
|
|
139
|
-
});
|
|
140
128
|
this.logInfo("OrderServer模块初始化完成", {
|
|
141
129
|
hasDbManager: !!this.dbManager,
|
|
142
130
|
hasLogger: !!this.logger,
|
|
@@ -176,144 +164,6 @@ var OrderModule = class extends import_BaseModule.BaseModule {
|
|
|
176
164
|
}
|
|
177
165
|
} catch {
|
|
178
166
|
}
|
|
179
|
-
this.pushOrderFlowLog(`error.${title}`, metadata);
|
|
180
|
-
}
|
|
181
|
-
/**
|
|
182
|
-
* 初始化 window 上的订单流程调试日志数组及辅助方法。
|
|
183
|
-
*
|
|
184
|
-
* @example
|
|
185
|
-
* window.clearOrderModuleFlowLogs()
|
|
186
|
-
* // 操作完成后:
|
|
187
|
-
* copy(window.exportOrderModuleFlowLogs?.() || '[]')
|
|
188
|
-
*/
|
|
189
|
-
initOrderFlowLogs() {
|
|
190
|
-
try {
|
|
191
|
-
const win = typeof globalThis !== "undefined" ? globalThis.window : void 0;
|
|
192
|
-
if (!win)
|
|
193
|
-
return;
|
|
194
|
-
win[ORDER_FLOW_LOG_WINDOW_KEY] = [];
|
|
195
|
-
win[ORDER_WRITE_STATS_WINDOW_KEY] = {};
|
|
196
|
-
win.clearOrderModuleFlowLogs = () => {
|
|
197
|
-
win[ORDER_FLOW_LOG_WINDOW_KEY] = [];
|
|
198
|
-
win[ORDER_WRITE_STATS_WINDOW_KEY] = {};
|
|
199
|
-
this.orderFlowWriteSeq = 0;
|
|
200
|
-
this.recentSqliteWriteByStorageKey.clear();
|
|
201
|
-
this.lastServerFullFetchAtMs = 0;
|
|
202
|
-
this.pushOrderFlowLog("debug.clear", {});
|
|
203
|
-
};
|
|
204
|
-
win.exportOrderModuleFlowLogs = () => {
|
|
205
|
-
const logs = win[ORDER_FLOW_LOG_WINDOW_KEY] || [];
|
|
206
|
-
return JSON.stringify(logs, null, 2);
|
|
207
|
-
};
|
|
208
|
-
win.exportOrderModuleWriteStats = () => {
|
|
209
|
-
const stats = win[ORDER_WRITE_STATS_WINDOW_KEY] || {};
|
|
210
|
-
return JSON.stringify(stats, null, 2);
|
|
211
|
-
};
|
|
212
|
-
win.summarizeOrderModuleWrites = (topN = 20) => {
|
|
213
|
-
const stats = win[ORDER_WRITE_STATS_WINDOW_KEY] || {};
|
|
214
|
-
return Object.entries(stats).map(([storageKey, entries]) => {
|
|
215
|
-
const list = entries || [];
|
|
216
|
-
const sources = [...new Set(list.map((item) => item.source))];
|
|
217
|
-
const last = list[list.length - 1];
|
|
218
|
-
return {
|
|
219
|
-
storageKey,
|
|
220
|
-
orderId: (last == null ? void 0 : last.orderId) ?? null,
|
|
221
|
-
writeCount: list.length,
|
|
222
|
-
sources,
|
|
223
|
-
lastWriteAt: (last == null ? void 0 : last.ts) || ""
|
|
224
|
-
};
|
|
225
|
-
}).sort((a, b) => b.writeCount - a.writeCount).slice(0, topN);
|
|
226
|
-
};
|
|
227
|
-
this.pushOrderFlowLog("debug.init", {
|
|
228
|
-
logKey: ORDER_FLOW_LOG_WINDOW_KEY,
|
|
229
|
-
writeStatsKey: ORDER_WRITE_STATS_WINDOW_KEY,
|
|
230
|
-
helpers: [
|
|
231
|
-
"clearOrderModuleFlowLogs",
|
|
232
|
-
"exportOrderModuleFlowLogs",
|
|
233
|
-
"exportOrderModuleWriteStats",
|
|
234
|
-
"summarizeOrderModuleWrites"
|
|
235
|
-
]
|
|
236
|
-
});
|
|
237
|
-
} catch {
|
|
238
|
-
}
|
|
239
|
-
}
|
|
240
|
-
/**
|
|
241
|
-
* 写入 window 订单流程调试日志。
|
|
242
|
-
*
|
|
243
|
-
* @example
|
|
244
|
-
* this.pushOrderFlowLog('mergeOrdersToStore.start', { count: 3 })
|
|
245
|
-
*/
|
|
246
|
-
pushOrderFlowLog(step, metadata) {
|
|
247
|
-
try {
|
|
248
|
-
const win = typeof globalThis !== "undefined" ? globalThis.window : void 0;
|
|
249
|
-
if (!win)
|
|
250
|
-
return;
|
|
251
|
-
if (!Array.isArray(win[ORDER_FLOW_LOG_WINDOW_KEY])) {
|
|
252
|
-
win[ORDER_FLOW_LOG_WINDOW_KEY] = [];
|
|
253
|
-
}
|
|
254
|
-
const entry = {
|
|
255
|
-
ts: (/* @__PURE__ */ new Date()).toISOString(),
|
|
256
|
-
step,
|
|
257
|
-
metadata: metadata || {}
|
|
258
|
-
};
|
|
259
|
-
win[ORDER_FLOW_LOG_WINDOW_KEY].push(entry);
|
|
260
|
-
const logs = win[ORDER_FLOW_LOG_WINDOW_KEY];
|
|
261
|
-
if (logs.length > ORDER_FLOW_LOG_MAX_LENGTH) {
|
|
262
|
-
logs.splice(0, logs.length - ORDER_FLOW_LOG_MAX_LENGTH);
|
|
263
|
-
}
|
|
264
|
-
console.log(`[OrderFlow] ${step}`, metadata || {});
|
|
265
|
-
} catch {
|
|
266
|
-
}
|
|
267
|
-
}
|
|
268
|
-
/** 当前内存 store 快照信息,供流程日志使用 */
|
|
269
|
-
getOrderStoreSnapshot() {
|
|
270
|
-
return {
|
|
271
|
-
storeListCount: this.store.list.length,
|
|
272
|
-
storeMapCount: this.store.map.size,
|
|
273
|
-
pendingSyncCount: this.pendingSyncMessages.length,
|
|
274
|
-
isProcessingSyncBatch: this.isProcessingSyncBatch,
|
|
275
|
-
isIdlePhase: this.isIdlePhase
|
|
276
|
-
};
|
|
277
|
-
}
|
|
278
|
-
/**
|
|
279
|
-
* 记录一次 SQLite 写入到 window 统计,用于排查同一订单被多次写入的原因。
|
|
280
|
-
*
|
|
281
|
-
* @example
|
|
282
|
-
* this.trackOrderWrite('upsertOrdersFromRemote', 'bulkUpdate', orders, 'insert')
|
|
283
|
-
*/
|
|
284
|
-
trackOrderWrite(source, writeMethod, orders, mergeActions = {}, defaultMergeAction = "update") {
|
|
285
|
-
this.orderFlowWriteSeq += 1;
|
|
286
|
-
const writeSeq = this.orderFlowWriteSeq;
|
|
287
|
-
try {
|
|
288
|
-
const win = typeof globalThis !== "undefined" ? globalThis.window : void 0;
|
|
289
|
-
if (!win)
|
|
290
|
-
return writeSeq;
|
|
291
|
-
if (!win[ORDER_WRITE_STATS_WINDOW_KEY]) {
|
|
292
|
-
win[ORDER_WRITE_STATS_WINDOW_KEY] = {};
|
|
293
|
-
}
|
|
294
|
-
const stats = win[ORDER_WRITE_STATS_WINDOW_KEY];
|
|
295
|
-
for (const order of orders) {
|
|
296
|
-
const storageKey = this.getOrderStorageKey(order);
|
|
297
|
-
if (!storageKey)
|
|
298
|
-
continue;
|
|
299
|
-
const orderKey = order.order_id !== void 0 && order.order_id !== null ? this.getIdKey(order.order_id) : "";
|
|
300
|
-
const mergeAction = orderKey && mergeActions[orderKey] || defaultMergeAction;
|
|
301
|
-
const entry = {
|
|
302
|
-
ts: (/* @__PURE__ */ new Date()).toISOString(),
|
|
303
|
-
writeSeq,
|
|
304
|
-
source,
|
|
305
|
-
writeMethod,
|
|
306
|
-
orderId: order.order_id ?? null,
|
|
307
|
-
storageKey,
|
|
308
|
-
mergeAction
|
|
309
|
-
};
|
|
310
|
-
if (!stats[storageKey])
|
|
311
|
-
stats[storageKey] = [];
|
|
312
|
-
stats[storageKey].push(entry);
|
|
313
|
-
}
|
|
314
|
-
} catch {
|
|
315
|
-
}
|
|
316
|
-
return writeSeq;
|
|
317
167
|
}
|
|
318
168
|
/**
|
|
319
169
|
* 记录订单最近一次 SQLite 写入,供 pubsub 回声去重使用。
|
|
@@ -478,21 +328,15 @@ var OrderModule = class extends import_BaseModule.BaseModule {
|
|
|
478
328
|
return DEFAULT_ORDER_SYNC_THROTTLE_MS;
|
|
479
329
|
}
|
|
480
330
|
async preload() {
|
|
481
|
-
this.pushOrderFlowLog("preload.start", this.getOrderStoreSnapshot());
|
|
482
331
|
const getData = async () => {
|
|
483
332
|
const orders = await this.loadOrdersByServer();
|
|
484
333
|
if (orders.length === 0) {
|
|
485
|
-
this.pushOrderFlowLog("preload.empty", this.getOrderStoreSnapshot());
|
|
486
334
|
return;
|
|
487
335
|
}
|
|
488
336
|
this.store.list = (0, import_lodash_es.cloneDeep)(orders);
|
|
489
337
|
this.syncOrdersMap();
|
|
490
338
|
this.core.effects.emit(import_types.OrderHooks.onOrdersChanged, this.store.list);
|
|
491
339
|
this.core.effects.emit(import_types.OrderHooks.onOrdersSyncCompleted, null);
|
|
492
|
-
this.pushOrderFlowLog("preload.done", {
|
|
493
|
-
...this.getOrderStoreSnapshot(),
|
|
494
|
-
loadedCount: orders.length
|
|
495
|
-
});
|
|
496
340
|
};
|
|
497
341
|
getData();
|
|
498
342
|
}
|
|
@@ -510,10 +354,8 @@ var OrderModule = class extends import_BaseModule.BaseModule {
|
|
|
510
354
|
const key = this.getIdKey(orderId);
|
|
511
355
|
const memoryOrder = this.store.map.get(key);
|
|
512
356
|
if (memoryOrder) {
|
|
513
|
-
this.pushOrderFlowLog("getLocalOrderByOrderId.hitMemory", { orderId, key });
|
|
514
357
|
return memoryOrder;
|
|
515
358
|
}
|
|
516
|
-
this.pushOrderFlowLog("getLocalOrderByOrderId.missMemory", { orderId, key });
|
|
517
359
|
const orders = await this.loadOrdersFromSQLite();
|
|
518
360
|
const localOrder = orders.find((order) => {
|
|
519
361
|
const shopOrderNumber = order == null ? void 0 : order.shop_order_number;
|
|
@@ -531,16 +373,11 @@ var OrderModule = class extends import_BaseModule.BaseModule {
|
|
|
531
373
|
return localOrder;
|
|
532
374
|
}
|
|
533
375
|
async loadOrdersByServer() {
|
|
534
|
-
var _a
|
|
376
|
+
var _a;
|
|
535
377
|
let orderList = [];
|
|
536
|
-
this.pushOrderFlowLog("loadOrdersByServer.start", {
|
|
537
|
-
...this.getOrderStoreSnapshot(),
|
|
538
|
-
query: ((_a = this.store) == null ? void 0 : _a.createdAtQuery) || null,
|
|
539
|
-
hasOrderDataSource: !!this.orderDataSource
|
|
540
|
-
});
|
|
541
378
|
this.logInfo("loadOrdersByServer-开始", {
|
|
542
379
|
hasOrderDataSource: !!this.orderDataSource,
|
|
543
|
-
query: ((
|
|
380
|
+
query: ((_a = this.store) == null ? void 0 : _a.createdAtQuery) || null
|
|
544
381
|
});
|
|
545
382
|
if (this.orderDataSource) {
|
|
546
383
|
try {
|
|
@@ -550,7 +387,6 @@ var OrderModule = class extends import_BaseModule.BaseModule {
|
|
|
550
387
|
}
|
|
551
388
|
});
|
|
552
389
|
orderList = data || [];
|
|
553
|
-
this.pushOrderFlowLog("loadOrdersByServer.sseSuccess", { count: orderList.length });
|
|
554
390
|
this.logInfo("loadOrdersByServer-SSE拉取成功", {
|
|
555
391
|
count: orderList.length
|
|
556
392
|
});
|
|
@@ -558,15 +394,10 @@ var OrderModule = class extends import_BaseModule.BaseModule {
|
|
|
558
394
|
this.lastServerFullFetchAtMs = Date.now();
|
|
559
395
|
} catch {
|
|
560
396
|
orderList = [];
|
|
561
|
-
this.pushOrderFlowLog("loadOrdersByServer.sseFailed", {});
|
|
562
397
|
this.logInfo("loadOrdersByServer-SSE拉取失败,回退为空数组");
|
|
563
398
|
}
|
|
564
399
|
}
|
|
565
400
|
await this.replaceOrdersSnapshotInSQLite(orderList, "loadOrdersByServer");
|
|
566
|
-
this.pushOrderFlowLog("loadOrdersByServer.done", {
|
|
567
|
-
count: orderList.length,
|
|
568
|
-
...this.getOrderStoreSnapshot()
|
|
569
|
-
});
|
|
570
401
|
this.logInfo("loadOrdersByServer-结束", {
|
|
571
402
|
count: orderList.length
|
|
572
403
|
});
|
|
@@ -582,17 +413,8 @@ var OrderModule = class extends import_BaseModule.BaseModule {
|
|
|
582
413
|
const startTime = Date.now();
|
|
583
414
|
const elapsedSinceFullFetch = Date.now() - this.lastServerFullFetchAtMs;
|
|
584
415
|
if (this.lastServerFullFetchAtMs > 0 && elapsedSinceFullFetch < ORDER_SILENT_REFRESH_MIN_INTERVAL_MS) {
|
|
585
|
-
this.pushOrderFlowLog("silentRefresh.skipped", {
|
|
586
|
-
elapsedMs: elapsedSinceFullFetch,
|
|
587
|
-
minIntervalMs: ORDER_SILENT_REFRESH_MIN_INTERVAL_MS,
|
|
588
|
-
...this.getOrderStoreSnapshot()
|
|
589
|
-
});
|
|
590
416
|
return this.store.list;
|
|
591
417
|
}
|
|
592
|
-
this.pushOrderFlowLog("silentRefresh.start", {
|
|
593
|
-
...this.getOrderStoreSnapshot(),
|
|
594
|
-
callerHint: "Server.refreshOrdersInBackground 或其他调用方"
|
|
595
|
-
});
|
|
596
418
|
this.logInfo("silentRefresh 开始");
|
|
597
419
|
try {
|
|
598
420
|
const orders = await this.loadOrdersByServer();
|
|
@@ -601,17 +423,11 @@ var OrderModule = class extends import_BaseModule.BaseModule {
|
|
|
601
423
|
this.syncOrdersMap();
|
|
602
424
|
this.core.effects.emit(import_types.OrderHooks.onOrdersChanged, this.store.list);
|
|
603
425
|
await this.core.effects.emit(import_types.OrderHooks.onOrdersSyncCompleted, null);
|
|
604
|
-
this.pushOrderFlowLog("silentRefresh.done", {
|
|
605
|
-
orderCount: orders.length,
|
|
606
|
-
durationMs: Date.now() - startTime,
|
|
607
|
-
...this.getOrderStoreSnapshot()
|
|
608
|
-
});
|
|
609
426
|
this.logInfo("silentRefresh 完成", {
|
|
610
427
|
orderCount: orders.length,
|
|
611
428
|
duration: `${Date.now() - startTime}ms`
|
|
612
429
|
});
|
|
613
430
|
} else {
|
|
614
|
-
this.pushOrderFlowLog("silentRefresh.empty", { durationMs: Date.now() - startTime });
|
|
615
431
|
this.logInfo("silentRefresh: 服务器未返回数据");
|
|
616
432
|
}
|
|
617
433
|
return this.store.list;
|
|
@@ -643,12 +459,6 @@ var OrderModule = class extends import_BaseModule.BaseModule {
|
|
|
643
459
|
this.logInfo("overwriteExistingOrder-本地不存在该订单,跳过", { orderId });
|
|
644
460
|
return { overwritten: false };
|
|
645
461
|
}
|
|
646
|
-
this.pushOrderFlowLog("overwriteExistingOrder.start", {
|
|
647
|
-
orderId,
|
|
648
|
-
storageKey: this.getOrderStorageKey(fresh),
|
|
649
|
-
...this.getOrderStoreSnapshot(),
|
|
650
|
-
callerHint: "Server.handleUpdateLocalOrder 本地已存在时"
|
|
651
|
-
});
|
|
652
462
|
this.logInfo("overwriteExistingOrder-开始覆盖", {
|
|
653
463
|
orderId,
|
|
654
464
|
storeOrderCountBefore: this.store.list.length
|
|
@@ -661,10 +471,6 @@ var OrderModule = class extends import_BaseModule.BaseModule {
|
|
|
661
471
|
});
|
|
662
472
|
this.syncOrdersMap();
|
|
663
473
|
await this.updateOrderInSQLite(fresh, "overwriteExistingOrder");
|
|
664
|
-
this.pushOrderFlowLog("overwriteExistingOrder.done", {
|
|
665
|
-
orderId,
|
|
666
|
-
...this.getOrderStoreSnapshot()
|
|
667
|
-
});
|
|
668
474
|
this.logInfo("overwriteExistingOrder-结束", {
|
|
669
475
|
orderId,
|
|
670
476
|
storeOrderCountAfter: this.store.list.length
|
|
@@ -681,11 +487,6 @@ var OrderModule = class extends import_BaseModule.BaseModule {
|
|
|
681
487
|
this.logInfo("upsertOrdersFromRemote-订单列表为空", {});
|
|
682
488
|
return;
|
|
683
489
|
}
|
|
684
|
-
this.pushOrderFlowLog("upsertOrdersFromRemote.start", {
|
|
685
|
-
count: freshOrders.length,
|
|
686
|
-
orderIds: freshOrders.map((o) => o.order_id).filter((id) => id !== void 0 && id !== null),
|
|
687
|
-
callerHint: "Server.handleUpdateLocalOrder / handleOrderSalesDetail / handleUpdateLocalOrdersBatch"
|
|
688
|
-
});
|
|
689
490
|
this.logInfo("upsertOrdersFromRemote-开始", { count: freshOrders.length });
|
|
690
491
|
await this.mergeOrdersToStore(freshOrders, "upsertOrdersFromRemote");
|
|
691
492
|
}
|
|
@@ -804,15 +605,6 @@ var OrderModule = class extends import_BaseModule.BaseModule {
|
|
|
804
605
|
this.pendingSyncMessages.push(message);
|
|
805
606
|
const throttleMs = this.getOrderSyncThrottleMs();
|
|
806
607
|
const routeInfo = this.resolveSyncMessageRoute(message);
|
|
807
|
-
this.pushOrderFlowLog("orderSync.received", {
|
|
808
|
-
id: message.id ?? null,
|
|
809
|
-
order_id: message.order_id ?? null,
|
|
810
|
-
type: message.type ?? null,
|
|
811
|
-
pendingCount: this.pendingSyncMessages.length,
|
|
812
|
-
throttleMs,
|
|
813
|
-
...routeInfo,
|
|
814
|
-
duplicateRiskHint: routeInfo.route === "httpRefresh.batchIds" && routeInfo.hasNormalizedBody ? "有 body 但被 ids 优先,会走 HTTP 重拉" : routeInfo.route === "bodyUpsert" ? "body 直写,可能与 upsertOrdersFromRemote 重复" : null
|
|
815
|
-
});
|
|
816
608
|
this.logInfo("orderSync-收到消息并入队", {
|
|
817
609
|
id: message.id ?? null,
|
|
818
610
|
order_id: message.order_id ?? null,
|
|
@@ -838,9 +630,6 @@ var OrderModule = class extends import_BaseModule.BaseModule {
|
|
|
838
630
|
return;
|
|
839
631
|
if (this.isIdlePhase) {
|
|
840
632
|
this.isIdlePhase = false;
|
|
841
|
-
this.pushOrderFlowLog("orderSync.flushImmediate", {
|
|
842
|
-
pendingCount: this.pendingSyncMessages.length
|
|
843
|
-
});
|
|
844
633
|
this.logInfo("orderSync-首条消息立即触发处理");
|
|
845
634
|
void this.flushOrderSyncMessagesByThrottle();
|
|
846
635
|
return;
|
|
@@ -848,10 +637,6 @@ var OrderModule = class extends import_BaseModule.BaseModule {
|
|
|
848
637
|
if (this.syncTimer)
|
|
849
638
|
return;
|
|
850
639
|
const throttleMs = this.getOrderSyncThrottleMs();
|
|
851
|
-
this.pushOrderFlowLog("orderSync.flushScheduled", {
|
|
852
|
-
pendingCount: this.pendingSyncMessages.length,
|
|
853
|
-
throttleMs
|
|
854
|
-
});
|
|
855
640
|
this.syncTimer = setTimeout(() => {
|
|
856
641
|
this.syncTimer = void 0;
|
|
857
642
|
void this.flushOrderSyncMessagesByThrottle();
|
|
@@ -869,9 +654,6 @@ var OrderModule = class extends import_BaseModule.BaseModule {
|
|
|
869
654
|
return;
|
|
870
655
|
}
|
|
871
656
|
this.isProcessingSyncBatch = true;
|
|
872
|
-
this.pushOrderFlowLog("orderSync.flushStart", {
|
|
873
|
-
pendingCount: this.pendingSyncMessages.length
|
|
874
|
-
});
|
|
875
657
|
try {
|
|
876
658
|
await this.processOrderSyncMessages();
|
|
877
659
|
} finally {
|
|
@@ -900,10 +682,6 @@ var OrderModule = class extends import_BaseModule.BaseModule {
|
|
|
900
682
|
if (messages.length === 0)
|
|
901
683
|
return;
|
|
902
684
|
const batchProcessStartAt = Date.now();
|
|
903
|
-
this.pushOrderFlowLog("processOrderSyncMessages.start", {
|
|
904
|
-
messageCount: messages.length,
|
|
905
|
-
...this.getOrderStoreSnapshot()
|
|
906
|
-
});
|
|
907
685
|
const earliestReceivedAt = Math.min(
|
|
908
686
|
...messages.map((m) => m._pubsubReceivedAt ?? batchProcessStartAt)
|
|
909
687
|
);
|
|
@@ -917,10 +695,6 @@ var OrderModule = class extends import_BaseModule.BaseModule {
|
|
|
917
695
|
const refreshIds = [];
|
|
918
696
|
messages.forEach((msg, msgIndex) => {
|
|
919
697
|
const routeInfo = this.resolveSyncMessageRoute(msg);
|
|
920
|
-
this.pushOrderFlowLog("processOrderSyncMessages.messageRoute", {
|
|
921
|
-
msgIndex,
|
|
922
|
-
...routeInfo
|
|
923
|
-
});
|
|
924
698
|
const hasBatchIds = routeInfo.hasBatchIds;
|
|
925
699
|
const singleId = routeInfo.singleId;
|
|
926
700
|
const hasSingleId = routeInfo.hasSingleId;
|
|
@@ -938,13 +712,6 @@ var OrderModule = class extends import_BaseModule.BaseModule {
|
|
|
938
712
|
});
|
|
939
713
|
const uniqueRefreshIds = this.uniqueOrderIds(refreshIds);
|
|
940
714
|
const upsertList = [...upsertOrders.values()];
|
|
941
|
-
this.pushOrderFlowLog("processOrderSyncMessages.deduped", {
|
|
942
|
-
upsertCount: upsertList.length,
|
|
943
|
-
upsertOrderIds: upsertList.map((o) => o.order_id),
|
|
944
|
-
refreshIdCount: uniqueRefreshIds.length,
|
|
945
|
-
refreshIds: uniqueRefreshIds,
|
|
946
|
-
willCallMergeTimes: (upsertList.length > 0 ? 1 : 0) + (uniqueRefreshIds.length > 0 ? 1 : 0)
|
|
947
|
-
});
|
|
948
715
|
this.logInfo("processOrderSyncMessages-去重完成", {
|
|
949
716
|
upsertCount: upsertList.length,
|
|
950
717
|
refreshIdCount: uniqueRefreshIds.length
|
|
@@ -955,34 +722,19 @@ var OrderModule = class extends import_BaseModule.BaseModule {
|
|
|
955
722
|
if (uniqueRefreshIds.length > 0) {
|
|
956
723
|
const { toFetch, skipped } = this.filterRedundantHttpRefreshIds(uniqueRefreshIds);
|
|
957
724
|
if (skipped.length > 0) {
|
|
958
|
-
this.pushOrderFlowLog("processOrderSyncMessages.httpRefreshFiltered", {
|
|
959
|
-
requestedIds: uniqueRefreshIds,
|
|
960
|
-
skippedIds: skipped.map((item) => item.orderId),
|
|
961
|
-
skipped
|
|
962
|
-
});
|
|
963
725
|
}
|
|
964
726
|
if (toFetch.length > 0) {
|
|
965
727
|
const freshOrders = await this.fetchOrdersByHttp(toFetch);
|
|
966
728
|
if (freshOrders.length > 0) {
|
|
967
729
|
await this.mergeOrdersToStore(freshOrders, "pubsub.httpRefresh");
|
|
968
730
|
} else {
|
|
969
|
-
this.pushOrderFlowLog("processOrderSyncMessages.httpRefreshEmpty", {
|
|
970
|
-
refreshIds: toFetch
|
|
971
|
-
});
|
|
972
731
|
}
|
|
973
732
|
}
|
|
974
733
|
}
|
|
975
734
|
if (upsertList.length === 0 && uniqueRefreshIds.length === 0) {
|
|
976
|
-
this.pushOrderFlowLog("processOrderSyncMessages.noop", {});
|
|
977
735
|
return;
|
|
978
736
|
}
|
|
979
737
|
const batchProcessEndAt = Date.now();
|
|
980
|
-
this.pushOrderFlowLog("processOrderSyncMessages.done", {
|
|
981
|
-
upsertCount: upsertList.length,
|
|
982
|
-
refreshIdCount: uniqueRefreshIds.length,
|
|
983
|
-
batchProcessDurationMs: batchProcessEndAt - batchProcessStartAt,
|
|
984
|
-
...this.getOrderStoreSnapshot()
|
|
985
|
-
});
|
|
986
738
|
this.logInfo("processOrderSyncMessages-结束", {
|
|
987
739
|
upsertCount: upsertList.length,
|
|
988
740
|
refreshIdCount: uniqueRefreshIds.length,
|
|
@@ -1004,15 +756,9 @@ var OrderModule = class extends import_BaseModule.BaseModule {
|
|
|
1004
756
|
async fetchOrdersByHttp(ids) {
|
|
1005
757
|
if (!this.orderDataSource || ids.length === 0)
|
|
1006
758
|
return [];
|
|
1007
|
-
this.pushOrderFlowLog("fetchOrdersByHttp.start", { ids, count: ids.length });
|
|
1008
759
|
try {
|
|
1009
760
|
if (typeof this.orderDataSource.fetchOrdersByIds === "function") {
|
|
1010
761
|
const orderList2 = await this.orderDataSource.fetchOrdersByIds(ids);
|
|
1011
|
-
this.pushOrderFlowLog("fetchOrdersByHttp.done", {
|
|
1012
|
-
ids,
|
|
1013
|
-
count: (orderList2 == null ? void 0 : orderList2.length) || 0,
|
|
1014
|
-
via: "fetchOrdersByIds"
|
|
1015
|
-
});
|
|
1016
762
|
return orderList2 || [];
|
|
1017
763
|
}
|
|
1018
764
|
const orderList = await this.orderDataSource.run({
|
|
@@ -1020,14 +766,8 @@ var OrderModule = class extends import_BaseModule.BaseModule {
|
|
|
1020
766
|
query: { ids }
|
|
1021
767
|
}
|
|
1022
768
|
});
|
|
1023
|
-
this.pushOrderFlowLog("fetchOrdersByHttp.done", {
|
|
1024
|
-
ids,
|
|
1025
|
-
count: (orderList == null ? void 0 : orderList.length) || 0,
|
|
1026
|
-
via: "run.http"
|
|
1027
|
-
});
|
|
1028
769
|
return orderList || [];
|
|
1029
770
|
} catch {
|
|
1030
|
-
this.pushOrderFlowLog("fetchOrdersByHttp.failed", { ids });
|
|
1031
771
|
return [];
|
|
1032
772
|
}
|
|
1033
773
|
}
|
|
@@ -1035,12 +775,6 @@ var OrderModule = class extends import_BaseModule.BaseModule {
|
|
|
1035
775
|
* 将增量订单合并到 store
|
|
1036
776
|
*/
|
|
1037
777
|
async mergeOrdersToStore(freshOrders, source) {
|
|
1038
|
-
this.pushOrderFlowLog("mergeOrdersToStore.start", {
|
|
1039
|
-
source,
|
|
1040
|
-
freshOrderCount: freshOrders.length,
|
|
1041
|
-
freshOrderIds: freshOrders.map((o) => o.order_id).filter((id) => id !== void 0 && id !== null),
|
|
1042
|
-
...this.getOrderStoreSnapshot()
|
|
1043
|
-
});
|
|
1044
778
|
this.logInfo("mergeOrdersToStore-开始", {
|
|
1045
779
|
freshOrderCount: freshOrders.length,
|
|
1046
780
|
storeOrderCountBefore: this.store.list.length
|
|
@@ -1079,17 +813,6 @@ var OrderModule = class extends import_BaseModule.BaseModule {
|
|
|
1079
813
|
this.store.list = updatedList;
|
|
1080
814
|
this.syncOrdersMap();
|
|
1081
815
|
await this.patchOrdersInSQLite(patchedOrders, source, mergeActions);
|
|
1082
|
-
this.pushOrderFlowLog("mergeOrdersToStore.done", {
|
|
1083
|
-
source,
|
|
1084
|
-
uniqueFreshCount,
|
|
1085
|
-
insertCount,
|
|
1086
|
-
updateCount,
|
|
1087
|
-
patchedCount: patchedOrders.length,
|
|
1088
|
-
patchedOrderIds: patchedOrders.map((o) => o.order_id),
|
|
1089
|
-
patchedStorageKeys: patchedOrders.map((o) => this.getOrderStorageKey(o)),
|
|
1090
|
-
mergeActions,
|
|
1091
|
-
...this.getOrderStoreSnapshot()
|
|
1092
|
-
});
|
|
1093
816
|
this.logInfo("mergeOrdersToStore-结束", {
|
|
1094
817
|
uniqueFreshCount,
|
|
1095
818
|
storeOrderCountAfter: this.store.list.length
|
|
@@ -1248,19 +971,10 @@ var OrderModule = class extends import_BaseModule.BaseModule {
|
|
|
1248
971
|
*/
|
|
1249
972
|
async patchOrdersInSQLite(orders, source, mergeActions = {}) {
|
|
1250
973
|
if (!this.dbManager || orders.length === 0) {
|
|
1251
|
-
this.pushOrderFlowLog("patchOrdersInSQLite.skip", {
|
|
1252
|
-
source,
|
|
1253
|
-
hasDbManager: !!this.dbManager,
|
|
1254
|
-
inputCount: orders.length
|
|
1255
|
-
});
|
|
1256
974
|
return;
|
|
1257
975
|
}
|
|
1258
976
|
const ordersSnapshot = (0, import_lodash_es.cloneDeep)(orders).filter((order) => this.getOrderStorageKey(order));
|
|
1259
977
|
if (ordersSnapshot.length === 0) {
|
|
1260
|
-
this.pushOrderFlowLog("patchOrdersInSQLite.skipNoStorageKey", {
|
|
1261
|
-
source,
|
|
1262
|
-
inputCount: orders.length
|
|
1263
|
-
});
|
|
1264
978
|
return;
|
|
1265
979
|
}
|
|
1266
980
|
const { toWrite, skipped } = this.filterRedundantPatchOrders(
|
|
@@ -1268,35 +982,17 @@ var OrderModule = class extends import_BaseModule.BaseModule {
|
|
|
1268
982
|
ordersSnapshot,
|
|
1269
983
|
mergeActions
|
|
1270
984
|
);
|
|
1271
|
-
if (skipped.length > 0) {
|
|
1272
|
-
this.pushOrderFlowLog("patchOrdersInSQLite.dedupeSkipped", {
|
|
1273
|
-
source,
|
|
1274
|
-
skippedCount: skipped.length,
|
|
1275
|
-
skipped
|
|
1276
|
-
});
|
|
1277
|
-
}
|
|
1278
985
|
if (toWrite.length === 0) {
|
|
1279
|
-
this.pushOrderFlowLog("patchOrdersInSQLite.skipAllDeduped", {
|
|
1280
|
-
source,
|
|
1281
|
-
inputCount: ordersSnapshot.length,
|
|
1282
|
-
skipped
|
|
1283
|
-
});
|
|
1284
986
|
return;
|
|
1285
987
|
}
|
|
1286
988
|
try {
|
|
1287
989
|
await this.runInOrderSQLiteSaveQueue(async () => {
|
|
1288
990
|
let writeMethod = "none";
|
|
1289
|
-
this.
|
|
991
|
+
this.logInfo("patchOrdersInSQLite-开始", {
|
|
1290
992
|
source,
|
|
1291
993
|
count: toWrite.length,
|
|
1292
|
-
orderIds: toWrite.map((o) => o.order_id),
|
|
1293
|
-
storageKeys: toWrite.map((o) => this.getOrderStorageKey(o)),
|
|
1294
994
|
dedupedCount: skipped.length
|
|
1295
995
|
});
|
|
1296
|
-
this.logInfo("patchOrdersInSQLite-开始", {
|
|
1297
|
-
source,
|
|
1298
|
-
count: toWrite.length
|
|
1299
|
-
});
|
|
1300
996
|
if (typeof this.dbManager.bulkUpdate === "function") {
|
|
1301
997
|
writeMethod = "bulkUpdate";
|
|
1302
998
|
await this.dbManager.bulkUpdate(INDEXDB_STORE_NAME, toWrite);
|
|
@@ -1309,27 +1005,15 @@ var OrderModule = class extends import_BaseModule.BaseModule {
|
|
|
1309
1005
|
await this.dbManager.update(INDEXDB_STORE_NAME, order);
|
|
1310
1006
|
}
|
|
1311
1007
|
}
|
|
1312
|
-
const writeSeq = this.trackOrderWrite(source, writeMethod, toWrite, mergeActions);
|
|
1313
1008
|
this.recordRecentSqliteWrite(source, toWrite);
|
|
1314
|
-
this.pushOrderFlowLog("patchOrdersInSQLite.done", {
|
|
1315
|
-
source,
|
|
1316
|
-
writeSeq,
|
|
1317
|
-
count: toWrite.length,
|
|
1318
|
-
writeMethod,
|
|
1319
|
-
dedupedCount: skipped.length
|
|
1320
|
-
});
|
|
1321
1009
|
this.logInfo("patchOrdersInSQLite-完成", {
|
|
1322
1010
|
source,
|
|
1323
1011
|
count: toWrite.length,
|
|
1012
|
+
writeMethod,
|
|
1324
1013
|
dedupedCount: skipped.length
|
|
1325
1014
|
});
|
|
1326
1015
|
});
|
|
1327
1016
|
} catch (error) {
|
|
1328
|
-
this.pushOrderFlowLog("patchOrdersInSQLite.error", {
|
|
1329
|
-
source,
|
|
1330
|
-
error: error instanceof Error ? error.message : String(error),
|
|
1331
|
-
orderCount: ordersSnapshot.length
|
|
1332
|
-
});
|
|
1333
1017
|
this.logError("增量保存订单到 SQLite 失败", {
|
|
1334
1018
|
error: error instanceof Error ? error.message : String(error),
|
|
1335
1019
|
orderCount: ordersSnapshot.length
|
|
@@ -1344,22 +1028,12 @@ var OrderModule = class extends import_BaseModule.BaseModule {
|
|
|
1344
1028
|
*/
|
|
1345
1029
|
async updateOrderInSQLite(order, source) {
|
|
1346
1030
|
if (!this.dbManager || !this.getOrderStorageKey(order)) {
|
|
1347
|
-
this.pushOrderFlowLog("updateOrderInSQLite.skip", {
|
|
1348
|
-
source,
|
|
1349
|
-
hasDbManager: !!this.dbManager,
|
|
1350
|
-
orderId: (order == null ? void 0 : order.order_id) ?? null
|
|
1351
|
-
});
|
|
1352
1031
|
return;
|
|
1353
1032
|
}
|
|
1354
1033
|
const orderSnapshot = (0, import_lodash_es.cloneDeep)(order);
|
|
1355
1034
|
try {
|
|
1356
1035
|
await this.runInOrderSQLiteSaveQueue(async () => {
|
|
1357
1036
|
let writeMethod = "none";
|
|
1358
|
-
this.pushOrderFlowLog("updateOrderInSQLite.start", {
|
|
1359
|
-
source,
|
|
1360
|
-
orderId: orderSnapshot.order_id ?? null,
|
|
1361
|
-
storageKey: this.getOrderStorageKey(orderSnapshot)
|
|
1362
|
-
});
|
|
1363
1037
|
this.logInfo("updateOrderInSQLite-开始", {
|
|
1364
1038
|
source,
|
|
1365
1039
|
orderId: orderSnapshot.order_id ?? null,
|
|
@@ -1375,31 +1049,14 @@ var OrderModule = class extends import_BaseModule.BaseModule {
|
|
|
1375
1049
|
writeMethod = "bulkAdd";
|
|
1376
1050
|
await this.dbManager.bulkAdd(INDEXDB_STORE_NAME, [orderSnapshot]);
|
|
1377
1051
|
}
|
|
1378
|
-
const writeSeq = this.trackOrderWrite(
|
|
1379
|
-
source,
|
|
1380
|
-
writeMethod,
|
|
1381
|
-
[orderSnapshot],
|
|
1382
|
-
{},
|
|
1383
|
-
"singleUpdate"
|
|
1384
|
-
);
|
|
1385
1052
|
this.recordRecentSqliteWrite(source, [orderSnapshot]);
|
|
1386
|
-
this.
|
|
1053
|
+
this.logInfo("updateOrderInSQLite-完成", {
|
|
1387
1054
|
source,
|
|
1388
|
-
writeSeq,
|
|
1389
1055
|
orderId: orderSnapshot.order_id ?? null,
|
|
1390
|
-
storageKey: this.getOrderStorageKey(orderSnapshot),
|
|
1391
1056
|
writeMethod
|
|
1392
1057
|
});
|
|
1393
|
-
this.logInfo("updateOrderInSQLite-完成", {
|
|
1394
|
-
orderId: orderSnapshot.order_id ?? null
|
|
1395
|
-
});
|
|
1396
1058
|
});
|
|
1397
1059
|
} catch (error) {
|
|
1398
|
-
this.pushOrderFlowLog("updateOrderInSQLite.error", {
|
|
1399
|
-
source,
|
|
1400
|
-
error: error instanceof Error ? error.message : String(error),
|
|
1401
|
-
orderId: orderSnapshot.order_id ?? null
|
|
1402
|
-
});
|
|
1403
1060
|
this.logError("单条保存订单到 SQLite 失败", {
|
|
1404
1061
|
error: error instanceof Error ? error.message : String(error),
|
|
1405
1062
|
orderId: orderSnapshot.order_id ?? null
|
|
@@ -1414,57 +1071,30 @@ var OrderModule = class extends import_BaseModule.BaseModule {
|
|
|
1414
1071
|
*/
|
|
1415
1072
|
async replaceOrdersSnapshotInSQLite(orderList, source) {
|
|
1416
1073
|
if (!this.dbManager) {
|
|
1417
|
-
this.pushOrderFlowLog("replaceOrdersSnapshotInSQLite.skip", { source, count: orderList.length });
|
|
1418
1074
|
return;
|
|
1419
1075
|
}
|
|
1420
1076
|
const orderListSnapshot = (0, import_lodash_es.cloneDeep)(orderList);
|
|
1421
1077
|
try {
|
|
1422
1078
|
await this.runInOrderSQLiteSaveQueue(async () => {
|
|
1423
|
-
this.pushOrderFlowLog("replaceOrdersSnapshotInSQLite.start", {
|
|
1424
|
-
source,
|
|
1425
|
-
count: orderListSnapshot.length
|
|
1426
|
-
});
|
|
1427
1079
|
this.logInfo("replaceOrdersSnapshotInSQLite-开始", {
|
|
1428
1080
|
source,
|
|
1429
1081
|
count: orderListSnapshot.length
|
|
1430
1082
|
});
|
|
1431
1083
|
await this.dbManager.clear(INDEXDB_STORE_NAME);
|
|
1432
|
-
this.pushOrderFlowLog("replaceOrdersSnapshotInSQLite.cleared", {
|
|
1433
|
-
source,
|
|
1434
|
-
count: orderListSnapshot.length
|
|
1435
|
-
});
|
|
1436
1084
|
this.logInfo("replaceOrdersSnapshotInSQLite-clear完成", {
|
|
1437
1085
|
count: orderListSnapshot.length
|
|
1438
1086
|
});
|
|
1439
1087
|
if (orderListSnapshot.length === 0) {
|
|
1440
|
-
this.pushOrderFlowLog("replaceOrdersSnapshotInSQLite.doneEmpty", { source });
|
|
1441
1088
|
return;
|
|
1442
1089
|
}
|
|
1443
1090
|
await this.dbManager.bulkAdd(INDEXDB_STORE_NAME, orderListSnapshot);
|
|
1444
|
-
const writeSeq = this.trackOrderWrite(
|
|
1445
|
-
source,
|
|
1446
|
-
"clear+bulkAdd",
|
|
1447
|
-
orderListSnapshot,
|
|
1448
|
-
{},
|
|
1449
|
-
"snapshot"
|
|
1450
|
-
);
|
|
1451
1091
|
this.recordRecentSqliteWrite(source, orderListSnapshot);
|
|
1452
|
-
this.pushOrderFlowLog("replaceOrdersSnapshotInSQLite.done", {
|
|
1453
|
-
source,
|
|
1454
|
-
writeSeq,
|
|
1455
|
-
count: orderListSnapshot.length,
|
|
1456
|
-
writeMethod: "clear+bulkAdd"
|
|
1457
|
-
});
|
|
1458
1092
|
this.logInfo("replaceOrdersSnapshotInSQLite-bulkAdd完成", {
|
|
1093
|
+
source,
|
|
1459
1094
|
count: orderListSnapshot.length
|
|
1460
1095
|
});
|
|
1461
1096
|
});
|
|
1462
1097
|
} catch (error) {
|
|
1463
|
-
this.pushOrderFlowLog("replaceOrdersSnapshotInSQLite.error", {
|
|
1464
|
-
source,
|
|
1465
|
-
error: error instanceof Error ? error.message : String(error),
|
|
1466
|
-
orderList: orderListSnapshot.length
|
|
1467
|
-
});
|
|
1468
1098
|
this.logError("全量保存订单到 SQLite 失败", {
|
|
1469
1099
|
error: error instanceof Error ? error.message : String(error),
|
|
1470
1100
|
orderList: orderListSnapshot.length
|