@pisell/pisellos 2.2.23 → 2.2.25

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.
@@ -29,6 +29,8 @@ import { getProductOriginTotalPrice, getProductTotalPrice } from "../Cart/utils"
29
29
  import Decimal from 'decimal.js';
30
30
  import dayjs from 'dayjs';
31
31
  import { isBoolean } from 'lodash-es';
32
+ import { isOrderLevelFixedAmountDiscount } from "../../solution/ShopDiscount/utils";
33
+ import { calculateOrderLevelDiscountAllocation } from "../../solution/ShopDiscount/utils";
32
34
 
33
35
  // 临时变量
34
36
  var flatItem;
@@ -216,6 +218,25 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
216
218
  }
217
219
  });
218
220
 
221
+ // 🔥 检查 editModeDiscount 中是否有订单级别的折扣,如果有则禁用 addModeDiscount 中相同 product_id 的折扣卡
222
+ var editModeOrderLevelProductIds = new Set();
223
+ editModeDiscount.forEach(function (discount) {
224
+ var _discount$discount2;
225
+ if (((_discount$discount2 = discount.discount) === null || _discount$discount2 === void 0 ? void 0 : _discount$discount2.discount_calculation_mode) === 'order_level') {
226
+ var _discount$discount3;
227
+ editModeOrderLevelProductIds.add(discount === null || discount === void 0 || (_discount$discount3 = discount.discount) === null || _discount$discount3 === void 0 ? void 0 : _discount$discount3.discount_product_id);
228
+ }
229
+ });
230
+
231
+ // 如果存在订单级别的编辑模式折扣,禁用 addModeDiscount 中相同 product_id 的折扣卡
232
+ if (editModeOrderLevelProductIds.size > 0) {
233
+ addModeDiscount.forEach(function (discount) {
234
+ if (editModeOrderLevelProductIds.has(discount.product_id)) {
235
+ discount.isDisabled = true;
236
+ }
237
+ });
238
+ }
239
+
219
240
  // 过滤掉手动取消false的优惠券,但仅用于应用优惠逻辑
