@pisell/pisellos 3.0.75 → 3.0.77

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.
@@ -25,9 +25,10 @@ function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol"
25
25
  function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
26
26
  import { BaseModule } from "../BaseModule";
27
27
  import { RulesHooks } from "./types";
28
- import { uniqueById, getDiscountAmount, getDiscountListAmountTotal, getDiscountListAmount, filterDiscountListByBookingTime } from "../../solution/ShopDiscount/utils";
28
+ import { uniqueById, getDiscountAmount, getDiscountListAmountTotal, getDiscountListAmount, filterDiscountListByBookingTime, isOrderLevelFixedAmountDiscount, calculateOrderLevelDiscountAllocation } from "../../solution/ShopDiscount/utils";
29
29
  import { getProductOriginTotalPrice, getProductTotalPrice } from "../Cart/utils";
30
30
  import Decimal from 'decimal.js';
31
+ import { isBoolean } from 'lodash-es';
31
32
  import dayjs from 'dayjs';
32
33
  export var RulesModule = /*#__PURE__*/function (_BaseModule) {
33
34
  _inherits(RulesModule, _BaseModule);
@@ -142,10 +143,11 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
142
143
  }
143
144
 
144
145
  // 合并新旧折扣列表,并计算折扣结果,注意,如果旧折扣里有 isEditMode 为 true 的优惠券,则不合并
146
+ // 保留之前扫码得到的商品券(isScan: true)
145
147
  var filteredOldDiscountList = oldDiscountList.filter(function (discount) {
146
- return !discount.isEditMode && discount.tag !== 'good_pass';
148
+ return !discount.isEditMode && (discount.tag !== 'good_pass' || discount.isScan);
147
149
  });
148
- var mergedDiscountList = uniqueById(uniqueById([].concat(_toConsumableArray(filteredOldDiscountList), _toConsumableArray(newDiscountList))), 'product_id');
150
+ var mergedDiscountList = uniqueById([].concat(_toConsumableArray(filteredOldDiscountList), _toConsumableArray(newDiscountList)));
149
151
  var result = this.calcDiscount({
150
152
  discountList: mergedDiscountList,
151
153
  productList: _toConsumableArray(productList),
@@ -212,6 +214,25 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
212
214
  }
213
215
  });
214
216
 
217
+ // 🔥 检查 editModeDiscount 中是否有订单级别的折扣,如果有则禁用 addModeDiscount 中相同 product_id 的折扣卡
218
+ var editModeOrderLevelProductIds = new Set();
219
+ editModeDiscount.forEach(function (discount) {
220
+ var _discount$discount2;
221
+ if (((_discount$discount2 = discount.discount) === null || _discount$discount2 === void 0 ? void 0 : _discount$discount2.discount_calculation_mode) === 'order_level') {
222
+ var _discount$discount3;
223
+ editModeOrderLevelProductIds.add(discount === null || discount === void 0 || (_discount$discount3 = discount.discount) === null || _discount$discount3 === void 0 ? void 0 : _discount$discount3.discount_product_id);
224
+ }
225
+ });
226
+
227
+ // 如果存在订单级别的编辑模式折扣,禁用 addModeDiscount 中相同 product_id 的折扣卡
228
+ if (editModeOrderLevelProductIds.size > 0) {
229
+ addModeDiscount.forEach(function (discount) {
230
+ if (editModeOrderLevelProductIds.has(discount.product_id)) {
231
+ discount.isDisabled = true;
232
+ }
233
+ });
234
+ }
235
+
215
236
  // 过滤掉手动取消false的优惠券,但仅用于应用优惠逻辑
