@pisell/pisellos 2.2.190 → 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.
Files changed (67) hide show
  1. package/dist/model/strategy/adapter/promotion/index.js +0 -9
  2. package/dist/modules/BookingContext/utils/buildCacheItemFromOrderLine.js +2 -1
  3. package/dist/modules/BookingContext/utils/buildNormalProductCacheItemFromOrderLine.js +2 -7
  4. package/dist/modules/Cart/utils/cartProduct.js +13 -2
  5. package/dist/modules/Order/index.d.ts +1 -3
  6. package/dist/modules/Order/index.js +69 -95
  7. package/dist/modules/Order/types.d.ts +9 -6
  8. package/dist/modules/Order/utils.d.ts +9 -2
  9. package/dist/modules/Order/utils.js +36 -13
  10. package/dist/modules/SalesSummary/types.d.ts +3 -1
  11. package/dist/modules/SalesSummary/utils.js +7 -3
  12. package/dist/server/modules/order/index.d.ts +60 -7
  13. package/dist/server/modules/order/index.js +670 -178
  14. package/dist/server/modules/order/types.d.ts +0 -2
  15. package/dist/server/modules/products/index.d.ts +33 -6
  16. package/dist/server/modules/products/index.js +528 -231
  17. package/dist/solution/BaseSales/index.js +13 -7
  18. package/dist/solution/BaseSales/types.d.ts +8 -5
  19. package/dist/solution/BaseSales/types.js +2 -3
  20. package/dist/solution/BaseSales/utils/cartPromotion.js +7 -7
  21. package/dist/solution/BaseSales/utils/transformBaseProductToOrderProduct.d.ts +1 -0
  22. package/dist/solution/BaseSales/utils/transformBaseProductToOrderProduct.js +16 -13
  23. package/dist/solution/BaseSales/utils.d.ts +1 -1
  24. package/dist/solution/BaseSales/utils.js +22 -19
  25. package/dist/solution/BookingByStep/index.d.ts +1 -1
  26. package/dist/solution/BookingTicket/index.d.ts +1 -1
  27. package/dist/solution/ScanOrder/index.js +7 -5
  28. package/dist/solution/ScanOrder/types.d.ts +8 -5
  29. package/dist/solution/ScanOrder/types.js +2 -3
  30. package/dist/solution/ScanOrder/utils.d.ts +1 -1
  31. package/dist/solution/ScanOrder/utils.js +26 -23
  32. package/dist/solution/VenueBooking/index.js +5 -3
  33. package/dist/solution/VenueBooking/sales-section-4-annotated.json +0 -2
  34. package/lib/model/strategy/adapter/promotion/index.js +0 -49
  35. package/lib/modules/BookingContext/utils/buildCacheItemFromOrderLine.js +2 -1
  36. package/lib/modules/BookingContext/utils/buildNormalProductCacheItemFromOrderLine.js +2 -1
  37. package/lib/modules/Cart/utils/cartProduct.js +9 -2
  38. package/lib/modules/Order/index.d.ts +1 -3
  39. package/lib/modules/Order/index.js +45 -58
  40. package/lib/modules/Order/types.d.ts +9 -6
  41. package/lib/modules/Order/utils.d.ts +9 -2
  42. package/lib/modules/Order/utils.js +41 -10
  43. package/lib/modules/SalesSummary/types.d.ts +3 -1
  44. package/lib/modules/SalesSummary/utils.js +5 -3
  45. package/lib/server/modules/order/index.d.ts +60 -7
  46. package/lib/server/modules/order/index.js +350 -40
  47. package/lib/server/modules/order/types.d.ts +0 -2
  48. package/lib/server/modules/products/index.d.ts +33 -6
  49. package/lib/server/modules/products/index.js +159 -30
  50. package/lib/solution/BaseSales/index.js +14 -5
  51. package/lib/solution/BaseSales/types.d.ts +8 -5
  52. package/lib/solution/BaseSales/utils/cartPromotion.js +6 -6
  53. package/lib/solution/BaseSales/utils/transformBaseProductToOrderProduct.d.ts +1 -0
  54. package/lib/solution/BaseSales/utils/transformBaseProductToOrderProduct.js +21 -14
  55. package/lib/solution/BaseSales/utils.d.ts +1 -1
  56. package/lib/solution/BaseSales/utils.js +11 -7
  57. package/lib/solution/BookingByStep/index.d.ts +1 -1
  58. package/lib/solution/BookingTicket/index.d.ts +1 -1
  59. package/lib/solution/ScanOrder/index.js +4 -4
  60. package/lib/solution/ScanOrder/types.d.ts +8 -5
  61. package/lib/solution/ScanOrder/utils.d.ts +1 -1
  62. package/lib/solution/ScanOrder/utils.js +12 -8
  63. package/lib/solution/VenueBooking/index.js +2 -2
  64. package/lib/solution/VenueBooking/sales-section-4-annotated.json +0 -2
  65. package/package.json +1 -1
  66. package/dist/solution/Checkout/appointmentDemo.json +0 -1
  67. package/lib/solution/Checkout/appointmentDemo.json +0 -1
@@ -38,6 +38,12 @@ var INDEXDB_STORE_NAME = 'orders';
38
38
  var ORDER_LAST_FULL_FETCH_AT_STORAGE_KEY = 'server_order_last_full_fetch_at';
39
39
  var ORDER_SYNC_THROTTLE_MS_STORAGE_KEY = 'order_sync_throttle_ms';
40
40
  var DEFAULT_ORDER_SYNC_THROTTLE_MS = 2000;
41
+ /** pubsub 回声写入去重窗口:业务 API 刚写入后,跳过同单 SQLite patch */
42
+ var ORDER_SQLITE_DEDUPE_MS = 15000;
43
+ /** silentRefresh 最小间隔,避免 preload 后立即全量重拉 */
44
+ var ORDER_SILENT_REFRESH_MIN_INTERVAL_MS = 30000;
45
+ /** 视为「业务侧刚写入」的来源,pubsub 回声可跳过 SQLite */
46
+ var ORDER_BUSINESS_WRITE_SOURCES = new Set(['upsertOrdersFromRemote', 'overwriteExistingOrder']);
41
47
 
42
48
  /**
43
49
  * Order 模块 - 基础框架
@@ -64,6 +70,10 @@ export var OrderModule = /*#__PURE__*/function (_BaseModule) {
64
70
  // LoggerManager 实例
65
71
  _defineProperty(_assertThisInitialized(_this), "storage", void 0);
66
72
  _defineProperty(_assertThisInitialized(_this), "orderSQLiteSaveQueue", Promise.resolve());
73
+ /** 按 storageKey 记录最近一次 SQLite 写入来源与时间,用于去重 */
74
+ _defineProperty(_assertThisInitialized(_this), "recentSqliteWriteByStorageKey", new Map());
75
+ /** 最近一次 SSE 全量拉取完成时间 */
76
+ _defineProperty(_assertThisInitialized(_this), "lastServerFullFetchAtMs", 0);
67
77
  return _this;
68
78
  }