220
241
  var filteredDiscountList = addModeDiscount.filter(function (discount) {
221
242
  return !discount.isManualSelect;
@@ -271,26 +292,69 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
271
292
  };
272
293
 
273
294
  // 优惠力度排序,传进来的数据里可能有商品券,也可能有优惠券
274
- // 1. 商品券(n.tag=good_pass)视为最优惠(免费)
275
- // 2. 折扣券(n.tag=product_discount_card)按照新的优先级排序:
276
- // - 固定金额优先于百分比(固定金额 > 百分比)
277
- // - 固定金额内部:金额越大越优先
278
- // - 百分比内部:折扣越大越优先(par_value越小越优先)
279
- // 3. 相同类型和金额的情况下,按照过期时间(n.expire_time)排序
295
+ // 新的优先级排序规则(从高到低):
296
+ // 1. 带有Holder的商品券(metadata?.holder?.type === 'custom' && tag === 'good_pass')
297
+ // 2. 带有Holder的商品级折扣卡(metadata?.holder?.type === 'custom' && tag === 'product_discount_card' && discount_calculation_mode !== 'order_level')
298
+ // 3. 带有Holder的订单级折扣卡(metadata?.holder?.type === 'custom' && tag === 'product_discount_card' && discount_calculation_mode === 'order_level')
299
+ // 4. Customer商品券(metadata?.holder?.type !== 'custom' && tag === 'good_pass')
300
+ // 5. Customer商品级折扣卡(metadata?.holder?.type !== 'custom' && tag === 'product_discount_card' && discount_calculation_mode !== 'order_level')
301
+ // 6. Customer订单级固定金额折扣(metadata?.holder?.type !== 'custom' && tag === 'product_discount_card' && discount_calculation_mode === 'order_level')
302
+ // 同一优先级内部:固定金额优先于百分比,金额/折扣力度越大越优先,最后按过期时间排序
280
303
  var sortedDiscountList = _toConsumableArray(filteredDiscountList).sort(function (a, b) {
281
- // 1. 商品券优先级最高
282
- if (a.tag === 'good_pass' && b.tag !== 'good_pass') return -1;
283
- if (b.tag === 'good_pass' && a.tag !== 'good_pass') return 1;
304
+ // 辅助函数:判断是否是带有Holder的优惠券
305
+ var isHolderDiscount = function isHolderDiscount(discount) {
306
+ var _discount$metadata;
307
+ return ((_discount$metadata = discount.metadata) === null || _discount$metadata === void 0 || (_discount$metadata = _discount$metadata.holder) === null || _discount$metadata === void 0 ? void 0 : _discount$metadata.type) === 'custom';
308
+ };
284
309
 
285
- // 2. 如果都是商品券或都不是商品券,按照类型和折扣力度排序
286
- if (a.tag === 'good_pass' && b.tag === 'good_pass') {
287
- // 都是商品券,按照过期时间排序
288
- return compareByExpireTime(a, b);
289
- } else if (a.tag === 'product_discount_card' && b.tag === 'product_discount_card') {
290
- var _a$metadata, _b$metadata;
291
- // 都是折扣券,按照新的优先级排序:固定金额优先于百分比
292
- var typeA = ((_a$metadata = a.metadata) === null || _a$metadata === void 0 ? void 0 : _a$metadata.discount_card_type) || 'percent'; // 默认为百分比
293
- var typeB = ((_b$metadata = b.metadata) === null || _b$metadata === void 0 ? void 0 : _b$metadata.discount_card_type) || 'percent'; // 默认为百分比
310
+ // 辅助函数:判断是否是商品券
311
+ var isGoodPass = function isGoodPass(discount) {
312
+ return discount.tag === 'good_pass';
313
+ };
314
+
315
+ // 辅助函数:判断是否是订单级折扣卡
316
+ var isOrderLevelDiscount = function isOrderLevelDiscount(discount) {
317
+ var _discount$metadata2;
318
+ return discount.tag === 'product_discount_card' && ((_discount$metadata2 = discount.metadata) === null || _discount$metadata2 === void 0 ? void 0 : _discount$metadata2.discount_calculation_mode) === 'order_level';
319
+ };
320
+
321
+ // 辅助函数:判断是否是商品级折扣卡
322
+ var isItemLevelDiscount = function isItemLevelDiscount(discount) {
323
+ var _discount$metadata3;
324
+ return discount.tag === 'product_discount_card' && ((_discount$metadata3 = discount.metadata) === null || _discount$metadata3 === void 0 ? void 0 : _discount$metadata3.discount_calculation_mode) !== 'order_level';
325
+ };
326
+
327
+ // 辅助函数:获取优先级(数字越小优先级越高)
328
+ var getPriority = function getPriority(discount) {
329
+ var isHolder = isHolderDiscount(discount);
330
+ if (isHolder) {
331
+ if (isGoodPass(discount)) return 1; // 带有Holder的商品券
332
+ if (isItemLevelDiscount(discount)) return 2; // 带有Holder的商品级折扣卡
333
+ if (isOrderLevelDiscount(discount)) return 3; // 带有Holder的订单级折扣卡
334
+ } else {
335
+ if (isGoodPass(discount)) return 4; // Customer商品券
336
+ if (isItemLevelDiscount(discount)) return 5; // Customer商品级折扣卡
337
+ if (isOrderLevelDiscount(discount)) return 6; // Customer订单级固定金额折扣
338
+ }
339
+ return 7; // 其他情况
340
+ };
341
+
342
+ // 辅助函数:按过期时间比较
343
+ function compareByExpireTime(itemA, itemB) {
344
+ if (itemA.expire_time && itemB.expire_time) {
345
+ var timeA = new Date(itemA.expire_time).getTime();
346
+ var timeB = new Date(itemB.expire_time).getTime();
347
+ return timeA - timeB;
348
+ }
349
+ // 有过期时间的优先级高于永久的
350
+ return itemA.expire_time ? -1 : itemB.expire_time ? 1 : 0;
351
+ }
352
+
353
+ // 辅助函数:同类型折扣卡内部排序(固定金额优先于百分比,金额/折扣力度排序)
354
+ function compareDiscountCardValue(itemA, itemB) {
355
+ var _itemA$metadata, _itemB$metadata;
356
+ var typeA = ((_itemA$metadata = itemA.metadata) === null || _itemA$metadata === void 0 ? void 0 : _itemA$metadata.discount_card_type) || 'percent';
357
+ var typeB = ((_itemB$metadata = itemB.metadata) === null || _itemB$metadata === void 0 ? void 0 : _itemB$metadata.discount_card_type) || 'percent';
294
358
 
295
359
  // 1. 固定金额优先于百分比
296
360
  if (typeA === 'fixed_amount' && typeB === 'percent') return -1;
@@ -298,37 +362,46 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
298
362
 
299
363
  // 2. 都是固定金额时,金额越大越优先
300
364
  if (typeA === 'fixed_amount' && typeB === 'fixed_amount') {
301
- if (a.par_value !== b.par_value) {
302
- var valueA = new Decimal(a.par_value || 0);
303
- var valueB = new Decimal(b.par_value || 0);
365
+ if (itemA.par_value !== itemB.par_value) {
366
+ var valueA = new Decimal(itemA.par_value || 0);
367
+ var valueB = new Decimal(itemB.par_value || 0);
304
368
  return valueB.minus(valueA).toNumber(); // 金额大的在前
305
369
  }
306
370
  }
307
371
 
308
372
  // 3. 都是百分比时,折扣越大越优先(par_value越大越优先)
309
373
  if (typeA === 'percent' && typeB === 'percent') {
310
- if (a.par_value !== b.par_value) {
311
- var _valueA = new Decimal(100).minus(a.par_value || 0);
312
- var _valueB = new Decimal(100).minus(b.par_value || 0);
374
+ if (itemA.par_value !== itemB.par_value) {
375
+ var _valueA = new Decimal(100).minus(itemA.par_value || 0);
376
+ var _valueB = new Decimal(100).minus(itemB.par_value || 0);
313
377
  return _valueA.minus(_valueB).toNumber(); // 折扣大的在前
314
378
  }
315
379
  }
380
+ return 0;
381
+ }
382
+
383
+ // 1. 先按优先级排序
384
+ var priorityA = getPriority(a);
385
+ var priorityB = getPriority(b);
386
+ if (priorityA !== priorityB) {
387
+ return priorityA - priorityB;
388
+ }
316
389
 
317
- // 相同类型和金额的情况下,按照过期时间排序
390
+ // 2. 同一优先级内部排序
391
+ // 如果是商品券(优先级1或4),按过期时间排序
392
+ if (isGoodPass(a) && isGoodPass(b)) {
393
+ return compareByExpireTime(a, b);
394
+ }
395
+
396
+ // 如果是折扣卡(优先级2、3、5、6),按折扣力度排序,然后按过期时间
397
+ if (a.tag === 'product_discount_card' && b.tag === 'product_discount_card') {
398
+ var valueCompare = compareDiscountCardValue(a, b);
399
+ if (valueCompare !== 0) return valueCompare;
318
400
  return compareByExpireTime(a, b);
319
401
  }
320
402
 
321
403
  // 3. 其他情况按照过期时间排序
322
404
  return compareByExpireTime(a, b);
323
- function compareByExpireTime(itemA, itemB) {
324
- if (itemA.expire_time && itemB.expire_time) {
325
- var timeA = new Date(itemA.expire_time).getTime();
326
- var timeB = new Date(itemB.expire_time).getTime();
327
- return timeA - timeB;
328
- }
329
- // 有过期时间的优先级高于永久的
330
- return itemA.expire_time ? -1 : itemB.expire_time ? 1 : 0;
331
- }
332
405
  });
333
406
 
334
407
  // const newProductList = [];
@@ -349,8 +422,10 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
349
422
 
350
423
  // 对扁平化后的列表按价格降序排序(用于应用优惠券时优先选择高价商品)
351
424
  var sortedFlattenedList = flattenedList.sort(function (a, b) {
352
- var priceA = new Decimal(a.price || '0');
353
- var priceB = new Decimal(b.price || '0');
425
+ var _ref3, _a$original_price, _ref4, _b$original_price;
426
+ // 子商品优先使用 original_price,主商品使用 price
427
+ var priceA = new Decimal(a.type === 'bundle' ? (_ref3 = (_a$original_price = a.original_price) !== null && _a$original_price !== void 0 ? _a$original_price : a.price) !== null && _ref3 !== void 0 ? _ref3 : '0' : a.price || '0');
428
+ var priceB = new Decimal(b.type === 'bundle' ? (_ref4 = (_b$original_price = b.original_price) !== null && _b$original_price !== void 0 ? _b$original_price : b.price) !== null && _ref4 !== void 0 ? _ref4 : '0' : b.price || '0');
354
429
  if (priceA.equals(priceB)) {
355
430
  // 价格相同时,主商品优先
356
431
  if (a.type !== b.type) {
@@ -404,6 +479,187 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
404
479
  discountApplicableProducts.set(discount.id, []);
405
480
  });
406
481
 
482
+ // 预处理 order_level 固定金额折扣卡的分摊信息
483
+ // Map<discountId, Map<productId, { discountAmount, difference }>>
484
+ var orderLevelDiscountAllocations = new Map();
485
+
486
+ // 第一步:收集所有被勾选的 order_level 折扣卡及其适用商品
487
+ // Map<discountId, Set<flatItemId>> - 记录每个折扣卡适用的商品
488
+ var orderLevelDiscountApplicableItems = new Map();
489
+ // Map<flatItemId, Set<discountId>> - 记录每个商品可以被哪些折扣卡使用
490
+ var itemApplicableDiscounts = new Map();
491
+
492
+ // 辅助函数:检查商品是否对某个折扣卡可用
493
+ var checkItemApplicableForDiscount = function checkItemApplicableForDiscount(flatItem, discount) {
494
+ var _product, _product2, _product3, _flatItem$bundleItem, _flatItem$bundleItem2, _product4;
495
+ var product;
496
+ if (flatItem.type === 'main') {
497
+ product = flatItem.product;
498
+ } else {
499
+ var _flatItem$parentProdu;
500
+ product = {
501
+ _id: flatItem._id,
502
+ id: flatItem.id,
503
+ price: flatItem.price,
504
+ quantity: flatItem.quantity,
505
+ num: flatItem.num,
506
+ booking_id: flatItem.booking_id,
507
+ discount_list: flatItem.discount_list || [],
508
+ startDate: (_flatItem$parentProdu = flatItem.parentProduct) === null || _flatItem$parentProdu === void 0 ? void 0 : _flatItem$parentProdu.startDate
509
+ };
510
+ }
511
+
512
+ // 编辑的商品使用了优惠券不可用
513
+ 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 (d) {
514
+ return d.id && ['good_pass', 'discount_card', 'product_discount_card'].includes(d.tag || d.type);
515
+ })) : !(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.discount_list) !== null && _flatItem$bundleItem !== void 0 && _flatItem$bundleItem.length) && flatItem !== null && flatItem !== void 0 && (_flatItem$bundleItem2 = flatItem.bundleItem) !== null && _flatItem$bundleItem2 !== void 0 && (_flatItem$bundleItem2 = _flatItem$bundleItem2.discount_list) !== null && _flatItem$bundleItem2 !== void 0 && _flatItem$bundleItem2.every(function (d) {
516
+ return d.id;
517
+ }));
518
+ if (!isAvailableProduct) {
519
+ return false;
520
+ }
521
+
522
+ // vouchersApplicable 为 false 的商品不参与订单级别折扣均摊
523
+ if (isBoolean(product.vouchersApplicable) && !product.vouchersApplicable) {
524
+ return false;
525
+ }
526
+
527
+ // 商品价格为0时不可用
528
+ if (Number(product.price) <= 0 || !product.price) {
529
+ return false;
530
+ }
531
+ var limitedData = discount.limited_relation_product_data;
532
+
533
+ // 时间限制检查
534
+ var timeLimit = !!filterDiscountListByBookingTime([discount], (((_product4 = product) === null || _product4 === void 0 ? void 0 : _product4.startDate) || dayjs()).format('YYYY-MM-DD HH:mm:ss')).length;
535
+ if (!timeLimit) {
536
+ return false;
537
+ }
538
+
539
+ // 判断商品是否适用
540
+ var isLimitedProduct = false;
541
+ if (limitedData.type === 'product_all') {
542
+ var _limitedData$exclude_;
543
+ // 全品类,但需要检查是否被排除
544
+ if (limitedData.filter === 1 && (_limitedData$exclude_ = limitedData.exclude_product_ids) !== null && _limitedData$exclude_ !== void 0 && _limitedData$exclude_.includes(product.id)) {
545
+ isLimitedProduct = false;
546
+ } else {
547
+ isLimitedProduct = true;
548
+ }
549
+ } else if (limitedData.product_ids && limitedData.product_ids.includes(product.id)) {
550
+ isLimitedProduct = true;
551
+ }
552
+
553
+ // 套餐规则检查
554
+ var isBundleAvailable = _this3.checkPackageSubItemUsageRules(discount, flatItem);
555
+ return isLimitedProduct && isBundleAvailable;
556
+ };
557
+
558
+ // 收集所有被勾选的 order_level 折扣卡及其适用商品
559
+ var selectedOrderLevelDiscounts = sortedDiscountList.filter(function (discount) {
560
+ return isOrderLevelFixedAmountDiscount(discount) && discount.isSelected !== false;
561
+ });
562
+
563
+ // 遍历所有被勾选的 order_level 折扣卡,建立双向映射
564
+ selectedOrderLevelDiscounts.forEach(function (discount) {
565
+ var applicableItemIds = new Set();
566
+ sortedFlattenedList.forEach(function (flatItem) {
567
+ if (checkItemApplicableForDiscount(flatItem, discount)) {
568
+ applicableItemIds.add(flatItem._id);
569
+
570
+ // 记录商品可以被哪些折扣卡使用
571
+ if (!itemApplicableDiscounts.has(flatItem._id)) {
572
+ itemApplicableDiscounts.set(flatItem._id, new Set());
573
+ }
574
+ itemApplicableDiscounts.get(flatItem._id).add(discount.id);
575
+ }
576
+ });
577
+ orderLevelDiscountApplicableItems.set(discount.id, applicableItemIds);
578
+ });
579
+
580
+ // 专属折扣卡定义:限定了特定商品(product_ids),而不是全品类(product_all)的折扣卡
581
+ // 如果专属折扣卡被勾选,它适用的商品会被占用,其他折扣卡均摊时应排除这些商品
582
+ // Map<flatItemId, discountId> - 记录被占用的商品及其对应的折扣卡
583
+ var occupiedItems = new Map();
584
+
585
+ // 按排序顺序遍历折扣卡,判断哪些是专属折扣卡
586
+ selectedOrderLevelDiscounts.forEach(function (discount) {
587
+ var limitedData = discount.limited_relation_product_data;
588
+
589
+ // 判断是否是专属折扣卡(只能用于特定商品,而不是全品类)
590
+ var isExclusiveDiscount = limitedData.type !== 'product_all';
591
+ if (isExclusiveDiscount) {
592
+ // 专属折扣卡:它适用的所有商品都会被它占用
593
+ var applicableItems = orderLevelDiscountApplicableItems.get(discount.id);
594
+ if (applicableItems) {
595
+ applicableItems.forEach(function (itemId) {
596
+ // 如果商品还没被占用,则标记为被当前折扣卡占用
597
+ if (!occupiedItems.has(itemId)) {
598
+ occupiedItems.set(itemId, discount.id);
599
+ }
600
+ });
601
+ }
602
+ }
603
+ });
604
+ console.log('occupiedItems', occupiedItems, 'orderLevelDiscountApplicableItems', orderLevelDiscountApplicableItems);
605
+
606
+ // 🔥 第三步:识别 order_level 折扣卡并预计算分摊(排除被其他专属折扣卡占用的商品)
607
+ sortedDiscountList.forEach(function (discount) {
608
+ if (!isOrderLevelFixedAmountDiscount(discount)) {
609
+ return;
610
+ }
611
+
612
+ // 收集该折扣卡适用的商品(排除被其他专属折扣卡占用的商品)
613
+ var applicableProducts = [];
614
+ sortedFlattenedList.forEach(function (flatItem) {
615
+ var _flatItem$parentProdu2;
616
+ // 🔥 检查该商品是否被其他专属折扣卡占用
617
+ var occupyingDiscountId = occupiedItems.get(flatItem._id);
618
+ if (occupyingDiscountId !== undefined && occupyingDiscountId !== discount.id) {
619
+ // 该商品被其他专属折扣卡占用,跳过
620
+ return;
621
+ }
622
+ if (!checkItemApplicableForDiscount(flatItem, discount)) {
623
+ return;
624
+ }
625
+ var product;
626
+ if (flatItem.type === 'main') {
627
+ product = flatItem.product;
628
+ } else {
629
+ product = {
630
+ _id: flatItem._id,
631
+ id: flatItem.id,
632
+ price: flatItem.price,
633
+ quantity: flatItem.quantity,
634
+ num: flatItem.num
635
+ };
636
+ }
637
+
638
+ // 对于 bundle 子商品,quantity 需要乘以主商品的购买数量
639
+ var quantity = flatItem.type === 'main' ? product.quantity || 1 : (product.num || 1) * (((_flatItem$parentProdu2 = flatItem.parentProduct) === null || _flatItem$parentProdu2 === void 0 ? void 0 : _flatItem$parentProdu2.quantity) || 1);
640
+
641
+ // 传递 parentQuantity 用于差值处理时判断是否是真正的"数量为1"
642
+ var productData = {
643
+ productId: flatItem._id,
644
+ amount: Number(product.price || 0),
645
+ quantity: quantity
646
+ };
647
+
648
+ // 子商品需要传递主商品数量
649
+ if (flatItem.type === 'bundle') {
650
+ var _flatItem$parentProdu3;
651
+ productData.parentQuantity = ((_flatItem$parentProdu3 = flatItem.parentProduct) === null || _flatItem$parentProdu3 === void 0 ? void 0 : _flatItem$parentProdu3.quantity) || 1;
652
+ }
653
+ applicableProducts.push(productData);
654
+ });
655
+
656
+ // 计算分摊
657
+ if (applicableProducts.length > 0) {
658
+ var allocation = calculateOrderLevelDiscountAllocation(discount, applicableProducts);
659
+ orderLevelDiscountAllocations.set(discount.id, allocation);
660
+ }
661
+ });
662
+
407
663
  // 处理后的商品列表(按排序后的顺序处理,记录优惠信息)
408
664
  var processedProductsMap = new Map();
409
665
 
@@ -419,6 +675,7 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
419
675
  product = flatItem.product;
420
676
  originProduct = flatItem.originProduct;
421
677
  } else {
678
+ var _flatItem$parentProdu4;
422
679
  // bundle子商品:构造虚拟商品对象
423
680
  product = {
424
681
  _id: flatItem._id,
@@ -429,12 +686,13 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
429
686
  total: flatItem.total,
430
687
  origin_total: flatItem.origin_total,
431
688
  booking_id: flatItem.booking_id,
432
- discount_list: flatItem.discount_list || []
689
+ discount_list: flatItem.discount_list || [],
690
+ startDate: (_flatItem$parentProdu4 = flatItem.parentProduct) === null || _flatItem$parentProdu4 === void 0 ? void 0 : _flatItem$parentProdu4.startDate
433
691
  };
434
692
  originProduct = flatItem.originProduct;
435
693
  }
436
694
  addModeDiscount.forEach(function (discount) {
437
- var _product, _product2, _product3, _product4, _flatItem$bundleItem;
695
+ var _product5, _product6, _product7, _product8, _flatItem$bundleItem3, _flatItem$bundleItem4;
438
696
  var limitedData = discount === null || discount === void 0 ? void 0 : discount.limited_relation_product_data;
439
697
  // 拿到discount配置的holder信息 product信息 product.holder 加在 isLimitedProduct
440
698
  var _tempVar = (flatItem === null || flatItem === void 0 ? void 0 : flatItem.type) === 'bundle' ? flatItem === null || flatItem === void 0 ? void 0 : flatItem.parentProduct : flatItem === null || flatItem === void 0 ? void 0 : flatItem.product;
@@ -443,21 +701,23 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
443
701
  holder_id: (_tempVar === null || _tempVar === void 0 ? void 0 : _tempVar.holder_id) || product.holder_id
444
702
  }, holders);
445
703
  var timeLimit = true;
446
- timeLimit = !!filterDiscountListByBookingTime([discount], (((_product = product) === null || _product === void 0 ? void 0 : _product.startDate) || dayjs()).format('YYYY-MM-DD HH:mm:ss')).length;
704
+ timeLimit = !!filterDiscountListByBookingTime([discount], (((_product5 = product) === null || _product5 === void 0 ? void 0 : _product5.startDate) || dayjs()).format('YYYY-MM-DD HH:mm:ss')).length;
447
705
  // 是符合折扣的商品
448
706
  var isLimitedProduct = (limitedData.type === 'product_all' || limitedData.product_ids && limitedData.product_ids.includes(product.id)) && isHolderMatch;
449
707
 
450
708
  // 编辑的商品 使用了优惠券不可用
451
- 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) {
709
+ var isAvailableProduct = flatItem.type === 'main' ? !((_product6 = product) !== null && _product6 !== void 0 && _product6.booking_id && (_product7 = product) !== null && _product7 !== void 0 && (_product7 = _product7.discount_list) !== null && _product7 !== void 0 && _product7.length && (_product8 = product) !== null && _product8 !== void 0 && (_product8 = _product8.discount_list) !== null && _product8 !== void 0 && _product8.every(function (discount) {
452
710
  return discount.id && ['good_pass', 'discount_card', 'product_discount_card'].includes(discount.tag || discount.type);
453
- })) : !(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);
711
+ })) : !(flatItem !== null && flatItem !== void 0 && flatItem.booking_id && !!(flatItem !== null && flatItem !== void 0 && (_flatItem$bundleItem3 = flatItem.bundleItem) !== null && _flatItem$bundleItem3 !== void 0 && (_flatItem$bundleItem3 = _flatItem$bundleItem3.discount_list) !== null && _flatItem$bundleItem3 !== void 0 && _flatItem$bundleItem3.length) && flatItem !== null && flatItem !== void 0 && (_flatItem$bundleItem4 = flatItem.bundleItem) !== null && _flatItem$bundleItem4 !== void 0 && (_flatItem$bundleItem4 = _flatItem$bundleItem4.discount_list) !== null && _flatItem$bundleItem4 !== void 0 && _flatItem$bundleItem4.every(function (discount) {
712
+ return discount.id;
713
+ }));
454
714
 