216
237
  var filteredDiscountList = addModeDiscount.filter(function (discount) {
217
238
  return !discount.isManualSelect;
@@ -267,26 +288,69 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
267
288
  };
268
289
 
269
290
  // 优惠力度排序,传进来的数据里可能有商品券,也可能有优惠券
270
- // 1. 商品券(n.tag=good_pass)视为最优惠(免费)
271
- // 2. 折扣券(n.tag=product_discount_card)按照新的优先级排序:
272
- // - 固定金额优先于百分比(固定金额 > 百分比)
273
- // - 固定金额内部:金额越大越优先
274
- // - 百分比内部:折扣越大越优先(par_value越小越优先)
275
- // 3. 相同类型和金额的情况下,按照过期时间(n.expire_time)排序
291
+ // 新的优先级排序规则(从高到低):
292
+ // 1. 带有Holder的商品券(metadata?.holder?.type === 'custom' && tag === 'good_pass')
293
+ // 2. 带有Holder的商品级折扣卡(metadata?.holder?.type === 'custom' && tag === 'product_discount_card' && discount_calculation_mode !== 'order_level')
294
+ // 3. 带有Holder的订单级折扣卡(metadata?.holder?.type === 'custom' && tag === 'product_discount_card' && discount_calculation_mode === 'order_level')
295
+ // 4. Customer商品券(metadata?.holder?.type !== 'custom' && tag === 'good_pass')
296
+ // 5. Customer商品级折扣卡(metadata?.holder?.type !== 'custom' && tag === 'product_discount_card' && discount_calculation_mode !== 'order_level')
297
+ // 6. Customer订单级固定金额折扣(metadata?.holder?.type !== 'custom' && tag === 'product_discount_card' && discount_calculation_mode === 'order_level')
298
+ // 同一优先级内部:固定金额优先于百分比,金额/折扣力度越大越优先,最后按过期时间排序
276
299
  var sortedDiscountList = _toConsumableArray(filteredDiscountList).sort(function (a, b) {
277
- // 1. 商品券优先级最高
278
- if (a.tag === 'good_pass' && b.tag !== 'good_pass') return -1;
279
- if (b.tag === 'good_pass' && a.tag !== 'good_pass') return 1;
300
+ // 辅助函数:判断是否是带有Holder的优惠券
301
+ var isHolderDiscount = function isHolderDiscount(discount) {
302
+ var _discount$metadata;
303
+ 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';
304
+ };
280
305
 
281
- // 2. 如果都是商品券或都不是商品券,按照类型和折扣力度排序
282
- if (a.tag === 'good_pass' && b.tag === 'good_pass') {
283
- // 都是商品券,按照过期时间排序
284
- return compareByExpireTime(a, b);
285
- } else if (a.tag === 'product_discount_card' && b.tag === 'product_discount_card') {
286
- var _a$metadata, _b$metadata;
287
- // 都是折扣券,按照新的优先级排序:固定金额优先于百分比
288
- var typeA = ((_a$metadata = a.metadata) === null || _a$metadata === void 0 ? void 0 : _a$metadata.discount_card_type) || 'percent'; // 默认为百分比
289
- var typeB = ((_b$metadata = b.metadata) === null || _b$metadata === void 0 ? void 0 : _b$metadata.discount_card_type) || 'percent'; // 默认为百分比
306
+ // 辅助函数:判断是否是商品券
307
+ var isGoodPass = function isGoodPass(discount) {
308
+ return discount.tag === 'good_pass';
309
+ };
310
+
311
+ // 辅助函数:判断是否是订单级折扣卡
312
+ var isOrderLevelDiscount = function isOrderLevelDiscount(discount) {
313
+ var _discount$metadata2;
314
+ return discount.tag === 'product_discount_card' && ((_discount$metadata2 = discount.metadata) === null || _discount$metadata2 === void 0 ? void 0 : _discount$metadata2.discount_calculation_mode) === 'order_level';
315
+ };
316
+
317
+ // 辅助函数:判断是否是商品级折扣卡
318
+ var isItemLevelDiscount = function isItemLevelDiscount(discount) {
319
+ var _discount$metadata3;
320
+ return discount.tag === 'product_discount_card' && ((_discount$metadata3 = discount.metadata) === null || _discount$metadata3 === void 0 ? void 0 : _discount$metadata3.discount_calculation_mode) !== 'order_level';
321
+ };
322
+
323
+ // 辅助函数:获取优先级(数字越小优先级越高)
324
+ var getPriority = function getPriority(discount) {
325
+ var isHolder = isHolderDiscount(discount);
326
+ if (isHolder) {
327
+ if (isGoodPass(discount)) return 1; // 带有Holder的商品券
328
+ if (isItemLevelDiscount(discount)) return 2; // 带有Holder的商品级折扣卡
329
+ if (isOrderLevelDiscount(discount)) return 3; // 带有Holder的订单级折扣卡
330
+ } else {
331
+ if (isGoodPass(discount)) return 4; // Customer商品券
332
+ if (isItemLevelDiscount(discount)) return 5; // Customer商品级折扣卡
333
+ if (isOrderLevelDiscount(discount)) return 6; // Customer订单级固定金额折扣
334
+ }
335
+ return 7; // 其他情况
336
+ };
337
+
338
+ // 辅助函数:按过期时间比较
339
+ function compareByExpireTime(itemA, itemB) {
340
+ if (itemA.expire_time && itemB.expire_time) {
341
+ var timeA = new Date(itemA.expire_time).getTime();
342
+ var timeB = new Date(itemB.expire_time).getTime();
343
+ return timeA - timeB;
344
+ }
345
+ // 有过期时间的优先级高于永久的
346
+ return itemA.expire_time ? -1 : itemB.expire_time ? 1 : 0;
347
+ }
348
+
349
+ // 辅助函数:同类型折扣卡内部排序(固定金额优先于百分比,金额/折扣力度排序)
350
+ function compareDiscountCardValue(itemA, itemB) {
351
+ var _itemA$metadata, _itemB$metadata;
352
+ var typeA = ((_itemA$metadata = itemA.metadata) === null || _itemA$metadata === void 0 ? void 0 : _itemA$metadata.discount_card_type) || 'percent';
353
+ var typeB = ((_itemB$metadata = itemB.metadata) === null || _itemB$metadata === void 0 ? void 0 : _itemB$metadata.discount_card_type) || 'percent';
290
354
 
291
355
  // 1. 固定金额优先于百分比
292
356
  if (typeA === 'fixed_amount' && typeB === 'percent') return -1;
@@ -294,37 +358,46 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
294
358
 
295
359
  // 2. 都是固定金额时,金额越大越优先
296
360
  if (typeA === 'fixed_amount' && typeB === 'fixed_amount') {
297
- if (a.par_value !== b.par_value) {
298
- var valueA = new Decimal(a.par_value || 0);
299
- var valueB = new Decimal(b.par_value || 0);
361
+ if (itemA.par_value !== itemB.par_value) {
362
+ var valueA = new Decimal(itemA.par_value || 0);
363
+ var valueB = new Decimal(itemB.par_value || 0);
300
364
  return valueB.minus(valueA).toNumber(); // 金额大的在前
301
365
  }
302
366
  }
303
367
 
304
368
  // 3. 都是百分比时,折扣越大越优先(par_value越大越优先)
305
369
  if (typeA === 'percent' && typeB === 'percent') {
306
- if (a.par_value !== b.par_value) {
307
- var _valueA = new Decimal(100).minus(a.par_value || 0);
308
- var _valueB = new Decimal(100).minus(b.par_value || 0);
370
+ if (itemA.par_value !== itemB.par_value) {
371
+ var _valueA = new Decimal(100).minus(itemA.par_value || 0);
372
+ var _valueB = new Decimal(100).minus(itemB.par_value || 0);
309
373
  return _valueA.minus(_valueB).toNumber(); // 折扣大的在前
310
374
  }
311
375
  }
376
+ return 0;
377
+ }
378
+
379
+ // 1. 先按优先级排序
380
+ var priorityA = getPriority(a);
381
+ var priorityB = getPriority(b);
382
+ if (priorityA !== priorityB) {
383
+ return priorityA - priorityB;
384
+ }
312
385
 
313
- // 相同类型和金额的情况下,按照过期时间排序
386
+ // 2. 同一优先级内部排序
387
+ // 如果是商品券(优先级1或4),按过期时间排序
388
+ if (isGoodPass(a) && isGoodPass(b)) {
389
+ return compareByExpireTime(a, b);
390
+ }
391
+
392
+ // 如果是折扣卡(优先级2、3、5、6),按折扣力度排序,然后按过期时间
393
+ if (a.tag === 'product_discount_card' && b.tag === 'product_discount_card') {
394
+ var valueCompare = compareDiscountCardValue(a, b);
395
+ if (valueCompare !== 0) return valueCompare;
314
396
  return compareByExpireTime(a, b);
315
397
  }
316
398
 
317
399
  // 3. 其他情况按照过期时间排序
318
400
  return compareByExpireTime(a, b);
319
- function compareByExpireTime(itemA, itemB) {
320
- if (itemA.expire_time && itemB.expire_time) {
321
- var timeA = new Date(itemA.expire_time).getTime();
322
- var timeB = new Date(itemB.expire_time).getTime();
323
- return timeA - timeB;
324
- }
325
- // 有过期时间的优先级高于永久的
326
- return itemA.expire_time ? -1 : itemB.expire_time ? 1 : 0;
327
- }
328
401
  });
329
402
 
330
403
  // const newProductList = [];
@@ -345,8 +418,10 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
345
418
 
346
419
  // 对扁平化后的列表按价格降序排序(用于应用优惠券时优先选择高价商品)
347
420
  var sortedFlattenedList = flattenedList.sort(function (a, b) {
348
- var priceA = new Decimal(a.price || '0');
349
- var priceB = new Decimal(b.price || '0');
421
+ var _ref3, _a$original_price, _ref4, _b$original_price;
422
+ // 子商品优先使用 original_price,主商品使用 price
423
+ 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');
424
+ 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');
350
425
  if (priceA.equals(priceB)) {
351
426
  // 价格相同时,主商品优先
352
427
  if (a.type !== b.type) {
@@ -400,6 +475,191 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
400
475
  discountApplicableProducts.set(discount.id, []);
401
476
  });
402
477
 
478
+ // 预处理 order_level 固定金额折扣卡的分摊信息
479
+ // Map<discountId, Map<productId, { discountAmount, difference }>>
480
+ var orderLevelDiscountAllocations = new Map();
481
+
482
+ // 第一步:收集所有被勾选的 order_level 折扣卡及其适用商品
483
+ // Map<discountId, Set<flatItemId>> - 记录每个折扣卡适用的商品
484
+ var orderLevelDiscountApplicableItems = new Map();
485
+ // Map<flatItemId, Set<discountId>> - 记录每个商品可以被哪些折扣卡使用
486
+ var itemApplicableDiscounts = new Map();
487
+
488
+ // 辅助函数:检查商品是否对某个折扣卡可用
489
+ var checkItemApplicableForDiscount = function checkItemApplicableForDiscount(flatItem, discount) {
490
+ var _product, _product2, _product3, _flatItem$bundleItem, _flatItem$bundleItem2, _product4;
491
+ var product;
492
+ if (flatItem.type === 'main') {
493
+ product = flatItem.product;
494
+ } else {
495
+ var _flatItem$parentProdu;
496
+ product = {
497
+ _id: flatItem._id,
498
+ id: flatItem.id,
499
+ price: flatItem.price,
500
+ quantity: flatItem.quantity,
501
+ num: flatItem.num,
502
+ booking_id: flatItem.booking_id,
503
+ discount_list: flatItem.discount_list || [],
504
+ startDate: (_flatItem$parentProdu = flatItem.parentProduct) === null || _flatItem$parentProdu === void 0 ? void 0 : _flatItem$parentProdu.startDate
505
+ };
506
+ }
507
+
508
+ // 编辑的商品使用了优惠券不可用
509
+ 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) {
510
+ return d.id && ['good_pass', 'discount_card', 'product_discount_card'].includes(d.tag || d.type);
511
+ })) : !(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) {
512
+ return d.id;
513
+ }));
514
+ if (!isAvailableProduct) {
515
+ return false;
516
+ }
517
+
518
+ // vouchersApplicable 为 false 的商品不参与订单级别折扣均摊
519
+ if (isBoolean(product.vouchersApplicable) && !product.vouchersApplicable) {
520
+ return false;
521
+ }
522
+
523
+ // 商品价格为0时不可用
524
+ if (Number(product.price) <= 0 || !product.price) {
525
+ return false;
526
+ }
527
+ var limitedData = discount.limited_relation_product_data;
528
+
529
+ // 时间限制检查
530
+ var timeLimit = !!filterDiscountListByBookingTime([discount], (((_product4 = product) === null || _product4 === void 0 ? void 0 : _product4.startDate) || dayjs()).format('YYYY-MM-DD HH:mm:ss')).length;
531
+ if (!timeLimit) {
532
+ return false;
533
+ }
534
+
535
+ // 判断商品是否适用
536
+ var isLimitedProduct = false;
537
+ if (limitedData.type === 'product_all') {
538
+ var _limitedData$exclude_;
539
+ // 全品类,但需要检查是否被排除
540
+ if (limitedData.filter === 1 && (_limitedData$exclude_ = limitedData.exclude_product_ids) !== null && _limitedData$exclude_ !== void 0 && _limitedData$exclude_.includes(product.id)) {
541
+ isLimitedProduct = false;
542
+ } else {
543
+ isLimitedProduct = true;
544
+ }
545
+ } else if (limitedData.product_ids && limitedData.product_ids.includes(product.id)) {
546
+ isLimitedProduct = true;
547
+ }
548
+
549
+ // 套餐规则检查
550
+ var isBundleAvailable = _this3.checkPackageSubItemUsageRules(discount, flatItem);
551
+ return isLimitedProduct && isBundleAvailable;
552
+ };
553
+
554
+ // 收集所有被勾选的 order_level 折扣卡及其适用商品
555
+ var selectedOrderLevelDiscounts = sortedDiscountList.filter(function (discount) {
556
+ return isOrderLevelFixedAmountDiscount(discount) && discount.isSelected !== false;
557
+ });
558
+
559
+ // 遍历所有被勾选的 order_level 折扣卡,建立双向映射
560
+ selectedOrderLevelDiscounts.forEach(function (discount) {
561
+ var applicableItemIds = new Set();
562
+ sortedFlattenedList.forEach(function (flatItem) {
563
+ if (checkItemApplicableForDiscount(flatItem, discount)) {
564
+ applicableItemIds.add(flatItem._id);
565
+
566
+ // 记录商品可以被哪些折扣卡使用
567
+ if (!itemApplicableDiscounts.has(flatItem._id)) {
568
+ itemApplicableDiscounts.set(flatItem._id, new Set());
569
+ }
570
+ itemApplicableDiscounts.get(flatItem._id).add(discount.id);
571
+ }
572
+ });
573
+ orderLevelDiscountApplicableItems.set(discount.id, applicableItemIds);
574
+ });
575
+
576
+ // 专属折扣卡定义:限定了特定商品(product_ids),而不是全品类(product_all)的折扣卡
577
+ // 如果专属折扣卡被勾选,它适用的商品会被占用,其他折扣卡均摊时应排除这些商品
578
+ // Map<flatItemId, discountId> - 记录被占用的商品及其对应的折扣卡
579
+ var occupiedItems = new Map();
580
+
581
+ // 按排序顺序遍历折扣卡,判断哪些是专属折扣卡
582
+ selectedOrderLevelDiscounts.forEach(function (discount) {
583
+ var limitedData = discount.limited_relation_product_data;
584
+
585
+ // 判断是否是专属折扣卡(只能用于特定商品,而不是全品类)
586
+ var isExclusiveDiscount = limitedData.type !== 'product_all';
587
+ if (isExclusiveDiscount) {
588
+ // 专属折扣卡:它适用的所有商品都会被它占用
589
+ var applicableItems = orderLevelDiscountApplicableItems.get(discount.id);
590
+ if (applicableItems) {
591
+ applicableItems.forEach(function (itemId) {
592
+ // 如果商品还没被占用,则标记为被当前折扣卡占用
593
+ if (!occupiedItems.has(itemId)) {
594
+ occupiedItems.set(itemId, discount.id);
595
+ }
596
+ });
597
+ }
598
+ }
599
+ });
600
+ console.log('occupiedItems', occupiedItems, 'orderLevelDiscountApplicableItems', orderLevelDiscountApplicableItems);
601
+
602
+ // 🔥 第三步:识别 order_level 折扣卡并预计算分摊(排除被其他专属折扣卡占用的商品)
603
+ sortedDiscountList.forEach(function (discount) {
604
+ if (!isOrderLevelFixedAmountDiscount(discount)) {
605
+ return;
606
+ }
607
+
608
+ // 收集该折扣卡适用的商品(排除被其他专属折扣卡占用的商品)
609
+ var applicableProducts = [];
610
+ sortedFlattenedList.forEach(function (flatItem) {
611
+ var _flatItem$parentProdu2, _product$price, _ref5, _flatItem$original_pr;
612
+ // 🔥 检查该商品是否被其他专属折扣卡占用
613
+ var occupyingDiscountId = occupiedItems.get(flatItem._id);
614
+ if (occupyingDiscountId !== undefined && occupyingDiscountId !== discount.id) {
615
+ // 该商品被其他专属折扣卡占用,跳过
616
+ return;
617
+ }
618
+ if (!checkItemApplicableForDiscount(flatItem, discount)) {
619
+ return;
620
+ }
621
+ var product;
622
+ if (flatItem.type === 'main') {
623
+ product = flatItem.product;
624
+ } else {
625
+ product = {
626
+ _id: flatItem._id,
627
+ id: flatItem.id,
628
+ price: flatItem.price,
629
+ quantity: flatItem.quantity,
630
+ num: flatItem.num
631
+ };
632
+ }
633
+
634
+ // 对于 bundle 子商品,quantity 需要乘以主商品的购买数量
635
+ 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);
636
+
637
+ // 对于主商品:使用 price
638
+ // 对于子商品:优先使用 flatItem.original_price,否则使用 flatItem.price
639
+ var originalAmount = flatItem.type === 'main' ? Number((_product$price = product.price) !== null && _product$price !== void 0 ? _product$price : 0) : Number((_ref5 = (_flatItem$original_pr = flatItem.original_price) !== null && _flatItem$original_pr !== void 0 ? _flatItem$original_pr : flatItem.price) !== null && _ref5 !== void 0 ? _ref5 : 0);
640
+
641
+ // 传递 parentQuantity 用于差值处理时判断是否是真正的"数量为1"
642
+ var productData = {
643
+ productId: flatItem._id,
644
+ amount: originalAmount,
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
+
403
663
  // 处理后的商品列表(按排序后的顺序处理,记录优惠信息)
404
664
  var processedProductsMap = new Map();
405
665
 
@@ -415,7 +675,7 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
415
675
  product = flatItem.product;
416
676
  originProduct = flatItem.originProduct;
417
677
  } else {
418
- var _flatItem$parentProdu;
678
+ var _flatItem$parentProdu4;
419
679
  // bundle子商品:构造虚拟商品对象
420
680
  product = {
421
681
  _id: flatItem._id,
@@ -427,12 +687,12 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
427
687
  origin_total: flatItem.origin_total,
428
688
  booking_id: flatItem.booking_id,
429
689
  discount_list: flatItem.discount_list || [],
430
- startDate: (_flatItem$parentProdu = flatItem.parentProduct) === null || _flatItem$parentProdu === void 0 ? void 0 : _flatItem$parentProdu.startDate
690
+ startDate: (_flatItem$parentProdu4 = flatItem.parentProduct) === null || _flatItem$parentProdu4 === void 0 ? void 0 : _flatItem$parentProdu4.startDate
431
691
  };
432
692
  originProduct = flatItem.originProduct;
433
693
  }
434
694
  addModeDiscount.forEach(function (discount) {
435
- var _product, _product2, _product3, _product4, _flatItem$bundleItem;
695
+ var _product5, _product6, _product7, _product8, _flatItem$bundleItem3, _flatItem$bundleItem4;
436
696
  var limitedData = discount === null || discount === void 0 ? void 0 : discount.limited_relation_product_data;
437
697
 
438
698
  // 拿到discount配置的holder信息 product信息 product.holder 加在 isLimitedProduct
@@ -442,21 +702,23 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
442
702
  holder_id: (_tempVar === null || _tempVar === void 0 ? void 0 : _tempVar.holder_id) || product.holder_id
443
703
  }, holders);
444
704
  var timeLimit = true;
445
- timeLimit = !!filterDiscountListByBookingTime([discount], (((_product = product) === null || _product === void 0 ? void 0 : _product.startDate) || dayjs()).format('YYYY-MM-DD HH:mm:ss')).length;
705
+ timeLimit = !!filterDiscountListByBookingTime([discount], (((_product5 = product) === null || _product5 === void 0 ? void 0 : _product5.startDate) || dayjs()).format('YYYY-MM-DD HH:mm:ss')).length;
446
706
  // 是符合折扣的商品
447
707
  var isLimitedProduct = (limitedData.type === 'product_all' || limitedData.product_ids && limitedData.product_ids.includes(product.id)) && isHolderMatch;
448
708
 
449
709
  // 编辑的商品 使用了优惠券不可用
450
- 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) {
710
+ 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) {
451
711
  return discount.id && ['good_pass', 'discount_card', 'product_discount_card'].includes(discount.tag || discount.type);
452
- })) : !(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);
712
+ })) : !(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) {
713
+ return discount.id;
714
+ }));
453
715
 
