@pisell/pisellos 2.0.25 → 2.0.26

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.
@@ -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);
@@ -1806,6 +1807,7 @@ export var BookingByStepImpl = /*#__PURE__*/function (_BaseModule) {
1806
1807
  // .format("HH:mm");
1807
1808
  // 根据 start_time 和 end_time 去匹配资源
1808
1809
  var targetResource = null;
1810
+ var targetResourceTime = 0;
1809
1811
  var _iterator3 = _createForOfIteratorHelper(resources),
1810
1812
  _step3;
1811
1813
  try {
@@ -1848,8 +1850,14 @@ export var BookingByStepImpl = /*#__PURE__*/function (_BaseModule) {
1848
1850
  }
1849
1851
  return res.usable;
1850
1852
  });
1851
- if (canUseTime && !n.onlyComputed) {
1853
+ var currentResourceIdleTime = calculateResourceAvailableTime({
1854
+ resource: n,
1855
+ timeSlots: timeSlots,
1856
+ currentCapacity: totalCapacity + (capacity || 0)
1857
+ });
1858
+ if (canUseTime && !n.onlyComputed && currentResourceIdleTime > targetResourceTime) {
1852
1859
  targetResource = n;
1860
+ targetResourceTime = currentResourceIdleTime;
1853
1861
  return 1; // break
1854
1862
  }
1855
1863
  },
@@ -2047,7 +2055,12 @@ export var BookingByStepImpl = /*#__PURE__*/function (_BaseModule) {
2047
2055
  })) {
2048
2056
  return;
2049
2057
  }
2050
- var targetResource = targetRenderList[0];
2058
+ var fastestResource = findFastestAvailableResource({
2059
+ resources: targetRenderList,
2060
+ currentCapacity: currentCapacity,
2061
+ countMap: selectResourcesMap
2062
+ });
2063
+ var targetResource = fastestResource || targetRenderList[0];
2051
2064
  targetResource.capacity = currentCapacity;
2052
2065
  // 在这里处理 children 的数据
