@pisell/pisellos 2.1.46 → 2.1.48

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.
@@ -131,6 +131,49 @@ var RulesModule = class extends import_BaseModule.BaseModule {
131
131
  const filteredDiscountList = addModeDiscount.filter((discount) => {
132
132
  return !discount.isManualSelect;
133
133
  });
134
+ const flattenProductsWithBundle = (productList2) => {
135
+ const flattened = [];
136
+ productList2.forEach((originProduct) => {
137
+ const product = this.hooks.getProduct(originProduct);
138
+ flattened.push({
139
+ type: "main",
140
+ originProduct,
141
+ product,
142
+ price: Number(product.price || 0),
143
+ id: product.id,
144
+ _id: product._id,
145
+ parentId: product._id,
146
+ quantity: product.quantity,
147
+ num: product.num
148
+ });
149
+ if (product.bundle && Array.isArray(product.bundle) && product.bundle.length > 0) {
150
+ product.bundle.forEach((bundleItem, bundleIndex) => {
151
+ flattened.push({
152
+ type: "bundle",
153
+ originProduct,
154
+ parentProduct: product,
155
+ bundleItem,
156
+ bundleIndex,
157
+ // 虚拟商品属性
158
+ price: Number(bundleItem.price || 0),
159
+ id: bundleItem._bundle_product_id,
160
+ // 🔥 使用 _bundle_product_id
161
+ _id: `${product._id}_bundle_${bundleIndex}`,
162
+ parentId: product._id,
163
+ num: bundleItem.num || 1,
164
+ quantity: bundleItem.num || 1,
165
+ total: new import_decimal.default(bundleItem.price || 0).mul(bundleItem.num || 1).toNumber(),
166
+ origin_total: new import_decimal.default(bundleItem.price || 0).mul(bundleItem.num || 1).toNumber(),
167
+ original_price: bundleItem.original_price,
168
+ // 继承主商品属性
169
+ booking_id: product.booking_id,
170
+ discount_list: bundleItem.discount_list || []
171
+ });
172
+ });
173
+ }
174
+ });
175
+ return flattened;
176
+ };
134
177
  const sortedDiscountList = [...filteredDiscountList].sort((a, b) => {
135
178
  var _a, _b;
136
179
  if (a.tag === "good_pass" && b.tag !== "good_pass")
@@ -157,7 +200,7 @@ var RulesModule = class extends import_BaseModule.BaseModule {
157
200
  if (a.par_value !== b.par_value) {
158
201
  const valueA = new import_decimal.default(100).minus(a.par_value || 0);
159
202
  const valueB = new import_decimal.default(100).minus(b.par_value || 0);
160
- return valueB.minus(valueA).toNumber();
203
+ return valueA.minus(valueB).toNumber();
161
204
  }
162
205
  }
163
206
  return compareByExpireTime(a, b);
@@ -172,19 +215,26 @@ var RulesModule = class extends import_BaseModule.BaseModule {
172
215
  return itemA.expire_time ? -1 : itemB.expire_time ? 1 : 0;
173
216
  }
174
217
  });
175
- const sortedProductList = [...productList].sort((a, b) => {
218
+ const flattenedList = flattenProductsWithBundle(productList);
219
+ const sortedFlattenedList = flattenedList.sort((a, b) => {
176
220
  var _a, _b;
177
- const aProduct = this.hooks.getProduct(a);
178
- const bProduct = this.hooks.getProduct(b);
179
- const priceA = new import_decimal.default(aProduct.price || "0");
180
- const priceB = new import_decimal.default(bProduct.price || "0");
181
- if (priceA.toNumber() === priceB.toNumber()) {
182
- if (aProduct.quantity === bProduct.quantity) {
183
- return ((_a = bProduct.discount_list) == null ? void 0 : _a.length) - ((_b = aProduct.discount_list) == null ? void 0 : _b.length);
221
+ const priceA = new import_decimal.default(a.price || "0");
222
+ const priceB = new import_decimal.default(b.price || "0");
223
+ if (priceA.equals(priceB)) {
224
+ if (a.type !== b.type) {
225
+ return a.type === "main" ? -1 : 1;
226
+ }
227
+ if (a.type === "main" && b.type === "main") {
228
+ if (a.product.quantity === b.product.quantity) {
229
+ return (((_a = b.product.discount_list) == null ? void 0 : _a.length) || 0) - (((_b = a.product.discount_list) == null ? void 0 : _b.length) || 0);
230
+ }
231
+ return a.product.quantity - b.product.quantity;
232
+ }
233
+ if (a.type === "bundle" && b.type === "bundle") {
234
+ return (a.num || 1) - (b.num || 1);
184
235
  }
185
- return aProduct.quantity - bProduct.quantity;
186
236
  }
187
- return priceB.toNumber() - priceA.toNumber();
237
+ return priceB.minus(priceA).toNumber();
188
238
  });
189
239
  const usedDiscounts = /* @__PURE__ */ new Map();
190
240
  const discountApplicability = /* @__PURE__ */ new Map();
@@ -195,54 +245,102 @@ var RulesModule = class extends import_BaseModule.BaseModule {
195
245
  });
196
246
  const processedProductsMap = /* @__PURE__ */ new Map();
197
247
  const appliedDiscountProducts = /* @__PURE__ */ new Map();
198
- sortedProductList.forEach((originProduct) => {
199
- const product = this.hooks.getProduct(originProduct);
248
+ sortedFlattenedList.forEach((flatItem) => {
249
+ let product, originProduct;
250
+ if (flatItem.type === "main") {
251
+ product = flatItem.product;
252
+ originProduct = flatItem.originProduct;
253
+ } else {
254
+ product = {
255
+ _id: flatItem._id,
256
+ id: flatItem.id,
257
+ price: flatItem.price,
258
+ quantity: flatItem.quantity,
259
+ num: flatItem.num,
260
+ total: flatItem.total,
261
+ origin_total: flatItem.origin_total,
262
+ booking_id: flatItem.booking_id,
263
+ discount_list: flatItem.discount_list || []
264
+ };
265
+ originProduct = flatItem.originProduct;
266
+ }
200
267
  addModeDiscount.forEach((discount) => {
201
- var _a, _b, _c, _d;
268
+ var _a, _b, _c, _d, _e, _f;
202
269
  const limitedData = discount == null ? void 0 : discount.limited_relation_product_data;
203
270
  let timeLimit = true;
204
271
  timeLimit = !!(0, import_utils.filterDiscountListByBookingTime)([discount], ((product == null ? void 0 : product.startDate) || (0, import_dayjs.default)()).format("YYYY-MM-DD HH:mm:ss")).length;
205
272
  const isLimitedProduct = limitedData.type === "product_all" || limitedData.product_ids && limitedData.product_ids.includes(product.id);
206
- const isAvailableProduct = !((product == null ? void 0 : product.booking_id) && ((_a = product == null ? void 0 : product.discount_list) == null ? void 0 : _a.length) && ((_b = product == null ? void 0 : product.discount_list) == null ? void 0 : _b.every((discount2) => discount2.id && ["good_pass", "discount_card", "product_discount_card"].includes(discount2.tag || discount2.type))));
207
- if (isAvailableProduct && isLimitedProduct && timeLimit) {
208
- (_c = discountApplicability.get(discount.id)) == null ? void 0 : _c.push(product.id);
273
+ const isAvailableProduct = flatItem.type === "main" ? !((product == null ? void 0 : product.booking_id) && ((_a = product == null ? void 0 : product.discount_list) == null ? void 0 : _a.length) && ((_b = product == null ? void 0 : product.discount_list) == null ? void 0 : _b.every((discount2) => discount2.id && ["good_pass", "discount_card", "product_discount_card"].includes(discount2.tag || discount2.type)))) : !((flatItem == null ? void 0 : flatItem.booking_id) && ((_d = (_c = flatItem == null ? void 0 : flatItem.bundleItem) == null ? void 0 : _c.metadata) == null ? void 0 : _d.custom_product_bundle_map_id));
274
+ const isBundleAvailable = this.checkPackageSubItemUsageRules(discount, flatItem);
275
+ if (isAvailableProduct && isLimitedProduct && timeLimit && isBundleAvailable) {
276
+ (_e = discountApplicability.get(discount.id)) == null ? void 0 : _e.push(product.id);
209
277
  const applicableProducts = discountApplicableProducts.get(discount.id) || [];
210
278
  const discountType = discount.tag || discount.type;
279
+ const isGoodPass = discountType === "good_pass";
280
+ const num = isGoodPass || (flatItem == null ? void 0 : flatItem.type) === "main" ? 1 : product.num;
211
281
  const productData = {
212
- amount: product.price,
282
+ amount: product.price * num,
213
283
  type: discountType,
214
284
  tag: discountType,
215
285
  discount: {
216
- discount_card_type: (_d = discount == null ? void 0 : discount.metadata) == null ? void 0 : _d.discount_card_type,
286
+ discount_card_type: (_f = discount == null ? void 0 : discount.metadata) == null ? void 0 : _f.discount_card_type,
217
287
  fixed_amount: product.price,
218
288
  resource_id: discount.id,
219
289
  title: discount.format_title,
220
- original_amount: product.origin_total,
290
+ original_amount: product.price || product.origin_total,
221
291
  pre_value: discount.par_value,
222
292
  product_id: originProduct.id
293
+ },
294
+ metadata: {
295
+ num
223
296
  }
224
297
  };
225
- if (discountType !== "good_pass") {
226
- productData.num = product.num || 1;
227
- }
228
298
  applicableProducts.push(productData);
229
299
  discountApplicableProducts.set(discount.id, applicableProducts);
230
300
  }
231
301
  });
232
302
  });
233
- console.log(sortedProductList, "sortedProductListsortedProductList");
234
- sortedProductList.forEach((originProduct, index) => {
235
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
236
- const product = this.hooks.getProduct(originProduct);
237
- if ((product == null ? void 0 : product.booking_id) && ((_a = product.discount_list) == null ? void 0 : _a.length) && ((_b = product == null ? void 0 : product.discount_list) == null ? void 0 : _b.every((discount) => discount.id && ["good_pass", "discount_card", "product_discount_card"].includes(discount.tag || discount.type)))) {
238
- processedProductsMap.set(product._id, [originProduct]);
303
+ const processedFlatItemsMap = /* @__PURE__ */ new Map();
304
+ sortedFlattenedList.forEach((flatItem, index) => {
305
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x;
306
+ let product, originProduct;
307
+ if (flatItem.type === "main") {
308
+ product = flatItem.product;
309
+ originProduct = flatItem.originProduct;
310
+ } else {
311
+ product = {
312
+ _id: flatItem._id,
313
+ id: flatItem.id,
314
+ price: flatItem.price,
315
+ quantity: flatItem.quantity,
316
+ num: flatItem.num,
317
+ total: flatItem.total,
318
+ original_price: (_a = flatItem == null ? void 0 : flatItem.bundleItem) == null ? void 0 : _a.original_price,
319
+ origin_total: (_b = flatItem == null ? void 0 : flatItem.bundleItem) == null ? void 0 : _b.original_price,
320
+ booking_id: flatItem.booking_id,
321
+ discount_list: ((_c = flatItem == null ? void 0 : flatItem.bundleItem) == null ? void 0 : _c.discount_list) || []
322
+ };
323
+ originProduct = flatItem.originProduct;
324
+ }
325
+ if ((product == null ? void 0 : product.booking_id) && ((_d = product.discount_list) == null ? void 0 : _d.length) && ((_e = product == null ? void 0 : product.discount_list) == null ? void 0 : _e.every((discount) => discount.id && ["good_pass", "discount_card", "product_discount_card"].includes(discount.tag || discount.type)))) {
326
+ if (flatItem.type === "main") {
327
+ processedProductsMap.set(product._id, [originProduct]);
328
+ } else {
329
+ processedFlatItemsMap.set(flatItem._id, [{
330
+ ...flatItem,
331
+ processed: true
332
+ }]);
333
+ }
239
334
  return;
240
335
  }
241
336
  const applicableDiscounts = sortedDiscountList.filter((discount) => {
242
- var _a2;
243
- if ((Number(product.price) <= 0 || !product.price) && (discount.tag || discount.type) === "good_pass")
337
+ var _a2, _b2;
338
+ if ((Number(product.price) <= 0 || !product.price) && !((_a2 = product.discount_list) == null ? void 0 : _a2.find((n) => {
339
+ var _a3;
340
+ return ((_a3 = n.discount) == null ? void 0 : _a3.resource_id) === discount.id;
341
+ })) && (discount.tag || discount.type) === "good_pass")
244
342
  return false;
245
- if ((Number(product.total) <= 0 || !product.total) && !((_a2 = product.discount_list) == null ? void 0 : _a2.find((n) => {
343
+ if ((Number(product.price) <= 0 || !product.price) && !((_b2 = product.discount_list) == null ? void 0 : _b2.find((n) => {
246
344
  var _a3;
247
345
  return ((_a3 = n.discount) == null ? void 0 : _a3.resource_id) === discount.id;
248
346
  })) && (discount.tag || discount.type) !== "good_pass")
@@ -257,69 +355,131 @@ var RulesModule = class extends import_BaseModule.BaseModule {
257
355
  return false;
258
356
  }
259
357
  if (limitedData.type === "product_all") {
358
+ if (!this.checkPackageSubItemUsageRules(discount, flatItem)) {
359
+ return false;
360
+ }
260
361
  return true;
261
362
  } else if (limitedData.product_ids && limitedData.product_ids.includes(product.id)) {
363
+ if (!this.checkPackageSubItemUsageRules(discount, flatItem)) {
364
+ return false;
365
+ }
262
366
  return true;
263
367
  }
264
368
  return false;
265
369
  });
266
370
  const selectedDiscountCard = applicableDiscounts.find((n) => n.isScan && n.isSelected && (n.tag || n.type) !== "good_pass");
267
371
  const selectedDiscount = selectedDiscountCard || applicableDiscounts[0];
268
- let isManualDiscount = typeof product.isManualDiscount === "boolean" ? product.isManualDiscount : product.total != product.origin_total && (!((_c = product.discount_list) == null ? void 0 : _c.length) || ((_e = (_d = product == null ? void 0 : product.discount_list) == null ? void 0 : _d.every) == null ? void 0 : _e.call(_d, (item) => item.type === "product")));
269
- if ((options == null ? void 0 : options.discountId) && ((_f = product.discount_list) == null ? void 0 : _f.some((item) => {
270
- var _a2;
271
- return ((_a2 = item.discount) == null ? void 0 : _a2.resource_id) === options.discountId;
272
- }))) {
273
- isManualDiscount = false;
372
+ let isManualDiscount = false;
373
+ if (flatItem.type === "main") {
374
+ isManualDiscount = typeof product.isManualDiscount === "boolean" ? product.isManualDiscount : product.total != product.origin_total && (product.bundle || []).every((item) => {
375
+ var _a2;
376
+ return !((_a2 = item.discount_list || []) == null ? void 0 : _a2.length);
377
+ }) && (!((_f = product.discount_list) == null ? void 0 : _f.length) || ((_h = (_g = product == null ? void 0 : product.discount_list) == null ? void 0 : _g.every) == null ? void 0 : _h.call(_g, (item) => item.type === "product")));
378
+ } else {
379
+ const parentProduct = flatItem.parentProduct;
380
+ if (parentProduct) {
381
+ isManualDiscount = typeof parentProduct.isManualDiscount === "boolean" ? parentProduct.isManualDiscount : parentProduct.total != parentProduct.origin_total && (parentProduct.bundle || []).every((item) => {
382
+ var _a2;
383
+ return !((_a2 = item.discount_list || []) == null ? void 0 : _a2.length);
384
+ }) && (!((_i = parentProduct.discount_list) == null ? void 0 : _i.length) || ((_k = (_j = parentProduct == null ? void 0 : parentProduct.discount_list) == null ? void 0 : _j.every) == null ? void 0 : _k.call(_j, (item) => item.type === "product")));
385
+ }
274
386
  }
275
- if ((options == null ? void 0 : options.selectedList) && ((_g = product.discount_list) == null ? void 0 : _g.some((item) => {
276
- var _a2;
277
- return (_a2 = options == null ? void 0 : options.selectedList) == null ? void 0 : _a2.some((n) => {
278
- var _a3;
279
- return n.discountId === ((_a3 = item.discount) == null ? void 0 : _a3.resource_id);
280
- });
281
- }))) {
282
- isManualDiscount = false;
387
+ if (options == null ? void 0 : options.discountId) {
388
+ if (flatItem.type === "main" && ((_l = product.discount_list) == null ? void 0 : _l.some((item) => {
389
+ var _a2;
390
+ return ((_a2 = item.discount) == null ? void 0 : _a2.resource_id) === options.discountId;
391
+ }))) {
392
+ isManualDiscount = false;
393
+ }
394
+ if (flatItem.type === "bundle") {
395
+ if (((_m = product.discount_list) == null ? void 0 : _m.some((item) => {
396
+ var _a2;
397
+ return ((_a2 = item.discount) == null ? void 0 : _a2.resource_id) === options.discountId;
398
+ })) || ((_o = (_n = flatItem.parentProduct) == null ? void 0 : _n.discount_list) == null ? void 0 : _o.some((item) => {
399
+ var _a2;
400
+ return ((_a2 = item.discount) == null ? void 0 : _a2.resource_id) === options.discountId;
401
+ }))) {
402
+ isManualDiscount = false;
403
+ }
404
+ }
405
+ }
406
+ if (options == null ? void 0 : options.selectedList) {
407
+ if (flatItem.type === "main" && ((_p = product.discount_list) == null ? void 0 : _p.some((item) => {
408
+ var _a2;
409
+ return (_a2 = options == null ? void 0 : options.selectedList) == null ? void 0 : _a2.some((n) => {
410
+ var _a3;
411
+ return n.discountId === ((_a3 = item.discount) == null ? void 0 : _a3.resource_id);
412
+ });
413
+ }))) {
414
+ isManualDiscount = false;
415
+ }
416
+ if (flatItem.type === "bundle") {
417
+ if (((_q = product.discount_list) == null ? void 0 : _q.some((item) => {
418
+ var _a2;
419
+ return (_a2 = options == null ? void 0 : options.selectedList) == null ? void 0 : _a2.some((n) => {
420
+ var _a3;
421
+ return n.discountId === ((_a3 = item.discount) == null ? void 0 : _a3.resource_id);
422
+ });
423
+ })) || ((_s = (_r = flatItem.parentProduct) == null ? void 0 : _r.discount_list) == null ? void 0 : _s.some((item) => {
424
+ var _a2;
425
+ return (_a2 = options == null ? void 0 : options.selectedList) == null ? void 0 : _a2.some((n) => {
426
+ var _a3;
427
+ return n.discountId === ((_a3 = item.discount) == null ? void 0 : _a3.resource_id);
428
+ });
429
+ }))) {
430
+ isManualDiscount = false;
431
+ }
432
+ }
283
433
  }
284
434
  if (applicableDiscounts.length === 0 || isManualDiscount || (0, import_lodash_es.isBoolean)(product.vouchersApplicable) && !product.vouchersApplicable) {
285
- if (product.isClient) {
286
- processedProductsMap.set(
287
- product._id,
288
- [this.hooks.setProduct(originProduct, {
289
- ...isManualDiscount ? {} : {
290
- origin_total: (0, import_utils2.getProductOriginTotalPrice)({
291
- product: {
292
- original_price: product.original_price
293
- },
294
- bundle: product.bundle,
295
- options: product.options
296
- }),
297
- variant: originProduct._productInit.variant,
298
- original_price: originProduct._productInit.original_price,
299
- total: (0, import_utils2.getProductTotalPrice)({
300
- product: {
301
- price: product.price
302
- },
303
- bundle: product.bundle,
304
- options: product.options
305
- }),
306
- price: product.price
307
- },
308
- discount_list: []
309
- })]
310
- );
435
+ if (flatItem.type === "main") {
436
+ if (product.isClient) {
437
+ processedProductsMap.set(
438
+ product._id,
439
+ [this.hooks.setProduct(originProduct, {
440
+ ...isManualDiscount ? {} : {
441
+ origin_total: (0, import_utils2.getProductOriginTotalPrice)({
442
+ product: {
443
+ original_price: product.original_price
444
+ },
445
+ bundle: product.bundle,
446
+ options: product.options
447
+ }),
448
+ variant: originProduct._productInit.variant,
449
+ original_price: originProduct._productInit.original_price,
450
+ total: (0, import_utils2.getProductTotalPrice)({
451
+ product: {
452
+ price: product.price
453
+ },
454
+ bundle: product.bundle,
455
+ options: product.options
456
+ }),
457
+ price: product.price
458
+ },
459
+ discount_list: []
460
+ })]
461
+ );
462
+ } else {
463
+ processedProductsMap.set(
464
+ product._id,
465
+ [this.hooks.setProduct(originProduct, {
466
+ ...isManualDiscount ? {} : {
467
+ _id: product._id.split("___")[0] + "___" + index,
468
+ total: product.origin_total || product.total,
469
+ price: product.price,
470
+ main_product_selling_price: product.price
471
+ },
472
+ discount_list: []
473
+ })]
474
+ );
475
+ }
311
476
  } else {
312
- processedProductsMap.set(
313
- product._id,
314
- [this.hooks.setProduct(originProduct, {
315
- ...isManualDiscount ? {} : {
316
- _id: product._id.split("___")[0] + "___" + index,
317
- total: product.origin_total || product.total,
318
- price: product.price
319
- },
320
- discount_list: []
321
- })]
322
- );
477
+ processedFlatItemsMap.set(flatItem._id, [{
478
+ ...flatItem,
479
+ discount_list: [],
480
+ price: flatItem.bundleItem.original_price,
481
+ processed: true
482
+ }]);
323
483
  }
324
484
  return;
325
485
  }
@@ -327,113 +487,513 @@ var RulesModule = class extends import_BaseModule.BaseModule {
327
487
  return;
328
488
  }
329
489
  const isNeedSplit = (selectedDiscount.tag || selectedDiscount.type) === "good_pass";
330
- const splitCount = isNeedSplit ? Math.min(product.quantity || product.num || 1, applicableDiscounts.filter((item) => (item.tag || item.type) === "good_pass").length) : 1;
490
+ const totalQuantity = product.quantity || product.num || 1;
491
+ const availableGoodPassCount = applicableDiscounts.filter((item) => (item.tag || item.type) === "good_pass").length;
492
+ const splitCount = isNeedSplit ? Math.min(totalQuantity, availableGoodPassCount) : 1;
331
493
  const arr = [];
332
- if (splitCount < product.quantity && isNeedSplit) {
333
- arr.push(this.hooks.setProduct(originProduct, {
334
- discount_list: [],
335
- quantity: product.quantity - splitCount,
336
- _id: product._id.split("___")[0]
337
- }));
338
- }
339
- for (let i = 0; i < splitCount; i++) {
340
- const selectedDiscount2 = selectedDiscountCard || applicableDiscounts[i];
341
- usedDiscounts.set(selectedDiscount2.id, true);
342
- const appliedProducts = appliedDiscountProducts.get(selectedDiscount2.id) || [];
343
- let productOriginTotal = product.origin_total || product.total || 0;
344
- if (((_h = product.discount_list) == null ? void 0 : _h.length) && product.origin_total) {
345
- productOriginTotal = product.origin_total;
346
- }
347
- if (Number(((_i = originProduct == null ? void 0 : originProduct._productInit) == null ? void 0 : _i.original_price) || 0) > 0 && product.origin_total && product.total && product.origin_total !== product.total) {
348
- productOriginTotal = product.total;
494
+ if (flatItem.type === "main") {
495
+ if (splitCount < totalQuantity && isNeedSplit) {
496
+ arr.push(this.hooks.setProduct(originProduct, {
497
+ discount_list: [],
498
+ quantity: totalQuantity - splitCount,
499
+ _id: product._id.split("___")[0]
500
+ }));
349
501
  }
350
- const targetProductTotal = (0, import_utils.getDiscountAmount)(selectedDiscount2, productOriginTotal, product.price);
351
- const discountType = selectedDiscount2.tag === "product_discount_card" ? "discount_card" : selectedDiscount2.tag;
352
- const discountDetail = {
353
- amount: new import_decimal.default(productOriginTotal).minus(new import_decimal.default(targetProductTotal)).toNumber(),
354
- type: discountType,
355
- discount: {
356
- discount_card_type: (_j = selectedDiscount2 == null ? void 0 : selectedDiscount2.metadata) == null ? void 0 : _j.discount_card_type,
357
- fixed_amount: new import_decimal.default(productOriginTotal).minus(new import_decimal.default(targetProductTotal)).toNumber(),
358
- resource_id: selectedDiscount2.id,
359
- title: selectedDiscount2.format_title,
360
- original_amount: productOriginTotal,
361
- product_id: originProduct.id,
362
- percent: selectedDiscount2.par_value
502
+ for (let i = 0; i < splitCount; i++) {
503
+ const selectedDiscount2 = selectedDiscountCard || applicableDiscounts[i];
504
+ usedDiscounts.set(selectedDiscount2.id, true);
505
+ const appliedProducts = appliedDiscountProducts.get(selectedDiscount2.id) || [];
506
+ let productOriginTotal = product.origin_total || product.total || 0;
507
+ if (((_t = product.discount_list) == null ? void 0 : _t.length) && product.origin_total) {
508
+ productOriginTotal = product.origin_total;
509
+ }
510
+ if (Number(((_u = originProduct == null ? void 0 : originProduct._productInit) == null ? void 0 : _u.original_price) || 0) > 0 && product.origin_total && product.total && product.origin_total !== product.total) {
511
+ productOriginTotal = product.total;
512
+ }
513
+ const targetProductTotal = (0, import_utils.getDiscountAmount)(selectedDiscount2, product.price, product.price);
514
+ const discountType = selectedDiscount2.tag || selectedDiscount2.type;
515
+ const isGoodPass = discountType === "good_pass";
516
+ const amount = new import_decimal.default(product.price).minus(new import_decimal.default(targetProductTotal)).toNumber();
517
+ const discountDetail = {
518
+ amount,
519
+ type: selectedDiscount2.tag === "product_discount_card" ? "discount_card" : discountType,
520
+ discount: {
521
+ discount_card_type: (_v = selectedDiscount2 == null ? void 0 : selectedDiscount2.metadata) == null ? void 0 : _v.discount_card_type,
522
+ fixed_amount: amount,
523
+ resource_id: selectedDiscount2.id,
524
+ title: selectedDiscount2.format_title,
525
+ original_amount: product.price,
526
+ product_id: originProduct.id,
527
+ percent: selectedDiscount2.par_value
528
+ },
529
+ // 前端使用的num数量,为了计算优惠金额
530
+ _num: isGoodPass ? 1 : product.num,
531
+ metadata: {
532
+ num: 1
533
+ }
534
+ };
535
+ appliedProducts.push(discountDetail);
536
+ appliedDiscountProducts.set(selectedDiscount2.id, appliedProducts);
537
+ let total = targetProductTotal;
538
+ if (product.options) {
539
+ total = product.options.reduce((accumulator, currentValue) => {
540
+ const currentPrice = new import_decimal.default(currentValue.price || 0);
541
+ const currentNum = new import_decimal.default(currentValue.num || 0);
542
+ return accumulator.add(currentPrice.mul(currentNum));
543
+ }, new import_decimal.default(total)).toNumber();
544
+ }
545
+ if (product.isClient) {
546
+ debugger;
547
+ arr.push(this.hooks.setProduct(originProduct, {
548
+ discount_list: [discountDetail],
549
+ price: selectedDiscount2.tag === "good_pass" ? 0 : product.price,
550
+ quantity: isNeedSplit ? 1 : product.quantity,
551
+ origin_total: (0, import_utils2.getProductOriginTotalPrice)({
552
+ product: {
553
+ original_price: product.original_price
554
+ },
555
+ bundle: product.bundle,
556
+ options: product.options
557
+ }),
558
+ variant: originProduct._productInit.variant,
559
+ original_price: new import_decimal.default(product.price || 0).toNumber(),
560
+ total
561
+ }));
562
+ } else {
563
+ arr.push(this.hooks.setProduct(originProduct, {
564
+ discount_list: [discountDetail],
565
+ _id: product._id.split("___")[0] + "___" + selectedDiscount2.id + index,
566
+ price: selectedDiscount2.tag === "good_pass" ? 0 : product.price,
567
+ quantity: isNeedSplit ? 1 : product.quantity,
568
+ total,
569
+ origin_total: productOriginTotal,
570
+ main_product_selling_price: targetProductTotal
571
+ }));
363
572
  }
364
- };
365
- if ((selectedDiscount2.tag || selectedDiscount2.type) !== "good_pass") {
366
- discountDetail.num = product.num || 1;
367
573
  }
368
- appliedProducts.push(discountDetail);
369
- appliedDiscountProducts.set(selectedDiscount2.id, appliedProducts);
370
- if (product.isClient) {
371
- arr.push(this.hooks.setProduct(originProduct, {
372
- discount_list: [discountDetail],
373
- price: selectedDiscount2.tag === "good_pass" ? 0 : product.price,
374
- quantity: isNeedSplit ? 1 : product.quantity,
375
- origin_total: (0, import_utils2.getProductOriginTotalPrice)({
376
- product: {
377
- original_price: product.original_price
574
+ processedProductsMap.set(product._id, arr);
575
+ } else {
576
+ const processedItems = [];
577
+ if (isNeedSplit) {
578
+ const discountNum = splitCount;
579
+ const normalNum = totalQuantity - discountNum;
580
+ for (let i = 0; i < discountNum; i++) {
581
+ const selectedDiscount2 = applicableDiscounts[i];
582
+ usedDiscounts.set(selectedDiscount2.id, true);
583
+ const uniqueId = `${flatItem._id}_split_${i}`;
584
+ const discountDetail = {
585
+ amount: product.origin_total,
586
+ type: "good_pass",
587
+ discount: {
588
+ fixed_amount: product.origin_total,
589
+ resource_id: selectedDiscount2.id,
590
+ title: selectedDiscount2.format_title,
591
+ original_amount: product.origin_total,
592
+ product_id: product.id
378
593
  },
379
- bundle: product.bundle,
380
- options: product.options
381
- }),
382
- variant: originProduct._productInit.variant,
383
- original_price: new import_decimal.default(product.price || 0).toNumber(),
384
- total: targetProductTotal
385
- }));
594
+ metadata: {
595
+ // 🔥 使用拆分后的唯一 _id
596
+ custom_product_bundle_map_id: uniqueId,
597
+ num: 1
598
+ },
599
+ _num: 1
600
+ };
601
+ const appliedProducts = appliedDiscountProducts.get(selectedDiscount2.id) || [];
602
+ appliedProducts.push(discountDetail);
603
+ appliedDiscountProducts.set(selectedDiscount2.id, appliedProducts);
604
+ processedItems.push({
605
+ ...flatItem,
606
+ // 🔥 使用唯一的 _id
607
+ _id: uniqueId,
608
+ num: 1,
609
+ quantity: 1,
610
+ price: 0,
611
+ // 商品券价格为0
612
+ total: 0,
613
+ discount_list: [discountDetail],
614
+ processed: true,
615
+ _discountId: selectedDiscount2.id
616
+ });
617
+ }
618
+ if (normalNum > 0) {
619
+ processedItems.push({
620
+ ...flatItem,
621
+ // 🔥 为剩余商品生成唯一的 _id
622
+ _id: `${flatItem._id}_split_rest`,
623
+ num: normalNum,
624
+ quantity: normalNum,
625
+ discount_list: [],
626
+ processed: true
627
+ });
628
+ }
386
629
  } else {
387
- arr.push(this.hooks.setProduct(originProduct, {
388
- discount_list: [discountDetail],
389
- _id: product._id.split("___")[0] + "___" + selectedDiscount2.id + "___" + index,
390
- price: selectedDiscount2.tag === "good_pass" ? 0 : product.price,
391
- quantity: isNeedSplit ? 1 : product.quantity,
630
+ const selectedDiscount2 = selectedDiscountCard || applicableDiscounts[0];
631
+ usedDiscounts.set(selectedDiscount2.id, true);
632
+ const productOriginTotal = product.original_price || product.price || 0;
633
+ const targetProductTotal = (0, import_utils.getDiscountAmount)(
634
+ selectedDiscount2,
635
+ productOriginTotal,
636
+ productOriginTotal
637
+ );
638
+ const uniqueId = flatItem._id;
639
+ const discountDetail = {
640
+ amount: new import_decimal.default(productOriginTotal).minus(targetProductTotal).toNumber() * (product.num || 1),
641
+ type: selectedDiscount2.tag === "product_discount_card" ? "discount_card" : selectedDiscount2.tag,
642
+ discount: {
643
+ discount_card_type: (_w = selectedDiscount2 == null ? void 0 : selectedDiscount2.metadata) == null ? void 0 : _w.discount_card_type,
644
+ fixed_amount: new import_decimal.default(productOriginTotal).minus(targetProductTotal).toNumber(),
645
+ resource_id: selectedDiscount2.id,
646
+ title: selectedDiscount2.format_title,
647
+ original_amount: product.original_price,
648
+ product_id: product.id,
649
+ percent: selectedDiscount2.par_value
650
+ },
651
+ metadata: {
652
+ // 🔥 使用唯一的 _id
653
+ custom_product_bundle_map_id: uniqueId,
654
+ num: product.num || 1
655
+ },
656
+ _num: (product.num || 1) * (((_x = flatItem == null ? void 0 : flatItem.parentProduct) == null ? void 0 : _x.num) || 1)
657
+ };
658
+ const appliedProducts = appliedDiscountProducts.get(selectedDiscount2.id) || [];
659
+ appliedProducts.push(discountDetail);
660
+ appliedDiscountProducts.set(selectedDiscount2.id, appliedProducts);
661
+ processedItems.push({
662
+ ...flatItem,
392
663
  total: targetProductTotal,
393
- origin_total: productOriginTotal
394
- }));
664
+ price: new import_decimal.default(productOriginTotal || 0).minus(discountDetail.discount.fixed_amount).toNumber(),
665
+ discount_list: [discountDetail],
666
+ processed: true
667
+ });
395
668
  }
669
+ processedFlatItemsMap.set(flatItem._id, processedItems);
396
670
  }
397
- console.log(arr, "arrarrarr");
398
- processedProductsMap.set(product._id, arr);
399
671
  });
400
- const processedProductList = [];
401
- productList.forEach((originProduct) => {
402
- const product = this.hooks.getProduct(originProduct);
403
- const getDefaultProduct = () => {
404
- if (product.isClient) {
405
- return this.hooks.setProduct(originProduct, {
406
- discount_list: [],
407
- price: product.price,
408
- origin_total: (0, import_utils2.getProductOriginTotalPrice)({
409
- product: {
410
- original_price: product.original_price
411
- },
412
- bundle: product.bundle,
413
- options: product.options
414
- }),
415
- variant: originProduct._productInit.variant,
416
- original_price: originProduct._productInit.original_price,
417
- total: (0, import_utils2.getProductTotalPrice)({
418
- product: {
672
+ const reconstructProductsWithBundle = (processedProductsMap2, processedFlatItemsMap2, originalProductList) => {
673
+ const result = [];
674
+ originalProductList.forEach((originProduct) => {
675
+ const product = this.hooks.getProduct(originProduct);
676
+ const mainProductArr = processedProductsMap2.get(product._id);
677
+ if (!mainProductArr || mainProductArr.length === 0) {
678
+ const getDefaultProduct = () => {
679
+ if (product.isClient) {
680
+ return this.hooks.setProduct(originProduct, {
681
+ discount_list: [],
682
+ price: product.price,
683
+ origin_total: (0, import_utils2.getProductOriginTotalPrice)({
684
+ product: {
685
+ original_price: product.original_price
686
+ },
687
+ bundle: product.bundle,
688
+ options: product.options
689
+ }),
690
+ variant: originProduct._productInit.variant,
691
+ original_price: originProduct._productInit.original_price,
692
+ total: (0, import_utils2.getProductTotalPrice)({
693
+ product: {
694
+ price: product.price
695
+ },
696
+ bundle: product.bundle,
697
+ options: product.options
698
+ })
699
+ });
700
+ } else {
701
+ return this.hooks.setProduct(originProduct, {
702
+ discount_list: [],
703
+ total: product.total,
704
+ origin_total: product.origin_total,
419
705
  price: product.price
420
- },
421
- bundle: product.bundle,
422
- options: product.options
423
- })
424
- });
706
+ });
707
+ }
708
+ };
709
+ result.push(getDefaultProduct());
710
+ return;
711
+ }
712
+ const hasBundle = product.bundle && Array.isArray(product.bundle) && product.bundle.length > 0;
713
+ debugger;
714
+ if (!hasBundle) {
715
+ result.push(...mainProductArr);
425
716
  } else {
426
- return this.hooks.setProduct(originProduct, {
427
- discount_list: [],
428
- total: product.total,
429
- origin_total: product.origin_total,
430
- price: product.price
431
- });
717
+ const bundleProcessingInfo = /* @__PURE__ */ new Map();
718
+ let hasGoodPassApplied = false;
719
+ if (product.bundle && Array.isArray(product.bundle)) {
720
+ product.bundle.forEach((bundleItem, bundleIndex) => {
721
+ const bundleItemId = `${product._id}_bundle_${bundleIndex}`;
722
+ const processedBundleItems = processedFlatItemsMap2.get(bundleItemId);
723
+ if (!processedBundleItems || processedBundleItems.length === 0) {
724
+ bundleProcessingInfo.set(bundleIndex, [bundleItem]);
725
+ } else {
726
+ bundleProcessingInfo.set(bundleIndex, processedBundleItems);
727
+ const hasGoodPass = processedBundleItems.some(
728
+ (item) => {
729
+ var _a;
730
+ return (_a = item.discount_list) == null ? void 0 : _a.some(
731
+ (discount) => discount.type === "good_pass" || discount.tag === "good_pass"
732
+ );
733
+ }
734
+ );
735
+ if (hasGoodPass) {
736
+ hasGoodPassApplied = true;
737
+ }
738
+ }
739
+ });
740
+ }
741
+ const mainProductQuantity = mainProductArr[0] ? this.hooks.getProduct(mainProductArr[0]).quantity : 1;
742
+ if (hasGoodPassApplied && mainProductArr.length === 1 && mainProductQuantity > 1) {
743
+ const mainProduct = mainProductArr[0];
744
+ const mainProductData = this.hooks.getProduct(mainProduct);
745
+ const newBundleWithDiscount = [];
746
+ (product.bundle || []).forEach((bundleItem, bundleIndex) => {
747
+ const processedItems = bundleProcessingInfo.get(bundleIndex) || [bundleItem];
748
+ if (processedItems.length > 1) {
749
+ processedItems.forEach((item) => {
750
+ const updatedDiscountList2 = (item.discount_list || []).map((discount) => {
751
+ var _a;
752
+ return {
753
+ ...discount,
754
+ metadata: {
755
+ num: item.num,
756
+ custom_product_bundle_map_id: (_a = item.metadata) == null ? void 0 : _a.custom_product_bundle_map_id
757
+ }
758
+ // num: item.num, // 使用拆分后的 num
759
+ };
760
+ });
761
+ newBundleWithDiscount.push({
762
+ ...bundleItem,
763
+ _id: item._id,
764
+ product_id: bundleItem.product_id,
765
+ price: item.price,
766
+ num: item.num,
767
+ discount_list: updatedDiscountList2
768
+ });
769
+ });
770
+ } else {
771
+ const item = processedItems[0];
772
+ if (item.processed) {
773
+ const updatedDiscountList2 = (item.discount_list || []).map((discount) => {
774
+ var _a;
775
+ return {
776
+ ...discount,
777
+ metadata: {
778
+ num: item.num,
779
+ custom_product_bundle_map_id: (_a = item.metadata) == null ? void 0 : _a.custom_product_bundle_map_id
780
+ }
781
+ // num: item.num, // 使用当前的 num
782
+ };
783
+ });
784
+ newBundleWithDiscount.push({
785
+ ...bundleItem,
786
+ _id: item._id,
787
+ product_id: bundleItem.product_id,
788
+ price: item.price,
789
+ num: item.num,
790
+ discount_list: updatedDiscountList2
791
+ });
792
+ } else {
793
+ newBundleWithDiscount.push(item);
794
+ }
795
+ }
796
+ });
797
+ let newTotalWithDiscount = Number(mainProductData.price || 0);
798
+ let newOriginTotalWithDiscount = Number(mainProductData.original_price || mainProductData.price || 0);
799
+ const updatedMainDiscountList = mainProductData.discount_list.map((discount) => {
800
+ var _a, _b;
801
+ if ((_a = discount == null ? void 0 : discount.metadata) == null ? void 0 : _a.custom_product_bundle_map_id) {
802
+ return discount;
803
+ }
804
+ return {
805
+ ...discount,
806
+ // num: 1,
807
+ metadata: {
808
+ custom_product_bundle_map_id: (_b = discount == null ? void 0 : discount.metadata) == null ? void 0 : _b.custom_product_bundle_map_id,
809
+ num: 1
810
+ }
811
+ };
812
+ });
813
+ const mainDiscountList = updatedMainDiscountList.filter((item) => {
814
+ var _a;
815
+ return !((_a = item == null ? void 0 : item.metadata) == null ? void 0 : _a.custom_product_bundle_map_id);
816
+ });
817
+ if (mainDiscountList && mainDiscountList.length > 0) {
818
+ const allDiscountAmount = (0, import_utils.getDiscountListAmountTotal)(mainDiscountList);
819
+ newTotalWithDiscount = new import_decimal.default(mainProductData.price || 0).minus(allDiscountAmount).toNumber() ?? newTotalWithDiscount;
820
+ newOriginTotalWithDiscount = mainProductData.origin_total ?? newOriginTotalWithDiscount;
821
+ }
822
+ if (newBundleWithDiscount.length > 0) {
823
+ newBundleWithDiscount.forEach((item) => {
824
+ newTotalWithDiscount += Number(item.price) * Number(item.num);
825
+ });
826
+ newBundleWithDiscount.forEach((item) => {
827
+ var _a, _b, _c;
828
+ const originalPrice = ((_c = (_b = (_a = item.discount_list) == null ? void 0 : _a[0]) == null ? void 0 : _b.discount) == null ? void 0 : _c.original_amount) || item.price;
829
+ newOriginTotalWithDiscount += Number(originalPrice) * Number(item.num);
830
+ });
831
+ }
832
+ if (product == null ? void 0 : product.options) {
833
+ newTotalWithDiscount = product.options.reduce((accumulator, currentValue) => {
834
+ const currentPrice = new import_decimal.default(currentValue.price || 0);
835
+ const currentNum = new import_decimal.default(currentValue.num || 0);
836
+ return accumulator.add(currentPrice.mul(currentNum));
837
+ }, new import_decimal.default(newTotalWithDiscount)).toNumber();
838
+ }
839
+ result.push(
840
+ this.hooks.setProduct(mainProduct, {
841
+ ...mainProductData,
842
+ _id: `${mainProductData._id.split("___")[0]}___split_discount`,
843
+ quantity: 1,
844
+ discount_list: updatedMainDiscountList,
845
+ bundle: newBundleWithDiscount,
846
+ total: newTotalWithDiscount,
847
+ origin_total: newOriginTotalWithDiscount
848
+ })
849
+ );
850
+ if (mainProductQuantity > 1) {
851
+ const newBundleOriginal = [];
852
+ (product.bundle || []).forEach((bundleItem, bundleIndex) => {
853
+ newBundleOriginal.push({
854
+ ...bundleItem,
855
+ discount_list: []
856
+ });
857
+ });
858
+ let newTotalOriginal = Number(mainProductData.price || 0);
859
+ let newOriginTotalOriginal = Number(mainProductData.original_price || mainProductData.price || 0);
860
+ const updatedMainDiscountListOriginal = mainProductData.discount_list.map((discount) => {
861
+ var _a, _b;
862
+ if ((_a = discount == null ? void 0 : discount.metadata) == null ? void 0 : _a.custom_product_bundle_map_id) {
863
+ return discount;
864
+ }
865
+ return {
866
+ ...discount,
867
+ metadata: {
868
+ num: mainProductQuantity - 1,
869
+ custom_product_bundle_map_id: (_b = discount == null ? void 0 : discount.metadata) == null ? void 0 : _b.custom_product_bundle_map_id
870
+ }
871
+ // num: mainProductQuantity - 1,
872
+ };
873
+ });
874
+ const mainDiscountListOriginal = updatedMainDiscountListOriginal.filter((item) => {
875
+ var _a;
876
+ return !((_a = item == null ? void 0 : item.metadata) == null ? void 0 : _a.custom_product_bundle_map_id);
877
+ });
878
+ if (mainDiscountListOriginal && mainDiscountListOriginal.length > 0) {
879
+ const allDiscountAmount = (0, import_utils.getDiscountListAmount)(mainDiscountListOriginal);
880
+ newTotalOriginal = new import_decimal.default(mainProductData.price || 0).minus(allDiscountAmount).toNumber() ?? newTotalOriginal;
881
+ newOriginTotalOriginal = mainProductData.origin_total ?? newOriginTotalOriginal;
882
+ }
883
+ if (newBundleOriginal.length > 0) {
884
+ newBundleOriginal.forEach((item) => {
885
+ newTotalOriginal += Number(item.price) * Number(item.num);
886
+ newOriginTotalOriginal += Number(item.price) * Number(item.num);
887
+ });
888
+ }
889
+ if (product == null ? void 0 : product.options) {
890
+ newTotalOriginal = product.options.reduce((accumulator, currentValue) => {
891
+ const currentPrice = new import_decimal.default(currentValue.price || 0);
892
+ const currentNum = new import_decimal.default(currentValue.num || 0);
893
+ return accumulator.add(currentPrice.mul(currentNum));
894
+ }, new import_decimal.default(newTotalOriginal)).toNumber();
895
+ }
896
+ result.push(
897
+ this.hooks.setProduct(mainProduct, {
898
+ ...mainProductData,
899
+ _id: `${mainProductData._id.split("___")[0]}___split_normal`,
900
+ quantity: mainProductQuantity - 1,
901
+ discount_list: updatedMainDiscountListOriginal,
902
+ bundle: newBundleOriginal,
903
+ total: newTotalOriginal,
904
+ origin_total: newOriginTotalOriginal
905
+ })
906
+ );
907
+ }
908
+ } else {
909
+ mainProductArr.forEach((mainProduct) => {
910
+ var _a, _b, _c;
911
+ const mainProductData = this.hooks.getProduct(mainProduct);
912
+ const newBundle = [];
913
+ if (product.bundle && Array.isArray(product.bundle)) {
914
+ product.bundle.forEach((bundleItem, bundleIndex) => {
915
+ const bundleItemId = `${product._id}_bundle_${bundleIndex}`;
916
+ const processedBundleItems = processedFlatItemsMap2.get(bundleItemId);
917
+ if (!processedBundleItems || processedBundleItems.length === 0) {
918
+ newBundle.push(bundleItem);
919
+ } else {
920
+ processedBundleItems.forEach((item) => {
921
+ const updatedDiscountList2 = (item.discount_list || []).map((discount) => {
922
+ var _a2;
923
+ return {
924
+ ...discount,
925
+ metadata: {
926
+ num: item.num,
927
+ custom_product_bundle_map_id: (_a2 = discount == null ? void 0 : discount.metadata) == null ? void 0 : _a2.custom_product_bundle_map_id
928
+ }
929
+ // num: item.num, // 使用拆分后的 num
930
+ };
931
+ });
932
+ newBundle.push({
933
+ ...bundleItem,
934
+ _id: item._id,
935
+ product_id: bundleItem.product_id,
936
+ price: item.price,
937
+ num: item.num,
938
+ discount_list: updatedDiscountList2
939
+ });
940
+ });
941
+ }
942
+ });
943
+ }
944
+ let newTotal = Number(mainProductData.price || 0);
945
+ let newOriginTotal = Number(mainProductData.original_price || mainProductData.price || 0);
946
+ const isManualDiscount = typeof mainProductData.isManualDiscount === "boolean" ? mainProductData.isManualDiscount : mainProductData.total != mainProductData.origin_total && (!((_a = mainProductData.discount_list) == null ? void 0 : _a.length) || ((_c = (_b = mainProductData == null ? void 0 : mainProductData.discount_list) == null ? void 0 : _b.every) == null ? void 0 : _c.call(_b, (item) => item.type === "product")));
947
+ if (isManualDiscount) {
948
+ newTotal = mainProductData.total ?? newTotal;
949
+ newOriginTotal = mainProductData.origin_total ?? newOriginTotal;
950
+ } else {
951
+ const mainDiscountList = mainProductData.discount_list.filter((item) => {
952
+ var _a2;
953
+ return !((_a2 = item == null ? void 0 : item.metadata) == null ? void 0 : _a2.custom_product_bundle_map_id);
954
+ });
955
+ if (mainDiscountList && mainDiscountList.length > 0) {
956
+ const allDiscountAmount = (0, import_utils.getDiscountListAmount)(mainDiscountList);
957
+ newTotal = new import_decimal.default(mainProductData.price || 0).minus(allDiscountAmount).toNumber() ?? newTotal;
958
+ newOriginTotal = mainProductData.origin_total ?? newOriginTotal;
959
+ }
960
+ if (newBundle.length > 0) {
961
+ newBundle.forEach((item) => {
962
+ newTotal += Number(item.price) * Number(item.num);
963
+ });
964
+ newBundle.forEach((item) => {
965
+ var _a2, _b2, _c2;
966
+ const originalPrice = ((_c2 = (_b2 = (_a2 = item.discount_list) == null ? void 0 : _a2[0]) == null ? void 0 : _b2.discount) == null ? void 0 : _c2.original_amount) || item.price;
967
+ newOriginTotal += Number(originalPrice) * Number(item.num);
968
+ });
969
+ }
970
+ }
971
+ if (product == null ? void 0 : product.options) {
972
+ newTotal = product.options.reduce((accumulator, currentValue) => {
973
+ const currentPrice = new import_decimal.default(currentValue.price || 0);
974
+ const currentNum = new import_decimal.default(currentValue.num || 0);
975
+ return accumulator.add(currentPrice.mul(currentNum));
976
+ }, new import_decimal.default(newTotal)).toNumber();
977
+ }
978
+ result.push(
979
+ this.hooks.setProduct(mainProduct, {
980
+ ...mainProductData,
981
+ bundle: newBundle,
982
+ total: newTotal,
983
+ origin_total: newOriginTotal
984
+ })
985
+ );
986
+ });
987
+ }
432
988
  }
433
- };
434
- const arr = processedProductsMap.get(product._id);
435
- (arr == null ? void 0 : arr.length) ? processedProductList.push(...arr) : processedProductList.push(getDefaultProduct());
436
- });
989
+ });
990
+ return result;
991
+ };
992
+ const processedProductList = reconstructProductsWithBundle(
993
+ processedProductsMap,
994
+ processedFlatItemsMap,
995
+ productList
996
+ );
437
997
  const updatedDiscountList = addModeDiscount.map((discount) => {
438
998
  const applicableProducts = discountApplicability.get(discount.id) || [];
439
999
  const applicableProductDetails = discountApplicableProducts.get(discount.id) || [];
@@ -494,6 +1054,75 @@ var RulesModule = class extends import_BaseModule.BaseModule {
494
1054
  discountList: [...editModeDiscount, ...updatedDiscountList]
495
1055
  };
496
1056
  }
1057
+ /**
1058
+ * 检查优惠是否符合 PackageSubItemUsageRules 配置
1059
+ * @param discount 优惠券
1060
+ * @param flatItem 扁平化后的商品项(可能是主商品或bundle子商品)
1061
+ * @returns 是否可用
1062
+ */
1063
+ checkPackageSubItemUsageRules(discount, flatItem) {
1064
+ var _a, _b, _c, _d;
1065
+ const limitedData = discount.limited_relation_product_data;
1066
+ const usageRules = limitedData == null ? void 0 : limitedData.package_sub_item_usage_rules;
1067
+ const rules = ["original_price", ...(usageRules == null ? void 0 : usageRules.rules) || []];
1068
+ if (!usageRules) {
1069
+ return true;
1070
+ }
1071
+ const ruleType = usageRules.type;
1072
+ const isMainProduct = flatItem.type === "main";
1073
+ const isBundleItem = flatItem.type === "bundle";
1074
+ if (isMainProduct) {
1075
+ return true;
1076
+ }
1077
+ if (ruleType === "universal_discount") {
1078
+ if (isMainProduct) {
1079
+ return true;
1080
+ }
1081
+ if (isBundleItem) {
1082
+ const priceType = (_a = flatItem.bundleItem) == null ? void 0 : _a.price_type;
1083
+ const priceTypeExt = (_b = flatItem.bundleItem) == null ? void 0 : _b.price_type_ext;
1084
+ const isOriginalPrice = priceType === "markup" && priceTypeExt === "product_price";
1085
+ const isMarkupPrice = priceType === "markup" && (priceTypeExt === "" || !priceTypeExt);
1086
+ if (rules.length > 0) {
1087
+ if (isOriginalPrice && rules.includes("original_price")) {
1088
+ return true;
1089
+ }
1090
+ if (isMarkupPrice && rules.includes("markup_price")) {
1091
+ return true;
1092
+ }
1093
+ return false;
1094
+ }
1095
+ return isOriginalPrice;
1096
+ }
1097
+ return true;
1098
+ }
1099
+ if (ruleType === "package_exclusive") {
1100
+ if (isMainProduct) {
1101
+ return false;
1102
+ }
1103
+ if (isBundleItem) {
1104
+ const priceType = (_c = flatItem.bundleItem) == null ? void 0 : _c.price_type;
1105
+ const priceTypeExt = (_d = flatItem.bundleItem) == null ? void 0 : _d.price_type_ext;
1106
+ const isOriginalPrice = priceType === "markup" && priceTypeExt === "product_price";
1107
+ const isMarkupPrice = priceType === "markup" && (priceTypeExt === "" || !priceTypeExt);
1108
+ if (rules.length > 0) {
1109
+ if (isOriginalPrice && rules.includes("original_price")) {
1110
+ return true;
1111
+ }
1112
+ if (isMarkupPrice && rules.includes("markup_price")) {
1113
+ return true;
1114
+ }
1115
+ return false;
1116
+ }
1117
+ return isOriginalPrice;
1118
+ }
1119
+ return false;
1120
+ }
1121
+ if (ruleType === "single_item_promo") {
1122
+ return isMainProduct;
1123
+ }
1124
+ return true;
1125
+ }
497
1126
  async destroy() {
498
1127
  this.core.effects.offByModuleDestroy(this.name);
499
1128
  await this.core.effects.emit(`${this.name}:onDestroy`, {});