@pisell/pisellos 2.1.92 → 2.1.93

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 +497 -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 +165 -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 +16 -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 +9 -5
  37. package/dist/modules/Rules/index.js +539 -125
  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 +23 -7
  48. package/dist/solution/ShopDiscount/types.d.ts +1 -0
  49. package/dist/solution/ShopDiscount/types.js +2 -1
  50. package/dist/solution/ShopDiscount/utils.js +26 -12
  51. package/lib/index.d.ts +1 -0
  52. package/lib/index.js +3 -1
  53. package/lib/model/index.d.ts +1 -0
  54. package/lib/model/index.js +23 -0
  55. package/lib/model/strategy/adapter/index.d.ts +3 -0
  56. package/lib/model/strategy/adapter/index.js +45 -0
  57. package/lib/model/strategy/adapter/type.d.ts +28 -0
  58. package/lib/model/strategy/adapter/type.js +17 -0
  59. package/lib/model/strategy/adapter/walletPass/evaluator.d.ts +84 -0
  60. package/lib/model/strategy/adapter/walletPass/evaluator.js +421 -0
  61. package/lib/model/strategy/adapter/walletPass/example.d.ts +4 -0
  62. package/lib/model/strategy/adapter/walletPass/example.js +207 -0
  63. package/lib/model/strategy/adapter/walletPass/index.d.ts +32 -0
  64. package/lib/model/strategy/adapter/walletPass/index.js +142 -0
  65. package/lib/model/strategy/adapter/walletPass/locales.d.ts +1 -0
  66. package/lib/model/strategy/adapter/walletPass/locales.js +51 -0
  67. package/lib/model/strategy/adapter/walletPass/type.d.ts +165 -0
  68. package/lib/model/strategy/adapter/walletPass/type.js +17 -0
  69. package/lib/model/strategy/adapter/walletPass/utils.d.ts +50 -0
  70. package/lib/model/strategy/adapter/walletPass/utils.js +660 -0
  71. package/lib/model/strategy/index.d.ts +94 -0
  72. package/lib/model/strategy/index.js +413 -0
  73. package/lib/model/strategy/strategy-example.d.ts +5 -0
  74. package/lib/model/strategy/strategy-example.js +318 -0
  75. package/lib/model/strategy/type.d.ts +228 -0
  76. package/lib/model/strategy/type.js +44 -0
  77. package/lib/modules/Cart/types.d.ts +2 -0
  78. package/lib/modules/Cart/utils/cartProduct.js +9 -0
  79. package/lib/modules/Discount/types.d.ts +16 -0
  80. package/lib/modules/Payment/index.d.ts +2 -1
  81. package/lib/modules/Payment/index.js +1 -0
  82. package/lib/modules/Payment/utils.js +3 -0
  83. package/lib/modules/Payment/walletpass.d.ts +23 -0
  84. package/lib/modules/Payment/walletpass.js +94 -17
  85. package/lib/modules/Product/index.d.ts +1 -1
  86. package/lib/modules/Rules/index.d.ts +9 -5
  87. package/lib/modules/Rules/index.js +665 -259
  88. package/lib/modules/Rules/types.d.ts +1 -0
  89. package/lib/modules/Summary/types.d.ts +2 -0
  90. package/lib/modules/Summary/utils.d.ts +6 -0
  91. package/lib/modules/Summary/utils.js +15 -0
  92. package/lib/plugins/window.d.ts +2 -0
  93. package/lib/solution/BookingByStep/index.d.ts +1 -0
  94. package/lib/solution/BookingTicket/index.d.ts +1 -1
  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 +22 -8
  98. package/lib/solution/ShopDiscount/types.d.ts +1 -0
  99. package/lib/solution/ShopDiscount/utils.js +10 -6
  100. package/package.json +1 -1
@@ -31,7 +31,6 @@ import { getProductOriginTotalPrice, getProductTotalPrice } from "../Cart/utils"
31
31
  import Decimal from 'decimal.js';
32
32
  import { isBoolean } from 'lodash-es';
33
33
  import dayjs from 'dayjs';
34
-
35
34
  // 临时变量
36
35
  var flatItem;
37
36
  export var RulesModule = /*#__PURE__*/function (_BaseModule) {
@@ -44,6 +43,8 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
44
43
  _defineProperty(_assertThisInitialized(_this), "defaultName", 'rules');
45
44
  _defineProperty(_assertThisInitialized(_this), "defaultVersion", '1.0.0');
46
45
  _defineProperty(_assertThisInitialized(_this), "store", void 0);
46
+ _defineProperty(_assertThisInitialized(_this), "window", void 0);
47
+ // WindowPlugin 实例
47
48
  _defineProperty(_assertThisInitialized(_this), "hooks", void 0);
48
49
  _this.hooks = {};
49
50
  return _this;
@@ -58,7 +59,8 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
58
59
  this.core = core;
59
60
  this.hooks = options === null || options === void 0 ? void 0 : options.hooks;
60
61
  this.store = options === null || options === void 0 ? void 0 : options.store;
61
- case 3:
62
+ this.window = core.getPlugin('window');
63
+ case 4:
62
64
  case "end":
63
65
  return _context.stop();
64
66
  }
@@ -113,6 +115,12 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
113
115
  // 最终直接匹配 holder 是否相同
114
116
  return (productHolderId || orderHolderId) === discount.holder.holder_id;
115
117
  }
