@pisell/pisellos 2.3.36 → 2.3.38

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (90) hide show
  1. package/dist/core/index.d.ts +2 -0
  2. package/dist/core/index.js +7 -0
  3. package/dist/model/strategy/adapter/promotion/adapter.d.ts +4 -0
  4. package/dist/model/strategy/adapter/promotion/adapter.js +23 -0
  5. package/dist/model/strategy/adapter/promotion/evaluator.d.ts +17 -0
  6. package/dist/model/strategy/adapter/promotion/evaluator.js +279 -6
  7. package/dist/model/strategy/adapter/promotion/examples.d.ts +86 -0
  8. package/dist/model/strategy/adapter/promotion/examples.js +99 -0
  9. package/dist/model/strategy/adapter/promotion/index.d.ts +1 -1
  10. package/dist/model/strategy/adapter/promotion/type.d.ts +90 -2
  11. package/dist/model/strategy/adapter/promotion/type.js +45 -0
  12. package/dist/modules/BookingContext/utils/buildCacheItemFromOrderLine.js +5 -3
  13. package/dist/modules/BookingContext/utils/buildNormalProductCacheItemFromOrderLine.js +6 -3
  14. package/dist/modules/OpenData/index.d.ts +2 -0
  15. package/dist/modules/OpenData/index.js +8 -0
  16. package/dist/modules/Order/index.js +21 -14
  17. package/dist/modules/Order/utils.js +2 -1
  18. package/dist/modules/Payment/index.js +1 -1
  19. package/dist/modules/Payment/types.d.ts +73 -4
  20. package/dist/modules/Payment/types.js +18 -3
  21. package/dist/modules/Payment/walletpass.d.ts +25 -1
  22. package/dist/modules/Payment/walletpass.js +707 -157
  23. package/dist/modules/Rules/index.d.ts +1 -0
  24. package/dist/modules/Rules/index.js +75 -49
  25. package/dist/server/index.js +28 -24
  26. package/dist/server/modules/order/index.d.ts +5 -0
  27. package/dist/server/modules/order/index.js +52 -31
  28. package/dist/solution/BaseSales/index.d.ts +77 -11
  29. package/dist/solution/BaseSales/index.js +1476 -434
  30. package/dist/solution/BaseSales/types.d.ts +52 -0
  31. package/dist/solution/BaseSales/types.js +2 -1
  32. package/dist/solution/BaseSales/utils/cartPromotion.d.ts +3 -1
  33. package/dist/solution/BaseSales/utils/cartPromotion.js +90 -31
  34. package/dist/solution/BookingByStep/index.d.ts +1 -1
  35. package/dist/solution/BookingTicket/index.d.ts +7 -3
  36. package/dist/solution/BookingTicket/index.js +130 -48
  37. package/dist/solution/BookingTicket/utils/addProductDecision.js +6 -0
  38. package/dist/solution/BookingTicket/utils/scan/applyGlobalScan.d.ts +8 -0
  39. package/dist/solution/BookingTicket/utils/scan/applyGlobalScan.js +172 -82
  40. package/dist/solution/BookingTicket/utils/weighingProductSku.d.ts +5 -0
  41. package/dist/solution/BookingTicket/utils/weighingProductSku.js +45 -0
  42. package/dist/solution/ScanOrder/index.js +31 -20
  43. package/dist/solution/VenueBooking/index.js +30 -19
  44. package/dist/types/index.d.ts +1 -0
  45. package/lib/core/index.d.ts +2 -0
  46. package/lib/core/index.js +4 -0
  47. package/lib/model/strategy/adapter/promotion/adapter.d.ts +4 -0
  48. package/lib/model/strategy/adapter/promotion/adapter.js +20 -0
  49. package/lib/model/strategy/adapter/promotion/evaluator.d.ts +17 -0
  50. package/lib/model/strategy/adapter/promotion/evaluator.js +235 -0
  51. package/lib/model/strategy/adapter/promotion/examples.d.ts +86 -0
  52. package/lib/model/strategy/adapter/promotion/examples.js +91 -0
  53. package/lib/model/strategy/adapter/promotion/index.d.ts +1 -1
  54. package/lib/model/strategy/adapter/promotion/index.js +2 -0
  55. package/lib/model/strategy/adapter/promotion/type.d.ts +90 -2
  56. package/lib/model/strategy/adapter/promotion/type.js +2 -0
  57. package/lib/modules/BookingContext/utils/buildCacheItemFromOrderLine.js +2 -1
  58. package/lib/modules/BookingContext/utils/buildNormalProductCacheItemFromOrderLine.js +2 -1
  59. package/lib/modules/OpenData/index.d.ts +2 -0
  60. package/lib/modules/OpenData/index.js +5 -0
  61. package/lib/modules/Order/index.js +10 -4
  62. package/lib/modules/Order/utils.js +6 -1
  63. package/lib/modules/Payment/index.js +1 -1
  64. package/lib/modules/Payment/types.d.ts +73 -4
  65. package/lib/modules/Payment/types.js +4 -3
  66. package/lib/modules/Payment/walletpass.d.ts +25 -1
  67. package/lib/modules/Payment/walletpass.js +470 -21
  68. package/lib/modules/Rules/index.d.ts +1 -0
  69. package/lib/modules/Rules/index.js +30 -7
  70. package/lib/server/index.js +11 -8
  71. package/lib/server/modules/order/index.d.ts +5 -0
  72. package/lib/server/modules/order/index.js +24 -2
  73. package/lib/solution/BaseSales/index.d.ts +77 -11
  74. package/lib/solution/BaseSales/index.js +699 -41
  75. package/lib/solution/BaseSales/types.d.ts +52 -0
  76. package/lib/solution/BaseSales/types.js +2 -1
  77. package/lib/solution/BaseSales/utils/cartPromotion.d.ts +3 -1
  78. package/lib/solution/BaseSales/utils/cartPromotion.js +63 -12
  79. package/lib/solution/BookingByStep/index.d.ts +1 -1
  80. package/lib/solution/BookingTicket/index.d.ts +7 -3
  81. package/lib/solution/BookingTicket/index.js +60 -8
  82. package/lib/solution/BookingTicket/utils/addProductDecision.js +4 -0
  83. package/lib/solution/BookingTicket/utils/scan/applyGlobalScan.d.ts +8 -0
  84. package/lib/solution/BookingTicket/utils/scan/applyGlobalScan.js +36 -4
  85. package/lib/solution/BookingTicket/utils/weighingProductSku.d.ts +5 -0
  86. package/lib/solution/BookingTicket/utils/weighingProductSku.js +63 -0
  87. package/lib/solution/ScanOrder/index.js +8 -0
  88. package/lib/solution/VenueBooking/index.js +8 -0
  89. package/lib/types/index.d.ts +1 -0
  90. package/package.json +2 -2
