@pisell/pisellos 2.3.28 → 2.3.30

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 (57) hide show
  1. package/dist/modules/Order/index.d.ts +32 -8
  2. package/dist/modules/Order/index.js +883 -521
  3. package/dist/modules/Order/payment-utils.d.ts +23 -0
  4. package/dist/modules/Order/payment-utils.js +203 -0
  5. package/dist/modules/Order/types.d.ts +33 -1
  6. package/dist/modules/Order/utils/orderCollectionIdentity.d.ts +53 -0
  7. package/dist/modules/Order/utils/orderCollectionIdentity.js +405 -0
  8. package/dist/modules/Order/utils.d.ts +2 -1
  9. package/dist/modules/Order/utils.js +6 -12
  10. package/dist/modules/Payment/index.d.ts +2 -0
  11. package/dist/modules/Payment/index.js +78 -49
  12. package/dist/modules/Payment/types.d.ts +26 -24
  13. package/dist/modules/Payment/types.js +2 -0
  14. package/dist/modules/Product/index.d.ts +1 -1
  15. package/dist/server/index.d.ts +14 -0
  16. package/dist/server/index.js +1826 -1000
  17. package/dist/server/modules/order/index.d.ts +47 -4
  18. package/dist/server/modules/order/index.js +1415 -711
  19. package/dist/server/modules/order/types.d.ts +11 -3
  20. package/dist/server/modules/order/types.js +1 -1
  21. package/dist/solution/BaseSales/index.d.ts +40 -4
  22. package/dist/solution/BaseSales/index.js +1268 -749
  23. package/dist/solution/BaseSales/utils/parseSalesResponse.d.ts +9 -4
  24. package/dist/solution/BaseSales/utils/parseSalesResponse.js +6 -8
  25. package/dist/solution/BookingByStep/index.d.ts +1 -1
  26. package/dist/solution/BookingTicket/index.d.ts +13 -1
  27. package/dist/solution/BookingTicket/index.js +2 -1
  28. package/dist/utils/payment-number.d.ts +9 -0
  29. package/dist/utils/payment-number.js +69 -0
  30. package/lib/modules/Order/index.d.ts +32 -8
  31. package/lib/modules/Order/index.js +270 -64
  32. package/lib/modules/Order/payment-utils.d.ts +23 -0
  33. package/lib/modules/Order/payment-utils.js +193 -0
  34. package/lib/modules/Order/types.d.ts +33 -1
  35. package/lib/modules/Order/utils/orderCollectionIdentity.d.ts +53 -0
  36. package/lib/modules/Order/utils/orderCollectionIdentity.js +415 -0
  37. package/lib/modules/Order/utils.d.ts +2 -1
  38. package/lib/modules/Order/utils.js +6 -14
  39. package/lib/modules/Payment/index.d.ts +2 -0
  40. package/lib/modules/Payment/index.js +33 -12
  41. package/lib/modules/Payment/types.d.ts +26 -24
  42. package/lib/modules/Product/index.d.ts +1 -1
  43. package/lib/server/index.d.ts +14 -0
  44. package/lib/server/index.js +604 -8
  45. package/lib/server/modules/order/index.d.ts +47 -4
  46. package/lib/server/modules/order/index.js +578 -116
  47. package/lib/server/modules/order/types.d.ts +11 -3
  48. package/lib/solution/BaseSales/index.d.ts +40 -4
  49. package/lib/solution/BaseSales/index.js +363 -37
  50. package/lib/solution/BaseSales/utils/parseSalesResponse.d.ts +9 -4
  51. package/lib/solution/BaseSales/utils/parseSalesResponse.js +4 -5
  52. package/lib/solution/BookingByStep/index.d.ts +1 -1
  53. package/lib/solution/BookingTicket/index.d.ts +13 -1
  54. package/lib/solution/BookingTicket/index.js +2 -1
  55. package/lib/utils/payment-number.d.ts +9 -0
  56. package/lib/utils/payment-number.js +64 -0
  57. package/package.json +1 -1
@@ -33,7 +33,9 @@ function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e
33
33
  import { cloneDeep } from 'lodash-es';
34
34
  import dayjs from 'dayjs';
35
35
  import { BaseModule } from "../../../modules/BaseModule";
36
+ import { applyOrderCollectionUidRemaps, getOrderCollectionPersistentId, getOrderCollectionReferenceUid, getOrderCollectionUid, isSameOrderCollectionItem, mergeOrderCollectionItems, normalizeOrderCollections } from "../../../modules/Order/utils/orderCollectionIdentity";
36
37
  import { OrderHooks } from "./types";
38
+ import { mergeOrderPaymentListsByIdentity, mergeOrderPaymentRecord, resolveOrderPaymentIdentity } from "../../../modules/Order/payment-utils";
37
39
 
38
40
  /**
39
41
  * SQLite 存储名称
@@ -47,7 +49,7 @@ var ORDER_SQLITE_DEDUPE_MS = 15000;
47
49
  /** silentRefresh 最小间隔,避免 preload 后立即全量重拉 */
48
50
  var ORDER_SILENT_REFRESH_MIN_INTERVAL_MS = 30000;
49
51
  /** 视为「业务侧刚写入」的来源,pubsub 回声可跳过 SQLite */
50
- var ORDER_BUSINESS_WRITE_SOURCES = new Set(['upsertOrdersFromRemote', 'upsertPendingSyncOrders', 'overwriteExistingOrder', 'markOrderSyncedByExternalSaleNumber']);
52
+ var ORDER_BUSINESS_WRITE_SOURCES = new Set(['upsertOrdersFromRemote', 'upsertPendingSyncOrders', 'upsertLocalDraftOrders', 'overwriteExistingOrder', 'markOrderSyncedByExternalSaleNumber', 'updateLocalPayment']);
51
53
 
52
54
  /**
53
55
  * 合并热路径全表去重自检开关:默认关闭。
@@ -55,6 +57,12 @@ var ORDER_BUSINESS_WRITE_SOURCES = new Set(['upsertOrdersFromRemote', 'upsertPen
55
57
  * 仅用于排查增量身份索引是否遗漏了重复订单,不应在生产常开。
56
58
  */
57
59
  var ORDER_MERGE_SELF_CHECK = false;
60
+ var ORDER_AUTHORITATIVE_COLLECTION_FIELDS = ['products', 'bookings', 'payments'];
61
+ var ORDER_COLLECTION_KIND_BY_FIELD = {
62
+ products: 'product',
63
+ bookings: 'booking',
64
+ payments: 'payment'
65
+ };
58
66
 
59
67
  /**
60
68
  * Order 模块 - 基础框架
@@ -233,6 +241,104 @@ export var OrderModule = /*#__PURE__*/function (_BaseModule) {
233
241
  }
234
242
  }
235
243
 
