@pisell/pisellos 2.2.215 → 2.2.217

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.
@@ -243,6 +243,21 @@ export var OrderModule = /*#__PURE__*/function (_BaseModule) {
243
243
  // 日志记录失败不影响主流程
244
244
  }
245
245
  }
246
+ }, {
247
+ key: "logWarning",
248
+ value: function logWarning(title, metadata) {
249
+ try {
250
+ if (this.logger) {
251
+ this.logger.addLog({
252
+ type: 'warning',
253
+ title: "[OrderServerModule] ".concat(title),
254
+ metadata: metadata || {}
255
+ });
256
+ }
257
+ } catch (_unused3) {
258
+ // 日志记录失败不影响主流程
259
+ }
260
+ }
246
261
 
247
262
  /**
248
263
  * 记录订单最近一次 SQLite 写入,供 pubsub 回声去重使用。
@@ -946,8 +961,7 @@ export var OrderModule = /*#__PURE__*/function (_BaseModule) {
946
961
  key: "upsertPendingSyncOrders",
947
962
  value: (function () {
948
963
  var _upsertPendingSyncOrders = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee11(pendingOrders) {
949
- var _this10 = this;
950
- var pendingMap, memoryPendingMap, _iterator4, _step4, order, storageKey, patchedOrders, mergeActions, updatedList, _iterator5, _step5, _step5$value, _storageKey, _order;
964
+ var pendingMap, memoryPendingMap, _iterator4, _step4, order, storageKey, patchedOrders, mergeActions, updatedList, _iterator5, _step5, _step5$value, _storageKey, pendingOrder, matchIndex, _iterator6, _step6, _step6$value, _storageKey2, _order;
951
965
  return _regeneratorRuntime().wrap(function _callee11$(_context11) {
952
966
  while (1) switch (_context11.prev = _context11.next) {
953
967
  case 0:
@@ -1007,50 +1021,70 @@ export var OrderModule = /*#__PURE__*/function (_BaseModule) {
1007
1021
  });
1008
1022
  patchedOrders = _toConsumableArray(pendingMap.values());
1009
1023
  mergeActions = {};
1010
- updatedList = this.store.list.map(function (order) {
1011
- var storageKey = _this10.getOrderStorageKey(order);
1012
- if (!storageKey || !pendingMap.has(storageKey)) return order;
1013
- var pendingOrder = memoryPendingMap.get(storageKey);
1014
- pendingMap.delete(storageKey);
1015
- memoryPendingMap.delete(storageKey);
1016
- mergeActions[storageKey] = 'update';
1017
- return pendingOrder;
1018
- });
1024
+ updatedList = _toConsumableArray(this.store.list);
1019
1025
  _iterator5 = _createForOfIteratorHelper(memoryPendingMap.entries());
1026
+ _context11.prev = 33;
1027
+ _iterator5.s();
1028
+ case 35:
1029
+ if ((_step5 = _iterator5.n()).done) {
1030
+ _context11.next = 46;
1031
+ break;
1032
+ }
1033
+ _step5$value = _slicedToArray(_step5.value, 2), _storageKey = _step5$value[0], pendingOrder = _step5$value[1];
1034
+ matchIndex = this.findOrderIndexByIdentity(updatedList, pendingOrder);
1035
+ if (!(matchIndex < 0)) {
1036
+ _context11.next = 40;
1037
+ break;
1038
+ }
1039
+ return _context11.abrupt("continue", 44);
1040
+ case 40:
1041
+ updatedList[matchIndex] = this.mergeOrderRecords(updatedList[matchIndex], pendingOrder);
1042
+ pendingMap.delete(_storageKey);
1043
+ memoryPendingMap.delete(_storageKey);
1044
+ mergeActions[_storageKey] = 'update';
1045
+ case 44:
1046
+ _context11.next = 35;
1047
+ break;
1048
+ case 46:
1049
+ _context11.next = 51;
1050
+ break;
1051
+ case 48:
1052
+ _context11.prev = 48;
1053
+ _context11.t1 = _context11["catch"](33);
1054
+ _iterator5.e(_context11.t1);
1055
+ case 51:
1056
+ _context11.prev = 51;
1057
+ _iterator5.f();
1058
+ return _context11.finish(51);
1059
+ case 54:
1060
+ _iterator6 = _createForOfIteratorHelper(memoryPendingMap.entries());
1020
1061
  try {
1021
- for (_iterator5.s(); !(_step5 = _iterator5.n()).done;) {
1022
- _step5$value = _slicedToArray(_step5.value, 2), _storageKey = _step5$value[0], _order = _step5$value[1];
1023
- mergeActions[_storageKey] = 'insert';
1062
+ for (_iterator6.s(); !(_step6 = _iterator6.n()).done;) {
1063
+ _step6$value = _slicedToArray(_step6.value, 2), _storageKey2 = _step6$value[0], _order = _step6$value[1];
1064
+ mergeActions[_storageKey2] = 'insert';
1024
1065
  updatedList.push(_order);
1025
1066
  }
1026
1067
  } catch (err) {
1027
- _iterator5.e(err);
1068
+ _iterator6.e(err);
1028
1069
  } finally {
1029
- _iterator5.f();
1070
+ _iterator6.f();
1030
1071
  }
1031
- this.store.list = updatedList;
1072
+ this.store.list = this.compactOrderListByIdentity(updatedList, 'upsertPendingSyncOrders.store').list;
1032
1073
  this.syncOrdersMap();
1033
- _context11.next = 38;
1074
+ _context11.next = 60;
1034
1075
  return this.patchOrdersInSQLite(patchedOrders, 'upsertPendingSyncOrders', mergeActions);
1035
- case 38:
1076
+ case 60:
1036
1077
  this.logInfo('upsertPendingSyncOrders-结束', {
1037
1078
  count: patchedOrders.length,
1038
1079
  storeOrderCountAfter: this.store.list.length
1039
1080
  });
1040
- this.emitOrdersChanged(patchedOrders, 'upsertPendingSyncOrders');
1041
- _context11.next = 42;
1042
- return this.core.effects.emit(OrderHooks.onOrdersChanged, {
1043
- list: this.store.list,
1044
- changedOrders: patchedOrders.map(function (order) {
1045
- return _this10.normalizeOrderForMemoryList(order);
1046
- }),
1047
- source: 'upsertPendingSyncOrders'
1048
- });
1049
- case 42:
1081
+ _context11.next = 63;
1082
+ return this.emitOrdersChanged(patchedOrders, 'upsertPendingSyncOrders');
1083
+ case 63:
1050
1084
  case "end":
1051
1085
  return _context11.stop();
1052
1086
  }
1053
- }, _callee11, this, [[6, 19, 22, 25]]);
1087
+ }, _callee11, this, [[6, 19, 22, 25], [33, 48, 51, 54]]);
1054
1088
  }));
