@pisell/pisellos 3.0.71 → 3.0.72

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 (33) hide show
  1. package/dist/modules/Cart/utils/cartProduct.js +41 -26
  2. package/dist/modules/Discount/index.d.ts +2 -0
  3. package/dist/modules/Discount/index.js +69 -36
  4. package/dist/modules/Discount/types.d.ts +14 -0
  5. package/dist/modules/Order/index.js +4 -1
  6. package/dist/modules/Order/utils.d.ts +1 -0
  7. package/dist/modules/Order/utils.js +9 -0
  8. package/dist/modules/Rules/index.d.ts +8 -0
  9. package/dist/modules/Rules/index.js +1033 -183
  10. package/dist/modules/Rules/types.d.ts +4 -1
  11. package/dist/solution/BookingByStep/index.js +1 -0
  12. package/dist/solution/ShopDiscount/index.d.ts +2 -0
  13. package/dist/solution/ShopDiscount/index.js +82 -19
  14. package/dist/solution/ShopDiscount/types.d.ts +4 -0
  15. package/dist/solution/ShopDiscount/utils.d.ts +55 -0
  16. package/dist/solution/ShopDiscount/utils.js +418 -3
  17. package/lib/modules/Cart/utils/cartProduct.js +35 -22
  18. package/lib/modules/Discount/index.d.ts +2 -0
  19. package/lib/modules/Discount/index.js +19 -4
  20. package/lib/modules/Discount/types.d.ts +14 -0
  21. package/lib/modules/Order/index.js +2 -0
  22. package/lib/modules/Order/utils.d.ts +1 -0
  23. package/lib/modules/Order/utils.js +11 -0
  24. package/lib/modules/Rules/index.d.ts +8 -0
  25. package/lib/modules/Rules/index.js +813 -172
  26. package/lib/modules/Rules/types.d.ts +4 -1
  27. package/lib/solution/BookingByStep/index.js +4 -0
  28. package/lib/solution/ShopDiscount/index.d.ts +2 -0
  29. package/lib/solution/ShopDiscount/index.js +58 -10
  30. package/lib/solution/ShopDiscount/types.d.ts +4 -0
  31. package/lib/solution/ShopDiscount/utils.d.ts +55 -0
  32. package/lib/solution/ShopDiscount/utils.js +257 -3
  33. package/package.json +1 -1
@@ -25,9 +25,10 @@ function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol"
25
25
  function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
26
26
  import { BaseModule } from "../BaseModule";
27
27
  import { RulesHooks } from "./types";
28
- import { uniqueById, getDiscountAmount } from "../../solution/ShopDiscount/utils";
28
+ import { uniqueById, getDiscountAmount, getDiscountListAmountTotal, getDiscountListAmount, filterDiscountListByBookingTime } from "../../solution/ShopDiscount/utils";
29
29
  import { getProductOriginTotalPrice, getProductTotalPrice } from "../Cart/utils";
30
30
  import Decimal from 'decimal.js';
