@pisell/pisellos 0.0.114 → 0.0.116

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.
@@ -42,12 +42,10 @@ export declare class BookingByStepImpl extends BaseModule implements Module {
42
42
  * 更新step
43
43
  */
44
44
  updateStep(key: string, step: IStep): void;
45
- loadProducts({ category_ids, product_ids, collection, schedule_ids, schedule_date, }: {
45
+ loadProducts({ category_ids, product_ids, collection, }: {
46
46
  category_ids?: number[];
47
47
  product_ids?: number[];
48
48
  collection?: number | string[];
49
- schedule_ids?: number[];
50
- schedule_date?: string;
51
49
  }): Promise<any>;
52
50
  loadProductByScheduleDate({ date, product_ids, category_ids, }: {
53
51
  date: string;
@@ -37,6 +37,7 @@ import { getResourcesMap } from "../../modules/Resource/utils";
37
37
  import { cloneDeep } from 'lodash-es';
38
38
  import { calcCalendarDataByScheduleResult, calcMinTimeMaxTimeBySchedules, getAllSortedDateRanges } from "../../modules/Schedule/utils";
39
39
  import { disableAllDates, disableDatesBeforeOneDay, generateMonthDates, handleAvailableDateByResource } from "../../modules/Date/utils";
40
+ import { calculateResourceAvailableTime, findFastestAvailableResource } from "./utils/timeslots";
40
41
  export var BookingByStepImpl = /*#__PURE__*/function (_BaseModule) {
41
42
  _inherits(BookingByStepImpl, _BaseModule);
42
43
  var _super = _createSuper(BookingByStepImpl);
@@ -207,24 +208,23 @@ export var BookingByStepImpl = /*#__PURE__*/function (_BaseModule) {
207
208
  key: "loadProducts",
208
209
  value: function () {
209
210
  var _loadProducts = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(_ref) {
210
- var _schedule_ids;
211
- var _ref$category_ids, category_ids, _ref$product_ids, product_ids, _ref$collection, collection, _ref$schedule_ids, schedule_ids, schedule_date, schedule_ids_data, userPlugin, customer_id, _userPlugin$get, productsData;
211
+ var _ref$category_ids, category_ids, _ref$product_ids, product_ids, _ref$collection, collection, userPlugin, customer_id, _userPlugin$get, productsData;
212
212
  return _regeneratorRuntime().wrap(function _callee2$(_context2) {
213
213
  while (1) switch (_context2.prev = _context2.next) {
214
214
  case 0:
215
- _ref$category_ids = _ref.category_ids, category_ids = _ref$category_ids === void 0 ? [] : _ref$category_ids, _ref$product_ids = _ref.product_ids, product_ids = _ref$product_ids === void 0 ? [] : _ref$product_ids, _ref$collection = _ref.collection, collection = _ref$collection === void 0 ? [] : _ref$collection, _ref$schedule_ids = _ref.schedule_ids, schedule_ids = _ref$schedule_ids === void 0 ? [] : _ref$schedule_ids, schedule_date = _ref.schedule_date;
216
- // 如果 schedule_ids 为空,则需要尝试从 schedule 模块里获取
217
- if (!((_schedule_ids = schedule_ids) !== null && _schedule_ids !== void 0 && _schedule_ids.length)) {
218
- schedule_ids_data = this.store.schedule.getScheduleListByIds(schedule_ids).map(function (n) {
219
- return n.id;
220
- });
221
- if (schedule_ids_data.length) {
222
- schedule_ids = schedule_ids_data;
223
- } else if (schedule_date) {
224
- // 后端说如果 schedule_ids 如果为空,需要给[0]
225
- schedule_ids = [0];
226
- }
227
- }
215
+ _ref$category_ids = _ref.category_ids, category_ids = _ref$category_ids === void 0 ? [] : _ref$category_ids, _ref$product_ids = _ref.product_ids, product_ids = _ref$product_ids === void 0 ? [] : _ref$product_ids, _ref$collection = _ref.collection, collection = _ref$collection === void 0 ? [] : _ref$collection;
216
+ // // 如果 schedule_ids 为空,则需要尝试从 schedule 模块里获取
217
+ // if (!schedule_ids?.length) {
218
+ // const schedule_ids_data = this.store.schedule
219
+ // .getScheduleListByIds(schedule_ids)
220
+ // .map((n) => n.id);
221
+ // if (schedule_ids_data.length) {
222
+ // schedule_ids = schedule_ids_data;
223
+ // } else if (schedule_date) {
224
+ // // 后端说如果 schedule_ids 如果为空,需要给[0]
225
+ // schedule_ids = [0];
226
+ // }
227
+ // }
228
228
  userPlugin = this.core.getPlugin('user');
229
229
  customer_id = undefined;
230
230
  try {
@@ -233,7 +233,7 @@ export var BookingByStepImpl = /*#__PURE__*/function (_BaseModule) {
233
233
  console.error(error);
234
234
  }
235
235
  // 如果没传schedule_date,则从
236
- _context2.next = 7;
236
+ _context2.next = 6;
237
237
  return this.request.post("/product/query", {
238
238
  open_quotation: 1,
239
239
  open_bundle: 0,
@@ -246,17 +246,17 @@ export var BookingByStepImpl = /*#__PURE__*/function (_BaseModule) {
246
246
  category_ids: category_ids,
247
247
  ids: product_ids,
248
248
  collection: collection,
249
- front_end_cache_id: this.cacheId,
250
- client_schedule_ids: schedule_ids,
251
- schedule_date: schedule_date
249
+ front_end_cache_id: this.cacheId
250
+ // client_schedule_ids: schedule_ids,
251
+ // schedule_date,
252
252
  }, {
253
253
  useCache: true
254
254
  });
255
- case 7:
255
+ case 6:
256
256
  productsData = _context2.sent;
257
257
  this.store.products.addProduct(productsData.data.list);
258
258
  return _context2.abrupt("return", productsData.data.list);
259
- case 10:
259
+ case 9:
260
260
  case "end":
261
261
  return _context2.stop();
262
262
  }
@@ -272,7 +272,7 @@ export var BookingByStepImpl = /*#__PURE__*/function (_BaseModule) {
272
272
  value: function () {
273
273
  var _loadProductByScheduleDate = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3(_ref2) {
274
274
  var _product_ids;
275
- var date, _ref2$product_ids, product_ids, _ref2$category_ids, category_ids, scheduleList, scheduleIds, _schedule$product_ids, schedule, otherProductsIds, allProductIds;
275
+ var date, _ref2$product_ids, product_ids, _ref2$category_ids, category_ids, scheduleList, _schedule$product_ids, schedule, otherProductsIds, allProductIds;
276
276
  return _regeneratorRuntime().wrap(function _callee3$(_context3) {
277
277
  while (1) switch (_context3.prev = _context3.next) {
278
278
  case 0:
@@ -289,13 +289,12 @@ export var BookingByStepImpl = /*#__PURE__*/function (_BaseModule) {
289
289
  week: '',
290
290
  weekNum: 0
291
291
  }]);
292
- scheduleIds = scheduleList.filter(function (n) {
293
- return n.date === date;
294
- }).flatMap(function (n) {
295
- return n.schedule_id;
296
- }).filter(function (n) {
297
- return n !== null && n !== undefined;
298
- }); // 如果外面没传 product_ids,尝试从对应 schedule 里拿 product_ids
292
+ // const scheduleIds = scheduleList
293
+ // .filter((n) => n.date === date)
294
+ // .flatMap((n) => n.schedule_id)
295
+ // .filter((n) => n !== null && n !== undefined);
296
+
297
+ // 如果外面没传 product_ids,尝试从对应 schedule 里拿 product_ids
299
298
  if (!((_product_ids = product_ids) !== null && _product_ids !== void 0 && _product_ids.length)) {
300
299
  schedule = scheduleList.find(function (n) {
301
300
  return n.date === date;
@@ -304,22 +303,21 @@ export var BookingByStepImpl = /*#__PURE__*/function (_BaseModule) {
304
303
  product_ids = schedule.product_ids;
305
304
  }
306
305
  }
307
-
308
306
  // 尝试去拿schedule 里的 other_product_ids 和 product_ids 拼在一起去重
309
307
  otherProductsIds = this.store.schedule.getOtherProductsIds() || [];
310
308
  allProductIds = [].concat(_toConsumableArray(product_ids), _toConsumableArray(otherProductsIds)).filter(function (n, index, self) {
311
309
  return self.indexOf(n) === index;
312
310
  });
313
- _context3.next = 9;
311
+ _context3.next = 8;
314
312
  return this.loadProducts({
315
- schedule_ids: scheduleIds,
313
+ // schedule_ids: scheduleIds,
316
314
  product_ids: allProductIds,
317
- category_ids: category_ids,
318
- schedule_date: date
315
+ category_ids: category_ids
316
+ // schedule_date: date,
319
317
  });
320
- case 9:
318
+ case 8:
321
319
  return _context3.abrupt("return", _context3.sent);
322
- case 10:
320
+ case 9:
323
321
  case "end":
324
322
  return _context3.stop();
325
323
  }
@@ -1820,6 +1818,7 @@ export var BookingByStepImpl = /*#__PURE__*/function (_BaseModule) {
1820
1818
  // .format("HH:mm");
1821
1819
  // 根据 start_time 和 end_time 去匹配资源
1822
1820
  var targetResource = null;
1821
+ var targetResourceTime = 0;
1823
1822
  var _iterator3 = _createForOfIteratorHelper(resources),
1824
1823
  _step3;
1825
1824
  try {
@@ -1862,8 +1861,14 @@ export var BookingByStepImpl = /*#__PURE__*/function (_BaseModule) {
1862
1861
  }
1863
1862
  return res.usable;
1864
1863
  });
1865
- if (canUseTime && !n.onlyComputed) {
1864
+ var currentResourceIdleTime = calculateResourceAvailableTime({
1865
+ resource: n,
1866
+ timeSlots: timeSlots,
1867
+ currentCapacity: totalCapacity + (capacity || 0)
1868
+ });
1869
+ if (canUseTime && !n.onlyComputed && currentResourceIdleTime > targetResourceTime) {
1866
1870
  targetResource = n;
1871
+ targetResourceTime = currentResourceIdleTime;
1867
1872
  return 1; // break
1868
1873
  }
1869
1874
  },
@@ -2061,7 +2066,12 @@ export var BookingByStepImpl = /*#__PURE__*/function (_BaseModule) {
2061
2066
  })) {
2062
2067
  return;
2063
2068
  }
2064
- var targetResource = targetRenderList[0];
2069
+ var fastestResource = findFastestAvailableResource({
2070
+ resources: targetRenderList,
2071
+ currentCapacity: currentCapacity,
2072
+ countMap: selectResourcesMap
2073
+ });
2074
+ var targetResource = fastestResource || targetRenderList[0];
2065
2075
  targetResource.capacity = currentCapacity;
2066
2076
  // 在这里处理 children 的数据
2067
2077
  checkSubResourcesCapacity(targetResource);
@@ -0,0 +1,25 @@
1
+ import { ResourceItem, TimeSliceItem } from "./resources";
2
+ /**
3
+ * 计算资源在指定时间段内的总可用时间(以分钟为单位)
4
+ * @param resource 需要计算可用时间的资源
5
+ * @param timeSlots 要检查可用性的时间段
6
+ * @param currentCapacity 当前预约所需的容量
7
+ * @returns 总可用时间(分钟)
8
+ */
9
+ export declare function calculateResourceAvailableTime({ resource, timeSlots, currentCapacity, }: {
10
+ resource: ResourceItem;
11
+ timeSlots: TimeSliceItem;
12
+ currentCapacity?: number;
13
+ }): number;
14
+ /**
15
+ * 查找最快可用的资源,如果有多个资源在相同时间点可用,则选择空闲时间最长的资源
16
+ * @param resources 资源列表
17
+ * @param currentCapacity 当前预约所需的容量
18
+ * @param countMap 已预约数量映射
19
+ * @returns 最快可用的资源
20
+ */
21
+ export declare function findFastestAvailableResource({ resources, currentCapacity, countMap, }: {
22
+ resources: ResourceItem[];
23
+ currentCapacity?: number;
24
+ countMap?: Record<number, number>;
25
+ }): ResourceItem | null;
@@ -0,0 +1,209 @@
1
+ function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
2
+ function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
3
+ function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
4
+ import dayjs from "dayjs";
5
+ /**
6
+ * 计算资源在指定时间段内的总可用时间(以分钟为单位)
7
+ * @param resource 需要计算可用时间的资源
8
+ * @param timeSlots 要检查可用性的时间段
9
+ * @param currentCapacity 当前预约所需的容量
10
+ * @returns 总可用时间(分钟)
11
+ */
12
+ export function calculateResourceAvailableTime(_ref) {
13
+ var resource = _ref.resource,
14
+ timeSlots = _ref.timeSlots,
15
+ _ref$currentCapacity = _ref.currentCapacity,
16
+ currentCapacity = _ref$currentCapacity === void 0 ? 1 : _ref$currentCapacity;
17
+ // 过滤出与给定日期相同的资源时间
18
+ var matchingTimes = resource.times.filter(function (time) {
19
+ return dayjs(time.start_at).isSame(dayjs(timeSlots.start_at), 'day');
20
+ });
21
+ if (matchingTimes.length === 0) return 0;
22
+
23
+ // 计算所有时间段与目标时间槽的重叠部分
24
+ var overlaps = [];
25
+ var _iterator = _createForOfIteratorHelper(matchingTimes),
26
+ _step;
27
+ try {
28
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
29
+ var time = _step.value;
30
+ // 计算实际重叠的时间段
31
+ var overlapStart = dayjs(time.start_at).isAfter(dayjs(timeSlots.start_at)) ? dayjs(time.start_at) : dayjs(timeSlots.start_at);
32
+ var overlapEnd = dayjs(time.end_at).isBefore(dayjs(timeSlots.end_at)) ? dayjs(time.end_at) : dayjs(timeSlots.end_at);
33
+
34
+ // 只有当重叠时间段有效时才添加
35
+ if (overlapStart.isBefore(overlapEnd)) {
36
+ overlaps.push({
37
+ start: overlapStart,
38
+ end: overlapEnd
39
+ });
40
+ }
41
+ }
42
+
43
+ // 合并重叠的时间段,计算并集
44
+ } catch (err) {
45
+ _iterator.e(err);
46
+ } finally {
47
+ _iterator.f();
48
+ }
49
+ if (overlaps.length === 0) return 0;
50
+
51
+ // 按开始时间排序
52
+ overlaps.sort(function (a, b) {
53
+ return a.start.diff(b.start);
54
+ });
55
+
56
+ // 合并重叠的时间段
57
+ var merged = [];
58
+ var current = overlaps[0];
59
+ for (var i = 1; i < overlaps.length; i++) {
60
+ var next = overlaps[i];
61
+
62
+ // 如果当前时间段与下一个时间段重叠或相邻,则合并
63
+ if (current.end.isSameOrAfter(next.start)) {
64
+ current.end = current.end.isAfter(next.end) ? current.end : next.end;
65
+ } else {
66
+ // 不重叠,添加当前时间段到结果中,开始处理下一个
67
+ merged.push(current);
68
+ current = next;
69
+ }
70
+ }
71
+ merged.push(current);
72
+
73
+ // 计算所有合并后时间段的总时长
74
+ var totalAvailableMinutes = 0;
75
+ for (var _i = 0, _merged = merged; _i < _merged.length; _i++) {
76
+ var segment = _merged[_i];
77
+ totalAvailableMinutes += segment.end.diff(segment.start, 'minute');
78
+ }
79
+ return totalAvailableMinutes;
80
+ }
81
+
82
+ /**
83
+ * 查找最快可用的资源,如果有多个资源在相同时间点可用,则选择空闲时间最长的资源
84
+ * @param resources 资源列表
85
+ * @param currentCapacity 当前预约所需的容量
86
+ * @param countMap 已预约数量映射
87
+ * @returns 最快可用的资源
88
+ */
89
+ export function findFastestAvailableResource(_ref2) {
90
+ var resources = _ref2.resources,
91
+ _ref2$currentCapacity = _ref2.currentCapacity,
92
+ currentCapacity = _ref2$currentCapacity === void 0 ? 1 : _ref2$currentCapacity,
93
+ _ref2$countMap = _ref2.countMap,
94
+ countMap = _ref2$countMap === void 0 ? {} : _ref2$countMap;
95
+ var currentTime = dayjs();
96
+ var fastestTime = null;
97
+ var fastestResources = [];
98
+
99
+ // 遍历所有资源,找到最快可用的时间点
100
+ var _iterator2 = _createForOfIteratorHelper(resources),
101
+ _step2;
102
+ try {
103
+ for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
104
+ var _resource = _step2.value;
105
+ // 获取资源当天的时间段
106
+ var todayTimes = _resource.times.filter(function (time) {
107
+ return dayjs(time.start_at).isSame(currentTime, 'day');
108
+ });
109
+ if (todayTimes.length === 0) continue;
110
+
111
+ // 按开始时间排序
112
+ todayTimes.sort(function (a, b) {
113
+ return dayjs(a.start_at).diff(dayjs(b.start_at));
114
+ });
115
+
116
+ // 找到第一个可用的时间段
117
+ var _iterator3 = _createForOfIteratorHelper(todayTimes),
118
+ _step3;
119
+ try {
120
+ var _loop = function _loop() {
121
+ var time = _step3.value;
122
+ var startTime = dayjs(time.start_at);
123
+
124
+ // 如果开始时间在当前时间之前,跳过
125
+ if (startTime.isBefore(currentTime)) return 0; // continue
126
+
127
+ // 检查这个时间段是否可用
128
+ if (_resource.resourceType === 'single') {
129
+ var _time$event_list;
130
+ // 单个预约类型:检查是否有预约
131
+ var hasBooking = (_time$event_list = time.event_list) === null || _time$event_list === void 0 ? void 0 : _time$event_list.some(function (event) {
132
+ return dayjs(event.start_at).isSame(startTime);
133
+ });
134
+ if (!hasBooking) {
135
+ if (!fastestTime || startTime.isSame(fastestTime)) {
136
+ fastestTime = startTime;
137
+ fastestResources.push(_resource);
138
+ } else if (startTime.isBefore(fastestTime)) {
139
+ fastestTime = startTime;
140
+ fastestResources = [_resource];
141
+ }
142
+ return 1; // break
143
+ }
144
+ } else {
145
+ var _time$event_list2;
146
+ // 多个预约类型:检查容量
147
+ var totalCapacity = _resource.capacity || 0;
148
+ var usedCapacity = ((_time$event_list2 = time.event_list) === null || _time$event_list2 === void 0 ? void 0 : _time$event_list2.reduce(function (sum, event) {
149
+ return dayjs(event.start_at).isSame(startTime) ? sum + (event.pax || 0) : sum;
150
+ }, 0)) || 0;
151
+ var remainingCapacity = totalCapacity - usedCapacity;
152
+ if (remainingCapacity >= (countMap[_resource.id] || 0) + currentCapacity) {
153
+ if (!fastestTime || startTime.isSame(fastestTime)) {
154
+ fastestTime = startTime;
155
+ fastestResources.push(_resource);
156
+ } else if (startTime.isBefore(fastestTime)) {
157
+ fastestTime = startTime;
158
+ fastestResources = [_resource];
159
+ }
160
+ return 1; // break
161
+ }
162
+ }
163
+ },
164
+ _ret;
165
+ for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
166
+ _ret = _loop();
167
+ if (_ret === 0) continue;
168
+ if (_ret === 1) break;
169
+ }
170
+ } catch (err) {
171
+ _iterator3.e(err);
172
+ } finally {
173
+ _iterator3.f();
174
+ }
175
+ }
176
+
177
+ // 如果没有找到可用资源,返回null
178
+ } catch (err) {
179
+ _iterator2.e(err);
180
+ } finally {
181
+ _iterator2.f();
182
+ }
183
+ if (!fastestTime || fastestResources.length === 0) return null;
184
+
185
+ // 如果只有一个最快可用的资源,直接返回
186
+ if (fastestResources.length === 1) return fastestResources[0];
187
+
188
+ // 如果有多个最快可用的资源,比较它们的空闲时间
189
+ var maxIdleTime = 0;
190
+ var selectedResource = null;
191
+ for (var _i2 = 0, _fastestResources = fastestResources; _i2 < _fastestResources.length; _i2++) {
192
+ var resource = _fastestResources[_i2];
193
+ var idleTime = calculateResourceAvailableTime({
194
+ resource: resource,
195
+ timeSlots: {
196
+ start_time: fastestTime.format('HH:mm'),
197
+ end_time: fastestTime.format('HH:mm'),
198
+ start_at: fastestTime,
199
+ end_at: fastestTime
200
+ },
201
+ currentCapacity: (countMap[resource.id] || 0) + currentCapacity
202
+ });
203
+ if (idleTime > maxIdleTime) {
204
+ maxIdleTime = idleTime;
205
+ selectedResource = resource;
206
+ }
207
+ }
208
+ return selectedResource;
209
+ }
@@ -42,12 +42,10 @@ export declare class BookingByStepImpl extends BaseModule implements Module {
42
42
  * 更新step
43
43
  */
44
44
  updateStep(key: string, step: IStep): void;
45
- loadProducts({ category_ids, product_ids, collection, schedule_ids, schedule_date, }: {
45
+ loadProducts({ category_ids, product_ids, collection, }: {
46
46
  category_ids?: number[];
47
47
  product_ids?: number[];
48
48
  collection?: number | string[];
49
- schedule_ids?: number[];
50
- schedule_date?: string;
51
49
  }): Promise<any>;
52
50
  loadProductByScheduleDate({ date, product_ids, category_ids, }: {
53
51
  date: string;
@@ -43,6 +43,7 @@ var import_utils = require("../../modules/Resource/utils");
43
43
  var import_lodash_es = require("lodash-es");
44
44
  var import_utils2 = require("../../modules/Schedule/utils");
45
45
  var import_utils3 = require("../../modules/Date/utils");
46
+ var import_timeslots = require("./utils/timeslots");
46
47
  import_dayjs.default.extend(import_isSameOrBefore.default);
47
48
  import_dayjs.default.extend(import_isSameOrAfter.default);
48
49
  var BookingByStepImpl = class extends import_BaseModule.BaseModule {
@@ -161,19 +162,11 @@ var BookingByStepImpl = class extends import_BaseModule.BaseModule {
161
162
  async loadProducts({
162
163
  category_ids = [],
163
164
  product_ids = [],
164
- collection = [],
165
- schedule_ids = [],
166
- schedule_date
165
+ collection = []
166
+ // schedule_ids = [],
167
+ // schedule_date,
167
168
  }) {
168
169
  var _a;
169
- if (!(schedule_ids == null ? void 0 : schedule_ids.length)) {
170
- const schedule_ids_data = this.store.schedule.getScheduleListByIds(schedule_ids).map((n) => n.id);
171
- if (schedule_ids_data.length) {
172
- schedule_ids = schedule_ids_data;
173
- } else if (schedule_date) {
174
- schedule_ids = [0];
175
- }
176
- }
177
170
  let userPlugin = this.core.getPlugin("user");
178
171
  let customer_id = void 0;
179
172
  try {
@@ -201,9 +194,9 @@ var BookingByStepImpl = class extends import_BaseModule.BaseModule {
201
194
  category_ids,
202
195
  ids: product_ids,
203
196
  collection,
204
- front_end_cache_id: this.cacheId,
205
- client_schedule_ids: schedule_ids,
206
- schedule_date
197
+ front_end_cache_id: this.cacheId
198
+ // client_schedule_ids: schedule_ids,
199
+ // schedule_date,
207
200
  },
208
201
  { useCache: true }
209
202
  );
@@ -221,7 +214,6 @@ var BookingByStepImpl = class extends import_BaseModule.BaseModule {
221
214
  { date, status: "available", week: "", weekNum: 0 },
222
215
  { date, status: "available", week: "", weekNum: 0 }
223
216
  ]);
224
- const scheduleIds = scheduleList.filter((n) => n.date === date).flatMap((n) => n.schedule_id).filter((n) => n !== null && n !== void 0);
225
217
  if (!(product_ids == null ? void 0 : product_ids.length)) {
226
218
  const schedule = scheduleList.find((n) => n.date === date);
227
219
  if (schedule && ((_a = schedule.product_ids) == null ? void 0 : _a.length)) {
@@ -229,12 +221,14 @@ var BookingByStepImpl = class extends import_BaseModule.BaseModule {
229
221
  }
230
222
  }
231
223
  const otherProductsIds = this.store.schedule.getOtherProductsIds() || [];
232
- const allProductIds = [...product_ids, ...otherProductsIds].filter((n, index, self) => self.indexOf(n) === index);
224
+ const allProductIds = [...product_ids, ...otherProductsIds].filter(
225
+ (n, index, self) => self.indexOf(n) === index
226
+ );
233
227
  return await this.loadProducts({
234
- schedule_ids: scheduleIds,
228
+ // schedule_ids: scheduleIds,
235
229
  product_ids: allProductIds,
236
- category_ids,
237
- schedule_date: date
230
+ category_ids
231
+ // schedule_date: date,
238
232
  });
239
233
  }
240
234
  async updateQuotationPriceAndCart(date) {
@@ -1179,6 +1173,7 @@ var BookingByStepImpl = class extends import_BaseModule.BaseModule {
1179
1173
  }, {});
1180
1174
  if (timeSlots) {
1181
1175
  let targetResource = null;
1176
+ let targetResourceTime = 0;
1182
1177
  for (const n of resources) {
1183
1178
  const mTimes = n.times.filter((n2) => {
1184
1179
  return !(0, import_dayjs.default)(n2.start_at).isAfter((0, import_dayjs.default)(timeSlots.start_at)) && !(0, import_dayjs.default)(n2.end_at).isBefore((0, import_dayjs.default)(timeSlots.end_at));
@@ -1208,8 +1203,14 @@ var BookingByStepImpl = class extends import_BaseModule.BaseModule {
1208
1203
  }
1209
1204
  return res.usable;
1210
1205
  });
1211
- if (canUseTime && !n.onlyComputed) {
1206
+ const currentResourceIdleTime = (0, import_timeslots.calculateResourceAvailableTime)({
1207
+ resource: n,
1208
+ timeSlots,
1209
+ currentCapacity: totalCapacity + (capacity || 0)
1210
+ });
1211
+ if (canUseTime && !n.onlyComputed && currentResourceIdleTime > targetResourceTime) {
1212
1212
  targetResource = n;
1213
+ targetResourceTime = currentResourceIdleTime;
1213
1214
  break;
1214
1215
  }
1215
1216
  }
@@ -1391,7 +1392,12 @@ var BookingByStepImpl = class extends import_BaseModule.BaseModule {
1391
1392
  )) {
1392
1393
  return;
1393
1394
  }
1394
- const targetResource = targetRenderList[0];
1395
+ const fastestResource = (0, import_timeslots.findFastestAvailableResource)({
1396
+ resources: targetRenderList,
1397
+ currentCapacity,
1398
+ countMap: selectResourcesMap
1399
+ });
1400
+ const targetResource = fastestResource || targetRenderList[0];
1395
1401
  targetResource.capacity = currentCapacity;
1396
1402
  (0, import_resources.checkSubResourcesCapacity)(targetResource);
1397
1403
  if (!selectResourcesMap[targetResource.id]) {
@@ -0,0 +1,25 @@
1
+ import { ResourceItem, TimeSliceItem } from "./resources";
2
+ /**
3
+ * 计算资源在指定时间段内的总可用时间(以分钟为单位)
4
+ * @param resource 需要计算可用时间的资源
5
+ * @param timeSlots 要检查可用性的时间段
6
+ * @param currentCapacity 当前预约所需的容量
7
+ * @returns 总可用时间(分钟)
8
+ */
9
+ export declare function calculateResourceAvailableTime({ resource, timeSlots, currentCapacity, }: {
10
+ resource: ResourceItem;
11
+ timeSlots: TimeSliceItem;
12
+ currentCapacity?: number;
13
+ }): number;
14
+ /**
15
+ * 查找最快可用的资源,如果有多个资源在相同时间点可用,则选择空闲时间最长的资源
16
+ * @param resources 资源列表
17
+ * @param currentCapacity 当前预约所需的容量
18
+ * @param countMap 已预约数量映射
19
+ * @returns 最快可用的资源
20
+ */
21
+ export declare function findFastestAvailableResource({ resources, currentCapacity, countMap, }: {
22
+ resources: ResourceItem[];
23
+ currentCapacity?: number;
24
+ countMap?: Record<number, number>;
25
+ }): ResourceItem | null;
@@ -0,0 +1,159 @@
1
+ var __create = Object.create;
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getProtoOf = Object.getPrototypeOf;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, { get: all[name], enumerable: true });
10
+ };
11
+ var __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from === "object" || typeof from === "function") {
13
+ for (let key of __getOwnPropNames(from))
14
+ if (!__hasOwnProp.call(to, key) && key !== except)
15
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
+ }
17
+ return to;
18
+ };
19
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
20
+ // If the importer is in node compatibility mode or this is not an ESM
21
+ // file that has been converted to a CommonJS file using a Babel-
22
+ // compatible transform (i.e. "__esModule" has not been set), then set
23
+ // "default" to the CommonJS "module.exports" for node compatibility.
24
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
25
+ mod
26
+ ));
27
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
28
+
29
+ // src/solution/BookingByStep/utils/timeslots.ts
30
+ var timeslots_exports = {};
31
+ __export(timeslots_exports, {
32
+ calculateResourceAvailableTime: () => calculateResourceAvailableTime,
33
+ findFastestAvailableResource: () => findFastestAvailableResource
34
+ });
35
+ module.exports = __toCommonJS(timeslots_exports);
36
+ var import_dayjs = __toESM(require("dayjs"));
37
+ function calculateResourceAvailableTime({
38
+ resource,
39
+ timeSlots,
40
+ currentCapacity = 1
41
+ }) {
42
+ const matchingTimes = resource.times.filter((time) => {
43
+ return (0, import_dayjs.default)(time.start_at).isSame((0, import_dayjs.default)(timeSlots.start_at), "day");
44
+ });
45
+ if (matchingTimes.length === 0)
46
+ return 0;
47
+ const overlaps = [];
48
+ for (const time of matchingTimes) {
49
+ const overlapStart = (0, import_dayjs.default)(time.start_at).isAfter((0, import_dayjs.default)(timeSlots.start_at)) ? (0, import_dayjs.default)(time.start_at) : (0, import_dayjs.default)(timeSlots.start_at);
50
+ const overlapEnd = (0, import_dayjs.default)(time.end_at).isBefore((0, import_dayjs.default)(timeSlots.end_at)) ? (0, import_dayjs.default)(time.end_at) : (0, import_dayjs.default)(timeSlots.end_at);
51
+ if (overlapStart.isBefore(overlapEnd)) {
52
+ overlaps.push({ start: overlapStart, end: overlapEnd });
53
+ }
54
+ }
55
+ if (overlaps.length === 0)
56
+ return 0;
57
+ overlaps.sort((a, b) => a.start.diff(b.start));
58
+ const merged = [];
59
+ let current = overlaps[0];
60
+ for (let i = 1; i < overlaps.length; i++) {
61
+ const next = overlaps[i];
62
+ if (current.end.isSameOrAfter(next.start)) {
63
+ current.end = current.end.isAfter(next.end) ? current.end : next.end;
64
+ } else {
65
+ merged.push(current);
66
+ current = next;
67
+ }
68
+ }
69
+ merged.push(current);
70
+ let totalAvailableMinutes = 0;
71
+ for (const segment of merged) {
72
+ totalAvailableMinutes += segment.end.diff(segment.start, "minute");
73
+ }
74
+ return totalAvailableMinutes;
75
+ }
76
+ function findFastestAvailableResource({
77
+ resources,
78
+ currentCapacity = 1,
79
+ countMap = {}
80
+ }) {
81
+ var _a, _b;
82
+ const currentTime = (0, import_dayjs.default)();
83
+ let fastestTime = null;
84
+ let fastestResources = [];
85
+ for (const resource of resources) {
86
+ const todayTimes = resource.times.filter((time) => {
87
+ return (0, import_dayjs.default)(time.start_at).isSame(currentTime, "day");
88
+ });
89
+ if (todayTimes.length === 0)
90
+ continue;
91
+ todayTimes.sort((a, b) => {
92
+ return (0, import_dayjs.default)(a.start_at).diff((0, import_dayjs.default)(b.start_at));
93
+ });
94
+ for (const time of todayTimes) {
95
+ const startTime = (0, import_dayjs.default)(time.start_at);
96
+ if (startTime.isBefore(currentTime))
97
+ continue;
98
+ if (resource.resourceType === "single") {
99
+ const hasBooking = (_a = time.event_list) == null ? void 0 : _a.some((event) => {
100
+ return (0, import_dayjs.default)(event.start_at).isSame(startTime);
101
+ });
102
+ if (!hasBooking) {
103
+ if (!fastestTime || startTime.isSame(fastestTime)) {
104
+ fastestTime = startTime;
105
+ fastestResources.push(resource);
106
+ } else if (startTime.isBefore(fastestTime)) {
107
+ fastestTime = startTime;
108
+ fastestResources = [resource];
109
+ }
110
+ break;
111
+ }
112
+ } else {
113
+ const totalCapacity = resource.capacity || 0;
114
+ const usedCapacity = ((_b = time.event_list) == null ? void 0 : _b.reduce((sum, event) => {
115
+ return (0, import_dayjs.default)(event.start_at).isSame(startTime) ? sum + (event.pax || 0) : sum;
116
+ }, 0)) || 0;
117
+ const remainingCapacity = totalCapacity - usedCapacity;
118
+ if (remainingCapacity >= (countMap[resource.id] || 0) + currentCapacity) {
119
+ if (!fastestTime || startTime.isSame(fastestTime)) {
120
+ fastestTime = startTime;
121
+ fastestResources.push(resource);
122
+ } else if (startTime.isBefore(fastestTime)) {
123
+ fastestTime = startTime;
124
+ fastestResources = [resource];
125
+ }
126
+ break;
127
+ }
128
+ }
129
+ }
130
+ }
131
+ if (!fastestTime || fastestResources.length === 0)
132
+ return null;
133
+ if (fastestResources.length === 1)
134
+ return fastestResources[0];
135
+ let maxIdleTime = 0;
136
+ let selectedResource = null;
137
+ for (const resource of fastestResources) {
138
+ const idleTime = calculateResourceAvailableTime({
139
+ resource,
140
+ timeSlots: {
141
+ start_time: fastestTime.format("HH:mm"),
142
+ end_time: fastestTime.format("HH:mm"),
143
+ start_at: fastestTime,
144
+ end_at: fastestTime
145
+ },
146
+ currentCapacity: (countMap[resource.id] || 0) + currentCapacity
147
+ });
148
+ if (idleTime > maxIdleTime) {
149
+ maxIdleTime = idleTime;
150
+ selectedResource = resource;
151
+ }
152
+ }
153
+ return selectedResource;
154
+ }
155
+ // Annotate the CommonJS export names for ESM import in node:
156
+ 0 && (module.exports = {
157
+ calculateResourceAvailableTime,
158
+ findFastestAvailableResource
159
+ });
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "private": false,
3
3
  "name": "@pisell/pisellos",
4
- "version": "0.0.114",
4
+ "version": "0.0.116",
5
5
  "description": "一个可扩展的前端模块化SDK框架,支持插件系统",
6
6
  "main": "dist/index.js",
7
7
  "types": "dist/index.d.ts",