@pisell/pisellos 2.1.58 → 2.1.60

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 (100) hide show
  1. package/dist/index.d.ts +1 -0
  2. package/dist/index.js +1 -0
  3. package/dist/model/index.d.ts +1 -0
  4. package/dist/model/index.js +1 -0
  5. package/dist/model/strategy/adapter/index.d.ts +3 -0
  6. package/dist/model/strategy/adapter/index.js +4 -0
  7. package/dist/model/strategy/adapter/type.d.ts +28 -0
  8. package/dist/model/strategy/adapter/type.js +1 -0
  9. package/dist/model/strategy/adapter/walletPass/evaluator.d.ts +84 -0
  10. package/dist/model/strategy/adapter/walletPass/evaluator.js +496 -0
  11. package/dist/model/strategy/adapter/walletPass/example.d.ts +4 -0
  12. package/dist/model/strategy/adapter/walletPass/example.js +258 -0
  13. package/dist/model/strategy/adapter/walletPass/index.d.ts +32 -0
  14. package/dist/model/strategy/adapter/walletPass/index.js +182 -0
  15. package/dist/model/strategy/adapter/walletPass/locales.d.ts +1 -0
  16. package/dist/model/strategy/adapter/walletPass/locales.js +23 -0
  17. package/dist/model/strategy/adapter/walletPass/type.d.ts +163 -0
  18. package/dist/model/strategy/adapter/walletPass/type.js +1 -0
  19. package/dist/model/strategy/adapter/walletPass/utils.d.ts +50 -0
  20. package/dist/model/strategy/adapter/walletPass/utils.js +965 -0
  21. package/dist/model/strategy/index.d.ts +94 -0
  22. package/dist/model/strategy/index.js +551 -0
  23. package/dist/model/strategy/strategy-example.d.ts +5 -0
  24. package/dist/model/strategy/strategy-example.js +331 -0
  25. package/dist/model/strategy/type.d.ts +228 -0
  26. package/dist/model/strategy/type.js +94 -0
  27. package/dist/modules/Cart/types.d.ts +2 -0
  28. package/dist/modules/Cart/utils/cartProduct.js +11 -0
  29. package/dist/modules/Discount/types.d.ts +17 -0
  30. package/dist/modules/Payment/index.d.ts +2 -1
  31. package/dist/modules/Payment/index.js +10 -7
  32. package/dist/modules/Payment/utils.js +3 -0
  33. package/dist/modules/Payment/walletpass.d.ts +23 -0
  34. package/dist/modules/Payment/walletpass.js +191 -95
  35. package/dist/modules/Product/index.d.ts +1 -1
  36. package/dist/modules/Rules/index.d.ts +8 -3
  37. package/dist/modules/Rules/index.js +152 -26
  38. package/dist/modules/Rules/types.d.ts +1 -0
  39. package/dist/modules/Summary/types.d.ts +2 -0
  40. package/dist/modules/Summary/utils.d.ts +6 -0
  41. package/dist/modules/Summary/utils.js +21 -0
  42. package/dist/plugins/window.d.ts +2 -0
  43. package/dist/solution/BookingByStep/index.d.ts +1 -0
  44. package/dist/solution/BookingTicket/index.d.ts +1 -1
  45. package/dist/solution/Checkout/index.js +2 -0
  46. package/dist/solution/ShopDiscount/index.d.ts +1 -0
  47. package/dist/solution/ShopDiscount/index.js +11 -1
  48. package/dist/solution/ShopDiscount/types.d.ts +1 -0
  49. package/dist/solution/ShopDiscount/utils.js +43 -14
  50. package/lib/index.d.ts +1 -0
  51. package/lib/index.js +3 -1
  52. package/lib/model/index.d.ts +1 -0
  53. package/lib/model/index.js +23 -0
  54. package/lib/model/strategy/adapter/index.d.ts +3 -0
  55. package/lib/model/strategy/adapter/index.js +45 -0
  56. package/lib/model/strategy/adapter/type.d.ts +28 -0
  57. package/lib/model/strategy/adapter/type.js +17 -0
  58. package/lib/model/strategy/adapter/walletPass/evaluator.d.ts +84 -0
  59. package/lib/model/strategy/adapter/walletPass/evaluator.js +420 -0
  60. package/lib/model/strategy/adapter/walletPass/example.d.ts +4 -0
  61. package/lib/model/strategy/adapter/walletPass/example.js +207 -0
  62. package/lib/model/strategy/adapter/walletPass/index.d.ts +32 -0
  63. package/lib/model/strategy/adapter/walletPass/index.js +142 -0
  64. package/lib/model/strategy/adapter/walletPass/locales.d.ts +1 -0
  65. package/lib/model/strategy/adapter/walletPass/locales.js +51 -0
  66. package/lib/model/strategy/adapter/walletPass/type.d.ts +163 -0
  67. package/lib/model/strategy/adapter/walletPass/type.js +17 -0
  68. package/lib/model/strategy/adapter/walletPass/utils.d.ts +50 -0
  69. package/lib/model/strategy/adapter/walletPass/utils.js +660 -0
  70. package/lib/model/strategy/index.d.ts +94 -0
  71. package/lib/model/strategy/index.js +413 -0
  72. package/lib/model/strategy/strategy-example.d.ts +5 -0
  73. package/lib/model/strategy/strategy-example.js +318 -0
  74. package/lib/model/strategy/type.d.ts +228 -0
  75. package/lib/model/strategy/type.js +44 -0
  76. package/lib/modules/Cart/types.d.ts +2 -0
  77. package/lib/modules/Cart/utils/cartProduct.js +9 -0
  78. package/lib/modules/Discount/types.d.ts +17 -0
  79. package/lib/modules/Payment/index.d.ts +2 -1
  80. package/lib/modules/Payment/index.js +1 -0
  81. package/lib/modules/Payment/utils.js +3 -0
  82. package/lib/modules/Payment/walletpass.d.ts +23 -0
  83. package/lib/modules/Payment/walletpass.js +94 -17
  84. package/lib/modules/Product/index.d.ts +1 -1
  85. package/lib/modules/Rules/index.d.ts +8 -3
  86. package/lib/modules/Rules/index.js +370 -192
  87. package/lib/modules/Rules/types.d.ts +1 -0
  88. package/lib/modules/Summary/types.d.ts +2 -0
  89. package/lib/modules/Summary/utils.d.ts +6 -0
  90. package/lib/modules/Summary/utils.js +15 -0
  91. package/lib/plugins/window.d.ts +2 -0
  92. package/lib/solution/BookingByStep/index.d.ts +1 -0
  93. package/lib/solution/BookingTicket/index.d.ts +1 -1
  94. package/lib/solution/BookingTicket/index.js +0 -6
  95. package/lib/solution/Checkout/index.js +2 -0
  96. package/lib/solution/ShopDiscount/index.d.ts +1 -0
  97. package/lib/solution/ShopDiscount/index.js +6 -0
  98. package/lib/solution/ShopDiscount/types.d.ts +1 -0
  99. package/lib/solution/ShopDiscount/utils.js +22 -9
  100. package/package.json +1 -1
