@pisell/pisellos 2.2.243 → 2.2.244
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/model/strategy/adapter/promotion/evaluator.js +2 -3
- package/dist/model/strategy/adapter/promotion/index.js +9 -0
- package/dist/model/strategy/adapter/walletPass/utils.js +4 -7
- package/dist/modules/Cart/utils/cartProduct.js +3 -24
- package/dist/modules/Cart/utils/changePrice.js +11 -11
- package/dist/modules/Customer/index.d.ts +0 -20
- package/dist/modules/Customer/index.js +83 -172
- package/dist/modules/Customer/types.d.ts +0 -2
- package/dist/modules/Discount/index.js +3 -4
- package/dist/modules/Discount/types.d.ts +0 -1
- package/dist/modules/OpenData/index.d.ts +0 -8
- package/dist/modules/OpenData/index.js +17 -37
- package/dist/modules/Order/index.d.ts +18 -268
- package/dist/modules/Order/index.js +757 -3306
- package/dist/modules/Order/types.d.ts +31 -279
- package/dist/modules/Order/types.js +0 -38
- package/dist/modules/Order/utils.d.ts +5 -147
- package/dist/modules/Order/utils.js +103 -746
- package/dist/modules/Payment/index.d.ts +13 -10
- package/dist/modules/Payment/index.js +837 -467
- package/dist/modules/Payment/types.d.ts +0 -2
- package/dist/modules/Payment/utils.js +1 -2
- package/dist/modules/Payment/walletpass.js +7 -14
- package/dist/modules/ProductList/index.d.ts +12 -16
- package/dist/modules/ProductList/index.js +59 -133
- package/dist/modules/ProductList/types.d.ts +0 -14
- package/dist/modules/Quotation/index.d.ts +1 -0
- package/dist/modules/Quotation/index.js +22 -25
- package/dist/modules/Rules/index.d.ts +0 -4
- package/dist/modules/Rules/index.js +69 -93
- package/dist/modules/Rules/types.d.ts +0 -1
- package/dist/modules/SalesSummary/index.d.ts +25 -8
- package/dist/modules/SalesSummary/index.js +35 -86
- package/dist/modules/SalesSummary/types.d.ts +1 -4
- package/dist/modules/SalesSummary/utils.d.ts +25 -2
- package/dist/modules/SalesSummary/utils.js +162 -794
- package/dist/modules/Schedule/index.d.ts +0 -7
- package/dist/modules/Schedule/index.js +2 -24
- package/dist/modules/Summary/index.js +3 -4
- package/dist/modules/index.d.ts +0 -2
- package/dist/modules/index.js +1 -3
- package/dist/plugins/request.d.ts +0 -1
- package/dist/server/index.d.ts +1 -204
- package/dist/server/index.js +928 -3658
- package/dist/server/modules/index.d.ts +0 -2
- package/dist/server/modules/index.js +0 -2
- package/dist/server/modules/order/index.d.ts +4 -120
- package/dist/server/modules/order/index.js +483 -1779
- package/dist/server/modules/order/types.d.ts +3 -32
- package/dist/server/modules/order/types.js +0 -8
- package/dist/server/modules/order/utils/filterBookings.js +3 -26
- package/dist/server/modules/order/utils/filterOrders.js +8 -23
- package/dist/server/modules/products/index.d.ts +7 -34
- package/dist/server/modules/products/index.js +317 -631
- package/dist/server/modules/resource/index.d.ts +0 -2
- package/dist/server/modules/resource/index.js +3 -29
- package/dist/server/modules/schedule/index.d.ts +4 -4
- package/dist/server/modules/schedule/index.js +88 -111
- package/dist/solution/BaseSales/index.d.ts +9 -131
- package/dist/solution/BaseSales/index.js +447 -1944
- package/dist/solution/BaseSales/types.d.ts +6 -76
- package/dist/solution/BaseSales/types.js +4 -5
- package/dist/solution/BaseSales/utils/transformBaseProductToOrderProduct.d.ts +0 -1
- package/dist/solution/BaseSales/utils/transformBaseProductToOrderProduct.js +24 -105
- package/dist/solution/BaseSales/utils.d.ts +1 -1
- package/dist/solution/BaseSales/utils.js +21 -62
- package/dist/solution/BookingByStep/index.d.ts +1 -1
- package/dist/solution/BookingTicket/index.d.ts +10 -270
- package/dist/solution/BookingTicket/index.js +237 -1772
- package/dist/solution/BookingTicket/types.d.ts +1 -118
- package/dist/solution/BookingTicket/types.js +2 -41
- package/dist/solution/BookingTicket/utils/bookingStatus.d.ts +0 -40
- package/dist/solution/BookingTicket/utils/bookingStatus.js +0 -70
- package/dist/solution/BookingTicket/utils/cartView.d.ts +1 -9
- package/dist/solution/BookingTicket/utils/cartView.js +7 -94
- package/dist/solution/BookingTicket/utils/scan/index.d.ts +0 -5
- package/dist/solution/BookingTicket/utils/scan/index.js +8 -20
- package/dist/solution/Checkout/appointmentDemo.json +1 -0
- package/dist/solution/Checkout/index.d.ts +0 -1
- package/dist/solution/Checkout/index.js +9 -10
- package/dist/solution/RegisterAndLogin/config.js +10 -2
- package/dist/solution/ScanOrder/index.d.ts +3 -9
- package/dist/solution/ScanOrder/index.js +134 -241
- package/dist/solution/ScanOrder/types.d.ts +5 -9
- package/dist/solution/ScanOrder/types.js +3 -2
- package/dist/solution/ScanOrder/utils.d.ts +1 -1
- package/dist/solution/ScanOrder/utils.js +22 -84
- package/dist/solution/VenueBooking/index.d.ts +2 -5
- package/dist/solution/VenueBooking/index.js +59 -110
- package/dist/solution/VenueBooking/sales-section-4-annotated.json +2 -0
- package/dist/solution/VenueBooking/utils/slotMerge.d.ts +0 -4
- package/dist/solution/VenueBooking/utils/slotMerge.js +0 -10
- package/dist/types/index.d.ts +0 -10
- package/lib/model/strategy/adapter/promotion/evaluator.js +1 -2
- package/lib/model/strategy/adapter/promotion/index.js +0 -49
- package/lib/model/strategy/adapter/walletPass/utils.js +2 -3
- package/lib/modules/Cart/utils/cartProduct.js +3 -20
- package/lib/modules/Cart/utils/changePrice.js +20 -12
- package/lib/modules/Customer/index.d.ts +0 -20
- package/lib/modules/Customer/index.js +11 -64
- package/lib/modules/Customer/types.d.ts +0 -2
- package/lib/modules/Discount/index.js +1 -5
- package/lib/modules/Discount/types.d.ts +0 -1
- package/lib/modules/OpenData/index.d.ts +0 -8
- package/lib/modules/OpenData/index.js +5 -19
- package/lib/modules/Order/index.d.ts +18 -268
- package/lib/modules/Order/index.js +344 -2149
- package/lib/modules/Order/types.d.ts +31 -279
- package/lib/modules/Order/types.js +0 -1
- package/lib/modules/Order/utils.d.ts +5 -147
- package/lib/modules/Order/utils.js +46 -614
- package/lib/modules/Payment/index.d.ts +13 -10
- package/lib/modules/Payment/index.js +150 -18
- package/lib/modules/Payment/types.d.ts +0 -2
- package/lib/modules/Payment/utils.js +1 -2
- package/lib/modules/Payment/walletpass.js +3 -7
- package/lib/modules/ProductList/index.d.ts +12 -16
- package/lib/modules/ProductList/index.js +4 -41
- package/lib/modules/ProductList/types.d.ts +0 -14
- package/lib/modules/Quotation/index.d.ts +1 -0
- package/lib/modules/Quotation/index.js +16 -19
- package/lib/modules/Rules/index.d.ts +0 -4
- package/lib/modules/Rules/index.js +120 -136
- package/lib/modules/Rules/types.d.ts +0 -1
- package/lib/modules/SalesSummary/index.d.ts +25 -8
- package/lib/modules/SalesSummary/index.js +4 -30
- package/lib/modules/SalesSummary/types.d.ts +1 -4
- package/lib/modules/SalesSummary/utils.d.ts +25 -2
- package/lib/modules/SalesSummary/utils.js +91 -645
- package/lib/modules/Schedule/index.d.ts +0 -7
- package/lib/modules/Schedule/index.js +1 -17
- package/lib/modules/Summary/index.js +4 -5
- package/lib/modules/index.d.ts +0 -2
- package/lib/modules/index.js +1 -5
- package/lib/plugins/request.d.ts +0 -1
- package/lib/server/index.d.ts +1 -204
- package/lib/server/index.js +57 -1888
- package/lib/server/modules/index.d.ts +0 -2
- package/lib/server/modules/index.js +0 -3
- package/lib/server/modules/order/index.d.ts +4 -120
- package/lib/server/modules/order/index.js +142 -919
- package/lib/server/modules/order/types.d.ts +3 -32
- package/lib/server/modules/order/utils/filterBookings.js +5 -24
- package/lib/server/modules/order/utils/filterOrders.js +6 -15
- package/lib/server/modules/products/index.d.ts +7 -34
- package/lib/server/modules/products/index.js +54 -189
- package/lib/server/modules/resource/index.d.ts +0 -2
- package/lib/server/modules/resource/index.js +3 -27
- package/lib/server/modules/schedule/index.d.ts +4 -4
- package/lib/server/modules/schedule/index.js +40 -47
- package/lib/solution/BaseSales/index.d.ts +9 -131
- package/lib/solution/BaseSales/index.js +63 -1106
- package/lib/solution/BaseSales/types.d.ts +6 -76
- package/lib/solution/BaseSales/types.js +1 -3
- package/lib/solution/BaseSales/utils/transformBaseProductToOrderProduct.d.ts +0 -1
- package/lib/solution/BaseSales/utils/transformBaseProductToOrderProduct.js +18 -106
- package/lib/solution/BaseSales/utils.d.ts +1 -1
- package/lib/solution/BaseSales/utils.js +10 -54
- package/lib/solution/BookingByStep/index.d.ts +1 -1
- package/lib/solution/BookingTicket/index.d.ts +10 -270
- package/lib/solution/BookingTicket/index.js +29 -922
- package/lib/solution/BookingTicket/types.d.ts +1 -118
- package/lib/solution/BookingTicket/types.js +0 -11
- package/lib/solution/BookingTicket/utils/bookingStatus.d.ts +0 -40
- package/lib/solution/BookingTicket/utils/bookingStatus.js +2 -44
- package/lib/solution/BookingTicket/utils/cartView.d.ts +1 -9
- package/lib/solution/BookingTicket/utils/cartView.js +6 -61
- package/lib/solution/BookingTicket/utils/scan/index.d.ts +0 -5
- package/lib/solution/BookingTicket/utils/scan/index.js +3 -17
- package/lib/solution/Checkout/appointmentDemo.json +1 -0
- package/lib/solution/Checkout/index.d.ts +0 -1
- package/lib/solution/Checkout/index.js +2 -1
- package/lib/solution/RegisterAndLogin/config.js +10 -2
- package/lib/solution/ScanOrder/index.d.ts +3 -9
- package/lib/solution/ScanOrder/index.js +70 -153
- package/lib/solution/ScanOrder/types.d.ts +5 -9
- package/lib/solution/ScanOrder/utils.d.ts +1 -1
- package/lib/solution/ScanOrder/utils.js +17 -83
- package/lib/solution/VenueBooking/index.d.ts +2 -5
- package/lib/solution/VenueBooking/index.js +17 -54
- package/lib/solution/VenueBooking/sales-section-4-annotated.json +2 -0
- package/lib/solution/VenueBooking/utils/slotMerge.d.ts +0 -4
- package/lib/solution/VenueBooking/utils/slotMerge.js +0 -10
- package/lib/types/index.d.ts +0 -10
- package/package.json +1 -1
- package/dist/modules/BookingContext/index.d.ts +0 -48
- package/dist/modules/BookingContext/index.js +0 -566
- package/dist/modules/BookingContext/types.d.ts +0 -102
- package/dist/modules/BookingContext/types.js +0 -51
- package/dist/modules/BookingContext/utils/buildCacheItemFromOrderLine.d.ts +0 -16
- package/dist/modules/BookingContext/utils/buildCacheItemFromOrderLine.js +0 -284
- package/dist/modules/BookingContext/utils/buildNormalProductCacheItemFromOrderLine.d.ts +0 -8
- package/dist/modules/BookingContext/utils/buildNormalProductCacheItemFromOrderLine.js +0 -145
- package/dist/modules/BookingContext/utils/cacheItemToBookingInput.d.ts +0 -76
- package/dist/modules/BookingContext/utils/cacheItemToBookingInput.js +0 -218
- package/dist/modules/BookingContext/utils/flexible.d.ts +0 -28
- package/dist/modules/BookingContext/utils/flexible.js +0 -56
- package/dist/modules/BookingContext/utils/formatResourceList.d.ts +0 -8
- package/dist/modules/BookingContext/utils/formatResourceList.js +0 -38
- package/dist/modules/BookingContext/utils/index.d.ts +0 -11
- package/dist/modules/BookingContext/utils/index.js +0 -11
- package/dist/modules/BookingContext/utils/noResource.d.ts +0 -13
- package/dist/modules/BookingContext/utils/noResource.js +0 -77
- package/dist/modules/BookingContext/utils/orderLineDisplay.d.ts +0 -1
- package/dist/modules/BookingContext/utils/orderLineDisplay.js +0 -36
- package/dist/modules/BookingContext/utils/productExtend.d.ts +0 -72
- package/dist/modules/BookingContext/utils/productExtend.js +0 -339
- package/dist/modules/BookingContext/utils/resourceErrors.d.ts +0 -41
- package/dist/modules/BookingContext/utils/resourceErrors.js +0 -74
- package/dist/modules/BookingContext/utils/resourceSelection.d.ts +0 -7
- package/dist/modules/BookingContext/utils/resourceSelection.js +0 -24
- package/dist/modules/BookingContext/utils/resources.d.ts +0 -80
- package/dist/modules/BookingContext/utils/resources.js +0 -486
- package/dist/modules/BookingContext/utils/serviceTimes.d.ts +0 -55
- package/dist/modules/BookingContext/utils/serviceTimes.js +0 -151
- package/dist/modules/BookingContext/utils/timeSlices.d.ts +0 -42
- package/dist/modules/BookingContext/utils/timeSlices.js +0 -100
- package/dist/modules/Order/utils/manualProductDiscount.d.ts +0 -106
- package/dist/modules/Order/utils/manualProductDiscount.js +0 -210
- package/dist/modules/SurchargeList/index.d.ts +0 -16
- package/dist/modules/SurchargeList/index.js +0 -162
- package/dist/modules/SurchargeList/types.d.ts +0 -11
- package/dist/modules/SurchargeList/types.js +0 -1
- package/dist/server/modules/payment/index.d.ts +0 -28
- package/dist/server/modules/payment/index.js +0 -305
- package/dist/server/modules/payment/types.d.ts +0 -9
- package/dist/server/modules/payment/types.js +0 -1
- package/dist/server/test.json +0 -713
- package/dist/server/utils/small-ticket.d.ts +0 -71
- package/dist/server/utils/small-ticket.js +0 -830
- package/dist/solution/BaseSales/utils/cartPromotion.d.ts +0 -287
- package/dist/solution/BaseSales/utils/cartPromotion.js +0 -1438
- package/dist/solution/BaseSales/utils/quotationPrice.d.ts +0 -7
- package/dist/solution/BaseSales/utils/quotationPrice.js +0 -237
- package/dist/solution/BookingTicket/utils/addProductDecision.d.ts +0 -117
- package/dist/solution/BookingTicket/utils/addProductDecision.js +0 -346
- package/dist/solution/BookingTicket/utils/addTimeAvailability.d.ts +0 -35
- package/dist/solution/BookingTicket/utils/addTimeAvailability.js +0 -95
- package/dist/solution/BookingTicket/utils/exampleData.d.ts +0 -123
- package/dist/solution/BookingTicket/utils/exampleData.js +0 -183
- package/dist/solution/BookingTicket/utils/resolveBestAddTimePlan.d.ts +0 -185
- package/dist/solution/BookingTicket/utils/resolveBestAddTimePlan.js +0 -438
- package/dist/solution/BookingTicket/utils/scan/applyGlobalScan.d.ts +0 -36
- package/dist/solution/BookingTicket/utils/scan/applyGlobalScan.js +0 -399
- package/dist/solution/BookingTicket/utils/scan/formatGlobalScan.d.ts +0 -32
- package/dist/solution/BookingTicket/utils/scan/formatGlobalScan.js +0 -56
- package/dist/solution/BookingTicket/utils/scan/scanProductValues.d.ts +0 -21
- package/dist/solution/BookingTicket/utils/scan/scanProductValues.js +0 -150
- package/dist/solution/BookingTicket/utils/sessionCatalogStale.d.ts +0 -23
- package/dist/solution/BookingTicket/utils/sessionCatalogStale.js +0 -78
- package/lib/modules/BookingContext/index.d.ts +0 -48
- package/lib/modules/BookingContext/index.js +0 -368
- package/lib/modules/BookingContext/types.d.ts +0 -102
- package/lib/modules/BookingContext/types.js +0 -34
- package/lib/modules/BookingContext/utils/buildCacheItemFromOrderLine.d.ts +0 -16
- package/lib/modules/BookingContext/utils/buildCacheItemFromOrderLine.js +0 -276
- package/lib/modules/BookingContext/utils/buildNormalProductCacheItemFromOrderLine.d.ts +0 -8
- package/lib/modules/BookingContext/utils/buildNormalProductCacheItemFromOrderLine.js +0 -151
- package/lib/modules/BookingContext/utils/cacheItemToBookingInput.d.ts +0 -76
- package/lib/modules/BookingContext/utils/cacheItemToBookingInput.js +0 -201
- package/lib/modules/BookingContext/utils/flexible.d.ts +0 -28
- package/lib/modules/BookingContext/utils/flexible.js +0 -80
- package/lib/modules/BookingContext/utils/formatResourceList.d.ts +0 -8
- package/lib/modules/BookingContext/utils/formatResourceList.js +0 -50
- package/lib/modules/BookingContext/utils/index.d.ts +0 -11
- package/lib/modules/BookingContext/utils/index.js +0 -43
- package/lib/modules/BookingContext/utils/noResource.d.ts +0 -13
- package/lib/modules/BookingContext/utils/noResource.js +0 -90
- package/lib/modules/BookingContext/utils/orderLineDisplay.d.ts +0 -1
- package/lib/modules/BookingContext/utils/orderLineDisplay.js +0 -56
- package/lib/modules/BookingContext/utils/productExtend.d.ts +0 -72
- package/lib/modules/BookingContext/utils/productExtend.js +0 -283
- package/lib/modules/BookingContext/utils/resourceErrors.d.ts +0 -41
- package/lib/modules/BookingContext/utils/resourceErrors.js +0 -80
- package/lib/modules/BookingContext/utils/resourceSelection.d.ts +0 -7
- package/lib/modules/BookingContext/utils/resourceSelection.js +0 -46
- package/lib/modules/BookingContext/utils/resources.d.ts +0 -80
- package/lib/modules/BookingContext/utils/resources.js +0 -377
- package/lib/modules/BookingContext/utils/serviceTimes.d.ts +0 -55
- package/lib/modules/BookingContext/utils/serviceTimes.js +0 -162
- package/lib/modules/BookingContext/utils/timeSlices.d.ts +0 -42
- package/lib/modules/BookingContext/utils/timeSlices.js +0 -124
- package/lib/modules/Order/utils/manualProductDiscount.d.ts +0 -106
- package/lib/modules/Order/utils/manualProductDiscount.js +0 -206
- package/lib/modules/SurchargeList/index.d.ts +0 -16
- package/lib/modules/SurchargeList/index.js +0 -89
- package/lib/modules/SurchargeList/types.d.ts +0 -11
- package/lib/modules/SurchargeList/types.js +0 -17
- package/lib/server/modules/payment/index.d.ts +0 -28
- package/lib/server/modules/payment/index.js +0 -192
- package/lib/server/modules/payment/types.d.ts +0 -9
- package/lib/server/modules/payment/types.js +0 -17
- package/lib/server/test.json +0 -713
- package/lib/server/utils/small-ticket.d.ts +0 -71
- package/lib/server/utils/small-ticket.js +0 -781
- package/lib/solution/BaseSales/utils/cartPromotion.d.ts +0 -287
- package/lib/solution/BaseSales/utils/cartPromotion.js +0 -992
- package/lib/solution/BaseSales/utils/quotationPrice.d.ts +0 -7
- package/lib/solution/BaseSales/utils/quotationPrice.js +0 -277
- package/lib/solution/BookingTicket/utils/addProductDecision.d.ts +0 -117
- package/lib/solution/BookingTicket/utils/addProductDecision.js +0 -318
- package/lib/solution/BookingTicket/utils/addTimeAvailability.d.ts +0 -35
- package/lib/solution/BookingTicket/utils/addTimeAvailability.js +0 -125
- package/lib/solution/BookingTicket/utils/exampleData.d.ts +0 -123
- package/lib/solution/BookingTicket/utils/exampleData.js +0 -0
- package/lib/solution/BookingTicket/utils/resolveBestAddTimePlan.d.ts +0 -185
- package/lib/solution/BookingTicket/utils/resolveBestAddTimePlan.js +0 -254
- package/lib/solution/BookingTicket/utils/scan/applyGlobalScan.d.ts +0 -36
- package/lib/solution/BookingTicket/utils/scan/applyGlobalScan.js +0 -168
- package/lib/solution/BookingTicket/utils/scan/formatGlobalScan.d.ts +0 -32
- package/lib/solution/BookingTicket/utils/scan/formatGlobalScan.js +0 -51
- package/lib/solution/BookingTicket/utils/scan/scanProductValues.d.ts +0 -21
- package/lib/solution/BookingTicket/utils/scan/scanProductValues.js +0 -127
- package/lib/solution/BookingTicket/utils/sessionCatalogStale.d.ts +0 -23
- package/lib/solution/BookingTicket/utils/sessionCatalogStale.js +0 -85
|
@@ -40,14 +40,6 @@ var INDEXDB_STORE_NAME = "orders";
|
|
|
40
40
|
var ORDER_LAST_FULL_FETCH_AT_STORAGE_KEY = "server_order_last_full_fetch_at";
|
|
41
41
|
var ORDER_SYNC_THROTTLE_MS_STORAGE_KEY = "order_sync_throttle_ms";
|
|
42
42
|
var DEFAULT_ORDER_SYNC_THROTTLE_MS = 2e3;
|
|
43
|
-
var ORDER_SQLITE_DEDUPE_MS = 15e3;
|
|
44
|
-
var ORDER_SILENT_REFRESH_MIN_INTERVAL_MS = 3e4;
|
|
45
|
-
var ORDER_BUSINESS_WRITE_SOURCES = /* @__PURE__ */ new Set([
|
|
46
|
-
"upsertOrdersFromRemote",
|
|
47
|
-
"upsertPendingSyncOrders",
|
|
48
|
-
"overwriteExistingOrder",
|
|
49
|
-
"markOrderSyncedByExternalSaleNumber"
|
|
50
|
-
]);
|
|
51
43
|
var OrderModule = class extends import_BaseModule.BaseModule {
|
|
52
44
|
constructor(name, version) {
|
|
53
45
|
super(name, version);
|
|
@@ -58,29 +50,9 @@ var OrderModule = class extends import_BaseModule.BaseModule {
|
|
|
58
50
|
this.isIdlePhase = true;
|
|
59
51
|
// Map<resource_id, OrderId[]> 资源到订单的倒排索引
|
|
60
52
|
this.resourceIdIndex = /* @__PURE__ */ new Map();
|
|
61
|
-
this.orderSQLiteSaveQueue = Promise.resolve();
|
|
62
|
-
/** 按 storageKey 记录最近一次 SQLite 写入来源与时间,用于去重 */
|
|
63
|
-
this.recentSqliteWriteByStorageKey = /* @__PURE__ */ new Map();
|
|
64
|
-
/** 最近一次 SSE 全量拉取完成时间 */
|
|
65
|
-
this.lastServerFullFetchAtMs = 0;
|
|
66
|
-
}
|
|
67
|
-
/**
|
|
68
|
-
* 广播订单变更事件,兼容全量列表订阅并额外携带本次变更订单。
|
|
69
|
-
*
|
|
70
|
-
* @example
|
|
71
|
-
* await this.emitOrdersChanged([freshOrder], 'pubsub.bodyUpsert')
|
|
72
|
-
*/
|
|
73
|
-
async emitOrdersChanged(changedOrders = [], source) {
|
|
74
|
-
console.log("触发emitOrdersChanged");
|
|
75
|
-
const payload = {
|
|
76
|
-
list: this.store.list,
|
|
77
|
-
changedOrders: changedOrders.map((order) => this.normalizeOrderForMemoryList(order)),
|
|
78
|
-
source
|
|
79
|
-
};
|
|
80
|
-
await this.core.effects.emit(import_types.OrderHooks.onOrdersChanged, payload);
|
|
81
53
|
}
|
|
82
54
|
async initialize(core, options) {
|
|
83
|
-
var _a
|
|
55
|
+
var _a;
|
|
84
56
|
this.core = core;
|
|
85
57
|
this.store = options == null ? void 0 : options.store;
|
|
86
58
|
if (!this.store.map)
|
|
@@ -91,51 +63,12 @@ var OrderModule = class extends import_BaseModule.BaseModule {
|
|
|
91
63
|
this.dbManager = app.sqlite;
|
|
92
64
|
this.logger = app.logger;
|
|
93
65
|
this.storage = app.storage;
|
|
94
|
-
|
|
95
|
-
if (!((_a = this.store.createdAtQuery) == null ? void 0 : _a.sales_time_between) && (coreData == null ? void 0 : coreData.core)) {
|
|
96
|
-
const today = (0, import_dayjs.default)();
|
|
97
|
-
if (coreData == null ? void 0 : coreData.core.operating_day_boundary) {
|
|
98
|
-
const operatingDayBoundary = coreData.core.operating_day_boundary;
|
|
99
|
-
if (operatingDayBoundary.type === "start_time") {
|
|
100
|
-
const boundaryTime = String(operatingDayBoundary.time || "").trim();
|
|
101
|
-
const boundaryStart = (0, import_dayjs.default)(`${today.format("YYYY-MM-DD")} ${boundaryTime}`);
|
|
102
|
-
if (boundaryStart.isValid()) {
|
|
103
|
-
const isAfterBoundary = today.isAfter(boundaryStart);
|
|
104
|
-
const queryStart = isAfterBoundary ? today.startOf("day") : today.subtract(1, "day").startOf("day");
|
|
105
|
-
const queryEnd = isAfterBoundary ? today.add(1, "day").endOf("day") : today.endOf("day");
|
|
106
|
-
this.store.createdAtQuery = {
|
|
107
|
-
sales_time_between: [
|
|
108
|
-
queryStart.format("YYYY-MM-DD HH:mm:ss"),
|
|
109
|
-
queryEnd.format("YYYY-MM-DD HH:mm:ss")
|
|
110
|
-
]
|
|
111
|
-
};
|
|
112
|
-
} else {
|
|
113
|
-
this.store.createdAtQuery = {
|
|
114
|
-
sales_time_between: [
|
|
115
|
-
today.startOf("day").format("YYYY-MM-DD HH:mm:ss"),
|
|
116
|
-
today.endOf("day").format("YYYY-MM-DD HH:mm:ss")
|
|
117
|
-
]
|
|
118
|
-
};
|
|
119
|
-
}
|
|
120
|
-
} else {
|
|
121
|
-
this.store.createdAtQuery = {
|
|
122
|
-
sales_time_between: [
|
|
123
|
-
today.startOf("day").format("YYYY-MM-DD HH:mm:ss"),
|
|
124
|
-
today.endOf("day").format("YYYY-MM-DD HH:mm:ss")
|
|
125
|
-
]
|
|
126
|
-
};
|
|
127
|
-
}
|
|
128
|
-
} else {
|
|
129
|
-
this.store.createdAtQuery = {
|
|
130
|
-
sales_time_between: [today.startOf("day").format("YYYY-MM-DD HH:mm:ss"), today.endOf("day").format("YYYY-MM-DD HH:mm:ss")]
|
|
131
|
-
};
|
|
132
|
-
}
|
|
133
|
-
}
|
|
66
|
+
this.updateCreateStore();
|
|
134
67
|
}
|
|
135
|
-
if (Array.isArray((
|
|
136
|
-
this.store.list = options.initialState.list
|
|
68
|
+
if (Array.isArray((_a = options == null ? void 0 : options.initialState) == null ? void 0 : _a.list)) {
|
|
69
|
+
this.store.list = options.initialState.list;
|
|
137
70
|
this.syncOrdersMap();
|
|
138
|
-
this.
|
|
71
|
+
this.core.effects.emit(import_types.OrderHooks.onOrdersChanged, this.store.list);
|
|
139
72
|
} else {
|
|
140
73
|
this.store.list = [];
|
|
141
74
|
this.store.map = /* @__PURE__ */ new Map();
|
|
@@ -182,170 +115,52 @@ var OrderModule = class extends import_BaseModule.BaseModule {
|
|
|
182
115
|
} catch {
|
|
183
116
|
}
|
|
184
117
|
}
|
|
185
|
-
|
|
186
|
-
try {
|
|
187
|
-
if (this.logger) {
|
|
188
|
-
this.logger.addLog({
|
|
189
|
-
type: "warning",
|
|
190
|
-
title: `[OrderServerModule] ${title}`,
|
|
191
|
-
metadata: metadata || {}
|
|
192
|
-
});
|
|
193
|
-
}
|
|
194
|
-
} catch {
|
|
195
|
-
}
|
|
196
|
-
}
|
|
197
|
-
/**
|
|
198
|
-
* 记录订单最近一次 SQLite 写入,供 pubsub 回声去重使用。
|
|
199
|
-
*
|
|
200
|
-
* @example
|
|
201
|
-
* this.recordRecentSqliteWrite('upsertOrdersFromRemote', orders)
|
|
202
|
-
*/
|
|
203
|
-
recordRecentSqliteWrite(source, orders) {
|
|
204
|
-
const now = Date.now();
|
|
205
|
-
for (const order of orders) {
|
|
206
|
-
const storageKey = this.getOrderStorageKey(order);
|
|
207
|
-
if (!storageKey)
|
|
208
|
-
continue;
|
|
209
|
-
this.recentSqliteWriteByStorageKey.set(storageKey, { source, ts: now });
|
|
210
|
-
}
|
|
211
|
-
}
|
|
212
|
-
/**
|
|
213
|
-
* 过滤 pubsub 触发的冗余 SQLite patch(业务 API 刚写入过的 update 回声)。
|
|
214
|
-
*
|
|
215
|
-
* @example
|
|
216
|
-
* const { toWrite, skipped } = this.filterRedundantPatchOrders('pubsub.bodyUpsert', orders, mergeActions)
|
|
217
|
-
*/
|
|
218
|
-
filterRedundantPatchOrders(source, orders, mergeActions) {
|
|
219
|
-
const eligibleSkipSources = /* @__PURE__ */ new Set(["pubsub.bodyUpsert", "pubsub.httpRefresh"]);
|
|
220
|
-
if (!eligibleSkipSources.has(source)) {
|
|
221
|
-
return { toWrite: orders, skipped: [] };
|
|
222
|
-
}
|
|
223
|
-
const now = Date.now();
|
|
224
|
-
const toWrite = [];
|
|
225
|
-
const skipped = [];
|
|
226
|
-
for (const order of orders) {
|
|
227
|
-
const storageKey = this.getOrderStorageKey(order);
|
|
228
|
-
if (!storageKey) {
|
|
229
|
-
toWrite.push(order);
|
|
230
|
-
continue;
|
|
231
|
-
}
|
|
232
|
-
const orderKey = order.order_id !== void 0 && order.order_id !== null ? this.getIdKey(order.order_id) : "";
|
|
233
|
-
const mergeAction = orderKey ? mergeActions[orderKey] : void 0;
|
|
234
|
-
if (mergeAction === "insert") {
|
|
235
|
-
toWrite.push(order);
|
|
236
|
-
continue;
|
|
237
|
-
}
|
|
238
|
-
const recent = this.recentSqliteWriteByStorageKey.get(storageKey);
|
|
239
|
-
if (recent && ORDER_BUSINESS_WRITE_SOURCES.has(recent.source) && now - recent.ts < ORDER_SQLITE_DEDUPE_MS) {
|
|
240
|
-
skipped.push({
|
|
241
|
-
orderId: order.order_id ?? null,
|
|
242
|
-
storageKey,
|
|
243
|
-
reason: "skip_pubsub_echo_after_business_write",
|
|
244
|
-
lastSource: recent.source,
|
|
245
|
-
elapsedMs: now - recent.ts
|
|
246
|
-
});
|
|
247
|
-
continue;
|
|
248
|
-
}
|
|
249
|
-
toWrite.push(order);
|
|
250
|
-
}
|
|
251
|
-
return { toWrite, skipped };
|
|
252
|
-
}
|
|
253
|
-
/**
|
|
254
|
-
* 过滤 pubsub HTTP 增量拉取 ids:本地刚由业务 API 写入的单可跳过 HTTP。
|
|
255
|
-
*
|
|
256
|
-
* @example
|
|
257
|
-
* const ids = this.filterRedundantHttpRefreshIds([99331, 99332])
|
|
258
|
-
*/
|
|
259
|
-
filterRedundantHttpRefreshIds(ids) {
|
|
260
|
-
const now = Date.now();
|
|
261
|
-
const toFetch = [];
|
|
262
|
-
const skipped = [];
|
|
263
|
-
for (const id of ids) {
|
|
264
|
-
const existing = this.getOrderByOrderId(id);
|
|
265
|
-
if (!existing) {
|
|
266
|
-
toFetch.push(id);
|
|
267
|
-
continue;
|
|
268
|
-
}
|
|
269
|
-
const storageKey = this.getOrderStorageKey(existing);
|
|
270
|
-
const recent = storageKey ? this.recentSqliteWriteByStorageKey.get(storageKey) : void 0;
|
|
271
|
-
if (recent && recent.source === "upsertOrdersFromRemote" && now - recent.ts < ORDER_SQLITE_DEDUPE_MS) {
|
|
272
|
-
skipped.push({
|
|
273
|
-
orderId: id,
|
|
274
|
-
lastSource: recent.source,
|
|
275
|
-
elapsedMs: now - recent.ts
|
|
276
|
-
});
|
|
277
|
-
continue;
|
|
278
|
-
}
|
|
279
|
-
toFetch.push(id);
|
|
280
|
-
}
|
|
281
|
-
return { toFetch, skipped };
|
|
282
|
-
}
|
|
283
|
-
/**
|
|
284
|
-
* 解析 pubsub 消息会走哪条同步分支,便于对照为何触发 HTTP 重拉或 body 直写。
|
|
285
|
-
*
|
|
286
|
-
* @example
|
|
287
|
-
* this.resolveSyncMessageRoute(msg)
|
|
288
|
-
* // => { route: 'httpRefresh.batchIds', ... }
|
|
289
|
-
*/
|
|
290
|
-
resolveSyncMessageRoute(msg) {
|
|
118
|
+
updateCreateStore() {
|
|
291
119
|
var _a;
|
|
292
|
-
const
|
|
293
|
-
const
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
singleId: null,
|
|
336
|
-
batchIds: msg.relation_order_ids,
|
|
337
|
-
bodyOrderId: (normalizedBody == null ? void 0 : normalizedBody.order_id) ?? null
|
|
338
|
-
};
|
|
120
|
+
const appPlugin = this.core.getPlugin("app");
|
|
121
|
+
const app = appPlugin == null ? void 0 : appPlugin.getApp();
|
|
122
|
+
if (app) {
|
|
123
|
+
const coreData = app.data.store.getStore().getDataByModel("core");
|
|
124
|
+
if (!((_a = this.store.createdAtQuery) == null ? void 0 : _a.sales_time_between) && (coreData == null ? void 0 : coreData.core)) {
|
|
125
|
+
const today = (0, import_dayjs.default)();
|
|
126
|
+
if (coreData == null ? void 0 : coreData.core.operating_day_boundary) {
|
|
127
|
+
const operatingDayBoundary = coreData.core.operating_day_boundary;
|
|
128
|
+
if (operatingDayBoundary.type === "start_time") {
|
|
129
|
+
const boundaryTime = String(operatingDayBoundary.time || "").trim();
|
|
130
|
+
const boundaryStart = (0, import_dayjs.default)(`${today.format("YYYY-MM-DD")} ${boundaryTime}`);
|
|
131
|
+
if (boundaryStart.isValid()) {
|
|
132
|
+
const isAfterBoundary = today.isAfter(boundaryStart);
|
|
133
|
+
const queryStart = isAfterBoundary ? today.startOf("day") : today.subtract(1, "day").startOf("day");
|
|
134
|
+
const queryEnd = isAfterBoundary ? today.add(1, "day").endOf("day") : today.endOf("day");
|
|
135
|
+
this.store.createdAtQuery = {
|
|
136
|
+
sales_time_between: [
|
|
137
|
+
queryStart.format("YYYY-MM-DD HH:mm:ss"),
|
|
138
|
+
queryEnd.format("YYYY-MM-DD HH:mm:ss")
|
|
139
|
+
]
|
|
140
|
+
};
|
|
141
|
+
} else {
|
|
142
|
+
this.store.createdAtQuery = {
|
|
143
|
+
sales_time_between: [
|
|
144
|
+
today.startOf("day").format("YYYY-MM-DD HH:mm:ss"),
|
|
145
|
+
today.endOf("day").format("YYYY-MM-DD HH:mm:ss")
|
|
146
|
+
]
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
} else {
|
|
150
|
+
this.store.createdAtQuery = {
|
|
151
|
+
sales_time_between: [
|
|
152
|
+
today.startOf("day").format("YYYY-MM-DD HH:mm:ss"),
|
|
153
|
+
today.endOf("day").format("YYYY-MM-DD HH:mm:ss")
|
|
154
|
+
]
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
} else {
|
|
158
|
+
this.store.createdAtQuery = {
|
|
159
|
+
sales_time_between: [today.startOf("day").format("YYYY-MM-DD HH:mm:ss"), today.endOf("day").format("YYYY-MM-DD HH:mm:ss")]
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
}
|
|
339
163
|
}
|
|
340
|
-
return {
|
|
341
|
-
route: "noop",
|
|
342
|
-
hasBatchIds: false,
|
|
343
|
-
hasSingleId: false,
|
|
344
|
-
hasNormalizedBody: !!normalizedBody,
|
|
345
|
-
singleId: null,
|
|
346
|
-
batchIds: [],
|
|
347
|
-
bodyOrderId: (normalizedBody == null ? void 0 : normalizedBody.order_id) ?? null
|
|
348
|
-
};
|
|
349
164
|
}
|
|
350
165
|
getOrderSyncThrottleMs() {
|
|
351
166
|
const raw = localStorage.getItem(ORDER_SYNC_THROTTLE_MS_STORAGE_KEY);
|
|
@@ -359,12 +174,11 @@ var OrderModule = class extends import_BaseModule.BaseModule {
|
|
|
359
174
|
async preload() {
|
|
360
175
|
const getData = async () => {
|
|
361
176
|
const orders = await this.loadOrdersByServer();
|
|
362
|
-
if (orders.length === 0)
|
|
177
|
+
if (orders.length === 0)
|
|
363
178
|
return;
|
|
364
|
-
}
|
|
365
179
|
this.store.list = (0, import_lodash_es.cloneDeep)(orders);
|
|
366
180
|
this.syncOrdersMap();
|
|
367
|
-
this.
|
|
181
|
+
this.core.effects.emit(import_types.OrderHooks.onOrdersChanged, this.store.list);
|
|
368
182
|
this.core.effects.emit(import_types.OrderHooks.onOrdersSyncCompleted, null);
|
|
369
183
|
};
|
|
370
184
|
getData();
|
|
@@ -375,33 +189,30 @@ var OrderModule = class extends import_BaseModule.BaseModule {
|
|
|
375
189
|
getOrderById(id) {
|
|
376
190
|
return this.store.map.get(this.getIdKey(id));
|
|
377
191
|
}
|
|
378
|
-
/** 按 order_id
|
|
192
|
+
/** 按 order_id 查询(推荐) */
|
|
379
193
|
getOrderByOrderId(orderId) {
|
|
380
194
|
return this.store.map.get(this.getIdKey(orderId));
|
|
381
195
|
}
|
|
382
|
-
|
|
383
|
-
const key = this.getIdKey(
|
|
384
|
-
const
|
|
385
|
-
if (memoryOrderById)
|
|
386
|
-
return memoryOrderById;
|
|
387
|
-
const memoryOrder = this.store.list.find((order) => this.isOrderLookupMatch(order, key));
|
|
388
|
-
return memoryOrder || null;
|
|
389
|
-
}
|
|
390
|
-
async getLocalOrderByLookup(lookup) {
|
|
391
|
-
const key = this.getIdKey(lookup);
|
|
392
|
-
const memoryOrderById = this.store.map.get(key);
|
|
393
|
-
if (memoryOrderById)
|
|
394
|
-
return memoryOrderById;
|
|
395
|
-
const memoryOrder = this.store.list.find((order) => this.isOrderLookupMatch(order, key));
|
|
196
|
+
async getLocalOrderByOrderId(orderId) {
|
|
197
|
+
const key = this.getIdKey(orderId);
|
|
198
|
+
const memoryOrder = this.store.map.get(key);
|
|
396
199
|
if (memoryOrder)
|
|
397
200
|
return memoryOrder;
|
|
398
201
|
const orders = await this.loadOrdersFromSQLite();
|
|
399
|
-
const localOrder = orders.find((order) =>
|
|
202
|
+
const localOrder = orders.find((order) => {
|
|
203
|
+
const shopOrderNumber = order == null ? void 0 : order.shop_order_number;
|
|
204
|
+
if (shopOrderNumber && this.getIdKey(shopOrderNumber) === key)
|
|
205
|
+
return true;
|
|
206
|
+
const id = order == null ? void 0 : order.order_id;
|
|
207
|
+
if (id === void 0 || id === null)
|
|
208
|
+
return false;
|
|
209
|
+
return this.getIdKey(id) === key;
|
|
210
|
+
});
|
|
400
211
|
if (!localOrder)
|
|
401
212
|
return null;
|
|
402
|
-
this.store.list = orders
|
|
213
|
+
this.store.list = orders;
|
|
403
214
|
this.syncOrdersMap();
|
|
404
|
-
return
|
|
215
|
+
return localOrder;
|
|
405
216
|
}
|
|
406
217
|
async loadOrdersByServer() {
|
|
407
218
|
var _a;
|
|
@@ -422,19 +233,17 @@ var OrderModule = class extends import_BaseModule.BaseModule {
|
|
|
422
233
|
count: orderList.length
|
|
423
234
|
});
|
|
424
235
|
this.markOrderPulledAtNow();
|
|
425
|
-
this.lastServerFullFetchAtMs = Date.now();
|
|
426
236
|
} catch {
|
|
427
|
-
|
|
428
|
-
|
|
237
|
+
orderList = [];
|
|
238
|
+
this.logInfo("loadOrdersByServer-SSE拉取失败,回退为空数组");
|
|
429
239
|
}
|
|
430
240
|
}
|
|
431
|
-
|
|
432
|
-
const memoryOrders = mergedOrders.map((order) => this.normalizeOrderForMemoryList(order));
|
|
241
|
+
await this.saveOrdersToSQLite(orderList);
|
|
433
242
|
this.logInfo("loadOrdersByServer-结束", {
|
|
434
|
-
count:
|
|
243
|
+
count: orderList.length
|
|
435
244
|
});
|
|
436
|
-
await this.core.effects.emit(import_types.OrderHooks.onOrdersLoaded,
|
|
437
|
-
return
|
|
245
|
+
await this.core.effects.emit(import_types.OrderHooks.onOrdersLoaded, orderList);
|
|
246
|
+
return orderList;
|
|
438
247
|
}
|
|
439
248
|
/**
|
|
440
249
|
* 静默全量刷新:后台重新拉取全量 SSE 订单数据并更新本地
|
|
@@ -443,17 +252,14 @@ var OrderModule = class extends import_BaseModule.BaseModule {
|
|
|
443
252
|
*/
|
|
444
253
|
async silentRefresh() {
|
|
445
254
|
const startTime = Date.now();
|
|
446
|
-
const elapsedSinceFullFetch = Date.now() - this.lastServerFullFetchAtMs;
|
|
447
|
-
if (this.lastServerFullFetchAtMs > 0 && elapsedSinceFullFetch < ORDER_SILENT_REFRESH_MIN_INTERVAL_MS) {
|
|
448
|
-
return this.store.list;
|
|
449
|
-
}
|
|
450
255
|
this.logInfo("silentRefresh 开始");
|
|
451
256
|
try {
|
|
257
|
+
this.updateCreateStore();
|
|
452
258
|
const orders = await this.loadOrdersByServer();
|
|
453
259
|
if (orders.length > 0) {
|
|
454
260
|
this.store.list = (0, import_lodash_es.cloneDeep)(orders);
|
|
455
261
|
this.syncOrdersMap();
|
|
456
|
-
this.
|
|
262
|
+
this.core.effects.emit(import_types.OrderHooks.onOrdersChanged, this.store.list);
|
|
457
263
|
await this.core.effects.emit(import_types.OrderHooks.onOrdersSyncCompleted, null);
|
|
458
264
|
this.logInfo("silentRefresh 完成", {
|
|
459
265
|
orderCount: orders.length,
|
|
@@ -476,19 +282,6 @@ var OrderModule = class extends import_BaseModule.BaseModule {
|
|
|
476
282
|
const ids = this.resourceIdIndex.get(String(resourceId)) || [];
|
|
477
283
|
return ids.map((id) => this.store.map.get(id)).filter((order) => !!order);
|
|
478
284
|
}
|
|
479
|
-
normalizeOrderForMemoryList(order) {
|
|
480
|
-
const externalSaleNumber = order == null ? void 0 : order.external_sale_number;
|
|
481
|
-
if ((order == null ? void 0 : order.order_id) !== void 0 && (order == null ? void 0 : order.order_id) !== null && (order == null ? void 0 : order.order_id) !== "") {
|
|
482
|
-
return order;
|
|
483
|
-
}
|
|
484
|
-
if (externalSaleNumber === void 0 || externalSaleNumber === null || externalSaleNumber === "") {
|
|
485
|
-
return order;
|
|
486
|
-
}
|
|
487
|
-
return {
|
|
488
|
-
...order,
|
|
489
|
-
order_id: externalSaleNumber
|
|
490
|
-
};
|
|
491
|
-
}
|
|
492
285
|
/**
|
|
493
286
|
* 仅覆盖本地已存在的订单;不存在则直接跳过,不落库、不 emit。
|
|
494
287
|
* 适用于"按 order_id 主动刷新本地订单详情"的代理场景。
|
|
@@ -515,12 +308,12 @@ var OrderModule = class extends import_BaseModule.BaseModule {
|
|
|
515
308
|
return this.getIdKey(id) === key ? fresh : order;
|
|
516
309
|
});
|
|
517
310
|
this.syncOrdersMap();
|
|
518
|
-
await this.
|
|
311
|
+
await this.saveOrdersToSQLite(this.store.list);
|
|
519
312
|
this.logInfo("overwriteExistingOrder-结束", {
|
|
520
313
|
orderId,
|
|
521
314
|
storeOrderCountAfter: this.store.list.length
|
|
522
315
|
});
|
|
523
|
-
await this.
|
|
316
|
+
await this.core.effects.emit(import_types.OrderHooks.onOrdersChanged, this.store.list);
|
|
524
317
|
return { overwritten: true };
|
|
525
318
|
}
|
|
526
319
|
/**
|
|
@@ -533,102 +326,7 @@ var OrderModule = class extends import_BaseModule.BaseModule {
|
|
|
533
326
|
return;
|
|
534
327
|
}
|
|
535
328
|
this.logInfo("upsertOrdersFromRemote-开始", { count: freshOrders.length });
|
|
536
|
-
await this.mergeOrdersToStore(freshOrders
|
|
537
|
-
}
|
|
538
|
-
async markOrderSyncedByExternalSaleNumber(params) {
|
|
539
|
-
const externalSaleNumber = params.externalSaleNumber;
|
|
540
|
-
if (externalSaleNumber === void 0 || externalSaleNumber === null || externalSaleNumber === "") {
|
|
541
|
-
this.logError("markOrderSyncedByExternalSaleNumber-缺少 external_sale_number");
|
|
542
|
-
return { updated: false };
|
|
543
|
-
}
|
|
544
|
-
const key = this.getIdKey(externalSaleNumber);
|
|
545
|
-
const targetIndex = this.store.list.findIndex((order) => {
|
|
546
|
-
const value = order == null ? void 0 : order.external_sale_number;
|
|
547
|
-
if (value === void 0 || value === null || value === "")
|
|
548
|
-
return false;
|
|
549
|
-
return this.getIdKey(value) === key;
|
|
550
|
-
});
|
|
551
|
-
if (targetIndex < 0) {
|
|
552
|
-
this.logInfo("markOrderSyncedByExternalSaleNumber-本地订单不存在,跳过", {
|
|
553
|
-
external_sale_number: externalSaleNumber
|
|
554
|
-
});
|
|
555
|
-
return { updated: false };
|
|
556
|
-
}
|
|
557
|
-
const existing = this.store.list[targetIndex];
|
|
558
|
-
const nextOrder = this.normalizeRemoteSyncedOrder({
|
|
559
|
-
...existing,
|
|
560
|
-
...params.patch || {},
|
|
561
|
-
external_sale_number: existing.external_sale_number ?? externalSaleNumber,
|
|
562
|
-
need_sync: 0
|
|
563
|
-
});
|
|
564
|
-
const storageKey = this.getOrderStorageKey(nextOrder) || key;
|
|
565
|
-
this.store.list = this.store.list.map((order, index) => {
|
|
566
|
-
if (index !== targetIndex)
|
|
567
|
-
return order;
|
|
568
|
-
return nextOrder;
|
|
569
|
-
});
|
|
570
|
-
this.syncOrdersMap();
|
|
571
|
-
await this.patchOrdersInSQLite(
|
|
572
|
-
[nextOrder],
|
|
573
|
-
"markOrderSyncedByExternalSaleNumber",
|
|
574
|
-
{ [storageKey]: "update" }
|
|
575
|
-
);
|
|
576
|
-
this.logInfo("markOrderSyncedByExternalSaleNumber-完成", {
|
|
577
|
-
external_sale_number: externalSaleNumber,
|
|
578
|
-
order_id: nextOrder.order_id ?? null
|
|
579
|
-
});
|
|
580
|
-
await this.emitOrdersChanged([nextOrder], "markOrderSyncedByExternalSaleNumber");
|
|
581
|
-
return { updated: true, order: nextOrder };
|
|
582
|
-
}
|
|
583
|
-
/**
|
|
584
|
-
* 将本地待同步订单按 SQLite storage key 合并进 store 并落库。
|
|
585
|
-
* checkout 离线兜底可能没有 order_id,但通常会有 external_sale_number。
|
|
586
|
-
*/
|
|
587
|
-
async upsertPendingSyncOrders(pendingOrders) {
|
|
588
|
-
if (!(pendingOrders == null ? void 0 : pendingOrders.length)) {
|
|
589
|
-
this.logInfo("upsertPendingSyncOrders-订单列表为空", {});
|
|
590
|
-
return;
|
|
591
|
-
}
|
|
592
|
-
const pendingMap = /* @__PURE__ */ new Map();
|
|
593
|
-
const memoryPendingMap = /* @__PURE__ */ new Map();
|
|
594
|
-
for (const order of pendingOrders) {
|
|
595
|
-
const storageKey = this.getOrderStorageKey(order);
|
|
596
|
-
if (!storageKey)
|
|
597
|
-
continue;
|
|
598
|
-
pendingMap.set(storageKey, order);
|
|
599
|
-
memoryPendingMap.set(storageKey, this.normalizeOrderForMemoryList(order));
|
|
600
|
-
}
|
|
601
|
-
if (pendingMap.size === 0) {
|
|
602
|
-
this.logError("upsertPendingSyncOrders-订单缺少可落库标识", {
|
|
603
|
-
count: pendingOrders.length
|
|
604
|
-
});
|
|
605
|
-
return;
|
|
606
|
-
}
|
|
607
|
-
this.logInfo("upsertPendingSyncOrders-开始", { count: pendingMap.size });
|
|
608
|
-
const patchedOrders = [...pendingMap.values()];
|
|
609
|
-
const mergeActions = {};
|
|
610
|
-
const updatedList = [...this.store.list];
|
|
611
|
-
for (const [storageKey, pendingOrder] of memoryPendingMap.entries()) {
|
|
612
|
-
const matchIndex = this.findOrderIndexByIdentity(updatedList, pendingOrder);
|
|
613
|
-
if (matchIndex < 0)
|
|
614
|
-
continue;
|
|
615
|
-
updatedList[matchIndex] = this.mergeOrderRecords(updatedList[matchIndex], pendingOrder);
|
|
616
|
-
pendingMap.delete(storageKey);
|
|
617
|
-
memoryPendingMap.delete(storageKey);
|
|
618
|
-
mergeActions[storageKey] = "update";
|
|
619
|
-
}
|
|
620
|
-
for (const [storageKey, order] of memoryPendingMap.entries()) {
|
|
621
|
-
mergeActions[storageKey] = "insert";
|
|
622
|
-
updatedList.push(order);
|
|
623
|
-
}
|
|
624
|
-
this.store.list = this.compactOrderListByIdentity(updatedList, "upsertPendingSyncOrders.store").list;
|
|
625
|
-
this.syncOrdersMap();
|
|
626
|
-
await this.patchOrdersInSQLite(patchedOrders, "upsertPendingSyncOrders", mergeActions);
|
|
627
|
-
this.logInfo("upsertPendingSyncOrders-结束", {
|
|
628
|
-
count: patchedOrders.length,
|
|
629
|
-
storeOrderCountAfter: this.store.list.length
|
|
630
|
-
});
|
|
631
|
-
await this.emitOrdersChanged(patchedOrders, "upsertPendingSyncOrders");
|
|
329
|
+
await this.mergeOrdersToStore(freshOrders);
|
|
632
330
|
}
|
|
633
331
|
/**
|
|
634
332
|
* 通过 SSE 按自定义 query 拉取订单(支持 select/with 精简字段)
|
|
@@ -665,65 +363,21 @@ var OrderModule = class extends import_BaseModule.BaseModule {
|
|
|
665
363
|
this.store.map.clear();
|
|
666
364
|
this.resourceIdIndex.clear();
|
|
667
365
|
for (const order of this.store.list) {
|
|
668
|
-
const
|
|
669
|
-
if (
|
|
366
|
+
const id = order.order_id;
|
|
367
|
+
if (id === void 0 || id === null)
|
|
670
368
|
continue;
|
|
671
|
-
this.store.map.set(
|
|
369
|
+
this.store.map.set(this.getIdKey(id), order);
|
|
672
370
|
const resourceIds = this.extractResourceIds(order);
|
|
673
371
|
for (const resourceId of resourceIds) {
|
|
674
372
|
const key = String(resourceId);
|
|
675
373
|
const existing = this.resourceIdIndex.get(key) || [];
|
|
676
|
-
if (existing.some((eid) => this.getIdKey(eid) ===
|
|
374
|
+
if (existing.some((eid) => this.getIdKey(eid) === this.getIdKey(id)))
|
|
677
375
|
continue;
|
|
678
|
-
existing.push(
|
|
376
|
+
existing.push(id);
|
|
679
377
|
this.resourceIdIndex.set(key, existing);
|
|
680
378
|
}
|
|
681
379
|
}
|
|
682
380
|
}
|
|
683
|
-
getOrderMapKey(order) {
|
|
684
|
-
if (!order)
|
|
685
|
-
return "";
|
|
686
|
-
if (!this.isBlankIdentityValue(order.order_id)) {
|
|
687
|
-
return this.getIdKey(order.order_id);
|
|
688
|
-
}
|
|
689
|
-
const externalSaleNumber = order.external_sale_number;
|
|
690
|
-
if (!this.isBlankIdentityValue(externalSaleNumber)) {
|
|
691
|
-
return this.getIdKey(externalSaleNumber);
|
|
692
|
-
}
|
|
693
|
-
return "";
|
|
694
|
-
}
|
|
695
|
-
isBlankIdentityValue(value) {
|
|
696
|
-
return value === void 0 || value === null || value === "";
|
|
697
|
-
}
|
|
698
|
-
getOrderIdentityEntries(order) {
|
|
699
|
-
if (!order)
|
|
700
|
-
return [];
|
|
701
|
-
const record = order;
|
|
702
|
-
const candidates = [
|
|
703
|
-
{ field: "order_id", value: order.order_id },
|
|
704
|
-
{ field: "external_sale_number", value: record.external_sale_number },
|
|
705
|
-
{ field: "order_number", value: record.order_number },
|
|
706
|
-
{ field: "shop_order_number", value: record.shop_order_number }
|
|
707
|
-
];
|
|
708
|
-
const entries = [];
|
|
709
|
-
for (const candidate of candidates) {
|
|
710
|
-
if (this.isBlankIdentityValue(candidate.value))
|
|
711
|
-
continue;
|
|
712
|
-
const key = this.getIdKey(candidate.value);
|
|
713
|
-
if (entries.some((entry) => entry.field === candidate.field && entry.key === key))
|
|
714
|
-
continue;
|
|
715
|
-
entries.push({ field: candidate.field, key });
|
|
716
|
-
}
|
|
717
|
-
return entries;
|
|
718
|
-
}
|
|
719
|
-
getOrderIdentityKeys(order) {
|
|
720
|
-
const keys = [];
|
|
721
|
-
for (const entry of this.getOrderIdentityEntries(order)) {
|
|
722
|
-
if (!keys.includes(entry.key))
|
|
723
|
-
keys.push(entry.key);
|
|
724
|
-
}
|
|
725
|
-
return keys;
|
|
726
|
-
}
|
|
727
381
|
extractResourceIds(order) {
|
|
728
382
|
const ids = /* @__PURE__ */ new Set();
|
|
729
383
|
const visitResources = (resources) => {
|
|
@@ -780,7 +434,7 @@ var OrderModule = class extends import_BaseModule.BaseModule {
|
|
|
780
434
|
this.orderDataSource.run({
|
|
781
435
|
pubsub: {
|
|
782
436
|
callback: (res) => {
|
|
783
|
-
console.log("orderDataSource"
|
|
437
|
+
console.log("orderDataSource");
|
|
784
438
|
const pubsubReceivedAt = Date.now();
|
|
785
439
|
const message = this.normalizeOrderSyncMessage(res);
|
|
786
440
|
if (!message)
|
|
@@ -788,15 +442,13 @@ var OrderModule = class extends import_BaseModule.BaseModule {
|
|
|
788
442
|
message._pubsubReceivedAt = pubsubReceivedAt;
|
|
789
443
|
this.pendingSyncMessages.push(message);
|
|
790
444
|
const throttleMs = this.getOrderSyncThrottleMs();
|
|
791
|
-
const routeInfo = this.resolveSyncMessageRoute(message);
|
|
792
445
|
this.logInfo("orderSync-收到消息并入队", {
|
|
793
446
|
id: message.id ?? null,
|
|
794
447
|
order_id: message.order_id ?? null,
|
|
795
448
|
type: message.type ?? null,
|
|
796
449
|
pendingCount: this.pendingSyncMessages.length,
|
|
797
450
|
throttleMs,
|
|
798
|
-
pubsubReceivedAt: new Date(pubsubReceivedAt).toISOString()
|
|
799
|
-
fullMessage: JSON.stringify(message)
|
|
451
|
+
pubsubReceivedAt: new Date(pubsubReceivedAt).toISOString()
|
|
800
452
|
});
|
|
801
453
|
this.scheduleOrderSyncThrottle();
|
|
802
454
|
}
|
|
@@ -862,6 +514,7 @@ var OrderModule = class extends import_BaseModule.BaseModule {
|
|
|
862
514
|
* await this.processOrderSyncMessages()
|
|
863
515
|
*/
|
|
864
516
|
async processOrderSyncMessages() {
|
|
517
|
+
var _a;
|
|
865
518
|
const messages = [...this.pendingSyncMessages];
|
|
866
519
|
this.pendingSyncMessages = [];
|
|
867
520
|
if (messages.length === 0)
|
|
@@ -878,47 +531,38 @@ var OrderModule = class extends import_BaseModule.BaseModule {
|
|
|
878
531
|
});
|
|
879
532
|
const upsertOrders = /* @__PURE__ */ new Map();
|
|
880
533
|
const refreshIds = [];
|
|
881
|
-
|
|
882
|
-
const
|
|
883
|
-
const
|
|
884
|
-
const
|
|
885
|
-
const
|
|
886
|
-
const normalizedBody = this.normalizeOrderSyncPayload(
|
|
887
|
-
if (
|
|
534
|
+
for (const msg of messages) {
|
|
535
|
+
const hasBatchIds = Array.isArray(msg.ids) && msg.ids.length > 0;
|
|
536
|
+
const singleId = msg.id ?? msg.order_id;
|
|
537
|
+
const hasSingleId = !hasBatchIds && singleId !== void 0 && singleId !== null;
|
|
538
|
+
const rawBody = msg.body || msg.data;
|
|
539
|
+
const normalizedBody = this.normalizeOrderSyncPayload(rawBody);
|
|
540
|
+
if (hasSingleId && normalizedBody) {
|
|
888
541
|
upsertOrders.set(this.getIdKey(normalizedBody.order_id), normalizedBody);
|
|
889
|
-
|
|
542
|
+
continue;
|
|
890
543
|
}
|
|
891
544
|
if (hasBatchIds)
|
|
892
|
-
refreshIds.push(...
|
|
893
|
-
else if (
|
|
545
|
+
refreshIds.push(...msg.ids);
|
|
546
|
+
else if (hasSingleId)
|
|
894
547
|
refreshIds.push(singleId);
|
|
895
|
-
else if (
|
|
896
|
-
refreshIds.push(...
|
|
897
|
-
}
|
|
548
|
+
else if ((_a = msg.relation_order_ids) == null ? void 0 : _a.length)
|
|
549
|
+
refreshIds.push(...msg.relation_order_ids);
|
|
550
|
+
}
|
|
898
551
|
const uniqueRefreshIds = this.uniqueOrderIds(refreshIds);
|
|
899
552
|
const upsertList = [...upsertOrders.values()];
|
|
900
553
|
this.logInfo("processOrderSyncMessages-去重完成", {
|
|
901
554
|
upsertCount: upsertList.length,
|
|
902
555
|
refreshIdCount: uniqueRefreshIds.length
|
|
903
556
|
});
|
|
904
|
-
if (upsertList.length > 0)
|
|
905
|
-
await this.mergeOrdersToStore(upsertList
|
|
906
|
-
}
|
|
557
|
+
if (upsertList.length > 0)
|
|
558
|
+
await this.mergeOrdersToStore(upsertList);
|
|
907
559
|
if (uniqueRefreshIds.length > 0) {
|
|
908
|
-
const
|
|
909
|
-
if (
|
|
910
|
-
|
|
911
|
-
if (toFetch.length > 0) {
|
|
912
|
-
const freshOrders = await this.fetchOrdersByHttp(toFetch);
|
|
913
|
-
if (freshOrders.length > 0) {
|
|
914
|
-
await this.mergeOrdersToStore(freshOrders, "pubsub.httpRefresh");
|
|
915
|
-
} else {
|
|
916
|
-
}
|
|
917
|
-
}
|
|
560
|
+
const freshOrders = await this.fetchOrdersByHttp(uniqueRefreshIds);
|
|
561
|
+
if (freshOrders.length > 0)
|
|
562
|
+
await this.mergeOrdersToStore(freshOrders);
|
|
918
563
|
}
|
|
919
|
-
if (upsertList.length === 0 && uniqueRefreshIds.length === 0)
|
|
564
|
+
if (upsertList.length === 0 && uniqueRefreshIds.length === 0)
|
|
920
565
|
return;
|
|
921
|
-
}
|
|
922
566
|
const batchProcessEndAt = Date.now();
|
|
923
567
|
this.logInfo("processOrderSyncMessages-结束", {
|
|
924
568
|
upsertCount: upsertList.length,
|
|
@@ -959,62 +603,41 @@ var OrderModule = class extends import_BaseModule.BaseModule {
|
|
|
959
603
|
/**
|
|
960
604
|
* 将增量订单合并到 store
|
|
961
605
|
*/
|
|
962
|
-
async mergeOrdersToStore(freshOrders
|
|
606
|
+
async mergeOrdersToStore(freshOrders) {
|
|
963
607
|
this.logInfo("mergeOrdersToStore-开始", {
|
|
964
608
|
freshOrderCount: freshOrders.length,
|
|
965
|
-
storeOrderCountBefore: this.store.list.length
|
|
966
|
-
source
|
|
609
|
+
storeOrderCountBefore: this.store.list.length
|
|
967
610
|
});
|
|
968
|
-
|
|
969
|
-
const normalizedFreshOrders = [];
|
|
611
|
+
const freshMap = /* @__PURE__ */ new Map();
|
|
970
612
|
for (const order of freshOrders) {
|
|
971
613
|
const id = order == null ? void 0 : order.order_id;
|
|
972
614
|
if (id === void 0 || id === null)
|
|
973
615
|
continue;
|
|
974
|
-
|
|
975
|
-
}
|
|
976
|
-
const uniqueFreshOrders = this.compactOrderListByIdentity(
|
|
977
|
-
normalizedFreshOrders,
|
|
978
|
-
`${source}.incoming`
|
|
979
|
-
).list;
|
|
980
|
-
const uniqueFreshCount = uniqueFreshOrders.length;
|
|
981
|
-
const patchedOrders = [];
|
|
982
|
-
const mergeActions = {};
|
|
983
|
-
const updatedList = [...this.store.list];
|
|
984
|
-
for (const fresh of uniqueFreshOrders) {
|
|
985
|
-
const matchIndex = this.findOrderIndexByIdentity(updatedList, fresh);
|
|
986
|
-
const storageKey = this.getOrderStorageKey(fresh);
|
|
987
|
-
const mergeKey = storageKey || (fresh.order_id !== void 0 && fresh.order_id !== null ? this.getIdKey(fresh.order_id) : "");
|
|
988
|
-
if (matchIndex >= 0) {
|
|
989
|
-
const mergedOrder = this.mergeOrderRecords(updatedList[matchIndex], fresh);
|
|
990
|
-
updatedList[matchIndex] = mergedOrder;
|
|
991
|
-
patchedOrders.push(mergedOrder);
|
|
992
|
-
if (mergeKey)
|
|
993
|
-
mergeActions[mergeKey] = "update";
|
|
994
|
-
continue;
|
|
995
|
-
}
|
|
996
|
-
updatedList.push(fresh);
|
|
997
|
-
patchedOrders.push(fresh);
|
|
998
|
-
if (mergeKey)
|
|
999
|
-
mergeActions[mergeKey] = "insert";
|
|
616
|
+
freshMap.set(this.getIdKey(id), order);
|
|
1000
617
|
}
|
|
1001
|
-
const
|
|
1002
|
-
const
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
618
|
+
const uniqueFreshCount = freshMap.size;
|
|
619
|
+
const updatedList = this.store.list.map((order) => {
|
|
620
|
+
const id = order.order_id;
|
|
621
|
+
if (id === void 0 || id === null)
|
|
622
|
+
return order;
|
|
623
|
+
const key = this.getIdKey(id);
|
|
624
|
+
if (!freshMap.has(key))
|
|
625
|
+
return order;
|
|
626
|
+
const fresh = freshMap.get(key);
|
|
627
|
+
freshMap.delete(key);
|
|
628
|
+
return fresh;
|
|
1007
629
|
});
|
|
1008
|
-
|
|
630
|
+
for (const order of freshMap.values()) {
|
|
631
|
+
updatedList.push(order);
|
|
632
|
+
}
|
|
633
|
+
this.store.list = updatedList;
|
|
1009
634
|
this.syncOrdersMap();
|
|
1010
|
-
await this.
|
|
635
|
+
await this.saveOrdersToSQLite(this.store.list);
|
|
1011
636
|
this.logInfo("mergeOrdersToStore-结束", {
|
|
1012
637
|
uniqueFreshCount,
|
|
1013
|
-
storeOrderCountAfter: this.store.list.length
|
|
1014
|
-
insertCount,
|
|
1015
|
-
updateCount
|
|
638
|
+
storeOrderCountAfter: this.store.list.length
|
|
1016
639
|
});
|
|
1017
|
-
|
|
640
|
+
this.core.effects.emit(import_types.OrderHooks.onOrdersChanged, this.store.list);
|
|
1018
641
|
}
|
|
1019
642
|
normalizeOrderSyncMessage(res) {
|
|
1020
643
|
const data = (res == null ? void 0 : res.data) || res;
|
|
@@ -1059,224 +682,6 @@ var OrderModule = class extends import_BaseModule.BaseModule {
|
|
|
1059
682
|
getIdKey(id) {
|
|
1060
683
|
return String(id);
|
|
1061
684
|
}
|
|
1062
|
-
isOrderLookupMatch(order, lookupKey) {
|
|
1063
|
-
if (!order || !lookupKey)
|
|
1064
|
-
return false;
|
|
1065
|
-
const candidates = [
|
|
1066
|
-
order.order_id,
|
|
1067
|
-
order.external_sale_number,
|
|
1068
|
-
order.order_number,
|
|
1069
|
-
order.shop_order_number,
|
|
1070
|
-
order.shop_full_order_number
|
|
1071
|
-
];
|
|
1072
|
-
return candidates.some((value) => {
|
|
1073
|
-
if (value === void 0 || value === null || value === "")
|
|
1074
|
-
return false;
|
|
1075
|
-
return this.getIdKey(value) === lookupKey;
|
|
1076
|
-
});
|
|
1077
|
-
}
|
|
1078
|
-
/**
|
|
1079
|
-
* 解析订单 SQLite 主键,规则与宿主 OrderDataSource.getOrderStorageKey 一致。
|
|
1080
|
-
*
|
|
1081
|
-
* @example
|
|
1082
|
-
* const key = this.getOrderStorageKey({ order_id: 1001, external_sale_number: 'S-1001' })
|
|
1083
|
-
* // => 'S-1001'
|
|
1084
|
-
*/
|
|
1085
|
-
getOrderStorageKey(order) {
|
|
1086
|
-
const externalSaleNumber = order == null ? void 0 : order.external_sale_number;
|
|
1087
|
-
if (externalSaleNumber !== void 0 && externalSaleNumber !== null && externalSaleNumber !== "") {
|
|
1088
|
-
return String(externalSaleNumber);
|
|
1089
|
-
}
|
|
1090
|
-
const orderId = order == null ? void 0 : order.order_id;
|
|
1091
|
-
if (orderId !== void 0 && orderId !== null && orderId !== "") {
|
|
1092
|
-
return String(orderId);
|
|
1093
|
-
}
|
|
1094
|
-
return "";
|
|
1095
|
-
}
|
|
1096
|
-
getOrderIdentityMatchKeys(order) {
|
|
1097
|
-
return this.getOrderIdentityEntries(order).map((entry) => `${entry.field}:${entry.key}`);
|
|
1098
|
-
}
|
|
1099
|
-
doOrdersShareIdentity(left, right) {
|
|
1100
|
-
const leftKeys = new Set(this.getOrderIdentityMatchKeys(left));
|
|
1101
|
-
if (leftKeys.size === 0)
|
|
1102
|
-
return false;
|
|
1103
|
-
return this.getOrderIdentityMatchKeys(right).some((key) => leftKeys.has(key));
|
|
1104
|
-
}
|
|
1105
|
-
findOrderIndexByIdentity(orders, target) {
|
|
1106
|
-
if (this.getOrderIdentityMatchKeys(target).length === 0)
|
|
1107
|
-
return -1;
|
|
1108
|
-
return orders.findIndex((order) => this.doOrdersShareIdentity(order, target));
|
|
1109
|
-
}
|
|
1110
|
-
getOrderCompletenessScore(order) {
|
|
1111
|
-
const record = order;
|
|
1112
|
-
let score = this.isPendingSyncOrder(order) ? 0 : 100;
|
|
1113
|
-
if (!this.isBlankIdentityValue(order.order_id) && order.order_id !== record.external_sale_number)
|
|
1114
|
-
score += 30;
|
|
1115
|
-
score += this.getOrderIdentityMatchKeys(order).length * 5;
|
|
1116
|
-
if (Array.isArray(order.products))
|
|
1117
|
-
score += Math.min(order.products.length, 10);
|
|
1118
|
-
if (Array.isArray(order.bookings))
|
|
1119
|
-
score += Math.min(order.bookings.length, 10);
|
|
1120
|
-
if (Array.isArray(order.payments))
|
|
1121
|
-
score += Math.min(order.payments.length, 10);
|
|
1122
|
-
if (record.updated_at)
|
|
1123
|
-
score += 1;
|
|
1124
|
-
return score;
|
|
1125
|
-
}
|
|
1126
|
-
pickPreferredOrder(left, right) {
|
|
1127
|
-
const leftScore = this.getOrderCompletenessScore(left);
|
|
1128
|
-
const rightScore = this.getOrderCompletenessScore(right);
|
|
1129
|
-
if (rightScore >= leftScore)
|
|
1130
|
-
return right;
|
|
1131
|
-
return left;
|
|
1132
|
-
}
|
|
1133
|
-
getProductMergeKey(product) {
|
|
1134
|
-
var _a;
|
|
1135
|
-
const record = product;
|
|
1136
|
-
if (!record)
|
|
1137
|
-
return "";
|
|
1138
|
-
const metadataUid = (_a = record.metadata) == null ? void 0 : _a.unique_identification_number;
|
|
1139
|
-
if (!this.isBlankIdentityValue(metadataUid))
|
|
1140
|
-
return `uid:${String(metadataUid)}`;
|
|
1141
|
-
const topLevelUid = record.unique_identification_number;
|
|
1142
|
-
if (!this.isBlankIdentityValue(topLevelUid))
|
|
1143
|
-
return `uid:${String(topLevelUid)}`;
|
|
1144
|
-
const bookingUid = record.booking_uid;
|
|
1145
|
-
if (!this.isBlankIdentityValue(bookingUid))
|
|
1146
|
-
return `booking:${String(bookingUid)}`;
|
|
1147
|
-
return "";
|
|
1148
|
-
}
|
|
1149
|
-
mergeOrderProductLists(preferred, secondary) {
|
|
1150
|
-
const preferredList = Array.isArray(preferred) ? preferred : [];
|
|
1151
|
-
const secondaryList = Array.isArray(secondary) ? secondary : [];
|
|
1152
|
-
if (secondaryList.length === 0)
|
|
1153
|
-
return preferredList;
|
|
1154
|
-
if (preferredList.length === 0)
|
|
1155
|
-
return secondaryList;
|
|
1156
|
-
const seenKeys = /* @__PURE__ */ new Set();
|
|
1157
|
-
for (const item of preferredList) {
|
|
1158
|
-
const key = this.getProductMergeKey(item);
|
|
1159
|
-
if (key)
|
|
1160
|
-
seenKeys.add(key);
|
|
1161
|
-
}
|
|
1162
|
-
const result = [...preferredList];
|
|
1163
|
-
for (const item of secondaryList) {
|
|
1164
|
-
const key = this.getProductMergeKey(item);
|
|
1165
|
-
if (!key || seenKeys.has(key))
|
|
1166
|
-
continue;
|
|
1167
|
-
seenKeys.add(key);
|
|
1168
|
-
result.push((0, import_lodash_es.cloneDeep)(item));
|
|
1169
|
-
}
|
|
1170
|
-
return result;
|
|
1171
|
-
}
|
|
1172
|
-
mergeOrderRecords(existing, incoming) {
|
|
1173
|
-
const preferred = this.pickPreferredOrder(existing, incoming);
|
|
1174
|
-
const secondary = preferred === existing ? incoming : existing;
|
|
1175
|
-
const merged = {
|
|
1176
|
-
...secondary,
|
|
1177
|
-
...preferred
|
|
1178
|
-
};
|
|
1179
|
-
const fieldsToPreserve = [
|
|
1180
|
-
"order_id",
|
|
1181
|
-
"external_sale_number",
|
|
1182
|
-
"order_number",
|
|
1183
|
-
"shop_order_number",
|
|
1184
|
-
"shop_full_order_number"
|
|
1185
|
-
];
|
|
1186
|
-
const incomingPatchFields = [
|
|
1187
|
-
"payment_status",
|
|
1188
|
-
"status",
|
|
1189
|
-
"shipping_status",
|
|
1190
|
-
"updated_at",
|
|
1191
|
-
"shop_discount",
|
|
1192
|
-
"summary",
|
|
1193
|
-
"payments",
|
|
1194
|
-
"products",
|
|
1195
|
-
"bookings"
|
|
1196
|
-
];
|
|
1197
|
-
const mergedRecord = merged;
|
|
1198
|
-
const preferredRecord = preferred;
|
|
1199
|
-
const secondaryRecord = secondary;
|
|
1200
|
-
const incomingRecord = incoming;
|
|
1201
|
-
for (const field of fieldsToPreserve) {
|
|
1202
|
-
if (!this.isBlankIdentityValue(preferredRecord[field])) {
|
|
1203
|
-
mergedRecord[field] = preferredRecord[field];
|
|
1204
|
-
continue;
|
|
1205
|
-
}
|
|
1206
|
-
if (!this.isBlankIdentityValue(secondaryRecord[field])) {
|
|
1207
|
-
mergedRecord[field] = secondaryRecord[field];
|
|
1208
|
-
}
|
|
1209
|
-
}
|
|
1210
|
-
for (const field of incomingPatchFields) {
|
|
1211
|
-
if (!Object.prototype.hasOwnProperty.call(incomingRecord, field))
|
|
1212
|
-
continue;
|
|
1213
|
-
if (incomingRecord[field] === void 0 || incomingRecord[field] === null)
|
|
1214
|
-
continue;
|
|
1215
|
-
if (typeof incomingRecord[field] === "string" && incomingRecord[field] === "")
|
|
1216
|
-
continue;
|
|
1217
|
-
mergedRecord[field] = (0, import_lodash_es.cloneDeep)(incomingRecord[field]);
|
|
1218
|
-
}
|
|
1219
|
-
mergedRecord.products = this.mergeOrderProductLists(preferredRecord.products, secondaryRecord.products);
|
|
1220
|
-
if (!this.isPendingSyncOrder(preferred) || !this.isPendingSyncOrder(secondary)) {
|
|
1221
|
-
merged.need_sync = 0;
|
|
1222
|
-
}
|
|
1223
|
-
return merged;
|
|
1224
|
-
}
|
|
1225
|
-
summarizeDuplicateOrders(orders) {
|
|
1226
|
-
const groups = /* @__PURE__ */ new Map();
|
|
1227
|
-
for (const order of orders) {
|
|
1228
|
-
for (const key of this.getOrderIdentityMatchKeys(order)) {
|
|
1229
|
-
const group = groups.get(key) || [];
|
|
1230
|
-
group.push(order);
|
|
1231
|
-
groups.set(key, group);
|
|
1232
|
-
}
|
|
1233
|
-
}
|
|
1234
|
-
return [...groups.entries()].filter(([, group]) => group.length > 1).slice(0, 20).map(([key, group]) => ({
|
|
1235
|
-
key,
|
|
1236
|
-
count: group.length,
|
|
1237
|
-
orders: group.map((order) => ({
|
|
1238
|
-
order_id: order.order_id ?? null,
|
|
1239
|
-
external_sale_number: order.external_sale_number ?? null,
|
|
1240
|
-
shop_order_number: order.shop_order_number ?? null,
|
|
1241
|
-
shop_full_order_number: order.shop_full_order_number ?? null,
|
|
1242
|
-
need_sync: order.need_sync ?? null
|
|
1243
|
-
}))
|
|
1244
|
-
}));
|
|
1245
|
-
}
|
|
1246
|
-
logDuplicateOrders(source, orders) {
|
|
1247
|
-
const duplicateGroups = this.summarizeDuplicateOrders(orders);
|
|
1248
|
-
if (duplicateGroups.length === 0)
|
|
1249
|
-
return;
|
|
1250
|
-
this.logWarning("检测到重复订单身份键", {
|
|
1251
|
-
source,
|
|
1252
|
-
duplicateGroupCount: duplicateGroups.length,
|
|
1253
|
-
duplicateGroups
|
|
1254
|
-
});
|
|
1255
|
-
}
|
|
1256
|
-
compactOrderListByIdentity(orders, source) {
|
|
1257
|
-
const list = [];
|
|
1258
|
-
let removedCount = 0;
|
|
1259
|
-
for (const order of orders) {
|
|
1260
|
-
const matchIndex = this.findOrderIndexByIdentity(list, order);
|
|
1261
|
-
if (matchIndex < 0) {
|
|
1262
|
-
list.push(order);
|
|
1263
|
-
continue;
|
|
1264
|
-
}
|
|
1265
|
-
list[matchIndex] = this.mergeOrderRecords(list[matchIndex], order);
|
|
1266
|
-
removedCount += 1;
|
|
1267
|
-
}
|
|
1268
|
-
if (removedCount > 0) {
|
|
1269
|
-
this.logWarning("订单列表重复项已压缩", {
|
|
1270
|
-
source,
|
|
1271
|
-
beforeCount: orders.length,
|
|
1272
|
-
afterCount: list.length,
|
|
1273
|
-
removedCount
|
|
1274
|
-
});
|
|
1275
|
-
} else {
|
|
1276
|
-
this.logDuplicateOrders(source, orders);
|
|
1277
|
-
}
|
|
1278
|
-
return { list, removedCount };
|
|
1279
|
-
}
|
|
1280
685
|
/**
|
|
1281
686
|
* 当前订单拉取窗口:
|
|
1282
687
|
* - 优先使用 createdAtQuery.sales_time_between(已按营业日边界初始化)
|
|
@@ -1347,221 +752,39 @@ var OrderModule = class extends import_BaseModule.BaseModule {
|
|
|
1347
752
|
return [];
|
|
1348
753
|
}
|
|
1349
754
|
}
|
|
1350
|
-
|
|
1351
|
-
|
|
1352
|
-
}
|
|
1353
|
-
normalizeRemoteSyncedOrder(order) {
|
|
1354
|
-
if (this.isPendingSyncOrder(order))
|
|
1355
|
-
return order;
|
|
1356
|
-
return {
|
|
1357
|
-
...order,
|
|
1358
|
-
need_sync: 0
|
|
1359
|
-
};
|
|
1360
|
-
}
|
|
1361
|
-
mergeRemoteSnapshotWithPendingOrders(remoteOrders, existingOrders) {
|
|
1362
|
-
const merged = remoteOrders.map((order) => this.normalizeRemoteSyncedOrder(order));
|
|
1363
|
-
for (const order of existingOrders) {
|
|
1364
|
-
if (!this.isPendingSyncOrder(order))
|
|
1365
|
-
continue;
|
|
1366
|
-
const matchIndex = this.findOrderIndexByIdentity(merged, order);
|
|
1367
|
-
if (matchIndex >= 0) {
|
|
1368
|
-
merged[matchIndex] = this.mergeOrderRecords(order, merged[matchIndex]);
|
|
1369
|
-
continue;
|
|
1370
|
-
}
|
|
1371
|
-
if (!this.getOrderStorageKey(order))
|
|
1372
|
-
continue;
|
|
1373
|
-
merged.push(order);
|
|
1374
|
-
}
|
|
1375
|
-
return this.compactOrderListByIdentity(merged, "mergeRemoteSnapshotWithPendingOrders").list;
|
|
1376
|
-
}
|
|
1377
|
-
/**
|
|
1378
|
-
* 串行执行订单 SQLite 写入任务,避免快照替换与增量 upsert 并发交错。
|
|
1379
|
-
*
|
|
1380
|
-
* @example
|
|
1381
|
-
* await this.runInOrderSQLiteSaveQueue(async () => {
|
|
1382
|
-
* await this.dbManager.bulkUpdate(INDEXDB_STORE_NAME, orders)
|
|
1383
|
-
* })
|
|
1384
|
-
*/
|
|
1385
|
-
async runInOrderSQLiteSaveQueue(task) {
|
|
1386
|
-
const queuedTask = this.orderSQLiteSaveQueue.then(task, task);
|
|
1387
|
-
this.orderSQLiteSaveQueue = queuedTask.then(() => void 0, () => void 0);
|
|
1388
|
-
return queuedTask;
|
|
1389
|
-
}
|
|
1390
|
-
/**
|
|
1391
|
-
* 批量增量 upsert 订单到 SQLite,不清空表;用于 pubsub / HTTP 增量合并。
|
|
1392
|
-
*
|
|
1393
|
-
* @example
|
|
1394
|
-
* await this.patchOrdersInSQLite(freshOrders)
|
|
1395
|
-
*/
|
|
1396
|
-
async patchOrdersInSQLite(orders, source, mergeActions = {}) {
|
|
1397
|
-
if (!this.dbManager || orders.length === 0) {
|
|
1398
|
-
return;
|
|
1399
|
-
}
|
|
1400
|
-
const ordersSnapshot = (0, import_lodash_es.cloneDeep)(orders).filter((order) => this.getOrderStorageKey(order));
|
|
1401
|
-
if (ordersSnapshot.length === 0) {
|
|
1402
|
-
return;
|
|
1403
|
-
}
|
|
1404
|
-
const { toWrite, skipped } = this.filterRedundantPatchOrders(
|
|
1405
|
-
source,
|
|
1406
|
-
ordersSnapshot,
|
|
1407
|
-
mergeActions
|
|
1408
|
-
);
|
|
1409
|
-
if (toWrite.length === 0) {
|
|
1410
|
-
return;
|
|
1411
|
-
}
|
|
1412
|
-
const compactedToWrite = this.compactOrderListByIdentity(toWrite, `${source}.sqlitePatch`).list;
|
|
1413
|
-
if (compactedToWrite.length === 0) {
|
|
1414
|
-
return;
|
|
1415
|
-
}
|
|
1416
|
-
try {
|
|
1417
|
-
await this.runInOrderSQLiteSaveQueue(async () => {
|
|
1418
|
-
let writeMethod = "none";
|
|
1419
|
-
this.logInfo("patchOrdersInSQLite-开始", {
|
|
1420
|
-
source,
|
|
1421
|
-
count: compactedToWrite.length,
|
|
1422
|
-
dedupedCount: skipped.length
|
|
1423
|
-
});
|
|
1424
|
-
if (typeof this.dbManager.bulkUpdate === "function") {
|
|
1425
|
-
writeMethod = "bulkUpdate";
|
|
1426
|
-
await this.dbManager.bulkUpdate(INDEXDB_STORE_NAME, compactedToWrite);
|
|
1427
|
-
} else if (typeof this.dbManager.bulkAdd === "function") {
|
|
1428
|
-
writeMethod = "bulkAdd";
|
|
1429
|
-
await this.dbManager.bulkAdd(INDEXDB_STORE_NAME, compactedToWrite);
|
|
1430
|
-
} else if (typeof this.dbManager.update === "function") {
|
|
1431
|
-
writeMethod = "update";
|
|
1432
|
-
for (const order of compactedToWrite) {
|
|
1433
|
-
await this.dbManager.update(INDEXDB_STORE_NAME, order);
|
|
1434
|
-
}
|
|
1435
|
-
}
|
|
1436
|
-
this.recordRecentSqliteWrite(source, compactedToWrite);
|
|
1437
|
-
this.logInfo("patchOrdersInSQLite-完成", {
|
|
1438
|
-
source,
|
|
1439
|
-
count: compactedToWrite.length,
|
|
1440
|
-
writeMethod,
|
|
1441
|
-
dedupedCount: skipped.length
|
|
1442
|
-
});
|
|
1443
|
-
});
|
|
1444
|
-
} catch (error) {
|
|
1445
|
-
this.logError("增量保存订单到 SQLite 失败", {
|
|
1446
|
-
error: error instanceof Error ? error.message : String(error),
|
|
1447
|
-
orderCount: ordersSnapshot.length
|
|
1448
|
-
});
|
|
1449
|
-
}
|
|
1450
|
-
}
|
|
1451
|
-
/**
|
|
1452
|
-
* 单条 upsert 订单到 SQLite;用于 overwriteExistingOrder 等单条覆盖场景。
|
|
1453
|
-
*
|
|
1454
|
-
* @example
|
|
1455
|
-
* await this.updateOrderInSQLite(freshOrder)
|
|
1456
|
-
*/
|
|
1457
|
-
async updateOrderInSQLite(order, source) {
|
|
1458
|
-
if (!this.dbManager || !this.getOrderStorageKey(order)) {
|
|
755
|
+
async saveOrdersToSQLite(orderList) {
|
|
756
|
+
if (!this.dbManager)
|
|
1459
757
|
return;
|
|
1460
|
-
}
|
|
1461
|
-
const orderSnapshot = (0, import_lodash_es.cloneDeep)(order);
|
|
1462
758
|
try {
|
|
1463
|
-
|
|
1464
|
-
|
|
1465
|
-
this.logInfo("updateOrderInSQLite-开始", {
|
|
1466
|
-
source,
|
|
1467
|
-
orderId: orderSnapshot.order_id ?? null,
|
|
1468
|
-
storageKey: this.getOrderStorageKey(orderSnapshot)
|
|
1469
|
-
});
|
|
1470
|
-
if (typeof this.dbManager.update === "function") {
|
|
1471
|
-
writeMethod = "update";
|
|
1472
|
-
await this.dbManager.update(INDEXDB_STORE_NAME, orderSnapshot);
|
|
1473
|
-
} else if (typeof this.dbManager.bulkUpdate === "function") {
|
|
1474
|
-
writeMethod = "bulkUpdate";
|
|
1475
|
-
await this.dbManager.bulkUpdate(INDEXDB_STORE_NAME, [orderSnapshot]);
|
|
1476
|
-
} else if (typeof this.dbManager.bulkAdd === "function") {
|
|
1477
|
-
writeMethod = "bulkAdd";
|
|
1478
|
-
await this.dbManager.bulkAdd(INDEXDB_STORE_NAME, [orderSnapshot]);
|
|
1479
|
-
}
|
|
1480
|
-
this.recordRecentSqliteWrite(source, [orderSnapshot]);
|
|
1481
|
-
this.logInfo("updateOrderInSQLite-完成", {
|
|
1482
|
-
source,
|
|
1483
|
-
orderId: orderSnapshot.order_id ?? null,
|
|
1484
|
-
writeMethod
|
|
1485
|
-
});
|
|
759
|
+
this.logInfo("saveOrdersToSQLite-开始", {
|
|
760
|
+
count: orderList.length
|
|
1486
761
|
});
|
|
1487
|
-
|
|
1488
|
-
this.
|
|
1489
|
-
|
|
1490
|
-
orderId: orderSnapshot.order_id ?? null
|
|
762
|
+
await this.dbManager.clear(INDEXDB_STORE_NAME);
|
|
763
|
+
this.logInfo("saveOrdersToSQLite-clear完成", {
|
|
764
|
+
count: orderList.length
|
|
1491
765
|
});
|
|
1492
|
-
|
|
1493
|
-
|
|
1494
|
-
|
|
1495
|
-
|
|
1496
|
-
|
|
1497
|
-
* @example
|
|
1498
|
-
* await this.replaceOrdersSnapshotInSQLite(orderList)
|
|
1499
|
-
*/
|
|
1500
|
-
async replaceOrdersSnapshotInSQLite(orderList, source) {
|
|
1501
|
-
if (!this.dbManager) {
|
|
1502
|
-
return this.compactOrderListByIdentity(
|
|
1503
|
-
orderList.map((order) => this.normalizeRemoteSyncedOrder(order)),
|
|
1504
|
-
`${source}.snapshotNoDb`
|
|
1505
|
-
).list;
|
|
1506
|
-
}
|
|
1507
|
-
const remoteSnapshot = (0, import_lodash_es.cloneDeep)(orderList);
|
|
1508
|
-
let mergedSnapshot = this.compactOrderListByIdentity(
|
|
1509
|
-
remoteSnapshot.map((order) => this.normalizeRemoteSyncedOrder(order)),
|
|
1510
|
-
`${source}.remoteSnapshot`
|
|
1511
|
-
).list;
|
|
1512
|
-
try {
|
|
1513
|
-
await this.runInOrderSQLiteSaveQueue(async () => {
|
|
1514
|
-
const existingOrders = typeof this.dbManager.getAll === "function" ? await this.dbManager.getAll(INDEXDB_STORE_NAME) : [];
|
|
1515
|
-
const orderListSnapshot = this.mergeRemoteSnapshotWithPendingOrders(
|
|
1516
|
-
remoteSnapshot,
|
|
1517
|
-
existingOrders || []
|
|
1518
|
-
);
|
|
1519
|
-
mergedSnapshot = this.compactOrderListByIdentity(orderListSnapshot, `${source}.sqliteSnapshot`).list;
|
|
1520
|
-
this.logInfo("replaceOrdersSnapshotInSQLite-开始", {
|
|
1521
|
-
source,
|
|
1522
|
-
count: mergedSnapshot.length,
|
|
1523
|
-
remoteCount: remoteSnapshot.length,
|
|
1524
|
-
preservedPendingCount: mergedSnapshot.length - remoteSnapshot.length
|
|
1525
|
-
});
|
|
1526
|
-
await this.dbManager.clear(INDEXDB_STORE_NAME);
|
|
1527
|
-
this.logInfo("replaceOrdersSnapshotInSQLite-clear完成", {
|
|
1528
|
-
count: mergedSnapshot.length
|
|
1529
|
-
});
|
|
1530
|
-
if (mergedSnapshot.length === 0) {
|
|
1531
|
-
return;
|
|
1532
|
-
}
|
|
1533
|
-
await this.dbManager.bulkAdd(INDEXDB_STORE_NAME, mergedSnapshot);
|
|
1534
|
-
this.recordRecentSqliteWrite(source, mergedSnapshot);
|
|
1535
|
-
this.logInfo("replaceOrdersSnapshotInSQLite-bulkAdd完成", {
|
|
1536
|
-
source,
|
|
1537
|
-
count: mergedSnapshot.length
|
|
1538
|
-
});
|
|
766
|
+
if (orderList.length === 0)
|
|
767
|
+
return;
|
|
768
|
+
await this.dbManager.bulkAdd(INDEXDB_STORE_NAME, orderList);
|
|
769
|
+
this.logInfo("saveOrdersToSQLite-bulkAdd完成", {
|
|
770
|
+
count: orderList.length
|
|
1539
771
|
});
|
|
1540
772
|
} catch (error) {
|
|
1541
|
-
this.logError("
|
|
773
|
+
this.logError("保存订单到 SQLite 失败", {
|
|
1542
774
|
error: error instanceof Error ? error.message : String(error),
|
|
1543
|
-
orderList:
|
|
775
|
+
orderList: orderList.length
|
|
1544
776
|
});
|
|
1545
777
|
}
|
|
1546
|
-
return mergedSnapshot;
|
|
1547
778
|
}
|
|
1548
|
-
/**
|
|
1549
|
-
* 清空订单模块的内存数据和本地 SQLite 订单表。
|
|
1550
|
-
*
|
|
1551
|
-
* @example
|
|
1552
|
-
* await orderModule.clear()
|
|
1553
|
-
*/
|
|
1554
779
|
async clear() {
|
|
1555
780
|
this.store.list = [];
|
|
1556
781
|
this.store.map = /* @__PURE__ */ new Map();
|
|
1557
782
|
this.resourceIdIndex.clear();
|
|
1558
783
|
if (this.dbManager) {
|
|
1559
|
-
await this.
|
|
1560
|
-
await this.dbManager.clear(INDEXDB_STORE_NAME);
|
|
1561
|
-
});
|
|
784
|
+
await this.dbManager.clear(INDEXDB_STORE_NAME);
|
|
1562
785
|
}
|
|
1563
786
|
this.setStorageItem(ORDER_LAST_FULL_FETCH_AT_STORAGE_KEY, "");
|
|
1564
|
-
this.
|
|
787
|
+
this.core.effects.emit(import_types.OrderHooks.onOrdersChanged, this.store.list);
|
|
1565
788
|
}
|
|
1566
789
|
};
|
|
1567
790
|
// Annotate the CommonJS export names for ESM import in node:
|