31
+ import dayjs from 'dayjs';
31
32
  export var RulesModule = /*#__PURE__*/function (_BaseModule) {
32
33
  _inherits(RulesModule, _BaseModule);
33
34
  var _super = _createSuper(RulesModule);
@@ -141,10 +142,15 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
141
142
  // 遍历处理后的产品列表,检查是否有产品使用了新折扣
142
143
  result.productList.forEach(function (product) {
143
144
  var _this2$hooks$getProdu = _this2.hooks.getProduct(product),
144
- discount_list = _this2$hooks$getProdu.discount_list;
145
- if (discount_list && discount_list.length > 0) {
145
+ discount_list = _this2$hooks$getProdu.discount_list,
146
+ bundle = _this2$hooks$getProdu.bundle;
147
+ var allDiscountList = _toConsumableArray(discount_list || []);
148
+ (bundle || []).forEach(function (item) {
149
+ allDiscountList.push.apply(allDiscountList, _toConsumableArray((item === null || item === void 0 ? void 0 : item.discount_list) || []));
150
+ });
151
+ if (allDiscountList && allDiscountList.length > 0) {
146
152
  // 检查是否有使用新折扣的情况
147
- var usedNewDiscount = discount_list.some(function (discount) {
153
+ var usedNewDiscount = allDiscountList.some(function (discount) {
148
154
  var _discount$discount;
149
155
  return newDiscountIds.includes(discount === null || discount === void 0 || (_discount$discount = discount.discount) === null || _discount$discount === void 0 ? void 0 : _discount$discount.resource_id);
150
156
  });
@@ -187,6 +193,55 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
187
193
  return !discount.isManualSelect;
188
194
  });
189
195
 
196
+ // 🔥 扁平化商品列表:将 bundle 子商品展开为虚拟商品
197
+ var flattenProductsWithBundle = function flattenProductsWithBundle(productList) {
198
+ var flattened = [];
199
+ productList.forEach(function (originProduct) {
200
+ var product = _this3.hooks.getProduct(originProduct);
201
+
202
+ // 1. 添加主商品
203
+ flattened.push({
204
+ type: 'main',
205
+ originProduct: originProduct,
206
+ product: product,
207
+ price: Number(product.price || 0),
208
+ id: product.id,
209
+ _id: product._id,
210
+ parentId: product._id,
211
+ quantity: product.quantity,
212
+ num: product.num
213
+ });
214
+
215
+ // 2. 展开 bundle 子商品
216
+ if (product.bundle && Array.isArray(product.bundle) && product.bundle.length > 0) {
217
+ product.bundle.forEach(function (bundleItem, bundleIndex) {
218
+ flattened.push({
219
+ type: 'bundle',
220
+ originProduct: originProduct,
221
+ parentProduct: product,
222
+ bundleItem: bundleItem,
223
+ bundleIndex: bundleIndex,
224
+ // 虚拟商品属性
225
+ price: Number(bundleItem.price || 0),
226
+ id: bundleItem._bundle_product_id,
227
+ // 🔥 使用 _bundle_product_id
228
+ _id: "".concat(product._id, "_bundle_").concat(bundleIndex),
229
+ parentId: product._id,
230
+ num: bundleItem.num || 1,
231
+ quantity: bundleItem.num || 1,
232
+ total: new Decimal(bundleItem.price || 0).mul(bundleItem.num || 1).toNumber(),
233
+ origin_total: new Decimal(bundleItem.price || 0).mul(bundleItem.num || 1).toNumber(),
234
+ original_price: bundleItem.original_price,
235
+ // 继承主商品属性
236
+ booking_id: product.booking_id,
237
+ discount_list: bundleItem.discount_list || []
238
+ });
239
+ });
240
+ }
241
+ });
242
+ return flattened;
243
+ };
244
+
190
245
  // 优惠力度排序,传进来的数据里可能有商品券,也可能有优惠券
191
246
  // 1. 商品券(n.tag=good_pass)视为最优惠(免费)
192
247
  // 2. 折扣券(n.tag=product_discount_card)按照新的优先级排序:
@@ -222,12 +277,12 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
222
277
  }
223
278
  }
224
279
 
225
- // 3. 都是百分比时,折扣越大越优先(par_value越小越优先)
280
+ // 3. 都是百分比时,折扣越大越优先(par_value越大越优先)
226
281
  if (typeA === 'percent' && typeB === 'percent') {
227
282
  if (a.par_value !== b.par_value) {
228
283
  var _valueA = new Decimal(100).minus(a.par_value || 0);
229
284
  var _valueB = new Decimal(100).minus(b.par_value || 0);
230
- return _valueB.minus(_valueA).toNumber(); // 折扣大的在前
285
+ return _valueA.minus(_valueB).toNumber(); // 折扣大的在前
231
286
  }
232
287
  }
233
288
 
@@ -261,21 +316,50 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
261
316
  // }
262
317
  // })
263
318
 
319
+ // 🔥 扁平化商品列表(包含主商品和bundle子商品)
320
+ var flattenedList = flattenProductsWithBundle(productList);
321
+
322
+ // 对扁平化后的列表按价格降序排序(用于应用优惠券时优先选择高价商品)
323
+ var sortedFlattenedList = flattenedList.sort(function (a, b) {
324
+ var priceA = new Decimal(a.price || '0');
325
+ var priceB = new Decimal(b.price || '0');
326
+ if (priceA.equals(priceB)) {
327
+ // 价格相同时,主商品优先
328
+ if (a.type !== b.type) {
329
+ return a.type === 'main' ? -1 : 1;
330
+ }
331
+ // 都是主商品时,按原有逻辑排序
332
+ if (a.type === 'main' && b.type === 'main') {
333
+ if (a.product.quantity === b.product.quantity) {
334
+ var _b$product$discount_l, _a$product$discount_l;
335
+ return (((_b$product$discount_l = b.product.discount_list) === null || _b$product$discount_l === void 0 ? void 0 : _b$product$discount_l.length) || 0) - (((_a$product$discount_l = a.product.discount_list) === null || _a$product$discount_l === void 0 ? void 0 : _a$product$discount_l.length) || 0);
336
+ }
337
+ return a.product.quantity - b.product.quantity;
338
+ }
339
+ // 都是bundle时,按数量排序
340
+ if (a.type === 'bundle' && b.type === 'bundle') {
341
+ return (a.num || 1) - (b.num || 1);
342
+ }
343
+ }
344
+ return priceB.minus(priceA).toNumber();
345
+ });
346
+
347
+ /**
264
348
  // 对productList按价格降序排序(用于应用优惠券时优先选择高价商品) 价格相同时使用quantity 排序
265
- var sortedProductList = _toConsumableArray(productList).sort(function (a, b) {
266
- var aProduct = _this3.hooks.getProduct(a);
267
- var bProduct = _this3.hooks.getProduct(b);
268
- var priceA = new Decimal(aProduct.price || '0');
269
- var priceB = new Decimal(bProduct.price || '0');
349
+ const sortedProductList = [...productList].sort((a, b) => {
350
+ const aProduct = this.hooks.getProduct(a);
351
+ const bProduct = this.hooks.getProduct(b);
352
+ const priceA = new Decimal((aProduct.price as string) || '0');
353
+ const priceB = new Decimal((bProduct.price as string) || '0');
270
354
  if (priceA.toNumber() === priceB.toNumber()) {
271
355
  if (aProduct.quantity === bProduct.quantity) {
272
- var _bProduct$discount_li, _aProduct$discount_li;
273
- return ((_bProduct$discount_li = bProduct.discount_list) === null || _bProduct$discount_li === void 0 ? void 0 : _bProduct$discount_li.length) - ((_aProduct$discount_li = aProduct.discount_list) === null || _aProduct$discount_li === void 0 ? void 0 : _aProduct$discount_li.length);
356
+ return bProduct.discount_list?.length - aProduct.discount_list?.length;
274
357
  }
275
358
  return aProduct.quantity - bProduct.quantity;
276
359
  }
277
360
  return priceB.toNumber() - priceA.toNumber();
278
361
  });
362
+ */
279
363
 
280
364
  // 标记已使用的优惠券
281
365
  var usedDiscounts = new Map();
@@ -299,80 +383,161 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
299
383
  var appliedDiscountProducts = new Map();
300
384
 
301
385
  // 首先遍历所有商品和所有优惠券,确定每个优惠券的适用范围,包括isSelected为false的优惠券
302
- sortedProductList.forEach(function (originProduct) {
303
- var product = _this3.hooks.getProduct(originProduct);
386
+ // 🔥 使用扁平化后的列表,包含主商品和bundle子商品
387
+ sortedFlattenedList.forEach(function (flatItem) {
388
+ // 获取商品数据
389
+ var product, originProduct;
390
+ if (flatItem.type === 'main') {
391
+ product = flatItem.product;
392
+ originProduct = flatItem.originProduct;
393
+ } else {
394
+ // bundle子商品:构造虚拟商品对象
395
+ product = {
396
+ _id: flatItem._id,
397
+ id: flatItem.id,
398
+ price: flatItem.price,
399
+ quantity: flatItem.quantity,
400
+ num: flatItem.num,
401
+ total: flatItem.total,
402
+ origin_total: flatItem.origin_total,
403
+ booking_id: flatItem.booking_id,
404
+ discount_list: flatItem.discount_list || []
405
+ };
406
+ originProduct = flatItem.originProduct;
407
+ }
304
408
  addModeDiscount.forEach(function (discount) {
305
- var _product$discount_lis2, _product$discount_lis3;
409
+ var _product, _product2, _product3, _product4, _flatItem$bundleItem;
306
410
  var limitedData = discount === null || discount === void 0 ? void 0 : discount.limited_relation_product_data;
307
-
411
+ var timeLimit = true;
412
+ timeLimit = !!filterDiscountListByBookingTime([discount], (((_product = product) === null || _product === void 0 ? void 0 : _product.startDate) || dayjs()).format('YYYY-MM-DD HH:mm:ss')).length;
308
413
  // 是符合折扣的商品
309
414
  var isLimitedProduct = limitedData.type === 'product_all' || limitedData.product_ids && limitedData.product_ids.includes(product.id);
310
415
 
311
416
  // 编辑的商品 使用了优惠券不可用
312
- var isAvailableProduct = !(product !== null && product !== void 0 && product.booking_id && product !== null && product !== void 0 && (_product$discount_lis2 = product.discount_list) !== null && _product$discount_lis2 !== void 0 && _product$discount_lis2.length && product !== null && product !== void 0 && (_product$discount_lis3 = product.discount_list) !== null && _product$discount_lis3 !== void 0 && _product$discount_lis3.every(function (discount) {
417
+ var isAvailableProduct = flatItem.type === 'main' ? !((_product2 = product) !== null && _product2 !== void 0 && _product2.booking_id && (_product3 = product) !== null && _product3 !== void 0 && (_product3 = _product3.discount_list) !== null && _product3 !== void 0 && _product3.length && (_product4 = product) !== null && _product4 !== void 0 && (_product4 = _product4.discount_list) !== null && _product4 !== void 0 && _product4.every(function (discount) {
313
418
  return discount.id && ['good_pass', 'discount_card', 'product_discount_card'].includes(discount.tag || discount.type);
314
- }));
419
+ })) : !(flatItem !== null && flatItem !== void 0 && flatItem.booking_id && flatItem !== null && flatItem !== void 0 && (_flatItem$bundleItem = flatItem.bundleItem) !== null && _flatItem$bundleItem !== void 0 && (_flatItem$bundleItem = _flatItem$bundleItem.metadata) !== null && _flatItem$bundleItem !== void 0 && _flatItem$bundleItem.custom_product_bundle_map_id);
420
+
421
+ // 套餐是否可用判断
422
+ var isBundleAvailable = _this3.checkPackageSubItemUsageRules(discount, flatItem);
315
423
 
316
424
  // 判断优惠券是否适用于该商品
317
- if (isAvailableProduct && isLimitedProduct) {
425
+ if (isAvailableProduct && isLimitedProduct && isBundleAvailable && timeLimit) {
318
426
  var _discountApplicabilit, _discount$metadata;
319
427
  // 记录此优惠券适用的商品
320
428
  (_discountApplicabilit = discountApplicability.get(discount.id)) === null || _discountApplicabilit === void 0 || _discountApplicabilit.push(product.id);
321
429
 
322
430
  // 记录可抵扣的商品详情
323
431
  var applicableProducts = discountApplicableProducts.get(discount.id) || [];
324
- applicableProducts.push({
325
- amount: product.price,
326
- type: discount.tag || discount.type,
327
- tag: discount.tag || discount.type,
432
+ var discountType = discount.tag || discount.type;
433
+ var isGoodPass = discountType === 'good_pass';
434
+
435
+ // 使用数量
436
+ var num = isGoodPass || (flatItem === null || flatItem === void 0 ? void 0 : flatItem.type) === 'main' ? 1 : product.num;
437
+ var productData = {
438
+ amount: product.price * num,
439
+ type: discountType,
440
+ tag: discountType,
328
441
  discount: {
329
442
  discount_card_type: discount === null || discount === void 0 || (_discount$metadata = discount.metadata) === null || _discount$metadata === void 0 ? void 0 : _discount$metadata.discount_card_type,
330
443
  fixed_amount: product.price,
331
444
  resource_id: discount.id,
332
445
  title: discount.format_title,
333
- original_amount: product.origin_total,
446
+ original_amount: product.price || product.origin_total,
334
447
  pre_value: discount.par_value,
335
448
  product_id: originProduct.id
336
449
  },
337
- num: product.num || 1
338
- });
450
+ metadata: {
451
+ num: num
452
+ }
453
+ };
454
+ applicableProducts.push(productData);
339
455
  discountApplicableProducts.set(discount.id, applicableProducts);
340
456
  }
341
457
  });
342
458
  });
343
- console.log(sortedProductList, 'sortedProductListsortedProductList');
459
+
460
+ // 🔥 用于存储扁平化商品处理结果的Map
461
+ var processedFlatItemsMap = new Map();
344
462
 
345
463
  // 然后再处理应用哪些优惠券,此时只考虑filteredDiscountList中的优惠券
346
- sortedProductList.forEach(function (originProduct, index) {
347
- var _product$discount_lis4, _product$discount_lis5, _product$discount_lis7, _product$discount_lis8, _product$discount_lis9, _product$discount_lis10;
348
- var product = _this3.hooks.getProduct(originProduct);
349
- if (product !== null && product !== void 0 && product.booking_id && (_product$discount_lis4 = product.discount_list) !== null && _product$discount_lis4 !== void 0 && _product$discount_lis4.length && product !== null && product !== void 0 && (_product$discount_lis5 = product.discount_list) !== null && _product$discount_lis5 !== void 0 && _product$discount_lis5.every(function (discount) {
464
+ // 🔥 使用扁平化后的列表进行处理
465
+ sortedFlattenedList.forEach(function (flatItem, index) {
466
+ var _product5, _product$discount_lis2, _product6;
467
+ // 获取商品数据
468
+ var product, originProduct;
469
+ if (flatItem.type === 'main') {
470
+ product = flatItem.product;
471
+ originProduct = flatItem.originProduct;
472
+ } else {
473
+ var _flatItem$bundleItem2, _flatItem$bundleItem3, _flatItem$bundleItem4;
474
+ // bundle子商品
475
+ product = {
476
+ _id: flatItem._id,
477
+ id: flatItem.id,
478
+ price: flatItem.price,
479
+ quantity: flatItem.quantity,
480
+ num: flatItem.num,
481
+ total: flatItem.total,
482
+ original_price: flatItem === null || flatItem === void 0 || (_flatItem$bundleItem2 = flatItem.bundleItem) === null || _flatItem$bundleItem2 === void 0 ? void 0 : _flatItem$bundleItem2.original_price,
483
+ origin_total: flatItem === null || flatItem === void 0 || (_flatItem$bundleItem3 = flatItem.bundleItem) === null || _flatItem$bundleItem3 === void 0 ? void 0 : _flatItem$bundleItem3.original_price,
484
+ booking_id: flatItem.booking_id,
485
+ discount_list: (flatItem === null || flatItem === void 0 || (_flatItem$bundleItem4 = flatItem.bundleItem) === null || _flatItem$bundleItem4 === void 0 ? void 0 : _flatItem$bundleItem4.discount_list) || []
486
+ };
487
+ originProduct = flatItem.originProduct;
488
+ }
489
+
490
+ // 已有优惠的商品跳过
491
+ if ((_product5 = product) !== null && _product5 !== void 0 && _product5.booking_id && (_product$discount_lis2 = product.discount_list) !== null && _product$discount_lis2 !== void 0 && _product$discount_lis2.length && (_product6 = product) !== null && _product6 !== void 0 && (_product6 = _product6.discount_list) !== null && _product6 !== void 0 && _product6.every(function (discount) {
350
492
  return discount.id && ['good_pass', 'discount_card', 'product_discount_card'].includes(discount.tag || discount.type);
351
493
  })) {
352
- processedProductsMap.set(product._id, [originProduct]);
494
+ if (flatItem.type === 'main') {
495
+ processedProductsMap.set(product._id, [originProduct]);
496
+ } else {
497
+ processedFlatItemsMap.set(flatItem._id, [_objectSpread(_objectSpread({}, flatItem), {}, {
498
+ processed: true
499
+ })]);
500
+ }
353
501
  return;
354
502
  }
355
503
 
356
504
  // 找到适用于此商品的所有优惠券,仅考虑isSelected不为false的优惠券
357
505
  var applicableDiscounts = sortedDiscountList.filter(function (discount) {
358
- var _product$discount_lis6;
506
+ var _product$discount_lis3, _product$discount_lis4;
359
507
  // 如果商品价格为 0,其实不需要使用任何优惠券,直接 return true
360
508
  // 商品券时主商品价格为0不可用
361
- if ((Number(product.price) === 0 || !product.price) && (discount.tag || discount.type) === 'good_pass') return false;
362
- // 折扣卡时总价为0时不可用
363
- if ((Number(product.total) === 0 || !product.total) && !((_product$discount_lis6 = product.discount_list) !== null && _product$discount_lis6 !== void 0 && _product$discount_lis6.find(function (n) {
509
+ if ((Number(product.price) <= 0 || !product.price) && !((_product$discount_lis3 = product.discount_list) !== null && _product$discount_lis3 !== void 0 && _product$discount_lis3.find(function (n) {
364
510
  var _n$discount;
365
511
  return ((_n$discount = n.discount) === null || _n$discount === void 0 ? void 0 : _n$discount.resource_id) === discount.id;
512
+ })) && (discount.tag || discount.type) === 'good_pass') return false;
513
+
514
+ // 折扣卡商品价格为0时不可用
515
+ if ((Number(product.price) === 0 || !product.price) && !((_product$discount_lis4 = product.discount_list) !== null && _product$discount_lis4 !== void 0 && _product$discount_lis4.find(function (n) {
516
+ var _n$discount2;
517
+ return ((_n$discount2 = n.discount) === null || _n$discount2 === void 0 ? void 0 : _n$discount2.resource_id) === discount.id;
366
518
  })) && (discount.tag || discount.type) !== 'good_pass') return false;
367
519
  // 如果优惠券已被使用,则跳过
368
520
  var targetUsedDiscounts = usedDiscounts.get(discount.id);
369
521
  if (targetUsedDiscounts && (discount.tag || discount.type) === 'good_pass') return false;
370
522
  var limitedData = discount.limited_relation_product_data;
523
+ var timeLimit = true;
524
+ timeLimit = !!filterDiscountListByBookingTime([discount], (product.startDate || dayjs()).format('YYYY-MM-DD HH:mm:ss')).length;
525
+ if (!timeLimit) {
526
+ return false;
527
+ }
371
528
 
372
529
  // 判断优惠券是否适用于该商品
373
530
  if (limitedData.type === 'product_all') {
531
+ // 检查 package_sub_item_usage_rules
532
+ if (!_this3.checkPackageSubItemUsageRules(discount, flatItem)) {
533
+ return false;
534
+ }
374
535
  return true;
375
536
  } else if (limitedData.product_ids && limitedData.product_ids.includes(product.id)) {
537
+ // 检查 package_sub_item_usage_rules
538
+ if (!_this3.checkPackageSubItemUsageRules(discount, flatItem)) {
539
+ return false;
540
+ }
376
541
  return true;
377
542
  }
378
543
  return false;
@@ -387,50 +552,130 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
387
552
  var selectedDiscount = selectedDiscountCard || applicableDiscounts[0];
388
553
 
389
554
  // 如果是手动折扣,则不适用优惠券
390
- var isManualDiscount = typeof product.isManualDiscount === 'boolean' ? product.isManualDiscount : product.total != product.origin_total && (!((_product$discount_lis7 = product.discount_list) !== null && _product$discount_lis7 !== void 0 && _product$discount_lis7.length) || (product === null || product === void 0 || (_product$discount_lis8 = product.discount_list) === null || _product$discount_lis8 === void 0 || (_product$discount_lis9 = _product$discount_lis8.every) === null || _product$discount_lis9 === void 0 ? void 0 : _product$discount_lis9.call(_product$discount_lis8, function (item) {
391
- return item.type === 'product';
392
- })));
555
+ var isManualDiscount = false;
556
+ if (flatItem.type === 'main') {
557
+ var _product$discount_lis5, _product7, _product7$every;
558
+ // 主商品:判断自身是否手动折扣
559
+ isManualDiscount = typeof product.isManualDiscount === 'boolean' ? product.isManualDiscount : product.total != product.origin_total && (product.bundle || []).every(function (item) {
560
+ var _ref3;
561
+ return !((_ref3 = item.discount_list || []) !== null && _ref3 !== void 0 && _ref3.length);
562
+ }) && (!((_product$discount_lis5 = product.discount_list) !== null && _product$discount_lis5 !== void 0 && _product$discount_lis5.length) || ((_product7 = product) === null || _product7 === void 0 || (_product7 = _product7.discount_list) === null || _product7 === void 0 || (_product7$every = _product7.every) === null || _product7$every === void 0 ? void 0 : _product7$every.call(_product7, function (item) {
563
+ return item.type === 'product';
564
+ })));
565
+ } else {
566
+ // bundle子商品:判断父主商品是否手动折扣
567
+ var parentProduct = flatItem.parentProduct;
568
+ if (parentProduct) {
569
+ var _parentProduct$discou, _parentProduct$discou2, _parentProduct$discou3;
570
+ isManualDiscount = typeof parentProduct.isManualDiscount === 'boolean' ? parentProduct.isManualDiscount : parentProduct.total != parentProduct.origin_total && (parentProduct.bundle || []).every(function (item) {
571
+ var _ref4;
572
+ return !((_ref4 = item.discount_list || []) !== null && _ref4 !== void 0 && _ref4.length);
573
+ }) && (!((_parentProduct$discou = parentProduct.discount_list) !== null && _parentProduct$discou !== void 0 && _parentProduct$discou.length) || (parentProduct === null || parentProduct === void 0 || (_parentProduct$discou2 = parentProduct.discount_list) === null || _parentProduct$discou2 === void 0 || (_parentProduct$discou3 = _parentProduct$discou2.every) === null || _parentProduct$discou3 === void 0 ? void 0 : _parentProduct$discou3.call(_parentProduct$discou2, function (item) {
574
+ return item.type === 'product';
575
+ })));
576
+ }
577
+ }
393
578
 
394
579
  // 勾选时覆盖手动折扣
395
- if (options !== null && options !== void 0 && options.discountId && (_product$discount_lis10 = product.discount_list) !== null && _product$discount_lis10 !== void 0 && _product$discount_lis10.some(function (item) {
396
- var _item$discount;
397
- return ((_item$discount = item.discount) === null || _item$discount === void 0 ? void 0 : _item$discount.resource_id) === options.discountId;
398
- })) {
399
- isManualDiscount = false;
580
+ if (options !== null && options !== void 0 && options.discountId) {
581
+ var _product$discount_lis6;
582
+ // 主商品:检查自己的 discount_list
583
+ if (flatItem.type === 'main' && (_product$discount_lis6 = product.discount_list) !== null && _product$discount_lis6 !== void 0 && _product$discount_lis6.some(function (item) {
584
+ var _item$discount;
585
+ return ((_item$discount = item.discount) === null || _item$discount === void 0 ? void 0 : _item$discount.resource_id) === options.discountId;
586
+ })) {
587
+ isManualDiscount = false;
588
+ }
589
+ // bundle子商品:检查自己的 discount_list 或父主商品的 discount_list
590
+ if (flatItem.type === 'bundle') {
591
+ var _product$discount_lis7, _flatItem$parentProdu;
592
+ if ((_product$discount_lis7 = product.discount_list) !== null && _product$discount_lis7 !== void 0 && _product$discount_lis7.some(function (item) {
593
+ var _item$discount2;
594
+ return ((_item$discount2 = item.discount) === null || _item$discount2 === void 0 ? void 0 : _item$discount2.resource_id) === options.discountId;
595
+ }) || (_flatItem$parentProdu = flatItem.parentProduct) !== null && _flatItem$parentProdu !== void 0 && (_flatItem$parentProdu = _flatItem$parentProdu.discount_list) !== null && _flatItem$parentProdu !== void 0 && _flatItem$parentProdu.some(function (item) {
596
+ var _item$discount3;
597
+ return ((_item$discount3 = item.discount) === null || _item$discount3 === void 0 ? void 0 : _item$discount3.resource_id) === options.discountId;
598
+ })) {
599
+ isManualDiscount = false;
600
+ }
601
+ }
602
+ }
603
+ if (options !== null && options !== void 0 && options.selectedList) {
604
+ var _product$discount_lis8;
605
+ // 主商品:检查自己的 discount_list
606
+ if (flatItem.type === 'main' && (_product$discount_lis8 = product.discount_list) !== null && _product$discount_lis8 !== void 0 && _product$discount_lis8.some(function (item) {
607
+ var _options$selectedList;
608
+ return options === null || options === void 0 || (_options$selectedList = options.selectedList) === null || _options$selectedList === void 0 ? void 0 : _options$selectedList.some(function (n) {
609
+ var _item$discount4;
610
+ return n.discountId === ((_item$discount4 = item.discount) === null || _item$discount4 === void 0 ? void 0 : _item$discount4.resource_id);
611
+ });
612
+ })) {
613
+ isManualDiscount = false;
614
+ }
615
+ // bundle子商品:检查自己的 discount_list 或父主商品的 discount_list
616
+ if (flatItem.type === 'bundle') {
617
+ var _product$discount_lis9, _flatItem$parentProdu2;
618
+ if ((_product$discount_lis9 = product.discount_list) !== null && _product$discount_lis9 !== void 0 && _product$discount_lis9.some(function (item) {
619
+ var _options$selectedList2;
620
+ return options === null || options === void 0 || (_options$selectedList2 = options.selectedList) === null || _options$selectedList2 === void 0 ? void 0 : _options$selectedList2.some(function (n) {
621
+ var _item$discount5;
622
+ return n.discountId === ((_item$discount5 = item.discount) === null || _item$discount5 === void 0 ? void 0 : _item$discount5.resource_id);
623
+ });
624
+ }) || (_flatItem$parentProdu2 = flatItem.parentProduct) !== null && _flatItem$parentProdu2 !== void 0 && (_flatItem$parentProdu2 = _flatItem$parentProdu2.discount_list) !== null && _flatItem$parentProdu2 !== void 0 && _flatItem$parentProdu2.some(function (item) {
625
+ var _options$selectedList3;
626
+ return options === null || options === void 0 || (_options$selectedList3 = options.selectedList) === null || _options$selectedList3 === void 0 ? void 0 : _options$selectedList3.some(function (n) {
627
+ var _item$discount6;
628
+ return n.discountId === ((_item$discount6 = item.discount) === null || _item$discount6 === void 0 ? void 0 : _item$discount6.resource_id);
629
+ });
630
+ })) {
631
+ isManualDiscount = false;
632
+ }
633
+ }
400
634
  }
401
635
 
402
636
  // 如果没有适用的优惠券,或者手动折扣,则不适用优惠券
403
637
  if (applicableDiscounts.length === 0 || isManualDiscount) {
404
- if (product.isClient) {
405
- processedProductsMap.set(product._id, [_this3.hooks.setProduct(originProduct, _objectSpread(_objectSpread({}, isManualDiscount ? {} : {
406
- origin_total: getProductOriginTotalPrice({
407
- product: {
408
- original_price: product.original_price
409
- },
410
- bundle: product.bundle,
411
- options: product.options
412
- }),
413
- variant: originProduct._productInit.variant,
414
- original_price: originProduct._productInit.original_price,
415
- total: getProductTotalPrice({
416
- product: {
417
- price: product.price
418
- },
419
- bundle: product.bundle,
420
- options: product.options
421
- }),
422
- price: product.price
423
- }), {}, {
424
- discount_list: []
425
- }))]);
638
+ if (flatItem.type === 'main') {
639
+ // 主商品:保持原有逻辑
640
+ if (product.isClient) {
641
+ processedProductsMap.set(product._id, [_this3.hooks.setProduct(originProduct, _objectSpread(_objectSpread({}, isManualDiscount ? {} : {
642
+ origin_total: getProductOriginTotalPrice({
643
+ product: {
644
+ original_price: product.original_price
645
+ },
646
+ bundle: product.bundle,
647
+ options: product.options
648
+ }),
649
+ variant: originProduct._productInit.variant,
650
+ original_price: originProduct._productInit.original_price,
651
+ total: getProductTotalPrice({
652
+ product: {
653
+ price: product.price
654
+ },
655
+ bundle: product.bundle,
656
+ options: product.options
657
+ }),
658
+ price: product.price
659
+ }), {}, {
660
+ discount_list: []
661
+ }))]);
662
+ } else {
663
+ processedProductsMap.set(product._id, [_this3.hooks.setProduct(originProduct, _objectSpread(_objectSpread({}, isManualDiscount ? {} : {
664
+ _id: product._id.split('___')[0] + '___' + index,
665
+ total: product.origin_total || product.total,
666
+ price: product.price,
667
+ main_product_selling_price: product.price
668
+ }), {}, {
669
+ discount_list: []
670
+ }))]);
671
+ }
426
672
  } else {
427
- processedProductsMap.set(product._id, [_this3.hooks.setProduct(originProduct, _objectSpread(_objectSpread({}, isManualDiscount ? {} : {
428
- _id: product._id.split('___')[0] + '___' + index,
429
- total: product.origin_total || product.total,
430
- price: product.price
431
- }), {}, {
432
- discount_list: []
433
- }))]);
673
+ // bundle子商品:保存到扁平化Map
674
+ processedFlatItemsMap.set(flatItem._id, [_objectSpread(_objectSpread({}, flatItem), {}, {
675
+ discount_list: [],
676
+ price: flatItem.bundleItem.original_price,
677
+ processed: true
678
+ })]);
434
679
  }
435
680
  return;
436
681
  }
@@ -438,132 +683,598 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
438
683
  return;
439
684
  }
440
685
 
441
- // 是否需要拆分
686
+ // 是否需要拆分(商品券需要拆分)
442
687
  var isNeedSplit = (selectedDiscount.tag || selectedDiscount.type) === 'good_pass';
443
688
 
444
689
  // 需要拆分出来的数量
445
- var splitCount = isNeedSplit ? Math.min(product.quantity || product.num || 1, applicableDiscounts.filter(function (item) {
690
+ var totalQuantity = product.quantity || product.num || 1;
691
+ var availableGoodPassCount = applicableDiscounts.filter(function (item) {
446
692
  return (item.tag || item.type) === 'good_pass';
447
- }).length) : 1;
693
+ }).length;
694
+ var splitCount = isNeedSplit ? Math.min(totalQuantity, availableGoodPassCount) : 1;
448
695
  var arr = [];
449
- if (splitCount < product.quantity && isNeedSplit) {
450
- arr.push(_this3.hooks.setProduct(originProduct, {
451
- discount_list: [],
452
- quantity: product.quantity - splitCount,
453
- _id: product._id.split('___')[0]
454
- }));
455
- }
456
- for (var i = 0; i < splitCount; i++) {
457
- var _product$discount_lis11, _originProduct$_produ, _selectedDiscount$met;
458
- // 如果用过折扣卡,也就不存在拆分的情况了,这里直接使用上面计算出来的折扣卡
459
- var _selectedDiscount = selectedDiscountCard || applicableDiscounts[i];
460
- // 标记优惠券为已使用
461
- usedDiscounts.set(_selectedDiscount.id, true);
462
-
463
- // 记录实际应用了优惠券的商品信息
464
- var appliedProducts = appliedDiscountProducts.get(_selectedDiscount.id) || [];
465
-
466
- // 优先从 origin_total拿,可能会拿不到(比如用户端预约在没有配置 original_price 的情况下)
467
- var productOriginTotal = product.origin_total || product.total || 0;
468
- // 如果当前 product 有 discount_list,则先从 origin_total 拿
469
- if ((_product$discount_lis11 = product.discount_list) !== null && _product$discount_lis11 !== void 0 && _product$discount_lis11.length && product.origin_total) {
470
- productOriginTotal = product.origin_total;
471
- }
472
- // 如果originProduct?._productInit?.original_price为 0,product.origin_total可能为空,此时取 product.total
473
- if (Number((originProduct === null || originProduct === void 0 || (_originProduct$_produ = originProduct._productInit) === null || _originProduct$_produ === void 0 ? void 0 : _originProduct$_produ.original_price) || 0) > 0 && product.origin_total && product.total && product.origin_total !== product.total) {
474
- productOriginTotal = product.total;
475
- }
476
-
477
- // 计算使用折扣卡/商品券以后,单个商品的总 total
478
- var targetProductTotal = getDiscountAmount(_selectedDiscount, productOriginTotal, product.price);
479
- var discountDetail = {
480
- amount: new Decimal(productOriginTotal).minus(new Decimal(targetProductTotal)).toNumber(),
481
- type: _selectedDiscount.tag === 'product_discount_card' ? 'discount_card' : _selectedDiscount.tag,
482
- discount: {
483
- discount_card_type: _selectedDiscount === null || _selectedDiscount === void 0 || (_selectedDiscount$met = _selectedDiscount.metadata) === null || _selectedDiscount$met === void 0 ? void 0 : _selectedDiscount$met.discount_card_type,
484
- fixed_amount: new Decimal(productOriginTotal).minus(new Decimal(targetProductTotal)).toNumber(),
485
- resource_id: _selectedDiscount.id,
486
- title: _selectedDiscount.format_title,
487
- original_amount: productOriginTotal,
488
- product_id: originProduct.id,
489
- percent: _selectedDiscount.par_value
490
- },
491
- num: product.num || 1
492
- };
493
- appliedProducts.push(discountDetail);
494
- appliedDiscountProducts.set(_selectedDiscount.id, appliedProducts);
495
696
 
496
- // 记录应用了优惠券的商品
497
- // 后续更新价格改为 getProductTotalPrice getProductOriginTotalPrice逻辑
498
- if (product.isClient) {
697
+ // 🔥 主商品和bundle子商品分别处理
698
+ if (flatItem.type === 'main') {
699
+ // 主商品:保持原有逻辑
700
+ if (splitCount < totalQuantity && isNeedSplit) {
499
701
  arr.push(_this3.hooks.setProduct(originProduct, {
500
- discount_list: [discountDetail],
501
- price: _selectedDiscount.tag === 'good_pass' ? 0 : product.price,
502
- quantity: isNeedSplit ? 1 : product.quantity,
503
- origin_total: getProductOriginTotalPrice({
504
- product: {
505
- original_price: product.original_price
506
- },
507
- bundle: product.bundle,
508
- options: product.options
509
- }),
510
- variant: originProduct._productInit.variant,
511
- original_price: new Decimal(product.price || 0).toNumber(),
512
- total: targetProductTotal
702
+ discount_list: [],
703
+ quantity: totalQuantity - splitCount,
704
+ _id: product._id.split('___')[0]
513
705
  }));
706
+ }
707
+ for (var i = 0; i < splitCount; i++) {
708
+ var _product$discount_lis10, _originProduct, _selectedDiscount$met;
709
+ // 如果用过折扣卡,也就不存在拆分的情况了,这里直接使用上面计算出来的折扣卡
710
+ var _selectedDiscount = selectedDiscountCard || applicableDiscounts[i];
711
+ // 标记优惠券为已使用
712
+ usedDiscounts.set(_selectedDiscount.id, true);
713
+
714
+ // 记录实际应用了优惠券的商品信息
715
+ var appliedProducts = appliedDiscountProducts.get(_selectedDiscount.id) || [];
716
+
717
+ // 优先从 origin_total拿,可能会拿不到(比如用户端预约在没有配置 original_price 的情况下)
718
+ var productOriginTotal = product.origin_total || product.total || 0;
719
+ // 如果当前 product 有 discount_list,则先从 origin_total 拿
720
+ if ((_product$discount_lis10 = product.discount_list) !== null && _product$discount_lis10 !== void 0 && _product$discount_lis10.length && product.origin_total) {
721
+ productOriginTotal = product.origin_total;
722
+ }
723
+ // 如果originProduct?._productInit?.original_price为 0,product.origin_total可能为空,此时取 product.total
724
+ if (Number(((_originProduct = originProduct) === null || _originProduct === void 0 || (_originProduct = _originProduct._productInit) === null || _originProduct === void 0 ? void 0 : _originProduct.original_price) || 0) > 0 && product.origin_total && product.total && product.origin_total !== product.total) {
725
+ productOriginTotal = product.total;
726
+ }
727
+
728
+ // 计算使用折扣卡/商品券以后,单个商品的总 total
729
+ var targetProductTotal = getDiscountAmount(_selectedDiscount, product.price, product.price);
730
+ var discountType = _selectedDiscount.tag || _selectedDiscount.type;
731
+ var isGoodPass = discountType === 'good_pass';
732
+ var amount = new Decimal(product.price).minus(new Decimal(targetProductTotal)).toNumber();
733
+ var discountDetail = {
734
+ amount: amount,
735
+ type: _selectedDiscount.tag === 'product_discount_card' ? 'discount_card' : discountType,
736
+ discount: {
737
+ discount_card_type: _selectedDiscount === null || _selectedDiscount === void 0 || (_selectedDiscount$met = _selectedDiscount.metadata) === null || _selectedDiscount$met === void 0 ? void 0 : _selectedDiscount$met.discount_card_type,
738
+ fixed_amount: amount,
739
+ resource_id: _selectedDiscount.id,
740
+ title: _selectedDiscount.format_title,
741
+ original_amount: product.price,
742
+ product_id: originProduct.id,
743
+ percent: _selectedDiscount.par_value
744
+ },
745
+ // 前端使用的num数量,为了计算优惠金额
746
+ _num: isGoodPass ? 1 : product.num,
747
+ metadata: {
748
+ num: 1
749
+ }
750
+ };
751
+ appliedProducts.push(discountDetail);
752
+ appliedDiscountProducts.set(_selectedDiscount.id, appliedProducts);
753
+ var total = targetProductTotal;
754
+ if (product.options) {
755
+ total = product.options.reduce(function (accumulator, currentValue) {
756
+ var currentPrice = new Decimal(currentValue.price || 0);
757
+ var currentNum = new Decimal(currentValue.num || 0);
758
+ return accumulator.add(currentPrice.mul(currentNum));
759
+ }, new Decimal(total)).toNumber();
760
+ }
761
+
762
+ // 记录应用了优惠券的商品
763
+ // 后续更新价格改为 getProductTotalPrice getProductOriginTotalPrice逻辑
764
+ if (product.isClient) {
765
+ debugger;
766
+ arr.push(_this3.hooks.setProduct(originProduct, {
767
+ discount_list: [discountDetail],
768
+ price: _selectedDiscount.tag === 'good_pass' ? 0 : product.price,
769
+ quantity: isNeedSplit ? 1 : product.quantity,
770
+ origin_total: getProductOriginTotalPrice({
771
+ product: {
772
+ original_price: product.original_price
773
+ },
774
+ bundle: product.bundle,
775
+ options: product.options
776
+ }),
777
+ variant: originProduct._productInit.variant,
778
+ original_price: new Decimal(product.price || 0).toNumber(),
779
+ total: total
780
+ }));
781
+ } else {
782
+ arr.push(_this3.hooks.setProduct(originProduct, {
783
+ discount_list: [discountDetail],
784
+ _id: product._id.split('___')[0] + "___" + _selectedDiscount.id + index,
785
+ price: _selectedDiscount.tag === 'good_pass' ? 0 : product.price,
786
+ quantity: isNeedSplit ? 1 : product.quantity,
787
+ total: total,
788
+ origin_total: productOriginTotal,
789
+ main_product_selling_price: targetProductTotal
790
+ }));
791
+ }
792
+ }
793
+ processedProductsMap.set(product._id, arr);
794
+ } else {
795
+ // 🔥 bundle子商品:支持拆分
796
+ var processedItems = [];
797
+ if (isNeedSplit) {
798
+ // 商品券:需要拆分数量
799
+ var discountNum = splitCount;
800
+ var normalNum = totalQuantity - discountNum;
801
+
802
+ // 生成有折扣的商品(每张商品券对应 num: 1)
803
+ for (var _i = 0; _i < discountNum; _i++) {
804
+ var _selectedDiscount2 = applicableDiscounts[_i];
805
+ usedDiscounts.set(_selectedDiscount2.id, true);
806
+
807
+ // 🔥 生成唯一的 _id
808
+ var uniqueId = "".concat(flatItem._id, "_split_").concat(_i);
809
+ var _discountDetail = {
810
+ amount: product.origin_total,
811
+ type: 'good_pass',
812
+ discount: {
813
+ fixed_amount: product.origin_total,
814
+ resource_id: _selectedDiscount2.id,
815
+ title: _selectedDiscount2.format_title,
816
+ original_amount: product.origin_total,
817
+ product_id: product.id
818
+ },
819
+ metadata: {
820
+ // 🔥 使用拆分后的唯一 _id
821
+ custom_product_bundle_map_id: uniqueId,
822
+ num: 1
823
+ },
824
+ _num: 1
825
+ };
826
+
827
+ // 记录实际应用的折扣
828
+ var _appliedProducts = appliedDiscountProducts.get(_selectedDiscount2.id) || [];
829
+ _appliedProducts.push(_discountDetail);
830
+ appliedDiscountProducts.set(_selectedDiscount2.id, _appliedProducts);
831
+ processedItems.push(_objectSpread(_objectSpread({}, flatItem), {}, {
832
+ // 🔥 使用唯一的 _id
833
+ _id: uniqueId,
834
+ num: 1,
835
+ quantity: 1,
836
+ price: 0,
837
+ // 商品券价格为0
838
+ total: 0,
839
+ discount_list: [_discountDetail],
840
+ processed: true,
841
+ _discountId: _selectedDiscount2.id
842
+ }));
843
+ }
844
+
845
+ // 生成无折扣的商品(剩余数量)
846
+ if (normalNum > 0) {
847
+ processedItems.push(_objectSpread(_objectSpread({}, flatItem), {}, {
848
+ // 🔥 为剩余商品生成唯一的 _id
849
+ _id: "".concat(flatItem._id, "_split_rest"),
850
+ num: normalNum,
851
+ quantity: normalNum,
852
+ discount_list: [],
853
+ processed: true
854
+ }));
855
+ }
514
856
  } else {
515
- arr.push(_this3.hooks.setProduct(originProduct, {
516
- discount_list: [discountDetail],
517
- _id: product._id.split('___')[0] + "___" + _selectedDiscount.id + index,
518
- price: _selectedDiscount.tag === 'good_pass' ? 0 : product.price,
519
- quantity: isNeedSplit ? 1 : product.quantity,
520
- total: targetProductTotal,
521
- origin_total: productOriginTotal
857
+ var _selectedDiscount3$me, _flatItem$parentProdu3;
858
+ // 折扣卡:不拆分数量,直接应用
859
+ var _selectedDiscount3 = selectedDiscountCard || applicableDiscounts[0];
860
+ usedDiscounts.set(_selectedDiscount3.id, true);
861
+ var _productOriginTotal = product.original_price || product.price || 0;
862
+ var _targetProductTotal = getDiscountAmount(_selectedDiscount3, _productOriginTotal, _productOriginTotal);
863
+
864
+ // 🔥 使用当前的 _id 作为唯一标识
865
+ var _uniqueId = flatItem._id;
866
+ var _discountDetail2 = {
867
+ amount: new Decimal(_productOriginTotal).minus(_targetProductTotal).toNumber() * (product.num || 1),
868
+ type: _selectedDiscount3.tag === 'product_discount_card' ? 'discount_card' : _selectedDiscount3.tag,
869
+ discount: {
870
+ discount_card_type: _selectedDiscount3 === null || _selectedDiscount3 === void 0 || (_selectedDiscount3$me = _selectedDiscount3.metadata) === null || _selectedDiscount3$me === void 0 ? void 0 : _selectedDiscount3$me.discount_card_type,
871
+ fixed_amount: new Decimal(_productOriginTotal).minus(_targetProductTotal).toNumber(),
872
+ resource_id: _selectedDiscount3.id,
873
+ title: _selectedDiscount3.format_title,
874
+ original_amount: product.original_price,
875
+ product_id: product.id,
876
+ percent: _selectedDiscount3.par_value
877
+ },
878
+ metadata: {
879
+ // 🔥 使用唯一的 _id
880
+ custom_product_bundle_map_id: _uniqueId,
881
+ num: product.num || 1
882
+ },
883
+ _num: (product.num || 1) * ((flatItem === null || flatItem === void 0 || (_flatItem$parentProdu3 = flatItem.parentProduct) === null || _flatItem$parentProdu3 === void 0 ? void 0 : _flatItem$parentProdu3.num) || 1)
884
+ };
885
+
886
+ // 记录实际应用的折扣
887
+ var _appliedProducts2 = appliedDiscountProducts.get(_selectedDiscount3.id) || [];
888
+ _appliedProducts2.push(_discountDetail2);
889
+ appliedDiscountProducts.set(_selectedDiscount3.id, _appliedProducts2);
890
+ processedItems.push(_objectSpread(_objectSpread({}, flatItem), {}, {
891
+ total: _targetProductTotal,
892
+ price: new Decimal(_productOriginTotal || 0).minus(_discountDetail2.discount.fixed_amount).toNumber(),
893
+ discount_list: [_discountDetail2],
894
+ processed: true
522
895
  }));
523
896
  }
897
+ processedFlatItemsMap.set(flatItem._id, processedItems);
524
898
  }
525
- console.log(arr, 'arrarrarr');
526
- processedProductsMap.set(product._id, arr);
527
899
  });
528
900
 
529
- // 按原始顺序构建处理后的商品列表
530
- var processedProductList = [];
531
- productList.forEach(function (originProduct) {
532
- var product = _this3.hooks.getProduct(originProduct);
533
- var getDefaultProduct = function getDefaultProduct() {
534
- if (product.isClient) {
535
- return _this3.hooks.setProduct(originProduct, {
536
- discount_list: [],
537
- price: product.price,
538
- origin_total: getProductOriginTotalPrice({
539
- product: {
540
- original_price: product.original_price
541
- },
542
- bundle: product.bundle,
543
- options: product.options
544
- }),
545
- variant: originProduct._productInit.variant,
546
- original_price: originProduct._productInit.original_price,
547
- total: getProductTotalPrice({
548
- product: {
901
+ // 🔥 重组:将处理后的bundle子商品重新组装回主商品
902
+ var reconstructProductsWithBundle = function reconstructProductsWithBundle(processedProductsMap, processedFlatItemsMap, originalProductList) {
903
+ var result = [];
904
+ originalProductList.forEach(function (originProduct) {
905
+ var product = _this3.hooks.getProduct(originProduct);
906
+
907
+ // 获取主商品处理结果
908
+ var mainProductArr = processedProductsMap.get(product._id);
909
+ if (!mainProductArr || mainProductArr.length === 0) {
910
+ // 如果没有处理结果,返回默认商品
911
+ var getDefaultProduct = function getDefaultProduct() {
912
+ if (product.isClient) {
913
+ return _this3.hooks.setProduct(originProduct, {
914
+ discount_list: [],
915
+ price: product.price,
916
+ origin_total: getProductOriginTotalPrice({
917
+ product: {
918
+ original_price: product.original_price
919
+ },
920
+ bundle: product.bundle,
921
+ options: product.options
922
+ }),
923
+ variant: originProduct._productInit.variant,
924
+ original_price: originProduct._productInit.original_price,
925
+ total: getProductTotalPrice({
926
+ product: {
927
+ price: product.price
928
+ },
929
+ bundle: product.bundle,
930
+ options: product.options
931
+ })
932
+ });
933
+ } else {
934
+ return _this3.hooks.setProduct(originProduct, {
935
+ discount_list: [],
936
+ total: product.total,
937
+ origin_total: product.origin_total,
549
938
  price: product.price
550
- },
551
- bundle: product.bundle,
552
- options: product.options
553
- })
554
- });
939
+ });
940
+ }
941
+ };
942
+ result.push(getDefaultProduct());
943
+ return;
944
+ }
945
+
946
+ // 检查是否有bundle子商品需要重组
947
+ var hasBundle = product.bundle && Array.isArray(product.bundle) && product.bundle.length > 0;
948
+ if (!hasBundle) {
949
+ // 没有bundle,直接使用主商品处理结果
950
+ result.push.apply(result, _toConsumableArray(mainProductArr));
555
951
  } else {
556
- return _this3.hooks.setProduct(originProduct, {
557
- discount_list: [],
558
- total: product.total,
559
- origin_total: product.origin_total,
560
- price: product.price
561
- });
952
+ // 🔥 有bundle,需要检查子商品是否应用了商品券
953
+ // 1. 先收集所有bundle子商品的处理结果
954
+ var bundleProcessingInfo = new Map();
955
+ var hasGoodPassApplied = false; // 标记是否有子商品应用了商品券
956
+
957
+ if (product.bundle && Array.isArray(product.bundle)) {
958
+ product.bundle.forEach(function (bundleItem, bundleIndex) {
959
+ var bundleItemId = "".concat(product._id, "_bundle_").concat(bundleIndex);
960
+ var processedBundleItems = processedFlatItemsMap.get(bundleItemId);
961
+ if (!processedBundleItems || processedBundleItems.length === 0) {
962
+ // 未处理的bundle item,保持原样
963
+ bundleProcessingInfo.set(bundleIndex, [bundleItem]);
964
+ } else {
965
+ // 处理过的bundle item
966
+ bundleProcessingInfo.set(bundleIndex, processedBundleItems);
967
+ // 🔥 检查是否应用了商品券(good_pass)
968
+ var hasGoodPass = processedBundleItems.some(function (item) {
969
+ var _item$discount_list;
970
+ return (_item$discount_list = item.discount_list) === null || _item$discount_list === void 0 ? void 0 : _item$discount_list.some(function (discount) {
971
+ return discount.type === 'good_pass' || discount.tag === 'good_pass';
972
+ });
973
+ });
974
+ if (hasGoodPass) {
975
+ hasGoodPassApplied = true;
976
+ }
977
+ }
978
+ });
979
+ }
980
+
981
+ // 🔥 2. 如果有子商品应用了商品券,且主商品数量大于1,需要拆分主商品
982
+ var mainProductQuantity = mainProductArr[0] ? _this3.hooks.getProduct(mainProductArr[0]).quantity : 1;
983
+ if (hasGoodPassApplied && mainProductArr.length === 1 && mainProductQuantity > 1) {
984
+ var mainProduct = mainProductArr[0];
985
+ var mainProductData = _this3.hooks.getProduct(mainProduct);
986
+
987
+ // 🔥 方案:拆分成2个主商品
988
+ // 1. 一个主商品(qty=1)包含拆分后的bundle
989
+ // 2. 一个主商品(qty=原quantity-1)包含原始未拆分的bundle
990
+
991
+ // 第一个:包含拆分后的bundle (qty=1)
992
+ var newBundleWithDiscount = [];
993
+ (product.bundle || []).forEach(function (bundleItem, bundleIndex) {
994
+ var processedItems = bundleProcessingInfo.get(bundleIndex) || [bundleItem];
995
+ if (processedItems.length > 1) {
996
+ // 被拆分的子商品,添加所有拆分项
997
+ processedItems.forEach(function (item) {
998
+ // 🔥 更新 discount_list 中的 num,使其与拆分后的 item.num 一致
999
+ var updatedDiscountList = (item.discount_list || []).map(function (discount) {
1000
+ var _item$metadata;
1001
+ return _objectSpread(_objectSpread({}, discount), {}, {
1002
+ metadata: {
1003
+ num: item.num,
1004
+ custom_product_bundle_map_id: (_item$metadata = item.metadata) === null || _item$metadata === void 0 ? void 0 : _item$metadata.custom_product_bundle_map_id
1005
+ }
1006
+ // num: item.num, // 使用拆分后的 num
1007
+ });
1008
+ });
1009
+ newBundleWithDiscount.push(_objectSpread(_objectSpread({}, bundleItem), {}, {
1010
+ _id: item._id,
1011
+ product_id: bundleItem.product_id,
1012
+ price: item.price,
1013
+ num: item.num,
1014
+ discount_list: updatedDiscountList
1015
+ }));
1016
+ });
1017
+ } else {
1018
+ // 未拆分的子商品,保持原样
1019
+ var item = processedItems[0];
1020
+ if (item.processed) {
1021
+ // 🔥 同样需要更新 discount_list 中的 num
1022
+ var _updatedDiscountList = (item.discount_list || []).map(function (discount) {
1023
+ var _item$metadata2;
1024
+ return _objectSpread(_objectSpread({}, discount), {}, {
1025
+ metadata: {
1026
+ num: item.num,
1027
+ custom_product_bundle_map_id: (_item$metadata2 = item.metadata) === null || _item$metadata2 === void 0 ? void 0 : _item$metadata2.custom_product_bundle_map_id
1028
+ }
1029
+ // num: item.num, // 使用当前的 num
1030
+ });
1031
+ });
1032
+ newBundleWithDiscount.push(_objectSpread(_objectSpread({}, bundleItem), {}, {
1033
+ _id: item._id,
1034
+ product_id: bundleItem.product_id,
1035
+ price: item.price,
1036
+ num: item.num,
1037
+ discount_list: _updatedDiscountList
1038
+ }));
1039
+ } else {
1040
+ newBundleWithDiscount.push(item);
1041
+ }
1042
+ }
1043
+ });
1044
+
1045
+ // 计算第一个主商品的总价(包含拆分后的bundle)
1046
+ var newTotalWithDiscount = Number(mainProductData.price || 0);
1047
+ var newOriginTotalWithDiscount = Number(mainProductData.original_price || mainProductData.price || 0);
1048
+
1049
+ // 🔥 更新主商品自己的 discount_list 中的 num(quantity=1)
1050
+ var updatedMainDiscountList = mainProductData.discount_list.map(function (discount) {
1051
+ var _discount$metadata2, _discount$metadata3;
1052
+ if (discount !== null && discount !== void 0 && (_discount$metadata2 = discount.metadata) !== null && _discount$metadata2 !== void 0 && _discount$metadata2.custom_product_bundle_map_id) {
1053
+ // bundle的discount_list保持不变
1054
+ return discount;
1055
+ }
1056
+ // 主商品自己的discount,更新num为1
1057
+ return _objectSpread(_objectSpread({}, discount), {}, {
1058
+ // num: 1,
1059
+ metadata: {
1060
+ custom_product_bundle_map_id: discount === null || discount === void 0 || (_discount$metadata3 = discount.metadata) === null || _discount$metadata3 === void 0 ? void 0 : _discount$metadata3.custom_product_bundle_map_id,
1061
+ num: 1
1062
+ }
1063
+ });
1064
+ });
1065
+
1066
+ // 🔥 使用更新后的列表计算折扣金额
1067
+ var mainDiscountList = updatedMainDiscountList.filter(function (item) {
1068
+ var _item$metadata3;
1069
+ return !(item !== null && item !== void 0 && (_item$metadata3 = item.metadata) !== null && _item$metadata3 !== void 0 && _item$metadata3.custom_product_bundle_map_id);
1070
+ });
1071
+ if (mainDiscountList && mainDiscountList.length > 0) {
1072
+ var _Decimal$minus$toNumb, _mainProductData$orig;
1073
+ var allDiscountAmount = getDiscountListAmountTotal(mainDiscountList);
1074
+ newTotalWithDiscount = (_Decimal$minus$toNumb = new Decimal(mainProductData.price || 0).minus(allDiscountAmount).toNumber()) !== null && _Decimal$minus$toNumb !== void 0 ? _Decimal$minus$toNumb : newTotalWithDiscount;
1075
+ newOriginTotalWithDiscount = (_mainProductData$orig = mainProductData.origin_total) !== null && _mainProductData$orig !== void 0 ? _mainProductData$orig : newOriginTotalWithDiscount;
1076
+ if (newBundleWithDiscount.length > 0) {
1077
+ newBundleWithDiscount.forEach(function (item) {
1078
+ var _item$discount_list2;
1079
+ newTotalWithDiscount += Number(item.price) * Number(item.num);
1080
+ var originalPrice = ((_item$discount_list2 = item.discount_list) === null || _item$discount_list2 === void 0 || (_item$discount_list2 = _item$discount_list2[0]) === null || _item$discount_list2 === void 0 || (_item$discount_list2 = _item$discount_list2.discount) === null || _item$discount_list2 === void 0 ? void 0 : _item$discount_list2.original_amount) || item.price;
1081
+ newOriginTotalWithDiscount += Number(originalPrice) * Number(item.num);
1082
+ });
1083
+ }
1084
+ }
1085
+
1086
+ // 累加options的价格(options不参与折扣,在最终设置total时处理)
1087
+ if (product !== null && product !== void 0 && product.options) {
1088
+ newTotalWithDiscount = product.options.reduce(function (accumulator, currentValue) {
1089
+ var currentPrice = new Decimal(currentValue.price || 0);
1090
+ var currentNum = new Decimal(currentValue.num || 0);
1091
+ return accumulator.add(currentPrice.mul(currentNum));
1092
+ }, new Decimal(newTotalWithDiscount)).toNumber();
1093
+ }
1094
+
1095
+ // 添加第一个主商品:qty=1,包含拆分后的bundle
1096
+ result.push(_this3.hooks.setProduct(mainProduct, _objectSpread(_objectSpread({}, mainProductData), {}, {
1097
+ _id: "".concat(mainProductData._id.split('___')[0], "___split_discount"),
1098
+ quantity: 1,
1099
+ discount_list: updatedMainDiscountList,
1100
+ bundle: newBundleWithDiscount,
1101
+ total: newTotalWithDiscount,
1102
+ origin_total: newOriginTotalWithDiscount
1103
+ })));
1104
+
1105
+ // 第二个:包含原始bundle (qty=原quantity-1)
1106
+ if (mainProductQuantity > 1) {
1107
+ var newBundleOriginal = [];
1108
+ (product.bundle || []).forEach(function (bundleItem, bundleIndex) {
1109
+ // 使用原始的bundle配置,不包含拆分
1110
+ newBundleOriginal.push(_objectSpread(_objectSpread({}, bundleItem), {}, {
1111
+ discount_list: []
1112
+ }));
1113
+ });
1114
+
1115
+ // 计算第二个主商品的总价
1116
+ var newTotalOriginal = Number(mainProductData.price || 0);
1117
+ var newOriginTotalOriginal = Number(mainProductData.original_price || mainProductData.price || 0);
1118
+
1119
+ // 🔥 更新主商品自己的 discount_list 中的 num(quantity=原quantity-1)
1120
+ var updatedMainDiscountListOriginal = mainProductData.discount_list.map(function (discount) {
1121
+ var _discount$metadata4, _discount$metadata5;
1122
+ if (discount !== null && discount !== void 0 && (_discount$metadata4 = discount.metadata) !== null && _discount$metadata4 !== void 0 && _discount$metadata4.custom_product_bundle_map_id) {
1123
+ // bundle的discount_list保持不变
1124
+ return discount;
1125
+ }
1126
+ // 主商品自己的discount,更新num为 mainProductQuantity - 1
1127
+ return _objectSpread(_objectSpread({}, discount), {}, {
1128
+ metadata: {
1129
+ num: mainProductQuantity - 1,
1130
+ custom_product_bundle_map_id: discount === null || discount === void 0 || (_discount$metadata5 = discount.metadata) === null || _discount$metadata5 === void 0 ? void 0 : _discount$metadata5.custom_product_bundle_map_id
1131
+ }
1132
+ // num: mainProductQuantity - 1,
1133
+ });
1134
+ });
1135
+
1136
+ // 🔥 使用更新后的列表计算折扣金额
1137
+ var mainDiscountListOriginal = updatedMainDiscountListOriginal.filter(function (item) {
1138
+ var _item$metadata4;
1139
+ return !(item !== null && item !== void 0 && (_item$metadata4 = item.metadata) !== null && _item$metadata4 !== void 0 && _item$metadata4.custom_product_bundle_map_id);
1140
+ });
1141
+ if (mainDiscountListOriginal && mainDiscountListOriginal.length > 0) {
1142
+ var _Decimal$minus$toNumb2, _mainProductData$orig2;
1143
+ var _allDiscountAmount = getDiscountListAmount(mainDiscountListOriginal);
1144
+ newTotalOriginal = (_Decimal$minus$toNumb2 = new Decimal(mainProductData.price || 0).minus(_allDiscountAmount).toNumber()) !== null && _Decimal$minus$toNumb2 !== void 0 ? _Decimal$minus$toNumb2 : newTotalOriginal;
1145
+ newOriginTotalOriginal = (_mainProductData$orig2 = mainProductData.origin_total) !== null && _mainProductData$orig2 !== void 0 ? _mainProductData$orig2 : newOriginTotalOriginal;
1146
+ if (newBundleOriginal.length > 0) {
1147
+ newBundleOriginal.forEach(function (item) {
1148
+ newTotalOriginal += Number(item.price) * Number(item.num);
1149
+ newOriginTotalOriginal += Number(item.price) * Number(item.num);
1150
+ });
1151
+ }
1152
+ }
1153
+
1154
+ // 累加options的价格(options不参与折扣,在最终设置total时处理)
1155
+ if (product !== null && product !== void 0 && product.options) {
1156
+ newTotalOriginal = product.options.reduce(function (accumulator, currentValue) {
1157
+ var currentPrice = new Decimal(currentValue.price || 0);
1158
+ var currentNum = new Decimal(currentValue.num || 0);
1159
+ return accumulator.add(currentPrice.mul(currentNum));
1160
+ }, new Decimal(newTotalOriginal)).toNumber();
1161
+ }
1162
+
1163
+ // 添加第二个主商品:qty=原quantity-1,包含原始bundle
1164
+ result.push(_this3.hooks.setProduct(mainProduct, _objectSpread(_objectSpread({}, mainProductData), {}, {
1165
+ _id: "".concat(mainProductData._id.split('___')[0], "___split_normal"),
1166
+ quantity: mainProductQuantity - 1,
1167
+ discount_list: updatedMainDiscountListOriginal,
1168
+ bundle: newBundleOriginal,
1169
+ total: newTotalOriginal,
1170
+ origin_total: newOriginTotalOriginal
1171
+ })));
1172
+ }
1173
+ } else {
1174
+ // 🔥 没有子商品被拆分,使用原有逻辑
1175
+ mainProductArr.forEach(function (mainProduct) {
1176
+ var _mainProductData$disc, _mainProductData$disc2, _mainProductData$disc3;
1177
+ var mainProductData = _this3.hooks.getProduct(mainProduct);
1178
+
1179
+ // 重组bundle
1180
+ var newBundle = [];
1181
+ if (product.bundle && Array.isArray(product.bundle)) {
1182
+ product.bundle.forEach(function (bundleItem, bundleIndex) {
1183
+ var bundleItemId = "".concat(product._id, "_bundle_").concat(bundleIndex);
1184
+ var processedBundleItems = processedFlatItemsMap.get(bundleItemId);
1185
+ if (!processedBundleItems || processedBundleItems.length === 0) {
1186
+ // 未处理的bundle item,保持原样
1187
+ newBundle.push(bundleItem);
1188
+ } else {
1189
+ // 🔥 关键:拆分后的bundle item
1190
+ processedBundleItems.forEach(function (item) {
1191
+ // 🔥 更新 discount_list 中的 num,使其与拆分后的 item.num 一致
1192
+ var updatedDiscountList = (item.discount_list || []).map(function (discount) {
1193
+ var _discount$metadata6;
1194
+ return _objectSpread(_objectSpread({}, discount), {}, {
1195
+ metadata: {
1196
+ num: item.num,
1197
+ custom_product_bundle_map_id: discount === null || discount === void 0 || (_discount$metadata6 = discount.metadata) === null || _discount$metadata6 === void 0 ? void 0 : _discount$metadata6.custom_product_bundle_map_id
1198
+ }
1199
+ // num: item.num, // 使用拆分后的 num
1200
+ });
1201
+ });
1202
+ newBundle.push(_objectSpread(_objectSpread({}, bundleItem), {}, {
1203
+ _id: item._id,
1204
+ product_id: bundleItem.product_id,
1205
+ price: item.price,
1206
+ num: item.num,
1207
+ discount_list: updatedDiscountList
1208
+ }));
1209
+ });
1210
+ }
1211
+ });
1212
+ }
1213
+
1214
+ // 🔥 重新计算主商品的总价(包含bundle)
1215
+ var newTotal = Number(mainProductData.price || 0);
1216
+ var newOriginTotal = Number(mainProductData.original_price || mainProductData.price || 0);
1217
+
1218
+ // 判断是否是手动折扣
1219
+ var isManualDiscount = typeof mainProductData.isManualDiscount === 'boolean' ? mainProductData.isManualDiscount : mainProductData.total != mainProductData.origin_total && (!((_mainProductData$disc = mainProductData.discount_list) !== null && _mainProductData$disc !== void 0 && _mainProductData$disc.length) || (mainProductData === null || mainProductData === void 0 || (_mainProductData$disc2 = mainProductData.discount_list) === null || _mainProductData$disc2 === void 0 || (_mainProductData$disc3 = _mainProductData$disc2.every) === null || _mainProductData$disc3 === void 0 ? void 0 : _mainProductData$disc3.call(_mainProductData$disc2, function (item) {
1220
+ return item.type === 'product';
1221
+ })));
1222
+
1223
+ // 如果是手动折扣,使用原有的total和origin_total,不重新计算
1224
+ if (isManualDiscount) {
1225
+ var _mainProductData$tota, _mainProductData$orig3;
1226
+ newTotal = (_mainProductData$tota = mainProductData.total) !== null && _mainProductData$tota !== void 0 ? _mainProductData$tota : newTotal;
1227
+ newOriginTotal = (_mainProductData$orig3 = mainProductData.origin_total) !== null && _mainProductData$orig3 !== void 0 ? _mainProductData$orig3 : newOriginTotal;
1228
+ } else {
1229
+ // 不是手动折扣时,才重新计算
1230
+ var _mainDiscountList = mainProductData.discount_list.filter(function (item) {
1231
+ var _item$metadata5;
1232
+ return !(item !== null && item !== void 0 && (_item$metadata5 = item.metadata) !== null && _item$metadata5 !== void 0 && _item$metadata5.custom_product_bundle_map_id);
1233
+ });
1234
+
1235
+ // 如果主商品本身有折扣,需要重新计算主商品价格
1236
+ if (_mainDiscountList && _mainDiscountList.length > 0) {
1237
+ var _Decimal$minus$toNumb3, _mainProductData$orig4;
1238
+ var _allDiscountAmount2 = getDiscountListAmount(_mainDiscountList);
1239
+ newTotal = (_Decimal$minus$toNumb3 = new Decimal(mainProductData.price || 0).minus(_allDiscountAmount2).toNumber()) !== null && _Decimal$minus$toNumb3 !== void 0 ? _Decimal$minus$toNumb3 : newTotal;
1240
+ newOriginTotal = (_mainProductData$orig4 = mainProductData.origin_total) !== null && _mainProductData$orig4 !== void 0 ? _mainProductData$orig4 : newOriginTotal;
1241
+ }
1242
+
1243
+ // 累加bundle的价格(只累加一次)
1244
+ if (newBundle.length > 0) {
1245
+ newBundle.forEach(function (item) {
1246
+ var _item$discount_list3;
1247
+ newTotal += Number(item.price) * Number(item.num);
1248
+ 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;
1249
+ newOriginTotal += Number(originalPrice) * Number(item.num);
1250
+ });
1251
+ }
1252
+ }
1253
+
1254
+ // 累加options的价格(options不参与折扣,在最终设置total时处理)
1255
+ if (product !== null && product !== void 0 && product.options) {
1256
+ newTotal = product.options.reduce(function (accumulator, currentValue) {
1257
+ var currentPrice = new Decimal(currentValue.price || 0);
1258
+ var currentNum = new Decimal(currentValue.num || 0);
1259
+ return accumulator.add(currentPrice.mul(currentNum));
1260
+ }, new Decimal(newTotal)).toNumber();
1261
+ }
1262
+
1263
+ // 生成最终的主商品
1264
+ result.push(_this3.hooks.setProduct(mainProduct, _objectSpread(_objectSpread({}, mainProductData), {}, {
1265
+ bundle: newBundle,
1266
+ total: newTotal,
1267
+ origin_total: newOriginTotal
1268
+ })));
1269
+ });
1270
+ }
562
1271
  }
563
- };
564
- var arr = processedProductsMap.get(product._id);
565
- arr !== null && arr !== void 0 && arr.length ? processedProductList.push.apply(processedProductList, _toConsumableArray(arr)) : processedProductList.push(getDefaultProduct());
566
- });
1272
+ });
1273
+ return result;
1274
+ };
1275
+
1276
+ // 按原始顺序构建处理后的商品列表
1277
+ var processedProductList = reconstructProductsWithBundle(processedProductsMap, processedFlatItemsMap, productList);
567
1278
 
568
1279
  // 按原始顺序更新优惠券列表,标记已使用和可用的优惠券
569
1280
  var updatedDiscountList = addModeDiscount.map(function (discount) {
@@ -634,6 +1345,145 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
634
1345
  discountList: [].concat(editModeDiscount, _toConsumableArray(updatedDiscountList))
635
1346
  };
636
1347
  }
1348
+
1349
+ /**
1350
+ * 检查优惠是否符合 PackageSubItemUsageRules 配置
1351
+ * @param discount 优惠券
1352
+ * @param flatItem 扁平化后的商品项(可能是主商品或bundle子商品)
1353
+ * @returns 是否可用
1354
+ */
1355
+ }, {
1356
+ key: "checkPackageSubItemUsageRules",
1357
+ value: function checkPackageSubItemUsageRules(discount, flatItem) {
1358
+ var limitedData = discount.limited_relation_product_data;
1359
+ var usageRules = limitedData === null || limitedData === void 0 ? void 0 : limitedData.package_sub_item_usage_rules;
1360
+ var rules = ['original_price'].concat(_toConsumableArray((usageRules === null || usageRules === void 0 ? void 0 : usageRules.rules) || []));
1361
+
1362
+ // 如果没有配置 package_sub_item_usage_rules,则默认可用
1363
+ if (!usageRules) {
1364
+ return true;
1365
+ }
1366
+ var ruleType = usageRules.type;
1367
+ var isMainProduct = flatItem.type === 'main'; // 单独购买
1368
+ var isBundleItem = flatItem.type === 'bundle'; // 套餐中购买
1369
+
1370
+ // 主商品直接可用
1371
+ if (isMainProduct) {
1372
+ return true;
1373
+ }
1374
+
1375
+ // universal_discount: 单独购买和套餐中(子商品为原价)均可使用
1376
+ if (ruleType === 'universal_discount') {
1377
+ if (isMainProduct) {
1378
+ return true; // 单独购买时可用
1379
+ }
1380
+ if (isBundleItem) {
1381
+ var _flatItem$bundleItem5, _flatItem$bundleItem6;
1382
+ // 套餐中购买时,判断是否为原价
1383
+ var priceType = (_flatItem$bundleItem5 = flatItem.bundleItem) === null || _flatItem$bundleItem5 === void 0 ? void 0 : _flatItem$bundleItem5.price_type;
1384
+ var priceTypeExt = (_flatItem$bundleItem6 = flatItem.bundleItem) === null || _flatItem$bundleItem6 === void 0 ? void 0 : _flatItem$bundleItem6.price_type_ext;
1385
+ // original_price 对应:
1386
+ // 1. price_type: "markup" && price_type_ext: "product_price"
1387
+ // markup_price 对应:
1388
+ // price_type: "markup" && price_type_ext: ""
1389
+ /** 原价 */
1390
+ var isOriginalPrice = priceType === 'markup' && priceTypeExt === 'product_price';
1391
+ /** 加价 */
1392
+ var isMarkupPrice = priceType === 'markup' && (priceTypeExt === '' || !priceTypeExt);
1393
+
1394
+ // 检查 rules
1395
+ if (rules.length > 0) {
1396
+ // 检查原价
1397
+ if (isOriginalPrice && rules.includes('original_price')) {
1398
+ return true;
1399
+ }
1400
+
1401
+ // 检查加价
1402
+ if (isMarkupPrice && rules.includes('markup_price')) {
1403
+ return true;
1404
+ }
1405
+
1406
+ // 如果都不匹配,则不可用
1407
+ return false;
1408
+ }
1409
+
1410
+ // 原价包括:price_type: "markup" && price_type_ext: "product_price"
1411
+ return isOriginalPrice;
1412
+ }
1413
+ return true;
1414
+ }
1415
+
1416
+ // package_exclusive: 仅在套餐中(子商品为原价)时可使用
1417
+ if (ruleType === 'package_exclusive') {
1418
+ if (isMainProduct) {
1419
+ return false; // 单独购买时不可用
1420
+ }
1421
+ if (isBundleItem) {
1422
+ var _flatItem$bundleItem7, _flatItem$bundleItem8;
1423
+ // 套餐中购买时,判断是否为原价
1424
+ var _priceType = (_flatItem$bundleItem7 = flatItem.bundleItem) === null || _flatItem$bundleItem7 === void 0 ? void 0 : _flatItem$bundleItem7.price_type;
1425
+ var _priceTypeExt = (_flatItem$bundleItem8 = flatItem.bundleItem) === null || _flatItem$bundleItem8 === void 0 ? void 0 : _flatItem$bundleItem8.price_type_ext;
1426
+ // 原价包括:price_type: "markup" && price_type_ext: "product_price"
1427
+ return _priceType === 'markup' && _priceTypeExt === 'product_price';
1428
+ }
1429
+ return false;
1430
+ }
1431
+
1432
+ // single_item_promo: 仅在单独购买时可使用
1433
+ if (ruleType === 'single_item_promo') {
1434
+ return isMainProduct; // 只有单独购买时可用
1435
+ }
1436
+
1437
+ /*
1438
+ // custom_usage_rules: 根据自定义规则判断
1439
+ if (ruleType === 'custom_usage_rules') {
1440
+ const customRules = usageRules.custom_usage_rules;
1441
+ if (!customRules) {
1442
+ return true; // 如果没有自定义规则,默认可用
1443
+ }
1444
+ const { types, package_sub_item_rules } = customRules;
1445
+ // 判断商品类型是否在允许的类型中
1446
+ if (isMainProduct) {
1447
+ // 主商品对应 standalone_product
1448
+ if (!types.includes('standalone_product')) {
1449
+ return false;
1450
+ }
1451
+ return true; // 主商品不需要判断 package_sub_item_rules
1452
+ }
1453
+ if (isBundleItem) {
1454
+ // bundle子商品对应 package_sub_item
1455
+ if (!types.includes('package_sub_item')) {
1456
+ return false;
1457
+ }
1458
+ // 如果包含 package_sub_item,还需要判断 price_type
1459
+ const priceType = flatItem.bundleItem?.price_type;
1460
+ const priceTypeExt = flatItem.bundleItem?.price_type_ext;
1461
+ // 检查 package_sub_item_rules
1462
+ if (package_sub_item_rules && package_sub_item_rules.length > 0) {
1463
+ // original_price 对应:
1464
+ // 1. price_type: "markup" && price_type_ext: "product_price"
1465
+ // markup_price 对应:
1466
+ // price_type: "markup" && price_type_ext: ""
1467
+ const isOriginalPrice = (priceType === 'markup' && priceTypeExt === 'product_price');
1468
+ const isMarkupPrice = priceType === 'markup' && (priceTypeExt === '' || !priceTypeExt);
1469
+ if (isOriginalPrice && package_sub_item_rules.includes('original_price')) {
1470
+ return true;
1471
+ }
1472
+ if (isMarkupPrice && package_sub_item_rules.includes('markup_price')) {
1473
+ return true;
1474
+ }
1475
+ // 如果都不匹配,则不可用
1476
+ return false;
1477
+ }
1478
+ return true; // 如果没有 package_sub_item_rules 配置,默认可用
1479
+ }
1480
+ return true;
1481
+ }
1482
+ * */
1483
+
1484
+ // 默认可用
1485
+ return true;
1486
+ }
637
1487
  }, {
638
1488
  key: "destroy",
639
1489
  value: function () {