@pisell/pisellos 1.0.77 → 1.0.78

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 } from "../../solution/ShopDiscount/utils";
27
+ import { uniqueById, getDiscountAmount, 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';
@@ -187,6 +187,55 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
187
187
  return !discount.isManualSelect;
188
188
  });
189
189
 
190
+ // 🔥 扁平化商品列表:将 bundle 子商品展开为虚拟商品
191
+ var flattenProductsWithBundle = function flattenProductsWithBundle(productList) {
192
+ var flattened = [];
193
+ productList.forEach(function (originProduct) {
194
+ var product = _this3.hooks.getProduct(originProduct);
195
+
196
+ // 1. 添加主商品
197
+ flattened.push({
198
+ type: 'main',
199
+ originProduct: originProduct,
200
+ product: product,
201
+ price: Number(product.price || 0),
202
+ id: product.id,
203
+ _id: product._id,
204
+ parentId: product._id,
205
+ quantity: product.quantity,
206
+ num: product.num
207
+ });
208
+
209
+ // 2. 展开 bundle 子商品
210
+ if (product.bundle && Array.isArray(product.bundle) && product.bundle.length > 0) {
211
+ product.bundle.forEach(function (bundleItem, bundleIndex) {
212
+ flattened.push({
213
+ type: 'bundle',
214
+ originProduct: originProduct,
215
+ parentProduct: product,
216
+ bundleItem: bundleItem,
217
+ bundleIndex: bundleIndex,
218
+ // 虚拟商品属性
219
+ price: Number(bundleItem.price || 0),
220
+ id: bundleItem._bundle_product_id,
221
+ // 🔥 使用 _bundle_product_id
222
+ _id: "".concat(product._id, "_bundle_").concat(bundleIndex),
223
+ parentId: product._id,
224
+ num: bundleItem.num || 1,
225
+ quantity: bundleItem.num || 1,
226
+ total: new Decimal(bundleItem.price || 0).mul(bundleItem.num || 1).toNumber(),
227
+ origin_total: new Decimal(bundleItem.price || 0).mul(bundleItem.num || 1).toNumber(),
228
+ original_price: bundleItem.original_price,
229
+ // 继承主商品属性
230
+ booking_id: product.booking_id,
231
+ discount_list: []
232
+ });
233
+ });
234
+ }
235
+ });
236
+ return flattened;
237
+ };
238
+
190
239
  // 优惠力度排序,传进来的数据里可能有商品券,也可能有优惠券
191
240
  // 1. 商品券(n.tag=good_pass)视为最优惠(免费)
192
241
  // 2. 折扣券(n.tag=product_discount_card)按照新的优先级排序:
@@ -222,12 +271,12 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
222
271
  }
223
272
  }
224
273
 
225
- // 3. 都是百分比时,折扣越大越优先(par_value越小越优先)
274
+ // 3. 都是百分比时,折扣越大越优先(par_value越大越优先)
226
275
  if (typeA === 'percent' && typeB === 'percent') {
227
276
  if (a.par_value !== b.par_value) {
228
277
  var _valueA = new Decimal(100).minus(a.par_value || 0);
229
278
  var _valueB = new Decimal(100).minus(b.par_value || 0);
230
- return _valueB.minus(_valueA).toNumber(); // 折扣大的在前
279
+ return _valueA.minus(_valueB).toNumber(); // 折扣大的在前
231
280
  }
232
281
  }
233
282
 
@@ -261,21 +310,50 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
261
310
  // }
262
311
  // })
263
312
 
313
+ // 🔥 扁平化商品列表(包含主商品和bundle子商品)
314
+ var flattenedList = flattenProductsWithBundle(productList);
315
+
316
+ // 对扁平化后的列表按价格降序排序(用于应用优惠券时优先选择高价商品)
317
+ var sortedFlattenedList = flattenedList.sort(function (a, b) {
318
+ var priceA = new Decimal(a.price || '0');
319
+ var priceB = new Decimal(b.price || '0');
320
+ if (priceA.equals(priceB)) {
321
+ // 价格相同时,主商品优先
322
+ if (a.type !== b.type) {
323
+ return a.type === 'main' ? -1 : 1;
324
+ }
325
+ // 都是主商品时,按原有逻辑排序
326
+ if (a.type === 'main' && b.type === 'main') {
327
+ if (a.product.quantity === b.product.quantity) {
328
+ var _b$product$discount_l, _a$product$discount_l;
329
+ 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);
330
+ }
331
+ return a.product.quantity - b.product.quantity;
332
+ }
333
+ // 都是bundle时,按数量排序
334
+ if (a.type === 'bundle' && b.type === 'bundle') {
335
+ return (a.num || 1) - (b.num || 1);
336
+ }
337
+ }
338
+ return priceB.minus(priceA).toNumber();
339
+ });
340
+
341
+ /**
264
342
  // 对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');
343
+ const sortedProductList = [...productList].sort((a, b) => {
344
+ const aProduct = this.hooks.getProduct(a);
345
+ const bProduct = this.hooks.getProduct(b);
346
+ const priceA = new Decimal((aProduct.price as string) || '0');
347
+ const priceB = new Decimal((bProduct.price as string) || '0');
270
348
  if (priceA.toNumber() === priceB.toNumber()) {
271
349
  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);
350
+ return bProduct.discount_list?.length - aProduct.discount_list?.length;
274
351
  }
275
352
  return aProduct.quantity - bProduct.quantity;
276
353
  }
277
354
  return priceB.toNumber() - priceA.toNumber();
278
355
  });
356
+ */
279
357
 
280
358
  // 标记已使用的优惠券
281
359
  var usedDiscounts = new Map();
@@ -299,19 +377,39 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
299
377
  var appliedDiscountProducts = new Map();
300
378
 
301
379
  // 首先遍历所有商品和所有优惠券,确定每个优惠券的适用范围,包括isSelected为false的优惠券
302
- sortedProductList.forEach(function (originProduct) {
303
- var product = _this3.hooks.getProduct(originProduct);
380
+ // 🔥 使用扁平化后的列表,包含主商品和bundle子商品
381
+ sortedFlattenedList.forEach(function (flatItem) {
382
+ // 获取商品数据
383
+ var product, originProduct;
384
+ if (flatItem.type === 'main') {
385
+ product = flatItem.product;
386
+ originProduct = flatItem.originProduct;
387
+ } else {
388
+ // bundle子商品:构造虚拟商品对象
389
+ product = {
390
+ _id: flatItem._id,
391
+ id: flatItem.id,
392
+ price: flatItem.price,
393
+ quantity: flatItem.quantity,
394
+ num: flatItem.num,
395
+ total: flatItem.total,
396
+ origin_total: flatItem.origin_total,
397
+ booking_id: flatItem.booking_id,
398
+ discount_list: []
399
+ };
400
+ originProduct = flatItem.originProduct;
401
+ }
304
402
  addModeDiscount.forEach(function (discount) {
305
- var _product$discount_lis2, _product$discount_lis3;
403
+ var _product, _product2, _product3, _flatItem$bundleItem;
306
404
  var limitedData = discount === null || discount === void 0 ? void 0 : discount.limited_relation_product_data;
307
405
 
308
406
  // 是符合折扣的商品
309
407
  var isLimitedProduct = limitedData.type === 'product_all' || limitedData.product_ids && limitedData.product_ids.includes(product.id);
310
408
 
311
409
  // 编辑的商品 使用了优惠券不可用
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) {
410
+ var isAvailableProduct = flatItem.type === 'main' ? !((_product = product) !== null && _product !== void 0 && _product.booking_id && (_product2 = product) !== null && _product2 !== void 0 && (_product2 = _product2.discount_list) !== null && _product2 !== void 0 && _product2.length && (_product3 = product) !== null && _product3 !== void 0 && (_product3 = _product3.discount_list) !== null && _product3 !== void 0 && _product3.every(function (discount) {
313
411
  return discount.id && ['good_pass', 'discount_card', 'product_discount_card'].includes(discount.tag || discount.type);
314
- }));
412
+ })) : !(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);
315
413
 
316
414
  // 判断优惠券是否适用于该商品
317
415
  if (isAvailableProduct && isLimitedProduct) {
@@ -331,7 +429,7 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
331
429
  fixed_amount: product.price,
332
430
  resource_id: discount.id,
333
431
  title: discount.format_title,
334
- original_amount: product.origin_total,
432
+ original_amount: product.price || product.origin_total,
335
433
  pre_value: discount.par_value,
336
434
  product_id: originProduct.id
337
435
  }
@@ -346,27 +444,59 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
346
444
  }
347
445
  });