455
715
  // 套餐是否可用判断
456
716
  var isBundleAvailable = _this3.checkPackageSubItemUsageRules(discount, flatItem);
457
717
 
458
718
  // 判断优惠券是否适用于该商品
459
719
  if (isAvailableProduct && isLimitedProduct && isBundleAvailable && timeLimit) {
460
- var _discountApplicabilit, _discount$metadata;
720
+ var _discountApplicabilit, _discount$metadata4, _discount$metadata5;
461
721
  // 记录此优惠券适用的商品
462
722
  (_discountApplicabilit = discountApplicability.get(discount.id)) === null || _discountApplicabilit === void 0 || _discountApplicabilit.push(product.id);
463
723
 
@@ -473,8 +733,9 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
473
733
  type: discountType,
474
734
  tag: discountType,
475
735
  discount: {
476
- discount_card_type: discount === null || discount === void 0 || (_discount$metadata = discount.metadata) === null || _discount$metadata === void 0 ? void 0 : _discount$metadata.discount_card_type,
736
+ discount_card_type: discount === null || discount === void 0 || (_discount$metadata4 = discount.metadata) === null || _discount$metadata4 === void 0 ? void 0 : _discount$metadata4.discount_card_type,
477
737
  fixed_amount: product.price,
738
+ discount_calculation_mode: discount === null || discount === void 0 || (_discount$metadata5 = discount.metadata) === null || _discount$metadata5 === void 0 ? void 0 : _discount$metadata5.discount_calculation_mode,
478
739
  resource_id: discount.id,
479
740
  title: discount.format_title,
480
741
  original_amount: product.price || product.origin_total,
@@ -497,14 +758,14 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
497
758
  // 然后再处理应用哪些优惠券,此时只考虑filteredDiscountList中的优惠券
498
759
  // 🔥 使用扁平化后的列表进行处理
499
760
  sortedFlattenedList.forEach(function (flatItem, index) {
500
- var _product5, _product$discount_lis2, _product6;
761
+ var _product9, _product$discount_lis2, _product10;
501
762
  // 获取商品数据
502
763
  var product, originProduct;
503
764
  if (flatItem.type === 'main') {
504
765
  product = flatItem.product;
505
766
  originProduct = flatItem.originProduct;
506
767
  } else {
507
- var _flatItem$bundleItem2, _flatItem$bundleItem3, _flatItem$bundleItem4;
768
+ var _flatItem$bundleItem5, _flatItem$bundleItem6, _flatItem$bundleItem7, _flatItem$parentProdu5;
508
769
  // bundle子商品
509
770
  product = {
510
771
  _id: flatItem._id,
@@ -513,16 +774,17 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
513
774
  quantity: flatItem.quantity,
514
775
  num: flatItem.num,
515
776
  total: flatItem.total,
516
- original_price: flatItem === null || flatItem === void 0 || (_flatItem$bundleItem2 = flatItem.bundleItem) === null || _flatItem$bundleItem2 === void 0 ? void 0 : _flatItem$bundleItem2.original_price,
517
- origin_total: flatItem === null || flatItem === void 0 || (_flatItem$bundleItem3 = flatItem.bundleItem) === null || _flatItem$bundleItem3 === void 0 ? void 0 : _flatItem$bundleItem3.original_price,
777
+ original_price: flatItem === null || flatItem === void 0 || (_flatItem$bundleItem5 = flatItem.bundleItem) === null || _flatItem$bundleItem5 === void 0 ? void 0 : _flatItem$bundleItem5.original_price,
778
+ origin_total: flatItem === null || flatItem === void 0 || (_flatItem$bundleItem6 = flatItem.bundleItem) === null || _flatItem$bundleItem6 === void 0 ? void 0 : _flatItem$bundleItem6.original_price,
518
779
  booking_id: flatItem.booking_id,
519
- discount_list: (flatItem === null || flatItem === void 0 || (_flatItem$bundleItem4 = flatItem.bundleItem) === null || _flatItem$bundleItem4 === void 0 ? void 0 : _flatItem$bundleItem4.discount_list) || []
780
+ discount_list: (flatItem === null || flatItem === void 0 || (_flatItem$bundleItem7 = flatItem.bundleItem) === null || _flatItem$bundleItem7 === void 0 ? void 0 : _flatItem$bundleItem7.discount_list) || [],
781
+ startDate: (_flatItem$parentProdu5 = flatItem.parentProduct) === null || _flatItem$parentProdu5 === void 0 ? void 0 : _flatItem$parentProdu5.startDate
520
782
  };
521
783
  originProduct = flatItem.originProduct;
522
784
  }
523
785
 
524
786
  // 已有优惠的商品跳过
525
- 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) {
787
+ if ((_product9 = product) !== null && _product9 !== void 0 && _product9.booking_id && (_product$discount_lis2 = product.discount_list) !== null && _product$discount_lis2 !== void 0 && _product$discount_lis2.length && (_product10 = product) !== null && _product10 !== void 0 && (_product10 = _product10.discount_list) !== null && _product10 !== void 0 && _product10.every(function (discount) {
526
788
  return discount.id && ['good_pass', 'discount_card', 'product_discount_card'].includes(discount.tag || discount.type);
527
789
  })) {
528
790
  if (flatItem.type === 'main') {
@@ -540,15 +802,12 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
540
802
  var _product$discount_lis3, _product$discount_lis4;
541
803
  // 如果商品价格为 0,其实不需要使用任何优惠券,直接 return true
542
804
  // 商品券时主商品价格为0不可用
543
- 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) {
544
- var _n$discount;
545
- return ((_n$discount = n.discount) === null || _n$discount === void 0 ? void 0 : _n$discount.resource_id) === discount.id;
546
- })) && (discount.tag || discount.type) === 'good_pass') return false;
805
+ if ((Number(product.price) <= 0 || !product.price) && !((_product$discount_lis3 = product.discount_list) !== null && _product$discount_lis3 !== void 0 && _product$discount_lis3.length) && (discount.tag || discount.type) === 'good_pass') return false;
547
806
 
548
807
  // 折扣卡商品价格为0时不可用
549
808
  if ((Number(product.price) <= 0 || !product.price) && !((_product$discount_lis4 = product.discount_list) !== null && _product$discount_lis4 !== void 0 && _product$discount_lis4.find(function (n) {
550
- var _n$discount2;
551
- return ((_n$discount2 = n.discount) === null || _n$discount2 === void 0 ? void 0 : _n$discount2.resource_id) === discount.id;
809
+ var _n$discount;
810
+ return ((_n$discount = n.discount) === null || _n$discount === void 0 ? void 0 : _n$discount.resource_id) === discount.id;
552
811
  })) && (discount.tag || discount.type) !== 'good_pass') return false;
553
812
  // 如果优惠券已被使用,则跳过
554
813
  var targetUsedDiscounts = usedDiscounts.get(discount.id);
@@ -597,12 +856,12 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
597
856
  // 如果是手动折扣,则不适用优惠券
598
857
  var isManualDiscount = false;
599
858
  if (flatItem.type === 'main') {
600
- var _product$discount_lis5, _product7, _product7$every;
859
+ var _product$discount_lis5, _product11, _product11$every;
601
860
  // 主商品:判断自身是否手动折扣
602
861
  isManualDiscount = typeof product.isManualDiscount === 'boolean' ? product.isManualDiscount : product.total != product.origin_total && (product.bundle || []).every(function (item) {
603
- var _ref3;
604
- return !((_ref3 = item.discount_list || []) !== null && _ref3 !== void 0 && _ref3.length);
605
- }) && (!((_product$discount_lis5 = product.discount_list) !== null && _product$discount_lis5 !== void 0 && _product$discount_lis5.length) || ((_product7 = product) === null || _product7 === void 0 || (_product7 = _product7.discount_list) === null || _product7 === void 0 || (_product7$every = _product7.every) === null || _product7$every === void 0 ? void 0 : _product7$every.call(_product7, function (item) {
862
+ var _ref5;
863
+ return !((_ref5 = item.discount_list || []) !== null && _ref5 !== void 0 && _ref5.length);
864
+ }) && (!((_product$discount_lis5 = product.discount_list) !== null && _product$discount_lis5 !== void 0 && _product$discount_lis5.length) || ((_product11 = product) === null || _product11 === void 0 || (_product11 = _product11.discount_list) === null || _product11 === void 0 || (_product11$every = _product11.every) === null || _product11$every === void 0 ? void 0 : _product11$every.call(_product11, function (item) {
606
865
  return item.type === 'product';
607
866
  })));
608
867
  } else {
@@ -611,8 +870,8 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
611
870
  if (parentProduct) {
612
871
  var _parentProduct$discou, _parentProduct$discou2, _parentProduct$discou3;
613
872
  isManualDiscount = typeof parentProduct.isManualDiscount === 'boolean' ? parentProduct.isManualDiscount : parentProduct.total != parentProduct.origin_total && (parentProduct.bundle || []).every(function (item) {
614
- var _ref4;
615
- return !((_ref4 = item.discount_list || []) !== null && _ref4 !== void 0 && _ref4.length);
873
+ var _ref6;
874
+ return !((_ref6 = item.discount_list || []) !== null && _ref6 !== void 0 && _ref6.length);
616
875
  }) && (!((_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) {
617
876
  return item.type === 'product';
618
877
  })));
@@ -631,11 +890,11 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
631
890
  }
632
891
  // bundle子商品:检查自己的 discount_list 或父主商品的 discount_list
633
892
  if (flatItem.type === 'bundle') {
634
- var _product$discount_lis7, _flatItem$parentProdu;
893
+ var _product$discount_lis7, _flatItem$parentProdu6;
635
894
  if ((_product$discount_lis7 = product.discount_list) !== null && _product$discount_lis7 !== void 0 && _product$discount_lis7.some(function (item) {
636
895
  var _item$discount2;
637
896
  return ((_item$discount2 = item.discount) === null || _item$discount2 === void 0 ? void 0 : _item$discount2.resource_id) === options.discountId;
638
- }) || (_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) {
897
+ }) || (_flatItem$parentProdu6 = flatItem.parentProduct) !== null && _flatItem$parentProdu6 !== void 0 && (_flatItem$parentProdu6 = _flatItem$parentProdu6.discount_list) !== null && _flatItem$parentProdu6 !== void 0 && _flatItem$parentProdu6.some(function (item) {
639
898
  var _item$discount3;
640
899
  return ((_item$discount3 = item.discount) === null || _item$discount3 === void 0 ? void 0 : _item$discount3.resource_id) === options.discountId;
641
900
  })) {
@@ -657,14 +916,14 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
657
916
  }
658
917
  // bundle子商品:检查自己的 discount_list 或父主商品的 discount_list
659
918
  if (flatItem.type === 'bundle') {
660
- var _product$discount_lis9, _flatItem$parentProdu2;
919
+ var _product$discount_lis9, _flatItem$parentProdu7;
661
920
  if ((_product$discount_lis9 = product.discount_list) !== null && _product$discount_lis9 !== void 0 && _product$discount_lis9.some(function (item) {
662
921
  var _options$selectedList2;
663
922
  return options === null || options === void 0 || (_options$selectedList2 = options.selectedList) === null || _options$selectedList2 === void 0 ? void 0 : _options$selectedList2.some(function (n) {
664
923
  var _item$discount5;
665
924
  return n.discountId === ((_item$discount5 = item.discount) === null || _item$discount5 === void 0 ? void 0 : _item$discount5.resource_id);
666
925
  });
667
- }) || (_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) {
926
+ }) || (_flatItem$parentProdu7 = flatItem.parentProduct) !== null && _flatItem$parentProdu7 !== void 0 && (_flatItem$parentProdu7 = _flatItem$parentProdu7.discount_list) !== null && _flatItem$parentProdu7 !== void 0 && _flatItem$parentProdu7.some(function (item) {
668
927
  var _options$selectedList3;
669
928
  return options === null || options === void 0 || (_options$selectedList3 = options.selectedList) === null || _options$selectedList3 === void 0 ? void 0 : _options$selectedList3.some(function (n) {
670
929
  var _item$discount6;
@@ -751,7 +1010,7 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
751
1010
  }));
752
1011
  }
753
1012
  for (var i = 0; i < splitCount; i++) {
754
- var _product$discount_lis10, _originProduct, _selectedDiscount$met;
1013
+ var _product$discount_lis10, _originProduct, _selectedDiscount$met, _selectedDiscount$met2;
755
1014
  // 如果用过折扣卡,也就不存在拆分的情况了,这里直接使用上面计算出来的折扣卡
756
1015
  var _selectedDiscount = selectedDiscountCard || applicableDiscounts[i];
757
1016
  // 标记优惠券为已使用
@@ -772,16 +1031,32 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
772
1031
  }
773
1032
 
774
1033
  // 计算使用折扣卡/商品券以后,单个商品的总 total
775
- var targetProductTotal = getDiscountAmount(_selectedDiscount, product.price, product.price);
1034
+ // 🔥 检查是否是 order_level 固定金额折扣卡
1035
+ var isOrderLevel = isOrderLevelFixedAmountDiscount(_selectedDiscount);
1036
+ var orderLevelAllocation = isOrderLevel ? orderLevelDiscountAllocations.get(_selectedDiscount.id) : null;
1037
+ var productAllocation = orderLevelAllocation === null || orderLevelAllocation === void 0 ? void 0 : orderLevelAllocation.get(flatItem._id);
1038
+ var targetProductTotal = void 0;
1039
+ var amount = void 0;
1040
+ var productDiscountDifference = void 0;
1041
+ if (isOrderLevel && productAllocation) {
1042
+ // order_level:使用预计算的分摊金额
1043
+ amount = productAllocation.discountAmount;
1044
+ productDiscountDifference = productAllocation.difference;
1045
+ targetProductTotal = Math.max(new Decimal(product.price).minus(amount).toNumber(), 0);
1046
+ } else {
1047
+ // item_level 或其他类型:使用原有逻辑
1048
+ targetProductTotal = getDiscountAmount(_selectedDiscount, product.price, product.price);
1049
+ amount = new Decimal(product.price).minus(new Decimal(targetProductTotal)).toNumber();
1050
+ }
776
1051
  var discountType = _selectedDiscount.tag || _selectedDiscount.type;
777
1052
  var isGoodPass = discountType === 'good_pass';
778
- var amount = new Decimal(product.price).minus(new Decimal(targetProductTotal)).toNumber();
779
1053
  var discountDetail = {
780
1054
  amount: amount,
781
1055
  type: _selectedDiscount.tag === 'product_discount_card' ? 'discount_card' : discountType,
782
1056
  discount: {
783
1057
  discount_card_type: _selectedDiscount === null || _selectedDiscount === void 0 || (_selectedDiscount$met = _selectedDiscount.metadata) === null || _selectedDiscount$met === void 0 ? void 0 : _selectedDiscount$met.discount_card_type,
784
1058
  fixed_amount: amount,
1059
+ discount_calculation_mode: _selectedDiscount === null || _selectedDiscount === void 0 || (_selectedDiscount$met2 = _selectedDiscount.metadata) === null || _selectedDiscount$met2 === void 0 ? void 0 : _selectedDiscount$met2.discount_calculation_mode,
785
1060
  resource_id: _selectedDiscount.id,
786
1061
  title: _selectedDiscount.format_title,
787
1062
  original_amount: product.price,
@@ -790,9 +1065,11 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
790
1065
  },
791
1066
  // 前端使用的num数量,为了计算优惠金额
792
1067
  _num: isGoodPass ? 1 : product.num,
793
- metadata: {
1068
+ metadata: _objectSpread({
794
1069
  num: 1
795
- }
1070
+ }, productDiscountDifference !== undefined && {
1071
+ product_discount_difference: productDiscountDifference
1072
+ })
796
1073
  };
797
1074
  appliedProducts.push(discountDetail);
798
1075
  appliedDiscountProducts.set(_selectedDiscount.id, appliedProducts);
@@ -847,6 +1124,7 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
847
1124
 
848
1125
  // 生成有折扣的商品(每张商品券对应 num: 1)
849
1126
  for (var _i = 0; _i < discountNum; _i++) {
1127
+ var _selectedDiscount2$me;
850
1128
  var _selectedDiscount2 = applicableDiscounts[_i];
851
1129
  usedDiscounts.set(_selectedDiscount2.id, true);
852
1130
 
@@ -857,6 +1135,7 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
857
1135
  type: 'good_pass',
858
1136
  discount: {
859
1137
  fixed_amount: product.origin_total,
1138
+ discount_calculation_mode: _selectedDiscount2 === null || _selectedDiscount2 === void 0 || (_selectedDiscount2$me = _selectedDiscount2.metadata) === null || _selectedDiscount2$me === void 0 ? void 0 : _selectedDiscount2$me.discount_calculation_mode,
860
1139
  resource_id: _selectedDiscount2.id,
861
1140
  title: _selectedDiscount2.format_title,
862
1141
  original_amount: product.origin_total,
@@ -900,33 +1179,53 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
900
1179
  }));
901
1180
  }
902
1181
  } else {
903
- var _selectedDiscount3$me, _flatItem$parentProdu3;
1182
+ var _selectedDiscount3$me, _selectedDiscount3$me2, _flatItem$parentProdu8;
904
1183
  // 折扣卡:不拆分数量,直接应用
905
1184
  var _selectedDiscount3 = selectedDiscountCard || applicableDiscounts[0];
906
1185
  usedDiscounts.set(_selectedDiscount3.id, true);
907
1186
  var _productOriginTotal = product.original_price || product.price || 0;
908
- var _targetProductTotal = getDiscountAmount(_selectedDiscount3, _productOriginTotal, _productOriginTotal);
909
1187
 
1188
+ // 🔥 检查是否是 order_level 固定金额折扣卡
1189
+ var _isOrderLevel = isOrderLevelFixedAmountDiscount(_selectedDiscount3);
1190
+ var _orderLevelAllocation = _isOrderLevel ? orderLevelDiscountAllocations.get(_selectedDiscount3.id) : null;
1191
+ var _productAllocation = _orderLevelAllocation === null || _orderLevelAllocation === void 0 ? void 0 : _orderLevelAllocation.get(flatItem._id);
1192
+ var _targetProductTotal;
1193
+ var fixedAmountPerItem;
1194
+ var _productDiscountDifference;
1195
+ if (_isOrderLevel && _productAllocation) {
1196
+ // order_level:使用预计算的单价折扣金额(已经是单价,无需再除以数量)
1197
+ fixedAmountPerItem = _productAllocation.discountAmount;
1198
+ _productDiscountDifference = _productAllocation.difference;
1199
+ _targetProductTotal = Math.max(new Decimal(_productOriginTotal).minus(fixedAmountPerItem).toNumber(), 0);
1200
+ } else {
1201
+ // item_level 或其他类型:使用原有逻辑
1202
+ _targetProductTotal = getDiscountAmount(_selectedDiscount3, _productOriginTotal, _productOriginTotal);
1203
+ fixedAmountPerItem = new Decimal(_productOriginTotal).minus(_targetProductTotal).toNumber();
1204
+ }
1205
+ debugger;
910
1206
  // 🔥 使用当前的 _id 作为唯一标识
911
1207
  var _uniqueId = flatItem._id;
912
1208
  var _discountDetail2 = {
913
- amount: new Decimal(_productOriginTotal).minus(_targetProductTotal).toNumber() * (product.num || 1),
1209
+ amount: fixedAmountPerItem * (product.num || 1),
914
1210
  type: _selectedDiscount3.tag === 'product_discount_card' ? 'discount_card' : _selectedDiscount3.tag,
915
1211
  discount: {
916
1212
  discount_card_type: _selectedDiscount3 === null || _selectedDiscount3 === void 0 || (_selectedDiscount3$me = _selectedDiscount3.metadata) === null || _selectedDiscount3$me === void 0 ? void 0 : _selectedDiscount3$me.discount_card_type,
917
- fixed_amount: new Decimal(_productOriginTotal).minus(_targetProductTotal).toNumber(),
1213
+ fixed_amount: fixedAmountPerItem,
1214
+ discount_calculation_mode: _selectedDiscount3 === null || _selectedDiscount3 === void 0 || (_selectedDiscount3$me2 = _selectedDiscount3.metadata) === null || _selectedDiscount3$me2 === void 0 ? void 0 : _selectedDiscount3$me2.discount_calculation_mode,
918
1215
  resource_id: _selectedDiscount3.id,
919
1216
  title: _selectedDiscount3.format_title,
920
1217
  original_amount: product.original_price,
921
1218
  product_id: product.id,
922
1219
  percent: _selectedDiscount3.par_value
923
1220
  },
924
- metadata: {
1221
+ metadata: _objectSpread({
925
1222
  // 🔥 使用唯一的 _id
926
1223
  custom_product_bundle_map_id: _uniqueId,
927
1224
  num: product.num || 1
928
- },
929
- _num: (product.num || 1) * ((flatItem === null || flatItem === void 0 || (_flatItem$parentProdu3 = flatItem.parentProduct) === null || _flatItem$parentProdu3 === void 0 ? void 0 : _flatItem$parentProdu3.num) || 1)
1225
+ }, _productDiscountDifference !== undefined && {
1226
+ product_discount_difference: _productDiscountDifference
1227
+ }),
1228
+ _num: (product.num || 1) * ((flatItem === null || flatItem === void 0 || (_flatItem$parentProdu8 = flatItem.parentProduct) === null || _flatItem$parentProdu8 === void 0 ? void 0 : _flatItem$parentProdu8.num) || 1)
930
1229
  };
931
1230
 
932
1231
  // 记录实际应用的折扣
@@ -935,7 +1234,7 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
935
1234
  appliedDiscountProducts.set(_selectedDiscount3.id, _appliedProducts2);
936
1235
  processedItems.push(_objectSpread(_objectSpread({}, flatItem), {}, {
937
1236
  total: _targetProductTotal,
938
- price: new Decimal(_productOriginTotal || 0).minus(_discountDetail2.discount.fixed_amount).toNumber(),
1237
+ price: new Decimal(_productOriginTotal || 0).minus(fixedAmountPerItem).toNumber(),
939
1238
  discount_list: [_discountDetail2],
940
1239
  processed: true
941
1240
  }));
@@ -1045,10 +1344,10 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
1045
1344
  var updatedDiscountList = (item.discount_list || []).map(function (discount) {
1046
1345
  var _item$metadata;
1047
1346
  return _objectSpread(_objectSpread({}, discount), {}, {
1048
- metadata: {
1347
+ metadata: _objectSpread(_objectSpread({}, discount.metadata), {}, {
1049
1348
  num: item.num,
1050
1349
  custom_product_bundle_map_id: (_item$metadata = item.metadata) === null || _item$metadata === void 0 ? void 0 : _item$metadata.custom_product_bundle_map_id
1051
- }
1350
+ })
1052
1351
  // num: item.num, // 使用拆分后的 num
1053
1352
  });
1054
1353
  });
@@ -1068,10 +1367,10 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
1068
1367
  var _updatedDiscountList = (item.discount_list || []).map(function (discount) {
1069
1368
  var _item$metadata2;
1070
1369
  return _objectSpread(_objectSpread({}, discount), {}, {
1071
- metadata: {
1370
+ metadata: _objectSpread(_objectSpread({}, discount.metadata), {}, {
1072
1371
  num: item.num,
1073
1372
  custom_product_bundle_map_id: (_item$metadata2 = item.metadata) === null || _item$metadata2 === void 0 ? void 0 : _item$metadata2.custom_product_bundle_map_id
1074
- }
1373
+ })
1075
1374
  // num: item.num, // 使用当前的 num
1076
1375
  });
1077
1376
  });
@@ -1094,18 +1393,18 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
1094
1393
 
1095
1394
  // 🔥 更新主商品自己的 discount_list 中的 num(quantity=1)
1096
1395
  var updatedMainDiscountList = mainProductData.discount_list.map(function (discount) {
1097
- var _discount$metadata2, _discount$metadata3;
1098
- if (discount !== null && discount !== void 0 && (_discount$metadata2 = discount.metadata) !== null && _discount$metadata2 !== void 0 && _discount$metadata2.custom_product_bundle_map_id) {
1396
+ var _discount$metadata6, _discount$metadata7;
1397
+ if (discount !== null && discount !== void 0 && (_discount$metadata6 = discount.metadata) !== null && _discount$metadata6 !== void 0 && _discount$metadata6.custom_product_bundle_map_id) {
1099
1398
  // bundle的discount_list保持不变
1100
1399
  return discount;
1101
1400
  }
1102
1401
  // 主商品自己的discount,更新num为1
1103
1402
  return _objectSpread(_objectSpread({}, discount), {}, {
1104
1403
  // num: 1,
1105
- metadata: {
1106
- 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,
1404
+ metadata: _objectSpread(_objectSpread({}, discount.metadata), {}, {
1405
+ custom_product_bundle_map_id: discount === null || discount === void 0 || (_discount$metadata7 = discount.metadata) === null || _discount$metadata7 === void 0 ? void 0 : _discount$metadata7.custom_product_bundle_map_id,
1107
1406
  num: 1
1108
- }
1407
+ })
1109
1408
  });
1110
1409
  });
1111
1410
 
@@ -1168,17 +1467,17 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
1168
1467
 
1169
1468
  // 🔥 更新主商品自己的 discount_list 中的 num(quantity=原quantity-1)
1170
1469
  var updatedMainDiscountListOriginal = mainProductData.discount_list.map(function (discount) {
1171
- var _discount$metadata4, _discount$metadata5;
1172
- if (discount !== null && discount !== void 0 && (_discount$metadata4 = discount.metadata) !== null && _discount$metadata4 !== void 0 && _discount$metadata4.custom_product_bundle_map_id) {
1470
+ var _discount$metadata8, _discount$metadata9;
1471
+ if (discount !== null && discount !== void 0 && (_discount$metadata8 = discount.metadata) !== null && _discount$metadata8 !== void 0 && _discount$metadata8.custom_product_bundle_map_id) {
1173
1472
  // bundle的discount_list保持不变
1174
1473
  return discount;
1175
1474
  }
1176
1475
  // 主商品自己的discount,更新num为 mainProductQuantity - 1
1177
1476
  return _objectSpread(_objectSpread({}, discount), {}, {
1178
- metadata: {
1477
+ metadata: _objectSpread(_objectSpread({}, discount.metadata), {}, {
1179
1478
  num: mainProductQuantity - 1,
1180
- 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
1181
- }
1479
+ custom_product_bundle_map_id: discount === null || discount === void 0 || (_discount$metadata9 = discount.metadata) === null || _discount$metadata9 === void 0 ? void 0 : _discount$metadata9.custom_product_bundle_map_id
1480
+ })
1182
1481
  // num: mainProductQuantity - 1,
1183
1482
  });
1184
1483
  });
@@ -1242,12 +1541,12 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
1242
1541
  processedBundleItems.forEach(function (item) {
1243
1542
  // 🔥 更新 discount_list 中的 num,使其与拆分后的 item.num 一致
1244
1543
  var updatedDiscountList = (item.discount_list || []).map(function (discount) {
1245
- var _discount$metadata6;
1544
+ var _discount$metadata10;
1246
1545
  return _objectSpread(_objectSpread({}, discount), {}, {
1247
- metadata: {
1546
+ metadata: _objectSpread(_objectSpread({}, discount.metadata), {}, {
1248
1547
  num: item.num,
1249
- 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
1250
- }
1548
+ custom_product_bundle_map_id: discount === null || discount === void 0 || (_discount$metadata10 = discount.metadata) === null || _discount$metadata10 === void 0 ? void 0 : _discount$metadata10.custom_product_bundle_map_id
1549
+ })
1251
1550
  // num: item.num, // 使用拆分后的 num
1252
1551
  });
1253
1552
  });
@@ -1434,10 +1733,10 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
1434
1733
  return true; // 单独购买时可用
1435
1734
  }
1436
1735
  if (isBundleItem) {
1437
- var _flatItem$bundleItem5, _flatItem$bundleItem6;
1736
+ var _flatItem$bundleItem8, _flatItem$bundleItem9;
1438
1737
  // 套餐中购买时,判断是否为原价
1439
- var priceType = (_flatItem$bundleItem5 = flatItem.bundleItem) === null || _flatItem$bundleItem5 === void 0 ? void 0 : _flatItem$bundleItem5.price_type;
1440
- var priceTypeExt = (_flatItem$bundleItem6 = flatItem.bundleItem) === null || _flatItem$bundleItem6 === void 0 ? void 0 : _flatItem$bundleItem6.price_type_ext;
1738
+ var priceType = (_flatItem$bundleItem8 = flatItem.bundleItem) === null || _flatItem$bundleItem8 === void 0 ? void 0 : _flatItem$bundleItem8.price_type;
1739
+ var priceTypeExt = (_flatItem$bundleItem9 = flatItem.bundleItem) === null || _flatItem$bundleItem9 === void 0 ? void 0 : _flatItem$bundleItem9.price_type_ext;
1441
1740
  // original_price 对应:
1442
1741
  // 1. price_type: "markup" && price_type_ext: "product_price"
1443
1742
  // markup_price 对应:
@@ -1475,10 +1774,10 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
1475
1774
  return false; // 单独购买时不可用
1476
1775
  }
1477
1776
  if (isBundleItem) {
1478
- var _flatItem$bundleItem7, _flatItem$bundleItem8;
1777
+ var _flatItem$bundleItem10, _flatItem$bundleItem11;
1479
1778
  // 套餐中购买时,判断是否为原价
1480
- var _priceType = (_flatItem$bundleItem7 = flatItem.bundleItem) === null || _flatItem$bundleItem7 === void 0 ? void 0 : _flatItem$bundleItem7.price_type;
1481
- var _priceTypeExt = (_flatItem$bundleItem8 = flatItem.bundleItem) === null || _flatItem$bundleItem8 === void 0 ? void 0 : _flatItem$bundleItem8.price_type_ext;
1779
+ var _priceType = (_flatItem$bundleItem10 = flatItem.bundleItem) === null || _flatItem$bundleItem10 === void 0 ? void 0 : _flatItem$bundleItem10.price_type;
1780
+ var _priceTypeExt = (_flatItem$bundleItem11 = flatItem.bundleItem) === null || _flatItem$bundleItem11 === void 0 ? void 0 : _flatItem$bundleItem11.price_type_ext;
1482
1781
 
1483
1782
  // original_price 对应:
1484
1783
  // 1. price_type: "markup" && price_type_ext: "product_price"