69
79
  _createClass(OrderModule, [{
@@ -185,6 +195,216 @@ export var OrderModule = /*#__PURE__*/function (_BaseModule) {
185
195
  // 日志记录失败不影响主流程
186
196
  }
187
197
  }
198
+
199
+ /**
200
+ * 记录订单最近一次 SQLite 写入,供 pubsub 回声去重使用。
201
+ *
202
+ * @example
203
+ * this.recordRecentSqliteWrite('upsertOrdersFromRemote', orders)
204
+ */
205
+ }, {
206
+ key: "recordRecentSqliteWrite",
207
+ value: function recordRecentSqliteWrite(source, orders) {
208
+ var now = Date.now();
209
+ var _iterator = _createForOfIteratorHelper(orders),
210
+ _step;
211
+ try {
212
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
213
+ var order = _step.value;
214
+ var storageKey = this.getOrderStorageKey(order);
215
+ if (!storageKey) continue;
216
+ this.recentSqliteWriteByStorageKey.set(storageKey, {
217
+ source: source,
218
+ ts: now
219
+ });
220
+ }
221
+ } catch (err) {
222
+ _iterator.e(err);
223
+ } finally {
224
+ _iterator.f();
225
+ }
226
+ }
227
+
228
+ /**
229
+ * 过滤 pubsub 触发的冗余 SQLite patch(业务 API 刚写入过的 update 回声)。
230
+ *
231
+ * @example
232
+ * const { toWrite, skipped } = this.filterRedundantPatchOrders('pubsub.bodyUpsert', orders, mergeActions)
233
+ */
234
+ }, {
235
+ key: "filterRedundantPatchOrders",
236
+ value: function filterRedundantPatchOrders(source, orders, mergeActions) {
237
+ var eligibleSkipSources = new Set(['pubsub.bodyUpsert', 'pubsub.httpRefresh']);
238
+ if (!eligibleSkipSources.has(source)) {
239
+ return {
240
+ toWrite: orders,
241
+ skipped: []
242
+ };
243
+ }
244
+ var now = Date.now();
245
+ var toWrite = [];
246
+ var skipped = [];
247
+ var _iterator2 = _createForOfIteratorHelper(orders),
248
+ _step2;
249
+ try {
250
+ for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
251
+ var order = _step2.value;
252
+ var storageKey = this.getOrderStorageKey(order);
253
+ if (!storageKey) {
254
+ toWrite.push(order);
255
+ continue;
256
+ }
257
+ var orderKey = order.order_id !== undefined && order.order_id !== null ? this.getIdKey(order.order_id) : '';
258
+ var mergeAction = orderKey ? mergeActions[orderKey] : undefined;
259
+
260
+ // 新增单必须落库,不参与去重
261
+ if (mergeAction === 'insert') {
262
+ toWrite.push(order);
263
+ continue;
264
+ }
265
+ var recent = this.recentSqliteWriteByStorageKey.get(storageKey);
266
+ if (recent && ORDER_BUSINESS_WRITE_SOURCES.has(recent.source) && now - recent.ts < ORDER_SQLITE_DEDUPE_MS) {
267
+ var _order$order_id;
268
+ skipped.push({
269
+ orderId: (_order$order_id = order.order_id) !== null && _order$order_id !== void 0 ? _order$order_id : null,
270
+ storageKey: storageKey,
271
+ reason: 'skip_pubsub_echo_after_business_write',
272
+ lastSource: recent.source,
273
+ elapsedMs: now - recent.ts
274
+ });
275
+ continue;
276
+ }
277
+ toWrite.push(order);
278
+ }
279
+ } catch (err) {
280
+ _iterator2.e(err);
281
+ } finally {
282
+ _iterator2.f();
283
+ }
284
+ return {
285
+ toWrite: toWrite,
286
+ skipped: skipped
287
+ };
288
+ }
289
+
290
+ /**
291
+ * 过滤 pubsub HTTP 增量拉取 ids:本地刚由业务 API 写入的单可跳过 HTTP。
292
+ *
293
+ * @example
294
+ * const ids = this.filterRedundantHttpRefreshIds([99331, 99332])
295
+ */
296
+ }, {
297
+ key: "filterRedundantHttpRefreshIds",
298
+ value: function filterRedundantHttpRefreshIds(ids) {
299
+ var now = Date.now();
300
+ var toFetch = [];
301
+ var skipped = [];
302
+ var _iterator3 = _createForOfIteratorHelper(ids),
303
+ _step3;
304
+ try {
305
+ for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
306
+ var id = _step3.value;
307
+ var existing = this.getOrderByOrderId(id);
308
+ if (!existing) {
309
+ toFetch.push(id);
310
+ continue;
311
+ }
312
+ var storageKey = this.getOrderStorageKey(existing);
313
+ var recent = storageKey ? this.recentSqliteWriteByStorageKey.get(storageKey) : undefined;
314
+ if (recent && recent.source === 'upsertOrdersFromRemote' && now - recent.ts < ORDER_SQLITE_DEDUPE_MS) {
315
+ skipped.push({
316
+ orderId: id,
317
+ lastSource: recent.source,
318
+ elapsedMs: now - recent.ts
319
+ });
320
+ continue;
321
+ }
322
+ toFetch.push(id);
323
+ }
324
+ } catch (err) {
325
+ _iterator3.e(err);
326
+ } finally {
327
+ _iterator3.f();
328
+ }
329
+ return {
330
+ toFetch: toFetch,
331
+ skipped: skipped
332
+ };
333
+ }
334
+
335
+ /**
336
+ * 解析 pubsub 消息会走哪条同步分支,便于对照为何触发 HTTP 重拉或 body 直写。
337
+ *
338
+ * @example
339
+ * this.resolveSyncMessageRoute(msg)
340
+ * // => { route: 'httpRefresh.batchIds', ... }
341
+ */
342
+ }, {
343
+ key: "resolveSyncMessageRoute",
344
+ value: function resolveSyncMessageRoute(msg) {
345
+ var _msg$id, _msg$relation_order_i, _normalizedBody$order5;
346
+ var hasBatchIds = Array.isArray(msg.ids) && msg.ids.length > 0;
347
+ var singleIdRaw = (_msg$id = msg.id) !== null && _msg$id !== void 0 ? _msg$id : msg.order_id;
348
+ var hasSingleId = !hasBatchIds && singleIdRaw !== undefined && singleIdRaw !== null;
349
+ var normalizedBody = this.normalizeOrderSyncPayload(msg.body || msg.data);
350
+ if (hasBatchIds) {
351
+ var _normalizedBody$order;
352
+ return {
353
+ route: 'httpRefresh.batchIds',
354
+ hasBatchIds: true,
355
+ hasSingleId: false,
356
+ hasNormalizedBody: !!normalizedBody,
357
+ singleId: null,
358
+ batchIds: msg.ids || [],
359
+ bodyOrderId: (_normalizedBody$order = normalizedBody === null || normalizedBody === void 0 ? void 0 : normalizedBody.order_id) !== null && _normalizedBody$order !== void 0 ? _normalizedBody$order : null
360
+ };
361
+ }
362
+ if (hasSingleId && normalizedBody) {
363
+ var _normalizedBody$order2;
364
+ return {
365
+ route: 'bodyUpsert',
366
+ hasBatchIds: false,
367
+ hasSingleId: true,
368
+ hasNormalizedBody: true,
369
+ singleId: singleIdRaw,
370
+ batchIds: [],
371
+ bodyOrderId: (_normalizedBody$order2 = normalizedBody.order_id) !== null && _normalizedBody$order2 !== void 0 ? _normalizedBody$order2 : null
372
+ };
373
+ }
374
+ if (hasSingleId) {
375
+ var _normalizedBody$order3;
376
+ return {
377
+ route: 'httpRefresh.singleId',
378
+ hasBatchIds: false,
379
+ hasSingleId: true,
380
+ hasNormalizedBody: !!normalizedBody,
381
+ singleId: singleIdRaw,
382
+ batchIds: [],
383
+ bodyOrderId: (_normalizedBody$order3 = normalizedBody === null || normalizedBody === void 0 ? void 0 : normalizedBody.order_id) !== null && _normalizedBody$order3 !== void 0 ? _normalizedBody$order3 : null
384
+ };
385
+ }
386
+ if ((_msg$relation_order_i = msg.relation_order_ids) !== null && _msg$relation_order_i !== void 0 && _msg$relation_order_i.length) {
387
+ var _normalizedBody$order4;
388
+ return {
389
+ route: 'httpRefresh.relationIds',
390
+ hasBatchIds: false,
391
+ hasSingleId: false,
392
+ hasNormalizedBody: !!normalizedBody,
393
+ singleId: null,
394
+ batchIds: msg.relation_order_ids,
395
+ bodyOrderId: (_normalizedBody$order4 = normalizedBody === null || normalizedBody === void 0 ? void 0 : normalizedBody.order_id) !== null && _normalizedBody$order4 !== void 0 ? _normalizedBody$order4 : null
396
+ };
397
+ }
398
+ return {
399
+ route: 'noop',
400
+ hasBatchIds: false,
401
+ hasSingleId: false,
402
+ hasNormalizedBody: !!normalizedBody,
403
+ singleId: null,
404
+ batchIds: [],
405
+ bodyOrderId: (_normalizedBody$order5 = normalizedBody === null || normalizedBody === void 0 ? void 0 : normalizedBody.order_id) !== null && _normalizedBody$order5 !== void 0 ? _normalizedBody$order5 : null
406
+ };
407
+ }
188
408
  }, {
189
409
  key: "getOrderSyncThrottleMs",
190
410
  value: function getOrderSyncThrottleMs() {
@@ -204,7 +424,6 @@ export var OrderModule = /*#__PURE__*/function (_BaseModule) {
204
424
  return _regeneratorRuntime().wrap(function _callee3$(_context3) {
205
425
  while (1) switch (_context3.prev = _context3.next) {
206
426
  case 0:
207
- // const cachedOrders = await this.loadOrdersFromSQLite()
208
427
  // const hasPulledInCurrentWindow = this.hasPulledOrdersToday()
209
428
  // // 仅当缓存属于当前统计窗口时才直接复用,避免跨天/跨营业日窗口继续使用旧数据
210
429
  // if (cachedOrders.length > 0 && hasPulledInCurrentWindow) {
@@ -341,7 +560,7 @@ export var OrderModule = /*#__PURE__*/function (_BaseModule) {
341
560
  query: ((_this$store = this.store) === null || _this$store === void 0 ? void 0 : _this$store.createdAtQuery) || null
342
561
  });
343
562
  if (!this.orderDataSource) {
344
- _context5.next = 16;
563
+ _context5.next = 17;
345
564
  break;
346
565
  }
347
566
  _context5.prev = 3;
@@ -358,29 +577,30 @@ export var OrderModule = /*#__PURE__*/function (_BaseModule) {
358
577
  count: orderList.length
359
578
  });
360
579
  this.markOrderPulledAtNow();
361
- _context5.next = 16;
580
+ this.lastServerFullFetchAtMs = Date.now();
581
+ _context5.next = 17;
362
582
  break;
363
- case 12:
364
- _context5.prev = 12;
583
+ case 13:
584
+ _context5.prev = 13;
365
585
  _context5.t0 = _context5["catch"](3);
366
586
  orderList = [];
367
587
  this.logInfo('loadOrdersByServer-SSE拉取失败,回退为空数组');
368
- case 16:
369
- _context5.next = 18;
370
- return this.saveOrdersToSQLite(orderList);
371
- case 18:
588
+ case 17:
589
+ _context5.next = 19;
590
+ return this.replaceOrdersSnapshotInSQLite(orderList, 'loadOrdersByServer');
591
+ case 19:
372
592
  this.logInfo('loadOrdersByServer-结束', {
373
593
  count: orderList.length
374
594
  });
375
- _context5.next = 21;
595
+ _context5.next = 22;
376
596
  return this.core.effects.emit(OrderHooks.onOrdersLoaded, orderList);
377
- case 21:
378
- return _context5.abrupt("return", orderList);
379
597
  case 22:
598
+ return _context5.abrupt("return", orderList);
599
+ case 23:
380
600
  case "end":
381
601
  return _context5.stop();
382
602
  }
383
- }, _callee5, this, [[3, 12]]);
603
+ }, _callee5, this, [[3, 13]]);
384
604
  }));