348
446
  });
349
- console.log(sortedProductList, 'sortedProductListsortedProductList');
447
+
448
+ // 🔥 用于存储扁平化商品处理结果的Map
449
+ var processedFlatItemsMap = new Map();
350
450
 
351
451
  // 然后再处理应用哪些优惠券,此时只考虑filteredDiscountList中的优惠券
352
- sortedProductList.forEach(function (originProduct, index) {
353
- var _product$discount_lis4, _product$discount_lis5, _product$discount_lis7, _product$discount_lis8, _product$discount_lis9, _product$discount_lis10, _product$discount_lis11;
354
- var product = _this3.hooks.getProduct(originProduct);
355
- 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) {
452
+ // 🔥 使用扁平化后的列表进行处理
453
+ sortedFlattenedList.forEach(function (flatItem, index) {
454
+ var _product4, _product$discount_lis2, _product5, _product$discount_lis4, _product6, _product6$every, _product$discount_lis5, _product$discount_lis6;
455
+ // 获取商品数据
456
+ var product, originProduct;
457
+ if (flatItem.type === 'main') {
458
+ product = flatItem.product;
459
+ originProduct = flatItem.originProduct;
460
+ } else {
461
+ var _flatItem$bundleItem2, _flatItem$bundleItem3, _flatItem$bundleItem4;
462
+ // bundle子商品
463
+ product = {
464
+ _id: flatItem._id,
465
+ id: flatItem.id,
466
+ price: flatItem.price,
467
+ quantity: flatItem.quantity,
468
+ num: flatItem.num,
469
+ total: flatItem.total,
470
+ original_price: flatItem === null || flatItem === void 0 || (_flatItem$bundleItem2 = flatItem.bundleItem) === null || _flatItem$bundleItem2 === void 0 ? void 0 : _flatItem$bundleItem2.original_price,
471
+ origin_total: flatItem === null || flatItem === void 0 || (_flatItem$bundleItem3 = flatItem.bundleItem) === null || _flatItem$bundleItem3 === void 0 ? void 0 : _flatItem$bundleItem3.original_price,
472
+ booking_id: flatItem.booking_id,
473
+ discount_list: (flatItem === null || flatItem === void 0 || (_flatItem$bundleItem4 = flatItem.bundleItem) === null || _flatItem$bundleItem4 === void 0 ? void 0 : _flatItem$bundleItem4.discount_list) || []
474
+ };
475
+ originProduct = flatItem.originProduct;
476
+ }
477
+
478
+ // 已有优惠的商品跳过
479
+ if ((_product4 = product) !== null && _product4 !== void 0 && _product4.booking_id && (_product$discount_lis2 = product.discount_list) !== null && _product$discount_lis2 !== void 0 && _product$discount_lis2.length && (_product5 = product) !== null && _product5 !== void 0 && (_product5 = _product5.discount_list) !== null && _product5 !== void 0 && _product5.every(function (discount) {
356
480
  return discount.id && ['good_pass', 'discount_card', 'product_discount_card'].includes(discount.tag || discount.type);
357
481
  })) {
358
- processedProductsMap.set(product._id, [originProduct]);
482
+ if (flatItem.type === 'main') {
483
+ processedProductsMap.set(product._id, [originProduct]);
484
+ } else {
485
+ processedFlatItemsMap.set(flatItem._id, [_objectSpread(_objectSpread({}, flatItem), {}, {
486
+ processed: true
487
+ })]);
488
+ }
359
489
  return;
360
490
  }
361
491
 
362
492
  // 找到适用于此商品的所有优惠券,仅考虑isSelected不为false的优惠券
363
493
  var applicableDiscounts = sortedDiscountList.filter(function (discount) {
364
- var _product$discount_lis6;
494
+ var _product$discount_lis3;
365
495
  // 如果商品价格为 0,其实不需要使用任何优惠券,直接 return true
366
496
  // 商品券时主商品价格小于等于0不可用
367
- if ((Number(product.price) <= 0 || !product.price) && (discount.tag || discount.type) === 'good_pass') return false;
497
+ if ((Number(product.price) <= 0 || !product.price) && flatItem.type === 'main' && (discount.tag || discount.type) === 'good_pass') return false;
368
498
  // 折扣卡时总价小于等于0时不可用
369
- 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) {
499
+ if ((Number(product.total) <= 0 || !product.total) && !((_product$discount_lis3 = product.discount_list) !== null && _product$discount_lis3 !== void 0 && _product$discount_lis3.find(function (n) {
370
500
  var _n$discount;
371
501
  return ((_n$discount = n.discount) === null || _n$discount === void 0 ? void 0 : _n$discount.resource_id) === discount.id;
372
502
  })) && (discount.tag || discount.type) !== 'good_pass') return false;
@@ -377,8 +507,16 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
377
507
 
378
508
  // 判断优惠券是否适用于该商品
379
509
  if (limitedData.type === 'product_all') {
510
+ // 检查 package_sub_item_usage_rules
511
+ if (!_this3.checkPackageSubItemUsageRules(discount, flatItem)) {
512
+ return false;
513
+ }
380
514
  return true;
381
515
  } else if (limitedData.product_ids && limitedData.product_ids.includes(product.id)) {
516
+ // 检查 package_sub_item_usage_rules
517
+ if (!_this3.checkPackageSubItemUsageRules(discount, flatItem)) {
518
+ return false;
519
+ }
382
520
  return true;
383
521
  }
384
522
  return false;
@@ -393,18 +531,21 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
393
531
  var selectedDiscount = selectedDiscountCard || applicableDiscounts[0];
394
532
 
395
533
  // 如果是手动折扣,则不适用优惠券
396
- 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) {
534
+ var isManualDiscount = typeof product.isManualDiscount === 'boolean' ? product.isManualDiscount : product.total != product.origin_total && flatItem.type === 'main' && (product.bundle || []).every(function (item) {
535
+ var _ref3;
536
+ return !((_ref3 = item.discount_list || []) !== null && _ref3 !== void 0 && _ref3.length);
537
+ }) && (!((_product$discount_lis4 = product.discount_list) !== null && _product$discount_lis4 !== void 0 && _product$discount_lis4.length) || ((_product6 = product) === null || _product6 === void 0 || (_product6 = _product6.discount_list) === null || _product6 === void 0 || (_product6$every = _product6.every) === null || _product6$every === void 0 ? void 0 : _product6$every.call(_product6, function (item) {
397
538
  return item.type === 'product';
398
539
  })));
399
540
 
400
541
  // 勾选时覆盖手动折扣
401
- 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) {
542
+ if (options !== null && options !== void 0 && options.discountId && (_product$discount_lis5 = product.discount_list) !== null && _product$discount_lis5 !== void 0 && _product$discount_lis5.some(function (item) {
402
543
  var _item$discount;
403
544
  return ((_item$discount = item.discount) === null || _item$discount === void 0 ? void 0 : _item$discount.resource_id) === options.discountId;
404
545
  })) {
405
546
  isManualDiscount = false;
406
547
  }
407
- 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) {
548
+ if (options !== null && options !== void 0 && options.selectedList && (_product$discount_lis6 = product.discount_list) !== null && _product$discount_lis6 !== void 0 && _product$discount_lis6.some(function (item) {
408
549
  var _options$selectedList;
409
550
  return options === null || options === void 0 || (_options$selectedList = options.selectedList) === null || _options$selectedList === void 0 ? void 0 : _options$selectedList.some(function (n) {
410
551
  var _item$discount2;
@@ -415,38 +556,47 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
415
556
  }
416
557
 
417
558
  // 如果没有适用的优惠券,或者手动折扣,则不适用优惠券
418
- // 自定义商品:如果未开启适用折扣卡(product.vouchersApplicable),则不适用优惠券
419
559
  if (applicableDiscounts.length === 0 || isManualDiscount || isBoolean(product.vouchersApplicable) && !product.vouchersApplicable) {
420
- if (product.isClient) {
421
- processedProductsMap.set(product._id, [_this3.hooks.setProduct(originProduct, _objectSpread(_objectSpread({}, isManualDiscount ? {} : {
422
- origin_total: getProductOriginTotalPrice({
423
- product: {
424
- original_price: product.original_price
425
- },
426
- bundle: product.bundle,
427
- options: product.options
428
- }),
429
- variant: originProduct._productInit.variant,
430
- original_price: originProduct._productInit.original_price,
431
- total: getProductTotalPrice({
432
- product: {
433
- price: product.price
434
- },
435
- bundle: product.bundle,
436
- options: product.options
437
- }),
438
- price: product.price
439
- }), {}, {
440
- discount_list: []
441
- }))]);
560
+ if (flatItem.type === 'main') {
561
+ // 主商品:保持原有逻辑
562
+ if (product.isClient) {
563
+ processedProductsMap.set(product._id, [_this3.hooks.setProduct(originProduct, _objectSpread(_objectSpread({}, isManualDiscount ? {} : {
564
+ origin_total: getProductOriginTotalPrice({
565
+ product: {
566
+ original_price: product.original_price
567
+ },
568
+ bundle: product.bundle,
569
+ options: product.options
570
+ }),
571
+ variant: originProduct._productInit.variant,
572
+ original_price: originProduct._productInit.original_price,
573
+ total: getProductTotalPrice({
574
+ product: {
575
+ price: product.price
576
+ },
577
+ bundle: product.bundle,
578
+ options: product.options
579
+ }),
580
+ price: product.price
581
+ }), {}, {
582
+ discount_list: []
583
+ }))]);
584
+ } else {
585
+ processedProductsMap.set(product._id, [_this3.hooks.setProduct(originProduct, _objectSpread(_objectSpread({}, isManualDiscount ? {} : {
586
+ _id: product._id.split('___')[0] + '___' + index,
587
+ total: product.origin_total || product.total,
588
+ price: product.price
589
+ }), {}, {
590
+ discount_list: []
591
+ }))]);
592
+ }
442
593
  } else {
443
- processedProductsMap.set(product._id, [_this3.hooks.setProduct(originProduct, _objectSpread(_objectSpread({}, isManualDiscount ? {} : {
444
- _id: product._id.split('___')[0] + '___' + index,
445
- total: product.origin_total || product.total,
446
- price: product.price
447
- }), {}, {
448
- discount_list: []
449
- }))]);
594
+ // bundle子商品:保存到扁平化Map
595
+ processedFlatItemsMap.set(flatItem._id, [_objectSpread(_objectSpread({}, flatItem), {}, {
596
+ discount_list: [],
597
+ price: flatItem.bundleItem.original_price,
598
+ processed: true
599
+ })]);
450
600
  }
451
601
  return;
452
602
  }
@@ -454,137 +604,550 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
454
604
  return;
455
605
  }
456
606
 
457
- // 是否需要拆分
607
+ // 是否需要拆分(商品券需要拆分)
458
608
  var isNeedSplit = (selectedDiscount.tag || selectedDiscount.type) === 'good_pass';
459
609
 
460
610
  // 需要拆分出来的数量
461
- var splitCount = isNeedSplit ? Math.min(product.quantity || product.num || 1, applicableDiscounts.filter(function (item) {
611
+ var totalQuantity = product.quantity || product.num || 1;
612
+ var availableGoodPassCount = applicableDiscounts.filter(function (item) {
462
613
  return (item.tag || item.type) === 'good_pass';
463
- }).length) : 1;
614
+ }).length;
615
+ var splitCount = isNeedSplit ? Math.min(totalQuantity, availableGoodPassCount) : 1;
464
616
  var arr = [];
465
- if (splitCount < product.quantity && isNeedSplit) {
466
- arr.push(_this3.hooks.setProduct(originProduct, {
467
- discount_list: [],
468
- quantity: product.quantity - splitCount,
469
- _id: product._id.split('___')[0]
470
- }));
471
- }
472
- for (var i = 0; i < splitCount; i++) {
473
- var _product$discount_lis12, _originProduct$_produ, _selectedDiscount$met;
474
- // 如果用过折扣卡,也就不存在拆分的情况了,这里直接使用上面计算出来的折扣卡
475
- var _selectedDiscount = selectedDiscountCard || applicableDiscounts[i];
476
- // 标记优惠券为已使用
477
- usedDiscounts.set(_selectedDiscount.id, true);
478
-
479
- // 记录实际应用了优惠券的商品信息
480
- var appliedProducts = appliedDiscountProducts.get(_selectedDiscount.id) || [];
481
-
482
- // 优先从 origin_total拿,可能会拿不到(比如用户端预约在没有配置 original_price 的情况下)
483
- var productOriginTotal = product.origin_total || product.total || 0;
484
- // 如果当前 product 有 discount_list,则先从 origin_total 拿
485
- if ((_product$discount_lis12 = product.discount_list) !== null && _product$discount_lis12 !== void 0 && _product$discount_lis12.length && product.origin_total) {
486
- productOriginTotal = product.origin_total;
487
- }
488
- // 如果originProduct?._productInit?.original_price为 0,product.origin_total可能为空,此时取 product.total
489
- 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) {
490
- productOriginTotal = product.total;
617
+
618
+ // 🔥 主商品和bundle子商品分别处理
619
+ if (flatItem.type === 'main') {
620
+ // 主商品:保持原有逻辑
621
+ if (splitCount < totalQuantity && isNeedSplit) {
622
+ arr.push(_this3.hooks.setProduct(originProduct, {
623
+ discount_list: [],
624
+ quantity: totalQuantity - splitCount,
625
+ _id: product._id.split('___')[0]
626
+ }));
491
627
  }
628
+ for (var i = 0; i < splitCount; i++) {
629
+ var _product$discount_lis7, _originProduct, _selectedDiscount$met;
630
+ // 如果用过折扣卡,也就不存在拆分的情况了,这里直接使用上面计算出来的折扣卡
631
+ var _selectedDiscount = selectedDiscountCard || applicableDiscounts[i];
632
+ // 标记优惠券为已使用
633
+ usedDiscounts.set(_selectedDiscount.id, true);
492
634
 
493
- // 计算使用折扣卡/商品券以后,单个商品的总 total
494
- var targetProductTotal = getDiscountAmount(_selectedDiscount, productOriginTotal, product.price);
495
- var discountType = _selectedDiscount.tag === 'product_discount_card' ? 'discount_card' : _selectedDiscount.tag;
496
- var discountDetail = {
497
- amount: new Decimal(productOriginTotal).minus(new Decimal(targetProductTotal)).toNumber(),
498
- type: discountType,
499
- discount: {
500
- discount_card_type: _selectedDiscount === null || _selectedDiscount === void 0 || (_selectedDiscount$met = _selectedDiscount.metadata) === null || _selectedDiscount$met === void 0 ? void 0 : _selectedDiscount$met.discount_card_type,
501
- fixed_amount: new Decimal(productOriginTotal).minus(new Decimal(targetProductTotal)).toNumber(),
502
- resource_id: _selectedDiscount.id,
503
- title: _selectedDiscount.format_title,
504
- original_amount: productOriginTotal,
505
- product_id: originProduct.id,
506
- percent: _selectedDiscount.par_value
635
+ // 记录实际应用了优惠券的商品信息
636
+ var appliedProducts = appliedDiscountProducts.get(_selectedDiscount.id) || [];
637
+
638
+ // 优先从 origin_total拿,可能会拿不到(比如用户端预约在没有配置 original_price 的情况下)
639
+ var productOriginTotal = product.origin_total || product.total || 0;
640
+ // 如果当前 product 有 discount_list,则先从 origin_total 拿
641
+ if ((_product$discount_lis7 = product.discount_list) !== null && _product$discount_lis7 !== void 0 && _product$discount_lis7.length && product.origin_total) {
642
+ productOriginTotal = product.origin_total;
507
643
  }
508
- };
644
+ // 如果originProduct?._productInit?.original_price为 0,product.origin_total可能为空,此时取 product.total
645
+ 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) {
646
+ productOriginTotal = product.total;
647
+ }
648
+
649
+ // 计算使用折扣卡/商品券以后,单个商品的总 total
650
+ var targetProductTotal = getDiscountAmount(_selectedDiscount, product.price, product.price);
651
+ var discountDetail = {
652
+ amount: new Decimal(product.price).minus(new Decimal(targetProductTotal)).toNumber(),
653
+ type: _selectedDiscount.tag === 'product_discount_card' ? 'discount_card' : _selectedDiscount.tag,
654
+ discount: {
655
+ discount_card_type: _selectedDiscount === null || _selectedDiscount === void 0 || (_selectedDiscount$met = _selectedDiscount.metadata) === null || _selectedDiscount$met === void 0 ? void 0 : _selectedDiscount$met.discount_card_type,
656
+ fixed_amount: new Decimal(product.price).minus(new Decimal(targetProductTotal)).toNumber(),
657
+ resource_id: _selectedDiscount.id,
658
+ title: _selectedDiscount.format_title,
659
+ original_amount: product.price,
660
+ product_id: originProduct.id,
661
+ percent: _selectedDiscount.par_value
662
+ }
663
+ };
509
664
 
510
- // 如果 discount.tag 或者 discount.type 是 good_pass,则不需要添加 num 属性
511
- if ((_selectedDiscount.tag || _selectedDiscount.type) !== 'good_pass') {
512
- discountDetail.num = product.num || 1;
665
+ // 如果 discount.tag 或者 discount.type 是 good_pass,则不需要添加 num 属性
666
+ if ((_selectedDiscount.tag || _selectedDiscount.type) !== 'good_pass') {
667
+ discountDetail.num = product.num || 1;
668
+ }
669
+ appliedProducts.push(discountDetail);
670
+ appliedDiscountProducts.set(_selectedDiscount.id, appliedProducts);
671
+
672
+ // 记录应用了优惠券的商品
673
+ // 后续更新价格改为 getProductTotalPrice getProductOriginTotalPrice逻辑
674
+ if (product.isClient) {
675
+ debugger;
676
+ arr.push(_this3.hooks.setProduct(originProduct, {
677
+ discount_list: [discountDetail],
678
+ price: _selectedDiscount.tag === 'good_pass' ? 0 : product.price,
679
+ quantity: isNeedSplit ? 1 : product.quantity,
680
+ origin_total: getProductOriginTotalPrice({
681
+ product: {
682
+ original_price: product.original_price
683
+ },
684
+ bundle: product.bundle,
685
+ options: product.options
686
+ }),
687
+ variant: originProduct._productInit.variant,
688
+ original_price: new Decimal(product.price || 0).toNumber(),
689
+ total: targetProductTotal
690
+ }));
691
+ } else {
692
+ arr.push(_this3.hooks.setProduct(originProduct, {
693
+ discount_list: [discountDetail],
694
+ _id: product._id.split('___')[0] + "___" + _selectedDiscount.id + index,
695
+ price: _selectedDiscount.tag === 'good_pass' ? 0 : product.price,
696
+ quantity: isNeedSplit ? 1 : product.quantity,
697
+ total: targetProductTotal,
698
+ origin_total: productOriginTotal
699
+ }));
700
+ }
513
701
  }
514
- appliedProducts.push(discountDetail);
515
- appliedDiscountProducts.set(_selectedDiscount.id, appliedProducts);
702
+ processedProductsMap.set(product._id, arr);
703
+ } else {
704
+ // 🔥 bundle子商品:支持拆分
705
+ var processedItems = [];
706
+ if (isNeedSplit) {
707
+ // 商品券:需要拆分数量
708
+ var discountNum = splitCount;
709
+ var normalNum = totalQuantity - discountNum;
516
710
 
517
- // 记录应用了优惠券的商品
518
- // 后续更新价格改为 getProductTotalPrice getProductOriginTotalPrice逻辑
519
- if (product.isClient) {
520
- arr.push(_this3.hooks.setProduct(originProduct, {
521
- discount_list: [discountDetail],
522
- price: _selectedDiscount.tag === 'good_pass' ? 0 : product.price,
523
- quantity: isNeedSplit ? 1 : product.quantity,
524
- origin_total: getProductOriginTotalPrice({
525
- product: {
526
- original_price: product.original_price
711
+ // 生成有折扣的商品(每张商品券对应 num: 1)
712
+ for (var _i = 0; _i < discountNum; _i++) {
713
+ var _selectedDiscount2 = applicableDiscounts[_i];
714
+ usedDiscounts.set(_selectedDiscount2.id, true);
715
+
716
+ // 🔥 生成唯一的 _id
717
+ var uniqueId = "".concat(flatItem._id, "_split_").concat(_i);
718
+ var _discountDetail = {
719
+ amount: product.origin_total,
720
+ type: 'good_pass',
721
+ discount: {
722
+ resource_id: _selectedDiscount2.id,
723
+ title: _selectedDiscount2.format_title,
724
+ original_amount: product.origin_total,
725
+ product_id: product.id
527
726
  },
528
- bundle: product.bundle,
529
- options: product.options
530
- }),
531
- variant: originProduct._productInit.variant,
532
- original_price: new Decimal(product.price || 0).toNumber(),
533
- total: targetProductTotal
534
- }));
727
+ metadata: {
728
+ // 🔥 使用拆分后的唯一 _id
729
+ custom_product_bundle_map_id: uniqueId,
730
+ num: 1
731
+ },
732
+ num: 1
733
+ };
734
+
735
+ // 记录实际应用的折扣
736
+ var _appliedProducts = appliedDiscountProducts.get(_selectedDiscount2.id) || [];
737
+ _appliedProducts.push(_discountDetail);
738
+ appliedDiscountProducts.set(_selectedDiscount2.id, _appliedProducts);
739
+ processedItems.push(_objectSpread(_objectSpread({}, flatItem), {}, {
740
+ // 🔥 使用唯一的 _id
741
+ _id: uniqueId,
742
+ num: 1,
743
+ quantity: 1,
744
+ price: 0,
745
+ // 商品券价格为0
746
+ total: 0,
747
+ discount_list: [_discountDetail],
748
+ processed: true,
749
+ _discountId: _selectedDiscount2.id
750
+ }));
751
+ }
752
+
753
+ // 生成无折扣的商品(剩余数量)
754
+ if (normalNum > 0) {
755
+ processedItems.push(_objectSpread(_objectSpread({}, flatItem), {}, {
756
+ // 🔥 为剩余商品生成唯一的 _id
757
+ _id: "".concat(flatItem._id, "_split_rest"),
758
+ num: normalNum,
759
+ quantity: normalNum,
760
+ discount_list: [],
761
+ processed: true
762
+ }));
763
+ }
535
764
  } else {
536
- arr.push(_this3.hooks.setProduct(originProduct, {
537
- discount_list: [discountDetail],
538
- _id: product._id.split('___')[0] + "___" + _selectedDiscount.id + "___" + index,
539
- price: _selectedDiscount.tag === 'good_pass' ? 0 : product.price,
540
- quantity: isNeedSplit ? 1 : product.quantity,
541
- total: targetProductTotal,
542
- origin_total: productOriginTotal
765
+ var _selectedDiscount3$me;
766
+ // 折扣卡:不拆分数量,直接应用
767
+ var _selectedDiscount3 = selectedDiscountCard || applicableDiscounts[0];
768
+ usedDiscounts.set(_selectedDiscount3.id, true);
769
+ var _productOriginTotal = product.original_price || product.price || 0;
770
+ var _targetProductTotal = getDiscountAmount(_selectedDiscount3, _productOriginTotal, _productOriginTotal);
771
+
772
+ // 🔥 使用当前的 _id 作为唯一标识
773
+ var _uniqueId = flatItem._id;
774
+ var _discountDetail2 = {
775
+ amount: new Decimal(_productOriginTotal).minus(_targetProductTotal).toNumber(),
776
+ type: _selectedDiscount3.tag === 'product_discount_card' ? 'discount_card' : _selectedDiscount3.tag,
777
+ discount: {
778
+ discount_card_type: _selectedDiscount3 === null || _selectedDiscount3 === void 0 || (_selectedDiscount3$me = _selectedDiscount3.metadata) === null || _selectedDiscount3$me === void 0 ? void 0 : _selectedDiscount3$me.discount_card_type,
779
+ fixed_amount: new Decimal(_productOriginTotal).minus(_targetProductTotal).toNumber(),
780
+ resource_id: _selectedDiscount3.id,
781
+ title: _selectedDiscount3.format_title,
782
+ original_amount: product.original_price,
783
+ product_id: product.id,
784
+ percent: _selectedDiscount3.par_value
785
+ },
786
+ metadata: {
787
+ // 🔥 使用唯一的 _id
788
+ custom_product_bundle_map_id: _uniqueId,
789
+ num: product.num || 1
790
+ },
791
+ num: product.num || 1
792
+ };
793
+
794
+ // 记录实际应用的折扣
795
+ var _appliedProducts2 = appliedDiscountProducts.get(_selectedDiscount3.id) || [];
796
+ _appliedProducts2.push(_discountDetail2);
797
+ appliedDiscountProducts.set(_selectedDiscount3.id, _appliedProducts2);
798
+ processedItems.push(_objectSpread(_objectSpread({}, flatItem), {}, {
799
+ total: _targetProductTotal,
800
+ price: new Decimal(_productOriginTotal || 0).minus(_discountDetail2.amount).toNumber(),
801
+ discount_list: [_discountDetail2],
802
+ processed: true
543
803
  }));
544
804
  }
805
+ processedFlatItemsMap.set(flatItem._id, processedItems);
545
806
  }
546
- console.log(arr, 'arrarrarr');
547
- processedProductsMap.set(product._id, arr);
548
807
  });
549
808
 
550
- // 按原始顺序构建处理后的商品列表
551
- var processedProductList = [];
552
- productList.forEach(function (originProduct) {
553
- var product = _this3.hooks.getProduct(originProduct);
554
- var getDefaultProduct = function getDefaultProduct() {
555
- if (product.isClient) {
556
- return _this3.hooks.setProduct(originProduct, {
557
- discount_list: [],
558
- price: product.price,
559
- origin_total: getProductOriginTotalPrice({
560
- product: {
561
- original_price: product.original_price
562
- },
563
- bundle: product.bundle,
564
- options: product.options
565
- }),
566
- variant: originProduct._productInit.variant,
567
- original_price: originProduct._productInit.original_price,
568
- total: getProductTotalPrice({
569
- product: {
809
+ // 🔥 重组:将处理后的bundle子商品重新组装回主商品
810
+ var reconstructProductsWithBundle = function reconstructProductsWithBundle(processedProductsMap, processedFlatItemsMap, originalProductList) {
811
+ var result = [];
812
+ originalProductList.forEach(function (originProduct) {
813
+ var product = _this3.hooks.getProduct(originProduct);
814
+
815
+ // 获取主商品处理结果
816
+ var mainProductArr = processedProductsMap.get(product._id);
817
+ if (!mainProductArr || mainProductArr.length === 0) {
818
+ // 如果没有处理结果,返回默认商品
819
+ var getDefaultProduct = function getDefaultProduct() {
820
+ if (product.isClient) {
821
+ return _this3.hooks.setProduct(originProduct, {
822
+ discount_list: [],
823
+ price: product.price,
824
+ origin_total: getProductOriginTotalPrice({
825
+ product: {
826
+ original_price: product.original_price
827
+ },
828
+ bundle: product.bundle,
829
+ options: product.options
830
+ }),
831
+ variant: originProduct._productInit.variant,
832
+ original_price: originProduct._productInit.original_price,
833
+ total: getProductTotalPrice({
834
+ product: {
835
+ price: product.price
836
+ },
837
+ bundle: product.bundle,
838
+ options: product.options
839
+ })
840
+ });
841
+ } else {
842
+ return _this3.hooks.setProduct(originProduct, {
843
+ discount_list: [],
844
+ total: product.total,
845
+ origin_total: product.origin_total,
570
846
  price: product.price
571
- },
572
- bundle: product.bundle,
573
- options: product.options
574
- })
575
- });
847
+ });
848
+ }
849
+ };
850
+ result.push(getDefaultProduct());
851
+ return;
852
+ }
853
+
854
+ // 检查是否有bundle子商品需要重组
855
+ var hasBundle = product.bundle && Array.isArray(product.bundle) && product.bundle.length > 0;
856
+ if (!hasBundle) {
857
+ // 没有bundle,直接使用主商品处理结果
858
+ result.push.apply(result, _toConsumableArray(mainProductArr));
576
859
  } else {
577
- return _this3.hooks.setProduct(originProduct, {
578
- discount_list: [],
579
- total: product.total,
580
- origin_total: product.origin_total,
581
- price: product.price
582
- });
860
+ // 🔥 有bundle,需要检查子商品是否应用了商品券
861
+ // 1. 先收集所有bundle子商品的处理结果
862
+ var bundleProcessingInfo = new Map();
863
+ var hasGoodPassApplied = false; // 标记是否有子商品应用了商品券
864
+
865
+ if (product.bundle && Array.isArray(product.bundle)) {
866
+ product.bundle.forEach(function (bundleItem, bundleIndex) {
867
+ var bundleItemId = "".concat(product._id, "_bundle_").concat(bundleIndex);
868
+ var processedBundleItems = processedFlatItemsMap.get(bundleItemId);
869
+ if (!processedBundleItems || processedBundleItems.length === 0) {
870
+ // 未处理的bundle item,保持原样
871
+ bundleProcessingInfo.set(bundleIndex, [bundleItem]);
872
+ } else {
873
+ // 处理过的bundle item
874
+ bundleProcessingInfo.set(bundleIndex, processedBundleItems);
875
+ // 🔥 检查是否应用了商品券(good_pass)
876
+ var hasGoodPass = processedBundleItems.some(function (item) {
877
+ var _item$discount_list;
878
+ return (_item$discount_list = item.discount_list) === null || _item$discount_list === void 0 ? void 0 : _item$discount_list.some(function (discount) {
879
+ return discount.type === 'good_pass' || discount.tag === 'good_pass';
880
+ });
881
+ });
882
+ if (hasGoodPass) {
883
+ hasGoodPassApplied = true;
884
+ }
885
+ }
886
+ });
887
+ }
888
+
889
+ // 🔥 2. 如果有子商品应用了商品券,且主商品数量大于1,需要拆分主商品
890
+ var mainProductQuantity = mainProductArr[0] ? _this3.hooks.getProduct(mainProductArr[0]).quantity : 1;
891
+ if (hasGoodPassApplied && mainProductArr.length === 1 && mainProductQuantity > 1) {
892
+ var mainProduct = mainProductArr[0];
893
+ var mainProductData = _this3.hooks.getProduct(mainProduct);
894
+
895
+ // 🔥 方案:拆分成2个主商品
896
+ // 1. 一个主商品(qty=1)包含拆分后的bundle
897
+ // 2. 一个主商品(qty=原quantity-1)包含原始未拆分的bundle
898
+
899
+ // 第一个:包含拆分后的bundle (qty=1)
900
+ var newBundleWithDiscount = [];
901
+ (product.bundle || []).forEach(function (bundleItem, bundleIndex) {
902
+ var processedItems = bundleProcessingInfo.get(bundleIndex) || [bundleItem];
903
+ if (processedItems.length > 1) {
904
+ // 被拆分的子商品,添加所有拆分项
905
+ processedItems.forEach(function (item) {
906
+ // 🔥 更新 discount_list 中的 num,使其与拆分后的 item.num 一致
907
+ var updatedDiscountList = (item.discount_list || []).map(function (discount) {
908
+ return _objectSpread(_objectSpread({}, discount), {}, {
909
+ num: item.num // 使用拆分后的 num
910
+ });
911
+ });
912
+ newBundleWithDiscount.push(_objectSpread(_objectSpread({}, bundleItem), {}, {
913
+ _id: item._id,
914
+ product_id: bundleItem.product_id,
915
+ price: item.price,
916
+ num: item.num,
917
+ discount_list: updatedDiscountList
918
+ }));
919
+ });
920
+ } else {
921
+ // 未拆分的子商品,保持原样
922
+ var item = processedItems[0];
923
+ if (item.processed) {
924
+ // 🔥 同样需要更新 discount_list 中的 num
925
+ var _updatedDiscountList = (item.discount_list || []).map(function (discount) {
926
+ return _objectSpread(_objectSpread({}, discount), {}, {
927
+ num: item.num // 使用当前的 num
928
+ });
929
+ });
930
+ newBundleWithDiscount.push(_objectSpread(_objectSpread({}, bundleItem), {}, {
931
+ _id: item._id,
932
+ product_id: bundleItem.product_id,
933
+ price: item.price,
934
+ num: item.num,
935
+ discount_list: _updatedDiscountList
936
+ }));
937
+ } else {
938
+ newBundleWithDiscount.push(item);
939
+ }
940
+ }
941
+ });
942
+
943
+ // 计算第一个主商品的总价(包含拆分后的bundle)
944
+ var newTotalWithDiscount = Number(mainProductData.price || 0);
945
+ var newOriginTotalWithDiscount = Number(mainProductData.original_price || mainProductData.price || 0);
946
+ if (newBundleWithDiscount.length > 0) {
947
+ newBundleWithDiscount.forEach(function (item) {
948
+ newTotalWithDiscount += Number(item.price) * Number(item.num);
949
+ });
950
+ newBundleWithDiscount.forEach(function (item) {
951
+ var _item$discount_list2;
952
+ 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;
953
+ newOriginTotalWithDiscount += Number(originalPrice) * Number(item.num);
954
+ });
955
+ }
956
+
957
+ // 🔥 更新主商品自己的 discount_list 中的 num(quantity=1)
958
+ var updatedMainDiscountList = mainProductData.discount_list.map(function (discount) {
959
+ var _discount$metadata2;
960
+ if (discount !== null && discount !== void 0 && (_discount$metadata2 = discount.metadata) !== null && _discount$metadata2 !== void 0 && _discount$metadata2.custom_product_bundle_map_id) {
961
+ // bundle的discount_list保持不变
962
+ return discount;
963
+ }
964
+ // 主商品自己的discount,更新num为1
965
+ return _objectSpread(_objectSpread({}, discount), {}, {
966
+ num: 1
967
+ });
968
+ });
969
+
970
+ // 🔥 使用更新后的列表计算折扣金额
971
+ var mainDiscountList = updatedMainDiscountList.filter(function (item) {
972
+ var _item$metadata;
973
+ return !(item !== null && item !== void 0 && (_item$metadata = item.metadata) !== null && _item$metadata !== void 0 && _item$metadata.custom_product_bundle_map_id);
974
+ });
975
+ if (mainDiscountList && mainDiscountList.length > 0) {
976
+ var _Decimal$minus$toNumb, _mainProductData$orig;
977
+ var allDiscountAmount = getDiscountListAmountTotal(mainDiscountList);
978
+ newTotalWithDiscount = (_Decimal$minus$toNumb = new Decimal(mainProductData.price || 0).minus(allDiscountAmount).toNumber()) !== null && _Decimal$minus$toNumb !== void 0 ? _Decimal$minus$toNumb : newTotalWithDiscount;
979
+ newOriginTotalWithDiscount = (_mainProductData$orig = mainProductData.origin_total) !== null && _mainProductData$orig !== void 0 ? _mainProductData$orig : newOriginTotalWithDiscount;
980
+ if (newBundleWithDiscount.length > 0) {
981
+ newBundleWithDiscount.forEach(function (item) {
982
+ var _item$discount_list3;
983
+ newTotalWithDiscount += Number(item.price) * Number(item.num);
984
+ 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;
985
+ newOriginTotalWithDiscount += Number(originalPrice) * Number(item.num);
986
+ });
987
+ }
988
+ }
989
+
990
+ // 添加第一个主商品:qty=1,包含拆分后的bundle
991
+ result.push(_this3.hooks.setProduct(mainProduct, _objectSpread(_objectSpread({}, mainProductData), {}, {
992
+ _id: "".concat(mainProductData._id.split('___')[0], "___split_discount"),
993
+ quantity: 1,
994
+ discount_list: updatedMainDiscountList,
995
+ bundle: newBundleWithDiscount,
996
+ total: newTotalWithDiscount,
997
+ origin_total: newOriginTotalWithDiscount
998
+ })));
999
+
1000
+ // 第二个:包含原始bundle (qty=原quantity-1)
1001
+ if (mainProductQuantity > 1) {
1002
+ var newBundleOriginal = [];
1003
+ (product.bundle || []).forEach(function (bundleItem, bundleIndex) {
1004
+ // 使用原始的bundle配置,不包含拆分
1005
+ newBundleOriginal.push(_objectSpread(_objectSpread({}, bundleItem), {}, {
1006
+ discount_list: []
1007
+ }));
1008
+ });
1009
+
1010
+ // 计算第二个主商品的总价
1011
+ var newTotalOriginal = Number(mainProductData.price || 0);
1012
+ var newOriginTotalOriginal = Number(mainProductData.original_price || mainProductData.price || 0);
1013
+ if (newBundleOriginal.length > 0) {
1014
+ newBundleOriginal.forEach(function (item) {
1015
+ newTotalOriginal += Number(item.price) * Number(item.num);
1016
+ newOriginTotalOriginal += Number(item.price) * Number(item.num);
1017
+ });
1018
+ }
1019
+
1020
+ // 🔥 更新主商品自己的 discount_list 中的 num(quantity=原quantity-1)
1021
+ var updatedMainDiscountListOriginal = mainProductData.discount_list.map(function (discount) {
1022
+ var _discount$metadata3;
1023
+ if (discount !== null && discount !== void 0 && (_discount$metadata3 = discount.metadata) !== null && _discount$metadata3 !== void 0 && _discount$metadata3.custom_product_bundle_map_id) {
1024
+ // bundle的discount_list保持不变
1025
+ return discount;
1026
+ }
1027
+ // 主商品自己的discount,更新num为 mainProductQuantity - 1
1028
+ return _objectSpread(_objectSpread({}, discount), {}, {
1029
+ num: mainProductQuantity - 1
1030
+ });
1031
+ });
1032
+
1033
+ // 🔥 使用更新后的列表计算折扣金额
1034
+ var mainDiscountListOriginal = updatedMainDiscountListOriginal.filter(function (item) {
1035
+ var _item$metadata2;
1036
+ return !(item !== null && item !== void 0 && (_item$metadata2 = item.metadata) !== null && _item$metadata2 !== void 0 && _item$metadata2.custom_product_bundle_map_id);
1037
+ });
1038
+ if (mainDiscountListOriginal && mainDiscountListOriginal.length > 0) {
1039
+ var _Decimal$minus$toNumb2, _mainProductData$orig2;
1040
+ var _allDiscountAmount = getDiscountListAmount(mainDiscountListOriginal);
1041
+ newTotalOriginal = (_Decimal$minus$toNumb2 = new Decimal(mainProductData.price || 0).minus(_allDiscountAmount).toNumber()) !== null && _Decimal$minus$toNumb2 !== void 0 ? _Decimal$minus$toNumb2 : newTotalOriginal;
1042
+ newOriginTotalOriginal = (_mainProductData$orig2 = mainProductData.origin_total) !== null && _mainProductData$orig2 !== void 0 ? _mainProductData$orig2 : newOriginTotalOriginal;
1043
+ if (newBundleOriginal.length > 0) {
1044
+ newBundleOriginal.forEach(function (item) {
1045
+ newTotalOriginal += Number(item.price) * Number(item.num);
1046
+ newOriginTotalOriginal += Number(item.price) * Number(item.num);
1047
+ });
1048
+ }
1049
+ }
1050
+
1051
+ // 添加第二个主商品:qty=原quantity-1,包含原始bundle
1052
+ result.push(_this3.hooks.setProduct(mainProduct, _objectSpread(_objectSpread({}, mainProductData), {}, {
1053
+ _id: "".concat(mainProductData._id.split('___')[0], "___split_normal"),
1054
+ quantity: mainProductQuantity - 1,
1055
+ discount_list: updatedMainDiscountListOriginal,
1056
+ bundle: newBundleOriginal,
1057
+ total: newTotalOriginal,
1058
+ origin_total: newOriginTotalOriginal
1059
+ })));
1060
+ }
1061
+ } else {
1062
+ // 🔥 没有子商品被拆分,使用原有逻辑
1063
+ mainProductArr.forEach(function (mainProduct) {
1064
+ var mainProductData = _this3.hooks.getProduct(mainProduct);
1065
+
1066
+ // 重组bundle
1067
+ var newBundle = [];
1068
+ if (product.bundle && Array.isArray(product.bundle)) {
1069
+ product.bundle.forEach(function (bundleItem, bundleIndex) {
1070
+ var bundleItemId = "".concat(product._id, "_bundle_").concat(bundleIndex);
1071
+ var processedBundleItems = processedFlatItemsMap.get(bundleItemId);
1072
+ if (!processedBundleItems || processedBundleItems.length === 0) {
1073
+ // 未处理的bundle item,保持原样
1074
+ newBundle.push(bundleItem);
1075
+ } else {
1076
+ // 🔥 关键:拆分后的bundle item
1077
+ processedBundleItems.forEach(function (item) {
1078
+ // 🔥 更新 discount_list 中的 num,使其与拆分后的 item.num 一致
1079
+ var updatedDiscountList = (item.discount_list || []).map(function (discount) {
1080
+ return _objectSpread(_objectSpread({}, discount), {}, {
1081
+ num: item.num // 使用拆分后的 num
1082
+ });
1083
+ });
1084
+ newBundle.push(_objectSpread(_objectSpread({}, bundleItem), {}, {
1085
+ _id: item._id,
1086
+ product_id: bundleItem.product_id,
1087
+ price: item.price,
1088
+ num: item.num,
1089
+ discount_list: updatedDiscountList
1090
+ }));
1091
+ });
1092
+ }
1093
+ });
1094
+ }
1095
+
1096
+ // 🔥 重新计算主商品的总价(包含bundle)
1097
+ var newTotal = Number(mainProductData.price || 0);
1098
+ var newOriginTotal = Number(mainProductData.original_price || mainProductData.price || 0);
1099
+
1100
+ // 累加bundle的价格
1101
+ if (newBundle.length > 0) {
1102
+ newBundle.forEach(function (item) {
1103
+ newTotal += Number(item.price) * Number(item.num);
1104
+ });
1105
+
1106
+ // 计算原始总价(不考虑折扣)
1107
+ newBundle.forEach(function (item) {
1108
+ var _item$discount_list4;
1109
+ var originalPrice = ((_item$discount_list4 = item.discount_list) === null || _item$discount_list4 === void 0 || (_item$discount_list4 = _item$discount_list4[0]) === null || _item$discount_list4 === void 0 || (_item$discount_list4 = _item$discount_list4.discount) === null || _item$discount_list4 === void 0 ? void 0 : _item$discount_list4.original_amount) || item.price;
1110
+ newOriginTotal += Number(originalPrice) * Number(item.num);
1111
+ });
1112
+ }
1113
+ var mainDiscountList = mainProductData.discount_list.filter(function (item) {
1114
+ var _item$metadata3;
1115
+ return !(item !== null && item !== void 0 && (_item$metadata3 = item.metadata) !== null && _item$metadata3 !== void 0 && _item$metadata3.custom_product_bundle_map_id);
1116
+ });
1117
+
1118
+ // 如果主商品本身有折扣,需要重新计算
1119
+ if (mainDiscountList && mainDiscountList.length > 0) {
1120
+ var _Decimal$minus$toNumb3, _mainProductData$orig3;
1121
+ var _allDiscountAmount2 = getDiscountListAmount(mainDiscountList);
1122
+ newTotal = (_Decimal$minus$toNumb3 = new Decimal(mainProductData.price || 0).minus(_allDiscountAmount2).toNumber()) !== null && _Decimal$minus$toNumb3 !== void 0 ? _Decimal$minus$toNumb3 : newTotal;
1123
+ newOriginTotal = (_mainProductData$orig3 = mainProductData.origin_total) !== null && _mainProductData$orig3 !== void 0 ? _mainProductData$orig3 : newOriginTotal;
1124
+
1125
+ // 累加bundle的价格
1126
+ if (newBundle.length > 0) {
1127
+ newBundle.forEach(function (item) {
1128
+ var _item$discount_list5;
1129
+ newTotal += Number(item.price) * Number(item.num);
1130
+ var originalPrice = ((_item$discount_list5 = item.discount_list) === null || _item$discount_list5 === void 0 || (_item$discount_list5 = _item$discount_list5[0]) === null || _item$discount_list5 === void 0 || (_item$discount_list5 = _item$discount_list5.discount) === null || _item$discount_list5 === void 0 ? void 0 : _item$discount_list5.original_amount) || item.price;
1131
+ newOriginTotal += Number(originalPrice) * Number(item.num);
1132
+ });
1133
+ }
1134
+ }
1135
+
1136
+ // 生成最终的主商品
1137
+ result.push(_this3.hooks.setProduct(mainProduct, _objectSpread(_objectSpread({}, mainProductData), {}, {
1138
+ bundle: newBundle,
1139
+ total: newTotal,
1140
+ origin_total: newOriginTotal
1141
+ })));
1142
+ });
1143
+ }
583
1144
  }
584
- };
585
- var arr = processedProductsMap.get(product._id);
586
- arr !== null && arr !== void 0 && arr.length ? processedProductList.push.apply(processedProductList, _toConsumableArray(arr)) : processedProductList.push(getDefaultProduct());
587
- });
1145
+ });
1146
+ return result;
1147
+ };
1148
+
1149
+ // 按原始顺序构建处理后的商品列表
1150
+ var processedProductList = reconstructProductsWithBundle(processedProductsMap, processedFlatItemsMap, productList);
588
1151
 
589
1152
  // 按原始顺序更新优惠券列表,标记已使用和可用的优惠券
590
1153
  var updatedDiscountList = addModeDiscount.map(function (discount) {
@@ -655,6 +1218,145 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
655
1218
  discountList: [].concat(editModeDiscount, _toConsumableArray(updatedDiscountList))
656
1219
  };
657
1220
  }
1221
+
1222
+ /**
1223
+ * 检查优惠是否符合 PackageSubItemUsageRules 配置
1224
+ * @param discount 优惠券
1225
+ * @param flatItem 扁平化后的商品项(可能是主商品或bundle子商品)
1226
+ * @returns 是否可用
1227
+ */
1228
+ }, {
1229
+ key: "checkPackageSubItemUsageRules",
1230
+ value: function checkPackageSubItemUsageRules(discount, flatItem) {
1231
+ var limitedData = discount.limited_relation_product_data;
1232
+ var usageRules = limitedData === null || limitedData === void 0 ? void 0 : limitedData.package_sub_item_usage_rules;
1233
+ var rules = ['original_price'].concat(_toConsumableArray((usageRules === null || usageRules === void 0 ? void 0 : usageRules.rules) || []));
1234
+
1235
+ // 如果没有配置 package_sub_item_usage_rules,则默认可用
1236
+ if (!usageRules) {
1237
+ return true;
1238
+ }
1239
+ var ruleType = usageRules.type;
1240
+ var isMainProduct = flatItem.type === 'main'; // 单独购买
1241
+ var isBundleItem = flatItem.type === 'bundle'; // 套餐中购买
1242
+
1243
+ // 主商品直接可用
1244
+ if (isMainProduct) {
1245
+ return true;
1246
+ }
1247
+
1248
+ // universal_discount: 单独购买和套餐中(子商品为原价)均可使用
1249
+ if (ruleType === 'universal_discount') {
1250
+ if (isMainProduct) {
1251
+ return true; // 单独购买时可用
1252
+ }
1253
+ if (isBundleItem) {
1254
+ var _flatItem$bundleItem5, _flatItem$bundleItem6;
1255
+ // 套餐中购买时,判断是否为原价
1256
+ var priceType = (_flatItem$bundleItem5 = flatItem.bundleItem) === null || _flatItem$bundleItem5 === void 0 ? void 0 : _flatItem$bundleItem5.price_type;
1257
+ var priceTypeExt = (_flatItem$bundleItem6 = flatItem.bundleItem) === null || _flatItem$bundleItem6 === void 0 ? void 0 : _flatItem$bundleItem6.price_type_ext;
1258
+ // original_price 对应:
1259
+ // 1. price_type: "markup" && price_type_ext: "product_price"
1260
+ // markup_price 对应:
1261
+ // price_type: "markup" && price_type_ext: ""
1262
+ /** 原价 */
1263
+ var isOriginalPrice = priceType === 'markup' && priceTypeExt === 'product_price';
1264
+ /** 加价 */
1265
+ var isMarkupPrice = priceType === 'markup' && (priceTypeExt === '' || !priceTypeExt);
1266
+
1267
+ // 检查 rules
1268
+ if (rules.length > 0) {
1269
+ // 检查原价
1270
+ if (isOriginalPrice && rules.includes('original_price')) {
1271
+ return true;
1272
+ }
1273
+
1274
+ // 检查加价
1275
+ if (isMarkupPrice && rules.includes('markup_price')) {
1276
+ return true;
1277
+ }
1278
+
1279
+ // 如果都不匹配,则不可用
1280
+ return false;
1281
+ }
1282
+
1283
+ // 原价包括:price_type: "markup" && price_type_ext: "product_price"
1284
+ return isOriginalPrice;
1285
+ }
1286
+ return true;
1287
+ }
1288
+
1289
+ // package_exclusive: 仅在套餐中(子商品为原价)时可使用
1290
+ if (ruleType === 'package_exclusive') {
1291
+ if (isMainProduct) {
1292
+ return false; // 单独购买时不可用
1293
+ }
1294
+ if (isBundleItem) {
1295
+ var _flatItem$bundleItem7, _flatItem$bundleItem8;
1296
+ // 套餐中购买时,判断是否为原价
1297
+ var _priceType = (_flatItem$bundleItem7 = flatItem.bundleItem) === null || _flatItem$bundleItem7 === void 0 ? void 0 : _flatItem$bundleItem7.price_type;
1298
+ var _priceTypeExt = (_flatItem$bundleItem8 = flatItem.bundleItem) === null || _flatItem$bundleItem8 === void 0 ? void 0 : _flatItem$bundleItem8.price_type_ext;
1299
+ // 原价包括:price_type: "markup" && price_type_ext: "product_price"
1300
+ return _priceType === 'markup' && _priceTypeExt === 'product_price';
1301
+ }
1302
+ return false;
1303
+ }
1304
+
1305
+ // single_item_promo: 仅在单独购买时可使用
1306
+ if (ruleType === 'single_item_promo') {
1307
+ return isMainProduct; // 只有单独购买时可用
1308
+ }
1309
+
1310
+ /*
1311
+ // custom_usage_rules: 根据自定义规则判断
1312
+ if (ruleType === 'custom_usage_rules') {
1313
+ const customRules = usageRules.custom_usage_rules;
1314
+ if (!customRules) {
1315
+ return true; // 如果没有自定义规则,默认可用
1316
+ }
1317
+ const { types, package_sub_item_rules } = customRules;
1318
+ // 判断商品类型是否在允许的类型中
1319
+ if (isMainProduct) {
1320
+ // 主商品对应 standalone_product
1321
+ if (!types.includes('standalone_product')) {
1322
+ return false;
1323
+ }
1324
+ return true; // 主商品不需要判断 package_sub_item_rules
1325
+ }
1326
+ if (isBundleItem) {
1327
+ // bundle子商品对应 package_sub_item
1328
+ if (!types.includes('package_sub_item')) {
1329
+ return false;
1330
+ }
1331
+ // 如果包含 package_sub_item,还需要判断 price_type
1332
+ const priceType = flatItem.bundleItem?.price_type;
1333
+ const priceTypeExt = flatItem.bundleItem?.price_type_ext;
1334
+ // 检查 package_sub_item_rules
1335
+ if (package_sub_item_rules && package_sub_item_rules.length > 0) {
1336
+ // original_price 对应:
1337
+ // 1. price_type: "markup" && price_type_ext: "product_price"
1338
+ // markup_price 对应:
1339
+ // price_type: "markup" && price_type_ext: ""
1340
+ const isOriginalPrice = (priceType === 'markup' && priceTypeExt === 'product_price');
1341
+ const isMarkupPrice = priceType === 'markup' && (priceTypeExt === '' || !priceTypeExt);
1342
+ if (isOriginalPrice && package_sub_item_rules.includes('original_price')) {
1343
+ return true;
1344
+ }
1345
+ if (isMarkupPrice && package_sub_item_rules.includes('markup_price')) {
1346
+ return true;
1347
+ }
1348
+ // 如果都不匹配,则不可用
1349
+ return false;
1350
+ }
1351
+ return true; // 如果没有 package_sub_item_rules 配置,默认可用
1352
+ }
1353
+ return true;
1354
+ }
1355
+ * */
1356
+
1357
+ // 默认可用
1358
+ return true;
1359
+ }
658
1360
  }, {
659
1361
  key: "destroy",
660
1362
  value: function () {