2053
2066
  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,225 @@
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
+ var totalAvailableMinutes = 0;
23
+ var _iterator = _createForOfIteratorHelper(matchingTimes),
24
+ _step;
25
+ try {
26
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
27
+ var _time$event_list;
28
+ var time = _step.value;
29
+ // 计算实际重叠的时间段
30
+ var overlapStart = dayjs(time.start_at).isAfter(dayjs(timeSlots.start_at)) ? dayjs(time.start_at) : dayjs(timeSlots.start_at);
31
+ var overlapEnd = dayjs(time.end_at).isBefore(dayjs(timeSlots.end_at)) ? dayjs(time.end_at) : dayjs(timeSlots.end_at);
32
+
33
+ // 如果没有预约事件,整个重叠时间段都是可用的
34
+ if (!((_time$event_list = time.event_list) !== null && _time$event_list !== void 0 && _time$event_list.length)) {
35
+ totalAvailableMinutes += overlapEnd.diff(overlapStart, 'minute');
36
+ continue;
37
+ }
38
+
39
+ // 处理时间段内的每个预约事件
40
+ var currentTime = overlapStart;
41
+ var _iterator2 = _createForOfIteratorHelper(time.event_list),
42
+ _step2;
43
+ try {
44
+ for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
45
+ var event = _step2.value;
46
+ var eventStart = dayjs(event.start_at);
47
+ var eventEnd = dayjs(event.end_at);
48
+
49
+ // 如果事件在当前时间之前,跳过
50
+ if (eventEnd.isBefore(currentTime)) continue;
51
+
52
+ // 如果事件开始时间在重叠结束时间之后,可以添加剩余时间
53
+ if (eventStart.isAfter(overlapEnd)) {
54
+ totalAvailableMinutes += overlapEnd.diff(currentTime, 'minute');
55
+ break;
56
+ }
57
+
58
+ // 如果事件开始时间在当前时间之后,添加事件开始前的时间
59
+ if (eventStart.isAfter(currentTime)) {
60
+ totalAvailableMinutes += eventStart.diff(currentTime, 'minute');
61
+ }
62
+
63
+ // 对于单个预约类型,整个事件时间都是不可用的
64
+ if (resource.resourceType === 'single') {
65
+ currentTime = eventEnd;
66
+ continue;
67
+ }
68
+
69
+ // 对于多个预约类型,检查容量
70
+ if (resource.resourceType === 'multiple') {
71
+ var remainingCapacity = (resource.capacity || 0) - (event.pax || 0);
72
+ if (remainingCapacity >= currentCapacity) {
73
+ // 如果剩余容量足够,这个时间段仍然是可用的
74
+ totalAvailableMinutes += eventEnd.diff(eventStart, 'minute');
75
+ }
76
+ currentTime = eventEnd;
77
+ }
78
+ }
79
+
80
+ // 添加最后一个事件之后的剩余时间
81
+ } catch (err) {
82
+ _iterator2.e(err);
83
+ } finally {
84
+ _iterator2.f();
85
+ }
86
+ if (currentTime.isBefore(overlapEnd)) {
87
+ totalAvailableMinutes += overlapEnd.diff(currentTime, 'minute');
88
+ }
89
+ }
90
+ } catch (err) {
91
+ _iterator.e(err);
92
+ } finally {
93
+ _iterator.f();
94
+ }
95
+ return totalAvailableMinutes;
96
+ }
97
+
98
+ /**
99
+ * 查找最快可用的资源,如果有多个资源在相同时间点可用,则选择空闲时间最长的资源
100
+ * @param resources 资源列表
101
+ * @param currentCapacity 当前预约所需的容量
102
+ * @param countMap 已预约数量映射
103
+ * @returns 最快可用的资源
104
+ */
105
+ export function findFastestAvailableResource(_ref2) {
106
+ var resources = _ref2.resources,
107
+ _ref2$currentCapacity = _ref2.currentCapacity,
108
+ currentCapacity = _ref2$currentCapacity === void 0 ? 1 : _ref2$currentCapacity,
109
+ _ref2$countMap = _ref2.countMap,
110
+ countMap = _ref2$countMap === void 0 ? {} : _ref2$countMap;
111
+ var currentTime = dayjs();
112
+ var fastestTime = null;
113
+ var fastestResources = [];
114
+
115
+ // 遍历所有资源,找到最快可用的时间点
116
+ var _iterator3 = _createForOfIteratorHelper(resources),
117
+ _step3;
118
+ try {
119
+ for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
120
+ var _resource = _step3.value;
121
+ // 获取资源当天的时间段
122
+ var todayTimes = _resource.times.filter(function (time) {
123
+ return dayjs(time.start_at).isSame(currentTime, 'day');
124
+ });
125
+ if (todayTimes.length === 0) continue;
126
+
127
+ // 按开始时间排序
128
+ todayTimes.sort(function (a, b) {
129
+ return dayjs(a.start_at).diff(dayjs(b.start_at));
130
+ });
131
+
132
+ // 找到第一个可用的时间段
133
+ var _iterator4 = _createForOfIteratorHelper(todayTimes),
134
+ _step4;
135
+ try {
136
+ var _loop = function _loop() {
137
+ var time = _step4.value;
138
+ var startTime = dayjs(time.start_at);
139
+
140
+ // 如果开始时间在当前时间之前,跳过
141
+ if (startTime.isBefore(currentTime)) return 0; // continue
142
+
143
+ // 检查这个时间段是否可用
144
+ if (_resource.resourceType === 'single') {
145
+ var _time$event_list2;
146
+ // 单个预约类型:检查是否有预约
147
+ var hasBooking = (_time$event_list2 = time.event_list) === null || _time$event_list2 === void 0 ? void 0 : _time$event_list2.some(function (event) {
148
+ return dayjs(event.start_at).isSame(startTime);
149
+ });
150
+ if (!hasBooking) {
151
+ if (!fastestTime || startTime.isSame(fastestTime)) {
152
+ fastestTime = startTime;
153
+ fastestResources.push(_resource);
154
+ } else if (startTime.isBefore(fastestTime)) {
155
+ fastestTime = startTime;
156
+ fastestResources = [_resource];
157
+ }
158
+ return 1; // break
159
+ }
160
+ } else {
161
+ var _time$event_list3;
162
+ // 多个预约类型:检查容量
163
+ var totalCapacity = _resource.capacity || 0;
164
+ var usedCapacity = ((_time$event_list3 = time.event_list) === null || _time$event_list3 === void 0 ? void 0 : _time$event_list3.reduce(function (sum, event) {
165
+ return dayjs(event.start_at).isSame(startTime) ? sum + (event.pax || 0) : sum;
166
+ }, 0)) || 0;
167
+ var remainingCapacity = totalCapacity - usedCapacity;
168
+ if (remainingCapacity >= (countMap[_resource.id] || 0) + currentCapacity) {
169
+ if (!fastestTime || startTime.isSame(fastestTime)) {
170
+ fastestTime = startTime;
171
+ fastestResources.push(_resource);
172
+ } else if (startTime.isBefore(fastestTime)) {
173
+ fastestTime = startTime;
174
+ fastestResources = [_resource];
175
+ }
176
+ return 1; // break
177
+ }
178
+ }
179
+ },
180
+ _ret;
181
+ for (_iterator4.s(); !(_step4 = _iterator4.n()).done;) {
182
+ _ret = _loop();
183
+ if (_ret === 0) continue;
184
+ if (_ret === 1) break;
185
+ }
186
+ } catch (err) {
187
+ _iterator4.e(err);
188
+ } finally {
189
+ _iterator4.f();
190
+ }
191
+ }
192
+
193
+ // 如果没有找到可用资源,返回null
194
+ } catch (err) {
195
+ _iterator3.e(err);
196
+ } finally {
197
+ _iterator3.f();
198
+ }
199
+ if (!fastestTime || fastestResources.length === 0) return null;
200
+
201
+ // 如果只有一个最快可用的资源,直接返回
202
+ if (fastestResources.length === 1) return fastestResources[0];
203
+
204
+ // 如果有多个最快可用的资源,比较它们的空闲时间
205
+ var maxIdleTime = 0;
206
+ var selectedResource = null;
207
+ for (var _i = 0, _fastestResources = fastestResources; _i < _fastestResources.length; _i++) {
208
+ var resource = _fastestResources[_i];
209
+ var idleTime = calculateResourceAvailableTime({
210
+ resource: resource,
211
+ timeSlots: {
212
+ start_time: fastestTime.format('HH:mm'),
213
+ end_time: fastestTime.format('HH:mm'),
214
+ start_at: fastestTime,
215
+ end_at: fastestTime
216
+ },
217
+ currentCapacity: (countMap[resource.id] || 0) + currentCapacity
218
+ });
219
+ if (idleTime > maxIdleTime) {
220
+ maxIdleTime = idleTime;
221
+ selectedResource = resource;
222
+ }
223
+ }
224
+ return selectedResource;
225
+ }
@@ -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 {
@@ -1170,6 +1171,7 @@ var BookingByStepImpl = class extends import_BaseModule.BaseModule {
1170
1171
  }, {});