244
+ /**
245
+ * 在订单进入内存或 SQLite 前统一压缩子项强身份重复并补齐协议 UID。
246
+ * 仅记录计数与订单标识,不输出商品、预约、支付或客户明细。
247
+ */
248
+ }, {
249
+ key: "normalizeOrderCollectionsForIngress",
250
+ value: function normalizeOrderCollectionsForIngress(order, source) {
251
+ var result = normalizeOrderCollections(order);
252
+ var kinds = ['product', 'booking', 'payment'];
253
+ var hasChanges = kinds.some(function (kind) {
254
+ var stats = result.stats[kind];
255
+ return stats.backfilledUidCount > 0 || stats.collapsedDuplicateCount > 0 || stats.uidConflictCount > 0 || stats.anonymousRowCount > 0;
256
+ });
257
+ if (hasChanges) {
258
+ var _order$order_id, _external_sale_number;
259
+ var summarize = function summarize(stats) {
260
+ return {
261
+ backfilled_uid_count: stats.backfilledUidCount,
262
+ collapsed_duplicate_count: stats.collapsedDuplicateCount,
263
+ uid_conflict_count: stats.uidConflictCount,
264
+ anonymous_row_count: stats.anonymousRowCount
265
+ };
266
+ };
267
+ var context = {
268
+ source: source,
269
+ order_id: (_order$order_id = order === null || order === void 0 ? void 0 : order.order_id) !== null && _order$order_id !== void 0 ? _order$order_id : null,
270
+ external_sale_number: (_external_sale_number = order === null || order === void 0 ? void 0 : order.external_sale_number) !== null && _external_sale_number !== void 0 ? _external_sale_number : null,
271
+ products: summarize(result.stats.product),
272
+ bookings: summarize(result.stats.booking),
273
+ payments: summarize(result.stats.payment)
274
+ };
275
+ var hasUnsafeIdentity = kinds.some(function (kind) {
276
+ var stats = result.stats[kind];
277
+ return stats.uidConflictCount > 0 || stats.anonymousRowCount > 0;
278
+ });
279
+ if (hasUnsafeIdentity) {
280
+ this.logWarning('订单子项身份存在冲突或匿名行', context);
281
+ } else {
282
+ this.logInfo('订单子项身份已规范化', context);
283
+ }
284
+ }
285
+ return result.order;
286
+ }
287
+
288
+ /**
289
+ * incoming 决定权威成员集合;同一持久化行仍从 existing 补齐 incoming 省略的本地展示字段。
290
+ */
291
+ }, {
292
+ key: "mergeAuthoritativeOrderCollection",
293
+ value: function mergeAuthoritativeOrderCollection(field, existing, incoming) {
294
+ var uidRemaps = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : [];
295
+ var kind = ORDER_COLLECTION_KIND_BY_FIELD[field];
296
+ var existingItems = Array.isArray(existing) ? existing : [];
297
+ return incoming.map(function (incomingItem) {
298
+ var existingItem = existingItems.find(function (item) {
299
+ return isSameOrderCollectionItem(kind, item, incomingItem);
300
+ });
301
+ if (!existingItem) return cloneDeep(incomingItem);
302
+ var mergedItem = mergeOrderCollectionItems(kind, existingItem, incomingItem);
303
+ var mergedUid = getOrderCollectionUid(kind, mergedItem);
304
+ if (mergedUid) {
305
+ var _iterator = _createForOfIteratorHelper(new Set([getOrderCollectionReferenceUid(kind, existingItem), getOrderCollectionReferenceUid(kind, incomingItem)])),
306
+ _step;
307
+ try {
308
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
309
+ var from = _step.value;
310
+ if (!from || from === mergedUid) continue;
311
+ uidRemaps.push({
312
+ kind: kind,
313
+ from: from,
314
+ to: mergedUid,
315
+ persistentId: getOrderCollectionPersistentId(kind, incomingItem) || getOrderCollectionPersistentId(kind, existingItem)
316
+ });
317
+ }
318
+ } catch (err) {
319
+ _iterator.e(err);
320
+ } finally {
321
+ _iterator.f();
322
+ }
323
+ }
324
+ return mergedItem;
325
+ });
326
+ }
327
+ }, {
328
+ key: "reconcileOverwriteOrderCollections",
329
+ value: function reconcileOverwriteOrderCollections(existing, incoming) {
330
+ var reconciled = cloneDeep(incoming);
331
+ var existingRecord = existing;
332
+ var incomingRecord = incoming;
333
+ var uidRemaps = [];
334
+ for (var _i = 0, _ORDER_AUTHORITATIVE_ = ORDER_AUTHORITATIVE_COLLECTION_FIELDS; _i < _ORDER_AUTHORITATIVE_.length; _i++) {
335
+ var field = _ORDER_AUTHORITATIVE_[_i];
336
+ if (!Array.isArray(incomingRecord[field])) continue;
337
+ reconciled[field] = this.mergeAuthoritativeOrderCollection(field, existingRecord[field], incomingRecord[field], uidRemaps);
338
+ }
339
+ return this.normalizeOrderCollectionsForIngress(applyOrderCollectionUidRemaps(reconciled, uidRemaps), 'overwriteExistingOrder.reconciled');
340
+ }
341
+
236
342
  /**
237
343
  * 记录订单最近一次 SQLite 写入,供 pubsub 回声去重使用。
238
344
  *
@@ -243,11 +349,11 @@ export var OrderModule = /*#__PURE__*/function (_BaseModule) {
243
349
  key: "recordRecentSqliteWrite",
244
350
  value: function recordRecentSqliteWrite(source, orders) {
245
351
  var now = Date.now();
246
- var _iterator = _createForOfIteratorHelper(orders),
247
- _step;
352
+ var _iterator2 = _createForOfIteratorHelper(orders),
353
+ _step2;
248
354
  try {
249
- for (_iterator.s(); !(_step = _iterator.n()).done;) {
250
- var order = _step.value;
355
+ for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
356
+ var order = _step2.value;
251
357
  var storageKey = this.getOrderStorageKey(order);
252
358
  if (!storageKey) continue;
253
359
  this.recentSqliteWriteByStorageKey.set(storageKey, {
@@ -256,9 +362,9 @@ export var OrderModule = /*#__PURE__*/function (_BaseModule) {
256
362
  });
257
363
  }
258
364
  } catch (err) {
259
- _iterator.e(err);
365
+ _iterator2.e(err);
260
366
  } finally {
261
- _iterator.f();
367
+ _iterator2.f();
262
368
  }
263
369
  }
264
370
 
@@ -281,11 +387,11 @@ export var OrderModule = /*#__PURE__*/function (_BaseModule) {
281
387
  var now = Date.now();
282
388
  var toWrite = [];
283
389
  var skipped = [];
284
- var _iterator2 = _createForOfIteratorHelper(orders),
285
- _step2;
390
+ var _iterator3 = _createForOfIteratorHelper(orders),
391
+ _step3;
286
392
  try {
287
- for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
288
- var order = _step2.value;
393
+ for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
394
+ var order = _step3.value;
289
395
  var storageKey = this.getOrderStorageKey(order);
290
396
  if (!storageKey) {
291
397
  toWrite.push(order);
@@ -301,9 +407,9 @@ export var OrderModule = /*#__PURE__*/function (_BaseModule) {
301
407
  }
302
408
  var recent = this.recentSqliteWriteByStorageKey.get(storageKey);
303
409
  if (recent && ORDER_BUSINESS_WRITE_SOURCES.has(recent.source) && now - recent.ts < ORDER_SQLITE_DEDUPE_MS) {
304
- var _order$order_id;
410
+ var _order$order_id2;
305
411
  skipped.push({
306
- orderId: (_order$order_id = order.order_id) !== null && _order$order_id !== void 0 ? _order$order_id : null,
412
+ orderId: (_order$order_id2 = order.order_id) !== null && _order$order_id2 !== void 0 ? _order$order_id2 : null,
307
413
  storageKey: storageKey,
308
414
  reason: 'skip_pubsub_echo_after_business_write',
309
415
  lastSource: recent.source,
@@ -314,9 +420,9 @@ export var OrderModule = /*#__PURE__*/function (_BaseModule) {
314
420
  toWrite.push(order);
315
421
  }
316
422
  } catch (err) {
317
- _iterator2.e(err);
423
+ _iterator3.e(err);
318
424
  } finally {
319
- _iterator2.f();
425
+ _iterator3.f();
320
426
  }
321
427
  return {
322
428
  toWrite: toWrite,
@@ -336,11 +442,11 @@ export var OrderModule = /*#__PURE__*/function (_BaseModule) {
336
442
  var now = Date.now();
337
443
  var toFetch = [];
338
444
  var skipped = [];
339
- var _iterator3 = _createForOfIteratorHelper(ids),
340
- _step3;
445
+ var _iterator4 = _createForOfIteratorHelper(ids),
446
+ _step4;
341
447
  try {
342
- for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
343
- var id = _step3.value;
448
+ for (_iterator4.s(); !(_step4 = _iterator4.n()).done;) {
449
+ var id = _step4.value;
344
450
  var existing = this.getOrderByOrderId(id);
345
451
  if (!existing) {
346
452
  toFetch.push(id);
@@ -359,9 +465,9 @@ export var OrderModule = /*#__PURE__*/function (_BaseModule) {
359
465
  toFetch.push(id);
360
466
  }
361
467
  } catch (err) {
362
- _iterator3.e(err);
468
+ _iterator4.e(err);
363
469
  } finally {
364
- _iterator3.f();
470
+ _iterator4.f();
365
471
  }
366
472
  return {
367
473
  toFetch: toFetch,
@@ -575,6 +681,29 @@ export var OrderModule = /*#__PURE__*/function (_BaseModule) {
575
681
  value: function getOrderByOrderId(orderId) {
576
682
  return this.store.map.get(this.getIdKey(orderId));
577
683
  }
684
+ }, {
685
+ key: "buildLocalPaymentOrderLogContext",
686
+ value: function buildLocalPaymentOrderLogContext(order) {
687
+ var _order$order_id3, _order$external_sale_, _order$order_number, _order$shop_order_num, _order$shop_full_orde, _order$status, _order$payment_status, _order$platform, _order$business_code, _order$order_sales_ch, _order$sales_channel, _order$type;
688
+ if (!order) return {};
689
+ return {
690
+ order_id: (_order$order_id3 = order.order_id) !== null && _order$order_id3 !== void 0 ? _order$order_id3 : null,
691
+ external_sale_number: (_order$external_sale_ = order.external_sale_number) !== null && _order$external_sale_ !== void 0 ? _order$external_sale_ : null,
692
+ order_number: (_order$order_number = order.order_number) !== null && _order$order_number !== void 0 ? _order$order_number : null,
693
+ shop_order_number: (_order$shop_order_num = order.shop_order_number) !== null && _order$shop_order_num !== void 0 ? _order$shop_order_num : null,
694
+ shop_full_order_number: (_order$shop_full_orde = order.shop_full_order_number) !== null && _order$shop_full_orde !== void 0 ? _order$shop_full_orde : null,
695
+ order_status: (_order$status = order.status) !== null && _order$status !== void 0 ? _order$status : null,
696
+ payment_status: (_order$payment_status = order.payment_status) !== null && _order$payment_status !== void 0 ? _order$payment_status : null,
697
+ is_draft_order: Number(order.is_draft_order || 0),
698
+ need_sync: Number(order.need_sync || 0),
699
+ platform: (_order$platform = order.platform) !== null && _order$platform !== void 0 ? _order$platform : null,
700
+ business_code: (_order$business_code = order.business_code) !== null && _order$business_code !== void 0 ? _order$business_code : null,
701
+ order_sales_channel: (_order$order_sales_ch = order.order_sales_channel) !== null && _order$order_sales_ch !== void 0 ? _order$order_sales_ch : null,
702
+ sales_channel: (_order$sales_channel = order.sales_channel) !== null && _order$sales_channel !== void 0 ? _order$sales_channel : null,
703
+ order_type: (_order$type = order.type) !== null && _order$type !== void 0 ? _order$type : null,
704
+ payments_count: Array.isArray(order.payments) ? order.payments.length : 0
705
+ };
706
+ }
578
707
  }, {
579
708
  key: "getLocalOrderFromMemoryByLookup",
580
709
  value: function getLocalOrderFromMemoryByLookup(lookup) {
@@ -592,46 +721,83 @@ export var OrderModule = /*#__PURE__*/function (_BaseModule) {
592
721
  value: function () {
593
722
  var _getLocalOrderByLookup = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee5(lookup) {
594
723
  var _this6 = this;
595
- var key, memoryOrderById, memoryOrder, orders, localOrder;
724
+ var options,
725
+ key,
726
+ logContext,
727
+ memoryOrderById,
728
+ memoryOrder,
729
+ orders,
730
+ localOrder,
731
+ normalizedOrder,
732
+ _args5 = arguments;
596
733
  return _regeneratorRuntime().wrap(function _callee5$(_context5) {
597
734
  while (1) switch (_context5.prev = _context5.next) {
598
735
  case 0:
736
+ options = _args5.length > 1 && _args5[1] !== undefined ? _args5[1] : {};
599
737
  key = this.getIdKey(lookup);
738
+ logContext = {
739
+ operation_id: options.operationId,
740
+ lookup: key,
741
+ memory_order_count: this.store.list.length
742
+ };
743
+ if (options.operationId) {
744
+ this.logInfo('getLocalOrderByLookup-localPayment-开始', logContext);
745
+ }
600
746
  memoryOrderById = this.store.map.get(key);
601
747
  if (!memoryOrderById) {
602
- _context5.next = 4;
748
+ _context5.next = 8;
603
749
  break;
604
750
  }
751
+ if (options.operationId) {
752
+ this.logInfo('getLocalOrderByLookup-localPayment-命中内存映射', _objectSpread(_objectSpread({}, logContext), this.buildLocalPaymentOrderLogContext(memoryOrderById)));
753
+ }
605
754
  return _context5.abrupt("return", memoryOrderById);
606
- case 4:
755
+ case 8:
607
756
  memoryOrder = this.store.list.find(function (order) {
608
757
  return _this6.isOrderLookupMatch(order, key);
609
758
  });
610
759
  if (!memoryOrder) {
611
- _context5.next = 7;
760
+ _context5.next = 12;
612
761
  break;
613
762
  }
763
+ if (options.operationId) {
764
+ this.logInfo('getLocalOrderByLookup-localPayment-命中内存列表', _objectSpread(_objectSpread({}, logContext), this.buildLocalPaymentOrderLogContext(memoryOrder)));
765
+ }
614
766
  return _context5.abrupt("return", memoryOrder);
615
- case 7:
616
- _context5.next = 9;
767
+ case 12:
768
+ if (options.operationId) {
769
+ this.logInfo('getLocalOrderByLookup-localPayment-开始读取SQLite', logContext);
770
+ }
771
+ _context5.next = 15;
617
772
  return this.loadOrdersFromSQLite();
618
- case 9:
773
+ case 15:
619
774
  orders = _context5.sent;
620
775
  localOrder = orders.find(function (order) {
621
776
  return _this6.isOrderLookupMatch(order, key);
622
777
  });
623
778
  if (localOrder) {
624
- _context5.next = 13;
779
+ _context5.next = 20;
625
780
  break;
626
781
  }
782
+ if (options.operationId) {
783
+ this.logWarning('getLocalOrderByLookup-localPayment-SQLite未命中', _objectSpread(_objectSpread({}, logContext), {}, {
784
+ sqlite_order_count: orders.length
785
+ }));
786
+ }
627
787
  return _context5.abrupt("return", null);
628
- case 13:
788
+ case 20:
629
789
  this.store.list = orders.map(function (order) {
630
790
  return _this6.normalizeOrderForMemoryList(order);
631
791
  });
632
792
  this.syncOrdersMap();
633
- return _context5.abrupt("return", this.normalizeOrderForMemoryList(localOrder));
634
- case 16:
793
+ normalizedOrder = this.normalizeOrderForMemoryList(localOrder);
794
+ if (options.operationId) {
795
+ this.logInfo('getLocalOrderByLookup-localPayment-SQLite命中', _objectSpread(_objectSpread({}, logContext), {}, {
796
+ sqlite_order_count: orders.length
797
+ }, this.buildLocalPaymentOrderLogContext(normalizedOrder)));
798
+ }
799
+ return _context5.abrupt("return", normalizedOrder);
800
+ case 25:
635
801
  case "end":
636
802
  return _context5.stop();
637
803
  }
@@ -642,15 +808,186 @@ export var OrderModule = /*#__PURE__*/function (_BaseModule) {
642
808
  }
643
809
  return getLocalOrderByLookup;
644
810
  }()
811
+ }, {
812
+ key: "getLocalPayment",
813
+ value: function () {
814
+ var _getLocalPayment = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee6(params) {
815
+ var logContext, order, payments, match;
816
+ return _regeneratorRuntime().wrap(function _callee6$(_context6) {
817
+ while (1) switch (_context6.prev = _context6.next) {
818
+ case 0:
819
+ logContext = {
820
+ operation_id: params.operationId,
821
+ sale_id: params.saleId,
822
+ payment_number: params.paymentNumber
823
+ };
824
+ this.logInfo('getLocalPayment-开始', logContext);
825
+ _context6.prev = 2;
826
+ _context6.next = 5;
827
+ return this.getLocalOrderByLookup(params.saleId, {
828
+ operationId: params.operationId
829
+ });
830
+ case 5:
831
+ order = _context6.sent;
832
+ if (order) {
833
+ _context6.next = 9;
834
+ break;
835
+ }
836
+ this.logWarning('getLocalPayment-订单未命中', logContext);
837
+ return _context6.abrupt("return", null);
838
+ case 9:
839
+ payments = order.payments || [];
840
+ match = resolveOrderPaymentIdentity(payments, params.paymentNumber, 'compat');
841
+ if (match) {
842
+ _context6.next = 14;
843
+ break;
844
+ }
845
+ this.logWarning('getLocalPayment-支付项未命中', _objectSpread(_objectSpread(_objectSpread({}, logContext), this.buildLocalPaymentOrderLogContext(order)), {}, {
846
+ payments: payments
847
+ }));
848
+ return _context6.abrupt("return", null);
849
+ case 14:
850
+ this.logInfo('getLocalPayment-支付项命中', _objectSpread(_objectSpread(_objectSpread({}, logContext), this.buildLocalPaymentOrderLogContext(order)), {}, {
851
+ matched_by: match.matchedBy,
852
+ payment_index: match.index,
853
+ payment: match.payment
854
+ }));
855
+ return _context6.abrupt("return", {
856
+ order: order,
857
+ payment: match.payment,
858
+ paymentIndex: match.index,
859
+ matchedBy: match.matchedBy
860
+ });
861
+ case 18:
862
+ _context6.prev = 18;
863
+ _context6.t0 = _context6["catch"](2);
864
+ this.logError('getLocalPayment-查询失败', _objectSpread(_objectSpread({}, logContext), {}, {
865
+ error: _context6.t0 instanceof Error ? _context6.t0.message : String(_context6.t0)
866
+ }));
867
+ throw _context6.t0;
868
+ case 22:
869
+ case "end":
870
+ return _context6.stop();
871
+ }
872
+ }, _callee6, this, [[2, 18]]);
873
+ }));
874
+ function getLocalPayment(_x4) {
875
+ return _getLocalPayment.apply(this, arguments);
876
+ }
877
+ return getLocalPayment;
878
+ }()
879
+ }, {
880
+ key: "updateLocalPayment",
881
+ value: function () {
882
+ var _updateLocalPayment = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee7(params) {
883
+ var logContext, found, previousPayment, nextPayment, nextOrder, paymentContext, targetIndex, previousList, storageOrder, _storageOrder$order_i, _storageOrder$order_i2, _storageOrder$order_i3;
884
+ return _regeneratorRuntime().wrap(function _callee7$(_context7) {
885
+ while (1) switch (_context7.prev = _context7.next) {
886
+ case 0:
887
+ logContext = {
888
+ operation_id: params.operationId,
889
+ sale_id: params.saleId,
890
+ payment_number: params.paymentNumber,
891
+ incoming_payment_update: params.updates
892
+ };
893
+ this.logInfo('updateLocalPayment-开始', logContext);
894
+ _context7.next = 4;
895
+ return this.getLocalPayment(params);
896
+ case 4:
897
+ found = _context7.sent;
898
+ if (found) {
899
+ _context7.next = 8;
900
+ break;
901
+ }
902
+ this.logWarning('updateLocalPayment-支付项未命中', logContext);
903
+ return _context7.abrupt("return", null);
904
+ case 8:
905
+ previousPayment = cloneDeep(found.payment);
906
+ nextPayment = mergeOrderPaymentRecord(found.payment, _objectSpread(_objectSpread({}, params.updates), {}, {
907
+ updated_at: params.updates.updated_at || dayjs().format('YYYY-MM-DD HH:mm:ss')
908
+ }));
909
+ nextOrder = _objectSpread(_objectSpread({}, found.order), {}, {
910
+ payments: (found.order.payments || []).map(function (payment, index) {
911
+ return index === found.paymentIndex ? nextPayment : payment;
912
+ }),
913
+ updated_at: dayjs().format('YYYY-MM-DD HH:mm:ss')
914
+ });
915
+ paymentContext = _objectSpread(_objectSpread(_objectSpread({}, logContext), {}, {
916
+ matched_by: found.matchedBy,
917
+ payment_index: found.paymentIndex
918
+ }, this.buildLocalPaymentOrderLogContext(nextOrder)), {}, {
919
+ previous_payment: previousPayment,
920
+ payment: nextPayment
921
+ });
922
+ this.logInfo('updateLocalPayment-支付项合并完成', paymentContext);
923
+ targetIndex = this.findOrderIndexByIdentity(this.store.list, found.order);
924
+ if (!(targetIndex < 0)) {
925
+ _context7.next = 17;
926
+ break;
927
+ }
928
+ this.logError('updateLocalPayment-订单Store索引未命中', paymentContext);
929
+ return _context7.abrupt("return", null);
930
+ case 17:
931
+ previousList = this.store.list;
932
+ this.store.list = this.store.list.map(function (order, index) {
933
+ return index === targetIndex ? nextOrder : order;
934
+ });
935
+ this.syncOrdersMap();
936
+ storageOrder = this.withoutSyntheticDraftOrderId(nextOrder);
937
+ _context7.prev = 21;
938
+ this.logInfo('updateLocalPayment-SQLite写入开始', _objectSpread(_objectSpread({}, paymentContext), {}, {
939
+ storage_order_id: (_storageOrder$order_i = storageOrder.order_id) !== null && _storageOrder$order_i !== void 0 ? _storageOrder$order_i : null
940
+ }));
941
+ _context7.next = 25;
942
+ return this.updateOrderInSQLite(storageOrder, 'updateLocalPayment', {
943
+ throwOnError: true
944
+ });
945
+ case 25:
946
+ this.logInfo('updateLocalPayment-SQLite写入完成', _objectSpread(_objectSpread({}, paymentContext), {}, {
947
+ storage_order_id: (_storageOrder$order_i2 = storageOrder.order_id) !== null && _storageOrder$order_i2 !== void 0 ? _storageOrder$order_i2 : null
948
+ }));
949
+ _context7.next = 34;
950
+ break;
951
+ case 28:
952
+ _context7.prev = 28;
953
+ _context7.t0 = _context7["catch"](21);
954
+ this.store.list = previousList;
955
+ this.syncOrdersMap();
956
+ this.logError('updateLocalPayment-SQLite写入失败并回滚内存', _objectSpread(_objectSpread({}, paymentContext), {}, {
957
+ storage_order_id: (_storageOrder$order_i3 = storageOrder.order_id) !== null && _storageOrder$order_i3 !== void 0 ? _storageOrder$order_i3 : null,
958
+ error: _context7.t0 instanceof Error ? _context7.t0.message : String(_context7.t0)
959
+ }));
960
+ throw _context7.t0;
961
+ case 34:
962
+ _context7.next = 36;
963
+ return this.emitOrdersChanged([nextOrder], 'updateLocalPayment');
964
+ case 36:
965
+ this.logInfo('updateLocalPayment-变更事件完成', paymentContext);
966
+ return _context7.abrupt("return", {
967
+ order: nextOrder,
968
+ payment: nextPayment,
969
+ previousPayment: previousPayment
970
+ });
971
+ case 38:
972
+ case "end":
973
+ return _context7.stop();
974
+ }
975
+ }, _callee7, this, [[21, 28]]);
976
+ }));
977
+ function updateLocalPayment(_x5) {
978
+ return _updateLocalPayment.apply(this, arguments);
979
+ }
980
+ return updateLocalPayment;
981
+ }()
645
982
  }, {
646
983
  key: "loadOrdersByServer",
647
984
  value: function () {
648
- var _loadOrdersByServer = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee6() {
985
+ var _loadOrdersByServer = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee8() {
649
986
  var _this$store,
650
987
  _this7 = this;
651
988
  var orderList, data, mergedOrders, memoryOrders;
652
- return _regeneratorRuntime().wrap(function _callee6$(_context6) {
653
- while (1) switch (_context6.prev = _context6.next) {
989
+ return _regeneratorRuntime().wrap(function _callee8$(_context8) {
990
+ while (1) switch (_context8.prev = _context8.next) {
654
991
  case 0:
655
992
  orderList = [];
656
993
  this.logInfo('loadOrdersByServer-开始', {
@@ -658,51 +995,51 @@ export var OrderModule = /*#__PURE__*/function (_BaseModule) {
658
995
  query: ((_this$store = this.store) === null || _this$store === void 0 ? void 0 : _this$store.createdAtQuery) || null
659
996
  });
660
997
  if (!this.orderDataSource) {
661
- _context6.next = 17;
998
+ _context8.next = 17;
662
999
  break;
663
1000
  }
664
- _context6.prev = 3;
665
- _context6.next = 6;
1001
+ _context8.prev = 3;
1002
+ _context8.next = 6;
666
1003
  return this.orderDataSource.run({
667
1004
  sse: {
668
1005
  query: this.store.createdAtQuery
669
1006
  }
670
1007
  });
671
1008
  case 6:
672
- data = _context6.sent;
1009
+ data = _context8.sent;
673
1010
  orderList = data || [];
674
1011
  this.logInfo('loadOrdersByServer-SSE拉取成功', {
675
1012
  count: orderList.length
676
1013
  });
677
1014
  this.markOrderPulledAtNow();
678
1015
  this.lastServerFullFetchAtMs = Date.now();
679
- _context6.next = 17;
1016
+ _context8.next = 17;
680
1017
  break;
681
1018
  case 13:
682
- _context6.prev = 13;
683
- _context6.t0 = _context6["catch"](3);
1019
+ _context8.prev = 13;
1020
+ _context8.t0 = _context8["catch"](3);
684
1021
  this.logInfo('loadOrdersByServer-SSE拉取失败,保持当前内存订单');
685
- return _context6.abrupt("return", this.store.list);
1022
+ return _context8.abrupt("return", this.store.list);
686
1023
  case 17:
687
- _context6.next = 19;
1024
+ _context8.next = 19;
688
1025
  return this.replaceOrdersSnapshotInSQLite(orderList, 'loadOrdersByServer');
689
1026
  case 19:
690
- mergedOrders = _context6.sent;
1027
+ mergedOrders = _context8.sent;
691
1028
  memoryOrders = mergedOrders.map(function (order) {
692
1029
  return _this7.normalizeOrderForMemoryList(order);
693
1030
  });
694
1031
  this.logInfo('loadOrdersByServer-结束', {
695
1032
  count: memoryOrders.length
696
1033
  });
697
- _context6.next = 24;
1034
+ _context8.next = 24;
698
1035
  return this.core.effects.emit(OrderHooks.onOrdersLoaded, memoryOrders);
699
1036
  case 24:
700
- return _context6.abrupt("return", memoryOrders);
1037
+ return _context8.abrupt("return", memoryOrders);
701
1038
  case 25:
702
1039
  case "end":
703
- return _context6.stop();
1040
+ return _context8.stop();
704
1041
  }
705
- }, _callee6, this, [[3, 13]]);
1042
+ }, _callee8, this, [[3, 13]]);
706
1043
  }));
707
1044
  function loadOrdersByServer() {
708
1045
  return _loadOrdersByServer.apply(this, arguments);
@@ -717,61 +1054,61 @@ export var OrderModule = /*#__PURE__*/function (_BaseModule) {
717
1054
  }, {
718
1055
  key: "silentRefresh",
719
1056
  value: (function () {
720
- var _silentRefresh = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee7() {
1057
+ var _silentRefresh = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee9() {
721
1058
  var startTime, elapsedSinceFullFetch, orders, errorMessage;
722
- return _regeneratorRuntime().wrap(function _callee7$(_context7) {
723
- while (1) switch (_context7.prev = _context7.next) {
1059
+ return _regeneratorRuntime().wrap(function _callee9$(_context9) {
1060
+ while (1) switch (_context9.prev = _context9.next) {
724
1061
  case 0:
725
1062
  startTime = Date.now();
726
1063
  elapsedSinceFullFetch = Date.now() - this.lastServerFullFetchAtMs;
727
1064
  if (!(this.lastServerFullFetchAtMs > 0 && elapsedSinceFullFetch < ORDER_SILENT_REFRESH_MIN_INTERVAL_MS)) {
728
- _context7.next = 4;
1065
+ _context9.next = 4;
729
1066
  break;
730
1067
  }
731
- return _context7.abrupt("return", this.store.list);
1068
+ return _context9.abrupt("return", this.store.list);
732
1069
  case 4:
733
1070
  this.logInfo('silentRefresh 开始');
734
- _context7.prev = 5;
1071
+ _context9.prev = 5;
735
1072
  // 刷新前,需要更新当前营业日边界(store.createdAtQuery)
736
1073
  this.updateCreateStore();
737
- _context7.next = 9;
1074
+ _context9.next = 9;
738
1075
  return this.loadOrdersByServer();
739
1076
  case 9:
740
- orders = _context7.sent;
1077
+ orders = _context9.sent;
741
1078
  if (!(orders.length > 0)) {
742
- _context7.next = 19;
1079
+ _context9.next = 19;
743
1080
  break;
744
1081
  }
745
1082
  this.store.list = cloneDeep(orders);
746
1083
  this.syncOrdersMap();
747
1084
  this.emitOrdersChanged([], 'silentRefresh');
748
- _context7.next = 16;
1085
+ _context9.next = 16;
749
1086
  return this.core.effects.emit(OrderHooks.onOrdersSyncCompleted, null);
750
1087
  case 16:
751
1088
  this.logInfo('silentRefresh 完成', {
752
1089
  orderCount: orders.length,
753
1090
  duration: "".concat(Date.now() - startTime, "ms")
754
1091
  });
755
- _context7.next = 20;
1092
+ _context9.next = 20;
756
1093
  break;
757
1094
  case 19:
758
1095
  this.logInfo('silentRefresh: 服务器未返回数据');
759
1096
  case 20:
760
- return _context7.abrupt("return", this.store.list);
1097
+ return _context9.abrupt("return", this.store.list);
761
1098
  case 23:
762
- _context7.prev = 23;
763
- _context7.t0 = _context7["catch"](5);
764
- errorMessage = _context7.t0 instanceof Error ? _context7.t0.message : String(_context7.t0);
1099
+ _context9.prev = 23;
1100
+ _context9.t0 = _context9["catch"](5);
1101
+ errorMessage = _context9.t0 instanceof Error ? _context9.t0.message : String(_context9.t0);
765
1102
  this.logError('silentRefresh 失败', {
766
1103
  duration: "".concat(Date.now() - startTime, "ms"),
767
1104
  error: errorMessage
768
1105
  });
769
- return _context7.abrupt("return", this.store.list);
1106
+ return _context9.abrupt("return", this.store.list);
770
1107
  case 28:
771
1108
  case "end":
772
- return _context7.stop();
1109
+ return _context9.stop();
773
1110
  }
774
- }, _callee7, this, [[5, 23]]);
1111
+ }, _callee9, this, [[5, 23]]);
775
1112
  }));
776
1113
  function silentRefresh() {
777
1114
  return _silentRefresh.apply(this, arguments);
@@ -781,17 +1118,30 @@ export var OrderModule = /*#__PURE__*/function (_BaseModule) {
781
1118
  }, {
782
1119
  key: "normalizeOrderForMemoryList",
783
1120
  value: function normalizeOrderForMemoryList(order) {
784
- var externalSaleNumber = order === null || order === void 0 ? void 0 : order.external_sale_number;
785
- if ((order === null || order === void 0 ? void 0 : order.order_id) !== undefined && (order === null || order === void 0 ? void 0 : order.order_id) !== null && (order === null || order === void 0 ? void 0 : order.order_id) !== '') {
786
- return order;
1121
+ var source = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'normalizeOrderForMemoryList';
1122
+ var normalizedOrder = this.normalizeOrderCollectionsForIngress(order, source);
1123
+ var externalSaleNumber = normalizedOrder === null || normalizedOrder === void 0 ? void 0 : normalizedOrder.external_sale_number;
1124
+ if ((normalizedOrder === null || normalizedOrder === void 0 ? void 0 : normalizedOrder.order_id) !== undefined && (normalizedOrder === null || normalizedOrder === void 0 ? void 0 : normalizedOrder.order_id) !== null && (normalizedOrder === null || normalizedOrder === void 0 ? void 0 : normalizedOrder.order_id) !== '') {
1125
+ return normalizedOrder;
787
1126
  }
788
1127
  if (externalSaleNumber === undefined || externalSaleNumber === null || externalSaleNumber === '') {
789
- return order;
1128
+ return normalizedOrder;
790
1129
  }
791
- return _objectSpread(_objectSpread({}, order), {}, {
1130
+ return _objectSpread(_objectSpread({}, normalizedOrder), {}, {
792
1131
  order_id: externalSaleNumber
793
1132
  });
794
1133
  }
1134
+ }, {
1135
+ key: "withoutSyntheticDraftOrderId",
1136
+ value: function withoutSyntheticDraftOrderId(order) {
1137
+ var externalSaleNumber = order.external_sale_number;
1138
+ if (Number(order.is_draft_order || 0) !== 1 || order.order_id === undefined || externalSaleNumber === undefined || String(order.order_id) !== String(externalSaleNumber)) {
1139
+ return order;
1140
+ }
1141
+ var next = _objectSpread({}, order);
1142
+ delete next.order_id;
1143
+ return next;
1144
+ }
795
1145
 
796
1146
  /**
797
1147
  * 仅覆盖本地已存在的订单;不存在则直接跳过,不落库、不 emit。
@@ -800,31 +1150,31 @@ export var OrderModule = /*#__PURE__*/function (_BaseModule) {
800
1150
  }, {
801
1151
  key: "overwriteExistingOrder",
802
1152
  value: (function () {
803
- var _overwriteExistingOrder = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee8(fresh) {
1153
+ var _overwriteExistingOrder = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee10(fresh) {
804
1154
  var _this8 = this;
805
- var orderId, key;
806
- return _regeneratorRuntime().wrap(function _callee8$(_context8) {
807
- while (1) switch (_context8.prev = _context8.next) {
1155
+ var orderId, key, normalizedFresh, existingOrder, reconciledFresh;
1156
+ return _regeneratorRuntime().wrap(function _callee10$(_context10) {
1157
+ while (1) switch (_context10.prev = _context10.next) {
808
1158
  case 0:
809
1159
  orderId = fresh === null || fresh === void 0 ? void 0 : fresh.order_id;
810
1160
  if (!(orderId === undefined || orderId === null)) {
811
- _context8.next = 4;
1161
+ _context10.next = 4;
812
1162
  break;
813
1163
  }
814
1164
  this.logError('overwriteExistingOrder-入参缺少 order_id');
815
- return _context8.abrupt("return", {
1165
+ return _context10.abrupt("return", {
816
1166
  overwritten: false
817
1167
  });
818
1168
  case 4:
819
1169
  key = this.getIdKey(orderId);
820
1170
  if (this.store.map.has(key)) {
821
- _context8.next = 8;
1171
+ _context10.next = 8;
822
1172
  break;
823
1173
  }
824
1174
  this.logInfo('overwriteExistingOrder-本地不存在该订单,跳过', {
825
1175
  orderId: orderId
826
1176
  });
827
- return _context8.abrupt("return", {
1177
+ return _context10.abrupt("return", {
828
1178
  overwritten: false
829
1179
  });
830
1180
  case 8:
@@ -832,32 +1182,37 @@ export var OrderModule = /*#__PURE__*/function (_BaseModule) {
832
1182
  orderId: orderId,
833
1183
  storeOrderCountBefore: this.store.list.length
834
1184
  });
1185
+ normalizedFresh = this.normalizeOrderForMemoryList(fresh, 'overwriteExistingOrder.incoming');
1186
+ existingOrder = this.store.map.get(key);
1187
+ reconciledFresh = this.reconcileOverwriteOrderCollections(existingOrder, normalizedFresh);
835
1188
  this.store.list = this.store.list.map(function (order) {
836
1189
  var id = order === null || order === void 0 ? void 0 : order.order_id;
837
- if (id === undefined || id === null) return order;
838
- return _this8.getIdKey(id) === key ? fresh : order;
1190
+ if (id === undefined || id === null) {
1191
+ return _this8.normalizeOrderForMemoryList(order, 'overwriteExistingOrder.existingMemory');
1192
+ }
1193
+ return _this8.getIdKey(id) === key ? reconciledFresh : _this8.normalizeOrderForMemoryList(order, 'overwriteExistingOrder.existingMemory');
839
1194
  });
840
1195
  this.syncOrdersMap();
841
- _context8.next = 13;
842
- return this.updateOrderInSQLite(fresh, 'overwriteExistingOrder');
843
- case 13:
1196
+ _context10.next = 16;
1197
+ return this.updateOrderInSQLite(reconciledFresh, 'overwriteExistingOrder');
1198
+ case 16:
844
1199
  this.logInfo('overwriteExistingOrder-结束', {
845
1200
  orderId: orderId,
846
1201
  storeOrderCountAfter: this.store.list.length
847
1202
  });
848
- _context8.next = 16;
849
- return this.emitOrdersChanged([fresh], 'overwriteExistingOrder');
850
- case 16:
851
- return _context8.abrupt("return", {
1203
+ _context10.next = 19;
1204
+ return this.emitOrdersChanged([reconciledFresh], 'overwriteExistingOrder');
1205
+ case 19:
1206
+ return _context10.abrupt("return", {
852
1207
  overwritten: true
853
1208
  });
854
- case 17:
1209
+ case 20:
855
1210
  case "end":
856
- return _context8.stop();
1211
+ return _context10.stop();
857
1212
  }
858
- }, _callee8, this);
1213
+ }, _callee10, this);
859
1214
  }));
860
- function overwriteExistingOrder(_x4) {
1215
+ function overwriteExistingOrder(_x6) {
861
1216
  return _overwriteExistingOrder.apply(this, arguments);
862
1217
  }
863
1218
  return overwriteExistingOrder;
@@ -870,29 +1225,33 @@ export var OrderModule = /*#__PURE__*/function (_BaseModule) {
870
1225
  }, {
871
1226
  key: "upsertOrdersFromRemote",
872
1227
  value: (function () {
873
- var _upsertOrdersFromRemote = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee9(freshOrders) {
874
- return _regeneratorRuntime().wrap(function _callee9$(_context9) {
875
- while (1) switch (_context9.prev = _context9.next) {
1228
+ var _upsertOrdersFromRemote = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee11(freshOrders) {
1229
+ var source,
1230
+ _args11 = arguments;
1231
+ return _regeneratorRuntime().wrap(function _callee11$(_context11) {
1232
+ while (1) switch (_context11.prev = _context11.next) {
876
1233
  case 0:
1234
+ source = _args11.length > 1 && _args11[1] !== undefined ? _args11[1] : 'upsertOrdersFromRemote';
877
1235
  if (freshOrders !== null && freshOrders !== void 0 && freshOrders.length) {
878
- _context9.next = 3;
1236
+ _context11.next = 4;
879
1237
  break;
880
1238
  }
881
1239
  this.logInfo('upsertOrdersFromRemote-订单列表为空', {});
882
- return _context9.abrupt("return");
883
- case 3:
1240
+ return _context11.abrupt("return");
1241
+ case 4:
884
1242
  this.logInfo('upsertOrdersFromRemote-开始', {
885
- count: freshOrders.length
1243
+ count: freshOrders.length,
1244
+ source: source
886
1245
  });
887
- _context9.next = 6;
888
- return this.mergeOrdersToStore(freshOrders, 'upsertOrdersFromRemote');
889
- case 6:
1246
+ _context11.next = 7;
1247
+ return this.mergeOrdersToStore(freshOrders, source);
1248
+ case 7:
890
1249
  case "end":
891
- return _context9.stop();
1250
+ return _context11.stop();
892
1251
  }
893
- }, _callee9, this);
1252
+ }, _callee11, this);
894
1253
  }));
895
- function upsertOrdersFromRemote(_x5) {
1254
+ function upsertOrdersFromRemote(_x7) {
896
1255
  return _upsertOrdersFromRemote.apply(this, arguments);
897
1256
  }
898
1257
  return upsertOrdersFromRemote;
@@ -900,21 +1259,21 @@ export var OrderModule = /*#__PURE__*/function (_BaseModule) {
900
1259
  }, {
901
1260
  key: "markOrderSyncedByExternalSaleNumber",
902
1261
  value: function () {
903
- var _markOrderSyncedByExternalSaleNumber = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee10(params) {
1262
+ var _markOrderSyncedByExternalSaleNumber = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee12(params) {
904
1263
  var _this9 = this,
905
1264
  _existing$external_sa,
906
1265
  _nextOrder$order_id;
907
1266
  var externalSaleNumber, key, targetIndex, existing, nextOrder, storageKey;
908
- return _regeneratorRuntime().wrap(function _callee10$(_context10) {
909
- while (1) switch (_context10.prev = _context10.next) {
1267
+ return _regeneratorRuntime().wrap(function _callee12$(_context12) {
1268
+ while (1) switch (_context12.prev = _context12.next) {
910
1269
  case 0:
911
1270
  externalSaleNumber = params.externalSaleNumber;
912
1271
  if (!(externalSaleNumber === undefined || externalSaleNumber === null || externalSaleNumber === '')) {
913
- _context10.next = 4;
1272
+ _context12.next = 4;
914
1273
  break;
915
1274
  }
916
1275
  this.logError('markOrderSyncedByExternalSaleNumber-缺少 external_sale_number');
917
- return _context10.abrupt("return", {
1276
+ return _context12.abrupt("return", {
918
1277
  updated: false
919
1278
  });
920
1279
  case 4:
@@ -925,20 +1284,21 @@ export var OrderModule = /*#__PURE__*/function (_BaseModule) {
925
1284
  return _this9.getIdKey(value) === key;
926
1285
  });
927
1286
  if (!(targetIndex < 0)) {
928
- _context10.next = 9;
1287
+ _context12.next = 9;
929
1288
  break;
930
1289
  }
931
1290
  this.logInfo('markOrderSyncedByExternalSaleNumber-本地订单不存在,跳过', {
932
1291
  external_sale_number: externalSaleNumber
933
1292
  });
934
- return _context10.abrupt("return", {
1293
+ return _context12.abrupt("return", {
935
1294
  updated: false
936
1295
  });
937
1296
  case 9:
938
1297
  existing = this.store.list[targetIndex];
939
1298
  nextOrder = this.normalizeRemoteSyncedOrder(_objectSpread(_objectSpread(_objectSpread({}, existing), params.patch || {}), {}, {
940
1299
  external_sale_number: (_existing$external_sa = existing.external_sale_number) !== null && _existing$external_sa !== void 0 ? _existing$external_sa : externalSaleNumber,
941
- need_sync: 0
1300
+ need_sync: 0,
1301
+ is_draft_order: 0
942
1302
  }));
943
1303
  storageKey = this.getOrderStorageKey(nextOrder) || key;
944
1304
  this.store.list = this.store.list.map(function (order, index) {
@@ -946,27 +1306,27 @@ export var OrderModule = /*#__PURE__*/function (_BaseModule) {
946
1306
  return nextOrder;
947
1307
  });
948
1308
  this.syncOrdersMap();
949
- _context10.next = 16;
1309
+ _context12.next = 16;
950
1310
  return this.patchOrdersInSQLite([nextOrder], 'markOrderSyncedByExternalSaleNumber', _defineProperty({}, storageKey, 'update'));
951
1311
  case 16:
952
1312
  this.logInfo('markOrderSyncedByExternalSaleNumber-完成', {
953
1313
  external_sale_number: externalSaleNumber,
954
1314
  order_id: (_nextOrder$order_id = nextOrder.order_id) !== null && _nextOrder$order_id !== void 0 ? _nextOrder$order_id : null
955
1315
  });
956
- _context10.next = 19;
1316
+ _context12.next = 19;
957
1317
  return this.emitOrdersChanged([nextOrder], 'markOrderSyncedByExternalSaleNumber');
958
1318
  case 19:
959
- return _context10.abrupt("return", {
1319
+ return _context12.abrupt("return", {
960
1320
  updated: true,
961
1321
  order: nextOrder
962
1322
  });
963
1323
  case 20:
964
1324
  case "end":
965
- return _context10.stop();
1325
+ return _context12.stop();
966
1326
  }
967
- }, _callee10, this);
1327
+ }, _callee12, this);
968
1328
  }));
969
- function markOrderSyncedByExternalSaleNumber(_x6) {
1329
+ function markOrderSyncedByExternalSaleNumber(_x8) {
970
1330
  return _markOrderSyncedByExternalSaleNumber.apply(this, arguments);
971
1331
  }
972
1332
  return markOrderSyncedByExternalSaleNumber;
@@ -978,149 +1338,357 @@ export var OrderModule = /*#__PURE__*/function (_BaseModule) {
978
1338
  }, {
979
1339
  key: "upsertPendingSyncOrders",
980
1340
  value: (function () {
981
- var _upsertPendingSyncOrders = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee11(pendingOrders) {
982
- var pendingMap, memoryPendingMap, _iterator4, _step4, order, storageKey, patchedOrders, mergeActions, updatedList, identityKeyToIndex, index, _iterator5, _step5, key, _iterator6, _step6, _step6$value, _storageKey, pendingOrder, matchIndex, mergedOrder, insertIndex;
983
- return _regeneratorRuntime().wrap(function _callee11$(_context11) {
984
- while (1) switch (_context11.prev = _context11.next) {
1341
+ var _upsertPendingSyncOrders = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee13(pendingOrders) {
1342
+ var _this10 = this;
1343
+ var options,
1344
+ pendingMap,
1345
+ memoryPendingMap,
1346
+ _iterator5,
1347
+ _step5,
1348
+ order,
1349
+ normalizedOrder,
1350
+ pendingOrder,
1351
+ storageKey,
1352
+ mergeActions,
1353
+ updatedList,
1354
+ identityKeyToIndex,
1355
+ index,
1356
+ _iterator6,
1357
+ _step6,
1358
+ key,
1359
+ _iterator7,
1360
+ _step7,
1361
+ _step7$value,
1362
+ _storageKey,
1363
+ _pendingOrder,
1364
+ matchIndex,
1365
+ existingOrder,
1366
+ mergedOrder,
1367
+ nextOrder,
1368
+ pendingSnapshot,
1369
+ insertIndex,
1370
+ patchedOrders,
1371
+ previousList,
1372
+ _args13 = arguments;
1373
+ return _regeneratorRuntime().wrap(function _callee13$(_context13) {
1374
+ while (1) switch (_context13.prev = _context13.next) {
985
1375
  case 0:
1376
+ options = _args13.length > 1 && _args13[1] !== undefined ? _args13[1] : {};
986
1377
  if (pendingOrders !== null && pendingOrders !== void 0 && pendingOrders.length) {
987
- _context11.next = 3;
1378
+ _context13.next = 4;
988
1379
  break;
989
1380
  }
990
1381
  this.logInfo('upsertPendingSyncOrders-订单列表为空', {});
991
- return _context11.abrupt("return");
992
- case 3:
1382
+ return _context13.abrupt("return");
1383
+ case 4:
993
1384
  pendingMap = new Map();
994
1385
  memoryPendingMap = new Map();
995
- _iterator4 = _createForOfIteratorHelper(pendingOrders);
996
- _context11.prev = 6;
997
- _iterator4.s();
998
- case 8:
999
- if ((_step4 = _iterator4.n()).done) {
1000
- _context11.next = 17;
1386
+ _iterator5 = _createForOfIteratorHelper(pendingOrders);
1387
+ _context13.prev = 7;
1388
+ _iterator5.s();
1389
+ case 9:
1390
+ if ((_step5 = _iterator5.n()).done) {
1391
+ _context13.next = 20;
1001
1392
  break;
1002
1393
  }
1003
- order = _step4.value;
1004
- storageKey = this.getOrderStorageKey(order);
1394
+ order = _step5.value;
1395
+ normalizedOrder = this.normalizeOrderCollectionsForIngress(order, 'upsertPendingSyncOrders.incoming');
1396
+ pendingOrder = _objectSpread(_objectSpread({}, normalizedOrder), {}, {
1397
+ need_sync: 1,
1398
+ is_draft_order: 0
1399
+ });
1400
+ storageKey = this.getOrderStorageKey(pendingOrder);
1005
1401
  if (storageKey) {
1006
- _context11.next = 13;
1402
+ _context13.next = 16;
1007
1403
  break;
1008
1404
  }
1009
- return _context11.abrupt("continue", 15);
1010
- case 13:
1011
- pendingMap.set(storageKey, order);
1012
- memoryPendingMap.set(storageKey, this.normalizeOrderForMemoryList(order));
1013
- case 15:
1014
- _context11.next = 8;
1405
+ return _context13.abrupt("continue", 18);
1406
+ case 16:
1407
+ pendingMap.set(storageKey, pendingOrder);
1408
+ memoryPendingMap.set(storageKey, this.normalizeOrderForMemoryList(pendingOrder, 'upsertPendingSyncOrders.memory'));
1409
+ case 18:
1410
+ _context13.next = 9;
1015
1411
  break;
1016
- case 17:
1017
- _context11.next = 22;
1412
+ case 20:
1413
+ _context13.next = 25;
1018
1414
  break;
1019
- case 19:
1020
- _context11.prev = 19;
1021
- _context11.t0 = _context11["catch"](6);
1022
- _iterator4.e(_context11.t0);
1023
1415
  case 22:
1024
- _context11.prev = 22;
1025
- _iterator4.f();
1026
- return _context11.finish(22);
1416
+ _context13.prev = 22;
1417
+ _context13.t0 = _context13["catch"](7);
1418
+ _iterator5.e(_context13.t0);
1027
1419
  case 25:
1420
+ _context13.prev = 25;
1421
+ _iterator5.f();
1422
+ return _context13.finish(25);
1423
+ case 28:
1028
1424
  if (!(pendingMap.size === 0)) {
1029
- _context11.next = 28;
1425
+ _context13.next = 31;
1030
1426
  break;
1031
1427
  }
1032
1428
  this.logError('upsertPendingSyncOrders-订单缺少可落库标识', {
1033
1429
  count: pendingOrders.length
1034
1430
  });
1035
- return _context11.abrupt("return");
1036
- case 28:
1431
+ return _context13.abrupt("return");
1432
+ case 31:
1037
1433
  this.logInfo('upsertPendingSyncOrders-开始', {
1038
1434
  count: pendingMap.size
1039
1435
  });
1040
- patchedOrders = _toConsumableArray(pendingMap.values());
1041
1436
  mergeActions = {};
1042
- updatedList = _toConsumableArray(this.store.list); // 与 mergeOrdersToStore 一致:基于身份键(field:value)建立索引,把「查找已存在订单」
1437
+ updatedList = this.store.list.map(function (order) {
1438
+ return _this10.normalizeOrderForMemoryList(order, 'upsertPendingSyncOrders.existingMemory');
1439
+ }); // 与 mergeOrdersToStore 一致:基于身份键(field:value)建立索引,把「查找已存在订单」
1043
1440
  // 从 O(n) 线性扫描降为 O(1),整体复杂度由 O(n^2) 降为 O(n + m)。
1044
1441
  identityKeyToIndex = new Map();
1045
1442
  for (index = 0; index < updatedList.length; index += 1) {
1046
- _iterator5 = _createForOfIteratorHelper(this.getOrderIdentityMatchKeys(updatedList[index]));
1443
+ _iterator6 = _createForOfIteratorHelper(this.getOrderIdentityMatchKeys(updatedList[index]));
1047
1444
  try {
1048
- for (_iterator5.s(); !(_step5 = _iterator5.n()).done;) {
1049
- key = _step5.value;
1445
+ for (_iterator6.s(); !(_step6 = _iterator6.n()).done;) {
1446
+ key = _step6.value;
1050
1447
  // 同一 key 命中多张时保留最靠前的下标,与原 findIndex「取首个匹配」一致
1051
1448
  if (!identityKeyToIndex.has(key)) identityKeyToIndex.set(key, index);
1052
1449
  }
1053
1450
  } catch (err) {
1054
- _iterator5.e(err);
1451
+ _iterator6.e(err);
1055
1452
  } finally {
1056
- _iterator5.f();
1453
+ _iterator6.f();
1057
1454
  }
1058
1455
  }
1059
- _iterator6 = _createForOfIteratorHelper(memoryPendingMap.entries());
1060
- _context11.prev = 35;
1061
- _iterator6.s();
1062
- case 37:
1063
- if ((_step6 = _iterator6.n()).done) {
1064
- _context11.next = 52;
1456
+ _iterator7 = _createForOfIteratorHelper(memoryPendingMap.entries());
1457
+ _context13.prev = 37;
1458
+ _iterator7.s();
1459
+ case 39:
1460
+ if ((_step7 = _iterator7.n()).done) {
1461
+ _context13.next = 58;
1065
1462
  break;
1066
1463
  }
1067
- _step6$value = _slicedToArray(_step6.value, 2), _storageKey = _step6$value[0], pendingOrder = _step6$value[1];
1068
- matchIndex = this.lookupOrderIndexByIdentityKeys(identityKeyToIndex, this.getOrderIdentityMatchKeys(pendingOrder));
1464
+ _step7$value = _slicedToArray(_step7.value, 2), _storageKey = _step7$value[0], _pendingOrder = _step7$value[1];
1465
+ matchIndex = this.lookupOrderIndexByIdentityKeys(identityKeyToIndex, this.getOrderIdentityMatchKeys(_pendingOrder));
1069
1466
  if (!(matchIndex >= 0)) {
1070
- _context11.next = 46;
1467
+ _context13.next = 52;
1071
1468
  break;
1072
1469
  }
1073
- mergedOrder = this.mergeOrderRecords(updatedList[matchIndex], pendingOrder, {
1074
- preferIncomingProducts: true
1470
+ existingOrder = updatedList[matchIndex];
1471
+ mergedOrder = this.mergeOrderRecords(existingOrder, _pendingOrder);
1472
+ nextOrder = _objectSpread(_objectSpread({}, mergedOrder), {}, {
1473
+ need_sync: 1,
1474
+ is_draft_order: 0
1075
1475
  });
1076
- updatedList[matchIndex] = mergedOrder;
1476
+ updatedList[matchIndex] = nextOrder;
1477
+ pendingSnapshot = pendingMap.get(_storageKey);
1478
+ if (pendingSnapshot) {
1479
+ pendingMap.set(_storageKey, this.reconcileOverwriteOrderCollections(existingOrder, pendingSnapshot));
1480
+ }
1077
1481
  // 合并后身份字段可能新增(如挂单拿到 order_id),同步登记,供同批后续 pending 命中同一槽位
1078
- this.registerOrderIdentityKeys(identityKeyToIndex, mergedOrder, matchIndex);
1482
+ this.registerOrderIdentityKeys(identityKeyToIndex, nextOrder, matchIndex);
1079
1483
  mergeActions[_storageKey] = 'update';
1080
- return _context11.abrupt("continue", 50);
1081
- case 46:
1484
+ return _context13.abrupt("continue", 56);
1485
+ case 52:
1082
1486
  insertIndex = updatedList.length;
1083
- updatedList.push(pendingOrder);
1084
- this.registerOrderIdentityKeys(identityKeyToIndex, pendingOrder, insertIndex);
1487
+ updatedList.push(_pendingOrder);
1488
+ this.registerOrderIdentityKeys(identityKeyToIndex, _pendingOrder, insertIndex);
1085
1489
  mergeActions[_storageKey] = 'insert';
1086
- case 50:
1087
- _context11.next = 37;
1490
+ case 56:
1491
+ _context13.next = 39;
1088
1492
  break;
1089
- case 52:
1090
- _context11.next = 57;
1493
+ case 58:
1494
+ _context13.next = 63;
1091
1495
  break;
1092
- case 54:
1093
- _context11.prev = 54;
1094
- _context11.t1 = _context11["catch"](35);
1095
- _iterator6.e(_context11.t1);
1096
- case 57:
1097
- _context11.prev = 57;
1098
- _iterator6.f();
1099
- return _context11.finish(57);
1100
1496
  case 60:
1101
- // 增量索引已保证 store 去重不变量,无需再对全表做 O(n^2) 压缩;仅自检开关开启时兜底。
1497
+ _context13.prev = 60;
1498
+ _context13.t1 = _context13["catch"](37);
1499
+ _iterator7.e(_context13.t1);
1500
+ case 63:
1501
+ _context13.prev = 63;
1502
+ _iterator7.f();
1503
+ return _context13.finish(63);
1504
+ case 66:
1505
+ patchedOrders = _toConsumableArray(pendingMap.values()); // 增量索引已保证 store 去重不变量,无需再对全表做 O(n^2) 压缩;仅自检开关开启时兜底。
1506
+ previousList = this.store.list;
1102
1507
  this.store.list = this.runOrderStoreSelfCheck(updatedList, 'upsertPendingSyncOrders.store');
1103
1508
  this.syncOrdersMap();
1104
- _context11.next = 64;
1105
- return this.patchOrdersInSQLite(patchedOrders, 'upsertPendingSyncOrders', mergeActions);
1106
- case 64:
1509
+ _context13.prev = 70;
1510
+ _context13.next = 73;
1511
+ return this.patchOrdersInSQLite(patchedOrders, 'upsertPendingSyncOrders', mergeActions, options);
1512
+ case 73:
1513
+ _context13.next = 80;
1514
+ break;
1515
+ case 75:
1516
+ _context13.prev = 75;
1517
+ _context13.t2 = _context13["catch"](70);
1518
+ this.store.list = previousList;
1519
+ this.syncOrdersMap();
1520
+ throw _context13.t2;
1521
+ case 80:
1107
1522
  this.logInfo('upsertPendingSyncOrders-结束', {
1108
1523
  count: patchedOrders.length,
1109
1524
  storeOrderCountAfter: this.store.list.length
1110
1525
  });
1111
- _context11.next = 67;
1526
+ _context13.next = 83;
1112
1527
  return this.emitOrdersChanged(patchedOrders, 'upsertPendingSyncOrders');
1113
- case 67:
1528
+ case 83:
1114
1529
  case "end":
1115
- return _context11.stop();
1530
+ return _context13.stop();
1116
1531
  }
1117
- }, _callee11, this, [[6, 19, 22, 25], [35, 54, 57, 60]]);
1532
+ }, _callee13, this, [[7, 22, 25, 28], [37, 60, 63, 66], [70, 75]]);
1118
1533
  }));
1119
- function upsertPendingSyncOrders(_x7) {
1534
+ function upsertPendingSyncOrders(_x9) {
1120
1535
  return _upsertPendingSyncOrders.apply(this, arguments);
1121
1536
  }
1122
1537
  return upsertPendingSyncOrders;
1123
1538
  }()
1539
+ /**
1540
+ * 将 POS 草稿订单按 SQLite storage key 合并进 store 并落库。
1541
+ * 草稿只表示本地可恢复快照,不进入同步队列。
1542
+ */
1543
+ )
1544
+ }, {
1545
+ key: "upsertLocalDraftOrders",
1546
+ value: (function () {
1547
+ var _upsertLocalDraftOrders = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee14(draftOrders) {
1548
+ var draftMap, memoryDraftMap, _iterator8, _step8, order, draftOrder, storageKey, patchedOrders, mergeActions, updatedList, identityKeyToIndex, index, _iterator9, _step9, key, _iterator10, _step10, _step10$value, _storageKey2, _draftOrder, matchIndex, mergedOrder, nextOrder, insertIndex;
1549
+ return _regeneratorRuntime().wrap(function _callee14$(_context14) {
1550
+ while (1) switch (_context14.prev = _context14.next) {
1551
+ case 0:
1552
+ if (draftOrders !== null && draftOrders !== void 0 && draftOrders.length) {
1553
+ _context14.next = 3;
1554
+ break;
1555
+ }
1556
+ this.logInfo('upsertLocalDraftOrders-订单列表为空', {});
1557
+ return _context14.abrupt("return");
1558
+ case 3:
1559
+ draftMap = new Map();
1560
+ memoryDraftMap = new Map();
1561
+ _iterator8 = _createForOfIteratorHelper(draftOrders);
1562
+ _context14.prev = 6;
1563
+ _iterator8.s();
1564
+ case 8:
1565
+ if ((_step8 = _iterator8.n()).done) {
1566
+ _context14.next = 18;
1567
+ break;
1568
+ }
1569
+ order = _step8.value;
1570
+ draftOrder = _objectSpread(_objectSpread({}, order), {}, {
1571
+ need_sync: 0,
1572
+ is_draft_order: 1
1573
+ });
1574
+ storageKey = this.getOrderStorageKey(draftOrder);
1575
+ if (storageKey) {
1576
+ _context14.next = 14;
1577
+ break;
1578
+ }
1579
+ return _context14.abrupt("continue", 16);
1580
+ case 14:
1581
+ draftMap.set(storageKey, draftOrder);
1582
+ memoryDraftMap.set(storageKey, this.normalizeOrderForMemoryList(draftOrder));
1583
+ case 16:
1584
+ _context14.next = 8;
1585
+ break;
1586
+ case 18:
1587
+ _context14.next = 23;
1588
+ break;
1589
+ case 20:
1590
+ _context14.prev = 20;
1591
+ _context14.t0 = _context14["catch"](6);
1592
+ _iterator8.e(_context14.t0);
1593
+ case 23:
1594
+ _context14.prev = 23;
1595
+ _iterator8.f();
1596
+ return _context14.finish(23);
1597
+ case 26:
1598
+ if (!(draftMap.size === 0)) {
1599
+ _context14.next = 29;
1600
+ break;
1601
+ }
1602
+ this.logError('upsertLocalDraftOrders-订单缺少可落库标识', {
1603
+ count: draftOrders.length
1604
+ });
1605
+ return _context14.abrupt("return");
1606
+ case 29:
1607
+ this.logInfo('upsertLocalDraftOrders-开始', {
1608
+ count: draftMap.size
1609
+ });
1610
+ patchedOrders = _toConsumableArray(draftMap.values());
1611
+ mergeActions = {};
1612
+ updatedList = _toConsumableArray(this.store.list);
1613
+ identityKeyToIndex = new Map();
1614
+ for (index = 0; index < updatedList.length; index += 1) {
1615
+ _iterator9 = _createForOfIteratorHelper(this.getOrderIdentityMatchKeys(updatedList[index]));
1616
+ try {
1617
+ for (_iterator9.s(); !(_step9 = _iterator9.n()).done;) {
1618
+ key = _step9.value;
1619
+ if (!identityKeyToIndex.has(key)) identityKeyToIndex.set(key, index);
1620
+ }
1621
+ } catch (err) {
1622
+ _iterator9.e(err);
1623
+ } finally {
1624
+ _iterator9.f();
1625
+ }
1626
+ }
1627
+ _iterator10 = _createForOfIteratorHelper(memoryDraftMap.entries());
1628
+ _context14.prev = 36;
1629
+ _iterator10.s();
1630
+ case 38:
1631
+ if ((_step10 = _iterator10.n()).done) {
1632
+ _context14.next = 54;
1633
+ break;
1634
+ }
1635
+ _step10$value = _slicedToArray(_step10.value, 2), _storageKey2 = _step10$value[0], _draftOrder = _step10$value[1];
1636
+ matchIndex = this.lookupOrderIndexByIdentityKeys(identityKeyToIndex, this.getOrderIdentityMatchKeys(_draftOrder));
1637
+ if (!(matchIndex >= 0)) {
1638
+ _context14.next = 48;
1639
+ break;
1640
+ }
1641
+ mergedOrder = this.mergeOrderRecords(updatedList[matchIndex], _draftOrder);
1642
+ nextOrder = _objectSpread(_objectSpread({}, mergedOrder), {}, {
1643
+ need_sync: 0,
1644
+ is_draft_order: 1
1645
+ });
1646
+ updatedList[matchIndex] = nextOrder;
1647
+ this.registerOrderIdentityKeys(identityKeyToIndex, nextOrder, matchIndex);
1648
+ mergeActions[_storageKey2] = 'update';
1649
+ return _context14.abrupt("continue", 52);
1650
+ case 48:
1651
+ insertIndex = updatedList.length;
1652
+ updatedList.push(_draftOrder);
1653
+ this.registerOrderIdentityKeys(identityKeyToIndex, _draftOrder, insertIndex);
1654
+ mergeActions[_storageKey2] = 'insert';
1655
+ case 52:
1656
+ _context14.next = 38;
1657
+ break;
1658
+ case 54:
1659
+ _context14.next = 59;
1660
+ break;
1661
+ case 56:
1662
+ _context14.prev = 56;
1663
+ _context14.t1 = _context14["catch"](36);
1664
+ _iterator10.e(_context14.t1);
1665
+ case 59:
1666
+ _context14.prev = 59;
1667
+ _iterator10.f();
1668
+ return _context14.finish(59);
1669
+ case 62:
1670
+ this.store.list = this.runOrderStoreSelfCheck(updatedList, 'upsertLocalDraftOrders.store');
1671
+ this.syncOrdersMap();
1672
+ _context14.next = 66;
1673
+ return this.patchOrdersInSQLite(patchedOrders, 'upsertLocalDraftOrders', mergeActions);
1674
+ case 66:
1675
+ this.logInfo('upsertLocalDraftOrders-结束', {
1676
+ count: patchedOrders.length,
1677
+ storeOrderCountAfter: this.store.list.length
1678
+ });
1679
+ _context14.next = 69;
1680
+ return this.emitOrdersChanged(patchedOrders, 'upsertLocalDraftOrders');
1681
+ case 69:
1682
+ case "end":
1683
+ return _context14.stop();
1684
+ }
1685
+ }, _callee14, this, [[6, 20, 23, 26], [36, 56, 59, 62]]);
1686
+ }));
1687
+ function upsertLocalDraftOrders(_x10) {
1688
+ return _upsertLocalDraftOrders.apply(this, arguments);
1689
+ }
1690
+ return upsertLocalDraftOrders;
1691
+ }()
1124
1692
  /**
1125
1693
  * 通过 SSE 按自定义 query 拉取订单(支持 select/with 精简字段)
1126
1694
  */
@@ -1128,42 +1696,42 @@ export var OrderModule = /*#__PURE__*/function (_BaseModule) {
1128
1696
  }, {
1129
1697
  key: "fetchOrdersBySSE",
1130
1698
  value: (function () {
1131
- var _fetchOrdersBySSE = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee12() {
1699
+ var _fetchOrdersBySSE = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee15() {
1132
1700
  var query,
1133
1701
  data,
1134
- _args12 = arguments;
1135
- return _regeneratorRuntime().wrap(function _callee12$(_context12) {
1136
- while (1) switch (_context12.prev = _context12.next) {
1702
+ _args15 = arguments;
1703
+ return _regeneratorRuntime().wrap(function _callee15$(_context15) {
1704
+ while (1) switch (_context15.prev = _context15.next) {
1137
1705
  case 0:
1138
- query = _args12.length > 0 && _args12[0] !== undefined ? _args12[0] : {};
1706
+ query = _args15.length > 0 && _args15[0] !== undefined ? _args15[0] : {};
1139
1707
  if (this.orderDataSource) {
1140
- _context12.next = 3;
1708
+ _context15.next = 3;
1141
1709
  break;
1142
1710
  }
1143
- return _context12.abrupt("return", []);
1711
+ return _context15.abrupt("return", []);
1144
1712
  case 3:
1145
- _context12.prev = 3;
1146
- _context12.next = 6;
1713
+ _context15.prev = 3;
1714
+ _context15.next = 6;
1147
1715
  return this.orderDataSource.run({
1148
1716
  sse: {
1149
1717
  query: query
1150
1718
  }
1151
1719
  });
1152
1720
  case 6:
1153
- data = _context12.sent;
1154
- return _context12.abrupt("return", data || []);
1721
+ data = _context15.sent;
1722
+ return _context15.abrupt("return", data || []);
1155
1723
  case 10:
1156
- _context12.prev = 10;
1157
- _context12.t0 = _context12["catch"](3);
1724
+ _context15.prev = 10;
1725
+ _context15.t0 = _context15["catch"](3);
1158
1726
  this.logError('fetchOrdersBySSE-失败', {
1159
- error: _context12.t0 instanceof Error ? _context12.t0.message : String(_context12.t0)
1727
+ error: _context15.t0 instanceof Error ? _context15.t0.message : String(_context15.t0)
1160
1728
  });
1161
- return _context12.abrupt("return", []);
1729
+ return _context15.abrupt("return", []);
1162
1730
  case 14:
1163
1731
  case "end":
1164
- return _context12.stop();
1732
+ return _context15.stop();
1165
1733
  }
1166
- }, _callee12, this, [[3, 10]]);
1734
+ }, _callee15, this, [[3, 10]]);
1167
1735
  }));
1168
1736
  function fetchOrdersBySSE() {
1169
1737
  return _fetchOrdersBySSE.apply(this, arguments);
@@ -1192,19 +1760,19 @@ export var OrderModule = /*#__PURE__*/function (_BaseModule) {
1192
1760
  key: "syncOrdersMap",
1193
1761
  value: function syncOrdersMap() {
1194
1762
  this.store.map.clear();
1195
- var _iterator7 = _createForOfIteratorHelper(this.store.list),
1196
- _step7;
1763
+ var _iterator11 = _createForOfIteratorHelper(this.store.list),
1764
+ _step11;
1197
1765
  try {
1198
- for (_iterator7.s(); !(_step7 = _iterator7.n()).done;) {
1199
- var order = _step7.value;
1766
+ for (_iterator11.s(); !(_step11 = _iterator11.n()).done;) {
1767
+ var order = _step11.value;
1200
1768
  var primaryIdentity = this.getOrderMapKey(order);
1201
1769
  if (!primaryIdentity) continue;
1202
1770
  this.store.map.set(primaryIdentity, order);
1203
1771
  }
1204
1772
  } catch (err) {
1205
- _iterator7.e(err);
1773
+ _iterator11.e(err);
1206
1774
  } finally {
1207
- _iterator7.f();
1775
+ _iterator11.f();
1208
1776
  }
1209
1777
  }
1210
1778
  }, {
@@ -1228,7 +1796,7 @@ export var OrderModule = /*#__PURE__*/function (_BaseModule) {
1228
1796
  }, {
1229
1797
  key: "getOrderIdentityEntries",
1230
1798
  value: function getOrderIdentityEntries(order) {
1231
- var _this10 = this;
1799
+ var _this11 = this;
1232
1800
  if (!order) return [];
1233
1801
  var record = order;
1234
1802
  var candidates = [{
@@ -1246,9 +1814,9 @@ export var OrderModule = /*#__PURE__*/function (_BaseModule) {
1246
1814
  }];
1247
1815
  var entries = [];
1248
1816
  var _loop = function _loop() {
1249
- var candidate = _candidates[_i];
1250
- if (_this10.isBlankIdentityValue(candidate.value)) return 0; // continue
1251
- var key = _this10.getIdKey(candidate.value);
1817
+ var candidate = _candidates[_i2];
1818
+ if (_this11.isBlankIdentityValue(candidate.value)) return 0; // continue
1819
+ var key = _this11.getIdKey(candidate.value);
1252
1820
  if (entries.some(function (entry) {
1253
1821
  return entry.field === candidate.field && entry.key === key;
1254
1822
  })) return 0; // continue
@@ -1258,7 +1826,7 @@ export var OrderModule = /*#__PURE__*/function (_BaseModule) {
1258
1826
  });
1259
1827
  },
1260
1828
  _ret;
1261
- for (var _i = 0, _candidates = candidates; _i < _candidates.length; _i++) {
1829
+ for (var _i2 = 0, _candidates = candidates; _i2 < _candidates.length; _i2++) {
1262
1830
  _ret = _loop();
1263
1831
  if (_ret === 0) continue;
1264
1832
  }
@@ -1268,17 +1836,17 @@ export var OrderModule = /*#__PURE__*/function (_BaseModule) {
1268
1836
  key: "getOrderIdentityKeys",
1269
1837
  value: function getOrderIdentityKeys(order) {
1270
1838
  var keys = [];
1271
- var _iterator8 = _createForOfIteratorHelper(this.getOrderIdentityEntries(order)),
1272
- _step8;
1839
+ var _iterator12 = _createForOfIteratorHelper(this.getOrderIdentityEntries(order)),
1840
+ _step12;
1273
1841
  try {
1274
- for (_iterator8.s(); !(_step8 = _iterator8.n()).done;) {
1275
- var entry = _step8.value;
1842
+ for (_iterator12.s(); !(_step12 = _iterator12.n()).done;) {
1843
+ var entry = _step12.value;
1276
1844
  if (!keys.includes(entry.key)) keys.push(entry.key);
1277
1845
  }
1278
1846
  } catch (err) {
1279
- _iterator8.e(err);
1847
+ _iterator12.e(err);
1280
1848
  } finally {
1281
- _iterator8.f();
1849
+ _iterator12.f();
1282
1850
  }
1283
1851
  return keys;
1284
1852
  }
@@ -1287,8 +1855,8 @@ export var OrderModule = /*#__PURE__*/function (_BaseModule) {
1287
1855
  value: function getOrderDateKey(order) {
1288
1856
  var _order$bookings, _order$bookings2;
1289
1857
  var candidates = [order === null || order === void 0 ? void 0 : order.schedule_date, order === null || order === void 0 ? void 0 : order.create_date, order === null || order === void 0 ? void 0 : order.created_at, order === null || order === void 0 || (_order$bookings = order.bookings) === null || _order$bookings === void 0 || (_order$bookings = _order$bookings[0]) === null || _order$bookings === void 0 ? void 0 : _order$bookings.start_date, order === null || order === void 0 || (_order$bookings2 = order.bookings) === null || _order$bookings2 === void 0 || (_order$bookings2 = _order$bookings2[0]) === null || _order$bookings2 === void 0 ? void 0 : _order$bookings2.select_date];
1290
- for (var _i2 = 0, _candidates2 = candidates; _i2 < _candidates2.length; _i2++) {
1291
- var v = _candidates2[_i2];
1858
+ for (var _i3 = 0, _candidates2 = candidates; _i3 < _candidates2.length; _i3++) {
1859
+ var v = _candidates2[_i3];
1292
1860
  if (!v) continue;
1293
1861
  var text = String(v);
1294
1862
  if (!text) continue;
@@ -1315,7 +1883,7 @@ export var OrderModule = /*#__PURE__*/function (_BaseModule) {
1315
1883
  }, {
1316
1884
  key: "setupOrderSync",
1317
1885
  value: function setupOrderSync() {
1318
- var _this11 = this;
1886
+ var _this12 = this;
1319
1887
  if (!this.orderDataSource) return;
1320
1888
  this.orderDataSource.run({
1321
1889
  pubsub: {
@@ -1323,22 +1891,22 @@ export var OrderModule = /*#__PURE__*/function (_BaseModule) {
1323
1891
  var _message$id, _message$order_id, _message$type;
1324
1892
  console.log('orderDataSource', res);
1325
1893
  var pubsubReceivedAt = Date.now();
1326
- var message = _this11.normalizeOrderSyncMessage(res);
1894
+ var message = _this12.normalizeOrderSyncMessage(res);
1327
1895
  if (!message) return;
1328
1896
  message._pubsubReceivedAt = pubsubReceivedAt;
1329
- _this11.pendingSyncMessages.push(message);
1330
- var throttleMs = _this11.getOrderSyncThrottleMs();
1331
- var routeInfo = _this11.resolveSyncMessageRoute(message);
1332
- _this11.logInfo('orderSync-收到消息并入队', {
1897
+ _this12.pendingSyncMessages.push(message);
1898
+ var throttleMs = _this12.getOrderSyncThrottleMs();
1899
+ var routeInfo = _this12.resolveSyncMessageRoute(message);
1900
+ _this12.logInfo('orderSync-收到消息并入队', {
1333
1901
  id: (_message$id = message.id) !== null && _message$id !== void 0 ? _message$id : null,
1334
1902
  order_id: (_message$order_id = message.order_id) !== null && _message$order_id !== void 0 ? _message$order_id : null,
1335
1903
  type: (_message$type = message.type) !== null && _message$type !== void 0 ? _message$type : null,
1336
- pendingCount: _this11.pendingSyncMessages.length,
1904
+ pendingCount: _this12.pendingSyncMessages.length,
1337
1905
  throttleMs: throttleMs,
1338
1906
  pubsubReceivedAt: new Date(pubsubReceivedAt).toISOString(),
1339
1907
  fullMessage: JSON.stringify(message)
1340
1908
  });
1341
- _this11.scheduleOrderSyncThrottle();
1909
+ _this12.scheduleOrderSyncThrottle();
1342
1910
  }
1343
1911
  }
1344
1912
  }).catch(function () {
@@ -1355,7 +1923,7 @@ export var OrderModule = /*#__PURE__*/function (_BaseModule) {
1355
1923
  }, {
1356
1924
  key: "scheduleOrderSyncThrottle",
1357
1925
  value: function scheduleOrderSyncThrottle() {
1358
- var _this12 = this;
1926
+ var _this13 = this;
1359
1927
  if (this.isProcessingSyncBatch) return;
1360
1928
  if (this.isIdlePhase) {
1361
1929
  this.isIdlePhase = false;
@@ -1366,8 +1934,8 @@ export var OrderModule = /*#__PURE__*/function (_BaseModule) {
1366
1934
  if (this.syncTimer) return;
1367
1935
  var throttleMs = this.getOrderSyncThrottleMs();
1368
1936
  this.syncTimer = setTimeout(function () {
1369
- _this12.syncTimer = undefined;
1370
- void _this12.flushOrderSyncMessagesByThrottle();
1937
+ _this13.syncTimer = undefined;
1938
+ void _this13.flushOrderSyncMessagesByThrottle();
1371
1939
  }, throttleMs);
1372
1940
  }
1373
1941
 
@@ -1378,41 +1946,41 @@ export var OrderModule = /*#__PURE__*/function (_BaseModule) {
1378
1946
  }, {
1379
1947
  key: "flushOrderSyncMessagesByThrottle",
1380
1948
  value: (function () {
1381
- var _flushOrderSyncMessagesByThrottle = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee13() {
1382
- return _regeneratorRuntime().wrap(function _callee13$(_context13) {
1383
- while (1) switch (_context13.prev = _context13.next) {
1949
+ var _flushOrderSyncMessagesByThrottle = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee16() {
1950
+ return _regeneratorRuntime().wrap(function _callee16$(_context16) {
1951
+ while (1) switch (_context16.prev = _context16.next) {
1384
1952
  case 0:
1385
1953
  if (!this.isProcessingSyncBatch) {
1386
- _context13.next = 2;
1954
+ _context16.next = 2;
1387
1955
  break;
1388
1956
  }
1389
- return _context13.abrupt("return");
1957
+ return _context16.abrupt("return");
1390
1958
  case 2:
1391
1959
  if (!(this.pendingSyncMessages.length === 0)) {
1392
- _context13.next = 5;
1960
+ _context16.next = 5;
1393
1961
  break;
1394
1962
  }
1395
1963
  this.isIdlePhase = true;
1396
- return _context13.abrupt("return");
1964
+ return _context16.abrupt("return");
1397
1965
  case 5:
1398
1966
  this.isProcessingSyncBatch = true;
1399
- _context13.prev = 6;
1400
- _context13.next = 9;
1967
+ _context16.prev = 6;
1968
+ _context16.next = 9;
1401
1969
  return this.processOrderSyncMessages();
1402
1970
  case 9:
1403
- _context13.prev = 9;
1971
+ _context16.prev = 9;
1404
1972
  this.isProcessingSyncBatch = false;
1405
1973
  if (this.pendingSyncMessages.length > 0) {
1406
1974
  this.scheduleOrderSyncThrottle();
1407
1975
  } else {
1408
1976
  this.isIdlePhase = true;
1409
1977
  }
1410
- return _context13.finish(9);
1978
+ return _context16.finish(9);
1411
1979
  case 13:
1412
1980
  case "end":
1413
- return _context13.stop();
1981
+ return _context16.stop();
1414
1982
  }
1415
- }, _callee13, this, [[6,, 9, 13]]);
1983
+ }, _callee16, this, [[6,, 9, 13]]);
1416
1984
  }));
1417
1985
  function flushOrderSyncMessagesByThrottle() {
1418
1986
  return _flushOrderSyncMessagesByThrottle.apply(this, arguments);
@@ -1434,19 +2002,19 @@ export var OrderModule = /*#__PURE__*/function (_BaseModule) {
1434
2002
  }, {
1435
2003
  key: "processOrderSyncMessages",
1436
2004
  value: (function () {
1437
- var _processOrderSyncMessages = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee14() {
1438
- var _this13 = this;
2005
+ var _processOrderSyncMessages = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee17() {
2006
+ var _this14 = this;
1439
2007
  var messages, batchProcessStartAt, earliestReceivedAt, upsertOrders, refreshIds, uniqueRefreshIds, upsertList, _this$filterRedundant, toFetch, skipped, freshOrders, batchProcessEndAt;
1440
- return _regeneratorRuntime().wrap(function _callee14$(_context14) {
1441
- while (1) switch (_context14.prev = _context14.next) {
2008
+ return _regeneratorRuntime().wrap(function _callee17$(_context17) {
2009
+ while (1) switch (_context17.prev = _context17.next) {
1442
2010
  case 0:
1443
2011
  messages = _toConsumableArray(this.pendingSyncMessages);
1444
2012
  this.pendingSyncMessages = [];
1445
2013
  if (!(messages.length === 0)) {
1446
- _context14.next = 4;
2014
+ _context17.next = 4;
1447
2015
  break;
1448
2016
  }
1449
- return _context14.abrupt("return");
2017
+ return _context17.abrupt("return");
1450
2018
  case 4:
1451
2019
  batchProcessStartAt = Date.now();
1452
2020
  earliestReceivedAt = Math.min.apply(Math, _toConsumableArray(messages.map(function (m) {
@@ -1462,13 +2030,13 @@ export var OrderModule = /*#__PURE__*/function (_BaseModule) {
1462
2030
  upsertOrders = new Map();
1463
2031
  refreshIds = [];
1464
2032
  messages.forEach(function (msg, msgIndex) {
1465
- var routeInfo = _this13.resolveSyncMessageRoute(msg);
2033
+ var routeInfo = _this14.resolveSyncMessageRoute(msg);
1466
2034
  var hasBatchIds = routeInfo.hasBatchIds;
1467
2035
  var singleId = routeInfo.singleId;
1468
2036
  var hasSingleId = routeInfo.hasSingleId;
1469
- var normalizedBody = _this13.normalizeOrderSyncPayload(msg.body || msg.data);
2037
+ var normalizedBody = _this14.normalizeOrderSyncPayload(msg.body || msg.data);
1470
2038
  if (routeInfo.route === 'bodyUpsert' && normalizedBody) {
1471
- upsertOrders.set(_this13.getIdKey(normalizedBody.order_id), normalizedBody);
2039
+ upsertOrders.set(_this14.getIdKey(normalizedBody.order_id), normalizedBody);
1472
2040
  return;
1473
2041
  }
1474
2042
  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));
@@ -1480,41 +2048,41 @@ export var OrderModule = /*#__PURE__*/function (_BaseModule) {
1480
2048
  refreshIdCount: uniqueRefreshIds.length
1481
2049
  });
1482
2050
  if (!(upsertList.length > 0)) {
1483
- _context14.next = 16;
2051
+ _context17.next = 16;
1484
2052
  break;
1485
2053
  }
1486
- _context14.next = 16;
2054
+ _context17.next = 16;
1487
2055
  return this.mergeOrdersToStore(upsertList, 'pubsub.bodyUpsert');
1488
2056
  case 16:
1489
2057
  if (!(uniqueRefreshIds.length > 0)) {
1490
- _context14.next = 28;
2058
+ _context17.next = 28;
1491
2059
  break;
1492
2060
  }
1493
2061
  _this$filterRedundant = this.filterRedundantHttpRefreshIds(uniqueRefreshIds), toFetch = _this$filterRedundant.toFetch, skipped = _this$filterRedundant.skipped;
1494
2062
  if (skipped.length > 0) {}
1495
2063
  if (!(toFetch.length > 0)) {
1496
- _context14.next = 28;
2064
+ _context17.next = 28;
1497
2065
  break;
1498
2066
  }
1499
- _context14.next = 22;
2067
+ _context17.next = 22;
1500
2068
  return this.fetchOrdersByHttp(toFetch);
1501
2069
  case 22:
1502
- freshOrders = _context14.sent;
2070
+ freshOrders = _context17.sent;
1503
2071
  if (!(freshOrders.length > 0)) {
1504
- _context14.next = 28;
2072
+ _context17.next = 28;
1505
2073
  break;
1506
2074
  }
1507
- _context14.next = 26;
2075
+ _context17.next = 26;
1508
2076
  return this.mergeOrdersToStore(freshOrders, 'pubsub.httpRefresh');
1509
2077
  case 26:
1510
- _context14.next = 28;
2078
+ _context17.next = 28;
1511
2079
  break;
1512
2080
  case 28:
1513
2081
  if (!(upsertList.length === 0 && uniqueRefreshIds.length === 0)) {
1514
- _context14.next = 30;
2082
+ _context17.next = 30;
1515
2083
  break;
1516
2084
  }
1517
- return _context14.abrupt("return");
2085
+ return _context17.abrupt("return");
1518
2086
  case 30:
1519
2087
  batchProcessEndAt = Date.now();
1520
2088
  this.logInfo('processOrderSyncMessages-结束', {
@@ -1523,13 +2091,13 @@ export var OrderModule = /*#__PURE__*/function (_BaseModule) {
1523
2091
  batchProcessDurationMs: batchProcessEndAt - batchProcessStartAt,
1524
2092
  totalFromPubsubToProcessedMs: batchProcessEndAt - earliestReceivedAt
1525
2093
  });
1526
- _context14.next = 34;
2094
+ _context17.next = 34;
1527
2095
  return this.core.effects.emit(OrderHooks.onOrdersSyncCompleted, null);
1528
2096
  case 34:
1529
2097
  case "end":
1530
- return _context14.stop();
2098
+ return _context17.stop();
1531
2099
  }
1532
- }, _callee14, this);
2100
+ }, _callee17, this);
1533
2101
  }));
1534
2102
  function processOrderSyncMessages() {
1535
2103
  return _processOrderSyncMessages.apply(this, arguments);
@@ -1550,29 +2118,29 @@ export var OrderModule = /*#__PURE__*/function (_BaseModule) {
1550
2118
  }, {
1551
2119
  key: "fetchOrdersByHttp",
1552
2120
  value: (function () {
1553
- var _fetchOrdersByHttp = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee15(ids) {
2121
+ var _fetchOrdersByHttp = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee18(ids) {
1554
2122
  var _orderList, orderList;
1555
- return _regeneratorRuntime().wrap(function _callee15$(_context15) {
1556
- while (1) switch (_context15.prev = _context15.next) {
2123
+ return _regeneratorRuntime().wrap(function _callee18$(_context18) {
2124
+ while (1) switch (_context18.prev = _context18.next) {
1557
2125
  case 0:
1558
2126
  if (!(!this.orderDataSource || ids.length === 0)) {
1559
- _context15.next = 2;
2127
+ _context18.next = 2;
1560
2128
  break;
1561
2129
  }
1562
- return _context15.abrupt("return", []);
2130
+ return _context18.abrupt("return", []);
1563
2131
  case 2:
1564
- _context15.prev = 2;
2132
+ _context18.prev = 2;
1565
2133
  if (!(typeof this.orderDataSource.fetchOrdersByIds === 'function')) {
1566
- _context15.next = 8;
2134
+ _context18.next = 8;
1567
2135
  break;
1568
2136
  }
1569
- _context15.next = 6;
2137
+ _context18.next = 6;
1570
2138
  return this.orderDataSource.fetchOrdersByIds(ids);
1571
2139
  case 6:
1572
- _orderList = _context15.sent;
1573
- return _context15.abrupt("return", _orderList || []);
2140
+ _orderList = _context18.sent;
2141
+ return _context18.abrupt("return", _orderList || []);
1574
2142
  case 8:
1575
- _context15.next = 10;
2143
+ _context18.next = 10;
1576
2144
  return this.orderDataSource.run({
1577
2145
  http: {
1578
2146
  query: {
@@ -1581,19 +2149,19 @@ export var OrderModule = /*#__PURE__*/function (_BaseModule) {
1581
2149
  }
1582
2150
  });
1583
2151
  case 10:
1584
- orderList = _context15.sent;
1585
- return _context15.abrupt("return", orderList || []);
2152
+ orderList = _context18.sent;
2153
+ return _context18.abrupt("return", orderList || []);
1586
2154
  case 14:
1587
- _context15.prev = 14;
1588
- _context15.t0 = _context15["catch"](2);
1589
- return _context15.abrupt("return", []);
2155
+ _context18.prev = 14;
2156
+ _context18.t0 = _context18["catch"](2);
2157
+ return _context18.abrupt("return", []);
1590
2158
  case 17:
1591
2159
  case "end":
1592
- return _context15.stop();
2160
+ return _context18.stop();
1593
2161
  }
1594
- }, _callee15, this, [[2, 14]]);
2162
+ }, _callee18, this, [[2, 14]]);
1595
2163
  }));
1596
- function fetchOrdersByHttp(_x8) {
2164
+ function fetchOrdersByHttp(_x11) {
1597
2165
  return _fetchOrdersByHttp.apply(this, arguments);
1598
2166
  }
1599
2167
  return fetchOrdersByHttp;
@@ -1605,10 +2173,11 @@ export var OrderModule = /*#__PURE__*/function (_BaseModule) {
1605
2173
  }, {
1606
2174
  key: "mergeOrdersToStore",
1607
2175
  value: (function () {
1608
- var _mergeOrdersToStore = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee16(freshOrders, source) {
1609
- var storeOrderCountBefore, normalizedFreshOrders, _iterator9, _step9, order, id, uniqueFreshOrders, uniqueFreshCount, patchedOrders, mergeActions, updatedList, identityKeyToIndex, index, identityKeys, _iterator10, _step10, key, _iterator11, _step11, fresh, freshIdentityKeys, matchIndex, storageKey, mergeKey, existingOrder, _ref2, _existingOrder$order_, _ref3, _external_sale_number, _existingOrder$paymen, _fresh$payment_status, _existingOrder$need_s, _fresh$need_sync, mergedOrder, insertIndex, insertCount, updateCount;
1610
- return _regeneratorRuntime().wrap(function _callee16$(_context16) {
1611
- while (1) switch (_context16.prev = _context16.next) {
2176
+ var _mergeOrdersToStore = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee19(freshOrders, source) {
2177
+ var _this15 = this;
2178
+ var storeOrderCountBefore, normalizedFreshOrders, _iterator13, _step13, order, id, uniqueFreshOrders, uniqueFreshCount, patchedOrders, mergeActions, updatedList, identityKeyToIndex, index, identityKeys, _iterator14, _step14, key, _iterator15, _step15, fresh, freshIdentityKeys, matchIndex, storageKey, mergeKey, existingOrder, _ref2, _existingOrder$order_, _ref3, _external_sale_number2, _existingOrder$paymen, _fresh$payment_status, _existingOrder$need_s, _fresh$need_sync, _ref4, _existingOrder$order_2, _ref5, _external_sale_number3, _existingOrder$is_dra, _fresh$is_draft_order, mergedOrder, adjustedMatchIndex, duplicateIndexes, _iterator16, _step16, _key2, _duplicateIndex, _iterator17, _step17, duplicateIndex, _index, _iterator18, _step18, _key, insertIndex, insertCount, updateCount;
2179
+ return _regeneratorRuntime().wrap(function _callee19$(_context19) {
2180
+ while (1) switch (_context19.prev = _context19.next) {
1612
2181
  case 0:
1613
2182
  storeOrderCountBefore = this.store.list.length;
1614
2183
  this.logInfo('mergeOrdersToStore-开始', {
@@ -1618,125 +2187,206 @@ export var OrderModule = /*#__PURE__*/function (_BaseModule) {
1618
2187
  });
1619
2188
  this.logDuplicateOrders("".concat(source, ".beforeMerge"), this.store.list);
1620
2189
  normalizedFreshOrders = [];
1621
- _iterator9 = _createForOfIteratorHelper(freshOrders);
1622
- _context16.prev = 5;
1623
- _iterator9.s();
2190
+ _iterator13 = _createForOfIteratorHelper(freshOrders);
2191
+ _context19.prev = 5;
2192
+ _iterator13.s();
1624
2193
  case 7:
1625
- if ((_step9 = _iterator9.n()).done) {
1626
- _context16.next = 15;
2194
+ if ((_step13 = _iterator13.n()).done) {
2195
+ _context19.next = 15;
1627
2196
  break;
1628
2197
  }
1629
- order = _step9.value;
2198
+ order = _step13.value;
1630
2199
  id = order === null || order === void 0 ? void 0 : order.order_id;
1631
2200
  if (!(id === undefined || id === null)) {
1632
- _context16.next = 12;
2201
+ _context19.next = 12;
1633
2202
  break;
1634
2203
  }
1635
- return _context16.abrupt("continue", 13);
2204
+ return _context19.abrupt("continue", 13);
1636
2205
  case 12:
1637
2206
  normalizedFreshOrders.push(this.normalizeRemoteSyncedOrder(order));
1638
2207
  case 13:
1639
- _context16.next = 7;
2208
+ _context19.next = 7;
1640
2209
  break;
1641
2210
  case 15:
1642
- _context16.next = 20;
2211
+ _context19.next = 20;
1643
2212
  break;
1644
2213
  case 17:
1645
- _context16.prev = 17;
1646
- _context16.t0 = _context16["catch"](5);
1647
- _iterator9.e(_context16.t0);
2214
+ _context19.prev = 17;
2215
+ _context19.t0 = _context19["catch"](5);
2216
+ _iterator13.e(_context19.t0);
1648
2217
  case 20:
1649
- _context16.prev = 20;
1650
- _iterator9.f();
1651
- return _context16.finish(20);
2218
+ _context19.prev = 20;
2219
+ _iterator13.f();
2220
+ return _context19.finish(20);
1652
2221
  case 23:
1653
2222
  // 先对本批新单做去重(批内规模 m 很小),保证每条 fresh 身份唯一
1654
2223
  uniqueFreshOrders = this.compactOrderListByIdentity(normalizedFreshOrders, "".concat(source, ".incoming")).list;
1655
2224
  uniqueFreshCount = uniqueFreshOrders.length;
1656
2225
  patchedOrders = [];
1657
2226
  mergeActions = {};
1658
- updatedList = _toConsumableArray(this.store.list); // 基于身份键(field:value)建立索引,把「查找已存在订单」从 O(n) 线性扫描降为 O(1),
2227
+ updatedList = this.store.list.map(function (order) {
2228
+ return _this15.normalizeOrderForMemoryList(order, "".concat(source, ".existingMemory"));
2229
+ }); // 基于身份键(field:value)建立索引,把「查找已存在订单」从 O(n) 线性扫描降为 O(1),
1659
2230
  // 整体合并复杂度由 O(n^2) 降为 O(n + m)。索引语义与 doOrdersShareIdentity 完全一致:
1660
2231
  // 任一身份字段(order_id/external_sale_number/order_number/shop_order_number)同值即同一单。
1661
2232
  identityKeyToIndex = new Map();
1662
2233
  for (index = 0; index < updatedList.length; index += 1) {
1663
2234
  identityKeys = this.getOrderIdentityMatchKeys(updatedList[index]);
1664
- _iterator10 = _createForOfIteratorHelper(identityKeys);
2235
+ _iterator14 = _createForOfIteratorHelper(identityKeys);
1665
2236
  try {
1666
- for (_iterator10.s(); !(_step10 = _iterator10.n()).done;) {
1667
- key = _step10.value;
2237
+ for (_iterator14.s(); !(_step14 = _iterator14.n()).done;) {
2238
+ key = _step14.value;
1668
2239
  // 同一 key 命中多张时保留最靠前的下标,与原 findIndex「取首个匹配」一致
1669
2240
  if (!identityKeyToIndex.has(key)) identityKeyToIndex.set(key, index);
1670
2241
  }
1671
2242
  } catch (err) {
1672
- _iterator10.e(err);
2243
+ _iterator14.e(err);
1673
2244
  } finally {
1674
- _iterator10.f();
2245
+ _iterator14.f();
1675
2246
  }
1676
2247
  }
1677
- _iterator11 = _createForOfIteratorHelper(uniqueFreshOrders);
1678
- _context16.prev = 31;
1679
- _iterator11.s();
2248
+ _iterator15 = _createForOfIteratorHelper(uniqueFreshOrders);
2249
+ _context19.prev = 31;
2250
+ _iterator15.s();
1680
2251
  case 33:
1681
- if ((_step11 = _iterator11.n()).done) {
1682
- _context16.next = 57;
2252
+ if ((_step15 = _iterator15.n()).done) {
2253
+ _context19.next = 82;
1683
2254
  break;
1684
2255
  }
1685
- fresh = _step11.value;
2256
+ fresh = _step15.value;
1686
2257
  freshIdentityKeys = this.getOrderIdentityMatchKeys(fresh);
1687
2258
  matchIndex = this.lookupOrderIndexByIdentityKeys(identityKeyToIndex, freshIdentityKeys);
1688
2259
  storageKey = this.getOrderStorageKey(fresh);
1689
2260
  mergeKey = storageKey || (fresh.order_id !== undefined && fresh.order_id !== null ? this.getIdKey(fresh.order_id) : '');
1690
2261
  if (!(matchIndex >= 0)) {
1691
- _context16.next = 50;
2262
+ _context19.next = 75;
1692
2263
  break;
1693
2264
  }
1694
2265
  existingOrder = updatedList[matchIndex];
1695
2266
  if (!(this.isPendingSyncOrder(existingOrder) && this.isRemoteEchoMergeSource(source))) {
1696
- _context16.next = 44;
2267
+ _context19.next = 44;
1697
2268
  break;
1698
2269
  }
1699
2270
  this.logInfo('mergeOrdersToStore-跳过 pending 订单远端覆盖', {
1700
2271
  source: source,
1701
2272
  order_id: (_ref2 = (_existingOrder$order_ = existingOrder.order_id) !== null && _existingOrder$order_ !== void 0 ? _existingOrder$order_ : fresh.order_id) !== null && _ref2 !== void 0 ? _ref2 : null,
1702
- external_sale_number: (_ref3 = (_external_sale_number = existingOrder.external_sale_number) !== null && _external_sale_number !== void 0 ? _external_sale_number : fresh.external_sale_number) !== null && _ref3 !== void 0 ? _ref3 : null,
2273
+ external_sale_number: (_ref3 = (_external_sale_number2 = existingOrder.external_sale_number) !== null && _external_sale_number2 !== void 0 ? _external_sale_number2 : fresh.external_sale_number) !== null && _ref3 !== void 0 ? _ref3 : null,
1703
2274
  local_payment_status: (_existingOrder$paymen = existingOrder.payment_status) !== null && _existingOrder$paymen !== void 0 ? _existingOrder$paymen : null,
1704
2275
  remote_payment_status: (_fresh$payment_status = fresh.payment_status) !== null && _fresh$payment_status !== void 0 ? _fresh$payment_status : null,
1705
2276
  local_need_sync: (_existingOrder$need_s = existingOrder.need_sync) !== null && _existingOrder$need_s !== void 0 ? _existingOrder$need_s : null,
1706
2277
  remote_need_sync: (_fresh$need_sync = fresh.need_sync) !== null && _fresh$need_sync !== void 0 ? _fresh$need_sync : null,
1707
2278
  reason: 'protect_pending_sync_order_from_remote_echo'
1708
2279
  });
1709
- return _context16.abrupt("continue", 55);
2280
+ return _context19.abrupt("continue", 80);
1710
2281
  case 44:
2282
+ if (!(this.isDraftOrder(existingOrder) && this.shouldProtectDraftOrderFromIncoming(source))) {
2283
+ _context19.next = 47;
2284
+ break;
2285
+ }
2286
+ this.logInfo('mergeOrdersToStore-跳过 draft 订单远端覆盖', {
2287
+ source: source,
2288
+ order_id: (_ref4 = (_existingOrder$order_2 = existingOrder.order_id) !== null && _existingOrder$order_2 !== void 0 ? _existingOrder$order_2 : fresh.order_id) !== null && _ref4 !== void 0 ? _ref4 : null,
2289
+ external_sale_number: (_ref5 = (_external_sale_number3 = existingOrder.external_sale_number) !== null && _external_sale_number3 !== void 0 ? _external_sale_number3 : fresh.external_sale_number) !== null && _ref5 !== void 0 ? _ref5 : null,
2290
+ local_is_draft_order: (_existingOrder$is_dra = existingOrder.is_draft_order) !== null && _existingOrder$is_dra !== void 0 ? _existingOrder$is_dra : null,
2291
+ remote_is_draft_order: (_fresh$is_draft_order = fresh.is_draft_order) !== null && _fresh$is_draft_order !== void 0 ? _fresh$is_draft_order : null,
2292
+ reason: 'protect_draft_order_from_remote_echo'
2293
+ });
2294
+ return _context19.abrupt("continue", 80);
2295
+ case 47:
1711
2296
  mergedOrder = this.mergeOrderRecords(existingOrder, fresh);
1712
- updatedList[matchIndex] = mergedOrder;
2297
+ adjustedMatchIndex = matchIndex;
2298
+ duplicateIndexes = new Set();
2299
+ _iterator16 = _createForOfIteratorHelper(freshIdentityKeys);
2300
+ _context19.prev = 51;
2301
+ _iterator16.s();
2302
+ case 53:
2303
+ if ((_step16 = _iterator16.n()).done) {
2304
+ _context19.next = 61;
2305
+ break;
2306
+ }
2307
+ _key2 = _step16.value;
2308
+ _duplicateIndex = identityKeyToIndex.get(_key2);
2309
+ if (!(_duplicateIndex === undefined || _duplicateIndex === matchIndex)) {
2310
+ _context19.next = 58;
2311
+ break;
2312
+ }
2313
+ return _context19.abrupt("continue", 59);
2314
+ case 58:
2315
+ duplicateIndexes.add(_duplicateIndex);
2316
+ case 59:
2317
+ _context19.next = 53;
2318
+ break;
2319
+ case 61:
2320
+ _context19.next = 66;
2321
+ break;
2322
+ case 63:
2323
+ _context19.prev = 63;
2324
+ _context19.t1 = _context19["catch"](51);
2325
+ _iterator16.e(_context19.t1);
2326
+ case 66:
2327
+ _context19.prev = 66;
2328
+ _iterator16.f();
2329
+ return _context19.finish(66);
2330
+ case 69:
2331
+ if (duplicateIndexes.size > 0) {
2332
+ _iterator17 = _createForOfIteratorHelper(_toConsumableArray(duplicateIndexes).sort(function (left, right) {
2333
+ return right - left;
2334
+ }));
2335
+ try {
2336
+ for (_iterator17.s(); !(_step17 = _iterator17.n()).done;) {
2337
+ duplicateIndex = _step17.value;
2338
+ mergedOrder = this.mergeOrderRecords(mergedOrder, updatedList[duplicateIndex]);
2339
+ updatedList.splice(duplicateIndex, 1);
2340
+ if (duplicateIndex < adjustedMatchIndex) adjustedMatchIndex -= 1;
2341
+ }
2342
+ } catch (err) {
2343
+ _iterator17.e(err);
2344
+ } finally {
2345
+ _iterator17.f();
2346
+ }
2347
+ identityKeyToIndex.clear();
2348
+ for (_index = 0; _index < updatedList.length; _index += 1) {
2349
+ _iterator18 = _createForOfIteratorHelper(this.getOrderIdentityMatchKeys(updatedList[_index]));
2350
+ try {
2351
+ for (_iterator18.s(); !(_step18 = _iterator18.n()).done;) {
2352
+ _key = _step18.value;
2353
+ if (!identityKeyToIndex.has(_key)) identityKeyToIndex.set(_key, _index);
2354
+ }
2355
+ } catch (err) {
2356
+ _iterator18.e(err);
2357
+ } finally {
2358
+ _iterator18.f();
2359
+ }
2360
+ }
2361
+ }
2362
+ updatedList[adjustedMatchIndex] = mergedOrder;
1713
2363
  // 合并后身份字段可能新增(如挂单拿到 order_id),同步登记到索引,
1714
2364
  // 供同批后续 fresh 命中同一槽位,避免重复插入
1715
- this.registerOrderIdentityKeys(identityKeyToIndex, mergedOrder, matchIndex);
2365
+ this.registerOrderIdentityKeys(identityKeyToIndex, mergedOrder, adjustedMatchIndex);
1716
2366
  patchedOrders.push(mergedOrder);
1717
2367
  if (mergeKey) mergeActions[mergeKey] = 'update';
1718
- return _context16.abrupt("continue", 55);
1719
- case 50:
2368
+ return _context19.abrupt("continue", 80);
2369
+ case 75:
1720
2370
  insertIndex = updatedList.length;
1721
2371
  updatedList.push(fresh);
1722
2372
  this.registerOrderIdentityKeys(identityKeyToIndex, fresh, insertIndex);
1723
2373
  patchedOrders.push(fresh);
1724
2374
  if (mergeKey) mergeActions[mergeKey] = 'insert';
1725
- case 55:
1726
- _context16.next = 33;
2375
+ case 80:
2376
+ _context19.next = 33;
1727
2377
  break;
1728
- case 57:
1729
- _context16.next = 62;
2378
+ case 82:
2379
+ _context19.next = 87;
1730
2380
  break;
1731
- case 59:
1732
- _context16.prev = 59;
1733
- _context16.t1 = _context16["catch"](31);
1734
- _iterator11.e(_context16.t1);
1735
- case 62:
1736
- _context16.prev = 62;
1737
- _iterator11.f();
1738
- return _context16.finish(62);
1739
- case 65:
2381
+ case 84:
2382
+ _context19.prev = 84;
2383
+ _context19.t2 = _context19["catch"](31);
2384
+ _iterator15.e(_context19.t2);
2385
+ case 87:
2386
+ _context19.prev = 87;
2387
+ _iterator15.f();
2388
+ return _context19.finish(87);
2389
+ case 90:
1740
2390
  insertCount = Object.values(mergeActions).filter(function (v) {
1741
2391
  return v === 'insert';
1742
2392
  }).length;
@@ -1752,24 +2402,24 @@ export var OrderModule = /*#__PURE__*/function (_BaseModule) {
1752
2402
  // 仅在自检开关开启时做一次兜底校验。
1753
2403
  this.store.list = this.runOrderStoreSelfCheck(updatedList, "".concat(source, ".store"));
1754
2404
  this.syncOrdersMap();
1755
- _context16.next = 72;
2405
+ _context19.next = 97;
1756
2406
  return this.patchOrdersInSQLite(patchedOrders, source, mergeActions);
1757
- case 72:
2407
+ case 97:
1758
2408
  this.logInfo('mergeOrdersToStore-结束', {
1759
2409
  uniqueFreshCount: uniqueFreshCount,
1760
2410
  storeOrderCountAfter: this.store.list.length,
1761
2411
  insertCount: insertCount,
1762
2412
  updateCount: updateCount
1763
2413
  });
1764
- _context16.next = 75;
2414
+ _context19.next = 100;
1765
2415
  return this.emitOrdersChanged(patchedOrders, source);
1766
- case 75:
2416
+ case 100:
1767
2417
  case "end":
1768
- return _context16.stop();
2418
+ return _context19.stop();
1769
2419
  }
1770
- }, _callee16, this, [[5, 17, 20, 23], [31, 59, 62, 65]]);
2420
+ }, _callee19, this, [[5, 17, 20, 23], [31, 84, 87, 90], [51, 63, 66, 69]]);
1771
2421
  }));
1772
- function mergeOrdersToStore(_x9, _x10) {
2422
+ function mergeOrdersToStore(_x12, _x13) {
1773
2423
  return _mergeOrdersToStore.apply(this, arguments);
1774
2424
  }
1775
2425
  return mergeOrdersToStore;
@@ -1786,19 +2436,19 @@ export var OrderModule = /*#__PURE__*/function (_BaseModule) {
1786
2436
  key: "lookupOrderIndexByIdentityKeys",
1787
2437
  value: function lookupOrderIndexByIdentityKeys(identityKeyToIndex, identityKeys) {
1788
2438
  var matchIndex = -1;
1789
- var _iterator12 = _createForOfIteratorHelper(identityKeys),
1790
- _step12;
2439
+ var _iterator19 = _createForOfIteratorHelper(identityKeys),
2440
+ _step19;
1791
2441
  try {
1792
- for (_iterator12.s(); !(_step12 = _iterator12.n()).done;) {
1793
- var key = _step12.value;
2442
+ for (_iterator19.s(); !(_step19 = _iterator19.n()).done;) {
2443
+ var key = _step19.value;
1794
2444
  var index = identityKeyToIndex.get(key);
1795
2445
  if (index === undefined) continue;
1796
2446
  if (matchIndex < 0 || index < matchIndex) matchIndex = index;
1797
2447
  }
1798
2448
  } catch (err) {
1799
- _iterator12.e(err);
2449
+ _iterator19.e(err);
1800
2450
  } finally {
1801
- _iterator12.f();
2451
+ _iterator19.f();
1802
2452
  }
1803
2453
  return matchIndex;
1804
2454
  }
@@ -1819,17 +2469,17 @@ export var OrderModule = /*#__PURE__*/function (_BaseModule) {
1819
2469
  key: "registerOrderIdentityKeys",
1820
2470
  value: function registerOrderIdentityKeys(identityKeyToIndex, order, index) {
1821
2471
  var identityKeys = this.getOrderIdentityMatchKeys(order);
1822
- var _iterator13 = _createForOfIteratorHelper(identityKeys),
1823
- _step13;
2472
+ var _iterator20 = _createForOfIteratorHelper(identityKeys),
2473
+ _step20;
1824
2474
  try {
1825
- for (_iterator13.s(); !(_step13 = _iterator13.n()).done;) {
1826
- var key = _step13.value;
2475
+ for (_iterator20.s(); !(_step20 = _iterator20.n()).done;) {
2476
+ var key = _step20.value;
1827
2477
  identityKeyToIndex.set(key, index);
1828
2478
  }
1829
2479
  } catch (err) {
1830
- _iterator13.e(err);
2480
+ _iterator20.e(err);
1831
2481
  } finally {
1832
- _iterator13.f();
2482
+ _iterator20.f();
1833
2483
  }
1834
2484
  }
1835
2485
 
@@ -1884,17 +2534,17 @@ export var OrderModule = /*#__PURE__*/function (_BaseModule) {
1884
2534
  key: "uniqueOrderIds",
1885
2535
  value: function uniqueOrderIds(ids) {
1886
2536
  var idMap = new Map();
1887
- var _iterator14 = _createForOfIteratorHelper(ids),
1888
- _step14;
2537
+ var _iterator21 = _createForOfIteratorHelper(ids),
2538
+ _step21;
1889
2539
  try {
1890
- for (_iterator14.s(); !(_step14 = _iterator14.n()).done;) {
1891
- var id = _step14.value;
2540
+ for (_iterator21.s(); !(_step21 = _iterator21.n()).done;) {
2541
+ var id = _step21.value;
1892
2542
  idMap.set(this.getIdKey(id), id);
1893
2543
  }
1894
2544
  } catch (err) {
1895
- _iterator14.e(err);
2545
+ _iterator21.e(err);
1896
2546
  } finally {
1897
- _iterator14.f();
2547
+ _iterator21.f();
1898
2548
  }
1899
2549
  return _toConsumableArray(idMap.values());
1900
2550
  }
@@ -1906,12 +2556,12 @@ export var OrderModule = /*#__PURE__*/function (_BaseModule) {
1906
2556
  }, {
1907
2557
  key: "isOrderLookupMatch",
1908
2558
  value: function isOrderLookupMatch(order, lookupKey) {
1909
- var _this14 = this;
2559
+ var _this16 = this;
1910
2560
  if (!order || !lookupKey) return false;
1911
2561
  var candidates = [order.order_id, order.external_sale_number, order.order_number, order.shop_order_number, order.shop_full_order_number];
1912
2562
  return candidates.some(function (value) {
1913
2563
  if (value === undefined || value === null || value === '') return false;
1914
- return _this14.getIdKey(value) === lookupKey;
2564
+ return _this16.getIdKey(value) === lookupKey;
1915
2565
  });
1916
2566
  }
1917
2567
 
@@ -1954,10 +2604,10 @@ export var OrderModule = /*#__PURE__*/function (_BaseModule) {
1954
2604
  }, {
1955
2605
  key: "findOrderIndexByIdentity",
1956
2606
  value: function findOrderIndexByIdentity(orders, target) {
1957
- var _this15 = this;
2607
+ var _this17 = this;
1958
2608
  if (this.getOrderIdentityMatchKeys(target).length === 0) return -1;
1959
2609
  return orders.findIndex(function (order) {
1960
- return _this15.doOrdersShareIdentity(order, target);
2610
+ return _this17.doOrdersShareIdentity(order, target);
1961
2611
  });
1962
2612
  }
1963
2613
  }, {
@@ -2006,128 +2656,85 @@ export var OrderModule = /*#__PURE__*/function (_BaseModule) {
2006
2656
  if (secondaryList.length === 0) return preferredList;
2007
2657
  if (preferredList.length === 0) return secondaryList;
2008
2658
  var seenKeys = new Set();
2009
- var _iterator15 = _createForOfIteratorHelper(preferredList),
2010
- _step15;
2659
+ var _iterator22 = _createForOfIteratorHelper(preferredList),
2660
+ _step22;
2011
2661
  try {
2012
- for (_iterator15.s(); !(_step15 = _iterator15.n()).done;) {
2013
- var item = _step15.value;
2662
+ for (_iterator22.s(); !(_step22 = _iterator22.n()).done;) {
2663
+ var item = _step22.value;
2014
2664
  var keys = this.getProductIdentityKeys(item);
2015
- var _iterator17 = _createForOfIteratorHelper(keys),
2016
- _step17;
2665
+ var _iterator24 = _createForOfIteratorHelper(keys),
2666
+ _step24;
2017
2667
  try {
2018
- for (_iterator17.s(); !(_step17 = _iterator17.n()).done;) {
2019
- var key = _step17.value;
2668
+ for (_iterator24.s(); !(_step24 = _iterator24.n()).done;) {
2669
+ var key = _step24.value;
2020
2670
  seenKeys.add(key);
2021
2671
  }
2022
2672
  } catch (err) {
2023
- _iterator17.e(err);
2673
+ _iterator24.e(err);
2024
2674
  } finally {
2025
- _iterator17.f();
2675
+ _iterator24.f();
2026
2676
  }
2027
2677
  }
2028
2678
  } catch (err) {
2029
- _iterator15.e(err);
2679
+ _iterator22.e(err);
2030
2680
  } finally {
2031
- _iterator15.f();
2681
+ _iterator22.f();
2032
2682
  }
2033
2683
  var result = _toConsumableArray(preferredList);
2034
- var _iterator16 = _createForOfIteratorHelper(secondaryList),
2035
- _step16;
2684
+ var _iterator23 = _createForOfIteratorHelper(secondaryList),
2685
+ _step23;
2036
2686
  try {
2037
- for (_iterator16.s(); !(_step16 = _iterator16.n()).done;) {
2038
- var _item = _step16.value;
2687
+ for (_iterator23.s(); !(_step23 = _iterator23.n()).done;) {
2688
+ var _item = _step23.value;
2039
2689
  var _keys = this.getProductIdentityKeys(_item);
2040
2690
  if (_keys.length === 0 || _keys.some(function (key) {
2041
2691
  return seenKeys.has(key);
2042
2692
  })) continue;
2043
- var _iterator18 = _createForOfIteratorHelper(_keys),
2044
- _step18;
2693
+ var _iterator25 = _createForOfIteratorHelper(_keys),
2694
+ _step25;
2045
2695
  try {
2046
- for (_iterator18.s(); !(_step18 = _iterator18.n()).done;) {
2047
- var _key = _step18.value;
2048
- seenKeys.add(_key);
2696
+ for (_iterator25.s(); !(_step25 = _iterator25.n()).done;) {
2697
+ var _key3 = _step25.value;
2698
+ seenKeys.add(_key3);
2049
2699
  }
2050
2700
  } catch (err) {
2051
- _iterator18.e(err);
2701
+ _iterator25.e(err);
2052
2702
  } finally {
2053
- _iterator18.f();
2703
+ _iterator25.f();
2054
2704
  }
2055
2705
  result.push(cloneDeep(_item));
2056
2706
  }
2057
2707
  } catch (err) {
2058
- _iterator16.e(err);
2708
+ _iterator23.e(err);
2059
2709
  } finally {
2060
- _iterator16.f();
2710
+ _iterator23.f();
2061
2711
  }
2062
2712
  return result;
2063
2713
  }
2064
- }, {
2065
- key: "getPaymentMergeKey",
2066
- value: function getPaymentMergeKey(payment) {
2067
- var _record$metadata2;
2068
- var record = payment;
2069
- if (!record) return '';
2070
- var uniquePaymentNumber = (_record$metadata2 = record.metadata) === null || _record$metadata2 === void 0 ? void 0 : _record$metadata2.unique_payment_number;
2071
- if (!this.isBlankIdentityValue(uniquePaymentNumber)) {
2072
- return "unique:".concat(String(uniquePaymentNumber));
2073
- }
2074
- var orderPaymentId = record.order_payment_id;
2075
- if (!this.isBlankIdentityValue(orderPaymentId)) return "order_payment:".concat(String(orderPaymentId));
2076
- var code = record.code;
2077
- var customPaymentId = record.custom_payment_id;
2078
- if (!this.isBlankIdentityValue(code) && !this.isBlankIdentityValue(customPaymentId)) {
2079
- return "custom:".concat(String(code), "#").concat(String(customPaymentId));
2080
- }
2081
- return '';
2082
- }
2083
2714
  }, {
2084
2715
  key: "mergeOrderPaymentLists",
2085
2716
  value: function mergeOrderPaymentLists(existing, incoming) {
2086
- var _this16 = this;
2087
2717
  var existingList = Array.isArray(existing) ? existing : [];
2088
2718
  var incomingList = Array.isArray(incoming) ? incoming : [];
2089
2719
  if (incomingList.length === 0) return cloneDeep(existingList);
2090
- if (existingList.length === 0) return cloneDeep(incomingList);
2091
- var existingByKey = new Map();
2092
- var _iterator19 = _createForOfIteratorHelper(existingList),
2093
- _step19;
2094
- try {
2095
- for (_iterator19.s(); !(_step19 = _iterator19.n()).done;) {
2096
- var payment = _step19.value;
2097
- var key = this.getPaymentMergeKey(payment);
2098
- if (!key) continue;
2099
- existingByKey.set(key, payment);
2100
- }
2101
- } catch (err) {
2102
- _iterator19.e(err);
2103
- } finally {
2104
- _iterator19.f();
2105
- }
2106
- return incomingList.map(function (payment) {
2107
- var incomingPayment = payment;
2108
- var key = _this16.getPaymentMergeKey(incomingPayment);
2109
- var existingPayment = key ? existingByKey.get(key) : undefined;
2110
- if (!existingPayment) return cloneDeep(incomingPayment);
2111
- return _objectSpread(_objectSpread(_objectSpread({}, cloneDeep(existingPayment)), cloneDeep(incomingPayment)), {}, {
2112
- metadata: _objectSpread(_objectSpread({}, cloneDeep(existingPayment.metadata || {})), cloneDeep(incomingPayment.metadata || {}))
2113
- });
2114
- });
2720
+ return mergeOrderPaymentListsByIdentity(existingList, incomingList);
2115
2721
  }
2116
2722
  }, {
2117
2723
  key: "mergeOrderRecords",
2118
- value: function mergeOrderRecords(existing, incoming, options) {
2724
+ value: function mergeOrderRecords(existing, incoming) {
2119
2725
  var preferred = this.pickPreferredOrder(existing, incoming);
2120
2726
  var secondary = preferred === existing ? incoming : existing;
2121
2727
  var merged = _objectSpread(_objectSpread({}, secondary), preferred);
2122
2728
  var fieldsToPreserve = ['order_id', 'external_sale_number', 'order_number', 'shop_order_number', 'shop_full_order_number'];
2123
- var incomingPatchFields = ['payment_status', 'status', 'shipping_status', 'updated_at', 'shop_discount', 'summary', 'products', 'bookings', 'note', 'customer_id', 'customer_name', 'customer', 'holder', 'contacts_info', 'phone', 'email', 'relation_forms'];
2729
+ var incomingPatchFields = ['payment_status', 'status', 'shipping_status', 'updated_at', 'shop_discount', 'summary', 'note', 'customer_id', 'customer_name', 'customer', 'holder', 'contacts_info', 'phone', 'email', 'relation_forms', 'is_draft_order'];
2124
2730
  var mergedRecord = merged;
2125
2731
  var preferredRecord = preferred;
2126
2732
  var secondaryRecord = secondary;
2733
+ var existingRecord = existing;
2127
2734
  var incomingRecord = incoming;
2128
- var previousRecord = preferred === incoming ? secondaryRecord : preferredRecord;
2129
- for (var _i3 = 0, _fieldsToPreserve = fieldsToPreserve; _i3 < _fieldsToPreserve.length; _i3++) {
2130
- var field = _fieldsToPreserve[_i3];
2735
+ var childUidRemaps = [];
2736
+ for (var _i4 = 0, _fieldsToPreserve = fieldsToPreserve; _i4 < _fieldsToPreserve.length; _i4++) {
2737
+ var field = _fieldsToPreserve[_i4];
2131
2738
  if (!this.isBlankIdentityValue(preferredRecord[field])) {
2132
2739
  mergedRecord[field] = preferredRecord[field];
2133
2740
  continue;
@@ -2136,76 +2743,82 @@ export var OrderModule = /*#__PURE__*/function (_BaseModule) {
2136
2743
  mergedRecord[field] = secondaryRecord[field];
2137
2744
  }
2138
2745
  }
2139
- var hasIncomingProducts = Object.prototype.hasOwnProperty.call(incomingRecord, 'products');
2140
- var shouldUseIncomingProductsSnapshot = (options === null || options === void 0 ? void 0 : options.preferIncomingProducts) === true && hasIncomingProducts && incomingRecord.products !== undefined && incomingRecord.products !== null;
2141
- for (var _i4 = 0, _incomingPatchFields = incomingPatchFields; _i4 < _incomingPatchFields.length; _i4++) {
2142
- var _field = _incomingPatchFields[_i4];
2746
+ for (var _i5 = 0, _incomingPatchFields = incomingPatchFields; _i5 < _incomingPatchFields.length; _i5++) {
2747
+ var _field = _incomingPatchFields[_i5];
2143
2748
  if (!Object.prototype.hasOwnProperty.call(incomingRecord, _field)) continue;
2144
2749
  if (incomingRecord[_field] === undefined || incomingRecord[_field] === null) continue;
2145
2750
  if (typeof incomingRecord[_field] === 'string' && incomingRecord[_field] === '') continue;
2146
2751
  mergedRecord[_field] = cloneDeep(incomingRecord[_field]);
2147
2752
  }
2148
- mergedRecord.products = shouldUseIncomingProductsSnapshot ? cloneDeep(incomingRecord.products) : this.mergeOrderProductLists(preferredRecord.products, secondaryRecord.products);
2149
- if (Object.prototype.hasOwnProperty.call(incomingRecord, 'payments')) {
2150
- if (Array.isArray(incomingRecord.payments)) {
2151
- mergedRecord.payments = this.mergeOrderPaymentLists(previousRecord.payments, incomingRecord.payments);
2152
- } else if (incomingRecord.payments !== undefined && incomingRecord.payments !== null) {
2153
- mergedRecord.payments = cloneDeep(incomingRecord.payments);
2753
+
2754
+ // 子项数组遵循完整快照语义:incoming 自有且为数组(包括 [])时权威替换;
2755
+ // 字段缺席或不是数组时视为稀疏 patch,继续保留 existing。
2756
+ for (var _i6 = 0, _ORDER_AUTHORITATIVE_2 = ORDER_AUTHORITATIVE_COLLECTION_FIELDS; _i6 < _ORDER_AUTHORITATIVE_2.length; _i6++) {
2757
+ var _field2 = _ORDER_AUTHORITATIVE_2[_i6];
2758
+ if (Array.isArray(incomingRecord[_field2])) {
2759
+ mergedRecord[_field2] = this.mergeAuthoritativeOrderCollection(_field2, existingRecord[_field2], incomingRecord[_field2], childUidRemaps);
2760
+ continue;
2761
+ }
2762
+ if (Object.prototype.hasOwnProperty.call(existingRecord, _field2)) {
2763
+ mergedRecord[_field2] = cloneDeep(existingRecord[_field2]);
2764
+ } else {
2765
+ delete mergedRecord[_field2];
2154
2766
  }
2155
2767
  }
2156
2768
  if (!this.isPendingSyncOrder(preferred) || !this.isPendingSyncOrder(secondary)) {
2157
2769
  merged.need_sync = 0;
2158
2770
  }
2159
- return merged;
2771
+ return this.normalizeOrderCollectionsForIngress(applyOrderCollectionUidRemaps(merged, childUidRemaps), 'mergeOrderRecords');
2160
2772
  }
2161
2773
  }, {
2162
2774
  key: "summarizeDuplicateOrders",
2163
2775
  value: function summarizeDuplicateOrders(orders) {
2164
2776
  var groups = new Map();
2165
- var _iterator20 = _createForOfIteratorHelper(orders),
2166
- _step20;
2777
+ var _iterator26 = _createForOfIteratorHelper(orders),
2778
+ _step26;
2167
2779
  try {
2168
- for (_iterator20.s(); !(_step20 = _iterator20.n()).done;) {
2169
- var order = _step20.value;
2170
- var _iterator21 = _createForOfIteratorHelper(this.getOrderIdentityMatchKeys(order)),
2171
- _step21;
2780
+ for (_iterator26.s(); !(_step26 = _iterator26.n()).done;) {
2781
+ var order = _step26.value;
2782
+ var _iterator27 = _createForOfIteratorHelper(this.getOrderIdentityMatchKeys(order)),
2783
+ _step27;
2172
2784
  try {
2173
- for (_iterator21.s(); !(_step21 = _iterator21.n()).done;) {
2174
- var key = _step21.value;
2785
+ for (_iterator27.s(); !(_step27 = _iterator27.n()).done;) {
2786
+ var key = _step27.value;
2175
2787
  var group = groups.get(key) || [];
2176
2788
  group.push(order);
2177
2789
  groups.set(key, group);
2178
2790
  }
2179
2791
  } catch (err) {
2180
- _iterator21.e(err);
2792
+ _iterator27.e(err);
2181
2793
  } finally {
2182
- _iterator21.f();
2794
+ _iterator27.f();
2183
2795
  }
2184
2796
  }
2185
2797
  } catch (err) {
2186
- _iterator20.e(err);
2798
+ _iterator26.e(err);
2187
2799
  } finally {
2188
- _iterator20.f();
2800
+ _iterator26.f();
2189
2801
  }
2190
- return _toConsumableArray(groups.entries()).filter(function (_ref4) {
2191
- var _ref5 = _slicedToArray(_ref4, 2),
2192
- group = _ref5[1];
2193
- return group.length > 1;
2194
- }).slice(0, 20).map(function (_ref6) {
2802
+ return _toConsumableArray(groups.entries()).filter(function (_ref6) {
2195
2803
  var _ref7 = _slicedToArray(_ref6, 2),
2196
- key = _ref7[0],
2197
2804
  group = _ref7[1];
2805
+ return group.length > 1;
2806
+ }).slice(0, 20).map(function (_ref8) {
2807
+ var _ref9 = _slicedToArray(_ref8, 2),
2808
+ key = _ref9[0],
2809
+ group = _ref9[1];
2198
2810
  return {
2199
2811
  key: key,
2200
2812
  count: group.length,
2201
2813
  orders: group.map(function (order) {
2202
- var _order$order_id2, _external_sale_number2, _shop_order_number, _shop_full_order_numb, _order$need_sync;
2814
+ var _order$order_id4, _external_sale_number4, _shop_order_number, _shop_full_order_numb, _order$need_sync, _order$is_draft_order;
2203
2815
  return {
2204
- order_id: (_order$order_id2 = order.order_id) !== null && _order$order_id2 !== void 0 ? _order$order_id2 : null,
2205
- external_sale_number: (_external_sale_number2 = order.external_sale_number) !== null && _external_sale_number2 !== void 0 ? _external_sale_number2 : null,
2816
+ order_id: (_order$order_id4 = order.order_id) !== null && _order$order_id4 !== void 0 ? _order$order_id4 : null,
2817
+ external_sale_number: (_external_sale_number4 = order.external_sale_number) !== null && _external_sale_number4 !== void 0 ? _external_sale_number4 : null,
2206
2818
  shop_order_number: (_shop_order_number = order.shop_order_number) !== null && _shop_order_number !== void 0 ? _shop_order_number : null,
2207
2819
  shop_full_order_number: (_shop_full_order_numb = order.shop_full_order_number) !== null && _shop_full_order_numb !== void 0 ? _shop_full_order_numb : null,
2208
- need_sync: (_order$need_sync = order.need_sync) !== null && _order$need_sync !== void 0 ? _order$need_sync : null
2820
+ need_sync: (_order$need_sync = order.need_sync) !== null && _order$need_sync !== void 0 ? _order$need_sync : null,
2821
+ is_draft_order: (_order$is_draft_order = order.is_draft_order) !== null && _order$is_draft_order !== void 0 ? _order$is_draft_order : null
2209
2822
  };
2210
2823
  })
2211
2824
  };
@@ -2227,23 +2840,24 @@ export var OrderModule = /*#__PURE__*/function (_BaseModule) {
2227
2840
  value: function compactOrderListByIdentity(orders, source) {
2228
2841
  var list = [];
2229
2842
  var removedCount = 0;
2230
- var _iterator22 = _createForOfIteratorHelper(orders),
2231
- _step22;
2843
+ var _iterator28 = _createForOfIteratorHelper(orders),
2844
+ _step28;
2232
2845
  try {
2233
- for (_iterator22.s(); !(_step22 = _iterator22.n()).done;) {
2234
- var order = _step22.value;
2235
- var matchIndex = this.findOrderIndexByIdentity(list, order);
2846
+ for (_iterator28.s(); !(_step28 = _iterator28.n()).done;) {
2847
+ var order = _step28.value;
2848
+ var normalizedOrder = this.normalizeOrderCollectionsForIngress(order, "".concat(source, ".order"));
2849
+ var matchIndex = this.findOrderIndexByIdentity(list, normalizedOrder);
2236
2850
  if (matchIndex < 0) {
2237
- list.push(order);
2851
+ list.push(normalizedOrder);
2238
2852
  continue;
2239
2853
  }
2240
- list[matchIndex] = this.mergeOrderRecords(list[matchIndex], order);
2854
+ list[matchIndex] = this.mergeOrderRecords(list[matchIndex], normalizedOrder);
2241
2855
  removedCount += 1;
2242
2856
  }
2243
2857
  } catch (err) {
2244
- _iterator22.e(err);
2858
+ _iterator28.e(err);
2245
2859
  } finally {
2246
- _iterator22.f();
2860
+ _iterator28.f();
2247
2861
  }
2248
2862
  if (removedCount > 0) {
2249
2863
  this.logWarning('订单列表重复项已压缩', {
@@ -2341,32 +2955,35 @@ export var OrderModule = /*#__PURE__*/function (_BaseModule) {
2341
2955
  }, {
2342
2956
  key: "loadOrdersFromSQLite",
2343
2957
  value: function () {
2344
- var _loadOrdersFromSQLite = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee17() {
2958
+ var _loadOrdersFromSQLite = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee20() {
2959
+ var _this18 = this;
2345
2960
  var orders;
2346
- return _regeneratorRuntime().wrap(function _callee17$(_context17) {
2347
- while (1) switch (_context17.prev = _context17.next) {
2961
+ return _regeneratorRuntime().wrap(function _callee20$(_context20) {
2962
+ while (1) switch (_context20.prev = _context20.next) {
2348
2963
  case 0:
2349
2964
  if (this.dbManager) {
2350
- _context17.next = 2;
2965
+ _context20.next = 2;
2351
2966
  break;
2352
2967
  }
2353
- return _context17.abrupt("return", []);
2968
+ return _context20.abrupt("return", []);
2354
2969
  case 2:
2355
- _context17.prev = 2;
2356
- _context17.next = 5;
2970
+ _context20.prev = 2;
2971
+ _context20.next = 5;
2357
2972
  return this.dbManager.getAll(INDEXDB_STORE_NAME);
2358
2973
  case 5:
2359
- orders = _context17.sent;
2360
- return _context17.abrupt("return", orders || []);
2974
+ orders = _context20.sent;
2975
+ return _context20.abrupt("return", (orders || []).map(function (order) {
2976
+ return _this18.normalizeOrderCollectionsForIngress(order, 'loadOrdersFromSQLite');
2977
+ }));
2361
2978
  case 9:
2362
- _context17.prev = 9;
2363
- _context17.t0 = _context17["catch"](2);
2364
- return _context17.abrupt("return", []);
2979
+ _context20.prev = 9;
2980
+ _context20.t0 = _context20["catch"](2);
2981
+ return _context20.abrupt("return", []);
2365
2982
  case 12:
2366
2983
  case "end":
2367
- return _context17.stop();
2984
+ return _context20.stop();
2368
2985
  }
2369
- }, _callee17, this, [[2, 9]]);
2986
+ }, _callee20, this, [[2, 9]]);
2370
2987
  }));
2371
2988
  function loadOrdersFromSQLite() {
2372
2989
  return _loadOrdersFromSQLite.apply(this, arguments);
@@ -2378,39 +2995,58 @@ export var OrderModule = /*#__PURE__*/function (_BaseModule) {
2378
2995
  value: function isPendingSyncOrder(order) {
2379
2996
  return Number(order === null || order === void 0 ? void 0 : order.need_sync) === 1;
2380
2997
  }
2998
+ }, {
2999
+ key: "isDraftOrder",
3000
+ value: function isDraftOrder(order) {
3001
+ return Number(order === null || order === void 0 ? void 0 : order.is_draft_order) === 1;
3002
+ }
3003
+ }, {
3004
+ key: "shouldProtectDraftOrderFromIncoming",
3005
+ value: function shouldProtectDraftOrderFromIncoming(source) {
3006
+ return source !== 'checkoutSync';
3007
+ }
2381
3008
  }, {
2382
3009
  key: "normalizeRemoteSyncedOrder",
2383
3010
  value: function normalizeRemoteSyncedOrder(order) {
2384
- if (this.isPendingSyncOrder(order)) return order;
2385
- return _objectSpread(_objectSpread({}, order), {}, {
2386
- need_sync: 0
3011
+ var normalizedOrder = this.normalizeOrderCollectionsForIngress(order, 'normalizeRemoteSyncedOrder');
3012
+ if (this.isPendingSyncOrder(normalizedOrder)) return _objectSpread(_objectSpread({}, normalizedOrder), {}, {
3013
+ is_draft_order: 0
3014
+ });
3015
+ return _objectSpread(_objectSpread({}, normalizedOrder), {}, {
3016
+ need_sync: 0,
3017
+ is_draft_order: 0
2387
3018
  });
2388
3019
  }
2389
3020
  }, {
2390
3021
  key: "mergeRemoteSnapshotWithPendingOrders",
2391
3022
  value: function mergeRemoteSnapshotWithPendingOrders(remoteOrders, existingOrders) {
2392
- var _this17 = this;
3023
+ var _this19 = this;
2393
3024
  var merged = remoteOrders.map(function (order) {
2394
- return _this17.normalizeRemoteSyncedOrder(order);
3025
+ return _this19.normalizeRemoteSyncedOrder(order);
2395
3026
  });
2396
- var _iterator23 = _createForOfIteratorHelper(existingOrders),
2397
- _step23;
3027
+ var _iterator29 = _createForOfIteratorHelper(existingOrders),
3028
+ _step29;
2398
3029
  try {
2399
- for (_iterator23.s(); !(_step23 = _iterator23.n()).done;) {
2400
- var order = _step23.value;
2401
- if (!this.isPendingSyncOrder(order)) continue;
3030
+ for (_iterator29.s(); !(_step29 = _iterator29.n()).done;) {
3031
+ var rawOrder = _step29.value;
3032
+ var order = this.normalizeOrderCollectionsForIngress(rawOrder, 'mergeRemoteSnapshotWithPendingOrders.existingSQLite');
3033
+ if (!this.isPendingSyncOrder(order) && !this.isDraftOrder(order)) continue;
2402
3034
  var matchIndex = this.findOrderIndexByIdentity(merged, order);
2403
3035
  if (matchIndex >= 0) {
2404
- merged[matchIndex] = this.mergeOrderRecords(order, merged[matchIndex]);
3036
+ if (this.isDraftOrder(order)) {
3037
+ merged[matchIndex] = order;
3038
+ } else {
3039
+ merged[matchIndex] = this.mergeOrderRecords(order, merged[matchIndex]);
3040
+ }
2405
3041
  continue;
2406
3042
  }
2407
3043
  if (!this.getOrderStorageKey(order)) continue;
2408
3044
  merged.push(order);
2409
3045
  }
2410
3046
  } catch (err) {
2411
- _iterator23.e(err);
3047
+ _iterator29.e(err);
2412
3048
  } finally {
2413
- _iterator23.f();
3049
+ _iterator29.f();
2414
3050
  }
2415
3051
  return this.compactOrderListByIdentity(merged, 'mergeRemoteSnapshotWithPendingOrders').list;
2416
3052
  }
@@ -2426,10 +3062,10 @@ export var OrderModule = /*#__PURE__*/function (_BaseModule) {
2426
3062
  }, {
2427
3063
  key: "runInOrderSQLiteSaveQueue",
2428
3064
  value: (function () {
2429
- var _runInOrderSQLiteSaveQueue = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee18(task) {
3065
+ var _runInOrderSQLiteSaveQueue = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee21(task) {
2430
3066
  var queuedTask;
2431
- return _regeneratorRuntime().wrap(function _callee18$(_context18) {
2432
- while (1) switch (_context18.prev = _context18.next) {
3067
+ return _regeneratorRuntime().wrap(function _callee21$(_context21) {
3068
+ while (1) switch (_context21.prev = _context21.next) {
2433
3069
  case 0:
2434
3070
  queuedTask = this.orderSQLiteSaveQueue.then(task, task);
2435
3071
  this.orderSQLiteSaveQueue = queuedTask.then(function () {
@@ -2437,14 +3073,14 @@ export var OrderModule = /*#__PURE__*/function (_BaseModule) {
2437
3073
  }, function () {
2438
3074
  return undefined;
2439
3075
  });
2440
- return _context18.abrupt("return", queuedTask);
3076
+ return _context21.abrupt("return", queuedTask);
2441
3077
  case 3:
2442
3078
  case "end":
2443
- return _context18.stop();
3079
+ return _context21.stop();
2444
3080
  }
2445
- }, _callee18, this);
3081
+ }, _callee21, this);
2446
3082
  }));
2447
- function runInOrderSQLiteSaveQueue(_x11) {
3083
+ function runInOrderSQLiteSaveQueue(_x14) {
2448
3084
  return _runInOrderSQLiteSaveQueue.apply(this, arguments);
2449
3085
  }
2450
3086
  return runInOrderSQLiteSaveQueue;
@@ -2459,144 +3095,174 @@ export var OrderModule = /*#__PURE__*/function (_BaseModule) {
2459
3095
  }, {
2460
3096
  key: "patchOrdersInSQLite",
2461
3097
  value: (function () {
2462
- var _patchOrdersInSQLite = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee20(orders, source) {
2463
- var _this18 = this;
3098
+ var _patchOrdersInSQLite = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee23(orders, source) {
3099
+ var _this20 = this;
2464
3100
  var mergeActions,
3101
+ options,
2465
3102
  ordersSnapshot,
2466
3103
  _this$filterRedundant2,
2467
3104
  toWrite,
2468
3105
  skipped,
2469
3106
  compactedToWrite,
2470
- _args20 = arguments;
2471
- return _regeneratorRuntime().wrap(function _callee20$(_context20) {
2472
- while (1) switch (_context20.prev = _context20.next) {
3107
+ _args23 = arguments;
3108
+ return _regeneratorRuntime().wrap(function _callee23$(_context23) {
3109
+ while (1) switch (_context23.prev = _context23.next) {
2473
3110
  case 0:
2474
- mergeActions = _args20.length > 2 && _args20[2] !== undefined ? _args20[2] : {};
2475
- if (!(!this.dbManager || orders.length === 0)) {
2476
- _context20.next = 3;
3111
+ mergeActions = _args23.length > 2 && _args23[2] !== undefined ? _args23[2] : {};
3112
+ options = _args23.length > 3 && _args23[3] !== undefined ? _args23[3] : {};
3113
+ if (!(orders.length === 0)) {
3114
+ _context23.next = 4;
2477
3115
  break;
2478
3116
  }
2479
- return _context20.abrupt("return");
2480
- case 3:
2481
- ordersSnapshot = cloneDeep(orders).filter(function (order) {
2482
- return _this18.getOrderStorageKey(order);
3117
+ return _context23.abrupt("return");
3118
+ case 4:
3119
+ if (this.dbManager) {
3120
+ _context23.next = 8;
3121
+ break;
3122
+ }
3123
+ if (!options.throwOnError) {
3124
+ _context23.next = 7;
3125
+ break;
3126
+ }
3127
+ throw new Error('订单 SQLite 数据库未初始化');
3128
+ case 7:
3129
+ return _context23.abrupt("return");
3130
+ case 8:
3131
+ ordersSnapshot = cloneDeep(orders).map(function (order) {
3132
+ return _this20.normalizeOrderCollectionsForIngress(order, "".concat(source, ".sqliteIngress"));
3133
+ }).filter(function (order) {
3134
+ return _this20.getOrderStorageKey(order);
2483
3135
  });
2484
3136
  if (!(ordersSnapshot.length === 0)) {
2485
- _context20.next = 6;
3137
+ _context23.next = 11;
2486
3138
  break;
2487
3139
  }
2488
- return _context20.abrupt("return");
2489
- case 6:
3140
+ return _context23.abrupt("return");
3141
+ case 11:
2490
3142
  _this$filterRedundant2 = this.filterRedundantPatchOrders(source, ordersSnapshot, mergeActions), toWrite = _this$filterRedundant2.toWrite, skipped = _this$filterRedundant2.skipped;
2491
3143
  if (!(toWrite.length === 0)) {
2492
- _context20.next = 9;
3144
+ _context23.next = 14;
2493
3145
  break;
2494
3146
  }
2495
- return _context20.abrupt("return");
2496
- case 9:
3147
+ return _context23.abrupt("return");
3148
+ case 14:
2497
3149
  compactedToWrite = this.compactOrderListByIdentity(toWrite, "".concat(source, ".sqlitePatch")).list;
2498
3150
  if (!(compactedToWrite.length === 0)) {
2499
- _context20.next = 12;
3151
+ _context23.next = 17;
2500
3152
  break;
2501
3153
  }
2502
- return _context20.abrupt("return");
2503
- case 12:
2504
- _context20.prev = 12;
2505
- _context20.next = 15;
2506
- return this.runInOrderSQLiteSaveQueue( /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee19() {
2507
- var writeMethod, _iterator24, _step24, order;
2508
- return _regeneratorRuntime().wrap(function _callee19$(_context19) {
2509
- while (1) switch (_context19.prev = _context19.next) {
3154
+ return _context23.abrupt("return");
3155
+ case 17:
3156
+ _context23.prev = 17;
3157
+ _context23.next = 20;
3158
+ return this.runInOrderSQLiteSaveQueue( /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee22() {
3159
+ var writeMethod, _iterator30, _step30, order;
3160
+ return _regeneratorRuntime().wrap(function _callee22$(_context22) {
3161
+ while (1) switch (_context22.prev = _context22.next) {
2510
3162
  case 0:
2511
3163
  writeMethod = 'none';
2512
- _this18.logInfo('patchOrdersInSQLite-开始', {
3164
+ _this20.logInfo('patchOrdersInSQLite-开始', {
2513
3165
  source: source,
2514
3166
  count: compactedToWrite.length,
2515
3167
  dedupedCount: skipped.length
2516
3168
  });
2517
- if (!(typeof _this18.dbManager.bulkUpdate === 'function')) {
2518
- _context19.next = 8;
3169
+ if (!(typeof _this20.dbManager.bulkUpdate === 'function')) {
3170
+ _context22.next = 8;
2519
3171
  break;
2520
3172
  }
2521
3173
  writeMethod = 'bulkUpdate';
2522
- _context19.next = 6;
2523
- return _this18.dbManager.bulkUpdate(INDEXDB_STORE_NAME, compactedToWrite);
3174
+ _context22.next = 6;
3175
+ return _this20.dbManager.bulkUpdate(INDEXDB_STORE_NAME, compactedToWrite);
2524
3176
  case 6:
2525
- _context19.next = 33;
3177
+ _context22.next = 37;
2526
3178
  break;
2527
3179
  case 8:
2528
- if (!(typeof _this18.dbManager.bulkAdd === 'function')) {
2529
- _context19.next = 14;
3180
+ if (!(typeof _this20.dbManager.bulkAdd === 'function')) {
3181
+ _context22.next = 14;
2530
3182
  break;
2531
3183
  }
2532
3184
  writeMethod = 'bulkAdd';
2533
- _context19.next = 12;
2534
- return _this18.dbManager.bulkAdd(INDEXDB_STORE_NAME, compactedToWrite);
3185
+ _context22.next = 12;
3186
+ return _this20.dbManager.bulkAdd(INDEXDB_STORE_NAME, compactedToWrite);
2535
3187
  case 12:
2536
- _context19.next = 33;
3188
+ _context22.next = 37;
2537
3189
  break;
2538
3190
  case 14:
2539
- if (!(typeof _this18.dbManager.update === 'function')) {
2540
- _context19.next = 33;
3191
+ if (!(typeof _this20.dbManager.update === 'function')) {
3192
+ _context22.next = 35;
2541
3193
  break;
2542
3194
  }
2543
3195
  writeMethod = 'update';
2544
- _iterator24 = _createForOfIteratorHelper(compactedToWrite);
2545
- _context19.prev = 17;
2546
- _iterator24.s();
3196
+ _iterator30 = _createForOfIteratorHelper(compactedToWrite);
3197
+ _context22.prev = 17;
3198
+ _iterator30.s();
2547
3199
  case 19:
2548
- if ((_step24 = _iterator24.n()).done) {
2549
- _context19.next = 25;
3200
+ if ((_step30 = _iterator30.n()).done) {
3201
+ _context22.next = 25;
2550
3202
  break;
2551
3203
  }
2552
- order = _step24.value;
2553
- _context19.next = 23;
2554
- return _this18.dbManager.update(INDEXDB_STORE_NAME, order);
3204
+ order = _step30.value;
3205
+ _context22.next = 23;
3206
+ return _this20.dbManager.update(INDEXDB_STORE_NAME, order);
2555
3207
  case 23:
2556
- _context19.next = 19;
3208
+ _context22.next = 19;
2557
3209
  break;
2558
3210
  case 25:
2559
- _context19.next = 30;
3211
+ _context22.next = 30;
2560
3212
  break;
2561
3213
  case 27:
2562
- _context19.prev = 27;
2563
- _context19.t0 = _context19["catch"](17);
2564
- _iterator24.e(_context19.t0);
3214
+ _context22.prev = 27;
3215
+ _context22.t0 = _context22["catch"](17);
3216
+ _iterator30.e(_context22.t0);
2565
3217
  case 30:
2566
- _context19.prev = 30;
2567
- _iterator24.f();
2568
- return _context19.finish(30);
3218
+ _context22.prev = 30;
3219
+ _iterator30.f();
3220
+ return _context22.finish(30);
2569
3221
  case 33:
2570
- _this18.recordRecentSqliteWrite(source, compactedToWrite);
2571
- _this18.logInfo('patchOrdersInSQLite-完成', {
3222
+ _context22.next = 37;
3223
+ break;
3224
+ case 35:
3225
+ if (!options.throwOnError) {
3226
+ _context22.next = 37;
3227
+ break;
3228
+ }
3229
+ throw new Error('订单 SQLite 数据库不支持写入');
3230
+ case 37:
3231
+ _this20.recordRecentSqliteWrite(source, compactedToWrite);
3232
+ _this20.logInfo('patchOrdersInSQLite-完成', {
2572
3233
  source: source,
2573
3234
  count: compactedToWrite.length,
2574
3235
  writeMethod: writeMethod,
2575
3236
  dedupedCount: skipped.length
2576
3237
  });
2577
- case 35:
3238
+ case 39:
2578
3239
  case "end":
2579
- return _context19.stop();
3240
+ return _context22.stop();
2580
3241
  }
2581
- }, _callee19, null, [[17, 27, 30, 33]]);
3242
+ }, _callee22, null, [[17, 27, 30, 33]]);
2582
3243
  })));
2583
- case 15:
2584
- _context20.next = 20;
3244
+ case 20:
3245
+ _context23.next = 27;
2585
3246
  break;
2586
- case 17:
2587
- _context20.prev = 17;
2588
- _context20.t0 = _context20["catch"](12);
3247
+ case 22:
3248
+ _context23.prev = 22;
3249
+ _context23.t0 = _context23["catch"](17);
2589
3250
  this.logError('增量保存订单到 SQLite 失败', {
2590
- error: _context20.t0 instanceof Error ? _context20.t0.message : String(_context20.t0),
3251
+ error: _context23.t0 instanceof Error ? _context23.t0.message : String(_context23.t0),
2591
3252
  orderCount: ordersSnapshot.length
2592
3253
  });
2593
- case 20:
3254
+ if (!options.throwOnError) {
3255
+ _context23.next = 27;
3256
+ break;
3257
+ }
3258
+ throw _context23.t0;
3259
+ case 27:
2594
3260
  case "end":
2595
- return _context20.stop();
3261
+ return _context23.stop();
2596
3262
  }
2597
- }, _callee20, this, [[12, 17]]);
3263
+ }, _callee23, this, [[17, 22]]);
2598
3264
  }));
2599
- function patchOrdersInSQLite(_x12, _x13) {
3265
+ function patchOrdersInSQLite(_x15, _x16) {
2600
3266
  return _patchOrdersInSQLite.apply(this, arguments);
2601
3267
  }
2602
3268
  return patchOrdersInSQLite;
@@ -2611,92 +3277,128 @@ export var OrderModule = /*#__PURE__*/function (_BaseModule) {
2611
3277
  }, {
2612
3278
  key: "updateOrderInSQLite",
2613
3279
  value: (function () {
2614
- var _updateOrderInSQLite = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee22(order, source) {
2615
- var _this19 = this;
2616
- var orderSnapshot, _orderSnapshot$order_3;
2617
- return _regeneratorRuntime().wrap(function _callee22$(_context22) {
2618
- while (1) switch (_context22.prev = _context22.next) {
3280
+ var _updateOrderInSQLite = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee25(order, source) {
3281
+ var _this21 = this;
3282
+ var options,
3283
+ orderSnapshot,
3284
+ _orderSnapshot$order_3,
3285
+ _args25 = arguments;
3286
+ return _regeneratorRuntime().wrap(function _callee25$(_context25) {
3287
+ while (1) switch (_context25.prev = _context25.next) {
2619
3288
  case 0:
2620
- if (!(!this.dbManager || !this.getOrderStorageKey(order))) {
2621
- _context22.next = 2;
3289
+ options = _args25.length > 2 && _args25[2] !== undefined ? _args25[2] : {};
3290
+ if (this.getOrderStorageKey(order)) {
3291
+ _context25.next = 5;
2622
3292
  break;
2623
3293
  }
2624
- return _context22.abrupt("return");
2625
- case 2:
2626
- orderSnapshot = cloneDeep(order);
2627
- _context22.prev = 3;
2628
- _context22.next = 6;
2629
- return this.runInOrderSQLiteSaveQueue( /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee21() {
3294
+ if (!options.throwOnError) {
3295
+ _context25.next = 4;
3296
+ break;
3297
+ }
3298
+ throw new Error('订单缺少可用的 SQLite 存储主键');
3299
+ case 4:
3300
+ return _context25.abrupt("return");
3301
+ case 5:
3302
+ if (this.dbManager) {
3303
+ _context25.next = 9;
3304
+ break;
3305
+ }
3306
+ if (!options.throwOnError) {
3307
+ _context25.next = 8;
3308
+ break;
3309
+ }
3310
+ throw new Error('订单 SQLite 数据库未初始化');
3311
+ case 8:
3312
+ return _context25.abrupt("return");
3313
+ case 9:
3314
+ orderSnapshot = this.normalizeOrderCollectionsForIngress(cloneDeep(order), "".concat(source, ".sqliteIngress"));
3315
+ _context25.prev = 10;
3316
+ _context25.next = 13;
3317
+ return this.runInOrderSQLiteSaveQueue( /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee24() {
2630
3318
  var _orderSnapshot$order_, _orderSnapshot$order_2;
2631
3319
  var writeMethod;
2632
- return _regeneratorRuntime().wrap(function _callee21$(_context21) {
2633
- while (1) switch (_context21.prev = _context21.next) {
3320
+ return _regeneratorRuntime().wrap(function _callee24$(_context24) {
3321
+ while (1) switch (_context24.prev = _context24.next) {
2634
3322
  case 0:
2635
3323
  writeMethod = 'none';
2636
- _this19.logInfo('updateOrderInSQLite-开始', {
3324
+ _this21.logInfo('updateOrderInSQLite-开始', {
2637
3325
  source: source,
2638
3326
  orderId: (_orderSnapshot$order_ = orderSnapshot.order_id) !== null && _orderSnapshot$order_ !== void 0 ? _orderSnapshot$order_ : null,
2639
- storageKey: _this19.getOrderStorageKey(orderSnapshot)
3327
+ storageKey: _this21.getOrderStorageKey(orderSnapshot)
2640
3328
  });
2641
- if (!(typeof _this19.dbManager.update === 'function')) {
2642
- _context21.next = 8;
3329
+ if (!(typeof _this21.dbManager.update === 'function')) {
3330
+ _context24.next = 8;
2643
3331
  break;
2644
3332
  }
2645
3333
  writeMethod = 'update';
2646
- _context21.next = 6;
2647
- return _this19.dbManager.update(INDEXDB_STORE_NAME, orderSnapshot);
3334
+ _context24.next = 6;
3335
+ return _this21.dbManager.update(INDEXDB_STORE_NAME, orderSnapshot);
2648
3336
  case 6:
2649
- _context21.next = 18;
3337
+ _context24.next = 22;
2650
3338
  break;
2651
3339
  case 8:
2652
- if (!(typeof _this19.dbManager.bulkUpdate === 'function')) {
2653
- _context21.next = 14;
3340
+ if (!(typeof _this21.dbManager.bulkUpdate === 'function')) {
3341
+ _context24.next = 14;
2654
3342
  break;
2655
3343
  }
2656
3344
  writeMethod = 'bulkUpdate';
2657
- _context21.next = 12;
2658
- return _this19.dbManager.bulkUpdate(INDEXDB_STORE_NAME, [orderSnapshot]);
3345
+ _context24.next = 12;
3346
+ return _this21.dbManager.bulkUpdate(INDEXDB_STORE_NAME, [orderSnapshot]);
2659
3347
  case 12:
2660
- _context21.next = 18;
3348
+ _context24.next = 22;
2661
3349
  break;
2662
3350
  case 14:
2663
- if (!(typeof _this19.dbManager.bulkAdd === 'function')) {
2664
- _context21.next = 18;
3351
+ if (!(typeof _this21.dbManager.bulkAdd === 'function')) {
3352
+ _context24.next = 20;
2665
3353
  break;
2666
3354
  }
2667
3355
  writeMethod = 'bulkAdd';
2668
- _context21.next = 18;
2669
- return _this19.dbManager.bulkAdd(INDEXDB_STORE_NAME, [orderSnapshot]);
3356
+ _context24.next = 18;
3357
+ return _this21.dbManager.bulkAdd(INDEXDB_STORE_NAME, [orderSnapshot]);
2670
3358
  case 18:
2671
- _this19.recordRecentSqliteWrite(source, [orderSnapshot]);
2672
- _this19.logInfo('updateOrderInSQLite-完成', {
3359
+ _context24.next = 22;
3360
+ break;
3361
+ case 20:
3362
+ if (!options.throwOnError) {
3363
+ _context24.next = 22;
3364
+ break;
3365
+ }
3366
+ throw new Error('订单 SQLite 数据库不支持写入');
3367
+ case 22:
3368
+ _this21.recordRecentSqliteWrite(source, [orderSnapshot]);
3369
+ _this21.logInfo('updateOrderInSQLite-完成', {
2673
3370
  source: source,
2674
3371
  orderId: (_orderSnapshot$order_2 = orderSnapshot.order_id) !== null && _orderSnapshot$order_2 !== void 0 ? _orderSnapshot$order_2 : null,
2675
3372
  writeMethod: writeMethod
2676
3373
  });
2677
- case 20:
3374
+ case 24:
2678
3375
  case "end":
2679
- return _context21.stop();
3376
+ return _context24.stop();
2680
3377
  }
2681
- }, _callee21);
3378
+ }, _callee24);
2682
3379
  })));
2683
- case 6:
2684
- _context22.next = 11;
3380
+ case 13:
3381
+ _context25.next = 20;
2685
3382
  break;
2686
- case 8:
2687
- _context22.prev = 8;
2688
- _context22.t0 = _context22["catch"](3);
3383
+ case 15:
3384
+ _context25.prev = 15;
3385
+ _context25.t0 = _context25["catch"](10);
2689
3386
  this.logError('单条保存订单到 SQLite 失败', {
2690
- error: _context22.t0 instanceof Error ? _context22.t0.message : String(_context22.t0),
3387
+ error: _context25.t0 instanceof Error ? _context25.t0.message : String(_context25.t0),
2691
3388
  orderId: (_orderSnapshot$order_3 = orderSnapshot.order_id) !== null && _orderSnapshot$order_3 !== void 0 ? _orderSnapshot$order_3 : null
2692
3389
  });
2693
- case 11:
3390
+ if (!options.throwOnError) {
3391
+ _context25.next = 20;
3392
+ break;
3393
+ }
3394
+ throw _context25.t0;
3395
+ case 20:
2694
3396
  case "end":
2695
- return _context22.stop();
3397
+ return _context25.stop();
2696
3398
  }
2697
- }, _callee22, this, [[3, 8]]);
3399
+ }, _callee25, this, [[10, 15]]);
2698
3400
  }));
2699
- function updateOrderInSQLite(_x14, _x15) {
3401
+ function updateOrderInSQLite(_x17, _x18) {
2700
3402
  return _updateOrderInSQLite.apply(this, arguments);
2701
3403
  }
2702
3404
  return updateOrderInSQLite;
@@ -2711,98 +3413,100 @@ export var OrderModule = /*#__PURE__*/function (_BaseModule) {
2711
3413
  }, {
2712
3414
  key: "replaceOrdersSnapshotInSQLite",
2713
3415
  value: (function () {
2714
- var _replaceOrdersSnapshotInSQLite = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee24(orderList, source) {
2715
- var _this20 = this;
3416
+ var _replaceOrdersSnapshotInSQLite = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee27(orderList, source) {
3417
+ var _this22 = this;
2716
3418
  var remoteSnapshot, mergedSnapshot;
2717
- return _regeneratorRuntime().wrap(function _callee24$(_context24) {
2718
- while (1) switch (_context24.prev = _context24.next) {
3419
+ return _regeneratorRuntime().wrap(function _callee27$(_context27) {
3420
+ while (1) switch (_context27.prev = _context27.next) {
2719
3421
  case 0:
2720
3422
  if (this.dbManager) {
2721
- _context24.next = 2;
3423
+ _context27.next = 2;
2722
3424
  break;
2723
3425
  }
2724
- return _context24.abrupt("return", this.compactOrderListByIdentity(orderList.map(function (order) {
2725
- return _this20.normalizeRemoteSyncedOrder(order);
3426
+ return _context27.abrupt("return", this.compactOrderListByIdentity(orderList.map(function (order) {
3427
+ return _this22.normalizeRemoteSyncedOrder(order);
2726
3428
  }), "".concat(source, ".snapshotNoDb")).list);
2727
3429
  case 2:
2728
- remoteSnapshot = cloneDeep(orderList);
3430
+ remoteSnapshot = cloneDeep(orderList).map(function (order) {
3431
+ return _this22.normalizeOrderCollectionsForIngress(order, "".concat(source, ".remoteIngress"));
3432
+ });
2729
3433
  mergedSnapshot = this.compactOrderListByIdentity(remoteSnapshot.map(function (order) {
2730
- return _this20.normalizeRemoteSyncedOrder(order);
3434
+ return _this22.normalizeRemoteSyncedOrder(order);
2731
3435
  }), "".concat(source, ".remoteSnapshot")).list;
2732
- _context24.prev = 4;
2733
- _context24.next = 7;
2734
- return this.runInOrderSQLiteSaveQueue( /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee23() {
3436
+ _context27.prev = 4;
3437
+ _context27.next = 7;
3438
+ return this.runInOrderSQLiteSaveQueue( /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee26() {
2735
3439
  var existingOrders, orderListSnapshot;
2736
- return _regeneratorRuntime().wrap(function _callee23$(_context23) {
2737
- while (1) switch (_context23.prev = _context23.next) {
3440
+ return _regeneratorRuntime().wrap(function _callee26$(_context26) {
3441
+ while (1) switch (_context26.prev = _context26.next) {
2738
3442
  case 0:
2739
- if (!(typeof _this20.dbManager.getAll === 'function')) {
2740
- _context23.next = 6;
3443
+ if (!(typeof _this22.dbManager.getAll === 'function')) {
3444
+ _context26.next = 6;
2741
3445
  break;
2742
3446
  }
2743
- _context23.next = 3;
2744
- return _this20.dbManager.getAll(INDEXDB_STORE_NAME);
3447
+ _context26.next = 3;
3448
+ return _this22.dbManager.getAll(INDEXDB_STORE_NAME);
2745
3449
  case 3:
2746
- _context23.t0 = _context23.sent;
2747
- _context23.next = 7;
3450
+ _context26.t0 = _context26.sent;
3451
+ _context26.next = 7;
2748
3452
  break;
2749
3453
  case 6:
2750
- _context23.t0 = [];
3454
+ _context26.t0 = [];
2751
3455
  case 7:
2752
- existingOrders = _context23.t0;
2753
- orderListSnapshot = _this20.mergeRemoteSnapshotWithPendingOrders(remoteSnapshot, existingOrders || []);
2754
- mergedSnapshot = _this20.compactOrderListByIdentity(orderListSnapshot, "".concat(source, ".sqliteSnapshot")).list;
2755
- _this20.logInfo('replaceOrdersSnapshotInSQLite-开始', {
3456
+ existingOrders = _context26.t0;
3457
+ orderListSnapshot = _this22.mergeRemoteSnapshotWithPendingOrders(remoteSnapshot, existingOrders || []);
3458
+ mergedSnapshot = _this22.compactOrderListByIdentity(orderListSnapshot, "".concat(source, ".sqliteSnapshot")).list;
3459
+ _this22.logInfo('replaceOrdersSnapshotInSQLite-开始', {
2756
3460
  source: source,
2757
3461
  count: mergedSnapshot.length,
2758
3462
  remoteCount: remoteSnapshot.length,
2759
- preservedPendingCount: mergedSnapshot.length - remoteSnapshot.length
3463
+ preservedLocalUnsyncedCount: mergedSnapshot.length - remoteSnapshot.length
2760
3464
  });
2761
- _context23.next = 13;
2762
- return _this20.dbManager.clear(INDEXDB_STORE_NAME);
3465
+ _context26.next = 13;
3466
+ return _this22.dbManager.clear(INDEXDB_STORE_NAME);
2763
3467
  case 13:
2764
- _this20.logInfo('replaceOrdersSnapshotInSQLite-clear完成', {
3468
+ _this22.logInfo('replaceOrdersSnapshotInSQLite-clear完成', {
2765
3469
  count: mergedSnapshot.length
2766
3470
  });
2767
3471
  if (!(mergedSnapshot.length === 0)) {
2768
- _context23.next = 16;
3472
+ _context26.next = 16;
2769
3473
  break;
2770
3474
  }
2771
- return _context23.abrupt("return");
3475
+ return _context26.abrupt("return");
2772
3476
  case 16:
2773
- _context23.next = 18;
2774
- return _this20.dbManager.bulkAdd(INDEXDB_STORE_NAME, mergedSnapshot);
3477
+ _context26.next = 18;
3478
+ return _this22.dbManager.bulkAdd(INDEXDB_STORE_NAME, mergedSnapshot);
2775
3479
  case 18:
2776
- _this20.recordRecentSqliteWrite(source, mergedSnapshot);
2777
- _this20.logInfo('replaceOrdersSnapshotInSQLite-bulkAdd完成', {
3480
+ _this22.recordRecentSqliteWrite(source, mergedSnapshot);
3481
+ _this22.logInfo('replaceOrdersSnapshotInSQLite-bulkAdd完成', {
2778
3482
  source: source,
2779
3483
  count: mergedSnapshot.length
2780
3484
  });
2781
3485
  case 20:
2782
3486
  case "end":
2783
- return _context23.stop();
3487
+ return _context26.stop();
2784
3488
  }
2785
- }, _callee23);
3489
+ }, _callee26);
2786
3490
  })));
2787
3491
  case 7:
2788
- _context24.next = 12;
3492
+ _context27.next = 12;
2789
3493
  break;
2790
3494
  case 9:
2791
- _context24.prev = 9;
2792
- _context24.t0 = _context24["catch"](4);
3495
+ _context27.prev = 9;
3496
+ _context27.t0 = _context27["catch"](4);
2793
3497
  this.logError('全量保存订单到 SQLite 失败', {
2794
- error: _context24.t0 instanceof Error ? _context24.t0.message : String(_context24.t0),
3498
+ error: _context27.t0 instanceof Error ? _context27.t0.message : String(_context27.t0),
2795
3499
  orderList: remoteSnapshot.length
2796
3500
  });
2797
3501
  case 12:
2798
- return _context24.abrupt("return", mergedSnapshot);
3502
+ return _context27.abrupt("return", mergedSnapshot);
2799
3503
  case 13:
2800
3504
  case "end":
2801
- return _context24.stop();
3505
+ return _context27.stop();
2802
3506
  }
2803
- }, _callee24, this, [[4, 9]]);
3507
+ }, _callee27, this, [[4, 9]]);
2804
3508
  }));
2805
- function replaceOrdersSnapshotInSQLite(_x16, _x17) {
3509
+ function replaceOrdersSnapshotInSQLite(_x19, _x20) {
2806
3510
  return _replaceOrdersSnapshotInSQLite.apply(this, arguments);
2807
3511
  }
2808
3512
  return replaceOrdersSnapshotInSQLite;
@@ -2817,38 +3521,38 @@ export var OrderModule = /*#__PURE__*/function (_BaseModule) {
2817
3521
  }, {
2818
3522
  key: "clear",
2819
3523
  value: (function () {
2820
- var _clear = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee26() {
2821
- var _this21 = this;
2822
- return _regeneratorRuntime().wrap(function _callee26$(_context26) {
2823
- while (1) switch (_context26.prev = _context26.next) {
3524
+ var _clear = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee29() {
3525
+ var _this23 = this;
3526
+ return _regeneratorRuntime().wrap(function _callee29$(_context29) {
3527
+ while (1) switch (_context29.prev = _context29.next) {
2824
3528
  case 0:
2825
3529
  this.store.list = [];
2826
3530
  this.store.map = new Map();
2827
3531
  if (!this.dbManager) {
2828
- _context26.next = 5;
3532
+ _context29.next = 5;
2829
3533
  break;
2830
3534
  }
2831
- _context26.next = 5;
2832
- return this.runInOrderSQLiteSaveQueue( /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee25() {
2833
- return _regeneratorRuntime().wrap(function _callee25$(_context25) {
2834
- while (1) switch (_context25.prev = _context25.next) {
3535
+ _context29.next = 5;
3536
+ return this.runInOrderSQLiteSaveQueue( /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee28() {
3537
+ return _regeneratorRuntime().wrap(function _callee28$(_context28) {
3538
+ while (1) switch (_context28.prev = _context28.next) {
2835
3539
  case 0:
2836
- _context25.next = 2;
2837
- return _this21.dbManager.clear(INDEXDB_STORE_NAME);
3540
+ _context28.next = 2;
3541
+ return _this23.dbManager.clear(INDEXDB_STORE_NAME);
2838
3542
  case 2:
2839
3543
  case "end":
2840
- return _context25.stop();
3544
+ return _context28.stop();
2841
3545
  }
2842
- }, _callee25);
3546
+ }, _callee28);
2843
3547
  })));
2844
3548
  case 5:
2845
3549
  this.setStorageItem(ORDER_LAST_FULL_FETCH_AT_STORAGE_KEY, '');
2846
3550
  this.emitOrdersChanged([], 'clear');
2847
3551
  case 7:
2848
3552
  case "end":
2849
- return _context26.stop();
3553
+ return _context29.stop();
2850
3554
  }
2851
- }, _callee26, this);
3555
+ }, _callee29, this);
2852
3556
  }));
2853
3557
  function clear() {
2854
3558
  return _clear.apply(this, arguments);