@pisell/pisellos 0.0.328 → 0.0.330

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.
@@ -323,48 +323,35 @@ var PaymentModule = class extends import_BaseModule.BaseModule {
323
323
  totalAmount: params.total_amount
324
324
  });
325
325
  try {
326
- const existingOrder = await this.dbManager.get("order", params.order_id);
327
- this.logInfo("createPaymentOrderAsync existingOrder", {
328
- existingOrder
326
+ const newOrder = {
327
+ uuid: (0, import_utils.getUniqueId)("pay_order_"),
328
+ id: params.order_id,
329
+ order_id: params.order_id,
330
+ order_info: params.order_info,
331
+ payment_status: import_types.PaymentStatus.Processing,
332
+ payment: [],
333
+ adjust_offline_payments: [],
334
+ total_amount: params.total_amount,
335
+ expect_amount: params.total_amount,
336
+ tax_fee: "0.00",
337
+ is_deposit: params.is_deposit || 0,
338
+ deposit_amount: params.deposit_amount || "0.00"
339
+ };
340
+ const dbAddStartTime = Date.now();
341
+ await this.dbManager.add("order", newOrder);
342
+ const dbAddDuration = Date.now() - dbAddStartTime;
343
+ this.logInfo("Database add operation completed", {
344
+ operation: "dbManager.add",
345
+ table: "order",
346
+ orderUuid: newOrder.uuid,
347
+ orderId: newOrder.order_id,
348
+ duration: `${dbAddDuration}ms`,
349
+ performance: dbAddDuration > 100 ? "slow" : dbAddDuration > 50 ? "medium" : "fast"
329
350
  });
330
- if (existingOrder) {
331
- this.logInfo(
332
- `createPaymentOrderAsync found duplicate order ID: ${params.order_id}, updating existing payment order`
333
- );
334
- existingOrder.order_info = params.order_info;
335
- existingOrder.total_amount = params.total_amount;
336
- existingOrder.is_deposit = params.is_deposit || 0;
337
- existingOrder.deposit_amount = params.deposit_amount || "0.00";
338
- this.recalculateOrderAmount(existingOrder);
339
- await this.dbManager.update("order", existingOrder);
340
- await this.core.effects.emit(
341
- `${this.name}:onOrderUpdated`,
342
- existingOrder
343
- );
344
- return existingOrder;
345
- } else {
346
- const newOrder = {
347
- uuid: (0, import_utils.getUniqueId)("pay_order_"),
348
- id: params.order_id,
349
- order_id: params.order_id,
350
- order_info: params.order_info,
351
- payment_status: import_types.PaymentStatus.Processing,
352
- payment: [],
353
- adjust_offline_payments: [],
354
- total_amount: params.total_amount,
355
- expect_amount: params.total_amount,
356
- tax_fee: "0.00",
357
- is_deposit: params.is_deposit || 0,
358
- deposit_amount: params.deposit_amount || "0.00"
359
- };
360
- await this.dbManager.add("order", newOrder);
351
+ setTimeout(() => {
361
352
  this.core.effects.emit(`${this.name}:onOrderAdded`, newOrder);
362
- this.logInfo("createPaymentOrderAsync completed - new payment order created", {
363
- orderUuid: newOrder.uuid,
364
- orderId: newOrder.id
365
- });
366
- return newOrder;
367
- }
353
+ }, 0);
354
+ return newOrder;
368
355
  } catch (error) {
369
356
  console.error("[PaymentModule] 创建支付订单失败", error);
370
357
  this.logError("createPaymentOrderAsync failed", error, {
@@ -49,5 +49,5 @@ export declare class Product extends BaseModule implements Module {
49
49
  getCategories(): ProductCategory[];
50
50
  setOtherParams(key: string, value: any): void;
51
51
  getOtherParams(): any;
52
- getProductType(): "normal" | "duration" | "session";
52
+ getProductType(): "duration" | "session" | "normal";
53
53
  }
@@ -324,6 +324,15 @@ export declare class BookingByStepImpl extends BaseModule implements Module {
324
324
  success: boolean;
325
325
  minAvailableCount: number;
326
326
  };
327
+ /**
328
+ * 将 ProductData 转换为 CartItem,但不添加到购物车
329
+ * 参考 addProductToCart 方法的实现
330
+ */
331
+ private convertProductToCartItem;
332
+ checkMaxDurationCapacityForDetailNums(currentProduct: ProductData): {
333
+ success: boolean;
334
+ minAvailableCount: number;
335
+ };
327
336
  setOtherData(key: string, value: any): void;
328
337
  getOtherData(key: string): any;
329
338
  getProductTypeById(id: number): Promise<"normal" | "duration" | "session">;
@@ -34,16 +34,17 @@ __export(BookingByStep_exports, {
34
34
  module.exports = __toCommonJS(BookingByStep_exports);
35
35
  var import_BaseModule = require("../../modules/BaseModule");
36
36
  var import_types = require("./types");
37
+ var import_utils = require("../../modules/Cart/utils");
37
38
  var import_products = require("./utils/products");
38
39
  var import_resources = require("./utils/resources");
39
40
  var import_dayjs = __toESM(require("dayjs"));
40
41
  var import_isSameOrBefore = __toESM(require("dayjs/plugin/isSameOrBefore"));
41
42
  var import_isSameOrAfter = __toESM(require("dayjs/plugin/isSameOrAfter"));
42
- var import_utils = require("../../modules/Resource/utils");
43
+ var import_utils2 = require("../../modules/Resource/utils");
43
44
  var import_lodash_es = require("lodash-es");
44
- var import_utils2 = require("../../modules/Schedule/utils");
45
- var import_utils3 = require("../../modules/Date/utils");
46
- var import_utils4 = require("../../modules/Product/utils");
45
+ var import_utils3 = require("../../modules/Schedule/utils");
46
+ var import_utils4 = require("../../modules/Date/utils");
47
+ var import_utils5 = require("../../modules/Product/utils");
47
48
  var import_timeslots = require("./utils/timeslots");
48
49
  var import_changePrice = require("../../modules/Cart/utils/changePrice");
49
50
  var import_capacity = require("./utils/capacity");
@@ -363,7 +364,7 @@ var BookingByStepImpl = class extends import_BaseModule.BaseModule {
363
364
  let date = (_b = (_a = this.store.date.getDateRange()) == null ? void 0 : _a[0]) == null ? void 0 : _b.date;
364
365
  if (!date) {
365
366
  const normalProductCartItem = cartItems.find(
366
- (n) => !(0, import_utils4.isNormalProduct)(n._productOrigin)
367
+ (n) => !(0, import_utils5.isNormalProduct)(n._productOrigin)
367
368
  );
368
369
  date = (normalProductCartItem == null ? void 0 : normalProductCartItem.start_date) || "";
369
370
  }
@@ -473,7 +474,7 @@ var BookingByStepImpl = class extends import_BaseModule.BaseModule {
473
474
  let date = (_b = (_a = this.store.date.getDateRange()) == null ? void 0 : _a[0]) == null ? void 0 : _b.date;
474
475
  if (!date) {
475
476
  const normalProductCartItem = cartItems.find(
476
- (n) => !(0, import_utils4.isNormalProduct)(n._productOrigin)
477
+ (n) => !(0, import_utils5.isNormalProduct)(n._productOrigin)
477
478
  );
478
479
  date = (normalProductCartItem == null ? void 0 : normalProductCartItem.start_date) || "";
479
480
  }
@@ -525,7 +526,7 @@ var BookingByStepImpl = class extends import_BaseModule.BaseModule {
525
526
  }
526
527
  });
527
528
  let type = this.otherParams.isRetailTemplate ? "virtual" : "appointment_booking";
528
- if ((0, import_utils4.areAllNormalProducts)(
529
+ if ((0, import_utils5.areAllNormalProducts)(
529
530
  newCartItems.map((n) => n._productOrigin)
530
531
  )) {
531
532
  type = "virtual";
@@ -648,7 +649,7 @@ var BookingByStepImpl = class extends import_BaseModule.BaseModule {
648
649
  date
649
650
  }) {
650
651
  if (date) {
651
- const cartItems = this.store.cart.getItems().filter((n) => !(0, import_utils4.isNormalProduct)(n._productOrigin));
652
+ const cartItems = this.store.cart.getItems().filter((n) => !(0, import_utils5.isNormalProduct)(n._productOrigin));
652
653
  const cartItemsByDate = cartItems.filter(
653
654
  (n) => !(0, import_dayjs.default)(n.start_date).isSame((0, import_dayjs.default)(date.startTime), "day")
654
655
  );
@@ -810,7 +811,7 @@ var BookingByStepImpl = class extends import_BaseModule.BaseModule {
810
811
  * @returns 不符合条件的购物车商品ID列表
811
812
  */
812
813
  checkCartItems(type) {
813
- const cartItems = this.store.cart.getItems().filter((n) => !(0, import_utils4.isNormalProduct)(n._productOrigin));
814
+ const cartItems = this.store.cart.getItems().filter((n) => !(0, import_utils5.isNormalProduct)(n._productOrigin));
814
815
  const errorCartItemIds = [];
815
816
  cartItems.forEach((cartItem) => {
816
817
  const result = this.store.cart.checkCartItemByType(cartItem, type);
@@ -844,7 +845,7 @@ var BookingByStepImpl = class extends import_BaseModule.BaseModule {
844
845
  resources.push(...n.resource);
845
846
  });
846
847
  }
847
- const cartItems = this.store.cart.getItems().filter((n) => !(0, import_utils4.isNormalProduct)(n._productOrigin));
848
+ const cartItems = this.store.cart.getItems().filter((n) => !(0, import_utils5.isNormalProduct)(n._productOrigin));
848
849
  if (!resources.length) {
849
850
  const firstDateCartItem = cartItems == null ? void 0 : cartItems.find((n) => n.start_date);
850
851
  if (firstDateCartItem == null ? void 0 : firstDateCartItem.start_date) {
@@ -860,7 +861,7 @@ var BookingByStepImpl = class extends import_BaseModule.BaseModule {
860
861
  });
861
862
  }
862
863
  }
863
- const resourcesMap = (0, import_utils.getResourcesMap)((0, import_lodash_es.cloneDeep)(resources));
864
+ const resourcesMap = (0, import_utils2.getResourcesMap)((0, import_lodash_es.cloneDeep)(resources));
864
865
  const arr = [];
865
866
  cartItems.forEach((cartItem) => {
866
867
  var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l;
@@ -989,14 +990,14 @@ var BookingByStepImpl = class extends import_BaseModule.BaseModule {
989
990
  */
990
991
  getResourcesListByCartItem(id) {
991
992
  var _a, _b;
992
- const cartItems = this.store.cart.getItems().filter((n) => !(0, import_utils4.isNormalProduct)(n._productOrigin));
993
+ const cartItems = this.store.cart.getItems().filter((n) => !(0, import_utils5.isNormalProduct)(n._productOrigin));
993
994
  const dateRange = this.store.date.getDateRange();
994
995
  const resources = [];
995
996
  dateRange.forEach((n) => {
996
997
  if (n.resource)
997
998
  resources.push(...n.resource);
998
999
  });
999
- const resourcesMap = (0, import_utils.getResourcesMap)(resources);
1000
+ const resourcesMap = (0, import_utils2.getResourcesMap)(resources);
1000
1001
  const targetCartItem = cartItems.find((n) => n._id === id);
1001
1002
  if (!targetCartItem) {
1002
1003
  throw new Error(`没有找到${id}购物车商品`);
@@ -1053,7 +1054,7 @@ var BookingByStepImpl = class extends import_BaseModule.BaseModule {
1053
1054
  capacity
1054
1055
  }) {
1055
1056
  var _a, _b, _c;
1056
- if ((0, import_utils4.isNormalProduct)(cartItem._productOrigin)) {
1057
+ if ((0, import_utils5.isNormalProduct)(cartItem._productOrigin)) {
1057
1058
  return {};
1058
1059
  }
1059
1060
  const dateRange = this.store.date.getDateRange();
@@ -1088,7 +1089,7 @@ var BookingByStepImpl = class extends import_BaseModule.BaseModule {
1088
1089
  });
1089
1090
  }
1090
1091
  }
1091
- const resourcesMap = (0, import_utils.getResourcesMap)((0, import_lodash_es.cloneDeep)(AllResources));
1092
+ const resourcesMap = (0, import_utils2.getResourcesMap)((0, import_lodash_es.cloneDeep)(AllResources));
1092
1093
  const allCartItems = (0, import_lodash_es.cloneDeep)(this.store.cart.getItems());
1093
1094
  const selectedResources = (0, import_resources.getOthersSelectedResources)(
1094
1095
  allCartItems,
@@ -1169,7 +1170,7 @@ var BookingByStepImpl = class extends import_BaseModule.BaseModule {
1169
1170
  selectedResource: targetResource
1170
1171
  };
1171
1172
  } else {
1172
- const resourcesMap2 = (0, import_utils.getResourcesMap)(resources);
1173
+ const resourcesMap2 = (0, import_utils2.getResourcesMap)(resources);
1173
1174
  const resourceIds = resources.map((n) => n.id);
1174
1175
  const timeSlots2 = (0, import_resources.getTimeSlicesByResources)({
1175
1176
  resourceIds,
@@ -1307,7 +1308,7 @@ var BookingByStepImpl = class extends import_BaseModule.BaseModule {
1307
1308
  currentResourcesRenderList.push(...n.renderList || []);
1308
1309
  }
1309
1310
  });
1310
- const resourcesMap = (0, import_utils.getResourcesMap)(currentResourcesRenderList);
1311
+ const resourcesMap = (0, import_utils2.getResourcesMap)(currentResourcesRenderList);
1311
1312
  if (item.holder_id) {
1312
1313
  selectedResources = (0, import_resources.getOthersSelectedResources)(
1313
1314
  allCartItems,
@@ -1372,7 +1373,7 @@ var BookingByStepImpl = class extends import_BaseModule.BaseModule {
1372
1373
  }
1373
1374
  });
1374
1375
  };
1375
- const cartItems = this.store.cart.getItems().filter((n) => !(0, import_utils4.isNormalProduct)(n._productOrigin));
1376
+ const cartItems = this.store.cart.getItems().filter((n) => !(0, import_utils5.isNormalProduct)(n._productOrigin));
1376
1377
  if (cartItems == null ? void 0 : cartItems[0].holder_id) {
1377
1378
  accountList.forEach((account) => {
1378
1379
  const cartItems2 = this.store.cart.getCartByAccount(account.getId());
@@ -1388,7 +1389,7 @@ var BookingByStepImpl = class extends import_BaseModule.BaseModule {
1388
1389
  var _a, _b, _c, _d, _e, _f, _g, _h;
1389
1390
  let dateRange = this.store.date.getDateRange();
1390
1391
  const resources = [];
1391
- const cartItems = this.store.cart.getItems().filter((n) => !(0, import_utils4.isNormalProduct)(n._productOrigin));
1392
+ const cartItems = this.store.cart.getItems().filter((n) => !(0, import_utils5.isNormalProduct)(n._productOrigin));
1392
1393
  const resourceIds = [];
1393
1394
  let resourcesTypeId = void 0;
1394
1395
  let isSingleResource = false;
@@ -1426,7 +1427,7 @@ var BookingByStepImpl = class extends import_BaseModule.BaseModule {
1426
1427
  }
1427
1428
  });
1428
1429
  }
1429
- const resourcesMap = (0, import_utils.getResourcesMap)(resources);
1430
+ const resourcesMap = (0, import_utils2.getResourcesMap)(resources);
1430
1431
  let duration = 0;
1431
1432
  const accountList = this.store.accountList.getAccounts();
1432
1433
  const checkDuration = (cartItems2) => {
@@ -1525,7 +1526,7 @@ var BookingByStepImpl = class extends import_BaseModule.BaseModule {
1525
1526
  // 提交时间切片,绑定到对应购物车的商品上,更新购物车---只有 duration 商品
1526
1527
  submitTimeSlot(timeSlots) {
1527
1528
  var _a, _b;
1528
- const cartItems = this.store.cart.getItems().filter((n) => !(0, import_utils4.isNormalProduct)(n._productOrigin));
1529
+ const cartItems = this.store.cart.getItems().filter((n) => !(0, import_utils5.isNormalProduct)(n._productOrigin));
1529
1530
  const allResources = this.store.date.getResourcesListByDate(
1530
1531
  timeSlots.start_at.format("YYYY-MM-DD")
1531
1532
  );
@@ -1603,7 +1604,7 @@ var BookingByStepImpl = class extends import_BaseModule.BaseModule {
1603
1604
  getScheduleDataByIds(scheduleIds) {
1604
1605
  const targetSchedules = this.store.schedule.getScheduleListByIds(scheduleIds);
1605
1606
  const targetSchedulesData = targetSchedules.map((item) => {
1606
- return (0, import_utils2.calcCalendarDataByScheduleResult)(item);
1607
+ return (0, import_utils3.calcCalendarDataByScheduleResult)(item);
1607
1608
  });
1608
1609
  const newSchedule = {};
1609
1610
  targetSchedulesData.forEach((item) => {
@@ -1675,7 +1676,7 @@ var BookingByStepImpl = class extends import_BaseModule.BaseModule {
1675
1676
  const resourcesDates = this.store.date.getDateList();
1676
1677
  const targetResourceDate = resourcesDates.find((n) => n.date === date);
1677
1678
  const cartItems = (0, import_lodash_es.cloneDeep)(this.store.cart.getItems());
1678
- const resourcesMap = (0, import_utils.getResourcesMap)((targetResourceDate == null ? void 0 : targetResourceDate.resource) || []);
1679
+ const resourcesMap = (0, import_utils2.getResourcesMap)((targetResourceDate == null ? void 0 : targetResourceDate.resource) || []);
1679
1680
  const selectedResources = (0, import_resources.getOthersSelectedResources)(
1680
1681
  cartItems,
1681
1682
  "",
@@ -1687,12 +1688,12 @@ var BookingByStepImpl = class extends import_BaseModule.BaseModule {
1687
1688
  selectedResources,
1688
1689
  1
1689
1690
  );
1690
- const minTimeMaxTime = (0, import_utils2.calcMinTimeMaxTimeBySchedules)(
1691
+ const minTimeMaxTime = (0, import_utils3.calcMinTimeMaxTimeBySchedules)(
1691
1692
  targetSchedules,
1692
1693
  {},
1693
1694
  date
1694
1695
  );
1695
- const scheduleTimeSlots = (0, import_utils2.getAllSortedDateRanges)(minTimeMaxTime);
1696
+ const scheduleTimeSlots = (0, import_utils3.getAllSortedDateRanges)(minTimeMaxTime);
1696
1697
  let allProductResources = productResources.flatMap((n) => n.renderList);
1697
1698
  allProductResources.sort((a, b) => {
1698
1699
  var _a2, _b2, _c2, _d2;
@@ -1808,7 +1809,7 @@ var BookingByStepImpl = class extends import_BaseModule.BaseModule {
1808
1809
  otherSameTimesCartItems.forEach((m) => {
1809
1810
  var _a2, _b2;
1810
1811
  const productResources2 = (0, import_resources.getResourcesByProduct)(
1811
- (0, import_utils.getResourcesMap)((targetResourceDate == null ? void 0 : targetResourceDate.resource) || []),
1812
+ (0, import_utils2.getResourcesMap)((targetResourceDate == null ? void 0 : targetResourceDate.resource) || []),
1812
1813
  ((_b2 = (_a2 = m._productOrigin) == null ? void 0 : _a2.product_resource) == null ? void 0 : _b2.resources) || [],
1813
1814
  selectedResources,
1814
1815
  1
@@ -1955,7 +1956,7 @@ var BookingByStepImpl = class extends import_BaseModule.BaseModule {
1955
1956
  const targetResourceDate = resourcesDates.find((n) => n.date === dateRange[0].date);
1956
1957
  if (!targetResourceDate)
1957
1958
  return { success: false, minAvailableCount: 0 };
1958
- const resourcesMap = (0, import_utils.getResourcesMap)(targetResourceDate.resource || []);
1959
+ const resourcesMap = (0, import_utils2.getResourcesMap)(targetResourceDate.resource || []);
1959
1960
  const resourceCodeToFormIdMap = {};
1960
1961
  Object.values(itemsByResourceType).flat().forEach((cartItem) => {
1961
1962
  var _a2, _b2;
@@ -2097,7 +2098,245 @@ var BookingByStepImpl = class extends import_BaseModule.BaseModule {
2097
2098
  const targetResourceDate = resourcesDates.find((n) => n.date === startDate);
2098
2099
  if (!targetResourceDate)
2099
2100
  continue;
2100
- const resourcesMap = (0, import_utils.getResourcesMap)(targetResourceDate.resource || []);
2101
+ const resourcesMap = (0, import_utils2.getResourcesMap)(targetResourceDate.resource || []);
2102
+ itemsInTimeSlot.forEach((cartItem) => {
2103
+ if (!cartItem._productOrigin)
2104
+ return;
2105
+ const productResourceIds = (0, import_capacity.getResourcesIdsByProduct)(cartItem._productOrigin);
2106
+ productResourceIds.forEach((resourceId) => {
2107
+ if (resourcesMap[resourceId] && !resourcesIdSet.has(resourceId)) {
2108
+ resourcesIdSet.add(resourceId);
2109
+ allResourcesForTimeSlot.push(resourcesMap[resourceId]);
2110
+ }
2111
+ });
2112
+ });
2113
+ if (!(0, import_capacity.checkTimeSlotCapacity)(timeSlotStart, timeSlotEnd, itemsInTimeSlot, allResourcesForTimeSlot)) {
2114
+ const minAvailableCount2 = availableCountsByResourceType.length > 0 ? Math.min(...availableCountsByResourceType) : 0;
2115
+ return { success: false, minAvailableCount: minAvailableCount2 };
2116
+ }
2117
+ }
2118
+ const minAvailableCount = availableCountsByResourceType.length > 0 ? Math.min(...availableCountsByResourceType) : 0;
2119
+ return { success: true, minAvailableCount };
2120
+ }
2121
+ /**
2122
+ * 将 ProductData 转换为 CartItem,但不添加到购物车
2123
+ * 参考 addProductToCart 方法的实现
2124
+ */
2125
+ convertProductToCartItem(product) {
2126
+ const {
2127
+ bundle,
2128
+ options,
2129
+ origin,
2130
+ product_variant_id,
2131
+ quantity = 1
2132
+ } = product || {};
2133
+ const productData = { ...origin, product_variant_id };
2134
+ const processedProduct = (0, import_utils.handleVariantProduct)(productData);
2135
+ const cartItem = {
2136
+ _id: (0, import_utils.getUniqueId)("temp_"),
2137
+ _origin: (0, import_utils.createCartItemOrigin)(),
2138
+ _productOrigin: processedProduct,
2139
+ _productInit: product
2140
+ };
2141
+ const activeAccount = this.getActiveAccount();
2142
+ (0, import_utils.formatProductToCartItem)({
2143
+ cartItem,
2144
+ product: processedProduct,
2145
+ bundle,
2146
+ options,
2147
+ product_variant_id,
2148
+ quantity
2149
+ });
2150
+ return cartItem;
2151
+ }
2152
+ checkMaxDurationCapacityForDetailNums(currentProduct) {
2153
+ var _a, _b;
2154
+ const cartItems = (0, import_lodash_es.cloneDeep)(this.store.cart.getItems());
2155
+ const currentCartItem = this.convertProductToCartItem(currentProduct);
2156
+ cartItems.push(currentCartItem);
2157
+ if (cartItems.length === 0)
2158
+ return { success: true, minAvailableCount: 0 };
2159
+ const itemsWithTime = [];
2160
+ const itemsWithoutTime = [];
2161
+ const availableCountsByResourceType = [];
2162
+ cartItems.forEach((cartItem) => {
2163
+ if (cartItem.start_time && cartItem.end_time && cartItem.start_date) {
2164
+ itemsWithTime.push(cartItem);
2165
+ } else {
2166
+ itemsWithoutTime.push(cartItem);
2167
+ }
2168
+ });
2169
+ const processedItemsWithoutTime = [];
2170
+ if (itemsWithoutTime.length > 0) {
2171
+ const itemsByResourceType = {};
2172
+ itemsWithoutTime.forEach((cartItem) => {
2173
+ var _a2;
2174
+ if (!cartItem._productOrigin)
2175
+ return;
2176
+ const resourceTypes = ((_a2 = cartItem._productOrigin.product_resource) == null ? void 0 : _a2.resources) || [];
2177
+ resourceTypes.forEach((resourceType) => {
2178
+ var _a3;
2179
+ if (resourceType.status === 1) {
2180
+ const resourceCode = resourceType.code || ((_a3 = resourceType.id) == null ? void 0 : _a3.toString());
2181
+ if (!itemsByResourceType[resourceCode]) {
2182
+ itemsByResourceType[resourceCode] = [];
2183
+ }
2184
+ if (!itemsByResourceType[resourceCode].find((item) => item._id === cartItem._id)) {
2185
+ itemsByResourceType[resourceCode].push(cartItem);
2186
+ }
2187
+ }
2188
+ });
2189
+ });
2190
+ const dateRange = this.store.date.getDateRange();
2191
+ if (!dateRange || dateRange.length === 0)
2192
+ return { success: false, minAvailableCount: 0 };
2193
+ const resourcesDates = this.store.date.getDateList();
2194
+ const targetResourceDate = resourcesDates.find((n) => n.date === dateRange[0].date);
2195
+ if (!targetResourceDate)
2196
+ return { success: false, minAvailableCount: 0 };
2197
+ const resourcesMap = (0, import_utils2.getResourcesMap)(targetResourceDate.resource || []);
2198
+ const resourceCodeToFormIdMap = {};
2199
+ Object.values(itemsByResourceType).flat().forEach((cartItem) => {
2200
+ var _a2, _b2;
2201
+ if ((_b2 = (_a2 = cartItem._productOrigin) == null ? void 0 : _a2.product_resource) == null ? void 0 : _b2.resources) {
2202
+ cartItem._productOrigin.product_resource.resources.forEach((resourceConfig) => {
2203
+ var _a3, _b3;
2204
+ if (resourceConfig.status === 1 && resourceConfig.code) {
2205
+ const formId = ((_a3 = resourceConfig.id) == null ? void 0 : _a3.toString()) || ((_b3 = resourceConfig.resource_type_id) == null ? void 0 : _b3.toString());
2206
+ if (formId) {
2207
+ resourceCodeToFormIdMap[resourceConfig.code] = formId;
2208
+ }
2209
+ }
2210
+ });
2211
+ }
2212
+ });
2213
+ let hasCapacityIssue = false;
2214
+ const resourceCapacityInfo = [];
2215
+ const processedCartItemIds = /* @__PURE__ */ new Set();
2216
+ for (const [resourceCode, items] of Object.entries(itemsByResourceType)) {
2217
+ const targetFormId = resourceCodeToFormIdMap[resourceCode];
2218
+ if (!targetFormId) {
2219
+ console.log(`资源类型 ${resourceCode} 找不到对应的 form_id`);
2220
+ return { success: false, minAvailableCount: 0 };
2221
+ }
2222
+ const resourcesOfThisType = [];
2223
+ items.forEach((cartItem) => {
2224
+ const productResourceIds = (0, import_capacity.getResourcesIdsByProduct)(cartItem._productOrigin);
2225
+ productResourceIds.forEach((resourceId) => {
2226
+ var _a2;
2227
+ const resource = resourcesMap[resourceId];
2228
+ if (resource && ((_a2 = resource.form_id) == null ? void 0 : _a2.toString()) === targetFormId) {
2229
+ if (!resourcesOfThisType.find((r) => r.id === resource.id)) {
2230
+ resourcesOfThisType.push(resource);
2231
+ }
2232
+ }
2233
+ });
2234
+ });
2235
+ if (resourcesOfThisType.length === 0) {
2236
+ console.log(`资源类型 ${resourceCode} 没有找到可用资源`);
2237
+ return { success: false, minAvailableCount: 0 };
2238
+ }
2239
+ let resourceTypeConfig = null;
2240
+ for (const cartItem of items) {
2241
+ if ((_b = (_a = cartItem._productOrigin) == null ? void 0 : _a.product_resource) == null ? void 0 : _b.resources) {
2242
+ resourceTypeConfig = cartItem._productOrigin.product_resource.resources.find(
2243
+ (r) => r.code === resourceCode && r.status === 1
2244
+ );
2245
+ if (resourceTypeConfig)
2246
+ break;
2247
+ }
2248
+ }
2249
+ const isMultipleBooking = (resourceTypeConfig == null ? void 0 : resourceTypeConfig.type) === "multiple";
2250
+ let totalAvailable;
2251
+ let requiredAmount;
2252
+ let availableAmount;
2253
+ if (isMultipleBooking) {
2254
+ totalAvailable = resourcesOfThisType.reduce((sum, resource) => {
2255
+ return sum + (resource.capacity || 0);
2256
+ }, 0);
2257
+ requiredAmount = items.reduce((sum, cartItem) => {
2258
+ const { currentCapacity } = (0, import_capacity.getCapacityInfoByCartItem)(cartItem);
2259
+ return sum + currentCapacity;
2260
+ }, 0);
2261
+ availableAmount = Math.max(0, totalAvailable - requiredAmount);
2262
+ } else {
2263
+ totalAvailable = resourcesOfThisType.length;
2264
+ requiredAmount = items.reduce((sum, cartItem) => {
2265
+ return sum + (cartItem.num || 1);
2266
+ }, 0);
2267
+ availableAmount = Math.max(0, totalAvailable - requiredAmount);
2268
+ }
2269
+ resourceCapacityInfo.push({
2270
+ code: resourceCode,
2271
+ available: availableAmount,
2272
+ total: totalAvailable,
2273
+ required: requiredAmount,
2274
+ isMultiple: isMultipleBooking
2275
+ });
2276
+ availableCountsByResourceType.push(availableAmount);
2277
+ if (requiredAmount > totalAvailable) {
2278
+ hasCapacityIssue = true;
2279
+ console.log(`资源类型 ${resourceCode} ${isMultipleBooking ? "容量" : "资源数量"}不足: 需要 ${requiredAmount}, 总共 ${totalAvailable}`);
2280
+ }
2281
+ console.log(`资源类型 ${resourceCode} 的资源时间信息:`, resourcesOfThisType.map((r) => ({
2282
+ id: r.id,
2283
+ times: r.times.map((t) => `${t.start_at} - ${t.end_at}`)
2284
+ })));
2285
+ const commonTimeSlots = this.findCommonAvailableTimeSlots(resourcesOfThisType);
2286
+ console.log(`资源类型 ${resourceCode} 的公共时间段:`, commonTimeSlots);
2287
+ if (commonTimeSlots.length === 0) {
2288
+ console.log(`资源类型 ${resourceCode} 没有公共可用时间段`);
2289
+ return { success: false, minAvailableCount: 0 };
2290
+ }
2291
+ const firstCommonSlot = commonTimeSlots[0];
2292
+ console.log(`使用公共时间段: ${firstCommonSlot.startTime} - ${firstCommonSlot.endTime}`);
2293
+ items.forEach((cartItem) => {
2294
+ if (!processedCartItemIds.has(cartItem._id)) {
2295
+ processedCartItemIds.add(cartItem._id);
2296
+ const processedItem = {
2297
+ ...cartItem,
2298
+ start_date: dateRange[0].date,
2299
+ start_time: firstCommonSlot.startTime,
2300
+ end_time: firstCommonSlot.endTime,
2301
+ end_date: dateRange[0].date
2302
+ };
2303
+ processedItemsWithoutTime.push(processedItem);
2304
+ }
2305
+ });
2306
+ }
2307
+ if (hasCapacityIssue) {
2308
+ const overCapacityResources = resourceCapacityInfo.filter((info) => info.required > info.total);
2309
+ if (overCapacityResources.length > 0) {
2310
+ const minTotalCapacity2 = Math.min(...overCapacityResources.map((info) => info.total));
2311
+ return { success: false, minAvailableCount: minTotalCapacity2 };
2312
+ }
2313
+ const minTotalCapacity = Math.min(...resourceCapacityInfo.map((info) => info.total));
2314
+ return { success: false, minAvailableCount: minTotalCapacity };
2315
+ }
2316
+ }
2317
+ const allProcessedItems = [...itemsWithTime, ...processedItemsWithoutTime];
2318
+ const cartItemsByTimeSlot = {};
2319
+ allProcessedItems.forEach((cartItem) => {
2320
+ if (!cartItem.start_time || !cartItem.end_time || !cartItem.start_date)
2321
+ return;
2322
+ const timeSlotKey = `${cartItem.start_date}_${cartItem.start_time}_${cartItem.end_date || cartItem.start_date}_${cartItem.end_time}`;
2323
+ if (!cartItemsByTimeSlot[timeSlotKey]) {
2324
+ cartItemsByTimeSlot[timeSlotKey] = [];
2325
+ }
2326
+ cartItemsByTimeSlot[timeSlotKey].push(cartItem);
2327
+ });
2328
+ for (const [timeSlotKey, itemsInTimeSlot] of Object.entries(cartItemsByTimeSlot)) {
2329
+ const [startDate, startTime, endDate, endTime] = timeSlotKey.split("_");
2330
+ const timeSlotStart = `${startDate} ${startTime}`;
2331
+ const timeSlotEnd = `${endDate} ${endTime}`;
2332
+ const allResourcesForTimeSlot = [];
2333
+ const resourcesIdSet = /* @__PURE__ */ new Set();
2334
+ const dateRange = this.store.date.getDateRange();
2335
+ const resourcesDates = this.store.date.getDateList();
2336
+ const targetResourceDate = resourcesDates.find((n) => n.date === startDate);
2337
+ if (!targetResourceDate)
2338
+ continue;
2339
+ const resourcesMap = (0, import_utils2.getResourcesMap)(targetResourceDate.resource || []);
2101
2340
  itemsInTimeSlot.forEach((cartItem) => {
2102
2341
  if (!cartItem._productOrigin)
2103
2342
  return;
@@ -2168,8 +2407,8 @@ var BookingByStepImpl = class extends import_BaseModule.BaseModule {
2168
2407
  resources.push(...n.resource);
2169
2408
  });
2170
2409
  }
2171
- const resourcesMap = (0, import_utils.getResourcesMap)((0, import_lodash_es.cloneDeep)(resources));
2172
- const cartItems = this.store.cart.getItems().filter((n) => !(0, import_utils4.isNormalProduct)(n._productOrigin));
2410
+ const resourcesMap = (0, import_utils2.getResourcesMap)((0, import_lodash_es.cloneDeep)(resources));
2411
+ const cartItems = this.store.cart.getItems().filter((n) => !(0, import_utils5.isNormalProduct)(n._productOrigin));
2173
2412
  const cartItem = cartItems.find((item) => item._id === cartItemId);
2174
2413
  if (!cartItem)
2175
2414
  return [];
@@ -2382,12 +2621,12 @@ var BookingByStepImpl = class extends import_BaseModule.BaseModule {
2382
2621
  }
2383
2622
  }
2384
2623
  if (status === "available") {
2385
- const minTimeMaxTime = (0, import_utils2.calcMinTimeMaxTimeBySchedules)(
2624
+ const minTimeMaxTime = (0, import_utils3.calcMinTimeMaxTimeBySchedules)(
2386
2625
  targetSchedules,
2387
2626
  {},
2388
2627
  currentDateStr
2389
2628
  );
2390
- const scheduleTimeSlots = (0, import_utils2.getAllSortedDateRanges)(minTimeMaxTime);
2629
+ const scheduleTimeSlots = (0, import_utils3.getAllSortedDateRanges)(minTimeMaxTime);
2391
2630
  const timesSlotCanUse = scheduleTimeSlots.some((item) => {
2392
2631
  const resourcesUseableMap = {};
2393
2632
  return openResources.every((resource) => {
@@ -2444,7 +2683,7 @@ var BookingByStepImpl = class extends import_BaseModule.BaseModule {
2444
2683
  }
2445
2684
  currentDate = (0, import_dayjs.default)(currentDate).add(1, "day");
2446
2685
  }
2447
- dates = (0, import_utils3.handleAvailableDateByResource)(res.data, dates);
2686
+ dates = (0, import_utils4.handleAvailableDateByResource)(res.data, dates);
2448
2687
  this.store.date.setDateList(dates);
2449
2688
  if (!this.store.currentProductMeta)
2450
2689
  this.store.currentProductMeta = {};
@@ -2460,11 +2699,11 @@ var BookingByStepImpl = class extends import_BaseModule.BaseModule {
2460
2699
  };
2461
2700
  }
2462
2701
  isCartAllNormalProducts() {
2463
- const cartItems = this.store.cart.getItems().filter((n) => !(0, import_utils4.isNormalProduct)(n._productOrigin));
2702
+ const cartItems = this.store.cart.getItems().filter((n) => !(0, import_utils5.isNormalProduct)(n._productOrigin));
2464
2703
  return !cartItems.length;
2465
2704
  }
2466
2705
  isCartHasDurationProduct() {
2467
- const cartItems = this.store.cart.getItems().filter((n) => !(0, import_utils4.isNormalProduct)(n._productOrigin));
2706
+ const cartItems = this.store.cart.getItems().filter((n) => !(0, import_utils5.isNormalProduct)(n._productOrigin));
2468
2707
  return cartItems.some((n) => {
2469
2708
  var _a;
2470
2709
  return (_a = n._productOrigin) == null ? void 0 : _a.duration;
@@ -2473,11 +2712,11 @@ var BookingByStepImpl = class extends import_BaseModule.BaseModule {
2473
2712
  isTargetNormalProduct(product) {
2474
2713
  if (!product)
2475
2714
  return false;
2476
- return (0, import_utils4.isNormalProduct)(product);
2715
+ return (0, import_utils5.isNormalProduct)(product);
2477
2716
  }
2478
2717
  isTargetCartIdNormalProduct(id) {
2479
2718
  const cartItem = this.store.cart.getItems().find((n) => n._id === id);
2480
- return cartItem && (0, import_utils4.isNormalProduct)(cartItem._productOrigin);
2719
+ return cartItem && (0, import_utils5.isNormalProduct)(cartItem._productOrigin);
2481
2720
  }
2482
2721
  };
2483
2722
  // Annotate the CommonJS export names for ESM import in node:
@@ -15,8 +15,9 @@ export declare const formatDefaultCapacitys: ({ capacity, product_bundle, }: any
15
15
  * @return {*}
16
16
  * @Author: zhiwei.Wang
17
17
  */
18
- export declare const getSumCapacity: ({ capacity }: {
18
+ export declare const getSumCapacity: ({ capacity, num }: {
19
19
  capacity: CapacityItem[];
20
+ num: number;
20
21
  }) => number;
21
22
  /**
22
23
  * 给定购物车数据,返回对应的 capacity 信息和套餐 capacity
@@ -68,20 +68,20 @@ var formatDefaultCapacitys = ({
68
68
  }
69
69
  return [{ id: 0, value: 1, name: "" }];
70
70
  };
71
- var getSumCapacity = ({ capacity }) => {
71
+ var getSumCapacity = ({ capacity, num = 1 }) => {
72
72
  let sum = 0;
73
73
  for (let item of capacity || []) {
74
74
  sum += item.value;
75
75
  }
76
- return sum;
76
+ return sum * num;
77
77
  };
78
78
  function getCapacityInfoByCartItem(targetCartItem) {
79
- var _a;
79
+ var _a, _b, _c;
80
80
  const formatCapacity = formatDefaultCapacitys({
81
81
  capacity: (_a = targetCartItem._productOrigin) == null ? void 0 : _a.capacity,
82
82
  product_bundle: targetCartItem._origin.product.product_bundle
83
83
  });
84
- const currentCapacity = getSumCapacity({ capacity: formatCapacity });
84
+ const currentCapacity = getSumCapacity({ capacity: formatCapacity, num: ((_c = (_b = targetCartItem == null ? void 0 : targetCartItem._origin) == null ? void 0 : _b.product) == null ? void 0 : _c.quantity) || 1 });
85
85
  return {
86
86
  formatCapacity,
87
87
  currentCapacity