1171
1172
  if (timeSlots) {
1172
1173
  let targetResource = null;
1174
+ let targetResourceTime = 0;
1173
1175
  for (const n of resources) {
1174
1176
  const mTimes = n.times.filter((n2) => {
1175
1177
  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));
@@ -1199,8 +1201,14 @@ var BookingByStepImpl = class extends import_BaseModule.BaseModule {
1199
1201
  }
1200
1202
  return res.usable;
1201
1203
  });
1202
- if (canUseTime && !n.onlyComputed) {
1204
+ const currentResourceIdleTime = (0, import_timeslots.calculateResourceAvailableTime)({
1205
+ resource: n,
1206
+ timeSlots,
1207
+ currentCapacity: totalCapacity + (capacity || 0)
1208
+ });
1209
+ if (canUseTime && !n.onlyComputed && currentResourceIdleTime > targetResourceTime) {
1203
1210
  targetResource = n;
1211
+ targetResourceTime = currentResourceIdleTime;
1204
1212
  break;
1205
1213
  }
1206
1214
  }
@@ -1382,7 +1390,12 @@ var BookingByStepImpl = class extends import_BaseModule.BaseModule {
1382
1390
  )) {
1383
1391
  return;
1384
1392
  }
1385
- const targetResource = targetRenderList[0];
1393
+ const fastestResource = (0, import_timeslots.findFastestAvailableResource)({
1394
+ resources: targetRenderList,
1395
+ currentCapacity,
1396
+ countMap: selectResourcesMap
1397
+ });
1398
+ const targetResource = fastestResource || targetRenderList[0];
1386
1399
  targetResource.capacity = currentCapacity;
1387
1400
  (0, import_resources.checkSubResourcesCapacity)(targetResource);