@@ -24,6 +24,74 @@ __export(walletpass_exports, {
24
24
  module.exports = __toCommonJS(walletpass_exports);
25
25
  var import_types = require("./types");
26
26
  var import_utils = require("./utils");
27
+ var WALLET_PAYMENT_ASSET_TAGS = /* @__PURE__ */ new Set([
28
+ "product_voucher",
29
+ "gift_card",
30
+ "point_card"
31
+ ]);
32
+ function isWalletPaymentAsset(asset) {
33
+ return WALLET_PAYMENT_ASSET_TAGS.has(
34
+ String((asset == null ? void 0 : asset.tag) || "").toLowerCase()
35
+ );
36
+ }
37
+ function getWalletAssetId(asset) {
38
+ return (asset == null ? void 0 : asset.id) ?? (asset == null ? void 0 : asset.voucher_id) ?? (asset == null ? void 0 : asset.code);
39
+ }
40
+ function getWalletAssetKey(assetOrId) {
41
+ const id = assetOrId && typeof assetOrId === "object" ? getWalletAssetId(assetOrId) : assetOrId;
42
+ return id === void 0 || id === null ? "" : String(id);
43
+ }
44
+ function isWalletAssetAvailable(asset) {
45
+ if (!asset)
46
+ return false;
47
+ if (asset.unified_available_status !== void 0 && Number(asset.unified_available_status) !== 1) {
48
+ return false;
49
+ }
50
+ if (asset._unified_available_status !== void 0 && Number(asset._unified_available_status) !== 1) {
51
+ return false;
52
+ }
53
+ if (asset.available === false || asset.isDisabled === true)
54
+ return false;
55
+ return true;
56
+ }
57
+ function getWalletAssetDeductionAmount(asset) {
58
+ const value = (asset == null ? void 0 : asset.actualDeduction) ?? (asset == null ? void 0 : asset._available_max_amount) ?? (asset == null ? void 0 : asset.edit_current_amount) ?? (asset == null ? void 0 : asset.recommended_usage_amount) ?? (asset == null ? void 0 : asset.available_max_amount) ?? (asset == null ? void 0 : asset.amount) ?? 0;
59
+ const amount = Number(value);
60
+ return Number.isFinite(amount) && amount > 0 ? amount : 0;
61
+ }
62
+ function mergeWalletAssets(...groups) {
63
+ const merged = [];
64
+ const indexByKey = /* @__PURE__ */ new Map();
65
+ groups.flat().forEach((asset) => {
66
+ const key = getWalletAssetKey(asset);
67
+ if (!key)
68
+ return;
69
+ const existedIndex = indexByKey.get(key);
70
+ if (existedIndex === void 0) {
71
+ indexByKey.set(key, merged.length);
72
+ merged.push(asset);
73
+ return;
74
+ }
75
+ merged[existedIndex] = {
76
+ ...merged[existedIndex],
77
+ ...asset
78
+ };
79
+ });
80
+ return merged;
81
+ }
82
+ function createInitialWalletAssetsState() {
83
+ return {
84
+ status: "idle",
85
+ items: [],
86
+ selectedIds: [],
87
+ selectedItems: [],
88
+ committedIds: [],
89
+ selectionSources: {},
90
+ totalSelectedAmount: 0,
91
+ error: null,
92
+ revision: 0
93
+ };
94
+ }
27
95
  var WalletPassPaymentImpl = class {
28
96
  constructor(paymentModule) {
29
97
  this.paymentModule = paymentModule;
@@ -32,6 +100,12 @@ var WalletPassPaymentImpl = class {
32
100
  this.searchResults = [];
33
101
  this.walletParams = null;
34
102
  this.walletInitData = null;
103
+ this.walletAssetsState = createInitialWalletAssetsState();
104
+ this.walletAssetsRequestSequence = 0;
105
+ this.walletAssetsCalculationContext = {
106
+ orderTotalAmount: 0,
107
+ products: []
108
+ };
35
109
  }
36
110
  /**
37
111
  * 发送事件的辅助方法
@@ -48,6 +122,145 @@ var WalletPassPaymentImpl = class {
48
122
  });
49
123
  }
50
124
  }
125
+ publishWalletAssetsState(patch) {
126
+ this.walletAssetsState = {
127
+ ...this.walletAssetsState,
128
+ ...patch,
129
+ items: [...patch.items ?? this.walletAssetsState.items],
130
+ selectedIds: [
131
+ ...patch.selectedIds ?? this.walletAssetsState.selectedIds
132
+ ],
133
+ selectedItems: [
134
+ ...patch.selectedItems ?? this.walletAssetsState.selectedItems
135
+ ],
136
+ committedIds: [
137
+ ...patch.committedIds ?? this.walletAssetsState.committedIds
138
+ ],
139
+ selectionSources: {
140
+ ...patch.selectionSources ?? this.walletAssetsState.selectionSources
141
+ },
142
+ revision: this.walletAssetsState.revision + 1
143
+ };
144
+ const state = this.getWalletAssetsState();
145
+ this.emitEvent(import_types.WalletPassHooks.OnWalletAssetsStateChanged, { state });
146
+ return state;
147
+ }
148
+ recalculateWalletAssets(items, requestedIds, selectionSources) {
149
+ var _a, _b;
150
+ const committedKeys = new Set(
151
+ this.walletAssetsState.committedIds.map((id) => String(id))
152
+ );
153
+ const requestedKeys = new Set(
154
+ requestedIds.map((id) => String(id)).filter((id) => !committedKeys.has(id))
155
+ );
156
+ const requested = items.filter(
157
+ (item) => requestedKeys.has(getWalletAssetKey(item)) && isWalletAssetAvailable(item)
158
+ );
159
+ let nextItems = items;
160
+ let selectedItems = requested;
161
+ const evaluator = (_b = (_a = this.paymentModule.window) == null ? void 0 : _a.getWalletPassEvaluator) == null ? void 0 : _b.call(_a);
162
+ if (evaluator == null ? void 0 : evaluator.recalculateVouchers) {
163
+ try {
164
+ const result = evaluator.recalculateVouchers(
165
+ items,
166
+ requested,
167
+ this.walletAssetsCalculationContext.orderTotalAmount,
168
+ this.walletAssetsCalculationContext.products
169
+ );
170
+ nextItems = mergeWalletAssets(items, (result == null ? void 0 : result.allWithUpdatedStatus) || []);
171
+ selectedItems = ((result == null ? void 0 : result.selectedWithDetails) || []).filter(
172
+ (item) => isWalletAssetAvailable(item) && getWalletAssetDeductionAmount(item) > 0
173
+ );
174
+ } catch (error) {
175
+ this.paymentModule.logWarning(
176
+ "[WalletPass] 共享钱包资产重新计算失败,回退接口可用状态",
177
+ {
178
+ error: error instanceof Error ? error.message : String(error)
179
+ }
180
+ );
181
+ }
182
+ }
183
+ const selectedIds = selectedItems.map((item) => getWalletAssetId(item)).filter((id) => id !== void 0 && id !== null);
184
+ const selectedKeys = new Set(selectedIds.map((id) => String(id)));
185
+ const nextSources = Object.entries(selectionSources).reduce((result, [key, source]) => {
186
+ if (selectedKeys.has(key))
187
+ result[key] = source;
188
+ return result;
189
+ }, {});
190
+ const totalSelectedAmount = selectedItems.reduce(
191
+ (total, item) => total + getWalletAssetDeductionAmount(item),
192
+ 0
193
+ );
194
+ return this.publishWalletAssetsState({
195
+ status: "ready",
196
+ items: nextItems,
197
+ selectedIds,
198
+ selectedItems,
199
+ selectionSources: nextSources,
200
+ totalSelectedAmount,
201
+ error: null
202
+ });
203
+ }
204
+ /**
205
+ * 使用最新订单参数重验扫码搜索过的支付类 Wallet 资产。
206
+ * 请求失败或暂时无匹配结果时保留旧资产,避免商品变化造成列表闪空。
207
+ */
208
+ async revalidateSearchedWalletAssets(requestSequence) {
209
+ const cachedAssets = this.searchResults.filter(isWalletPaymentAsset);
210
+ const searchCodes = [
211
+ ...new Set(
212
+ cachedAssets.map((item) => String((item == null ? void 0 : item.code) || "").trim()).filter(Boolean)
213
+ )
214
+ ];
215
+ if (searchCodes.length === 0)
216
+ return cachedAssets;
217
+ const preparePayments = this.formatWalletPassList2PreparePayments(
218
+ this.walletAssetsState.selectedItems
219
+ );
220
+ const refreshedGroups = await Promise.all(
221
+ searchCodes.map(async (searchCode) => {
222
+ try {
223
+ const result = await this.searchIdentificationCodeAsync(
224
+ {
225
+ ...this.walletParams || {},
226
+ code: searchCode,
227
+ prepare_payments: preparePayments
228
+ },
229
+ { noCache: true }
230
+ );
231
+ return {
232
+ searchCode,
233
+ items: result.type === "normalCode" ? result.data.filter(isWalletPaymentAsset) : []
234
+ };
235
+ } catch (error) {
236
+ this.paymentModule.logWarning(
237
+ "[WalletPass] 重验扫码 Wallet 资产失败,保留旧结果",
238
+ {
239
+ code: searchCode,
240
+ error: error instanceof Error ? error.message : String(error)
241
+ }
242
+ );
243
+ return { searchCode, items: [] };
244
+ }
245
+ })
246
+ );
247
+ if (requestSequence !== this.walletAssetsRequestSequence) {
248
+ return cachedAssets;
249
+ }
250
+ const replacedCodes = new Set(
251
+ refreshedGroups.filter((group) => group.items.length > 0).map((group) => group.searchCode.toUpperCase())
252
+ );
253
+ const refreshedAssets = refreshedGroups.flatMap((group) => group.items);
254
+ if (refreshedAssets.length > 0) {
255
+ this.searchResults = [
256
+ ...this.searchResults.filter(
257
+ (item) => !replacedCodes.has(String((item == null ? void 0 : item.code) || "").trim().toUpperCase())
258
+ ),
259
+ ...refreshedAssets
260
+ ];
261
+ }
262
+ return mergeWalletAssets(cachedAssets, refreshedAssets);
263
+ }
51
264
  /**
52
265
  * 生成钱包API默认参数
53
266
  * 根据业务数据生成标准的钱包API参数,并存储在模块中
@@ -75,7 +288,7 @@ var WalletPassPaymentImpl = class {
75
288
  // 订单小计金额
76
289
  order_product_amount: subTotal,
77
290
  // 订单待支付金额
78
- order_wait_pay_amount: order_wait_pay_amount || ((amountInfo == null ? void 0 : amountInfo.isDeposit) ? Number(amountInfo == null ? void 0 : amountInfo.depositAmount) : totalAmount),
291
+ order_wait_pay_amount: order_wait_pay_amount ?? ((amountInfo == null ? void 0 : amountInfo.isDeposit) ? Number(amountInfo == null ? void 0 : amountInfo.depositAmount) : totalAmount),
79
292
  // order_behavior_count_customer_id: 1,
80
293
  products,
81
294
  prepare_payments: [],
@@ -356,7 +569,6 @@ var WalletPassPaymentImpl = class {
356
569
  * 特殊逻辑:当识别码长度为9位且前3位为"000"时,调用 /wallet/detail/search 接口
357
570
  */
358
571
  async searchIdentificationCodeAsync(params, config = {}) {
359
- var _a;
360
572
  try {
361
573
  const { code } = params;
362
574
  this.paymentModule.logInfo("[WalletPass] 开始搜索识别码", {
@@ -394,14 +606,14 @@ var WalletPassPaymentImpl = class {
394
606
  const baseWalletParams = this.walletParams;
395
607
  const searchParams = {
396
608
  // 基础钱包参数
397
- sale_channel: params.sale_channel || (baseWalletParams == null ? void 0 : baseWalletParams.sale_channel) || "pos",
398
- customer_id: params.customer_id || (baseWalletParams == null ? void 0 : baseWalletParams.customer_id),
399
- order_expect_amount: params.order_expect_amount || (baseWalletParams == null ? void 0 : baseWalletParams.order_expect_amount),
400
- order_product_amount: params.order_product_amount || (baseWalletParams == null ? void 0 : baseWalletParams.order_product_amount),
401
- order_wait_pay_amount: params.order_wait_pay_amount || (baseWalletParams == null ? void 0 : baseWalletParams.order_wait_pay_amount),
402
- order_behavior_count_customer_id: params.order_behavior_count_customer_id || (baseWalletParams == null ? void 0 : baseWalletParams.order_behavior_count_customer_id) || params.customer_id || (baseWalletParams == null ? void 0 : baseWalletParams.customer_id),
403
- products: params.products || (baseWalletParams == null ? void 0 : baseWalletParams.products),
404
- prepare_payments: params.prepare_payments || (baseWalletParams == null ? void 0 : baseWalletParams.prepare_payments),
609
+ sale_channel: params.sale_channel ?? (baseWalletParams == null ? void 0 : baseWalletParams.sale_channel) ?? "pos",
610
+ customer_id: params.customer_id ?? (baseWalletParams == null ? void 0 : baseWalletParams.customer_id),
611
+ order_expect_amount: params.order_expect_amount ?? (baseWalletParams == null ? void 0 : baseWalletParams.order_expect_amount),
612
+ order_product_amount: params.order_product_amount ?? (baseWalletParams == null ? void 0 : baseWalletParams.order_product_amount),
613
+ order_wait_pay_amount: params.order_wait_pay_amount ?? (baseWalletParams == null ? void 0 : baseWalletParams.order_wait_pay_amount),
614
+ order_behavior_count_customer_id: params.order_behavior_count_customer_id ?? (baseWalletParams == null ? void 0 : baseWalletParams.order_behavior_count_customer_id) ?? params.customer_id ?? (baseWalletParams == null ? void 0 : baseWalletParams.customer_id),
615
+ products: params.products ?? (baseWalletParams == null ? void 0 : baseWalletParams.products) ?? [],
616
+ prepare_payments: params.prepare_payments ?? (baseWalletParams == null ? void 0 : baseWalletParams.prepare_payments) ?? [],
405
617
  multiple: 1,
406
618
  // 搜索特有参数
407
619
  code: params.code,
@@ -417,17 +629,10 @@ var WalletPassPaymentImpl = class {
417
629
  if (typeof searchParams.payment_order_id === "string" && searchParams.payment_order_id.startsWith("LOCAL_")) {
418
630
  searchParams.payment_order_id = void 0;
419
631
  }
420
- let response;
421
- if (!searchParams.products || ((_a = searchParams.products || []) == null ? void 0 : _a.length) === 0) {
422
- response = {
423
- data: []
424
- };
425
- } else {
426
- response = await this.paymentModule.request.post(
427
- "/machinecode/prepare/deduction/search",
428
- searchParams
429
- );
430
- }
632
+ const response = await this.paymentModule.request.post(
633
+ "/machinecode/prepare/deduction/search",
634
+ searchParams
635
+ );
431
636
  const searchResults = (response == null ? void 0 : response.data) || [];
432
637
  if (config.noCache) {
433
638
  return {
@@ -469,6 +674,243 @@ var WalletPassPaymentImpl = class {
469
674
  throw error;
470
675
  }
471
676
  }
677
+ /**
678
+ * 刷新购物车与结账共享的钱包资产。
679
+ *
680
+ * 与 legacy initializeWalletDataFromBusinessAsync 不同,这里维护选择状态;
681
+ * 推荐结果只作为可用性/金额计算输入,不会自动成为 selectedIds。
682
+ */
683
+ async refreshWalletAssetsFromBusinessAsync(businessData, options = {}) {
684
+ const requestSequence = ++this.walletAssetsRequestSequence;
685
+ const nextCustomerId = businessData.customer_id;
686
+ const previousCustomerId = this.walletAssetsState.customerId;
687
+ const customerChanged = String(previousCustomerId ?? "") !== String(nextCustomerId ?? "");
688
+ const preserveSelection = options.preserveSelection !== false && !customerChanged;
689
+ if (customerChanged) {
690
+ this.clearSearchResults();
691
+ }
692
+ const committedKeys = new Set(
693
+ this.walletAssetsState.committedIds.map((id) => String(id))
694
+ );
695
+ const committedItems = this.walletAssetsState.items.filter(
696
+ (item) => committedKeys.has(getWalletAssetKey(item))
697
+ );
698
+ const selectedIds = preserveSelection ? [...this.walletAssetsState.selectedIds] : [];
699
+ const selectionSources = preserveSelection ? { ...this.walletAssetsState.selectionSources } : {};
700
+ this.publishWalletAssetsState({
701
+ status: "loading",
702
+ customerId: nextCustomerId,
703
+ items: preserveSelection ? this.walletAssetsState.items : committedItems,
704
+ selectedIds,
705
+ selectedItems: preserveSelection ? this.walletAssetsState.selectedItems : [],
706
+ selectionSources,
707
+ totalSelectedAmount: preserveSelection ? this.walletAssetsState.totalSelectedAmount : 0,
708
+ error: null
709
+ });
710
+ try {
711
+ const result = await this.initializeWalletDataFromBusinessAsync(
712
+ businessData
713
+ );
714
+ if (requestSequence !== this.walletAssetsRequestSequence) {
715
+ return this.getWalletAssetsState();
716
+ }
717
+ this.walletAssetsCalculationContext = {
718
+ orderTotalAmount: Number(businessData.order_wait_pay_amount || 0),
719
+ products: result.products || businessData.products || []
720
+ };
721
+ const searchedWalletAssets = preserveSelection ? await this.revalidateSearchedWalletAssets(requestSequence) : [];
722
+ if (requestSequence !== this.walletAssetsRequestSequence) {
723
+ return this.getWalletAssetsState();
724
+ }
725
+ const recommendedKeys = new Set(
726
+ (result.walletRecommendList || []).map(
727
+ (asset) => getWalletAssetKey(asset)
728
+ )
729
+ );
730
+ const items = mergeWalletAssets(
731
+ preserveSelection ? this.walletAssetsState.selectedItems : [],
732
+ searchedWalletAssets,
733
+ result.transformList || [],
734
+ result.noApplicableVoucher || []
735
+ ).map((asset) => ({
736
+ ...asset,
737
+ _wallet_asset_recommended: recommendedKeys.has(
738
+ getWalletAssetKey(asset)
739
+ )
740
+ }));
741
+ const latestSelectedIds = preserveSelection ? [...this.walletAssetsState.selectedIds] : [];
742
+ const latestSelectionSources = preserveSelection ? { ...this.walletAssetsState.selectionSources } : {};
743
+ return this.recalculateWalletAssets(
744
+ items,
745
+ latestSelectedIds,
746
+ latestSelectionSources
747
+ );
748
+ } catch (error) {
749
+ if (requestSequence !== this.walletAssetsRequestSequence) {
750
+ return this.getWalletAssetsState();
751
+ }
752
+ return this.publishWalletAssetsState({
753
+ status: "error",
754
+ items: preserveSelection ? this.walletAssetsState.items : committedItems,
755
+ selectedIds: preserveSelection ? this.walletAssetsState.selectedIds : [],
756
+ selectedItems: preserveSelection ? this.walletAssetsState.selectedItems : [],
757
+ selectionSources: preserveSelection ? this.walletAssetsState.selectionSources : {},
758
+ totalSelectedAmount: preserveSelection ? this.walletAssetsState.totalSelectedAmount : 0,
759
+ customerId: nextCustomerId,
760
+ error: error instanceof Error ? error.message : String(error)
761
+ });
762
+ }
763
+ }
764
+ getWalletAssetsState() {
765
+ return {
766
+ ...this.walletAssetsState,
767
+ items: [...this.walletAssetsState.items],
768
+ selectedIds: [...this.walletAssetsState.selectedIds],
769
+ selectedItems: [...this.walletAssetsState.selectedItems],
770
+ committedIds: [...this.walletAssetsState.committedIds],
771
+ selectionSources: { ...this.walletAssetsState.selectionSources }
772
+ };
773
+ }
774
+ exportWalletAssetsSnapshot() {
775
+ return {
776
+ state: this.getWalletAssetsState(),
777
+ searchResults: [...this.searchResults],
778
+ walletParams: this.walletParams ? { ...this.walletParams } : null,
779
+ calculationContext: {
780
+ orderTotalAmount: this.walletAssetsCalculationContext.orderTotalAmount,
781
+ products: [...this.walletAssetsCalculationContext.products]
782
+ }
783
+ };
784
+ }
785
+ importWalletAssetsSnapshot(snapshot) {
786
+ var _a, _b;
787
+ if (!(snapshot == null ? void 0 : snapshot.state))
788
+ return this.getWalletAssetsState();
789
+ this.walletAssetsRequestSequence += 1;
790
+ this.searchResults = Array.isArray(snapshot.searchResults) ? [...snapshot.searchResults] : [];
791
+ this.walletParams = snapshot.walletParams ? { ...snapshot.walletParams } : null;
792
+ this.walletAssetsCalculationContext = {
793
+ orderTotalAmount: Number(
794
+ ((_a = snapshot.calculationContext) == null ? void 0 : _a.orderTotalAmount) || 0
795
+ ),
796
+ products: Array.isArray((_b = snapshot.calculationContext) == null ? void 0 : _b.products) ? [...snapshot.calculationContext.products] : []
797
+ };
798
+ return this.publishWalletAssetsState({
799
+ ...snapshot.state,
800
+ items: Array.isArray(snapshot.state.items) ? snapshot.state.items : [],
801
+ selectedIds: Array.isArray(snapshot.state.selectedIds) ? snapshot.state.selectedIds : [],
802
+ selectedItems: Array.isArray(snapshot.state.selectedItems) ? snapshot.state.selectedItems : [],
803
+ committedIds: Array.isArray(snapshot.state.committedIds) ? snapshot.state.committedIds : [],
804
+ selectionSources: snapshot.state.selectionSources || {}
805
+ });
806
+ }
807
+ selectWalletAsset(assetId, options) {
808
+ const assetKey = String(assetId);
809
+ const committedKeys = new Set(
810
+ this.walletAssetsState.committedIds.map((id) => String(id))
811
+ );
812
+ if (committedKeys.has(assetKey))
813
+ return this.getWalletAssetsState();
814
+ const selectedIds = this.walletAssetsState.selectedIds.filter(
815
+ (id) => String(id) !== assetKey
816
+ );
817
+ const selectionSources = { ...this.walletAssetsState.selectionSources };
818
+ delete selectionSources[assetKey];
819
+ if (options.selected) {
820
+ const asset = this.walletAssetsState.items.find(
821
+ (item) => getWalletAssetKey(item) === assetKey
822
+ );
823
+ if (!asset || !isWalletAssetAvailable(asset)) {
824
+ return this.getWalletAssetsState();
825
+ }
826
+ selectedIds.push(assetId);
827
+ selectionSources[assetKey] = options.source || "manual";
828
+ }
829
+ return this.recalculateWalletAssets(
830
+ this.walletAssetsState.items,
831
+ selectedIds,
832
+ selectionSources
833
+ );
834
+ }
835
+ async scanWalletAssetAsync(code) {
836
+ const normalizedCode = String(code || "").trim();
837
+ if (!normalizedCode) {
838
+ return {
839
+ type: "normalCode",
840
+ selected: false,
841
+ state: this.getWalletAssetsState()
842
+ };
843
+ }
844
+ this.walletAssetsRequestSequence += 1;
845
+ const result = await this.searchIdentificationCodeAsync({
846
+ ...this.walletParams || {},
847
+ code: normalizedCode,
848
+ prepare_payments: this.formatWalletPassList2PreparePayments(
849
+ this.walletAssetsState.selectedItems
850
+ )
851
+ });
852
+ const paymentAssets = result.data.filter(isWalletPaymentAsset);
853
+ const asset = paymentAssets.find(
854
+ (item) => String((item == null ? void 0 : item.code) || "") === normalizedCode
855
+ ) || paymentAssets[0];
856
+ if (!asset || result.type === "walletCode") {
857
+ return {
858
+ type: result.type,
859
+ asset,
860
+ selected: false,
861
+ state: this.getWalletAssetsState()
862
+ };
863
+ }
864
+ const items = mergeWalletAssets(this.walletAssetsState.items, [asset]);
865
+ this.recalculateWalletAssets(
866
+ items,
867
+ this.walletAssetsState.selectedIds,
868
+ this.walletAssetsState.selectionSources
869
+ );
870
+ const assetId = getWalletAssetId(asset);
871
+ const selected = assetId !== void 0 && isWalletAssetAvailable(asset) ? this.selectWalletAsset(assetId, {
872
+ selected: true,
873
+ source: "scan"
874
+ }) : this.getWalletAssetsState();
875
+ return {
876
+ type: result.type,
877
+ asset,
878
+ selected: assetId !== void 0 && selected.selectedIds.some((id) => String(id) === String(assetId)),
879
+ state: selected
880
+ };
881
+ }
882
+ clearWalletAssetSelection() {
883
+ return this.recalculateWalletAssets(
884
+ this.walletAssetsState.items,
885
+ [],
886
+ {}
887
+ );
888
+ }
889
+ setCommittedWalletAssets(committedIds, committedAssets = []) {
890
+ const committedKeys = new Set(committedIds.map((id) => String(id)));
891
+ const selectedIds = this.walletAssetsState.selectedIds.filter(
892
+ (id) => !committedKeys.has(String(id))
893
+ );
894
+ const selectionSources = Object.entries(
895
+ this.walletAssetsState.selectionSources
896
+ ).reduce(
897
+ (result, [key, source]) => {
898
+ if (!committedKeys.has(key))
899
+ result[key] = source;
900
+ return result;
901
+ },
902
+ {}
903
+ );
904
+ this.publishWalletAssetsState({
905
+ items: mergeWalletAssets(this.walletAssetsState.items, committedAssets),
906
+ committedIds
907
+ });
908
+ return this.recalculateWalletAssets(
909
+ this.walletAssetsState.items,
910
+ selectedIds,
911
+ selectionSources
912
+ );
913
+ }
472
914
  async processWalletPayment(amount, orderUuid, voucherId) {
473
915
  this.paymentModule.logInfo("[WalletPass] 开始处理钱包支付", {
474
916
  amount,
@@ -576,6 +1018,13 @@ var WalletPassPaymentImpl = class {
576
1018
  this.searchResults = [];
577
1019
  this.walletParams = null;
578
1020
  this.walletInitData = null;
1021
+ this.walletAssetsRequestSequence += 1;
1022
+ this.walletAssetsCalculationContext = {
1023
+ orderTotalAmount: 0,
1024
+ products: []
1025
+ };
1026
+ this.walletAssetsState = createInitialWalletAssetsState();
1027
+ this.publishWalletAssetsState({});
579
1028
  this.emitEvent(import_types.WalletPassHooks.OnWalletCacheCleared, {
580
1029
  clearedTypes: ["all"]
581
1030
  });
@@ -35,6 +35,7 @@ export declare class RulesModule extends BaseModule implements Module, RulesModu
35
35
  excludeDiscountListByType(discountList: Discount[], type: string): Discount[];
36
36
  /** 手动改价场景:去掉 promotion,保留 type=product 等行内折扣 */
37
37
  resolveDiscountListForManualOverride(discountList: Discount[]): Discount[];
38
+ private isItemRewardProductDiscount;
38
39
  private getUnavailableReason;
39
40
  calcDiscount({ discountList, productList, holders, isFormSubject, orderTotalAmount }: {
40
41
  discountList: Discount[];
@@ -167,6 +167,16 @@ var RulesModule = class extends import_BaseModule.BaseModule {
167
167
  resolveDiscountListForManualOverride(discountList) {
168
168
  return this.excludeDiscountListByType(discountList, "promotion");
169
169
  }
170
+ isItemRewardProductDiscount(product) {
171
+ var _a;
172
+ const discountList = Array.isArray(product == null ? void 0 : product.discount_list) ? product.discount_list : [];
173
+ return ((_a = product == null ? void 0 : product._promotion) == null ? void 0 : _a.actionType) === "ITEM_REWARD" && discountList.some(
174
+ (item) => {
175
+ var _a2, _b;
176
+ return (item == null ? void 0 : item.type) === "product" && ((_a2 = item == null ? void 0 : item.metadata) == null ? void 0 : _a2.source) === "promotion" && ((_b = item == null ? void 0 : item.metadata) == null ? void 0 : _b.actionType) === "ITEM_REWARD";
177
+ }
178
+ );
179
+ }
170
180
  // 获取券不可用的原因
171
181
  getUnavailableReason(discountList, productList) {
172
182
  var _a;
@@ -302,8 +312,7 @@ var RulesModule = class extends import_BaseModule.BaseModule {
302
312
  bundleIndex,
303
313
  // 虚拟商品属性
304
314
  price: Number(bundleItem.price || 0),
305
- id: bundleItem._bundle_product_id,
306
- // 🔥 使用 _bundle_product_id
315
+ id: bundleItem.bundle_product_id ?? bundleItem._bundle_product_id ?? bundleItem.product_id,
307
316
  _id: `${product._id}_bundle_${bundleIndex}`,
308
317
  parentId: product._id,
309
318
  num: bundleItem.num || 1,
@@ -500,6 +509,9 @@ var RulesModule = class extends import_BaseModule.BaseModule {
500
509
  startDate: (_a2 = flatItem.parentProduct) == null ? void 0 : _a2.startDate
501
510
  };
502
511
  }
512
+ if (this.isItemRewardProductDiscount(product)) {
513
+ return false;
514
+ }
503
515
  const isAvailableProduct = flatItem.type === "main" ? !((product == null ? void 0 : product.booking_id) && ((_b = product == null ? void 0 : product.discount_list) == null ? void 0 : _b.length) && ((_c = product == null ? void 0 : product.discount_list) == null ? void 0 : _c.every((d) => d.id && ["good_pass", "discount_card", "product_discount_card"].includes(d.tag || d.type)))) : !((flatItem == null ? void 0 : flatItem.booking_id) && !!((_e = (_d = flatItem == null ? void 0 : flatItem.bundleItem) == null ? void 0 : _d.discount_list) == null ? void 0 : _e.length) && ((_g = (_f = flatItem == null ? void 0 : flatItem.bundleItem) == null ? void 0 : _f.discount_list) == null ? void 0 : _g.every((d) => d.id)));
504
516
  if (!isAvailableProduct) {
505
517
  return false;
@@ -625,6 +637,7 @@ var RulesModule = class extends import_BaseModule.BaseModule {
625
637
  };
626
638
  originProduct = flatItem.originProduct;
627
639
  }
640
+ const isItemRewardProductDiscount = this.isItemRewardProductDiscount(product);
628
641
  addModeDiscount.forEach((discount) => {
629
642
  var _a3, _b, _c, _d, _e, _f, _g, _h, _i, _j;
630
643
  const limitedData = discount == null ? void 0 : discount.limited_relation_product_data;
@@ -648,7 +661,7 @@ var RulesModule = class extends import_BaseModule.BaseModule {
648
661
  ].includes(discount2.tag || discount2.type)
649
662
  ))) : !((flatItem == null ? void 0 : flatItem.booking_id) && !!((_d = (_c = flatItem == null ? void 0 : flatItem.bundleItem) == null ? void 0 : _c.discount_list) == null ? void 0 : _d.length) && ((_f = (_e = flatItem == null ? void 0 : flatItem.bundleItem) == null ? void 0 : _e.discount_list) == null ? void 0 : _f.every((discount2) => discount2.id)));
650
663
  const isBundleAvailable = this.checkPackageSubItemUsageRules(discount, flatItem);
651
- if (isAvailableProduct && isLimitedProduct && timeLimit && isBundleAvailable && ((_g = discount.config) == null ? void 0 : _g.isAvailable)) {
664
+ if (isAvailableProduct && !isItemRewardProductDiscount && isLimitedProduct && timeLimit && isBundleAvailable && ((_g = discount.config) == null ? void 0 : _g.isAvailable)) {
652
665
  (_h = discountApplicability.get(discount.id)) == null ? void 0 : _h.push(product.id);
653
666
  const applicableProducts = discountApplicableProducts.get(discount.id) || [];
654
667
  const discountType = discount.tag || discount.type;
@@ -771,6 +784,8 @@ var RulesModule = class extends import_BaseModule.BaseModule {
771
784
  }
772
785
  const applicableDiscounts = sortedDiscountList.filter((discount) => {
773
786
  var _a3, _b2, _c2, _d2;
787
+ if (this.isItemRewardProductDiscount(product))
788
+ return false;
774
789
  const discountType = discount.tag || discount.type;
775
790
  const currentOriginUid = getOriginProductUid(originProduct);
776
791
  const isReapplyEditModeDiscountForProduct = discount.isEditMode && reapplyBookingDiscountProductUids.size > 0 && currentOriginUid && reapplyBookingDiscountProductUids.has(String(currentOriginUid)) && discount.isSelected === true;
@@ -840,11 +855,17 @@ var RulesModule = class extends import_BaseModule.BaseModule {
840
855
  }
841
856
  return false;
842
857
  });
843
- const selectedDiscountCard = applicableDiscounts.find(
858
+ const explicitlySelectedDiscountCard = (options == null ? void 0 : options.isSelected) === true && options.discountId !== void 0 ? applicableDiscounts.find((discount) => {
859
+ const discountType = discount.tag || discount.type;
860
+ return String(discount.id) === String(options.discountId) && ["discount_card", "product_discount_card"].includes(discountType);
861
+ }) : void 0;
862
+ const scannedSelectedDiscountCard = applicableDiscounts.find(
844
863
  (n) => n.isScan && n.isSelected && (n.tag || n.type) !== "good_pass"
845
864
  );
865
+ const selectedDiscountCard = explicitlySelectedDiscountCard || scannedSelectedDiscountCard;
846
866
  const selectedDiscount = selectedDiscountCard || applicableDiscounts[0];
847
867
  let isManualDiscount = false;
868
+ let isItemRewardProductDiscount = false;
848
869
  if (flatItem.type === "main") {
849
870
  isManualDiscount = typeof product.isManualDiscount === "boolean" ? product.isManualDiscount : ((_g = product.discount_list) == null ? void 0 : _g.some((item) => item.type === "product")) || product.total != product.origin_total && (product.bundle || []).every(
850
871
  (item) => {
@@ -855,7 +876,8 @@ var RulesModule = class extends import_BaseModule.BaseModule {
855
876
  _i,
856
877
  (item) => item.type === "product"
857
878
  )));
858
- if (product.inPromotion) {
879
+ isItemRewardProductDiscount = this.isItemRewardProductDiscount(product);
880
+ if (product.inPromotion && !isItemRewardProductDiscount) {
859
881
  isManualDiscount = false;
860
882
  }
861
883
  } else {
@@ -969,7 +991,7 @@ var RulesModule = class extends import_BaseModule.BaseModule {
969
991
  } else {
970
992
  let total = product.inPromotion ? ((_w = product == null ? void 0 : product._promotion) == null ? void 0 : _w.finalPrice) ?? product.origin_total ?? product.total : product.origin_total ?? product.total;
971
993
  let main_product_selling_price = product.price;
972
- if ((product.discount_list || []).some((item) => item.type === "promotion") || (0, import_lodash_es.isBoolean)(product.vouchersApplicable) && !product.vouchersApplicable) {
994
+ if ((product.discount_list || []).some((item) => item.type === "promotion") || isItemRewardProductDiscount || (0, import_lodash_es.isBoolean)(product.vouchersApplicable) && !product.vouchersApplicable) {
973
995
  total = product.total ?? product.origin_total;
974
996
  main_product_selling_price = product.main_product_selling_price ?? main_product_selling_price;
975
997
  }
@@ -977,7 +999,8 @@ var RulesModule = class extends import_BaseModule.BaseModule {
977
999
  this.hooks.setProduct(originProduct, {
978
1000
  ...isManualDiscount ? {
979
1001
  price: product.price,
980
- main_product_selling_price: product.price
1002
+ main_product_selling_price: isItemRewardProductDiscount ? main_product_selling_price : product.price,
1003
+ ...isItemRewardProductDiscount ? { total } : {}
981
1004
  } : {
982
1005
  _id: product._id.split("___")[0] + "___" + index,
983
1006
  total,