@@ -49,6 +49,7 @@ var RulesModule = class extends import_BaseModule.BaseModule {
49
49
  this.core = core;
50
50
  this.hooks = options == null ? void 0 : options.hooks;
51
51
  this.store = options == null ? void 0 : options.store;
52
+ this.window = core.getPlugin("window");
52
53
  }
53
54
  async setRulesList(rulesList) {
54
55
  this.store.rulesList = rulesList;
@@ -57,6 +58,10 @@ var RulesModule = class extends import_BaseModule.BaseModule {
57
58
  getRulesList() {
58
59
  return this.store.rulesList;
59
60
  }
61
+ getWalletPassEvaluator() {
62
+ var _a, _b;
63
+ return (_b = (_a = this.window).getWalletPassEvaluator) == null ? void 0 : _b.call(_a);
64
+ }
60
65
  // 商品不需要holder,则不需要判断,直接返回true,商品需要holder但是还没填写,那么暂时不使用带有holder的券,直到填写才去匹配
61
66
  checkHolderMatch(discount, product, holders) {
62
67
  var _a;
@@ -76,6 +81,7 @@ var RulesModule = class extends import_BaseModule.BaseModule {
76
81
  newDiscountList,
77
82
  productList,
78
83
  holders,
84
+ orderTotalAmount,
79
85
  isFormSubject
80
86
  }) {
81
87
  if (!newDiscountList || newDiscountList.length === 0) {
@@ -96,15 +102,18 @@ var RulesModule = class extends import_BaseModule.BaseModule {
96
102
  productList
97
103
  };
98
104
  }
99
- const filteredOldDiscountList = oldDiscountList.filter((discount) => !discount.isEditMode && discount.tag !== "good_pass");
100
- const mergedDiscountList = (0, import_utils.uniqueById)((0, import_utils.uniqueById)([
101
- ...filteredOldDiscountList,
102
- ...newDiscountList
103
- ]), "product_id");
105
+ const filteredOldDiscountList = oldDiscountList.filter(
106
+ (discount) => !discount.isEditMode && discount.tag !== "good_pass"
107
+ );
108
+ const mergedDiscountList = (0, import_utils.uniqueById)(
109
+ (0, import_utils.uniqueById)([...filteredOldDiscountList, ...newDiscountList]),
110
+ "product_id"
111
+ );
104
112
  const result = this.calcDiscount({
105
113
  discountList: mergedDiscountList,
106
114
  productList: [...productList],
107
115
  holders,
116
+ orderTotalAmount,
108
117
  isFormSubject
109
118
  }, {
110
119
  scan: true
@@ -139,6 +148,7 @@ var RulesModule = class extends import_BaseModule.BaseModule {
139
148
  discountList,
140
149
  productList,
141
150
  holders,
151
+ orderTotalAmount,
142
152
  isFormSubject
143
153
  }, options) {
144
154
  const isEditModeAddNewProduct = productList.find((n) => n.booking_id) && productList.find((n) => !n.booking_id);
@@ -259,7 +269,50 @@ var RulesModule = class extends import_BaseModule.BaseModule {
259
269
  }
260
270
  return priceB.minus(priceA).toNumber();
261
271
  });
272
+ const evaluator = this.getWalletPassEvaluator();
273
+ if (evaluator) {
274
+ addModeDiscount.forEach((discount) => {
275
+ var _a, _b;
276
+ const discountType = discount.tag || discount.type;
277
+ if (["good_pass", "discount_card", "product_discount_card"].includes(
278
+ discountType
279
+ )) {
280
+ const voucher = {
281
+ id: discount.id,
282
+ amount: Number(discount.par_value || 0),
283
+ balance: Number(discount.balance || 0),
284
+ type: discountType,
285
+ product_id: discount.product_id,
286
+ unified_available_status: 1,
287
+ available_product_type: (_a = discount.limited_relation_product_data) == null ? void 0 : _a.type,
288
+ available_product_ids: (_b = discount.limited_relation_product_data) == null ? void 0 : _b.product_ids
289
+ };
290
+ const productsForEvaluate = sortedFlattenedList.map((item) => ({
291
+ product_id: item.id,
292
+ price: item.price || 0,
293
+ quantity: item.quantity || item.num || 1,
294
+ selling_price: item.price || 0
295
+ }));
296
+ const result = evaluator.checkVoucherAvailability({
297
+ orderTotalAmount,
298
+ products: productsForEvaluate,
299
+ vouchers: [voucher]
300
+ });
301
+ if (result.isAvailable) {
302
+ discount.config = {
303
+ ...result.config,
304
+ isAvailable: true
305
+ };
306
+ } else {
307
+ discount.config = {
308
+ isAvailable: false
309
+ };
310
+ }
311
+ }
312
+ });
313
+ }
262
314
  const usedDiscounts = /* @__PURE__ */ new Map();
315
+ const usedProductIdCounts = /* @__PURE__ */ new Map();
263
316
  const discountApplicability = /* @__PURE__ */ new Map();
264
317
  const discountApplicableProducts = /* @__PURE__ */ new Map();
265
318
  addModeDiscount.forEach((discount) => {
@@ -288,7 +341,7 @@ var RulesModule = class extends import_BaseModule.BaseModule {
288
341
  originProduct = flatItem.originProduct;
289
342
  }
290
343
  addModeDiscount.forEach((discount) => {
291
- var _a, _b, _c, _d, _e, _f;
344
+ var _a, _b, _c, _d, _e, _f, _g;
292
345
  const limitedData = discount == null ? void 0 : discount.limited_relation_product_data;
293
346
  const _tempVar = (flatItem == null ? void 0 : flatItem.type) === "bundle" ? flatItem == null ? void 0 : flatItem.parentProduct : flatItem == null ? void 0 : flatItem.product;
294
347
  const isHolderMatch = this.checkHolderMatch(
@@ -301,11 +354,17 @@ var RulesModule = class extends import_BaseModule.BaseModule {
301
354
  );
302
355
  let timeLimit = true;
303
356
  timeLimit = !!(0, import_utils.filterDiscountListByBookingTime)([discount], ((product == null ? void 0 : product.startDate) || (0, import_dayjs.default)()).format("YYYY-MM-DD HH:mm:ss")).length;
304
- const isLimitedProduct = (limitedData.type === "product_all" || limitedData.product_ids && limitedData.product_ids.includes(product.id)) && isHolderMatch;
305
- const isAvailableProduct = flatItem.type === "main" ? !((product == null ? void 0 : product.booking_id) && ((_a = product == null ? void 0 : product.discount_list) == null ? void 0 : _a.length) && ((_b = product == null ? void 0 : product.discount_list) == null ? void 0 : _b.every((discount2) => discount2.id && ["good_pass", "discount_card", "product_discount_card"].includes(discount2.tag || discount2.type)))) : !((flatItem == null ? void 0 : flatItem.booking_id) && ((_d = (_c = flatItem == null ? void 0 : flatItem.bundleItem) == null ? void 0 : _c.metadata) == null ? void 0 : _d.custom_product_bundle_map_id));
357
+ const isLimitedProduct = (limitedData.type === "product_all" && limitedData.filter !== 1 || limitedData.type === "product_all" && limitedData.filter === 1 && !limitedData.exclude_product_ids.includes(product.id) || limitedData.product_ids && limitedData.product_ids.includes(product.id)) && isHolderMatch;
358
+ const isAvailableProduct = flatItem.type === "main" ? !((product == null ? void 0 : product.booking_id) && ((_a = product == null ? void 0 : product.discount_list) == null ? void 0 : _a.length) && ((_b = product == null ? void 0 : product.discount_list) == null ? void 0 : _b.every(
359
+ (discount2) => discount2.id && [
360
+ "good_pass",
361
+ "discount_card",
362
+ "product_discount_card"
363
+ ].includes(discount2.tag || discount2.type)
364
+ ))) : !((flatItem == null ? void 0 : flatItem.booking_id) && ((_d = (_c = flatItem == null ? void 0 : flatItem.bundleItem) == null ? void 0 : _c.metadata) == null ? void 0 : _d.custom_product_bundle_map_id));
306
365
  const isBundleAvailable = this.checkPackageSubItemUsageRules(discount, flatItem);
307
- if (isAvailableProduct && isLimitedProduct && timeLimit && isBundleAvailable) {
308
- (_e = discountApplicability.get(discount.id)) == null ? void 0 : _e.push(product.id);
366
+ if (isAvailableProduct && isLimitedProduct && timeLimit && isBundleAvailable && ((_e = discount.config) == null ? void 0 : _e.isAvailable)) {
367
+ (_f = discountApplicability.get(discount.id)) == null ? void 0 : _f.push(product.id);
309
368
  const applicableProducts = discountApplicableProducts.get(discount.id) || [];
310
369
  const discountType = discount.tag || discount.type;
311
370
  const isGoodPass = discountType === "good_pass";
@@ -315,13 +374,14 @@ var RulesModule = class extends import_BaseModule.BaseModule {
315
374
  type: discountType,
316
375
  tag: discountType,
317
376
  discount: {
318
- discount_card_type: (_f = discount == null ? void 0 : discount.metadata) == null ? void 0 : _f.discount_card_type,
377
+ discount_card_type: (_g = discount == null ? void 0 : discount.metadata) == null ? void 0 : _g.discount_card_type,
319
378
  fixed_amount: product.price,
320
379
  resource_id: discount.id,
321
380
  title: discount.format_title,
322
381
  original_amount: product.price || product.origin_total,
323
382
  pre_value: discount.par_value,
324
- product_id: originProduct.id
383
+ product_id: originProduct.id,
384
+ discount_product_id: discount.product_id
325
385
  },
326
386
  metadata: {
327
387
  num
@@ -354,25 +414,39 @@ var RulesModule = class extends import_BaseModule.BaseModule {
354
414
  };
355
415
  originProduct = flatItem.originProduct;
356
416
  }
357
- if ((product == null ? void 0 : product.booking_id) && ((_d = product.discount_list) == null ? void 0 : _d.length) && ((_e = product == null ? void 0 : product.discount_list) == null ? void 0 : _e.every((discount) => discount.id && ["good_pass", "discount_card", "product_discount_card"].includes(discount.tag || discount.type)))) {
417
+ if ((product == null ? void 0 : product.booking_id) && ((_d = product.discount_list) == null ? void 0 : _d.length) && ((_e = product == null ? void 0 : product.discount_list) == null ? void 0 : _e.every(
418
+ (discount) => discount.id && ["good_pass", "discount_card", "product_discount_card"].includes(
419
+ discount.tag || discount.type
420
+ )
421
+ ))) {
358
422
  if (flatItem.type === "main") {
359
423
  processedProductsMap.set(product._id, [originProduct]);
360
424
  } else {
361
- processedFlatItemsMap.set(flatItem._id, [{
362
- ...flatItem,
363
- processed: true
364
- }]);
425
+ processedFlatItemsMap.set(flatItem._id, [
426
+ {
427
+ ...flatItem,
428
+ processed: true
429
+ }
430
+ ]);
365
431
  }
366
432
  return;
367
433
  }
368
434
  const applicableDiscounts = sortedDiscountList.filter((discount) => {
369
- var _a2, _b2;
370
- if ((Number(product.price) <= 0 || !product.price) && !((_a2 = product.discount_list) == null ? void 0 : _a2.find((n) => {
435
+ var _a2, _b2, _c2, _d2;
436
+ const discountType = discount.tag || discount.type;
437
+ if (["good_pass", "discount_card", "product_discount_card"].includes(
438
+ discountType
439
+ )) {
440
+ if (discount.config === void 0 || !((_a2 = discount == null ? void 0 : discount.config) == null ? void 0 : _a2.isAvailable)) {
441
+ return false;
442
+ }
443
+ }
444
+ if ((Number(product.price) <= 0 || !product.price) && !((_b2 = product.discount_list) == null ? void 0 : _b2.find((n) => {
371
445
  var _a3;
372
446
  return ((_a3 = n.discount) == null ? void 0 : _a3.resource_id) === discount.id;
373
447
  })) && (discount.tag || discount.type) === "good_pass")
374
448
  return false;
375
- if ((Number(product.price) <= 0 || !product.price) && !((_b2 = product.discount_list) == null ? void 0 : _b2.find((n) => {
449
+ if ((Number(product.price) <= 0 || !product.price) && !((_c2 = product.discount_list) == null ? void 0 : _c2.find((n) => {
376
450
  var _a3;
377
451
  return ((_a3 = n.discount) == null ? void 0 : _a3.resource_id) === discount.id;
378
452
  })) && (discount.tag || discount.type) !== "good_pass")
@@ -380,6 +454,15 @@ var RulesModule = class extends import_BaseModule.BaseModule {
380
454
  const targetUsedDiscounts = usedDiscounts.get(discount.id);
381
455
  if (targetUsedDiscounts && (discount.tag || discount.type) === "good_pass")
382
456
  return false;
457
+ if ((discount.tag || discount.type) === "good_pass") {
458
+ const maxUsagePerOrder = (_d2 = discount.config) == null ? void 0 : _d2.maxUsagePerOrder;
459
+ if (maxUsagePerOrder && maxUsagePerOrder > 0) {
460
+ const currentUsedCount = usedProductIdCounts.get(discount.product_id) || 0;
461
+ if (currentUsedCount >= maxUsagePerOrder) {
462
+ return false;
463
+ }
464
+ }
465
+ }
383
466
  const limitedData = discount.limited_relation_product_data;
384
467
  const _tempVar = (flatItem == null ? void 0 : flatItem.type) === "bundle" ? flatItem == null ? void 0 : flatItem.parentProduct : flatItem == null ? void 0 : flatItem.product;
385
468
  const isHolderMatch = this.checkHolderMatch(
@@ -398,6 +481,9 @@ var RulesModule = class extends import_BaseModule.BaseModule {
398
481
  return false;
399
482
  }
400
483
  if (limitedData.type === "product_all") {
484
+ if (limitedData.filter === 1 && limitedData.exclude_product_ids.includes(product.id)) {
485
+ return false;
486
+ }
401
487
  if (!this.checkPackageSubItemUsageRules(discount, flatItem)) {
402
488
  return false;
403
489
  }
@@ -410,66 +496,96 @@ var RulesModule = class extends import_BaseModule.BaseModule {
410
496
  }
411
497
  return false;
412
498
  });
413
- const selectedDiscountCard = applicableDiscounts.find((n) => n.isScan && n.isSelected && (n.tag || n.type) !== "good_pass");
499
+ const selectedDiscountCard = applicableDiscounts.find(
500
+ (n) => n.isScan && n.isSelected && (n.tag || n.type) !== "good_pass"
501
+ );
414
502
  const selectedDiscount = selectedDiscountCard || applicableDiscounts[0];
415
503
  let isManualDiscount = false;
416
504
  if (flatItem.type === "main") {
417
- isManualDiscount = typeof product.isManualDiscount === "boolean" ? product.isManualDiscount : product.total != product.origin_total && (product.bundle || []).every((item) => {
418
- var _a2;
419
- return !((_a2 = item.discount_list || []) == null ? void 0 : _a2.length);
420
- }) && (!((_f = product.discount_list) == null ? void 0 : _f.length) || ((_h = (_g = product == null ? void 0 : product.discount_list) == null ? void 0 : _g.every) == null ? void 0 : _h.call(_g, (item) => item.type === "product")));
505
+ isManualDiscount = typeof product.isManualDiscount === "boolean" ? product.isManualDiscount : product.total != product.origin_total && (product.bundle || []).every(
506
+ (item) => {
507
+ var _a2;
508
+ return !((_a2 = item.discount_list || []) == null ? void 0 : _a2.length);
509
+ }
510
+ ) && (!((_f = product.discount_list) == null ? void 0 : _f.length) || ((_h = (_g = product == null ? void 0 : product.discount_list) == null ? void 0 : _g.every) == null ? void 0 : _h.call(
511
+ _g,
512
+ (item) => item.type === "product"
513
+ )));
421
514
  } else {
422
515
  const parentProduct = flatItem.parentProduct;
423
516
  if (parentProduct) {
424
- isManualDiscount = typeof parentProduct.isManualDiscount === "boolean" ? parentProduct.isManualDiscount : parentProduct.total != parentProduct.origin_total && (parentProduct.bundle || []).every((item) => {
425
- var _a2;
426
- return !((_a2 = item.discount_list || []) == null ? void 0 : _a2.length);
427
- }) && (!((_i = parentProduct.discount_list) == null ? void 0 : _i.length) || ((_k = (_j = parentProduct == null ? void 0 : parentProduct.discount_list) == null ? void 0 : _j.every) == null ? void 0 : _k.call(_j, (item) => item.type === "product")));
517
+ isManualDiscount = typeof parentProduct.isManualDiscount === "boolean" ? parentProduct.isManualDiscount : parentProduct.total != parentProduct.origin_total && (parentProduct.bundle || []).every(
518
+ (item) => {
519
+ var _a2;
520
+ return !((_a2 = item.discount_list || []) == null ? void 0 : _a2.length);
521
+ }
522
+ ) && (!((_i = parentProduct.discount_list) == null ? void 0 : _i.length) || ((_k = (_j = parentProduct == null ? void 0 : parentProduct.discount_list) == null ? void 0 : _j.every) == null ? void 0 : _k.call(
523
+ _j,
524
+ (item) => item.type === "product"
525
+ )));
428
526
  }
429
527
  }
430
528
  if (options == null ? void 0 : options.discountId) {
431
- if (flatItem.type === "main" && ((_l = product.discount_list) == null ? void 0 : _l.some((item) => {
432
- var _a2;
433
- return ((_a2 = item.discount) == null ? void 0 : _a2.resource_id) === options.discountId;
434
- }))) {
529
+ if (flatItem.type === "main" && ((_l = product.discount_list) == null ? void 0 : _l.some(
530
+ (item) => {
531
+ var _a2;
532
+ return ((_a2 = item.discount) == null ? void 0 : _a2.resource_id) === options.discountId;
533
+ }
534
+ ))) {
435
535
  isManualDiscount = false;
436
536
  }
437
537
  if (flatItem.type === "bundle") {
438
- if (((_m = product.discount_list) == null ? void 0 : _m.some((item) => {
439
- var _a2;
440
- return ((_a2 = item.discount) == null ? void 0 : _a2.resource_id) === options.discountId;
441
- })) || ((_o = (_n = flatItem.parentProduct) == null ? void 0 : _n.discount_list) == null ? void 0 : _o.some((item) => {
442
- var _a2;
443
- return ((_a2 = item.discount) == null ? void 0 : _a2.resource_id) === options.discountId;
444
- }))) {
538
+ if (((_m = product.discount_list) == null ? void 0 : _m.some(
539
+ (item) => {
540
+ var _a2;
541
+ return ((_a2 = item.discount) == null ? void 0 : _a2.resource_id) === options.discountId;
542
+ }
543
+ )) || ((_o = (_n = flatItem.parentProduct) == null ? void 0 : _n.discount_list) == null ? void 0 : _o.some(
544
+ (item) => {
545
+ var _a2;
546
+ return ((_a2 = item.discount) == null ? void 0 : _a2.resource_id) === options.discountId;
547
+ }
548
+ ))) {
445
549
  isManualDiscount = false;
446
550
  }
447
551
  }
448
552
  }
449
553
  if (options == null ? void 0 : options.selectedList) {
450
- if (flatItem.type === "main" && ((_p = product.discount_list) == null ? void 0 : _p.some((item) => {
451
- var _a2;
452
- return (_a2 = options == null ? void 0 : options.selectedList) == null ? void 0 : _a2.some((n) => {
453
- var _a3;
454
- return n.discountId === ((_a3 = item.discount) == null ? void 0 : _a3.resource_id);
455
- });
456
- }))) {
554
+ if (flatItem.type === "main" && ((_p = product.discount_list) == null ? void 0 : _p.some(
555
+ (item) => {
556
+ var _a2;
557
+ return (_a2 = options == null ? void 0 : options.selectedList) == null ? void 0 : _a2.some(
558
+ (n) => {
559
+ var _a3;
560
+ return n.discountId === ((_a3 = item.discount) == null ? void 0 : _a3.resource_id);
561
+ }
562
+ );
563
+ }
564
+ ))) {
457
565
  isManualDiscount = false;
458
566
  }
459
567
  if (flatItem.type === "bundle") {
460
- if (((_q = product.discount_list) == null ? void 0 : _q.some((item) => {
461
- var _a2;
462
- return (_a2 = options == null ? void 0 : options.selectedList) == null ? void 0 : _a2.some((n) => {
463
- var _a3;
464
- return n.discountId === ((_a3 = item.discount) == null ? void 0 : _a3.resource_id);
465
- });
466
- })) || ((_s = (_r = flatItem.parentProduct) == null ? void 0 : _r.discount_list) == null ? void 0 : _s.some((item) => {
467
- var _a2;
468
- return (_a2 = options == null ? void 0 : options.selectedList) == null ? void 0 : _a2.some((n) => {
469
- var _a3;
470
- return n.discountId === ((_a3 = item.discount) == null ? void 0 : _a3.resource_id);
471
- });
472
- }))) {
568
+ if (((_q = product.discount_list) == null ? void 0 : _q.some(
569
+ (item) => {
570
+ var _a2;
571
+ return (_a2 = options == null ? void 0 : options.selectedList) == null ? void 0 : _a2.some(
572
+ (n) => {
573
+ var _a3;
574
+ return n.discountId === ((_a3 = item.discount) == null ? void 0 : _a3.resource_id);
575
+ }
576
+ );
577
+ }
578
+ )) || ((_s = (_r = flatItem.parentProduct) == null ? void 0 : _r.discount_list) == null ? void 0 : _s.some(
579
+ (item) => {
580
+ var _a2;
581
+ return (_a2 = options == null ? void 0 : options.selectedList) == null ? void 0 : _a2.some(
582
+ (n) => {
583
+ var _a3;
584
+ return n.discountId === ((_a3 = item.discount) == null ? void 0 : _a3.resource_id);
585
+ }
586
+ );
587
+ }
588
+ ))) {
473
589
  isManualDiscount = false;
474
590
  }
475
591
  }
@@ -477,9 +593,8 @@ var RulesModule = class extends import_BaseModule.BaseModule {
477
593
  if (applicableDiscounts.length === 0 || isManualDiscount || (0, import_lodash_es.isBoolean)(product.vouchersApplicable) && !product.vouchersApplicable) {
478
594
  if (flatItem.type === "main") {
479
595
  if (product.isClient) {
480
- processedProductsMap.set(
481
- product._id,
482
- [this.hooks.setProduct(originProduct, {
596
+ processedProductsMap.set(product._id, [
597
+ this.hooks.setProduct(originProduct, {
483
598
  ...isManualDiscount ? {} : {
484
599
  origin_total: (0, import_utils2.getProductOriginTotalPrice)({
485
600
  product: {
@@ -500,12 +615,11 @@ var RulesModule = class extends import_BaseModule.BaseModule {
500
615
  price: product.price
501
616
  },
502
617
  discount_list: []
503
- })]
504
- );
618
+ })
619
+ ]);
505
620
  } else {
506
- processedProductsMap.set(
507
- product._id,
508
- [this.hooks.setProduct(originProduct, {
621
+ processedProductsMap.set(product._id, [
622
+ this.hooks.setProduct(originProduct, {
509
623
  ...isManualDiscount ? {
510
624
  price: product.price,
511
625
  main_product_selling_price: product.price
@@ -516,8 +630,8 @@ var RulesModule = class extends import_BaseModule.BaseModule {
516
630
  main_product_selling_price: product.price
517
631
  },
518
632
  discount_list: []
519
- })]
520
- );
633
+ })
634
+ ]);
521
635
  }
522
636
  } else {
523
637
  processedFlatItemsMap.set(flatItem._id, [{
@@ -534,20 +648,36 @@ var RulesModule = class extends import_BaseModule.BaseModule {
534
648
  }
535
649
  const isNeedSplit = (selectedDiscount.tag || selectedDiscount.type) === "good_pass";
536
650
  const totalQuantity = product.quantity || product.num || 1;
537
- const availableGoodPassCount = applicableDiscounts.filter((item) => (item.tag || item.type) === "good_pass").length;
538
- const splitCount = isNeedSplit ? Math.min(totalQuantity, availableGoodPassCount) : 1;
651
+ const availableGoodPassCount = applicableDiscounts.filter(
652
+ (item) => (item.tag || item.type) === "good_pass"
653
+ ).length;
654
+ let maxUsageLimit = availableGoodPassCount;
655
+ if (selectedDiscount.config && selectedDiscount.config.maxUsagePerOrder) {
656
+ maxUsageLimit = Math.min(
657
+ availableGoodPassCount,
658
+ selectedDiscount.config.maxUsagePerOrder
659
+ );
660
+ }
661
+ const splitCount = isNeedSplit ? Math.min(product.quantity || product.num || 1, maxUsageLimit) : 1;
539
662
  const arr = [];
540
663
  if (flatItem.type === "main") {
541
664
  if (splitCount < totalQuantity && isNeedSplit) {
542
- arr.push(this.hooks.setProduct(originProduct, {
543
- discount_list: [],
544
- quantity: totalQuantity - splitCount,
545
- _id: product._id.split("___")[0]
546
- }));
665
+ arr.push(
666
+ this.hooks.setProduct(originProduct, {
667
+ discount_list: [],
668
+ quantity: totalQuantity - splitCount,
669
+ _id: product._id.split("___")[0],
670
+ total: product.origin_total || product.total
671
+ })
672
+ );
547
673
  }
548
674
  for (let i = 0; i < splitCount; i++) {
549
675
  const selectedDiscount2 = selectedDiscountCard || applicableDiscounts[i];
550
676
  usedDiscounts.set(selectedDiscount2.id, true);
677
+ if ((selectedDiscount2.tag || selectedDiscount2.type) === "good_pass") {
678
+ const currentCount = usedProductIdCounts.get(selectedDiscount2.product_id) || 0;
679
+ usedProductIdCounts.set(selectedDiscount2.product_id, currentCount + 1);
680
+ }
551
681
  const appliedProducts = appliedDiscountProducts.get(selectedDiscount2.id) || [];
552
682
  let productOriginTotal = product.origin_total || product.total || 0;
553
683
  if (((_t = product.discount_list) == null ? void 0 : _t.length) && product.origin_total) {
@@ -556,24 +686,30 @@ var RulesModule = class extends import_BaseModule.BaseModule {
556
686
  if (Number(((_u = originProduct == null ? void 0 : originProduct._productInit) == null ? void 0 : _u.original_price) || 0) > 0 && product.origin_total && product.total && product.origin_total !== product.total) {
557
687
  productOriginTotal = product.total;
558
688
  }
559
- const targetProductTotal = (0, import_utils.getDiscountAmount)(selectedDiscount2, product.price, product.price);
560
- const discountType = selectedDiscount2.tag || selectedDiscount2.type;
689
+ const targetProductTotal = (0, import_utils.getDiscountAmount)(
690
+ selectedDiscount2,
691
+ product.price,
692
+ product.price
693
+ );
694
+ const discountType = selectedDiscount2.tag === "product_discount_card" ? "discount_card" : selectedDiscount2.tag;
695
+ const actualDiscountAmount = new import_decimal.default(product.price).minus(new import_decimal.default(targetProductTotal)).toNumber();
561
696
  const isGoodPass = discountType === "good_pass";
562
- const amount = new import_decimal.default(product.price).minus(new import_decimal.default(targetProductTotal)).toNumber();
563
697
  const discountDetail = {
564
- amount,
565
- type: selectedDiscount2.tag === "product_discount_card" ? "discount_card" : discountType,
698
+ amount: actualDiscountAmount,
699
+ type: discountType,
566
700
  discount: {
567
701
  discount_card_type: (_v = selectedDiscount2 == null ? void 0 : selectedDiscount2.metadata) == null ? void 0 : _v.discount_card_type,
568
- fixed_amount: amount,
702
+ fixed_amount: actualDiscountAmount,
569
703
  resource_id: selectedDiscount2.id,
570
704
  title: selectedDiscount2.format_title,
571
705
  original_amount: product.price,
572
706
  product_id: originProduct.id,
573
- percent: selectedDiscount2.par_value
707
+ percent: selectedDiscount2.par_value,
708
+ discount_product_id: selectedDiscount2.product_id
574
709
  },
575
710
  // 前端使用的num数量,为了计算优惠金额
576
711
  _num: isGoodPass ? 1 : product.num,
712
+ config: selectedDiscount2 == null ? void 0 : selectedDiscount2.config,
577
713
  metadata: {
578
714
  num: 1
579
715
  }
@@ -589,7 +725,6 @@ var RulesModule = class extends import_BaseModule.BaseModule {
589
725
  }, new import_decimal.default(total)).toNumber();
590
726
  }
591
727
  if (product.isClient) {
592
- debugger;
593
728
  arr.push(this.hooks.setProduct(originProduct, {
594
729
  discount_list: [discountDetail],
595
730
  price: selectedDiscount2.tag === "good_pass" ? 0 : product.price,
@@ -626,23 +761,35 @@ var RulesModule = class extends import_BaseModule.BaseModule {
626
761
  for (let i = 0; i < discountNum; i++) {
627
762
  const selectedDiscount2 = applicableDiscounts[i];
628
763
  usedDiscounts.set(selectedDiscount2.id, true);
764
+ if ((selectedDiscount2.tag || selectedDiscount2.type) === "good_pass") {
765
+ const currentCount = usedProductIdCounts.get(selectedDiscount2.product_id) || 0;
766
+ usedProductIdCounts.set(selectedDiscount2.product_id, currentCount + 1);
767
+ }
629
768
  const uniqueId = `${flatItem._id}_split_${i}`;
769
+ const discountedPrice = (0, import_utils.getDiscountAmount)(
770
+ selectedDiscount2,
771
+ product.origin_total,
772
+ product.origin_total
773
+ );
774
+ const bundleDiscountAmount = new import_decimal.default(product.origin_total || 0).minus(discountedPrice).toNumber();
630
775
  const discountDetail = {
631
- amount: product.origin_total,
776
+ amount: bundleDiscountAmount,
632
777
  type: "good_pass",
633
778
  discount: {
634
779
  fixed_amount: product.origin_total,
635
780
  resource_id: selectedDiscount2.id,
636
781
  title: selectedDiscount2.format_title,
637
782
  original_amount: product.origin_total,
638
- product_id: product.id
783
+ product_id: product.id,
784
+ discount_product_id: selectedDiscount2.product_id
639
785
  },
640
786
  metadata: {
641
787
  // 🔥 使用拆分后的唯一 _id
642
788
  custom_product_bundle_map_id: uniqueId,
643
789
  num: 1
644
790
  },
645
- _num: 1
791
+ _num: 1,
792
+ config: selectedDiscount2 == null ? void 0 : selectedDiscount2.config
646
793
  };
647
794
  const appliedProducts = appliedDiscountProducts.get(selectedDiscount2.id) || [];
648
795
  appliedProducts.push(discountDetail);
@@ -653,9 +800,9 @@ var RulesModule = class extends import_BaseModule.BaseModule {
653
800
  _id: uniqueId,
654
801
  num: 1,
655
802
  quantity: 1,
656
- price: 0,
657
- // 商品券价格为0
658
- total: 0,
803
+ price: discountedPrice,
804
+ // 折扣后的价格
805
+ total: discountedPrice,
659
806
  discount_list: [discountDetail],
660
807
  processed: true,
661
808
  _discountId: selectedDiscount2.id
@@ -675,30 +822,37 @@ var RulesModule = class extends import_BaseModule.BaseModule {
675
822
  } else {
676
823
  const selectedDiscount2 = selectedDiscountCard || applicableDiscounts[0];
677
824
  usedDiscounts.set(selectedDiscount2.id, true);
825
+ if ((selectedDiscount2.tag || selectedDiscount2.type) === "good_pass") {
826
+ const currentCount = usedProductIdCounts.get(selectedDiscount2.product_id) || 0;
827
+ usedProductIdCounts.set(selectedDiscount2.product_id, currentCount + 1);
828
+ }
678
829
  const productOriginTotal = product.original_price || product.price || 0;
679
830
  const targetProductTotal = (0, import_utils.getDiscountAmount)(
680
831
  selectedDiscount2,
681
832
  productOriginTotal,
682
833
  productOriginTotal
683
834
  );
835
+ const bundleActualDiscountAmount = new import_decimal.default(productOriginTotal).minus(targetProductTotal).toNumber();
684
836
  const uniqueId = flatItem._id;
685
837
  const discountDetail = {
686
- amount: new import_decimal.default(productOriginTotal).minus(targetProductTotal).toNumber() * (product.num || 1),
838
+ amount: bundleActualDiscountAmount * (product.num || 1),
687
839
  type: selectedDiscount2.tag === "product_discount_card" ? "discount_card" : selectedDiscount2.tag,
688
840
  discount: {
689
841
  discount_card_type: (_w = selectedDiscount2 == null ? void 0 : selectedDiscount2.metadata) == null ? void 0 : _w.discount_card_type,
690
- fixed_amount: new import_decimal.default(productOriginTotal).minus(targetProductTotal).toNumber(),
842
+ fixed_amount: bundleActualDiscountAmount,
691
843
  resource_id: selectedDiscount2.id,
692
844
  title: selectedDiscount2.format_title,
693
845
  original_amount: product.original_price,
694
846
  product_id: product.id,
695
- percent: selectedDiscount2.par_value
847
+ percent: selectedDiscount2.par_value,
848
+ discount_product_id: selectedDiscount2.product_id
696
849
  },
697
850
  metadata: {
698
851
  // 🔥 使用唯一的 _id
699
852
  custom_product_bundle_map_id: uniqueId,
700
853
  num: product.num || 1
701
854
  },
855
+ config: selectedDiscount2 == null ? void 0 : selectedDiscount2.config,
702
856
  _num: (product.num || 1) * (((_x = flatItem == null ? void 0 : flatItem.parentProduct) == null ? void 0 : _x.num) || 1)
703
857
  };
704
858
  const appliedProducts = appliedDiscountProducts.get(selectedDiscount2.id) || [];
@@ -756,7 +910,6 @@ var RulesModule = class extends import_BaseModule.BaseModule {
756
910
  return;
757
911
  }
758
912
  const hasBundle = product.bundle && Array.isArray(product.bundle) && product.bundle.length > 0;
759
- debugger;
760
913
  if (!hasBundle) {
761
914
  result.push(...mainProductArr);
762
915
  } else {
@@ -789,59 +942,65 @@ var RulesModule = class extends import_BaseModule.BaseModule {
789
942
  const mainProduct = mainProductArr[0];
790
943
  const mainProductData = this.hooks.getProduct(mainProduct);
791
944
  const newBundleWithDiscount = [];
792
- (product.bundle || []).forEach((bundleItem, bundleIndex) => {
793
- const processedItems = bundleProcessingInfo.get(bundleIndex) || [bundleItem];
794
- if (processedItems.length > 1) {
795
- processedItems.forEach((item) => {
796
- const updatedDiscountList2 = (item.discount_list || []).map((discount) => {
797
- var _a;
798
- return {
799
- ...discount,
800
- metadata: {
801
- num: item.num,
802
- custom_product_bundle_map_id: (_a = item.metadata) == null ? void 0 : _a.custom_product_bundle_map_id
803
- }
804
- // num: item.num, // 使用拆分后的 num
805
- };
806
- });
807
- newBundleWithDiscount.push({
808
- ...bundleItem,
809
- _id: item._id,
810
- product_id: bundleItem.product_id,
811
- price: item.price,
812
- num: item.num,
813
- discount_list: updatedDiscountList2
814
- });
815
- });
816
- } else {
817
- const item = processedItems[0];
818
- if (item.processed) {
819
- const updatedDiscountList2 = (item.discount_list || []).map((discount) => {
820
- var _a;
821
- return {
822
- ...discount,
823
- metadata: {
824
- num: item.num,
825
- custom_product_bundle_map_id: (_a = item.metadata) == null ? void 0 : _a.custom_product_bundle_map_id
826
- }
827
- // num: item.num, // 使用当前的 num
828
- };
829
- });
830
- newBundleWithDiscount.push({
831
- ...bundleItem,
832
- _id: item._id,
833
- product_id: bundleItem.product_id,
834
- price: item.price,
835
- num: item.num,
836
- discount_list: updatedDiscountList2
945
+ (product.bundle || []).forEach(
946
+ (bundleItem, bundleIndex) => {
947
+ const processedItems = bundleProcessingInfo.get(
948
+ bundleIndex
949
+ ) || [bundleItem];
950
+ if (processedItems.length > 1) {
951
+ processedItems.forEach((item) => {
952
+ const updatedDiscountList2 = (item.discount_list || []).map((discount) => {
953
+ var _a;
954
+ return {
955
+ ...discount,
956
+ metadata: {
957
+ num: item.num,
958
+ custom_product_bundle_map_id: (_a = item.metadata) == null ? void 0 : _a.custom_product_bundle_map_id
959
+ }
960
+ // num: item.num, // 使用拆分后的 num
961
+ };
962
+ });
963
+ newBundleWithDiscount.push({
964
+ ...bundleItem,
965
+ _id: item._id,
966
+ product_id: bundleItem.product_id,
967
+ price: item.price,
968
+ num: item.num,
969
+ discount_list: updatedDiscountList2
970
+ });
837
971
  });
838
972
  } else {
839
- newBundleWithDiscount.push(item);
973
+ const item = processedItems[0];
974
+ if (item.processed) {
975
+ const updatedDiscountList2 = (item.discount_list || []).map((discount) => {
976
+ var _a;
977
+ return {
978
+ ...discount,
979
+ metadata: {
980
+ num: item.num,
981
+ custom_product_bundle_map_id: (_a = item.metadata) == null ? void 0 : _a.custom_product_bundle_map_id
982
+ }
983
+ // num: item.num, // 使用当前的 num
984
+ };
985
+ });
986
+ newBundleWithDiscount.push({
987
+ ...bundleItem,
988
+ _id: item._id,
989
+ product_id: bundleItem.product_id,
990
+ price: item.price,
991
+ num: item.num,
992
+ discount_list: updatedDiscountList2
993
+ });
994
+ } else {
995
+ newBundleWithDiscount.push(item);
996
+ }
840
997
  }
841
998
  }
842
- });
999
+ );
843
1000
  let newTotalWithDiscount = Number(mainProductData.price || 0);
844
- let newOriginTotalWithDiscount = Number(mainProductData.original_price || mainProductData.price || 0);
1001
+ let newOriginTotalWithDiscount = Number(
1002
+ mainProductData.original_price || mainProductData.price || 0
1003
+ );
845
1004
  const updatedMainDiscountList = mainProductData.discount_list.map((discount) => {
846
1005
  var _a, _b;
847
1006
  if ((_a = discount == null ? void 0 : discount.metadata) == null ? void 0 : _a.custom_product_bundle_map_id) {
@@ -856,10 +1015,12 @@ var RulesModule = class extends import_BaseModule.BaseModule {
856
1015
  }
857
1016
  };
858
1017
  });
859
- const mainDiscountList = updatedMainDiscountList.filter((item) => {
860
- var _a;
861
- return !((_a = item == null ? void 0 : item.metadata) == null ? void 0 : _a.custom_product_bundle_map_id);
862
- });
1018
+ const mainDiscountList = updatedMainDiscountList.filter(
1019
+ (item) => {
1020
+ var _a;
1021
+ return !((_a = item == null ? void 0 : item.metadata) == null ? void 0 : _a.custom_product_bundle_map_id);
1022
+ }
1023
+ );
863
1024
  if (mainDiscountList && mainDiscountList.length > 0) {
864
1025
  const allDiscountAmount = (0, import_utils.getDiscountListAmountTotal)(mainDiscountList);
865
1026
  newTotalWithDiscount = new import_decimal.default(mainProductData.price || 0).minus(allDiscountAmount).toNumber() ?? newTotalWithDiscount;
@@ -895,14 +1056,18 @@ var RulesModule = class extends import_BaseModule.BaseModule {
895
1056
  );
896
1057
  if (mainProductQuantity > 1) {
897
1058
  const newBundleOriginal = [];
898
- (product.bundle || []).forEach((bundleItem, bundleIndex) => {
899
- newBundleOriginal.push({
900
- ...bundleItem,
901
- discount_list: []
902
- });
903
- });
1059
+ (product.bundle || []).forEach(
1060
+ (bundleItem, bundleIndex) => {
1061
+ newBundleOriginal.push({
1062
+ ...bundleItem,
1063
+ discount_list: []
1064
+ });
1065
+ }
1066
+ );
904
1067
  let newTotalOriginal = Number(mainProductData.price || 0);
905
- let newOriginTotalOriginal = Number(mainProductData.original_price || mainProductData.price || 0);
1068
+ let newOriginTotalOriginal = Number(
1069
+ mainProductData.original_price || mainProductData.price || 0
1070
+ );
906
1071
  const updatedMainDiscountListOriginal = mainProductData.discount_list.map((discount) => {
907
1072
  var _a, _b;
908
1073
  if ((_a = discount == null ? void 0 : discount.metadata) == null ? void 0 : _a.custom_product_bundle_map_id) {
@@ -917,12 +1082,16 @@ var RulesModule = class extends import_BaseModule.BaseModule {
917
1082
  // num: mainProductQuantity - 1,
918
1083
  };
919
1084
  });
920
- const mainDiscountListOriginal = updatedMainDiscountListOriginal.filter((item) => {
921
- var _a;
922
- return !((_a = item == null ? void 0 : item.metadata) == null ? void 0 : _a.custom_product_bundle_map_id);
923
- });
1085
+ const mainDiscountListOriginal = updatedMainDiscountListOriginal.filter(
1086
+ (item) => {
1087
+ var _a;
1088
+ return !((_a = item == null ? void 0 : item.metadata) == null ? void 0 : _a.custom_product_bundle_map_id);
1089
+ }
1090
+ );
924
1091
  if (mainDiscountListOriginal && mainDiscountListOriginal.length > 0) {
925
- const allDiscountAmount = (0, import_utils.getDiscountListAmount)(mainDiscountListOriginal);
1092
+ const allDiscountAmount = (0, import_utils.getDiscountListAmount)(
1093
+ mainDiscountListOriginal
1094
+ );
926
1095
  newTotalOriginal = new import_decimal.default(mainProductData.price || 0).minus(allDiscountAmount).toNumber() ?? newTotalOriginal;
927
1096
  newOriginTotalOriginal = mainProductData.origin_total ?? newOriginTotalOriginal;
928
1097
  }
@@ -957,47 +1126,56 @@ var RulesModule = class extends import_BaseModule.BaseModule {
957
1126
  const mainProductData = this.hooks.getProduct(mainProduct);
958
1127
  const newBundle = [];
959
1128
  if (product.bundle && Array.isArray(product.bundle)) {
960
- product.bundle.forEach((bundleItem, bundleIndex) => {
961
- const bundleItemId = `${product._id}_bundle_${bundleIndex}`;
962
- const processedBundleItems = processedFlatItemsMap2.get(bundleItemId);
963
- if (!processedBundleItems || processedBundleItems.length === 0) {
964
- newBundle.push(bundleItem);
965
- } else {
966
- processedBundleItems.forEach((item) => {
967
- const updatedDiscountList2 = (item.discount_list || []).map((discount) => {
968
- var _a2;
969
- return {
970
- ...discount,
971
- metadata: {
972
- num: item.num,
973
- custom_product_bundle_map_id: (_a2 = discount == null ? void 0 : discount.metadata) == null ? void 0 : _a2.custom_product_bundle_map_id
974
- }
975
- // num: item.num, // 使用拆分后的 num
976
- };
1129
+ product.bundle.forEach(
1130
+ (bundleItem, bundleIndex) => {
1131
+ const bundleItemId = `${product._id}_bundle_${bundleIndex}`;
1132
+ const processedBundleItems = processedFlatItemsMap2.get(bundleItemId);
1133
+ if (!processedBundleItems || processedBundleItems.length === 0) {
1134
+ newBundle.push(bundleItem);
1135
+ } else {
1136
+ processedBundleItems.forEach((item) => {
1137
+ const updatedDiscountList2 = (item.discount_list || []).map((discount) => {
1138
+ var _a2;
1139
+ return {
1140
+ ...discount,
1141
+ metadata: {
1142
+ num: item.num,
1143
+ custom_product_bundle_map_id: (_a2 = discount == null ? void 0 : discount.metadata) == null ? void 0 : _a2.custom_product_bundle_map_id
1144
+ }
1145
+ // num: item.num, // 使用拆分后的 num
1146
+ };
1147
+ });
1148
+ newBundle.push({
1149
+ ...bundleItem,
1150
+ _id: item._id,
1151
+ product_id: bundleItem.product_id,
1152
+ price: item.price,
1153
+ num: item.num,
1154
+ discount_list: updatedDiscountList2
1155
+ });
977
1156
  });
978
- newBundle.push({
979
- ...bundleItem,
980
- _id: item._id,
981
- product_id: bundleItem.product_id,
982
- price: item.price,
983
- num: item.num,
984
- discount_list: updatedDiscountList2
985
- });
986
- });
1157
+ }
987
1158
  }
988
- });
1159
+ );
989
1160
  }
990
1161
  let newTotal = Number(mainProductData.price || 0);
991
- let newOriginTotal = Number(mainProductData.original_price || mainProductData.price || 0);
992
- const isManualDiscount = typeof mainProductData.isManualDiscount === "boolean" ? mainProductData.isManualDiscount : mainProductData.total != mainProductData.origin_total && (!((_a = mainProductData.discount_list) == null ? void 0 : _a.length) || ((_c = (_b = mainProductData == null ? void 0 : mainProductData.discount_list) == null ? void 0 : _b.every) == null ? void 0 : _c.call(_b, (item) => item.type === "product")));
1162
+ let newOriginTotal = Number(
1163
+ mainProductData.original_price || mainProductData.price || 0
1164
+ );
1165
+ const isManualDiscount = typeof mainProductData.isManualDiscount === "boolean" ? mainProductData.isManualDiscount : mainProductData.total != mainProductData.origin_total && (!((_a = mainProductData.discount_list) == null ? void 0 : _a.length) || ((_c = (_b = mainProductData == null ? void 0 : mainProductData.discount_list) == null ? void 0 : _b.every) == null ? void 0 : _c.call(
1166
+ _b,
1167
+ (item) => item.type === "product"
1168
+ )));
993
1169
  if (isManualDiscount) {
994
1170
  newTotal = mainProductData.total ?? newTotal;
995
1171
  newOriginTotal = mainProductData.origin_total ?? newOriginTotal;
996
1172
  } else {
997
- const mainDiscountList = mainProductData.discount_list.filter((item) => {
998
- var _a2;
999
- return !((_a2 = item == null ? void 0 : item.metadata) == null ? void 0 : _a2.custom_product_bundle_map_id);
1000
- });
1173
+ const mainDiscountList = mainProductData.discount_list.filter(
1174
+ (item) => {
1175
+ var _a2;
1176
+ return !((_a2 = item == null ? void 0 : item.metadata) == null ? void 0 : _a2.custom_product_bundle_map_id);
1177
+ }
1178
+ );
1001
1179
  if (mainDiscountList && mainDiscountList.length > 0) {
1002
1180
  const allDiscountAmount = (0, import_utils.getDiscountListAmount)(mainDiscountList);
1003
1181
  newTotal = new import_decimal.default(mainProductData.price || 0).minus(allDiscountAmount).toNumber() ?? newTotal;