1388
1401
  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,170 @@
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
+ var _a;
43
+ const matchingTimes = resource.times.filter((time) => {
44
+ return (0, import_dayjs.default)(time.start_at).isSame((0, import_dayjs.default)(timeSlots.start_at), "day");
45
+ });
46
+ if (matchingTimes.length === 0)
47
+ return 0;
48
+ let totalAvailableMinutes = 0;
49
+ for (const time of matchingTimes) {
50
+ 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);
51
+ 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);
52
+ if (!((_a = time.event_list) == null ? void 0 : _a.length)) {
53
+ totalAvailableMinutes += overlapEnd.diff(overlapStart, "minute");
54
+ continue;
55
+ }
56
+ let currentTime = overlapStart;
57
+ for (const event of time.event_list) {
58
+ const eventStart = (0, import_dayjs.default)(event.start_at);
59
+ const eventEnd = (0, import_dayjs.default)(event.end_at);
60
+ if (eventEnd.isBefore(currentTime))
61
+ continue;
62
+ if (eventStart.isAfter(overlapEnd)) {
63
+ totalAvailableMinutes += overlapEnd.diff(currentTime, "minute");
64
+ break;
65
+ }
66
+ if (eventStart.isAfter(currentTime)) {
67
+ totalAvailableMinutes += eventStart.diff(currentTime, "minute");
68
+ }
69
+ if (resource.resourceType === "single") {
70
+ currentTime = eventEnd;
71
+ continue;
72
+ }
73
+ if (resource.resourceType === "multiple") {
74
+ const remainingCapacity = (resource.capacity || 0) - (event.pax || 0);
75
+ if (remainingCapacity >= currentCapacity) {
76
+ totalAvailableMinutes += eventEnd.diff(eventStart, "minute");
77
+ }
78
+ currentTime = eventEnd;
79
+ }
80
+ }
81
+ if (currentTime.isBefore(overlapEnd)) {
82
+ totalAvailableMinutes += overlapEnd.diff(currentTime, "minute");
83
+ }
84
+ }
85
+ return totalAvailableMinutes;
86
+ }
87
+ function findFastestAvailableResource({
88
+ resources,
89
+ currentCapacity = 1,
90
+ countMap = {}
91
+ }) {
92
+ var _a, _b;
93
+ const currentTime = (0, import_dayjs.default)();
94
+ let fastestTime = null;
95
+ let fastestResources = [];
96
+ for (const resource of resources) {
97
+ const todayTimes = resource.times.filter((time) => {
98
+ return (0, import_dayjs.default)(time.start_at).isSame(currentTime, "day");
99
+ });
100
+ if (todayTimes.length === 0)
101
+ continue;
102
+ todayTimes.sort((a, b) => {
103
+ return (0, import_dayjs.default)(a.start_at).diff((0, import_dayjs.default)(b.start_at));
104
+ });
105
+ for (const time of todayTimes) {
106
+ const startTime = (0, import_dayjs.default)(time.start_at);
107
+ if (startTime.isBefore(currentTime))
108
+ continue;
109
+ if (resource.resourceType === "single") {
110
+ const hasBooking = (_a = time.event_list) == null ? void 0 : _a.some((event) => {
111
+ return (0, import_dayjs.default)(event.start_at).isSame(startTime);
112
+ });
113
+ if (!hasBooking) {
114
+ if (!fastestTime || startTime.isSame(fastestTime)) {
115
+ fastestTime = startTime;
116
+ fastestResources.push(resource);
117
+ } else if (startTime.isBefore(fastestTime)) {
118
+ fastestTime = startTime;
119
+ fastestResources = [resource];
120
+ }
121
+ break;
122
+ }
123
+ } else {
124
+ const totalCapacity = resource.capacity || 0;
125
+ const usedCapacity = ((_b = time.event_list) == null ? void 0 : _b.reduce((sum, event) => {
126
+ return (0, import_dayjs.default)(event.start_at).isSame(startTime) ? sum + (event.pax || 0) : sum;
127
+ }, 0)) || 0;
128
+ const remainingCapacity = totalCapacity - usedCapacity;
129
+ if (remainingCapacity >= (countMap[resource.id] || 0) + currentCapacity) {
130
+ if (!fastestTime || startTime.isSame(fastestTime)) {
131
+ fastestTime = startTime;
132
+ fastestResources.push(resource);
133
+ } else if (startTime.isBefore(fastestTime)) {
134
+ fastestTime = startTime;
135
+ fastestResources = [resource];
136
+ }
137
+ break;
138
+ }
139
+ }
140
+ }
141
+ }
142
+ if (!fastestTime || fastestResources.length === 0)
143
+ return null;
144
+ if (fastestResources.length === 1)
145
+ return fastestResources[0];
146
+ let maxIdleTime = 0;
147
+ let selectedResource = null;
148
+ for (const resource of fastestResources) {
149
+ const idleTime = calculateResourceAvailableTime({
150
+ resource,
151
+ timeSlots: {
152
+ start_time: fastestTime.format("HH:mm"),
153
+ end_time: fastestTime.format("HH:mm"),
154
+ start_at: fastestTime,
155
+ end_at: fastestTime
156
+ },
157
+ currentCapacity: (countMap[resource.id] || 0) + currentCapacity
158
+ });
159
+ if (idleTime > maxIdleTime) {
160
+ maxIdleTime = idleTime;
161
+ selectedResource = resource;
162
+ }
163
+ }
164
+ return selectedResource;
165
+ }
166
+ // Annotate the CommonJS export names for ESM import in node:
167
+ 0 && (module.exports = {
168
+ calculateResourceAvailableTime,
169
+ findFastestAvailableResource
170
+ });
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "private": false,
3
3
  "name": "@pisell/pisellos",
4
- "version": "2.0.25",
4
+ "version": "2.0.26",
5
5
  "description": "一个可扩展的前端模块化SDK框架,支持插件系统",
6
6
  "main": "dist/index.js",
7
7
  "types": "dist/index.d.ts",