454
716
  // 套餐是否可用判断
455
717
  var isBundleAvailable = _this3.checkPackageSubItemUsageRules(discount, flatItem);
456
718
 
457
719
  // 判断优惠券是否适用于该商品
458
720
  if (isAvailableProduct && isLimitedProduct && isBundleAvailable && timeLimit) {
459
- var _discountApplicabilit, _discount$metadata;
721
+ var _discountApplicabilit, _discount$metadata4, _discount$metadata5;
460
722
  // 记录此优惠券适用的商品
461
723
  (_discountApplicabilit = discountApplicability.get(discount.id)) === null || _discountApplicabilit === void 0 || _discountApplicabilit.push(product.id);
462
724
 
@@ -472,8 +734,9 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
472
734
  type: discountType,
473
735
  tag: discountType,
474
736
  discount: {
475
- discount_card_type: discount === null || discount === void 0 || (_discount$metadata = discount.metadata) === null || _discount$metadata === void 0 ? void 0 : _discount$metadata.discount_card_type,
737
+ discount_card_type: discount === null || discount === void 0 || (_discount$metadata4 = discount.metadata) === null || _discount$metadata4 === void 0 ? void 0 : _discount$metadata4.discount_card_type,
476
738
  fixed_amount: product.price,
739
+ discount_calculation_mode: discount === null || discount === void 0 || (_discount$metadata5 = discount.metadata) === null || _discount$metadata5 === void 0 ? void 0 : _discount$metadata5.discount_calculation_mode,
477
740
  resource_id: discount.id,
478
741
  title: discount.format_title,
479
742
  original_amount: product.price || product.origin_total,
@@ -496,14 +759,14 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
496
759
  // 然后再处理应用哪些优惠券,此时只考虑filteredDiscountList中的优惠券
497
760
  // 🔥 使用扁平化后的列表进行处理
498
761
  sortedFlattenedList.forEach(function (flatItem, index) {
499
- var _product5, _product$discount_lis2, _product6;
762
+ var _product9, _product$discount_lis2, _product10;
500
763
  // 获取商品数据
501
764
  var product, originProduct;
502
765
  if (flatItem.type === 'main') {
503
766
  product = flatItem.product;
504
767
  originProduct = flatItem.originProduct;
505
768
  } else {
506
- var _flatItem$bundleItem2, _flatItem$bundleItem3, _flatItem$bundleItem4, _flatItem$parentProdu2;
769
+ var _flatItem$bundleItem5, _flatItem$bundleItem6, _flatItem$bundleItem7, _flatItem$parentProdu5;
507
770
  // bundle子商品
508
771
  product = {
509
772
  _id: flatItem._id,
@@ -512,17 +775,17 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
512
775
  quantity: flatItem.quantity,
513
776
  num: flatItem.num,
514
777
  total: flatItem.total,
515
- original_price: flatItem === null || flatItem === void 0 || (_flatItem$bundleItem2 = flatItem.bundleItem) === null || _flatItem$bundleItem2 === void 0 ? void 0 : _flatItem$bundleItem2.original_price,
516
- origin_total: flatItem === null || flatItem === void 0 || (_flatItem$bundleItem3 = flatItem.bundleItem) === null || _flatItem$bundleItem3 === void 0 ? void 0 : _flatItem$bundleItem3.original_price,
778
+ original_price: flatItem === null || flatItem === void 0 || (_flatItem$bundleItem5 = flatItem.bundleItem) === null || _flatItem$bundleItem5 === void 0 ? void 0 : _flatItem$bundleItem5.original_price,
779
+ origin_total: flatItem === null || flatItem === void 0 || (_flatItem$bundleItem6 = flatItem.bundleItem) === null || _flatItem$bundleItem6 === void 0 ? void 0 : _flatItem$bundleItem6.original_price,
517
780
  booking_id: flatItem.booking_id,
518
- discount_list: (flatItem === null || flatItem === void 0 || (_flatItem$bundleItem4 = flatItem.bundleItem) === null || _flatItem$bundleItem4 === void 0 ? void 0 : _flatItem$bundleItem4.discount_list) || [],
519
- startDate: (_flatItem$parentProdu2 = flatItem.parentProduct) === null || _flatItem$parentProdu2 === void 0 ? void 0 : _flatItem$parentProdu2.startDate
781
+ discount_list: (flatItem === null || flatItem === void 0 || (_flatItem$bundleItem7 = flatItem.bundleItem) === null || _flatItem$bundleItem7 === void 0 ? void 0 : _flatItem$bundleItem7.discount_list) || [],
782
+ startDate: (_flatItem$parentProdu5 = flatItem.parentProduct) === null || _flatItem$parentProdu5 === void 0 ? void 0 : _flatItem$parentProdu5.startDate
520
783
  };
521
784
  originProduct = flatItem.originProduct;
522
785
  }
523
786
 
524
787
  // 已有优惠的商品跳过
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) {
788
+ 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
789
  return discount.id && ['good_pass', 'discount_card', 'product_discount_card'].includes(discount.tag || discount.type);
527
790
  })) {
528
791
  if (flatItem.type === 'main') {
@@ -594,12 +857,12 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
594
857
  // 如果是手动折扣,则不适用优惠券
595
858
  var isManualDiscount = false;
596
859
  if (flatItem.type === 'main') {
597
- var _product$discount_lis5, _product7, _product7$every;
860
+ var _product$discount_lis5, _product11, _product11$every;
598
861
  // 主商品:判断自身是否手动折扣
599
862
  isManualDiscount = typeof product.isManualDiscount === 'boolean' ? product.isManualDiscount : product.total != product.origin_total && (product.bundle || []).every(function (item) {
600
- var _ref3;
601
- return !((_ref3 = item.discount_list || []) !== null && _ref3 !== void 0 && _ref3.length);
602
- }) && (!((_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) {
863
+ var _ref6;
864
+ return !((_ref6 = item.discount_list || []) !== null && _ref6 !== void 0 && _ref6.length);
865
+ }) && (!((_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) {
603
866
  return item.type === 'product';
604
867
  })));
605
868
  } else {
@@ -608,8 +871,8 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
608
871
  if (parentProduct) {
609
872
  var _parentProduct$discou, _parentProduct$discou2, _parentProduct$discou3;
610
873
  isManualDiscount = typeof parentProduct.isManualDiscount === 'boolean' ? parentProduct.isManualDiscount : parentProduct.total != parentProduct.origin_total && (parentProduct.bundle || []).every(function (item) {
611
- var _ref4;
612
- return !((_ref4 = item.discount_list || []) !== null && _ref4 !== void 0 && _ref4.length);
874
+ var _ref7;
875
+ return !((_ref7 = item.discount_list || []) !== null && _ref7 !== void 0 && _ref7.length);
613
876
  }) && (!((_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) {
614
877
  return item.type === 'product';
615
878
  })));
@@ -628,11 +891,11 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
628
891
  }
629
892
  // bundle子商品:检查自己的 discount_list 或父主商品的 discount_list
630
893
  if (flatItem.type === 'bundle') {
631
- var _product$discount_lis7, _flatItem$parentProdu3;
894
+ var _product$discount_lis7, _flatItem$parentProdu6;
632
895
  if ((_product$discount_lis7 = product.discount_list) !== null && _product$discount_lis7 !== void 0 && _product$discount_lis7.some(function (item) {
633
896
  var _item$discount2;
634
897
  return ((_item$discount2 = item.discount) === null || _item$discount2 === void 0 ? void 0 : _item$discount2.resource_id) === options.discountId;
635
- }) || (_flatItem$parentProdu3 = flatItem.parentProduct) !== null && _flatItem$parentProdu3 !== void 0 && (_flatItem$parentProdu3 = _flatItem$parentProdu3.discount_list) !== null && _flatItem$parentProdu3 !== void 0 && _flatItem$parentProdu3.some(function (item) {
898
+ }) || (_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) {
636
899
  var _item$discount3;
637
900
  return ((_item$discount3 = item.discount) === null || _item$discount3 === void 0 ? void 0 : _item$discount3.resource_id) === options.discountId;
638
901
  })) {
@@ -654,14 +917,14 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
654
917
  }
655
918
  // bundle子商品:检查自己的 discount_list 或父主商品的 discount_list
656
919
  if (flatItem.type === 'bundle') {
657
- var _product$discount_lis9, _flatItem$parentProdu4;
920
+ var _product$discount_lis9, _flatItem$parentProdu7;
658
921
  if ((_product$discount_lis9 = product.discount_list) !== null && _product$discount_lis9 !== void 0 && _product$discount_lis9.some(function (item) {
659
922
  var _options$selectedList2;
660
923
  return options === null || options === void 0 || (_options$selectedList2 = options.selectedList) === null || _options$selectedList2 === void 0 ? void 0 : _options$selectedList2.some(function (n) {
661
924
  var _item$discount5;
662
925
  return n.discountId === ((_item$discount5 = item.discount) === null || _item$discount5 === void 0 ? void 0 : _item$discount5.resource_id);
663
926
  });
664
- }) || (_flatItem$parentProdu4 = flatItem.parentProduct) !== null && _flatItem$parentProdu4 !== void 0 && (_flatItem$parentProdu4 = _flatItem$parentProdu4.discount_list) !== null && _flatItem$parentProdu4 !== void 0 && _flatItem$parentProdu4.some(function (item) {
927
+ }) || (_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) {
665
928
  var _options$selectedList3;
666
929
  return options === null || options === void 0 || (_options$selectedList3 = options.selectedList) === null || _options$selectedList3 === void 0 ? void 0 : _options$selectedList3.some(function (n) {
667
930
  var _item$discount6;
@@ -745,7 +1008,7 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
745
1008
  }));
746
1009
  }
747
1010
  for (var i = 0; i < splitCount; i++) {
748
- var _product$discount_lis10, _originProduct, _selectedDiscount$met;
1011
+ var _product$discount_lis10, _originProduct, _selectedDiscount$met, _selectedDiscount$met2;
749
1012
  // 如果用过折扣卡,也就不存在拆分的情况了,这里直接使用上面计算出来的折扣卡
750
1013
  var _selectedDiscount = selectedDiscountCard || applicableDiscounts[i];
751
1014
  // 标记优惠券为已使用
@@ -766,16 +1029,32 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
766
1029
  }
767
1030
 
768
1031
  // 计算使用折扣卡/商品券以后,单个商品的总 total
769
- var targetProductTotal = getDiscountAmount(_selectedDiscount, product.price, product.price);
1032
+ // 🔥 检查是否是 order_level 固定金额折扣卡
1033
+ var isOrderLevel = isOrderLevelFixedAmountDiscount(_selectedDiscount);
1034
+ var orderLevelAllocation = isOrderLevel ? orderLevelDiscountAllocations.get(_selectedDiscount.id) : null;
1035
+ var productAllocation = orderLevelAllocation === null || orderLevelAllocation === void 0 ? void 0 : orderLevelAllocation.get(flatItem._id);
1036
+ var targetProductTotal = void 0;
1037
+ var amount = void 0;
1038
+ var productDiscountDifference = void 0;
1039
+ if (isOrderLevel && productAllocation) {
1040
+ // order_level:使用预计算的分摊金额
1041
+ amount = productAllocation.discountAmount;
1042
+ productDiscountDifference = productAllocation.difference;
1043
+ targetProductTotal = Math.max(new Decimal(product.price).minus(amount).toNumber(), 0);
1044
+ } else {
1045
+ // item_level 或其他类型:使用原有逻辑
1046
+ targetProductTotal = getDiscountAmount(_selectedDiscount, product.price, product.price);
1047
+ amount = new Decimal(product.price).minus(new Decimal(targetProductTotal)).toNumber();
1048
+ }
770
1049
  var discountType = _selectedDiscount.tag || _selectedDiscount.type;
771
1050
  var isGoodPass = discountType === 'good_pass';
772
- var amount = new Decimal(product.price).minus(new Decimal(targetProductTotal)).toNumber();
773
1051
  var discountDetail = {
774
1052
  amount: amount,
775
1053
  type: _selectedDiscount.tag === 'product_discount_card' ? 'discount_card' : discountType,
776
1054
  discount: {
777
1055
  discount_card_type: _selectedDiscount === null || _selectedDiscount === void 0 || (_selectedDiscount$met = _selectedDiscount.metadata) === null || _selectedDiscount$met === void 0 ? void 0 : _selectedDiscount$met.discount_card_type,
778
1056
  fixed_amount: amount,
1057
+ discount_calculation_mode: _selectedDiscount === null || _selectedDiscount === void 0 || (_selectedDiscount$met2 = _selectedDiscount.metadata) === null || _selectedDiscount$met2 === void 0 ? void 0 : _selectedDiscount$met2.discount_calculation_mode,
779
1058
  resource_id: _selectedDiscount.id,
780
1059
  title: _selectedDiscount.format_title,
781
1060
  original_amount: product.price,
@@ -784,9 +1063,11 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
784
1063
  },
785
1064
  // 前端使用的num数量,为了计算优惠金额
786
1065
  _num: isGoodPass ? 1 : product.num,
787
- metadata: {
1066
+ metadata: _objectSpread({
788
1067
  num: 1
789
- }
1068
+ }, productDiscountDifference !== undefined && {
1069
+ product_discount_difference: productDiscountDifference
1070
+ })
790
1071
  };
791
1072
  appliedProducts.push(discountDetail);
792
1073
  appliedDiscountProducts.set(_selectedDiscount.id, appliedProducts);
@@ -841,6 +1122,7 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
841
1122
 
842
1123
  // 生成有折扣的商品(每张商品券对应 num: 1)
843
1124
  for (var _i = 0; _i < discountNum; _i++) {
1125
+ var _selectedDiscount2$me;
844
1126
  var _selectedDiscount2 = applicableDiscounts[_i];
845
1127
  usedDiscounts.set(_selectedDiscount2.id, true);
846
1128
 
@@ -851,6 +1133,7 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
851
1133
  type: 'good_pass',
852
1134
  discount: {
853
1135
  fixed_amount: product.origin_total,
1136
+ discount_calculation_mode: _selectedDiscount2 === null || _selectedDiscount2 === void 0 || (_selectedDiscount2$me = _selectedDiscount2.metadata) === null || _selectedDiscount2$me === void 0 ? void 0 : _selectedDiscount2$me.discount_calculation_mode,
854
1137
  resource_id: _selectedDiscount2.id,
855
1138
  title: _selectedDiscount2.format_title,
856
1139
  original_amount: product.origin_total,
@@ -894,33 +1177,53 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
894
1177
  }));
895
1178
  }
896
1179
  } else {
897
- var _selectedDiscount3$me, _flatItem$parentProdu5;
1180
+ var _selectedDiscount3$me, _selectedDiscount3$me2, _flatItem$parentProdu8;
898
1181
  // 折扣卡:不拆分数量,直接应用
899
1182
  var _selectedDiscount3 = selectedDiscountCard || applicableDiscounts[0];
900
1183
  usedDiscounts.set(_selectedDiscount3.id, true);
901
1184
  var _productOriginTotal = product.original_price || product.price || 0;
902
- var _targetProductTotal = getDiscountAmount(_selectedDiscount3, _productOriginTotal, _productOriginTotal);
903
1185
 
1186
+ // 🔥 检查是否是 order_level 固定金额折扣卡
1187
+ var _isOrderLevel = isOrderLevelFixedAmountDiscount(_selectedDiscount3);
1188
+ var _orderLevelAllocation = _isOrderLevel ? orderLevelDiscountAllocations.get(_selectedDiscount3.id) : null;
1189
+ var _productAllocation = _orderLevelAllocation === null || _orderLevelAllocation === void 0 ? void 0 : _orderLevelAllocation.get(flatItem._id);
1190
+ var _targetProductTotal;
1191
+ var fixedAmountPerItem;
1192
+ var _productDiscountDifference;
1193
+ if (_isOrderLevel && _productAllocation) {
1194
+ // order_level:使用预计算的单价折扣金额(已经是单价,无需再除以数量)
1195
+ fixedAmountPerItem = _productAllocation.discountAmount;
1196
+ _productDiscountDifference = _productAllocation.difference;
1197
+ _targetProductTotal = Math.max(new Decimal(_productOriginTotal).minus(fixedAmountPerItem).toNumber(), 0);
1198
+ } else {
1199
+ // item_level 或其他类型:使用原有逻辑
1200
+ _targetProductTotal = getDiscountAmount(_selectedDiscount3, _productOriginTotal, _productOriginTotal);
1201
+ fixedAmountPerItem = new Decimal(_productOriginTotal).minus(_targetProductTotal).toNumber();
1202
+ }
1203
+ debugger;
904
1204
  // 🔥 使用当前的 _id 作为唯一标识
905
1205
  var _uniqueId = flatItem._id;
906
1206
  var _discountDetail2 = {
907
- amount: new Decimal(_productOriginTotal).minus(_targetProductTotal).toNumber() * (product.num || 1),
1207
+ amount: fixedAmountPerItem * (product.num || 1),
908
1208
  type: _selectedDiscount3.tag === 'product_discount_card' ? 'discount_card' : _selectedDiscount3.tag,
909
1209
  discount: {
910
1210
  discount_card_type: _selectedDiscount3 === null || _selectedDiscount3 === void 0 || (_selectedDiscount3$me = _selectedDiscount3.metadata) === null || _selectedDiscount3$me === void 0 ? void 0 : _selectedDiscount3$me.discount_card_type,
911
- fixed_amount: new Decimal(_productOriginTotal).minus(_targetProductTotal).toNumber(),
1211
+ fixed_amount: fixedAmountPerItem,
1212
+ discount_calculation_mode: _selectedDiscount3 === null || _selectedDiscount3 === void 0 || (_selectedDiscount3$me2 = _selectedDiscount3.metadata) === null || _selectedDiscount3$me2 === void 0 ? void 0 : _selectedDiscount3$me2.discount_calculation_mode,
912
1213
  resource_id: _selectedDiscount3.id,
913
1214
  title: _selectedDiscount3.format_title,
914
1215
  original_amount: product.original_price,
915
1216
  product_id: product.id,
916
1217
  percent: _selectedDiscount3.par_value
917
1218
  },
918
- metadata: {
1219
+ metadata: _objectSpread({
919
1220
  // 🔥 使用唯一的 _id
920
1221
  custom_product_bundle_map_id: _uniqueId,
921
1222
  num: product.num || 1
922
- },
923
- _num: (product.num || 1) * ((flatItem === null || flatItem === void 0 || (_flatItem$parentProdu5 = flatItem.parentProduct) === null || _flatItem$parentProdu5 === void 0 ? void 0 : _flatItem$parentProdu5.num) || 1)
1223
+ }, _productDiscountDifference !== undefined && {
1224
+ product_discount_difference: _productDiscountDifference
1225
+ }),
1226
+ _num: (product.num || 1) * ((flatItem === null || flatItem === void 0 || (_flatItem$parentProdu8 = flatItem.parentProduct) === null || _flatItem$parentProdu8 === void 0 ? void 0 : _flatItem$parentProdu8.num) || 1)
924
1227
  };
925
1228
 
926
1229
  // 记录实际应用的折扣
@@ -929,7 +1232,7 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
929
1232
  appliedDiscountProducts.set(_selectedDiscount3.id, _appliedProducts2);
930
1233
  processedItems.push(_objectSpread(_objectSpread({}, flatItem), {}, {
931
1234
  total: _targetProductTotal,
932
- price: new Decimal(_productOriginTotal || 0).minus(_discountDetail2.discount.fixed_amount).toNumber(),
1235
+ price: new Decimal(_productOriginTotal || 0).minus(fixedAmountPerItem).toNumber(),
933
1236
  discount_list: [_discountDetail2],
934
1237
  processed: true
935
1238
  }));
@@ -1039,10 +1342,10 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
1039
1342
  var updatedDiscountList = (item.discount_list || []).map(function (discount) {
1040
1343
  var _item$metadata;
1041
1344
  return _objectSpread(_objectSpread({}, discount), {}, {
1042
- metadata: {
1345
+ metadata: _objectSpread(_objectSpread({}, discount.metadata), {}, {
1043
1346
  num: item.num,
1044
1347
  custom_product_bundle_map_id: (_item$metadata = item.metadata) === null || _item$metadata === void 0 ? void 0 : _item$metadata.custom_product_bundle_map_id
1045
- }
1348
+ })
1046
1349
  // num: item.num, // 使用拆分后的 num
1047
1350
  });
1048
1351
  });
@@ -1062,10 +1365,10 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
1062
1365
  var _updatedDiscountList = (item.discount_list || []).map(function (discount) {
1063
1366
  var _item$metadata2;
1064
1367
  return _objectSpread(_objectSpread({}, discount), {}, {
1065
- metadata: {
1368
+ metadata: _objectSpread(_objectSpread({}, discount.metadata), {}, {
1066
1369
  num: item.num,
1067
1370
  custom_product_bundle_map_id: (_item$metadata2 = item.metadata) === null || _item$metadata2 === void 0 ? void 0 : _item$metadata2.custom_product_bundle_map_id
1068
- }
1371
+ })
1069
1372
  // num: item.num, // 使用当前的 num
1070
1373
  });
1071
1374
  });
@@ -1088,18 +1391,18 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
1088
1391
 
1089
1392
  // 🔥 更新主商品自己的 discount_list 中的 num(quantity=1)
1090
1393
  var updatedMainDiscountList = mainProductData.discount_list.map(function (discount) {
1091
- var _discount$metadata2, _discount$metadata3;
1092
- if (discount !== null && discount !== void 0 && (_discount$metadata2 = discount.metadata) !== null && _discount$metadata2 !== void 0 && _discount$metadata2.custom_product_bundle_map_id) {
1394
+ var _discount$metadata6, _discount$metadata7;
1395
+ if (discount !== null && discount !== void 0 && (_discount$metadata6 = discount.metadata) !== null && _discount$metadata6 !== void 0 && _discount$metadata6.custom_product_bundle_map_id) {
1093
1396
  // bundle的discount_list保持不变
1094
1397
  return discount;
1095
1398
  }
1096
1399
  // 主商品自己的discount,更新num为1
1097
1400
  return _objectSpread(_objectSpread({}, discount), {}, {
1098
1401
  // num: 1,
1099
- metadata: {
1100
- 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,
1402
+ metadata: _objectSpread(_objectSpread({}, discount.metadata), {}, {
1403
+ 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,
1101
1404
  num: 1
1102
- }
1405
+ })
1103
1406
  });
1104
1407
  });
1105
1408
 
@@ -1158,17 +1461,17 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
1158
1461
 
1159
1462
  // 🔥 更新主商品自己的 discount_list 中的 num(quantity=原quantity-1)
1160
1463
  var updatedMainDiscountListOriginal = mainProductData.discount_list.map(function (discount) {
1161
- var _discount$metadata4, _discount$metadata5;
1162
- if (discount !== null && discount !== void 0 && (_discount$metadata4 = discount.metadata) !== null && _discount$metadata4 !== void 0 && _discount$metadata4.custom_product_bundle_map_id) {
1464
+ var _discount$metadata8, _discount$metadata9;
1465
+ if (discount !== null && discount !== void 0 && (_discount$metadata8 = discount.metadata) !== null && _discount$metadata8 !== void 0 && _discount$metadata8.custom_product_bundle_map_id) {
1163
1466
  // bundle的discount_list保持不变
1164
1467
  return discount;
1165
1468
  }
1166
1469
  // 主商品自己的discount,更新num为 mainProductQuantity - 1
1167
1470
  return _objectSpread(_objectSpread({}, discount), {}, {
1168
- metadata: {
1471
+ metadata: _objectSpread(_objectSpread({}, discount.metadata), {}, {
1169
1472
  num: mainProductQuantity - 1,
1170
- 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
1171
- }
1473
+ 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
1474
+ })
1172
1475
  // num: mainProductQuantity - 1,
1173
1476
  });
1174
1477
  });
@@ -1230,12 +1533,12 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
1230
1533
  processedBundleItems.forEach(function (item) {
1231
1534
  // 🔥 更新 discount_list 中的 num,使其与拆分后的 item.num 一致
1232
1535
  var updatedDiscountList = (item.discount_list || []).map(function (discount) {
1233
- var _discount$metadata6;
1536
+ var _discount$metadata10;
1234
1537
  return _objectSpread(_objectSpread({}, discount), {}, {
1235
- metadata: {
1538
+ metadata: _objectSpread(_objectSpread({}, discount.metadata), {}, {
1236
1539
  num: item.num,
1237
- 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
1238
- }
1540
+ 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
1541
+ })
1239
1542
  // num: item.num, // 使用拆分后的 num
1240
1543
  });
1241
1544
  });
@@ -1418,10 +1721,10 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
1418
1721
  return true; // 单独购买时可用
1419
1722
  }
1420
1723
  if (isBundleItem) {
1421
- var _flatItem$bundleItem5, _flatItem$bundleItem6;
1724
+ var _flatItem$bundleItem8, _flatItem$bundleItem9;
1422
1725
  // 套餐中购买时,判断是否为原价
1423
- var priceType = (_flatItem$bundleItem5 = flatItem.bundleItem) === null || _flatItem$bundleItem5 === void 0 ? void 0 : _flatItem$bundleItem5.price_type;
1424
- var priceTypeExt = (_flatItem$bundleItem6 = flatItem.bundleItem) === null || _flatItem$bundleItem6 === void 0 ? void 0 : _flatItem$bundleItem6.price_type_ext;
1726
+ var priceType = (_flatItem$bundleItem8 = flatItem.bundleItem) === null || _flatItem$bundleItem8 === void 0 ? void 0 : _flatItem$bundleItem8.price_type;
1727
+ var priceTypeExt = (_flatItem$bundleItem9 = flatItem.bundleItem) === null || _flatItem$bundleItem9 === void 0 ? void 0 : _flatItem$bundleItem9.price_type_ext;
1425
1728
  // original_price 对应:
1426
1729
  // 1. price_type: "markup" && price_type_ext: "product_price"
1427
1730
  // markup_price 对应:
@@ -1459,10 +1762,10 @@ export var RulesModule = /*#__PURE__*/function (_BaseModule) {
1459
1762
  return false; // 单独购买时不可用
1460
1763
  }
1461
1764
  if (isBundleItem) {
1462
- var _flatItem$bundleItem7, _flatItem$bundleItem8;
1765
+ var _flatItem$bundleItem10, _flatItem$bundleItem11;
1463
1766
  // 套餐中购买时,判断是否为原价
1464
- var _priceType = (_flatItem$bundleItem7 = flatItem.bundleItem) === null || _flatItem$bundleItem7 === void 0 ? void 0 : _flatItem$bundleItem7.price_type;
1465
- var _priceTypeExt = (_flatItem$bundleItem8 = flatItem.bundleItem) === null || _flatItem$bundleItem8 === void 0 ? void 0 : _flatItem$bundleItem8.price_type_ext;
1767
+ var _priceType = (_flatItem$bundleItem10 = flatItem.bundleItem) === null || _flatItem$bundleItem10 === void 0 ? void 0 : _flatItem$bundleItem10.price_type;
1768
+ var _priceTypeExt = (_flatItem$bundleItem11 = flatItem.bundleItem) === null || _flatItem$bundleItem11 === void 0 ? void 0 : _flatItem$bundleItem11.price_type_ext;
1466
1769
  // 原价包括:price_type: "markup" && price_type_ext: "product_price"
1467
1770
  return _priceType === 'markup' && _priceTypeExt === 'product_price';
1468
1771
  }