385
605
  function loadOrdersByServer() {
386
606
  return _loadOrdersByServer.apply(this, arguments);
@@ -396,51 +616,58 @@ export var OrderModule = /*#__PURE__*/function (_BaseModule) {
396
616
  key: "silentRefresh",
397
617
  value: (function () {
398
618
  var _silentRefresh = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee6() {
399
- var startTime, orders, errorMessage;
619
+ var startTime, elapsedSinceFullFetch, orders, errorMessage;
400
620
  return _regeneratorRuntime().wrap(function _callee6$(_context6) {
401
621
  while (1) switch (_context6.prev = _context6.next) {
402
622
  case 0:
403
623
  startTime = Date.now();
624
+ elapsedSinceFullFetch = Date.now() - this.lastServerFullFetchAtMs;
625
+ if (!(this.lastServerFullFetchAtMs > 0 && elapsedSinceFullFetch < ORDER_SILENT_REFRESH_MIN_INTERVAL_MS)) {
626
+ _context6.next = 4;
627
+ break;
628
+ }
629
+ return _context6.abrupt("return", this.store.list);
630
+ case 4:
404
631
  this.logInfo('silentRefresh 开始');
405
- _context6.prev = 2;
406
- _context6.next = 5;
632
+ _context6.prev = 5;
633
+ _context6.next = 8;
407
634
  return this.loadOrdersByServer();
408
- case 5:
635
+ case 8:
409
636
  orders = _context6.sent;
410
637
  if (!(orders.length > 0)) {
411
- _context6.next = 15;
638
+ _context6.next = 18;
412
639
  break;
413
640
  }
414
641
  this.store.list = cloneDeep(orders);
415
642
  this.syncOrdersMap();
416
643
  this.core.effects.emit(OrderHooks.onOrdersChanged, this.store.list);
417
- _context6.next = 12;
644
+ _context6.next = 15;
418
645
  return this.core.effects.emit(OrderHooks.onOrdersSyncCompleted, null);
419
- case 12:
646
+ case 15:
420
647
  this.logInfo('silentRefresh 完成', {
421
648
  orderCount: orders.length,
422
649
  duration: "".concat(Date.now() - startTime, "ms")
423
650
  });
424
- _context6.next = 16;
651
+ _context6.next = 19;
425
652
  break;
426
- case 15:
653
+ case 18:
427
654
  this.logInfo('silentRefresh: 服务器未返回数据');
428
- case 16:
429
- return _context6.abrupt("return", this.store.list);
430
655
  case 19:
431
- _context6.prev = 19;
432
- _context6.t0 = _context6["catch"](2);
656
+ return _context6.abrupt("return", this.store.list);
657
+ case 22:
658
+ _context6.prev = 22;
659
+ _context6.t0 = _context6["catch"](5);
433
660
  errorMessage = _context6.t0 instanceof Error ? _context6.t0.message : String(_context6.t0);
434
661
  this.logError('silentRefresh 失败', {
435
662
  duration: "".concat(Date.now() - startTime, "ms"),
436
663
  error: errorMessage
437
664
  });
438
665
  return _context6.abrupt("return", this.store.list);
439
- case 24:
666
+ case 27:
440
667
  case "end":
441
668
  return _context6.stop();
442
669
  }
443
- }, _callee6, this, [[2, 19]]);
670
+ }, _callee6, this, [[5, 22]]);
444
671
  }));
445
672
  function silentRefresh() {
446
673
  return _silentRefresh.apply(this, arguments);
@@ -505,7 +732,7 @@ export var OrderModule = /*#__PURE__*/function (_BaseModule) {
505
732
  });
506
733
  this.syncOrdersMap();
507
734
  _context7.next = 13;
508
- return this.saveOrdersToSQLite(this.store.list);
735
+ return this.updateOrderInSQLite(fresh, 'overwriteExistingOrder');
509
736
  case 13:
