@pisell/pisellos 3.0.32 → 3.0.34

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.
@@ -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": "3.0.32",
4
+ "version": "3.0.34",
5
5
  "description": "一个可扩展的前端模块化SDK框架,支持插件系统",
6
6
  "main": "dist/index.js",
7
7
  "types": "dist/index.d.ts",