118
+ }, {
119
+ key: "getWalletPassEvaluator",
120
+ value: function getWalletPassEvaluator() {
121
+ var _this$window$getWalle, _this$window;
122
+ return (_this$window$getWalle = (_this$window = this.window).getWalletPassEvaluator) === null || _this$window$getWalle === void 0 ? void 0 : _this$window$getWalle.call(_this$window);
123
+ }
116
124
 
117
125
  // 判断discountList 是否可以对当前productList生效
118
126
  }, {
@@ -122,6 +130,7 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
122
130
  var oldDiscountList = _ref.oldDiscountList,
123
131
  newDiscountList = _ref.newDiscountList,
124
132
  productList = _ref.productList,
133
+ orderTotalAmount = _ref.orderTotalAmount,
125
134
  holders = _ref.holders,
126
135
  isFormSubject = _ref.isFormSubject;
127
136
  // 首先检查是否有新的优惠券可应用
@@ -156,6 +165,7 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
156
165
  var result = this.calcDiscount({
157
166
  discountList: mergedDiscountList,
158
167
  productList: _toConsumableArray(productList),
168
+ orderTotalAmount: orderTotalAmount,
159
169
  holders: holders,
160
170
  isFormSubject: isFormSubject
161
171
  }, {
@@ -282,7 +292,15 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
282
292
  var discountList = _ref2.discountList,
283
293
  productList = _ref2.productList,
284
294
  holders = _ref2.holders,
285
- isFormSubject = _ref2.isFormSubject;
295
+ isFormSubject = _ref2.isFormSubject,
296
+ orderTotalAmount = _ref2.orderTotalAmount;
297
+ // 识别出来是不是在编辑的界面里又新增了商品
298
+ // 这种情况下,如果有可用的优惠券,也会自动勾选上
299
+ var isEditModeAddNewProduct = productList.find(function (n) {
300
+ return n.booking_id;
301
+ }) && productList.find(function (n) {
302
+ return !n.booking_id;
303
+ });
286
304
  var editModeDiscount = [];
287
305
  var addModeDiscount = [];
288
306
  discountList.forEach(function (discount) {
@@ -496,14 +514,6 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
496
514
  // 🔥 扁平化商品列表(包含主商品和bundle子商品)
497
515
  var flattenedList = flattenProductsWithBundle(productList);
498
516
 
499
- // 识别出来是不是在编辑的界面里又新增了商品
500
- // 这种情况下,如果有可用的优惠券,也会自动勾选上
501
- var isEditModeAddNewProduct = flattenedList.find(function (n) {
502
- return n.booking_id;
503
- }) && flattenedList.find(function (n) {
504
- return !n.booking_id;
505
- });
506
-
507
517
  // 对扁平化后的列表按价格降序排序(用于应用优惠券时优先选择高价商品)
508
518
  var sortedFlattenedList = flattenedList.sort(function (a, b) {
509
519
  var _ref3, _a$original_price, _ref4, _b$original_price;
@@ -531,6 +541,57 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
531
541
  return priceB.minus(priceA).toNumber();
532
542
  });
533
543
 
544
+ // 🔥 为每个折扣卡/商品券执行策略检查并附加config
545
+ var evaluator = this.getWalletPassEvaluator();
546
+ if (evaluator) {
547
+ addModeDiscount.forEach(function (discount) {
548
+ var discountType = discount.tag || discount.type;
549
+ // 只对 good_pass 和 discount_card 类型执行策略检查
550
+ if (['good_pass', 'discount_card', 'product_discount_card'].includes(discountType)) {
551
+ var _discount$limited_rel, _discount$limited_rel2;
552
+ // 转换 discount 为 Voucher 格式
553
+ var voucher = {
554
+ id: discount.id,
555
+ amount: Number(discount.par_value || 0),
556
+ balance: Number(discount.balance || 0),
557
+ type: discountType,
558
+ product_id: discount.product_id,
559
+ unified_available_status: 1,
560
+ available_product_type: (_discount$limited_rel = discount.limited_relation_product_data) === null || _discount$limited_rel === void 0 ? void 0 : _discount$limited_rel.type,
561
+ available_product_ids: (_discount$limited_rel2 = discount.limited_relation_product_data) === null || _discount$limited_rel2 === void 0 ? void 0 : _discount$limited_rel2.product_ids
562
+ };
563
+
564
+ // 转换 sortedFlattenedList 为 Product 格式
565
+ var productsForEvaluate = sortedFlattenedList.map(function (item) {
566
+ return {
567
+ product_id: item.id,
568
+ price: item.price || 0,
569
+ quantity: item.quantity || item.num || 1,
570
+ selling_price: item.price || 0
571
+ };
572
+ });
573
+
574
+ // 调用策略检查
575
+ var result = evaluator.checkVoucherAvailability({
576
+ orderTotalAmount: orderTotalAmount,
577
+ products: productsForEvaluate,
578
+ vouchers: [voucher]
579
+ });
580
+
581
+ // 将 config 附加到 discount 对象
582
+ if (result.isAvailable) {
583
+ discount.config = _objectSpread(_objectSpread({}, result.config), {}, {
584
+ isAvailable: true
585
+ });
586
+ } else {
587
+ discount.config = {
588
+ isAvailable: false
589
+ };
590
+ }
591
+ }
592
+ });
593
+ }
594
+
534
595
  /**
535
596
  // 对productList按价格降序排序(用于应用优惠券时优先选择高价商品) 价格相同时使用quantity 排序
536
597
  const sortedProductList = [...productList].sort((a, b) => {
@@ -551,6 +612,22 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
551
612
  // 标记已使用的优惠券
552
613
  var usedDiscounts = new Map();
553
614
 
615
+ // 🔥 统计每个 product_id(商品券商品id)已使用的数量,用于 maxUsagePerOrder 限制
616
+ var usedProductIdCounts = new Map();
617
+
618
+ // 🔥 统计每张折扣卡的 applicableProductLimit 已使用次数(跨商品全局计数)
619
+ var usedDiscountCardLimitCounts = new Map();
620
+
621
+ // 🔥 预先将 editModeDiscount 中的折扣卡计入 applicableProductLimit 已使用次数
622
+ editModeDiscount.forEach(function (discount) {
623
+ var discountType = discount.tag || discount.type;
624
+ if (['discount_card', 'product_discount_card'].includes(discountType)) {
625
+ var _discount$metadata4;
626
+ var currentCount = usedDiscountCardLimitCounts.get(discount.id) || 0;
627
+ usedDiscountCardLimitCounts.set(discount.id, currentCount + (((_discount$metadata4 = discount.metadata) === null || _discount$metadata4 === void 0 ? void 0 : _discount$metadata4.num) || 1));
628
+ }
629
+ });
630
+
554
631
  // 记录每个优惠券适用的商品ID
555
632
  var discountApplicability = new Map();
556
633
 
@@ -780,7 +857,7 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
780
857
  originProduct = flatItem.originProduct;
781
858
  }
782
859
  addModeDiscount.forEach(function (discount) {
783
- var _product6, _product7, _product8, _product9, _flatItem$bundleItem3, _flatItem$bundleItem4;
860
+ var _product6, _product7, _product8, _product9, _flatItem$bundleItem3, _flatItem$bundleItem4, _discount$config;
784
861
  var limitedData = discount === null || discount === void 0 ? void 0 : discount.limited_relation_product_data;
785
862
  // 拿到discount配置的holder信息 product信息 product.holder 加在 isLimitedProduct
786
863
  var _tempVar = (flatItem === null || flatItem === void 0 ? void 0 : flatItem.type) === 'bundle' ? flatItem === null || flatItem === void 0 ? void 0 : flatItem.parentProduct : flatItem === null || flatItem === void 0 ? void 0 : flatItem.product;
@@ -791,7 +868,7 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
791
868
  var timeLimit = true;
792
869
  timeLimit = !!filterDiscountListByBookingTime([discount], (((_product6 = product) === null || _product6 === void 0 ? void 0 : _product6.startDate) || dayjs()).format('YYYY-MM-DD HH:mm:ss')).length;
793
870
  // 是符合折扣的商品
794
- var isLimitedProduct = (limitedData.type === 'product_all' || limitedData.product_ids && limitedData.product_ids.includes(product.id)) && isHolderMatch;
871
+ var 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;
795
872
 
796
873
  // 编辑的商品 使用了优惠券不可用
797
874
  var isAvailableProduct = flatItem.type === 'main' ? !((_product7 = product) !== null && _product7 !== void 0 && _product7.booking_id && (_product8 = product) !== null && _product8 !== void 0 && (_product8 = _product8.discount_list) !== null && _product8 !== void 0 && _product8.length && (_product9 = product) !== null && _product9 !== void 0 && (_product9 = _product9.discount_list) !== null && _product9 !== void 0 && _product9.every(function (discount) {
@@ -804,8 +881,8 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
804
881
  var isBundleAvailable = _this3.checkPackageSubItemUsageRules(discount, flatItem);
805
882
 
806
883
  // 判断优惠券是否适用于该商品
807
- if (isAvailableProduct && isLimitedProduct && timeLimit && isBundleAvailable) {
808
- var _discountApplicabilit, _discount$metadata4, _discount$metadata5;
884
+ if (isAvailableProduct && isLimitedProduct && timeLimit && isBundleAvailable && (_discount$config = discount.config) !== null && _discount$config !== void 0 && _discount$config.isAvailable) {
885
+ var _discountApplicabilit, _discount$metadata5, _discount$metadata6;
809
886
  // 记录此优惠券适用的商品
810
887
  (_discountApplicabilit = discountApplicability.get(discount.id)) === null || _discountApplicabilit === void 0 || _discountApplicabilit.push(product.id);
811
888
 
@@ -821,14 +898,15 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
821
898
  type: discountType,
822
899
  tag: discountType,
823
900
  discount: {
824
- discount_card_type: discount === null || discount === void 0 || (_discount$metadata4 = discount.metadata) === null || _discount$metadata4 === void 0 ? void 0 : _discount$metadata4.discount_card_type,
901
+ discount_card_type: discount === null || discount === void 0 || (_discount$metadata5 = discount.metadata) === null || _discount$metadata5 === void 0 ? void 0 : _discount$metadata5.discount_card_type,
825
902
  fixed_amount: product.price,
826
- discount_calculation_mode: discount === null || discount === void 0 || (_discount$metadata5 = discount.metadata) === null || _discount$metadata5 === void 0 ? void 0 : _discount$metadata5.discount_calculation_mode,
903
+ discount_calculation_mode: discount === null || discount === void 0 || (_discount$metadata6 = discount.metadata) === null || _discount$metadata6 === void 0 ? void 0 : _discount$metadata6.discount_calculation_mode,
827
904
  resource_id: discount.id,
828
905
  title: discount.format_title,
829
906
  original_amount: product.price || product.origin_total,
830
907
  pre_value: discount.par_value,
831
- product_id: originProduct.id
908
+ product_id: originProduct.id,
909
+ discount_product_id: discount.product_id
832
910
  },
833
911
  metadata: {
834
912
  num: num,
@@ -847,7 +925,7 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
847
925
  // 然后再处理应用哪些优惠券,此时只考虑filteredDiscountList中的优惠券
848
926
  // 🔥 使用扁平化后的列表进行处理
849
927
  sortedFlattenedList.forEach(function (flatItem, index) {
850
- var _product10, _product$discount_lis2, _product11;
928
+ var _product10, _product$discount_lis2, _product11, _selectedDiscount$con;
851
929
  // 获取商品数据
852
930
  var product, originProduct;
853
931
  if (flatItem.type === 'main') {
@@ -889,6 +967,16 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
889
967
  // 找到适用于此商品的所有优惠券,仅考虑isSelected不为false的优惠券
890
968
  var applicableDiscounts = sortedDiscountList.filter(function (discount) {
891
969
  var _product$discount_lis3, _product$discount_lis4;
970
+ // 🔥 检查策略可用性(针对 good_pass 和折扣卡)
971
+ var discountType = discount.tag || discount.type;
972
+ if (['good_pass', 'discount_card', 'product_discount_card'].includes(discountType)) {
973
+ var _discount$config2;
974
+ // 如果策略检查后没有config,说明不可用
975
+ if (discount.config === undefined || !(discount !== null && discount !== void 0 && (_discount$config2 = discount.config) !== null && _discount$config2 !== void 0 && _discount$config2.isAvailable)) {
976
+ return false;
977
+ }
978
+ }
979
+
892
980
  // 如果商品价格为 0,其实不需要使用任何优惠券,直接 return true
893
981
  // 商品券时主商品价格为0不可用
894
982
  if ((Number(product.price) <= 0 || !product.price) && !((_product$discount_lis3 = product.discount_list) !== null && _product$discount_lis3 !== void 0 && _product$discount_lis3.length) && (discount.tag || discount.type) === 'good_pass') return false;
@@ -901,8 +989,37 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
901
989
  // 如果优惠券已被使用,则跳过
902
990
  var targetUsedDiscounts = usedDiscounts.get(discount.id);
903
991
  if (targetUsedDiscounts && (discount.tag || discount.type) === 'good_pass') return false;
904
- var limitedData = discount.limited_relation_product_data;
905
992
 
993
+ // 🔥 检查 maxUsagePerOrder 限制:同一 product_id 的商品券使用数量不能超过配置值
994
+ if ((discount.tag || discount.type) === 'good_pass') {
995
+ var _discount$config3;
996
+ var maxUsagePerOrder = (_discount$config3 = discount.config) === null || _discount$config3 === void 0 ? void 0 : _discount$config3.maxUsagePerOrder;
997
+ if (maxUsagePerOrder && maxUsagePerOrder > 0) {
998
+ var currentUsedCount = usedProductIdCounts.get(discount.product_id) || 0;
999
+ if (currentUsedCount >= maxUsagePerOrder) {
1000
+ return false;
1001
+ }
1002
+ }
1003
+ }
1004
+
1005
+ // 🔥 检查折扣卡的 applicableProductLimit 限制:折扣卡可使用的最大次数(跨商品全局)
1006
+ var discountTypeForLimit = discount.tag || discount.type;
1007
+ if (['discount_card', 'product_discount_card'].includes(discountTypeForLimit)) {
1008
+ var _discount$config4;
1009
+ var applicableProductLimitConfig = ((_discount$config4 = discount.config) === null || _discount$config4 === void 0 ? void 0 : _discount$config4.applicableProductLimit) || 0;
1010
+ if (applicableProductLimitConfig > 0) {
1011
+ var currentUsedLimitCount = usedDiscountCardLimitCounts.get(discount.id) || 0;
1012
+ if (currentUsedLimitCount >= applicableProductLimitConfig) {
1013
+ return false;
1014
+ }
1015
+ }
1016
+ }
1017
+ var limitedData = discount.limited_relation_product_data;
1018
+ var timeLimit = true;
1019
+ timeLimit = !!filterDiscountListByBookingTime([discount], (product.startDate || dayjs()).format('YYYY-MM-DD HH:mm:ss')).length;
1020
+ if (!timeLimit) {
1021
+ return false;
1022
+ }
906
1023
  // 拿到discount配置的holder信息 product信息 product.holder 不可用return false
907
1024
  var _tempVar = (flatItem === null || flatItem === void 0 ? void 0 : flatItem.type) === 'bundle' ? flatItem === null || flatItem === void 0 ? void 0 : flatItem.parentProduct : flatItem === null || flatItem === void 0 ? void 0 : flatItem.product;
908
1025
  var isHolderMatch = _this3.checkHolderMatch(discount, {
@@ -911,14 +1028,12 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
911
1028
  }, holders);
912
1029
  // 如果 holder 不匹配,则不适用
913
1030
  if (!isHolderMatch) return false;
914
- var timeLimit = true;
915
- timeLimit = !!filterDiscountListByBookingTime([discount], (product.startDate || dayjs()).format('YYYY-MM-DD HH:mm:ss')).length;
916
- if (!timeLimit) {
917
- return false;
918
- }
919
1031
 
920
1032
  // 判断优惠券是否适用于该商品
921
1033
  if (limitedData.type === 'product_all') {
1034
+ if (limitedData.filter === 1 && limitedData.exclude_product_ids.includes(product.id)) {
1035
+ return false;
1036
+ }
922
1037
  // 检查 package_sub_item_usage_rules
923
1038
  if (!_this3.checkPackageSubItemUsageRules(discount, flatItem)) {
924
1039
  return false;
@@ -1077,15 +1192,39 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
1077
1192
  return;
1078
1193
  }
1079
1194
 
1080
- // 是否需要拆分(商品券需要拆分)
1081
- var isNeedSplit = (selectedDiscount.tag || selectedDiscount.type) === 'good_pass';
1195
+ // 是否需要拆分(商品券需要拆分,折扣卡在 applicableProductLimit > 0 时也需要拆分)
1196
+ var discountType = selectedDiscount.tag || selectedDiscount.type;
1197
+ var isGoodPass = discountType === 'good_pass';
1198
+ var isDiscountCard = ['discount_card', 'product_discount_card'].includes(discountType);
1199
+ var applicableProductLimit = ((_selectedDiscount$con = selectedDiscount.config) === null || _selectedDiscount$con === void 0 ? void 0 : _selectedDiscount$con.applicableProductLimit) || 0;
1200
+
1201
+ // 商品券始终需要拆分,折扣卡在 applicableProductLimit > 0 时需要拆分
1202
+ var isNeedSplit = isGoodPass || isDiscountCard && applicableProductLimit > 0;
1082
1203
 
1083
1204
  // 需要拆分出来的数量
1084
1205
  var totalQuantity = product.quantity || product.num || 1;
1085
1206
  var availableGoodPassCount = applicableDiscounts.filter(function (item) {
1086
1207
  return (item.tag || item.type) === 'good_pass';
1087
1208
  }).length;
1088
- var splitCount = isNeedSplit ? Math.min(totalQuantity, availableGoodPassCount) : 1;
1209
+
1210
+ // 🔥 计算最大使用次数限制
1211
+ var maxUsageLimit;
1212
+ if (isGoodPass) {
1213
+ // 商品券:使用 maxUsagePerOrder 限制
1214
+ maxUsageLimit = availableGoodPassCount;
1215
+ if (selectedDiscount.config && selectedDiscount.config.maxUsagePerOrder) {
1216
+ maxUsageLimit = Math.min(availableGoodPassCount, selectedDiscount.config.maxUsagePerOrder);
1217
+ }
1218
+ } else if (isDiscountCard && applicableProductLimit > 0) {
1219
+ // 折扣卡:使用 applicableProductLimit 限制(一个商品使用一次折扣卡算1次)
1220
+ // 需要减去已使用次数(跨商品全局计数)
1221
+ var usedLimitCount = usedDiscountCardLimitCounts.get(selectedDiscount.id) || 0;
1222
+ maxUsageLimit = Math.max(0, applicableProductLimit - usedLimitCount);
1223
+ } else {
1224
+ // 其他情况不拆分
1225
+ maxUsageLimit = 1;
1226
+ }
1227
+ var splitCount = isNeedSplit ? Math.min(product.quantity || product.num || 1, maxUsageLimit) : 1;
1089
1228
  var arr = [];
1090
1229
 
1091
1230
  // 🔥 主商品和bundle子商品分别处理
@@ -1095,18 +1234,31 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
1095
1234
  arr.push(_this3.hooks.setProduct(originProduct, {
1096
1235
  discount_list: [],
1097
1236
  quantity: totalQuantity - splitCount,
1098
- _id: product._id.split('___')[0]
1237
+ _id: product._id.split('___')[0],
1238
+ total: product.origin_total || product.total
1099
1239
  }));
1100
1240
  }
1101
1241
  for (var i = 0; i < splitCount; i++) {
1102
- var _product$discount_lis10, _originProduct, _selectedDiscount$met, _selectedDiscount$met2;
1103
- // 如果用过折扣卡,也就不存在拆分的情况了,这里直接使用上面计算出来的折扣卡
1104
- var _selectedDiscount = selectedDiscountCard || applicableDiscounts[i];
1242
+ var _product$discount_lis10, _originProduct, _currentSelectedDisco, _currentSelectedDisco2;
1243
+ // 商品券:每个拆分商品使用不同的商品券(从 applicableDiscounts 按索引取)
1244
+ // 折扣卡:所有拆分商品使用同一张折扣卡(selectedDiscountCard applicableDiscounts[0]
1245
+ var currentSelectedDiscount = isGoodPass ? selectedDiscountCard || applicableDiscounts[i] : selectedDiscountCard || applicableDiscounts[0];
1105
1246
  // 标记优惠券为已使用
1106
- usedDiscounts.set(_selectedDiscount.id, true);
1247
+ usedDiscounts.set(currentSelectedDiscount.id, true);
1248
+ // 🔥 更新 product_id 使用计数
1249
+ if ((currentSelectedDiscount.tag || currentSelectedDiscount.type) === 'good_pass') {
1250
+ var currentCount = usedProductIdCounts.get(currentSelectedDiscount.product_id) || 0;
1251
+ usedProductIdCounts.set(currentSelectedDiscount.product_id, currentCount + 1);
1252
+ }
1253
+ // 🔥 更新折扣卡 applicableProductLimit 使用计数(跨商品全局)
1254
+ var currentDiscountTypeForCount = currentSelectedDiscount.tag || currentSelectedDiscount.type;
1255
+ if (['discount_card', 'product_discount_card'].includes(currentDiscountTypeForCount)) {
1256
+ var currentLimitCount = usedDiscountCardLimitCounts.get(currentSelectedDiscount.id) || 0;
1257
+ usedDiscountCardLimitCounts.set(currentSelectedDiscount.id, currentLimitCount + 1);
1258
+ }
1107
1259
 
1108
1260
  // 记录实际应用了优惠券的商品信息
1109
- var appliedProducts = appliedDiscountProducts.get(_selectedDiscount.id) || [];
1261
+ var appliedProducts = appliedDiscountProducts.get(currentSelectedDiscount.id) || [];
1110
1262
 
1111
1263
  // 优先从 origin_total拿,可能会拿不到(比如用户端预约在没有配置 original_price 的情况下)
1112
1264
  var productOriginTotal = product.origin_total || product.total || 0;
@@ -1121,8 +1273,8 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
1121
1273
 
1122
1274
  // 计算使用折扣卡/商品券以后,单个商品的总 total
1123
1275
  // 🔥 检查是否是 order_level 固定金额折扣卡
1124
- var isOrderLevel = isOrderLevelFixedAmountDiscount(_selectedDiscount);
1125
- var orderLevelAllocation = isOrderLevel ? orderLevelDiscountAllocations.get(_selectedDiscount.id) : null;
1276
+ var isOrderLevel = isOrderLevelFixedAmountDiscount(selectedDiscount);
1277
+ var orderLevelAllocation = isOrderLevel ? orderLevelDiscountAllocations.get(selectedDiscount.id) : null;
1126
1278
  var productAllocation = orderLevelAllocation === null || orderLevelAllocation === void 0 ? void 0 : orderLevelAllocation.get(flatItem._id);
1127
1279
  var targetProductTotal = void 0;
1128
1280
  var amount = void 0;
@@ -1134,35 +1286,40 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
1134
1286
  targetProductTotal = Math.max(new Decimal(product.price).minus(amount).toNumber(), 0);
1135
1287
  } else {
1136
1288
  // item_level 或其他类型:使用原有逻辑
1137
- targetProductTotal = getDiscountAmount(_selectedDiscount, product.price, product.price);
1289
+ targetProductTotal = getDiscountAmount(currentSelectedDiscount, product.price, product.price);
1138
1290
  amount = new Decimal(product.price).minus(new Decimal(targetProductTotal)).toNumber();
1139
1291
  }
1140
- var discountType = _selectedDiscount.tag || _selectedDiscount.type;
1141
- var isGoodPass = discountType === 'good_pass';
1292
+ var currentDiscountType = currentSelectedDiscount.tag === 'product_discount_card' ? 'discount_card' : currentSelectedDiscount.tag;
1293
+
1294
+ // 计算实际折扣金额
1295
+ var actualDiscountAmount = new Decimal(product.price).minus(new Decimal(targetProductTotal)).toNumber();
1296
+ var currentIsGoodPass = currentDiscountType === 'good_pass';
1142
1297
  var discountDetail = {
1143
- amount: amount,
1144
- type: _selectedDiscount.tag === 'product_discount_card' ? 'discount_card' : discountType,
1298
+ amount: actualDiscountAmount,
1299
+ type: currentDiscountType,
1145
1300
  discount: {
1146
- discount_card_type: _selectedDiscount === null || _selectedDiscount === void 0 || (_selectedDiscount$met = _selectedDiscount.metadata) === null || _selectedDiscount$met === void 0 ? void 0 : _selectedDiscount$met.discount_card_type,
1147
- fixed_amount: amount,
1148
- discount_calculation_mode: _selectedDiscount === null || _selectedDiscount === void 0 || (_selectedDiscount$met2 = _selectedDiscount.metadata) === null || _selectedDiscount$met2 === void 0 ? void 0 : _selectedDiscount$met2.discount_calculation_mode,
1149
- resource_id: _selectedDiscount.id,
1150
- title: _selectedDiscount.format_title,
1301
+ discount_card_type: currentSelectedDiscount === null || currentSelectedDiscount === void 0 || (_currentSelectedDisco = currentSelectedDiscount.metadata) === null || _currentSelectedDisco === void 0 ? void 0 : _currentSelectedDisco.discount_card_type,
1302
+ fixed_amount: actualDiscountAmount,
1303
+ discount_calculation_mode: currentSelectedDiscount === null || currentSelectedDiscount === void 0 || (_currentSelectedDisco2 = currentSelectedDiscount.metadata) === null || _currentSelectedDisco2 === void 0 ? void 0 : _currentSelectedDisco2.discount_calculation_mode,
1304
+ resource_id: currentSelectedDiscount.id,
1305
+ title: currentSelectedDiscount.format_title,
1151
1306
  original_amount: product.price,
1152
1307
  product_id: originProduct.id,
1153
- percent: _selectedDiscount.par_value
1308
+ percent: currentSelectedDiscount.par_value,
1309
+ discount_product_id: currentSelectedDiscount.product_id
1154
1310
  },
1155
- // 前端使用的num数量,为了计算优惠金额
1156
- _num: isGoodPass ? 1 : product.num,
1311
+ // 前端使用的num数量,为了计算优惠金额(拆分时为1)
1312
+ _num: isNeedSplit ? 1 : product.num,
1313
+ config: currentSelectedDiscount === null || currentSelectedDiscount === void 0 ? void 0 : currentSelectedDiscount.config,
1157
1314
  metadata: _objectSpread({
1158
1315
  num: 1,
1159
- discount_rule_uncheck_flag: _selectedDiscount === null || _selectedDiscount === void 0 ? void 0 : _selectedDiscount.discount_rule_uncheck_flag
1316
+ discount_rule_uncheck_flag: selectedDiscount === null || selectedDiscount === void 0 ? void 0 : selectedDiscount.discount_rule_uncheck_flag
1160
1317
  }, productDiscountDifference !== undefined && {
1161
1318
  product_discount_difference: productDiscountDifference
1162
1319
  })
1163
1320
  };
1164
1321
  appliedProducts.push(discountDetail);
1165
- appliedDiscountProducts.set(_selectedDiscount.id, appliedProducts);
1322
+ appliedDiscountProducts.set(currentSelectedDiscount.id, appliedProducts);
1166
1323
  var total = targetProductTotal;
1167
1324
  if (product.options) {
1168
1325
  total = product.options.reduce(function (accumulator, currentValue) {
@@ -1175,10 +1332,9 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
1175
1332
  // 记录应用了优惠券的商品
1176
1333
  // 后续更新价格改为 getProductTotalPrice getProductOriginTotalPrice逻辑
1177
1334
  if (product.isClient) {
1178
- debugger;
1179
1335
  arr.push(_this3.hooks.setProduct(originProduct, {
1180
1336
  discount_list: [discountDetail],
1181
- price: _selectedDiscount.tag === 'good_pass' ? 0 : product.price,
1337
+ price: currentIsGoodPass ? product.price - discountDetail.amount : product.price,
1182
1338
  quantity: isNeedSplit ? 1 : product.quantity,
1183
1339
  origin_total: getProductOriginTotalPrice({
1184
1340
  product: {
@@ -1189,15 +1345,15 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
1189
1345
  }),
1190
1346
  variant: originProduct._productInit.variant,
1191
1347
  original_price: new Decimal(product.price || 0).toNumber(),
1192
- total: total
1348
+ total: targetProductTotal
1193
1349
  }));
1194
1350
  } else {
1195
1351
  arr.push(_this3.hooks.setProduct(originProduct, {
1196
1352
  discount_list: [discountDetail],
1197
- _id: product._id.split('___')[0] + "___" + _selectedDiscount.id + index,
1198
- price: _selectedDiscount.tag === 'good_pass' ? 0 : product.price,
1353
+ _id: product._id.split('___')[0] + '___' + currentSelectedDiscount.id + '_' + i + '_' + index,
1354
+ price: currentIsGoodPass ? 0 : product.price,
1199
1355
  quantity: isNeedSplit ? 1 : product.quantity,
1200
- total: total,
1356
+ total: targetProductTotal,
1201
1357
  origin_total: productOriginTotal,
1202
1358
  main_product_selling_price: targetProductTotal
1203
1359
  }));
@@ -1205,80 +1361,336 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
1205
1361
  }
1206
1362
  processedProductsMap.set(product._id, arr);
1207
1363
  } else {
1208
- // 🔥 bundle子商品:支持拆分
1364
+ // 🔥 bundle子商品:支持拆分(商品券和折扣卡在 applicableProductLimit > 0 时都需要拆分)
1209
1365
  var processedItems = [];
1210
1366
  if (isNeedSplit) {
1211
- // 商品券:需要拆分数量
1367
+ // 需要拆分数量(商品券或折扣卡有 applicableProductLimit 限制)
1212
1368
  var discountNum = splitCount;
1213
1369
  var normalNum = totalQuantity - discountNum;
1214
1370
 
1215
- // 生成有折扣的商品(每张商品券对应 num: 1)
1371
+ // 生成有折扣的商品(每个拆分商品 num: 1)
1216
1372
  for (var _i = 0; _i < discountNum; _i++) {
1217
- var _selectedDiscount2$me;
1218
- var _selectedDiscount2 = applicableDiscounts[_i];
1219
- usedDiscounts.set(_selectedDiscount2.id, true);
1373
+ var _currentBundleDiscoun, _currentBundleDiscoun2;
1374
+ // 商品券:每个拆分商品使用不同的商品券
1375
+ // 折扣卡:所有拆分商品使用同一张折扣卡
1376
+ var currentBundleDiscount = isGoodPass ? selectedDiscountCard || applicableDiscounts[_i] : selectedDiscountCard || applicableDiscounts[0];
1377
+ usedDiscounts.set(currentBundleDiscount.id, true);
1378
+ // 🔥 更新 product_id 使用计数
1379
+ if ((currentBundleDiscount.tag || currentBundleDiscount.type) === 'good_pass') {
1380
+ var _currentCount = usedProductIdCounts.get(currentBundleDiscount.product_id) || 0;
1381
+ usedProductIdCounts.set(currentBundleDiscount.product_id, _currentCount + 1);
1382
+ }
1383
+ // 🔥 更新折扣卡 applicableProductLimit 使用计数(跨商品全局)
1384
+ var bundleDiscountTypeForCount = currentBundleDiscount.tag || currentBundleDiscount.type;
1385
+ if (['discount_card', 'product_discount_card'].includes(bundleDiscountTypeForCount)) {
1386
+ var currentBundleLimitCount = usedDiscountCardLimitCounts.get(currentBundleDiscount.id) || 0;
1387
+ usedDiscountCardLimitCounts.set(currentBundleDiscount.id, currentBundleLimitCount + 1);
1388
+ }
1220
1389
 
1221
1390
  // 🔥 生成唯一的 _id
1222
1391
  var uniqueId = "".concat(flatItem._id, "_split_").concat(_i);
1392
+
1393
+ // 计算折扣后的价格(使用 getDiscountAmount,内部已处理 maxDeductionAmount)
1394
+ var discountedPrice = getDiscountAmount(currentBundleDiscount, product.origin_total, product.origin_total);
1395
+ var bundleDiscountAmount = new Decimal(product.origin_total || 0).minus(discountedPrice).toNumber();
1396
+
1397
+ // 确定折扣类型
1398
+ var bundleDiscountType = currentBundleDiscount.tag === 'product_discount_card' ? 'discount_card' : currentBundleDiscount.tag;
1223
1399
  var _discountDetail = {
1224
- amount: product.origin_total,
1225
- type: 'good_pass',
1400
+ amount: bundleDiscountAmount,
1401
+ type: bundleDiscountType,
1226
1402
  discount: {
1227
- fixed_amount: product.origin_total,
1228
- discount_calculation_mode: _selectedDiscount2 === null || _selectedDiscount2 === void 0 || (_selectedDiscount2$me = _selectedDiscount2.metadata) === null || _selectedDiscount2$me === void 0 ? void 0 : _selectedDiscount2$me.discount_calculation_mode,
1229
- resource_id: _selectedDiscount2.id,
1230
- title: _selectedDiscount2.format_title,
1403
+ discount_card_type: currentBundleDiscount === null || currentBundleDiscount === void 0 || (_currentBundleDiscoun = currentBundleDiscount.metadata) === null || _currentBundleDiscoun === void 0 ? void 0 : _currentBundleDiscoun.discount_card_type,
1404
+ discount_calculation_mode: currentBundleDiscount === null || currentBundleDiscount === void 0 || (_currentBundleDiscoun2 = currentBundleDiscount.metadata) === null || _currentBundleDiscoun2 === void 0 ? void 0 : _currentBundleDiscoun2.discount_calculation_mode,
1405
+ fixed_amount: bundleDiscountAmount,
1406
+ resource_id: currentBundleDiscount.id,
1407
+ title: currentBundleDiscount.format_title,
1231
1408
  original_amount: product.origin_total,
1232
- product_id: product.id
1409
+ product_id: product.id,
1410
+ percent: currentBundleDiscount.par_value,
1411
+ discount_product_id: currentBundleDiscount.product_id
1233
1412
  },
1234
1413
  metadata: {
1235
1414
  // 🔥 使用拆分后的唯一 _id
1236
1415
  custom_product_bundle_map_id: uniqueId,
1237
- discount_rule_uncheck_flag: _selectedDiscount2 === null || _selectedDiscount2 === void 0 ? void 0 : _selectedDiscount2.discount_rule_uncheck_flag,
1416
+ discount_rule_uncheck_flag: selectedDiscount === null || selectedDiscount === void 0 ? void 0 : selectedDiscount.discount_rule_uncheck_flag,
1238
1417
  num: 1
1239
1418
  },
1240
- _num: 1
1419
+ _num: 1,
1420
+ config: currentBundleDiscount === null || currentBundleDiscount === void 0 ? void 0 : currentBundleDiscount.config
1241
1421
  };
1242
1422
 
1243
1423
  // 记录实际应用的折扣
1244
- var _appliedProducts = appliedDiscountProducts.get(_selectedDiscount2.id) || [];
1424
+ var _appliedProducts = appliedDiscountProducts.get(currentBundleDiscount.id) || [];
1245
1425
  _appliedProducts.push(_discountDetail);
1246
- appliedDiscountProducts.set(_selectedDiscount2.id, _appliedProducts);
1426
+ appliedDiscountProducts.set(currentBundleDiscount.id, _appliedProducts);
1247
1427
  processedItems.push(_objectSpread(_objectSpread({}, flatItem), {}, {
1248
1428
  // 🔥 使用唯一的 _id
1249
1429
  _id: uniqueId,
1250
1430
  num: 1,
1251
1431
  quantity: 1,
1252
- price: 0,
1253
- // 商品券价格为0
1254
- total: 0,
1432
+ price: discountedPrice,
1433
+ // 折扣后的价格
1434
+ total: discountedPrice,
1255
1435
  discount_list: [_discountDetail],
1256
1436
  processed: true,
1257
- _discountId: _selectedDiscount2.id
1437
+ _discountId: currentBundleDiscount.id
1258
1438
  }));
1259
1439
  }
1260
1440
 
1261
- // 生成无折扣的商品(剩余数量)
1262
- if (normalNum > 0) {
1263
- processedItems.push(_objectSpread(_objectSpread({}, flatItem), {}, {
1264
- // 🔥 为剩余商品生成唯一的 _id
1265
- _id: "".concat(flatItem._id, "_split_rest"),
1266
- num: normalNum,
1267
- quantity: normalNum,
1268
- discount_list: [],
1269
- processed: true
1270
- }));
1441
+ // 🔥 对拆分后的剩余商品继续匹配其他可用折扣(而不是等到下次执行才应用)
1442
+ var remainingNum = normalNum;
1443
+ var continueSplitIndex = discountNum;
1444
+ while (remainingNum > 0) {
1445
+ var _nextSelectedDiscount;
1446
+ // 重新查找适用于剩余商品的可用折扣(已使用的会被自动过滤掉)
1447
+ var nextApplicableDiscounts = sortedDiscountList.filter(function (nextDiscount) {
1448
+ var _product$discount_lis11;
1449
+ var nextDiscountType = nextDiscount.tag || nextDiscount.type;
1450
+ // 检查策略可用性
1451
+ if (['good_pass', 'discount_card', 'product_discount_card'].includes(nextDiscountType)) {
1452
+ var _nextDiscount$config;
1453
+ if (nextDiscount.config === undefined || !(nextDiscount !== null && nextDiscount !== void 0 && (_nextDiscount$config = nextDiscount.config) !== null && _nextDiscount$config !== void 0 && _nextDiscount$config.isAvailable)) {
1454
+ return false;
1455
+ }
1456
+ }
1457
+ // 折扣卡时总价为0时不可用
1458
+ if ((Number(product.total) <= 0 || !product.total) && !((_product$discount_lis11 = product.discount_list) !== null && _product$discount_lis11 !== void 0 && _product$discount_lis11.find(function (n) {
1459
+ var _n$discount2;
1460
+ return ((_n$discount2 = n.discount) === null || _n$discount2 === void 0 ? void 0 : _n$discount2.resource_id) === nextDiscount.id;
1461
+ })) && nextDiscountType !== 'good_pass') return false;
1462
+
1463
+ // 商品券已被使用则跳过
1464
+ if (usedDiscounts.get(nextDiscount.id) && nextDiscountType === 'good_pass') return false;
1465
+
1466
+ // 检查 maxUsagePerOrder 限制
1467
+ if (nextDiscountType === 'good_pass') {
1468
+ var _nextDiscount$config2;
1469
+ var maxUsagePerOrder = (_nextDiscount$config2 = nextDiscount.config) === null || _nextDiscount$config2 === void 0 ? void 0 : _nextDiscount$config2.maxUsagePerOrder;
1470
+ if (maxUsagePerOrder && maxUsagePerOrder > 0) {
1471
+ var currentUsedCount = usedProductIdCounts.get(nextDiscount.product_id) || 0;
1472
+ if (currentUsedCount >= maxUsagePerOrder) {
1473
+ return false;
1474
+ }
1475
+ }
1476
+ }
1477
+
1478
+ // 检查折扣卡的 applicableProductLimit 限制
1479
+ if (['discount_card', 'product_discount_card'].includes(nextDiscountType)) {
1480
+ var _nextDiscount$config3;
1481
+ var limitConfig = ((_nextDiscount$config3 = nextDiscount.config) === null || _nextDiscount$config3 === void 0 ? void 0 : _nextDiscount$config3.applicableProductLimit) || 0;
1482
+ if (limitConfig > 0) {
1483
+ var currentUsedLimitCount = usedDiscountCardLimitCounts.get(nextDiscount.id) || 0;
1484
+ if (currentUsedLimitCount >= limitConfig) {
1485
+ return false;
1486
+ }
1487
+ }
1488
+ }
1489
+ var limitedData = nextDiscount.limited_relation_product_data;
1490
+ if (limitedData.type === 'product_all') {
1491
+ if (limitedData.filter === 1 && limitedData.exclude_product_ids.includes(product.id)) {
1492
+ return false;
1493
+ }
1494
+ if (!_this3.checkPackageSubItemUsageRules(nextDiscount, flatItem)) {
1495
+ return false;
1496
+ }
1497
+ return true;
1498
+ } else if (limitedData.product_ids && limitedData.product_ids.includes(product.id)) {
1499
+ if (!_this3.checkPackageSubItemUsageRules(nextDiscount, flatItem)) {
1500
+ return false;
1501
+ }
1502
+ return true;
1503
+ }
1504
+ return false;
1505
+ });
1506
+
1507
+ // 没有更多可用折扣,剩余商品无折扣存储
1508
+ if (nextApplicableDiscounts.length === 0) {
1509
+ processedItems.push(_objectSpread(_objectSpread({}, flatItem), {}, {
1510
+ _id: "".concat(flatItem._id, "_split_rest"),
1511
+ num: remainingNum,
1512
+ quantity: remainingNum,
1513
+ discount_list: [],
1514
+ processed: true
1515
+ }));
1516
+ break;
1517
+ }
1518
+
1519
+ // 选择最优折扣
1520
+ var nextSelectedDiscountCard = nextApplicableDiscounts.find(function (n) {
1521
+ return n.isScan && n.isSelected && (n.tag || n.type) !== 'good_pass';
1522
+ });
1523
+ var nextSelectedDiscount = nextSelectedDiscountCard || nextApplicableDiscounts[0];
1524
+
1525
+ // 判断新折扣是否需要拆分
1526
+ var nextDiscType = nextSelectedDiscount.tag || nextSelectedDiscount.type;
1527
+ var nextIsGoodPass = nextDiscType === 'good_pass';
1528
+ var nextIsDiscountCard = ['discount_card', 'product_discount_card'].includes(nextDiscType);
1529
+ var nextApplicableProductLimit = ((_nextSelectedDiscount = nextSelectedDiscount.config) === null || _nextSelectedDiscount === void 0 ? void 0 : _nextSelectedDiscount.applicableProductLimit) || 0;
1530
+ var nextIsNeedSplit = nextIsGoodPass || nextIsDiscountCard && nextApplicableProductLimit > 0;
1531
+ if (!nextIsNeedSplit) {
1532
+ var _nextBundleDiscount$m, _flatItem$parentProdu9;
1533
+ // 🔥 不需要拆分:直接应用折扣到所有剩余商品
1534
+ var nextBundleDiscount = nextSelectedDiscountCard || nextApplicableDiscounts[0];
1535
+ usedDiscounts.set(nextBundleDiscount.id, true);
1536
+ if ((nextBundleDiscount.tag || nextBundleDiscount.type) === 'good_pass') {
1537
+ var curCount = usedProductIdCounts.get(nextBundleDiscount.product_id) || 0;
1538
+ usedProductIdCounts.set(nextBundleDiscount.product_id, curCount + 1);
1539
+ }
1540
+ var nextNoSplitType = nextBundleDiscount.tag || nextBundleDiscount.type;
1541
+ if (['discount_card', 'product_discount_card'].includes(nextNoSplitType)) {
1542
+ var _curCount = usedDiscountCardLimitCounts.get(nextBundleDiscount.id) || 0;
1543
+ usedDiscountCardLimitCounts.set(nextBundleDiscount.id, _curCount + remainingNum);
1544
+ }
1545
+ var nextOriginTotal = product.original_price || product.price || 0;
1546
+ var nextTargetTotal = getDiscountAmount(nextBundleDiscount, nextOriginTotal, nextOriginTotal);
1547
+ var nextDiscountAmount = new Decimal(nextOriginTotal).minus(nextTargetTotal).toNumber();
1548
+ var nextUniqueId = "".concat(flatItem._id, "_split_").concat(continueSplitIndex);
1549
+ var nextBundleDiscType = nextBundleDiscount.tag === 'product_discount_card' ? 'discount_card' : nextBundleDiscount.tag;
1550
+ var nextDiscountDetail = {
1551
+ amount: nextDiscountAmount * remainingNum,
1552
+ type: nextBundleDiscType,
1553
+ discount: {
1554
+ discount_card_type: nextBundleDiscount === null || nextBundleDiscount === void 0 || (_nextBundleDiscount$m = nextBundleDiscount.metadata) === null || _nextBundleDiscount$m === void 0 ? void 0 : _nextBundleDiscount$m.discount_card_type,
1555
+ fixed_amount: nextDiscountAmount,
1556
+ resource_id: nextBundleDiscount.id,
1557
+ title: nextBundleDiscount.format_title,
1558
+ original_amount: product.original_price,
1559
+ product_id: product.id,
1560
+ percent: nextBundleDiscount.par_value,
1561
+ discount_product_id: nextBundleDiscount.product_id
1562
+ },
1563
+ metadata: {
1564
+ custom_product_bundle_map_id: nextUniqueId,
1565
+ num: remainingNum
1566
+ },
1567
+ config: nextBundleDiscount === null || nextBundleDiscount === void 0 ? void 0 : nextBundleDiscount.config,
1568
+ _num: remainingNum * ((flatItem === null || flatItem === void 0 || (_flatItem$parentProdu9 = flatItem.parentProduct) === null || _flatItem$parentProdu9 === void 0 ? void 0 : _flatItem$parentProdu9.num) || 1)
1569
+ };
1570
+ var nextAppliedProducts = appliedDiscountProducts.get(nextBundleDiscount.id) || [];
1571
+ nextAppliedProducts.push(nextDiscountDetail);
1572
+ appliedDiscountProducts.set(nextBundleDiscount.id, nextAppliedProducts);
1573
+ processedItems.push(_objectSpread(_objectSpread({}, flatItem), {}, {
1574
+ _id: nextUniqueId,
1575
+ num: remainingNum,
1576
+ quantity: remainingNum,
1577
+ total: nextTargetTotal,
1578
+ price: new Decimal(nextOriginTotal || 0).minus(nextDiscountDetail.discount.fixed_amount).toNumber(),
1579
+ discount_list: [nextDiscountDetail],
1580
+ processed: true
1581
+ }));
1582
+ remainingNum = 0;
1583
+ break;
1584
+ }
1585
+
1586
+ // 🔥 需要拆分:计算最大使用次数
1587
+ var nextMaxUsageLimit = void 0;
1588
+ if (nextIsGoodPass) {
1589
+ var _nextSelectedDiscount2;
1590
+ var nextGoodPassCount = nextApplicableDiscounts.filter(function (item) {
1591
+ return (item.tag || item.type) === 'good_pass';
1592
+ }).length;
1593
+ nextMaxUsageLimit = nextGoodPassCount;
1594
+ if ((_nextSelectedDiscount2 = nextSelectedDiscount.config) !== null && _nextSelectedDiscount2 !== void 0 && _nextSelectedDiscount2.maxUsagePerOrder) {
1595
+ var usedCount = usedProductIdCounts.get(nextSelectedDiscount.product_id) || 0;
1596
+ nextMaxUsageLimit = Math.min(nextGoodPassCount, Math.max(0, nextSelectedDiscount.config.maxUsagePerOrder - usedCount));
1597
+ }
1598
+ } else if (nextIsDiscountCard && nextApplicableProductLimit > 0) {
1599
+ var _usedLimitCount = usedDiscountCardLimitCounts.get(nextSelectedDiscount.id) || 0;
1600
+ nextMaxUsageLimit = Math.max(0, nextApplicableProductLimit - _usedLimitCount);
1601
+ } else {
1602
+ nextMaxUsageLimit = 1;
1603
+ }
1604
+ var nextSplitCount = Math.min(remainingNum, nextMaxUsageLimit);
1605
+ if (nextSplitCount === 0) {
1606
+ // 已达使用上限,剩余商品无折扣存储
1607
+ processedItems.push(_objectSpread(_objectSpread({}, flatItem), {}, {
1608
+ _id: "".concat(flatItem._id, "_split_rest"),
1609
+ num: remainingNum,
1610
+ quantity: remainingNum,
1611
+ discount_list: [],
1612
+ processed: true
1613
+ }));
1614
+ break;
1615
+ }
1616
+
1617
+ // 生成有折扣的商品
1618
+ for (var j = 0; j < nextSplitCount; j++) {
1619
+ var _nextBundleDiscount$m2;
1620
+ var _nextBundleDiscount = nextIsGoodPass ? nextSelectedDiscountCard || nextApplicableDiscounts[j] : nextSelectedDiscountCard || nextApplicableDiscounts[0];
1621
+ usedDiscounts.set(_nextBundleDiscount.id, true);
1622
+ if ((_nextBundleDiscount.tag || _nextBundleDiscount.type) === 'good_pass') {
1623
+ var _curCount2 = usedProductIdCounts.get(_nextBundleDiscount.product_id) || 0;
1624
+ usedProductIdCounts.set(_nextBundleDiscount.product_id, _curCount2 + 1);
1625
+ }
1626
+ var nextBundleDiscTypeForCount = _nextBundleDiscount.tag || _nextBundleDiscount.type;
1627
+ if (['discount_card', 'product_discount_card'].includes(nextBundleDiscTypeForCount)) {
1628
+ var curLimitCount = usedDiscountCardLimitCounts.get(_nextBundleDiscount.id) || 0;
1629
+ usedDiscountCardLimitCounts.set(_nextBundleDiscount.id, curLimitCount + 1);
1630
+ }
1631
+ var _nextUniqueId = "".concat(flatItem._id, "_split_").concat(continueSplitIndex);
1632
+ var nextDiscountedPrice = getDiscountAmount(_nextBundleDiscount, product.origin_total, product.origin_total);
1633
+ var nextBundleDiscAmount = new Decimal(product.origin_total || 0).minus(nextDiscountedPrice).toNumber();
1634
+ var nextBundleDiscTypeStr = _nextBundleDiscount.tag === 'product_discount_card' ? 'discount_card' : _nextBundleDiscount.tag;
1635
+ var _nextDiscountDetail = {
1636
+ amount: nextBundleDiscAmount,
1637
+ type: nextBundleDiscTypeStr,
1638
+ discount: {
1639
+ discount_card_type: _nextBundleDiscount === null || _nextBundleDiscount === void 0 || (_nextBundleDiscount$m2 = _nextBundleDiscount.metadata) === null || _nextBundleDiscount$m2 === void 0 ? void 0 : _nextBundleDiscount$m2.discount_card_type,
1640
+ fixed_amount: nextBundleDiscAmount,
1641
+ resource_id: _nextBundleDiscount.id,
1642
+ title: _nextBundleDiscount.format_title,
1643
+ original_amount: product.origin_total,
1644
+ product_id: product.id,
1645
+ percent: _nextBundleDiscount.par_value,
1646
+ discount_product_id: _nextBundleDiscount.product_id
1647
+ },
1648
+ metadata: {
1649
+ custom_product_bundle_map_id: _nextUniqueId,
1650
+ num: 1
1651
+ },
1652
+ _num: 1,
1653
+ config: _nextBundleDiscount === null || _nextBundleDiscount === void 0 ? void 0 : _nextBundleDiscount.config
1654
+ };
1655
+ var _nextAppliedProducts = appliedDiscountProducts.get(_nextBundleDiscount.id) || [];
1656
+ _nextAppliedProducts.push(_nextDiscountDetail);
1657
+ appliedDiscountProducts.set(_nextBundleDiscount.id, _nextAppliedProducts);
1658
+ processedItems.push(_objectSpread(_objectSpread({}, flatItem), {}, {
1659
+ _id: _nextUniqueId,
1660
+ num: 1,
1661
+ quantity: 1,
1662
+ price: nextDiscountedPrice,
1663
+ total: nextDiscountedPrice,
1664
+ discount_list: [_nextDiscountDetail],
1665
+ processed: true,
1666
+ _discountId: _nextBundleDiscount.id
1667
+ }));
1668
+ continueSplitIndex++;
1669
+ }
1670
+ remainingNum -= nextSplitCount;
1271
1671
  }
1272
1672
  } else {
1273
- var _selectedDiscount3$me, _selectedDiscount3$me2, _flatItem$parentProdu9;
1274
- // 折扣卡:不拆分数量,直接应用
1275
- var _selectedDiscount3 = selectedDiscountCard || applicableDiscounts[0];
1276
- usedDiscounts.set(_selectedDiscount3.id, true);
1673
+ var _currentBundleDiscoun3, _currentBundleDiscoun4, _flatItem$parentProdu10;
1674
+ // 不拆分:直接应用折扣
1675
+ var _currentBundleDiscount = selectedDiscountCard || applicableDiscounts[0];
1676
+ usedDiscounts.set(_currentBundleDiscount.id, true);
1677
+ // 🔥 更新 product_id 使用计数
1678
+ if ((_currentBundleDiscount.tag || _currentBundleDiscount.type) === 'good_pass') {
1679
+ var _currentCount2 = usedProductIdCounts.get(_currentBundleDiscount.product_id) || 0;
1680
+ usedProductIdCounts.set(_currentBundleDiscount.product_id, _currentCount2 + 1);
1681
+ }
1682
+ // 🔥 更新折扣卡 applicableProductLimit 使用计数(不拆分时按商品数量计数)
1683
+ var noSplitDiscountType = _currentBundleDiscount.tag || _currentBundleDiscount.type;
1684
+ if (['discount_card', 'product_discount_card'].includes(noSplitDiscountType)) {
1685
+ var bundleQuantity = product.num || 1;
1686
+ var currentNoSplitCount = usedDiscountCardLimitCounts.get(_currentBundleDiscount.id) || 0;
1687
+ usedDiscountCardLimitCounts.set(_currentBundleDiscount.id, currentNoSplitCount + bundleQuantity);
1688
+ }
1277
1689
  var _productOriginTotal = product.original_price || product.price || 0;
1278
1690
 
1279
1691
  // 🔥 检查是否是 order_level 固定金额折扣卡
1280
- var _isOrderLevel = isOrderLevelFixedAmountDiscount(_selectedDiscount3);
1281
- var _orderLevelAllocation = _isOrderLevel ? orderLevelDiscountAllocations.get(_selectedDiscount3.id) : null;
1692
+ var _isOrderLevel = isOrderLevelFixedAmountDiscount(selectedDiscount);
1693
+ var _orderLevelAllocation = _isOrderLevel ? orderLevelDiscountAllocations.get(selectedDiscount.id) : null;
1282
1694
  var _productAllocation = _orderLevelAllocation === null || _orderLevelAllocation === void 0 ? void 0 : _orderLevelAllocation.get(flatItem._id);
1283
1695
  var _targetProductTotal;
1284
1696
  var fixedAmountPerItem;
@@ -1290,40 +1702,45 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
1290
1702
  _targetProductTotal = Math.max(new Decimal(_productOriginTotal).minus(fixedAmountPerItem).toNumber(), 0);
1291
1703
  } else {
1292
1704
  // item_level 或其他类型:使用原有逻辑
1293
- _targetProductTotal = getDiscountAmount(_selectedDiscount3, _productOriginTotal, _productOriginTotal);
1705
+ _targetProductTotal = getDiscountAmount(_currentBundleDiscount, _productOriginTotal, _productOriginTotal);
1294
1706
  fixedAmountPerItem = new Decimal(_productOriginTotal).minus(_targetProductTotal).toNumber();
1295
1707
  }
1296
- debugger;
1708
+
1709
+ // 计算实际折扣金额
1710
+ var bundleActualDiscountAmount = new Decimal(_productOriginTotal).minus(_targetProductTotal).toNumber();
1711
+
1297
1712
  // 🔥 使用当前的 _id 作为唯一标识
1298
1713
  var _uniqueId = flatItem._id;
1299
1714
  var _discountDetail2 = {
1300
- amount: fixedAmountPerItem * (product.num || 1),
1301
- type: _selectedDiscount3.tag === 'product_discount_card' ? 'discount_card' : _selectedDiscount3.tag,
1715
+ amount: bundleActualDiscountAmount * (product.num || 1),
1716
+ type: _currentBundleDiscount.tag === 'product_discount_card' ? 'discount_card' : _currentBundleDiscount.tag,
1302
1717
  discount: {
1303
- discount_card_type: _selectedDiscount3 === null || _selectedDiscount3 === void 0 || (_selectedDiscount3$me = _selectedDiscount3.metadata) === null || _selectedDiscount3$me === void 0 ? void 0 : _selectedDiscount3$me.discount_card_type,
1304
- fixed_amount: fixedAmountPerItem,
1305
- discount_calculation_mode: _selectedDiscount3 === null || _selectedDiscount3 === void 0 || (_selectedDiscount3$me2 = _selectedDiscount3.metadata) === null || _selectedDiscount3$me2 === void 0 ? void 0 : _selectedDiscount3$me2.discount_calculation_mode,
1306
- resource_id: _selectedDiscount3.id,
1307
- title: _selectedDiscount3.format_title,
1718
+ discount_card_type: _currentBundleDiscount === null || _currentBundleDiscount === void 0 || (_currentBundleDiscoun3 = _currentBundleDiscount.metadata) === null || _currentBundleDiscoun3 === void 0 ? void 0 : _currentBundleDiscoun3.discount_card_type,
1719
+ fixed_amount: bundleActualDiscountAmount,
1720
+ discount_calculation_mode: _currentBundleDiscount === null || _currentBundleDiscount === void 0 || (_currentBundleDiscoun4 = _currentBundleDiscount.metadata) === null || _currentBundleDiscoun4 === void 0 ? void 0 : _currentBundleDiscoun4.discount_calculation_mode,
1721
+ resource_id: _currentBundleDiscount.id,
1722
+ title: _currentBundleDiscount.format_title,
1308
1723
  original_amount: product.original_price,
1309
1724
  product_id: product.id,
1310
- percent: _selectedDiscount3.par_value
1725
+ percent: _currentBundleDiscount.par_value,
1726
+ discount_product_id: _currentBundleDiscount.product_id
1311
1727
  },
1312
1728
  metadata: _objectSpread({
1313
1729
  // 🔥 使用唯一的 _id
1314
1730
  custom_product_bundle_map_id: _uniqueId,
1315
- discount_rule_uncheck_flag: _selectedDiscount3 === null || _selectedDiscount3 === void 0 ? void 0 : _selectedDiscount3.discount_rule_uncheck_flag,
1731
+ discount_rule_uncheck_flag: selectedDiscount === null || selectedDiscount === void 0 ? void 0 : selectedDiscount.discount_rule_uncheck_flag,
1316
1732
  num: product.num || 1
1317
1733
  }, _productDiscountDifference !== undefined && {
1318
1734
  product_discount_difference: _productDiscountDifference
1319
1735
  }),
1320
- _num: (product.num || 1) * ((flatItem === null || flatItem === void 0 || (_flatItem$parentProdu9 = flatItem.parentProduct) === null || _flatItem$parentProdu9 === void 0 ? void 0 : _flatItem$parentProdu9.num) || 1)
1736
+ config: _currentBundleDiscount === null || _currentBundleDiscount === void 0 ? void 0 : _currentBundleDiscount.config,
1737
+ _num: (product.num || 1) * ((flatItem === null || flatItem === void 0 || (_flatItem$parentProdu10 = flatItem.parentProduct) === null || _flatItem$parentProdu10 === void 0 ? void 0 : _flatItem$parentProdu10.num) || 1)
1321
1738
  };
1322
1739
 
1323
1740
  // 记录实际应用的折扣
1324
- var _appliedProducts2 = appliedDiscountProducts.get(_selectedDiscount3.id) || [];
1741
+ var _appliedProducts2 = appliedDiscountProducts.get(_currentBundleDiscount.id) || [];
1325
1742
  _appliedProducts2.push(_discountDetail2);
1326
- appliedDiscountProducts.set(_selectedDiscount3.id, _appliedProducts2);
1743
+ appliedDiscountProducts.set(_currentBundleDiscount.id, _appliedProducts2);
1327
1744
  processedItems.push(_objectSpread(_objectSpread({}, flatItem), {}, {
1328
1745
  total: _targetProductTotal,
1329
1746
  price: new Decimal(_productOriginTotal || 0).minus(fixedAmountPerItem).toNumber(),
@@ -1448,6 +1865,7 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
1448
1865
  product_id: bundleItem.product_id,
1449
1866
  price: item.price,
1450
1867
  num: item.num,
1868
+ quantity: item.num,
1451
1869
  discount_list: updatedDiscountList
1452
1870
  }));
1453
1871
  });
@@ -1471,6 +1889,7 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
1471
1889
  product_id: bundleItem.product_id,
1472
1890
  price: item.price,
1473
1891
  num: item.num,
1892
+ quantity: item.num,
1474
1893
  discount_list: _updatedDiscountList
1475
1894
  }));
1476
1895
  } else {
@@ -1485,8 +1904,8 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
1485
1904
 
1486
1905
  // 🔥 更新主商品自己的 discount_list 中的 num(quantity=1)
1487
1906
  var updatedMainDiscountList = mainProductData.discount_list.map(function (discount) {
1488
- var _discount$metadata6, _discount$metadata7;
1489
- if (discount !== null && discount !== void 0 && (_discount$metadata6 = discount.metadata) !== null && _discount$metadata6 !== void 0 && _discount$metadata6.custom_product_bundle_map_id) {
1907
+ var _discount$metadata7, _discount$metadata8;
1908
+ if (discount !== null && discount !== void 0 && (_discount$metadata7 = discount.metadata) !== null && _discount$metadata7 !== void 0 && _discount$metadata7.custom_product_bundle_map_id) {
1490
1909
  // bundle的discount_list保持不变
1491
1910
  return discount;
1492
1911
  }
@@ -1494,7 +1913,7 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
1494
1913
  return _objectSpread(_objectSpread({}, discount), {}, {
1495
1914
  // num: 1,
1496
1915
  metadata: _objectSpread(_objectSpread({}, discount.metadata), {}, {
1497
- custom_product_bundle_map_id: discount === null || discount === void 0 || (_discount$metadata7 = discount.metadata) === null || _discount$metadata7 === void 0 ? void 0 : _discount$metadata7.custom_product_bundle_map_id,
1916
+ custom_product_bundle_map_id: discount === null || discount === void 0 || (_discount$metadata8 = discount.metadata) === null || _discount$metadata8 === void 0 ? void 0 : _discount$metadata8.custom_product_bundle_map_id,
1498
1917
  num: 1
1499
1918
  })
1500
1919
  });
@@ -1539,8 +1958,8 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
1539
1958
  quantity: 1,
1540
1959
  discount_list: updatedMainDiscountList,
1541
1960
  bundle: newBundleWithDiscount,
1542
- total: new Decimal(newTotalWithDiscount).toDecimalPlaces(2, Decimal.ROUND_HALF_UP).toNumber(),
1543
- origin_total: new Decimal(newOriginTotalWithDiscount).toDecimalPlaces(2, Decimal.ROUND_HALF_UP).toNumber()
1961
+ total: newTotalWithDiscount,
1962
+ origin_total: newOriginTotalWithDiscount
1544
1963
  })));
1545
1964
 
1546
1965
  // 第二个:包含原始bundle (qty=原quantity-1)
@@ -1559,8 +1978,8 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
1559
1978
 
1560
1979
  // 🔥 更新主商品自己的 discount_list 中的 num(quantity=原quantity-1)
1561
1980
  var updatedMainDiscountListOriginal = mainProductData.discount_list.map(function (discount) {
1562
- var _discount$metadata8, _discount$metadata9;
1563
- if (discount !== null && discount !== void 0 && (_discount$metadata8 = discount.metadata) !== null && _discount$metadata8 !== void 0 && _discount$metadata8.custom_product_bundle_map_id) {
1981
+ var _discount$metadata9, _discount$metadata10;
1982
+ if (discount !== null && discount !== void 0 && (_discount$metadata9 = discount.metadata) !== null && _discount$metadata9 !== void 0 && _discount$metadata9.custom_product_bundle_map_id) {
1564
1983
  // bundle的discount_list保持不变
1565
1984
  return discount;
1566
1985
  }
@@ -1568,7 +1987,7 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
1568
1987
  return _objectSpread(_objectSpread({}, discount), {}, {
1569
1988
  metadata: _objectSpread(_objectSpread({}, discount.metadata), {}, {
1570
1989
  num: mainProductQuantity - 1,
1571
- custom_product_bundle_map_id: discount === null || discount === void 0 || (_discount$metadata9 = discount.metadata) === null || _discount$metadata9 === void 0 ? void 0 : _discount$metadata9.custom_product_bundle_map_id
1990
+ custom_product_bundle_map_id: discount === null || discount === void 0 || (_discount$metadata10 = discount.metadata) === null || _discount$metadata10 === void 0 ? void 0 : _discount$metadata10.custom_product_bundle_map_id
1572
1991
  })
1573
1992
  // num: mainProductQuantity - 1,
1574
1993
  });
@@ -1635,11 +2054,11 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
1635
2054
  processedBundleItems.forEach(function (item) {
1636
2055
  // 🔥 更新 discount_list 中的 num,使其与拆分后的 item.num 一致
1637
2056
  var updatedDiscountList = (item.discount_list || []).map(function (discount) {
1638
- var _discount$metadata10;
2057
+ var _discount$metadata11;
1639
2058
  return _objectSpread(_objectSpread({}, discount), {}, {
1640
2059
  metadata: _objectSpread(_objectSpread({}, discount.metadata), {}, {
1641
2060
  num: item.num,
1642
- custom_product_bundle_map_id: discount === null || discount === void 0 || (_discount$metadata10 = discount.metadata) === null || _discount$metadata10 === void 0 ? void 0 : _discount$metadata10.custom_product_bundle_map_id
2061
+ custom_product_bundle_map_id: discount === null || discount === void 0 || (_discount$metadata11 = discount.metadata) === null || _discount$metadata11 === void 0 ? void 0 : _discount$metadata11.custom_product_bundle_map_id
1643
2062
  })
1644
2063
  // num: item.num, // 使用拆分后的 num
1645
2064
  });
@@ -1649,6 +2068,7 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
1649
2068
  product_id: bundleItem.product_id,
1650
2069
  price: item.price,
1651
2070
  num: item.num,
2071
+ quantity: item.num,
1652
2072
  discount_list: updatedDiscountList
1653
2073
  }));
1654
2074
  });
@@ -1687,13 +2107,9 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
1687
2107
 
1688
2108
  // 累加bundle的价格(只累加一次)
1689
2109
  if (newBundle.length > 0) {
1690
- newBundle.forEach(function (item) {
1691
- newTotal += Number(item.price) * Number(item.num);
1692
- });
1693
-
1694
- // 计算原始总价(不考虑折扣)
1695
2110
  newBundle.forEach(function (item) {
1696
2111
  var _item$discount_list3;
2112
+ newTotal += Number(item.price) * Number(item.num);
1697
2113
  var originalPrice = ((_item$discount_list3 = item.discount_list) === null || _item$discount_list3 === void 0 || (_item$discount_list3 = _item$discount_list3[0]) === null || _item$discount_list3 === void 0 || (_item$discount_list3 = _item$discount_list3.discount) === null || _item$discount_list3 === void 0 ? void 0 : _item$discount_list3.original_amount) || item.price;
1698
2114
  newOriginTotal += Number(originalPrice) * Number(item.num);
1699
2115
  });
@@ -1708,8 +2124,6 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
1708
2124
  return accumulator.add(currentPrice.mul(currentNum));
1709
2125
  }, new Decimal(newTotal)).toNumber();
1710
2126
  }
1711
- newTotal = new Decimal(newTotal).toDecimalPlaces(2, Decimal.ROUND_HALF_UP).toNumber();
1712
- newOriginTotal = new Decimal(newOriginTotal).toDecimalPlaces(2, Decimal.ROUND_HALF_UP).toNumber();
1713
2127
 
1714
2128
  // 生成最终的主商品
1715
2129
  result.push(_this3.hooks.setProduct(mainProduct, _objectSpread(_objectSpread({}, mainProductData), {}, {