1055
1089
  function upsertPendingSyncOrders(_x7) {
1056
1090
  return _upsertPendingSyncOrders.apply(this, arguments);
@@ -1127,42 +1161,30 @@ export var OrderModule = /*#__PURE__*/function (_BaseModule) {
1127
1161
  }, {
1128
1162
  key: "syncOrdersMap",
1129
1163
  value: function syncOrdersMap() {
1130
- var _this11 = this;
1164
+ var _this10 = this;
1131
1165
  this.store.map.clear();
1132
1166
  this.resourceIdIndex.clear();
1133
- var _iterator6 = _createForOfIteratorHelper(this.store.list),
1134
- _step6;
1167
+ var _iterator7 = _createForOfIteratorHelper(this.store.list),
1168
+ _step7;
1135
1169
  try {
1136
1170
  var _loop = function _loop() {
1137
- var order = _step6.value;
1138
- var identityKeys = _this11.getOrderIdentityKeys(order);
1139
- if (identityKeys.length === 0) return 1; // continue
1140
- var _iterator7 = _createForOfIteratorHelper(identityKeys),
1141
- _step7;
1142
- try {
1143
- for (_iterator7.s(); !(_step7 = _iterator7.n()).done;) {
1144
- var identityKey = _step7.value;
1145
- _this11.store.map.set(identityKey, order);
1146
- }
1147
- } catch (err) {
1148
- _iterator7.e(err);
1149
- } finally {
1150
- _iterator7.f();
1151
- }
1152
- var resourceIds = _this11.extractResourceIds(order);
1153
- var primaryIdentity = identityKeys[0];
1171
+ var order = _step7.value;
1172
+ var primaryIdentity = _this10.getOrderMapKey(order);
1173
+ if (!primaryIdentity) return 1; // continue
1174
+ _this10.store.map.set(primaryIdentity, order);
1175
+ var resourceIds = _this10.extractResourceIds(order);
1154
1176
  var _iterator8 = _createForOfIteratorHelper(resourceIds),
1155
1177
  _step8;
1156
1178
  try {
1157
1179
  for (_iterator8.s(); !(_step8 = _iterator8.n()).done;) {
1158
1180
  var resourceId = _step8.value;
1159
1181
  var key = String(resourceId);
1160
- var existing = _this11.resourceIdIndex.get(key) || [];
1182
+ var existing = _this10.resourceIdIndex.get(key) || [];
1161
1183
  if (existing.some(function (eid) {
1162
- return _this11.getIdKey(eid) === primaryIdentity;
1184
+ return _this10.getIdKey(eid) === primaryIdentity;
1163
1185
  })) continue;
1164
1186
  existing.push(primaryIdentity);
1165
- _this11.resourceIdIndex.set(key, existing);
1187
+ _this10.resourceIdIndex.set(key, existing);
1166
1188
  }
1167
1189
  } catch (err) {
1168
1190
  _iterator8.e(err);
@@ -1170,27 +1192,90 @@ export var OrderModule = /*#__PURE__*/function (_BaseModule) {
1170
1192
  _iterator8.f();
1171
1193
  }
1172
1194
  };
1173
- for (_iterator6.s(); !(_step6 = _iterator6.n()).done;) {
1195
+ for (_iterator7.s(); !(_step7 = _iterator7.n()).done;) {
1174
1196
  if (_loop()) continue;
1175
1197
  }
1176
1198
  } catch (err) {
1177
- _iterator6.e(err);
1199
+ _iterator7.e(err);
1178
1200
  } finally {
1179
- _iterator6.f();
1201
+ _iterator7.f();
1180
1202
  }
1181
1203
  }
1204
+ }, {
1205
+ key: "getOrderMapKey",
1206
+ value: function getOrderMapKey(order) {
1207
+ if (!order) return '';
1208
+ if (!this.isBlankIdentityValue(order.order_id)) {
1209
+ return this.getIdKey(order.order_id);
1210
+ }
1211
+ var externalSaleNumber = order.external_sale_number;
1212
+ if (!this.isBlankIdentityValue(externalSaleNumber)) {
1213
+ return this.getIdKey(externalSaleNumber);
1214
+ }
1215
+ return '';
1216
+ }
1217
+ }, {
1218
+ key: "isBlankIdentityValue",
1219
+ value: function isBlankIdentityValue(value) {
1220
+ return value === undefined || value === null || value === '';
1221
+ }
1222
+ }, {
1223
+ key: "getOrderIdentityEntries",
1224
+ value: function getOrderIdentityEntries(order) {
1225
+ var _this11 = this;
1226
+ if (!order) return [];
1227
+ var record = order;
1228
+ var candidates = [{
1229
+ field: 'order_id',
1230
+ value: order.order_id
1231
+ }, {
1232
+ field: 'external_sale_number',
1233
+ value: record.external_sale_number
1234
+ }, {
1235
+ field: 'order_number',
1236
+ value: record.order_number
1237
+ }, {
1238
+ field: 'shop_order_number',
1239
+ value: record.shop_order_number
1240
+ }, {
1241
+ field: 'shop_full_order_number',
1242
+ value: record.shop_full_order_number
1243
+ }];
1244
+ var entries = [];
1245
+ var _loop2 = function _loop2() {
1246
+ var candidate = _candidates[_i];
1247
+ if (_this11.isBlankIdentityValue(candidate.value)) return 0; // continue
1248
+ var key = _this11.getIdKey(candidate.value);
1249
+ if (entries.some(function (entry) {
1250
+ return entry.field === candidate.field && entry.key === key;
1251
+ })) return 0; // continue
1252
+ entries.push({
1253
+ field: candidate.field,
1254
+ key: key
1255
+ });
1256
+ },
1257
+ _ret;
1258
+ for (var _i = 0, _candidates = candidates; _i < _candidates.length; _i++) {
1259
+ _ret = _loop2();
1260
+ if (_ret === 0) continue;
1261
+ }
1262
+ return entries;
1263
+ }
1182
1264
  }, {
1183
1265
  key: "getOrderIdentityKeys",
1184
1266
  value: function getOrderIdentityKeys(order) {
1185
1267
  var keys = [];
1186
- var id = order.order_id;
1187
- if (id !== undefined && id !== null && id !== '') {
1188
- keys.push(this.getIdKey(id));
1189
- }
1190
- var externalSaleNumber = order === null || order === void 0 ? void 0 : order.external_sale_number;
1191
- if (externalSaleNumber !== undefined && externalSaleNumber !== null && externalSaleNumber !== '') {
1192
- var externalKey = this.getIdKey(externalSaleNumber);
1193
- if (!keys.includes(externalKey)) keys.push(externalKey);
1268
+ var _iterator9 = _createForOfIteratorHelper(this.getOrderIdentityEntries(order)),
1269
+ _step9;
1270
+ try {
1271
+ for (_iterator9.s(); !(_step9 = _iterator9.n()).done;) {
1272
+ var entry = _step9.value;
1273
+ if (!keys.includes(entry.key)) keys.push(entry.key);
1274
+ }
1275
+ } catch (err) {
1276
+ _iterator9.e(err);
1277
+ } finally {
1278
+ _iterator9.f();
1194
1279
  }
1195
1280
  return keys;
1196
1281
  }
@@ -1218,8 +1303,8 @@ export var OrderModule = /*#__PURE__*/function (_BaseModule) {
1218
1303
  value: function getOrderDateKey(order) {
1219
1304
  var _order$bookings, _order$bookings2;
1220
1305
  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];
1221
- for (var _i = 0, _candidates = candidates; _i < _candidates.length; _i++) {
1222
- var v = _candidates[_i];
1306
+ for (var _i2 = 0, _candidates2 = candidates; _i2 < _candidates2.length; _i2++) {
1307
+ var v = _candidates2[_i2];
1223
1308
  if (!v) continue;
1224
1309
  var text = String(v);
1225
1310
  if (!text) continue;
@@ -1536,8 +1621,7 @@ export var OrderModule = /*#__PURE__*/function (_BaseModule) {
1536
1621
  key: "mergeOrdersToStore",
1537
1622
  value: (function () {
1538
1623
  var _mergeOrdersToStore = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee16(freshOrders, source) {
1539
- var _this15 = this;
1540
- var freshMap, _iterator9, _step9, order, id, freshStorageKeyMap, _iterator10, _step10, _order2, storageKey, uniqueFreshCount, patchedOrders, mergeActions, updatedList, _iterator11, _step11, _order3, _id, insertCount, updateCount;
1624
+ var normalizedFreshOrders, _iterator10, _step10, order, id, uniqueFreshOrders, uniqueFreshCount, patchedOrders, mergeActions, updatedList, _iterator11, _step11, fresh, matchIndex, storageKey, mergeKey, mergedOrder, insertCount, updateCount;
1541
1625
  return _regeneratorRuntime().wrap(function _callee16$(_context16) {
1542
1626
  while (1) switch (_context16.prev = _context16.next) {
1543
1627
  case 0:
@@ -1545,134 +1629,109 @@ export var OrderModule = /*#__PURE__*/function (_BaseModule) {
1545
1629
  freshOrderCount: freshOrders.length,
1546
1630
  storeOrderCountBefore: this.store.list.length
1547
1631
  });
1548
- freshMap = new Map();
1549
- _iterator9 = _createForOfIteratorHelper(freshOrders);
1550
- _context16.prev = 3;
1551
- _iterator9.s();
1552
- case 5:
1553
- if ((_step9 = _iterator9.n()).done) {
1554
- _context16.next = 13;
1632
+ this.logDuplicateOrders("".concat(source, ".beforeMerge"), this.store.list);
1633
+ normalizedFreshOrders = [];
1634
+ _iterator10 = _createForOfIteratorHelper(freshOrders);
1635
+ _context16.prev = 4;
1636
+ _iterator10.s();
1637
+ case 6:
1638
+ if ((_step10 = _iterator10.n()).done) {
1639
+ _context16.next = 14;
1555
1640
  break;
1556
1641
  }
1557
- order = _step9.value;
1642
+ order = _step10.value;
1558
1643
  id = order === null || order === void 0 ? void 0 : order.order_id;
1559
1644
  if (!(id === undefined || id === null)) {
1560
- _context16.next = 10;
1645
+ _context16.next = 11;
1561
1646
  break;
1562
1647
  }
1563
- return _context16.abrupt("continue", 11);
1564
- case 10:
1565
- freshMap.set(this.getIdKey(id), this.normalizeRemoteSyncedOrder(order));
1648
+ return _context16.abrupt("continue", 12);
1566
1649
  case 11:
1567
- _context16.next = 5;
1650
+ normalizedFreshOrders.push(this.normalizeRemoteSyncedOrder(order));
1651
+ case 12:
1652
+ _context16.next = 6;
1568
1653
  break;
1569
- case 13:
1570
- _context16.next = 18;
1654
+ case 14:
1655
+ _context16.next = 19;
1571
1656
  break;
1572
- case 15:
1573
- _context16.prev = 15;
1574
- _context16.t0 = _context16["catch"](3);
1575
- _iterator9.e(_context16.t0);
1576
- case 18:
1577
- _context16.prev = 18;
1578
- _iterator9.f();
1579
- return _context16.finish(18);
1580
- case 21:
1581
- freshStorageKeyMap = new Map();
1582
- _iterator10 = _createForOfIteratorHelper(freshMap.values());
1583
- _context16.prev = 23;
1584
- _iterator10.s();
1585
- case 25:
1586
- if ((_step10 = _iterator10.n()).done) {
1587
- _context16.next = 33;
1657
+ case 16:
1658
+ _context16.prev = 16;
1659
+ _context16.t0 = _context16["catch"](4);
1660
+ _iterator10.e(_context16.t0);
1661
+ case 19:
1662
+ _context16.prev = 19;
1663
+ _iterator10.f();
1664
+ return _context16.finish(19);
1665
+ case 22:
1666
+ uniqueFreshOrders = this.compactOrderListByIdentity(normalizedFreshOrders, "".concat(source, ".incoming")).list;
1667
+ uniqueFreshCount = uniqueFreshOrders.length;
1668
+ patchedOrders = [];
1669
+ mergeActions = {};
1670
+ updatedList = _toConsumableArray(this.store.list);
1671
+ _iterator11 = _createForOfIteratorHelper(uniqueFreshOrders);
1672
+ _context16.prev = 28;
1673
+ _iterator11.s();
1674
+ case 30:
1675
+ if ((_step11 = _iterator11.n()).done) {
1676
+ _context16.next = 46;
1588
1677
  break;
1589
1678
  }
1590
- _order2 = _step10.value;
1591
- storageKey = this.getOrderStorageKey(_order2);
1592
- if (storageKey) {
1593
- _context16.next = 30;
1679
+ fresh = _step11.value;
1680
+ matchIndex = this.findOrderIndexByIdentity(updatedList, fresh);
1681
+ storageKey = this.getOrderStorageKey(fresh);
1682
+ mergeKey = storageKey || (fresh.order_id !== undefined && fresh.order_id !== null ? this.getIdKey(fresh.order_id) : '');
1683
+ if (!(matchIndex >= 0)) {
1684
+ _context16.next = 41;
1594
1685
  break;
1595
1686
  }
1596
- return _context16.abrupt("continue", 31);
1597
- case 30:
1598
- freshStorageKeyMap.set(storageKey, _order2);
1599
- case 31:
1600
- _context16.next = 25;
1687
+ mergedOrder = this.mergeOrderRecords(updatedList[matchIndex], fresh);
1688
+ updatedList[matchIndex] = mergedOrder;
1689
+ patchedOrders.push(mergedOrder);
1690
+ if (mergeKey) mergeActions[mergeKey] = 'update';
1691
+ return _context16.abrupt("continue", 44);
1692
+ case 41:
1693
+ updatedList.push(fresh);
1694
+ patchedOrders.push(fresh);
1695
+ if (mergeKey) mergeActions[mergeKey] = 'insert';
1696
+ case 44:
1697
+ _context16.next = 30;
1601
1698
  break;
1602
- case 33:
1603
- _context16.next = 38;
1699
+ case 46:
1700
+ _context16.next = 51;
1604
1701
  break;
1605
- case 35:
1606
- _context16.prev = 35;
1607
- _context16.t1 = _context16["catch"](23);
1608
- _iterator10.e(_context16.t1);
1609
- case 38:
1610
- _context16.prev = 38;
1611
- _iterator10.f();
1612
- return _context16.finish(38);
1613
- case 41:
1614
- uniqueFreshCount = freshMap.size;
1615
- patchedOrders = _toConsumableArray(freshMap.values());
1616
- mergeActions = {};
1617
- updatedList = this.store.list.map(function (order) {
1618
- var storageKey = _this15.getOrderStorageKey(order);
1619
- if (storageKey && freshStorageKeyMap.has(storageKey)) {
1620
- var _fresh = freshStorageKeyMap.get(storageKey);
1621
- freshStorageKeyMap.delete(storageKey);
1622
- var freshId = _fresh.order_id;
1623
- if (freshId !== undefined && freshId !== null) {
1624
- freshMap.delete(_this15.getIdKey(freshId));
1625
- mergeActions[_this15.getIdKey(freshId)] = 'update';
1626
- } else {
1627
- mergeActions[storageKey] = 'update';
1628
- }
1629
- return _fresh;
1630
- }
1631
- var id = order.order_id;
1632
- if (id === undefined || id === null) return order;
1633
- var key = _this15.getIdKey(id);
1634
- if (!freshMap.has(key)) return order;
1635
- var fresh = freshMap.get(key);
1636
- freshMap.delete(key);
1637
- mergeActions[key] = 'update';
1638
- return fresh;
1639
- });
1640
- _iterator11 = _createForOfIteratorHelper(freshMap.values());
1641
- try {
1642
- for (_iterator11.s(); !(_step11 = _iterator11.n()).done;) {
1643
- _order3 = _step11.value;
1644
- _id = _order3.order_id;
1645
- if (_id !== undefined && _id !== null) {
1646
- mergeActions[this.getIdKey(_id)] = 'insert';
1647
- }
1648
- updatedList.push(_order3);
1649
- }
1650
- } catch (err) {
1651
- _iterator11.e(err);
1652
- } finally {
1653
- _iterator11.f();
1654
- }
1702
+ case 48:
1703
+ _context16.prev = 48;
1704
+ _context16.t1 = _context16["catch"](28);
1705
+ _iterator11.e(_context16.t1);
1706
+ case 51:
1707
+ _context16.prev = 51;
1708
+ _iterator11.f();
1709
+ return _context16.finish(51);
1710
+ case 54:
1655
1711
  insertCount = Object.values(mergeActions).filter(function (v) {
1656
1712
  return v === 'insert';
1657
1713
  }).length;
1658
1714
  updateCount = Object.values(mergeActions).filter(function (v) {
1659
1715
  return v === 'update';
1660
1716
  }).length;
1661
- this.store.list = updatedList;
1717
+ this.store.list = this.compactOrderListByIdentity(updatedList, "".concat(source, ".store")).list;
1662
1718
  this.syncOrdersMap();
1663
- _context16.next = 53;
1719
+ _context16.next = 60;
1664
1720
  return this.patchOrdersInSQLite(patchedOrders, source, mergeActions);
1665
- case 53:
1721
+ case 60:
1666
1722
  this.logInfo('mergeOrdersToStore-结束', {
1667
1723
  uniqueFreshCount: uniqueFreshCount,
1668
- storeOrderCountAfter: this.store.list.length
1724
+ storeOrderCountAfter: this.store.list.length,
1725
+ insertCount: insertCount,
1726
+ updateCount: updateCount
1669
1727
  });
1670
- this.emitOrdersChanged(patchedOrders, source);
1671
- case 55:
1728
+ _context16.next = 63;
1729
+ return this.emitOrdersChanged(patchedOrders, source);
1730
+ case 63:
1672
1731
  case "end":
1673
1732
  return _context16.stop();
1674
1733
  }
1675
- }, _callee16, this, [[3, 15, 18, 21], [23, 35, 38, 41]]);
1734
+ }, _callee16, this, [[4, 16, 19, 22], [28, 48, 51, 54]]);
1676
1735
  }));
1677
1736
  function mergeOrdersToStore(_x9, _x10) {
1678
1737
  return _mergeOrdersToStore.apply(this, arguments);
@@ -1733,12 +1792,12 @@ export var OrderModule = /*#__PURE__*/function (_BaseModule) {
1733
1792
  }, {
1734
1793
  key: "isOrderLookupMatch",
1735
1794
  value: function isOrderLookupMatch(order, lookupKey) {
1736
- var _this16 = this;
1795
+ var _this15 = this;
1737
1796
  if (!order || !lookupKey) return false;
1738
1797
  var candidates = [order.order_id, order.external_sale_number, order.order_number, order.shop_order_number, order.shop_full_order_number];
1739
1798
  return candidates.some(function (value) {
1740
1799
  if (value === undefined || value === null || value === '') return false;
1741
- return _this16.getIdKey(value) === lookupKey;
1800
+ return _this15.getIdKey(value) === lookupKey;
1742
1801
  });
1743
1802
  }
1744
1803
 
@@ -1762,6 +1821,179 @@ export var OrderModule = /*#__PURE__*/function (_BaseModule) {
1762
1821
  }
1763
1822
  return '';
1764
1823
  }
1824
+ }, {
1825
+ key: "getOrderIdentityMatchKeys",
1826
+ value: function getOrderIdentityMatchKeys(order) {
1827
+ return this.getOrderIdentityEntries(order).map(function (entry) {
1828
+ return "".concat(entry.field, ":").concat(entry.key);
1829
+ });
1830
+ }
1831
+ }, {
1832
+ key: "doOrdersShareIdentity",
1833
+ value: function doOrdersShareIdentity(left, right) {
1834
+ var leftKeys = new Set(this.getOrderIdentityMatchKeys(left));
1835
+ if (leftKeys.size === 0) return false;
1836
+ return this.getOrderIdentityMatchKeys(right).some(function (key) {
1837
+ return leftKeys.has(key);
1838
+ });
1839
+ }
1840
+ }, {
1841
+ key: "findOrderIndexByIdentity",
1842
+ value: function findOrderIndexByIdentity(orders, target) {
1843
+ var _this16 = this;
1844
+ if (this.getOrderIdentityMatchKeys(target).length === 0) return -1;
1845
+ return orders.findIndex(function (order) {
1846
+ return _this16.doOrdersShareIdentity(order, target);
1847
+ });
1848
+ }
1849
+ }, {
1850
+ key: "getOrderCompletenessScore",
1851
+ value: function getOrderCompletenessScore(order) {
1852
+ var record = order;
1853
+ var score = this.isPendingSyncOrder(order) ? 0 : 100;
1854
+ if (!this.isBlankIdentityValue(order.order_id) && order.order_id !== record.external_sale_number) score += 30;
1855
+ score += this.getOrderIdentityMatchKeys(order).length * 5;
1856
+ if (Array.isArray(order.products)) score += Math.min(order.products.length, 10);
1857
+ if (Array.isArray(order.bookings)) score += Math.min(order.bookings.length, 10);
1858
+ if (Array.isArray(order.payments)) score += Math.min(order.payments.length, 10);
1859
+ if (record.updated_at) score += 1;
1860
+ return score;
1861
+ }
1862
+ }, {
1863
+ key: "pickPreferredOrder",
1864
+ value: function pickPreferredOrder(left, right) {
1865
+ var leftScore = this.getOrderCompletenessScore(left);
1866
+ var rightScore = this.getOrderCompletenessScore(right);
1867
+ if (rightScore >= leftScore) return right;
1868
+ return left;
1869
+ }
1870
+ }, {
1871
+ key: "mergeOrderRecords",
1872
+ value: function mergeOrderRecords(existing, incoming) {
1873
+ var preferred = this.pickPreferredOrder(existing, incoming);
1874
+ var secondary = preferred === existing ? incoming : existing;
1875
+ var merged = _objectSpread(_objectSpread({}, secondary), preferred);
1876
+ var fieldsToPreserve = ['order_id', 'external_sale_number', 'order_number', 'shop_order_number', 'shop_full_order_number'];
1877
+ var mergedRecord = merged;
1878
+ var preferredRecord = preferred;
1879
+ var secondaryRecord = secondary;
1880
+ for (var _i3 = 0, _fieldsToPreserve = fieldsToPreserve; _i3 < _fieldsToPreserve.length; _i3++) {
1881
+ var field = _fieldsToPreserve[_i3];
1882
+ if (!this.isBlankIdentityValue(preferredRecord[field])) {
1883
+ mergedRecord[field] = preferredRecord[field];
1884
+ continue;
1885
+ }
1886
+ if (!this.isBlankIdentityValue(secondaryRecord[field])) {
1887
+ mergedRecord[field] = secondaryRecord[field];
1888
+ }
1889
+ }
1890
+ if (!this.isPendingSyncOrder(preferred) || !this.isPendingSyncOrder(secondary)) {
1891
+ merged.need_sync = 0;
1892
+ }
1893
+ return merged;
1894
+ }
1895
+ }, {
1896
+ key: "summarizeDuplicateOrders",
1897
+ value: function summarizeDuplicateOrders(orders) {
1898
+ var groups = new Map();
1899
+ var _iterator13 = _createForOfIteratorHelper(orders),
1900
+ _step13;
1901
+ try {
1902
+ for (_iterator13.s(); !(_step13 = _iterator13.n()).done;) {
1903
+ var order = _step13.value;
1904
+ var _iterator14 = _createForOfIteratorHelper(this.getOrderIdentityMatchKeys(order)),
1905
+ _step14;
1906
+ try {
1907
+ for (_iterator14.s(); !(_step14 = _iterator14.n()).done;) {
1908
+ var key = _step14.value;
1909
+ var group = groups.get(key) || [];
1910
+ group.push(order);
1911
+ groups.set(key, group);
1912
+ }
1913
+ } catch (err) {
1914
+ _iterator14.e(err);
1915
+ } finally {
1916
+ _iterator14.f();
1917
+ }
1918
+ }
1919
+ } catch (err) {
1920
+ _iterator13.e(err);
1921
+ } finally {
1922
+ _iterator13.f();
1923
+ }
1924
+ return _toConsumableArray(groups.entries()).filter(function (_ref2) {
1925
+ var _ref3 = _slicedToArray(_ref2, 2),
1926
+ group = _ref3[1];
1927
+ return group.length > 1;
1928
+ }).slice(0, 20).map(function (_ref4) {
1929
+ var _ref5 = _slicedToArray(_ref4, 2),
1930
+ key = _ref5[0],
1931
+ group = _ref5[1];
1932
+ return {
1933
+ key: key,
1934
+ count: group.length,
1935
+ orders: group.map(function (order) {
1936
+ var _order$order_id2, _external_sale_number, _shop_order_number, _shop_full_order_numb, _order$need_sync;
1937
+ return {
1938
+ order_id: (_order$order_id2 = order.order_id) !== null && _order$order_id2 !== void 0 ? _order$order_id2 : null,
1939
+ external_sale_number: (_external_sale_number = order.external_sale_number) !== null && _external_sale_number !== void 0 ? _external_sale_number : null,
1940
+ shop_order_number: (_shop_order_number = order.shop_order_number) !== null && _shop_order_number !== void 0 ? _shop_order_number : null,
1941
+ 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,
1942
+ need_sync: (_order$need_sync = order.need_sync) !== null && _order$need_sync !== void 0 ? _order$need_sync : null
1943
+ };
1944
+ })
1945
+ };
1946
+ });
1947
+ }
1948
+ }, {
1949
+ key: "logDuplicateOrders",
1950
+ value: function logDuplicateOrders(source, orders) {
1951
+ var duplicateGroups = this.summarizeDuplicateOrders(orders);
1952
+ if (duplicateGroups.length === 0) return;
1953
+ this.logWarning('检测到重复订单身份键', {
1954
+ source: source,
1955
+ duplicateGroupCount: duplicateGroups.length,
1956
+ duplicateGroups: duplicateGroups
1957
+ });
1958
+ }
1959
+ }, {
1960
+ key: "compactOrderListByIdentity",
1961
+ value: function compactOrderListByIdentity(orders, source) {
1962
+ var list = [];
1963
+ var removedCount = 0;
1964
+ var _iterator15 = _createForOfIteratorHelper(orders),
1965
+ _step15;
1966
+ try {
1967
+ for (_iterator15.s(); !(_step15 = _iterator15.n()).done;) {
1968
+ var order = _step15.value;
1969
+ var matchIndex = this.findOrderIndexByIdentity(list, order);
1970
+ if (matchIndex < 0) {
1971
+ list.push(order);
1972
+ continue;
1973
+ }
1974
+ list[matchIndex] = this.mergeOrderRecords(list[matchIndex], order);
1975
+ removedCount += 1;
1976
+ }
1977
+ } catch (err) {
1978
+ _iterator15.e(err);
1979
+ } finally {
1980
+ _iterator15.f();
1981
+ }
1982
+ if (removedCount > 0) {
1983
+ this.logWarning('订单列表重复项已压缩', {
1984
+ source: source,
1985
+ beforeCount: orders.length,
1986
+ afterCount: list.length,
1987
+ removedCount: removedCount
1988
+ });
1989
+ } else {
1990
+ this.logDuplicateOrders(source, orders);
1991
+ }
1992
+ return {
1993
+ list: list,
1994
+ removedCount: removedCount
1995
+ };
1996
+ }
1765
1997
 
1766
1998
  /**
1767
1999
  * 当前订单拉取窗口:
@@ -1819,7 +2051,7 @@ export var OrderModule = /*#__PURE__*/function (_BaseModule) {
1819
2051
  if (typeof globalThis !== 'undefined' && globalThis !== null && globalThis !== void 0 && globalThis.localStorage) {
1820
2052
  return globalThis.localStorage.getItem(key);
1821
2053
  }
1822
- } catch (_unused5) {
2054
+ } catch (_unused6) {
1823
2055
  // 忽略存储异常,避免影响主流程
1824
2056
  }
1825
2057
  return null;
@@ -1836,7 +2068,7 @@ export var OrderModule = /*#__PURE__*/function (_BaseModule) {
1836
2068
  if (typeof globalThis !== 'undefined' && globalThis !== null && globalThis !== void 0 && globalThis.localStorage) {
1837
2069
  globalThis.localStorage.setItem(key, value);
1838
2070
  }
1839
- } catch (_unused6) {
2071
+ } catch (_unused7) {
1840
2072
  // 忽略存储异常,避免影响主流程
1841
2073
  }
1842
2074
  }
@@ -1895,26 +2127,26 @@ export var OrderModule = /*#__PURE__*/function (_BaseModule) {
1895
2127
  var merged = remoteOrders.map(function (order) {
1896
2128
  return _this17.normalizeRemoteSyncedOrder(order);
1897
2129
  });
1898
- var remoteStorageKeys = new Set(merged.map(function (order) {
1899
- return _this17.getOrderStorageKey(order);
1900
- }).filter(Boolean));
1901
- var _iterator13 = _createForOfIteratorHelper(existingOrders),
1902
- _step13;
2130
+ var _iterator16 = _createForOfIteratorHelper(existingOrders),
2131
+ _step16;
1903
2132
  try {
1904
- for (_iterator13.s(); !(_step13 = _iterator13.n()).done;) {
1905
- var order = _step13.value;
2133
+ for (_iterator16.s(); !(_step16 = _iterator16.n()).done;) {
2134
+ var order = _step16.value;
1906
2135
  if (!this.isPendingSyncOrder(order)) continue;
1907
- var storageKey = this.getOrderStorageKey(order);
1908
- if (!storageKey || remoteStorageKeys.has(storageKey)) continue;
2136
+ var matchIndex = this.findOrderIndexByIdentity(merged, order);
2137
+ if (matchIndex >= 0) {
2138
+ merged[matchIndex] = this.mergeOrderRecords(order, merged[matchIndex]);
2139
+ continue;
2140
+ }
2141
+ if (!this.getOrderStorageKey(order)) continue;
1909
2142
  merged.push(order);
1910
- remoteStorageKeys.add(storageKey);
1911
2143
  }
1912
2144
  } catch (err) {
1913
- _iterator13.e(err);
2145
+ _iterator16.e(err);
1914
2146
  } finally {
1915
- _iterator13.f();
2147
+ _iterator16.f();
1916
2148
  }
1917
- return merged;
2149
+ return this.compactOrderListByIdentity(merged, 'mergeRemoteSnapshotWithPendingOrders').list;
1918
2150
  }
1919
2151
 
1920
2152
  /**
@@ -1968,6 +2200,7 @@ export var OrderModule = /*#__PURE__*/function (_BaseModule) {
1968
2200
  _this$filterRedundant2,
1969
2201
  toWrite,
1970
2202
  skipped,
2203
+ compactedToWrite,
1971
2204
  _args20 = arguments;
1972
2205
  return _regeneratorRuntime().wrap(function _callee20$(_context20) {
1973
2206
  while (1) switch (_context20.prev = _context20.next) {
@@ -1995,17 +2228,24 @@ export var OrderModule = /*#__PURE__*/function (_BaseModule) {
1995
2228
  }
1996
2229
  return _context20.abrupt("return");
1997
2230
  case 9:
1998
- _context20.prev = 9;
1999
- _context20.next = 12;
2231
+ compactedToWrite = this.compactOrderListByIdentity(toWrite, "".concat(source, ".sqlitePatch")).list;
2232
+ if (!(compactedToWrite.length === 0)) {
2233
+ _context20.next = 12;
2234
+ break;
2235
+ }
2236
+ return _context20.abrupt("return");
2237
+ case 12:
2238
+ _context20.prev = 12;
2239
+ _context20.next = 15;
2000
2240
  return this.runInOrderSQLiteSaveQueue( /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee19() {
2001
- var writeMethod, _iterator14, _step14, order;
2241
+ var writeMethod, _iterator17, _step17, order;
2002
2242
  return _regeneratorRuntime().wrap(function _callee19$(_context19) {
2003
2243
  while (1) switch (_context19.prev = _context19.next) {
2004
2244
  case 0:
2005
2245
  writeMethod = 'none';
2006
2246
  _this18.logInfo('patchOrdersInSQLite-开始', {
2007
2247
  source: source,
2008
- count: toWrite.length,
2248
+ count: compactedToWrite.length,
2009
2249
  dedupedCount: skipped.length
2010
2250
  });
2011
2251
  if (!(typeof _this18.dbManager.bulkUpdate === 'function')) {
@@ -2014,7 +2254,7 @@ export var OrderModule = /*#__PURE__*/function (_BaseModule) {
2014
2254
  }
2015
2255
  writeMethod = 'bulkUpdate';
2016
2256
  _context19.next = 6;
2017
- return _this18.dbManager.bulkUpdate(INDEXDB_STORE_NAME, toWrite);
2257
+ return _this18.dbManager.bulkUpdate(INDEXDB_STORE_NAME, compactedToWrite);
2018
2258
  case 6:
2019
2259
  _context19.next = 33;
2020
2260
  break;
@@ -2025,7 +2265,7 @@ export var OrderModule = /*#__PURE__*/function (_BaseModule) {
2025
2265
  }
2026
2266
  writeMethod = 'bulkAdd';
2027
2267
  _context19.next = 12;
2028
- return _this18.dbManager.bulkAdd(INDEXDB_STORE_NAME, toWrite);
2268
+ return _this18.dbManager.bulkAdd(INDEXDB_STORE_NAME, compactedToWrite);
2029
2269
  case 12:
2030
2270
  _context19.next = 33;
2031
2271
  break;
@@ -2035,15 +2275,15 @@ export var OrderModule = /*#__PURE__*/function (_BaseModule) {
2035
2275
  break;
2036
2276
  }
2037
2277
  writeMethod = 'update';
2038
- _iterator14 = _createForOfIteratorHelper(toWrite);
2278
+ _iterator17 = _createForOfIteratorHelper(compactedToWrite);
2039
2279
  _context19.prev = 17;
2040
- _iterator14.s();
2280
+ _iterator17.s();
2041
2281
  case 19:
2042
- if ((_step14 = _iterator14.n()).done) {
2282
+ if ((_step17 = _iterator17.n()).done) {
2043
2283
  _context19.next = 25;
2044
2284
  break;
2045
2285
  }
2046
- order = _step14.value;
2286
+ order = _step17.value;
2047
2287
  _context19.next = 23;
2048
2288
  return _this18.dbManager.update(INDEXDB_STORE_NAME, order);
2049
2289
  case 23:
@@ -2055,16 +2295,16 @@ export var OrderModule = /*#__PURE__*/function (_BaseModule) {
2055
2295
  case 27:
2056
2296
  _context19.prev = 27;
2057
2297
  _context19.t0 = _context19["catch"](17);
2058
- _iterator14.e(_context19.t0);
2298
+ _iterator17.e(_context19.t0);
2059
2299
  case 30:
2060
2300
  _context19.prev = 30;
2061
- _iterator14.f();
2301
+ _iterator17.f();
2062
2302
  return _context19.finish(30);
2063
2303
  case 33:
2064
- _this18.recordRecentSqliteWrite(source, toWrite);
2304
+ _this18.recordRecentSqliteWrite(source, compactedToWrite);
2065
2305
  _this18.logInfo('patchOrdersInSQLite-完成', {
2066
2306
  source: source,
2067
- count: toWrite.length,
2307
+ count: compactedToWrite.length,
2068
2308
  writeMethod: writeMethod,
2069
2309
  dedupedCount: skipped.length
2070
2310
  });
@@ -2074,21 +2314,21 @@ export var OrderModule = /*#__PURE__*/function (_BaseModule) {
2074
2314
  }
2075
2315
  }, _callee19, null, [[17, 27, 30, 33]]);
2076
2316
  })));
2077
- case 12:
2078
- _context20.next = 17;
2317
+ case 15:
2318
+ _context20.next = 20;
2079
2319
  break;
2080
- case 14:
2081
- _context20.prev = 14;
2082
- _context20.t0 = _context20["catch"](9);
2320
+ case 17:
2321
+ _context20.prev = 17;
2322
+ _context20.t0 = _context20["catch"](12);
2083
2323
  this.logError('增量保存订单到 SQLite 失败', {
2084
2324
  error: _context20.t0 instanceof Error ? _context20.t0.message : String(_context20.t0),
2085
2325
  orderCount: ordersSnapshot.length
2086
2326
  });
2087
- case 17:
2327
+ case 20:
2088
2328
  case "end":
2089
2329
  return _context20.stop();
2090
2330
  }
2091
- }, _callee20, this, [[9, 14]]);
2331
+ }, _callee20, this, [[12, 17]]);
2092
2332
  }));
2093
2333
  function patchOrdersInSQLite(_x12, _x13) {
2094
2334
  return _patchOrdersInSQLite.apply(this, arguments);
@@ -2215,14 +2455,14 @@ export var OrderModule = /*#__PURE__*/function (_BaseModule) {
2215
2455
  _context24.next = 2;
2216
2456
  break;
2217
2457
  }
2218
- return _context24.abrupt("return", orderList.map(function (order) {
2458
+ return _context24.abrupt("return", this.compactOrderListByIdentity(orderList.map(function (order) {
2219
2459
  return _this20.normalizeRemoteSyncedOrder(order);
2220
- }));
2460
+ }), "".concat(source, ".snapshotNoDb")).list);
2221
2461
  case 2:
2222
2462
  remoteSnapshot = cloneDeep(orderList);
2223
- mergedSnapshot = remoteSnapshot.map(function (order) {
2463
+ mergedSnapshot = this.compactOrderListByIdentity(remoteSnapshot.map(function (order) {
2224
2464
  return _this20.normalizeRemoteSyncedOrder(order);
2225
- });
2465
+ }), "".concat(source, ".remoteSnapshot")).list;
2226
2466
  _context24.prev = 4;
2227
2467
  _context24.next = 7;
2228
2468
  return this.runInOrderSQLiteSaveQueue( /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee23() {
@@ -2245,32 +2485,32 @@ export var OrderModule = /*#__PURE__*/function (_BaseModule) {
2245
2485
  case 7:
2246
2486
  existingOrders = _context23.t0;
2247
2487
  orderListSnapshot = _this20.mergeRemoteSnapshotWithPendingOrders(remoteSnapshot, existingOrders || []);
2248
- mergedSnapshot = orderListSnapshot;
2488
+ mergedSnapshot = _this20.compactOrderListByIdentity(orderListSnapshot, "".concat(source, ".sqliteSnapshot")).list;
2249
2489
  _this20.logInfo('replaceOrdersSnapshotInSQLite-开始', {
2250
2490
  source: source,
2251
- count: orderListSnapshot.length,
2491
+ count: mergedSnapshot.length,
2252
2492
  remoteCount: remoteSnapshot.length,
2253
- preservedPendingCount: orderListSnapshot.length - remoteSnapshot.length
2493
+ preservedPendingCount: mergedSnapshot.length - remoteSnapshot.length
2254
2494
  });
2255
2495
  _context23.next = 13;
2256
2496
  return _this20.dbManager.clear(INDEXDB_STORE_NAME);
2257
2497
  case 13:
2258
2498
  _this20.logInfo('replaceOrdersSnapshotInSQLite-clear完成', {
2259
- count: orderListSnapshot.length
2499
+ count: mergedSnapshot.length
2260
2500
  });
2261
- if (!(orderListSnapshot.length === 0)) {
2501
+ if (!(mergedSnapshot.length === 0)) {
2262
2502
  _context23.next = 16;
2263
2503
  break;
2264
2504
  }
2265
2505
  return _context23.abrupt("return");
2266
2506
  case 16:
2267
2507
  _context23.next = 18;
2268
- return _this20.dbManager.bulkAdd(INDEXDB_STORE_NAME, orderListSnapshot);
2508
+ return _this20.dbManager.bulkAdd(INDEXDB_STORE_NAME, mergedSnapshot);
2269
2509
  case 18:
2270
- _this20.recordRecentSqliteWrite(source, orderListSnapshot);
2510
+ _this20.recordRecentSqliteWrite(source, mergedSnapshot);
2271
2511
  _this20.logInfo('replaceOrdersSnapshotInSQLite-bulkAdd完成', {
2272
2512
  source: source,
2273
- count: orderListSnapshot.length
2513
+ count: mergedSnapshot.length
2274
2514
  });
2275
2515
  case 20:
2276
2516
  case "end":