@pisell/pisellos 2.1.46 → 2.1.47

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