510
737
  this.logInfo('overwriteExistingOrder-结束', {
511
738
  orderId: orderId,
@@ -551,7 +778,7 @@ export var OrderModule = /*#__PURE__*/function (_BaseModule) {
551
778
  count: freshOrders.length
552
779
  });
553
780
  _context8.next = 6;
554
- return this.mergeOrdersToStore(freshOrders);
781
+ return this.mergeOrdersToStore(freshOrders, 'upsertOrdersFromRemote');
555
782
  case 6:
556
783
  case "end":
557
784
  return _context8.stop();
@@ -636,20 +863,20 @@ export var OrderModule = /*#__PURE__*/function (_BaseModule) {
636
863
  var _this6 = this;
637
864
  this.store.map.clear();
638
865
  this.resourceIdIndex.clear();
639
- var _iterator = _createForOfIteratorHelper(this.store.list),
640
- _step;
866
+ var _iterator4 = _createForOfIteratorHelper(this.store.list),
867
+ _step4;
641
868
  try {
642
869
  var _loop = function _loop() {
643
- var order = _step.value;
870
+ var order = _step4.value;
644
871
  var id = order.order_id;
645
872
  if (id === undefined || id === null) return 1; // continue
646
873
  _this6.store.map.set(_this6.getIdKey(id), order);
647
874
  var resourceIds = _this6.extractResourceIds(order);
648
- var _iterator2 = _createForOfIteratorHelper(resourceIds),
649
- _step2;
875
+ var _iterator5 = _createForOfIteratorHelper(resourceIds),
876
+ _step5;
650
877
  try {
651
- for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
652
- var resourceId = _step2.value;
878
+ for (_iterator5.s(); !(_step5 = _iterator5.n()).done;) {
879
+ var resourceId = _step5.value;
653
880
  var key = String(resourceId);
654
881
  var existing = _this6.resourceIdIndex.get(key) || [];
655
882
  if (existing.some(function (eid) {
@@ -659,18 +886,18 @@ export var OrderModule = /*#__PURE__*/function (_BaseModule) {
659
886
  _this6.resourceIdIndex.set(key, existing);
660
887
  }
661
888
  } catch (err) {
662
- _iterator2.e(err);
889
+ _iterator5.e(err);
663
890
  } finally {
664
- _iterator2.f();
891
+ _iterator5.f();
665
892
  }
666
893
  };
667
- for (_iterator.s(); !(_step = _iterator.n()).done;) {
894
+ for (_iterator4.s(); !(_step4 = _iterator4.n()).done;) {
668
895
  if (_loop()) continue;
669
896
  }
670
897
  } catch (err) {
671
- _iterator.e(err);
898
+ _iterator4.e(err);
672
899
  } finally {
673
- _iterator.f();
900
+ _iterator4.f();
674
901
  }
675
902
  }
676
903
  }, {
@@ -731,13 +958,14 @@ export var OrderModule = /*#__PURE__*/function (_BaseModule) {
731
958
  pubsub: {
732
959
  callback: function callback(res) {
733
960
  var _message$id, _message$order_id, _message$type;
734
- console.log('orderDataSource');
961
+ console.log('orderDataSource', res);
735
962
  var pubsubReceivedAt = Date.now();
736
963
  var message = _this7.normalizeOrderSyncMessage(res);
737
964
  if (!message) return;
738
965
  message._pubsubReceivedAt = pubsubReceivedAt;
739
966
  _this7.pendingSyncMessages.push(message);
740
967
  var throttleMs = _this7.getOrderSyncThrottleMs();
968
+ var routeInfo = _this7.resolveSyncMessageRoute(message);
741
969
  _this7.logInfo('orderSync-收到消息并入队', {
742
970
  id: (_message$id = message.id) !== null && _message$id !== void 0 ? _message$id : null,
743
971
  order_id: (_message$order_id = message.order_id) !== null && _message$order_id !== void 0 ? _message$order_id : null,
@@ -843,7 +1071,8 @@ export var OrderModule = /*#__PURE__*/function (_BaseModule) {
843
1071
  key: "processOrderSyncMessages",
844
1072
  value: (function () {
845
1073
  var _processOrderSyncMessages = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee11() {
846
- var messages, batchProcessStartAt, earliestReceivedAt, upsertOrders, refreshIds, _iterator3, _step3, _msg$id, _msg$relation_order_i, msg, hasBatchIds, singleId, hasSingleId, rawBody, normalizedBody, uniqueRefreshIds, upsertList, freshOrders, batchProcessEndAt;
1074
+ var _this9 = this;
1075
+ var messages, batchProcessStartAt, earliestReceivedAt, upsertOrders, refreshIds, uniqueRefreshIds, upsertList, _this$filterRedundant, toFetch, skipped, freshOrders, batchProcessEndAt;
847
1076
  return _regeneratorRuntime().wrap(function _callee11$(_context11) {
848
1077
  while (1) switch (_context11.prev = _context11.next) {
849
1078
  case 0:
@@ -868,43 +1097,18 @@ export var OrderModule = /*#__PURE__*/function (_BaseModule) {
868
1097
  });
869
1098
  upsertOrders = new Map();
870
1099
  refreshIds = [];
871
- _iterator3 = _createForOfIteratorHelper(messages);
872
- _context11.prev = 10;
873
- _iterator3.s();
874
- case 12:
875
- if ((_step3 = _iterator3.n()).done) {
876
- _context11.next = 25;
877
- break;
878
- }
879
- msg = _step3.value;
880
- hasBatchIds = Array.isArray(msg.ids) && msg.ids.length > 0;
881
- singleId = (_msg$id = msg.id) !== null && _msg$id !== void 0 ? _msg$id : msg.order_id;
882
- hasSingleId = !hasBatchIds && singleId !== undefined && singleId !== null;
883
- rawBody = msg.body || msg.data;
884
- normalizedBody = this.normalizeOrderSyncPayload(rawBody);
885
- if (!(hasSingleId && normalizedBody)) {
886
- _context11.next = 22;
887
- break;
888
- }
889
- upsertOrders.set(this.getIdKey(normalizedBody.order_id), normalizedBody);
890
- return _context11.abrupt("continue", 23);
891
- case 22:
892
- if (hasBatchIds) refreshIds.push.apply(refreshIds, _toConsumableArray(msg.ids));else if (hasSingleId) refreshIds.push(singleId);else if ((_msg$relation_order_i = msg.relation_order_ids) !== null && _msg$relation_order_i !== void 0 && _msg$relation_order_i.length) refreshIds.push.apply(refreshIds, _toConsumableArray(msg.relation_order_ids));
893
- case 23:
894
- _context11.next = 12;
895
- break;
896
- case 25:
897
- _context11.next = 30;
898
- break;
899
- case 27:
900
- _context11.prev = 27;
901
- _context11.t0 = _context11["catch"](10);
902
- _iterator3.e(_context11.t0);
903
- case 30:
904
- _context11.prev = 30;
905
- _iterator3.f();
906
- return _context11.finish(30);
907
- case 33:
1100
+ messages.forEach(function (msg, msgIndex) {
1101
+ var routeInfo = _this9.resolveSyncMessageRoute(msg);
1102
+ var hasBatchIds = routeInfo.hasBatchIds;
1103
+ var singleId = routeInfo.singleId;
1104
+ var hasSingleId = routeInfo.hasSingleId;
1105
+ var normalizedBody = _this9.normalizeOrderSyncPayload(msg.body || msg.data);
1106
+ if (routeInfo.route === 'bodyUpsert' && normalizedBody) {
1107
+ upsertOrders.set(_this9.getIdKey(normalizedBody.order_id), normalizedBody);
1108
+ return;
1109
+ }
1110
+ if (hasBatchIds) refreshIds.push.apply(refreshIds, _toConsumableArray(routeInfo.batchIds));else if (routeInfo.route === 'httpRefresh.singleId' && singleId !== null) refreshIds.push(singleId);else if (routeInfo.route === 'httpRefresh.relationIds') refreshIds.push.apply(refreshIds, _toConsumableArray(routeInfo.batchIds));
1111
+ });
908
1112
  uniqueRefreshIds = this.uniqueOrderIds(refreshIds);
909
1113
  upsertList = _toConsumableArray(upsertOrders.values());
910
1114
  this.logInfo('processOrderSyncMessages-去重完成', {
@@ -912,33 +1116,42 @@ export var OrderModule = /*#__PURE__*/function (_BaseModule) {
912
1116
  refreshIdCount: uniqueRefreshIds.length
913
1117
  });
914
1118
  if (!(upsertList.length > 0)) {
915
- _context11.next = 39;
1119
+ _context11.next = 16;
916
1120
  break;
917
1121
  }
918
- _context11.next = 39;
919
- return this.mergeOrdersToStore(upsertList);
920
- case 39:
1122
+ _context11.next = 16;
1123
+ return this.mergeOrdersToStore(upsertList, 'pubsub.bodyUpsert');
1124
+ case 16:
921
1125
  if (!(uniqueRefreshIds.length > 0)) {
922
- _context11.next = 46;
1126
+ _context11.next = 28;
923
1127
  break;
924
1128
  }
925
- _context11.next = 42;
926
- return this.fetchOrdersByHttp(uniqueRefreshIds);
927
- case 42:
1129
+ _this$filterRedundant = this.filterRedundantHttpRefreshIds(uniqueRefreshIds), toFetch = _this$filterRedundant.toFetch, skipped = _this$filterRedundant.skipped;
1130
+ if (skipped.length > 0) {}
1131
+ if (!(toFetch.length > 0)) {
1132
+ _context11.next = 28;
1133
+ break;
1134
+ }
1135
+ _context11.next = 22;
1136
+ return this.fetchOrdersByHttp(toFetch);
1137
+ case 22:
928
1138
  freshOrders = _context11.sent;
929
1139
  if (!(freshOrders.length > 0)) {
930
- _context11.next = 46;
1140
+ _context11.next = 28;
931
1141
  break;
932
1142
  }
933
- _context11.next = 46;
934
- return this.mergeOrdersToStore(freshOrders);
935
- case 46:
1143
+ _context11.next = 26;
1144
+ return this.mergeOrdersToStore(freshOrders, 'pubsub.httpRefresh');
1145
+ case 26:
1146
+ _context11.next = 28;
1147
+ break;
1148
+ case 28:
936
1149
  if (!(upsertList.length === 0 && uniqueRefreshIds.length === 0)) {
937
- _context11.next = 48;
1150
+ _context11.next = 30;
938
1151
  break;
939
1152
  }
940
1153
  return _context11.abrupt("return");
941
- case 48:
1154
+ case 30:
942
1155
  batchProcessEndAt = Date.now();
943
1156
  this.logInfo('processOrderSyncMessages-结束', {
944
1157
  upsertCount: upsertList.length,
@@ -946,13 +1159,13 @@ export var OrderModule = /*#__PURE__*/function (_BaseModule) {
946
1159
  batchProcessDurationMs: batchProcessEndAt - batchProcessStartAt,
947
1160
  totalFromPubsubToProcessedMs: batchProcessEndAt - earliestReceivedAt
948
1161
  });
949
- _context11.next = 52;
1162
+ _context11.next = 34;
950
1163
  return this.core.effects.emit(OrderHooks.onOrdersSyncCompleted, null);
951
- case 52:
1164
+ case 34:
952
1165
  case "end":
953
1166
  return _context11.stop();
954
1167
  }
955
- }, _callee11, this, [[10, 27, 30, 33]]);
1168
+ }, _callee11, this);
956
1169
  }));
957
1170
  function processOrderSyncMessages() {
958
1171
  return _processOrderSyncMessages.apply(this, arguments);
@@ -1028,9 +1241,9 @@ export var OrderModule = /*#__PURE__*/function (_BaseModule) {
1028
1241
  }, {
1029
1242
  key: "mergeOrdersToStore",
1030
1243
  value: (function () {
1031
- var _mergeOrdersToStore = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee13(freshOrders) {
1032
- var _this9 = this;
1033
- var freshMap, _iterator4, _step4, order, id, uniqueFreshCount, updatedList, _iterator5, _step5, _order;
1244
+ var _mergeOrdersToStore = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee13(freshOrders, source) {
1245
+ var _this10 = this;
1246
+ var freshMap, _iterator6, _step6, order, id, uniqueFreshCount, patchedOrders, mergeActions, updatedList, _iterator7, _step7, _order, _id, insertCount, updateCount;
1034
1247
  return _regeneratorRuntime().wrap(function _callee13$(_context13) {
1035
1248
  while (1) switch (_context13.prev = _context13.next) {
1036
1249
  case 0:
@@ -1039,15 +1252,15 @@ export var OrderModule = /*#__PURE__*/function (_BaseModule) {
1039
1252
  storeOrderCountBefore: this.store.list.length
1040
1253
  });
1041
1254
  freshMap = new Map();
1042
- _iterator4 = _createForOfIteratorHelper(freshOrders);
1255
+ _iterator6 = _createForOfIteratorHelper(freshOrders);
1043
1256
  _context13.prev = 3;
1044
- _iterator4.s();
1257
+ _iterator6.s();
1045
1258
  case 5:
1046
- if ((_step4 = _iterator4.n()).done) {
1259
+ if ((_step6 = _iterator6.n()).done) {
1047
1260
  _context13.next = 13;
1048
1261
  break;
1049
1262
  }
1050
- order = _step4.value;
1263
+ order = _step6.value;
1051
1264
  id = order === null || order === void 0 ? void 0 : order.order_id;
1052
1265
  if (!(id === undefined || id === null)) {
1053
1266
  _context13.next = 10;
@@ -1065,50 +1278,63 @@ export var OrderModule = /*#__PURE__*/function (_BaseModule) {
1065
1278
  case 15:
1066
1279
  _context13.prev = 15;
1067
1280
  _context13.t0 = _context13["catch"](3);
1068
- _iterator4.e(_context13.t0);
1281
+ _iterator6.e(_context13.t0);
1069
1282
  case 18:
1070
1283
  _context13.prev = 18;
1071
- _iterator4.f();
1284
+ _iterator6.f();
1072
1285
  return _context13.finish(18);
1073
1286
  case 21:
1074
1287
  uniqueFreshCount = freshMap.size;
1288
+ patchedOrders = _toConsumableArray(freshMap.values());
1289
+ mergeActions = {};
1075
1290
  updatedList = this.store.list.map(function (order) {
1076
1291
  var id = order.order_id;
1077
1292
  if (id === undefined || id === null) return order;
1078
- var key = _this9.getIdKey(id);
1293
+ var key = _this10.getIdKey(id);
1079
1294
  if (!freshMap.has(key)) return order;
1080
1295
  var fresh = freshMap.get(key);
1081
1296
  freshMap.delete(key);
1297
+ mergeActions[key] = 'update';
1082
1298
  return fresh;
1083
1299
  });
1084
- _iterator5 = _createForOfIteratorHelper(freshMap.values());
1300
+ _iterator7 = _createForOfIteratorHelper(freshMap.values());
1085
1301
  try {
1086
- for (_iterator5.s(); !(_step5 = _iterator5.n()).done;) {
1087
- _order = _step5.value;
1302
+ for (_iterator7.s(); !(_step7 = _iterator7.n()).done;) {
1303
+ _order = _step7.value;
1304
+ _id = _order.order_id;
1305
+ if (_id !== undefined && _id !== null) {
1306
+ mergeActions[this.getIdKey(_id)] = 'insert';
1307
+ }
1088
1308
  updatedList.push(_order);
1089
1309
  }
1090
1310
  } catch (err) {
1091
- _iterator5.e(err);
1311
+ _iterator7.e(err);
1092
1312
  } finally {
1093
- _iterator5.f();
1313
+ _iterator7.f();
1094
1314
  }
1315
+ insertCount = Object.values(mergeActions).filter(function (v) {
1316
+ return v === 'insert';
1317
+ }).length;
1318
+ updateCount = Object.values(mergeActions).filter(function (v) {
1319
+ return v === 'update';
1320
+ }).length;
1095
1321
  this.store.list = updatedList;
1096
1322
  this.syncOrdersMap();
1097
- _context13.next = 29;
1098
- return this.saveOrdersToSQLite(this.store.list);
1099
- case 29:
1323
+ _context13.next = 33;
1324
+ return this.patchOrdersInSQLite(patchedOrders, source, mergeActions);
1325
+ case 33:
1100
1326
  this.logInfo('mergeOrdersToStore-结束', {
1101
1327
  uniqueFreshCount: uniqueFreshCount,
1102
1328
  storeOrderCountAfter: this.store.list.length
1103
1329
  });
1104
1330
  this.core.effects.emit(OrderHooks.onOrdersChanged, this.store.list);
1105
- case 31:
1331
+ case 35:
1106
1332
  case "end":
1107
1333
  return _context13.stop();
1108
1334
  }
1109
1335
  }, _callee13, this, [[3, 15, 18, 21]]);
1110
1336
  }));
1111
- function mergeOrdersToStore(_x7) {
1337
+ function mergeOrdersToStore(_x7, _x8) {
1112
1338
  return _mergeOrdersToStore.apply(this, arguments);
1113
1339
  }
1114
1340
  return mergeOrdersToStore;
@@ -1145,17 +1371,17 @@ export var OrderModule = /*#__PURE__*/function (_BaseModule) {
1145
1371
  key: "uniqueOrderIds",
1146
1372
  value: function uniqueOrderIds(ids) {
1147
1373
  var idMap = new Map();
1148
- var _iterator6 = _createForOfIteratorHelper(ids),
1149
- _step6;
1374
+ var _iterator8 = _createForOfIteratorHelper(ids),
1375
+ _step8;
1150
1376
  try {
1151
- for (_iterator6.s(); !(_step6 = _iterator6.n()).done;) {
1152
- var id = _step6.value;
1377
+ for (_iterator8.s(); !(_step8 = _iterator8.n()).done;) {
1378
+ var id = _step8.value;
1153
1379
  idMap.set(this.getIdKey(id), id);
1154
1380
  }
1155
1381
  } catch (err) {
1156
- _iterator6.e(err);
1382
+ _iterator8.e(err);
1157
1383
  } finally {
1158
- _iterator6.f();
1384
+ _iterator8.f();
1159
1385
  }
1160
1386
  return _toConsumableArray(idMap.values());
1161
1387
  }
@@ -1165,6 +1391,27 @@ export var OrderModule = /*#__PURE__*/function (_BaseModule) {
1165
1391
  return String(id);
1166
1392
  }
1167
1393
 
1394
+ /**
1395
+ * 解析订单 SQLite 主键,规则与宿主 OrderDataSource.getOrderStorageKey 一致。
1396
+ *
1397
+ * @example
1398
+ * const key = this.getOrderStorageKey({ order_id: 1001, external_sale_number: 'S-1001' })
1399
+ * // => 'S-1001'
1400
+ */
1401
+ }, {
1402
+ key: "getOrderStorageKey",
1403
+ value: function getOrderStorageKey(order) {
1404
+ var externalSaleNumber = order === null || order === void 0 ? void 0 : order.external_sale_number;
1405
+ if (externalSaleNumber !== undefined && externalSaleNumber !== null && externalSaleNumber !== '') {
1406
+ return String(externalSaleNumber);
1407
+ }
1408
+ var orderId = order === null || order === void 0 ? void 0 : order.order_id;
1409
+ if (orderId !== undefined && orderId !== null && orderId !== '') {
1410
+ return String(orderId);
1411
+ }
1412
+ return '';
1413
+ }
1414
+
1168
1415
  /**
1169
1416
  * 当前订单拉取窗口:
1170
1417
  * - 优先使用 createdAtQuery.sales_time_between(已按营业日边界初始化)
@@ -1278,12 +1525,11 @@ export var OrderModule = /*#__PURE__*/function (_BaseModule) {
1278
1525
  return loadOrdersFromSQLite;
1279
1526
  }()
1280
1527
  /**
1281
- * 串行执行订单 SQLite 保存任务,保证 clear + bulkAdd 组合不会被其它订单保存插入。
1528
+ * 串行执行订单 SQLite 写入任务,避免快照替换与增量 upsert 并发交错。
1282
1529
  *
1283
1530
  * @example
1284
1531
  * await this.runInOrderSQLiteSaveQueue(async () => {
1285
- * await this.dbManager.clear(INDEXDB_STORE_NAME)
1286
- * await this.dbManager.bulkAdd(INDEXDB_STORE_NAME, orders)
1532
+ * await this.dbManager.bulkUpdate(INDEXDB_STORE_NAME, orders)
1287
1533
  * })
1288
1534
  */
1289
1535
  }, {
@@ -1307,88 +1553,334 @@ export var OrderModule = /*#__PURE__*/function (_BaseModule) {
1307
1553
  }
1308
1554
  }, _callee15, this);
1309
1555
  }));
1310
- function runInOrderSQLiteSaveQueue(_x8) {
1556
+ function runInOrderSQLiteSaveQueue(_x9) {
1311
1557
  return _runInOrderSQLiteSaveQueue.apply(this, arguments);
1312
1558
  }
1313
1559
  return runInOrderSQLiteSaveQueue;
1314
1560
  }()
1315
1561
  /**
1316
- * 保存订单列表到 SQLite,内部会先清空 orders 表再批量写入当前快照。
1562
+ * 批量增量 upsert 订单到 SQLite,不清空表;用于 pubsub / HTTP 增量合并。
1317
1563
  *
1318
1564
  * @example
1319
- * await this.saveOrdersToSQLite(this.store.list)
1565
+ * await this.patchOrdersInSQLite(freshOrders)
1320
1566
  */
1321
1567
  )
1322
1568
  }, {
1323
- key: "saveOrdersToSQLite",
1569
+ key: "patchOrdersInSQLite",
1324
1570
  value: (function () {
1325
- var _saveOrdersToSQLite = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee17(orderList) {
1326
- var _this10 = this;
1327
- var orderListSnapshot;
1571
+ var _patchOrdersInSQLite = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee17(orders, source) {
1572
+ var _this11 = this;
1573
+ var mergeActions,
1574
+ ordersSnapshot,
1575
+ _this$filterRedundant2,
1576
+ toWrite,
1577
+ skipped,
1578
+ _args17 = arguments;
1328
1579
  return _regeneratorRuntime().wrap(function _callee17$(_context17) {
1329
1580
  while (1) switch (_context17.prev = _context17.next) {
1330
1581
  case 0:
1331
- if (this.dbManager) {
1332
- _context17.next = 2;
1582
+ mergeActions = _args17.length > 2 && _args17[2] !== undefined ? _args17[2] : {};
1583
+ if (!(!this.dbManager || orders.length === 0)) {
1584
+ _context17.next = 3;
1333
1585
  break;
1334
1586
  }
1335
1587
  return _context17.abrupt("return");
1336
- case 2:
1337
- orderListSnapshot = cloneDeep(orderList);
1338
- _context17.prev = 3;
1339
- _context17.next = 6;
1588
+ case 3:
1589
+ ordersSnapshot = cloneDeep(orders).filter(function (order) {
1590
+ return _this11.getOrderStorageKey(order);
1591
+ });
1592
+ if (!(ordersSnapshot.length === 0)) {
1593
+ _context17.next = 6;
1594
+ break;
1595
+ }
1596
+ return _context17.abrupt("return");
1597
+ case 6:
1598
+ _this$filterRedundant2 = this.filterRedundantPatchOrders(source, ordersSnapshot, mergeActions), toWrite = _this$filterRedundant2.toWrite, skipped = _this$filterRedundant2.skipped;
1599
+ if (!(toWrite.length === 0)) {
1600
+ _context17.next = 9;
1601
+ break;
1602
+ }
1603
+ return _context17.abrupt("return");
1604
+ case 9:
1605
+ _context17.prev = 9;
1606
+ _context17.next = 12;
1340
1607
  return this.runInOrderSQLiteSaveQueue( /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee16() {
1608
+ var writeMethod, _iterator9, _step9, order;
1341
1609
  return _regeneratorRuntime().wrap(function _callee16$(_context16) {
1342
1610
  while (1) switch (_context16.prev = _context16.next) {
1343
1611
  case 0:
1344
- _this10.logInfo('saveOrdersToSQLite-开始', {
1612
+ writeMethod = 'none';
1613
+ _this11.logInfo('patchOrdersInSQLite-开始', {
1614
+ source: source,
1615
+ count: toWrite.length,
1616
+ dedupedCount: skipped.length
1617
+ });
1618
+ if (!(typeof _this11.dbManager.bulkUpdate === 'function')) {
1619
+ _context16.next = 8;
1620
+ break;
1621
+ }
1622
+ writeMethod = 'bulkUpdate';
1623
+ _context16.next = 6;
1624
+ return _this11.dbManager.bulkUpdate(INDEXDB_STORE_NAME, toWrite);
1625
+ case 6:
1626
+ _context16.next = 33;
1627
+ break;
1628
+ case 8:
1629
+ if (!(typeof _this11.dbManager.bulkAdd === 'function')) {
1630
+ _context16.next = 14;
1631
+ break;
1632
+ }
1633
+ writeMethod = 'bulkAdd';
1634
+ _context16.next = 12;
1635
+ return _this11.dbManager.bulkAdd(INDEXDB_STORE_NAME, toWrite);
1636
+ case 12:
1637
+ _context16.next = 33;
1638
+ break;
1639
+ case 14:
1640
+ if (!(typeof _this11.dbManager.update === 'function')) {
1641
+ _context16.next = 33;
1642
+ break;
1643
+ }
1644
+ writeMethod = 'update';
1645
+ _iterator9 = _createForOfIteratorHelper(toWrite);
1646
+ _context16.prev = 17;
1647
+ _iterator9.s();
1648
+ case 19:
1649
+ if ((_step9 = _iterator9.n()).done) {
1650
+ _context16.next = 25;
1651
+ break;
1652
+ }
1653
+ order = _step9.value;
1654
+ _context16.next = 23;
1655
+ return _this11.dbManager.update(INDEXDB_STORE_NAME, order);
1656
+ case 23:
1657
+ _context16.next = 19;
1658
+ break;
1659
+ case 25:
1660
+ _context16.next = 30;
1661
+ break;
1662
+ case 27:
1663
+ _context16.prev = 27;
1664
+ _context16.t0 = _context16["catch"](17);
1665
+ _iterator9.e(_context16.t0);
1666
+ case 30:
1667
+ _context16.prev = 30;
1668
+ _iterator9.f();
1669
+ return _context16.finish(30);
1670
+ case 33:
1671
+ _this11.recordRecentSqliteWrite(source, toWrite);
1672
+ _this11.logInfo('patchOrdersInSQLite-完成', {
1673
+ source: source,
1674
+ count: toWrite.length,
1675
+ writeMethod: writeMethod,
1676
+ dedupedCount: skipped.length
1677
+ });
1678
+ case 35:
1679
+ case "end":
1680
+ return _context16.stop();
1681
+ }
1682
+ }, _callee16, null, [[17, 27, 30, 33]]);
1683
+ })));
1684
+ case 12:
1685
+ _context17.next = 17;
1686
+ break;
1687
+ case 14:
1688
+ _context17.prev = 14;
1689
+ _context17.t0 = _context17["catch"](9);
1690
+ this.logError('增量保存订单到 SQLite 失败', {
1691
+ error: _context17.t0 instanceof Error ? _context17.t0.message : String(_context17.t0),
1692
+ orderCount: ordersSnapshot.length
1693
+ });
1694
+ case 17:
1695
+ case "end":
1696
+ return _context17.stop();
1697
+ }
1698
+ }, _callee17, this, [[9, 14]]);
1699
+ }));
1700
+ function patchOrdersInSQLite(_x10, _x11) {
1701
+ return _patchOrdersInSQLite.apply(this, arguments);
1702
+ }
1703
+ return patchOrdersInSQLite;
1704
+ }()
1705
+ /**
1706
+ * 单条 upsert 订单到 SQLite;用于 overwriteExistingOrder 等单条覆盖场景。
1707
+ *
1708
+ * @example
1709
+ * await this.updateOrderInSQLite(freshOrder)
1710
+ */
1711
+ )
1712
+ }, {
1713
+ key: "updateOrderInSQLite",
1714
+ value: (function () {
1715
+ var _updateOrderInSQLite = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee19(order, source) {
1716
+ var _this12 = this;
1717
+ var orderSnapshot, _orderSnapshot$order_3;
1718
+ return _regeneratorRuntime().wrap(function _callee19$(_context19) {
1719
+ while (1) switch (_context19.prev = _context19.next) {
1720
+ case 0:
1721
+ if (!(!this.dbManager || !this.getOrderStorageKey(order))) {
1722
+ _context19.next = 2;
1723
+ break;
1724
+ }
1725
+ return _context19.abrupt("return");
1726
+ case 2:
1727
+ orderSnapshot = cloneDeep(order);
1728
+ _context19.prev = 3;
1729
+ _context19.next = 6;
1730
+ return this.runInOrderSQLiteSaveQueue( /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee18() {
1731
+ var _orderSnapshot$order_, _orderSnapshot$order_2;
1732
+ var writeMethod;
1733
+ return _regeneratorRuntime().wrap(function _callee18$(_context18) {
1734
+ while (1) switch (_context18.prev = _context18.next) {
1735
+ case 0:
1736
+ writeMethod = 'none';
1737
+ _this12.logInfo('updateOrderInSQLite-开始', {
1738
+ source: source,
1739
+ orderId: (_orderSnapshot$order_ = orderSnapshot.order_id) !== null && _orderSnapshot$order_ !== void 0 ? _orderSnapshot$order_ : null,
1740
+ storageKey: _this12.getOrderStorageKey(orderSnapshot)
1741
+ });
1742
+ if (!(typeof _this12.dbManager.update === 'function')) {
1743
+ _context18.next = 8;
1744
+ break;
1745
+ }
1746
+ writeMethod = 'update';
1747
+ _context18.next = 6;
1748
+ return _this12.dbManager.update(INDEXDB_STORE_NAME, orderSnapshot);
1749
+ case 6:
1750
+ _context18.next = 18;
1751
+ break;
1752
+ case 8:
1753
+ if (!(typeof _this12.dbManager.bulkUpdate === 'function')) {
1754
+ _context18.next = 14;
1755
+ break;
1756
+ }
1757
+ writeMethod = 'bulkUpdate';
1758
+ _context18.next = 12;
1759
+ return _this12.dbManager.bulkUpdate(INDEXDB_STORE_NAME, [orderSnapshot]);
1760
+ case 12:
1761
+ _context18.next = 18;
1762
+ break;
1763
+ case 14:
1764
+ if (!(typeof _this12.dbManager.bulkAdd === 'function')) {
1765
+ _context18.next = 18;
1766
+ break;
1767
+ }
1768
+ writeMethod = 'bulkAdd';
1769
+ _context18.next = 18;
1770
+ return _this12.dbManager.bulkAdd(INDEXDB_STORE_NAME, [orderSnapshot]);
1771
+ case 18:
1772
+ _this12.recordRecentSqliteWrite(source, [orderSnapshot]);
1773
+ _this12.logInfo('updateOrderInSQLite-完成', {
1774
+ source: source,
1775
+ orderId: (_orderSnapshot$order_2 = orderSnapshot.order_id) !== null && _orderSnapshot$order_2 !== void 0 ? _orderSnapshot$order_2 : null,
1776
+ writeMethod: writeMethod
1777
+ });
1778
+ case 20:
1779
+ case "end":
1780
+ return _context18.stop();
1781
+ }
1782
+ }, _callee18);
1783
+ })));
1784
+ case 6:
1785
+ _context19.next = 11;
1786
+ break;
1787
+ case 8:
1788
+ _context19.prev = 8;
1789
+ _context19.t0 = _context19["catch"](3);
1790
+ this.logError('单条保存订单到 SQLite 失败', {
1791
+ error: _context19.t0 instanceof Error ? _context19.t0.message : String(_context19.t0),
1792
+ orderId: (_orderSnapshot$order_3 = orderSnapshot.order_id) !== null && _orderSnapshot$order_3 !== void 0 ? _orderSnapshot$order_3 : null
1793
+ });
1794
+ case 11:
1795
+ case "end":
1796
+ return _context19.stop();
1797
+ }
1798
+ }, _callee19, this, [[3, 8]]);
1799
+ }));
1800
+ function updateOrderInSQLite(_x12, _x13) {
1801
+ return _updateOrderInSQLite.apply(this, arguments);
1802
+ }
1803
+ return updateOrderInSQLite;
1804
+ }()
1805
+ /**
1806
+ * 全量快照替换 orders 表(clear + bulkAdd),用于 SSE 全量拉取与营业日窗口裁剪。
1807
+ *
1808
+ * @example
1809
+ * await this.replaceOrdersSnapshotInSQLite(orderList)
1810
+ */
1811
+ )
1812
+ }, {
1813
+ key: "replaceOrdersSnapshotInSQLite",
1814
+ value: (function () {
1815
+ var _replaceOrdersSnapshotInSQLite = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee21(orderList, source) {
1816
+ var _this13 = this;
1817
+ var orderListSnapshot;
1818
+ return _regeneratorRuntime().wrap(function _callee21$(_context21) {
1819
+ while (1) switch (_context21.prev = _context21.next) {
1820
+ case 0:
1821
+ if (this.dbManager) {
1822
+ _context21.next = 2;
1823
+ break;
1824
+ }
1825
+ return _context21.abrupt("return");
1826
+ case 2:
1827
+ orderListSnapshot = cloneDeep(orderList);
1828
+ _context21.prev = 3;
1829
+ _context21.next = 6;
1830
+ return this.runInOrderSQLiteSaveQueue( /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee20() {
1831
+ return _regeneratorRuntime().wrap(function _callee20$(_context20) {
1832
+ while (1) switch (_context20.prev = _context20.next) {
1833
+ case 0:
1834
+ _this13.logInfo('replaceOrdersSnapshotInSQLite-开始', {
1835
+ source: source,
1345
1836
  count: orderListSnapshot.length
1346
1837
  });
1347
- _context16.next = 3;
1348
- return _this10.dbManager.clear(INDEXDB_STORE_NAME);
1838
+ _context20.next = 3;
1839
+ return _this13.dbManager.clear(INDEXDB_STORE_NAME);
1349
1840
  case 3:
1350
- _this10.logInfo('saveOrdersToSQLite-clear完成', {
1841
+ _this13.logInfo('replaceOrdersSnapshotInSQLite-clear完成', {
1351
1842
  count: orderListSnapshot.length
1352
1843
  });
1353
1844
  if (!(orderListSnapshot.length === 0)) {
1354
- _context16.next = 6;
1845
+ _context20.next = 6;
1355
1846
  break;
1356
1847
  }
1357
- return _context16.abrupt("return");
1848
+ return _context20.abrupt("return");
1358
1849
  case 6:
1359
- _context16.next = 8;
1360
- return _this10.dbManager.bulkAdd(INDEXDB_STORE_NAME, orderListSnapshot);
1850
+ _context20.next = 8;
1851
+ return _this13.dbManager.bulkAdd(INDEXDB_STORE_NAME, orderListSnapshot);
1361
1852
  case 8:
1362
- _this10.logInfo('saveOrdersToSQLite-bulkAdd完成', {
1853
+ _this13.recordRecentSqliteWrite(source, orderListSnapshot);
1854
+ _this13.logInfo('replaceOrdersSnapshotInSQLite-bulkAdd完成', {
1855
+ source: source,
1363
1856
  count: orderListSnapshot.length
1364
1857
  });
1365
- case 9:
1858
+ case 10:
1366
1859
  case "end":
1367
- return _context16.stop();
1860
+ return _context20.stop();
1368
1861
  }
1369
- }, _callee16);
1862
+ }, _callee20);
1370
1863
  })));
1371
1864
  case 6:
1372
- _context17.next = 11;
1865
+ _context21.next = 11;
1373
1866
  break;
1374
1867
  case 8:
1375
- _context17.prev = 8;
1376
- _context17.t0 = _context17["catch"](3);
1377
- // SQLite 异常,避免影响主流程
1378
- this.logError('保存订单到 SQLite 失败', {
1379
- error: _context17.t0 instanceof Error ? _context17.t0.message : String(_context17.t0),
1868
+ _context21.prev = 8;
1869
+ _context21.t0 = _context21["catch"](3);
1870
+ this.logError('全量保存订单到 SQLite 失败', {
1871
+ error: _context21.t0 instanceof Error ? _context21.t0.message : String(_context21.t0),
1380
1872
  orderList: orderListSnapshot.length
1381
1873
  });
1382
1874
  case 11:
1383
1875
  case "end":
1384
- return _context17.stop();
1876
+ return _context21.stop();
1385
1877
  }
1386
- }, _callee17, this, [[3, 8]]);
1878
+ }, _callee21, this, [[3, 8]]);
1387
1879
  }));
1388
- function saveOrdersToSQLite(_x9) {
1389
- return _saveOrdersToSQLite.apply(this, arguments);
1880
+ function replaceOrdersSnapshotInSQLite(_x14, _x15) {
1881
+ return _replaceOrdersSnapshotInSQLite.apply(this, arguments);
1390
1882
  }
1391
- return saveOrdersToSQLite;
1883
+ return replaceOrdersSnapshotInSQLite;
1392
1884
  }()
1393
1885
  /**
1394
1886
  * 清空订单模块的内存数据和本地 SQLite 订单表。
@@ -1400,39 +1892,39 @@ export var OrderModule = /*#__PURE__*/function (_BaseModule) {
1400
1892
  }, {
1401
1893
  key: "clear",
1402
1894
  value: (function () {
1403
- var _clear = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee19() {
1404
- var _this11 = this;
1405
- return _regeneratorRuntime().wrap(function _callee19$(_context19) {
1406
- while (1) switch (_context19.prev = _context19.next) {
1895
+ var _clear = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee23() {
1896
+ var _this14 = this;
1897
+ return _regeneratorRuntime().wrap(function _callee23$(_context23) {
1898
+ while (1) switch (_context23.prev = _context23.next) {
1407
1899
  case 0:
1408
1900
  this.store.list = [];
1409
1901
  this.store.map = new Map();
1410
1902
  this.resourceIdIndex.clear();
1411
1903
  if (!this.dbManager) {
1412
- _context19.next = 6;
1904
+ _context23.next = 6;
1413
1905
  break;
1414
1906
  }
1415
- _context19.next = 6;
1416
- return this.runInOrderSQLiteSaveQueue( /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee18() {
1417
- return _regeneratorRuntime().wrap(function _callee18$(_context18) {
1418
- while (1) switch (_context18.prev = _context18.next) {
1907
+ _context23.next = 6;
1908
+ return this.runInOrderSQLiteSaveQueue( /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee22() {
1909
+ return _regeneratorRuntime().wrap(function _callee22$(_context22) {
1910
+ while (1) switch (_context22.prev = _context22.next) {
1419
1911
  case 0:
1420
- _context18.next = 2;
1421
- return _this11.dbManager.clear(INDEXDB_STORE_NAME);
1912
+ _context22.next = 2;
1913
+ return _this14.dbManager.clear(INDEXDB_STORE_NAME);
1422
1914
  case 2:
1423
1915
  case "end":
1424
- return _context18.stop();
1916
+ return _context22.stop();
1425
1917
  }
1426
- }, _callee18);
1918
+ }, _callee22);
1427
1919
  })));
1428
1920
  case 6:
1429
1921
  this.setStorageItem(ORDER_LAST_FULL_FETCH_AT_STORAGE_KEY, '');
1430
1922
  this.core.effects.emit(OrderHooks.onOrdersChanged, this.store.list);
1431
1923
  case 8:
1432
1924
  case "end":
1433
- return _context19.stop();
1925
+ return _context23.stop();
1434
1926
  }
1435
- }, _callee19, this);
1927
+ }, _callee23, this);
1436
1928
  }));
1437
1929
  function clear() {
1438
1930
  return _clear.apply(this, arguments);