@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.
@@ -782,4 +782,103 @@ export var checkSubResourcesCapacity = function checkSubResourcesCapacity(resour
782
782
  }
783
783
  });
784
784
  }
785
- };
785
+ };
786
+
787
+ /**
788
+ * @title: 根据日期范围过滤日程
789
+ *
790
+ * @export
791
+ * @param {any[]} schedule
792
+ * @param {string} startDate
793
+ * @param {string} endDate
794
+ * @return {*}
795
+ */
796
+ export function filterScheduleByDateRange(schedule, startDate, endDate) {
797
+ if (!(schedule !== null && schedule !== void 0 && schedule.length)) return [];
798
+ var start = new Date(startDate);
799
+ var end = new Date(endDate);
800
+ return schedule.filter(function (item) {
801
+ var itemDate = new Date(item.date);
802
+ return itemDate >= start && itemDate <= end;
803
+ });
804
+ }
805
+
806
+ /**
807
+ * 传入商品数据,返回基于商品配置的提前量的最早开始日期和最晚结束日期
808
+ *
809
+ * @export
810
+ * @param {ProductData} product
811
+ * @return {*}
812
+ */
813
+ export function checkSessionProductLeadTime(product) {
814
+ var latestStartDate = ''; // 最晚开始日期
815
+ var earliestEndDate = ''; // 最早结束日期
816
+ var _product$cut_off_time = product.cut_off_time,
817
+ future_day = _product$cut_off_time.future_day,
818
+ unit = _product$cut_off_time.unit,
819
+ unit_type = _product$cut_off_time.unit_type;
820
+ // 预定天数存在,则开始计算从当前时间开始往后推预定天数作为结束时间
821
+ if (future_day) {
822
+ var endDate = dayjs().add(future_day, 'day');
823
+ if (!earliestEndDate || endDate.isBefore(dayjs(earliestEndDate), 'day')) {
824
+ earliestEndDate = endDate.format('YYYY-MM-DD');
825
+ }
826
+ } else if (future_day === 0) {
827
+ var _endDate = dayjs();
828
+ if (!earliestEndDate || _endDate.isBefore(dayjs(earliestEndDate), 'day')) {
829
+ earliestEndDate = _endDate.format('YYYY-MM-DD');
830
+ }
831
+ }
832
+ // 如果存在提前预约时间,则开始计算从当前时间开始往后推提前预约时间作为开始时间
833
+ if (unit && unit_type) {
834
+ var startDate = dayjs(dayjs().add(unit, unit_type).format('YYYY-MM-DD'));
835
+ if (!latestStartDate || startDate.isAfter(dayjs(latestStartDate), 'day')) {
836
+ latestStartDate = startDate.format('YYYY-MM-DD');
837
+ }
838
+ }
839
+ return {
840
+ latestStartDate: latestStartDate,
841
+ earliestEndDate: earliestEndDate
842
+ };
843
+ }
844
+
845
+ /**
846
+ * 基于商品的 resources 配置,返回可选择的资源的 id 列表
847
+ *
848
+ * @export
849
+ * @param {ProductData} product
850
+ * @return {*}
851
+ */
852
+ export function getResourcesIdsByProduct(product) {
853
+ var _product$product_reso, _product$product_reso2;
854
+ var tempResourceIds = [];
855
+ product === null || product === void 0 || (_product$product_reso = product.product_resource) === null || _product$product_reso === void 0 || (_product$product_reso = _product$product_reso.resources) === null || _product$product_reso === void 0 || (_product$product_reso2 = _product$product_reso.forEach) === null || _product$product_reso2 === void 0 || _product$product_reso2.call(_product$product_reso, function (resource) {
856
+ if ((resource === null || resource === void 0 ? void 0 : resource.status) == 1) {
857
+ var _resource$default_res, _resource$optional_re;
858
+ if (resource !== null && resource !== void 0 && (_resource$default_res = resource.default_resource) !== null && _resource$default_res !== void 0 && _resource$default_res.length) {
859
+ tempResourceIds.push.apply(tempResourceIds, _toConsumableArray(resource === null || resource === void 0 ? void 0 : resource.default_resource));
860
+ } else if (resource !== null && resource !== void 0 && (_resource$optional_re = resource.optional_resource) !== null && _resource$optional_re !== void 0 && _resource$optional_re.length) {
861
+ tempResourceIds.push.apply(tempResourceIds, _toConsumableArray(resource === null || resource === void 0 ? void 0 : resource.optional_resource));
862
+ }
863
+ }
864
+ });
865
+ return tempResourceIds;
866
+ }
867
+
868
+ /**
869
+ * 资源排序,把单个资源靠前,组合资源排在后面
870
+ *
871
+ * @export
872
+ * @param {ResourceItem[]} resourcesList
873
+ * @return {*}
874
+ */
875
+ export function sortCombinedResources(resourcesList) {
876
+ var newResourcesList = _toConsumableArray(resourcesList);
877
+ newResourcesList.sort(function (a, b) {
878
+ var _a$metadata2, _b$metadata2;
879
+ var aIsCombined = ((_a$metadata2 = a.metadata) === null || _a$metadata2 === void 0 || (_a$metadata2 = _a$metadata2.combined_resource) === null || _a$metadata2 === void 0 ? void 0 : _a$metadata2.status) === 1;
880
+ var bIsCombined = ((_b$metadata2 = b.metadata) === null || _b$metadata2 === void 0 || (_b$metadata2 = _b$metadata2.combined_resource) === null || _b$metadata2 === void 0 ? void 0 : _b$metadata2.status) === 1;
881
+ return aIsCombined === bIsCombined ? 0 : aIsCombined ? 1 : -1;
882
+ });
883
+ return newResourcesList;
884
+ }
@@ -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
+ }
@@ -15,6 +15,8 @@ export declare class DateModule extends BaseModule implements Module, DateModule
15
15
  getDateRange(): ITime[];
16
16
  getResourceDates(params: IGetAvailableTimeListParams): Promise<ITime[]>;
17
17
  getDateList(): ITime[];
18
+ setDateList(dateList: ITime[]): void;
19
+ fetchResourceDates(params: IGetAvailableTimeListParams): Promise<any>;
18
20
  getResourceAvailableTimeList(params: IGetAvailableTimeListParams): Promise<ITime[]>;
19
21
  clearDateRange(): void;
20
22
  storeChange(): void;
@@ -65,18 +65,13 @@ var DateModule = class extends import_BaseModule.BaseModule {
65
65
  getDateList() {
66
66
  return this.store.dateList;
67
67
  }
68
- async getResourceAvailableTimeList(params) {
69
- var _a;
70
- const { url, query, rules, type } = params;
68
+ setDateList(dateList) {
69
+ this.store.dateList = dateList;
70
+ }
71
+ async fetchResourceDates(params) {
72
+ const { url, query } = params;
71
73
  const fetchUrl = url || "/schedule/resource/list";
72
74
  const { start_date, end_date, resource_ids } = query || {};
73
- if (!start_date || !end_date) {
74
- return [];
75
- }
76
- let dates = (0, import_utils.generateMonthDates)(start_date, end_date, type);
77
- if (!(resource_ids == null ? void 0 : resource_ids.length)) {
78
- return (0, import_utils.disableAllDates)(dates);
79
- }
80
75
  try {
81
76
  const res = await this.request.get(fetchUrl, {
82
77
  start_date,
@@ -86,6 +81,24 @@ var DateModule = class extends import_BaseModule.BaseModule {
86
81
  }, {
87
82
  useCache: true
88
83
  });
84
+ return res;
85
+ } catch (error) {
86
+ console.error(error);
87
+ }
88
+ }
89
+ async getResourceAvailableTimeList(params) {
90
+ var _a;
91
+ const { query, rules, type } = params;
92
+ const { start_date, end_date, resource_ids } = query || {};
93
+ if (!start_date || !end_date) {
94
+ return [];
95
+ }
96
+ let dates = (0, import_utils.generateMonthDates)(start_date, end_date, type);
97
+ if (!(resource_ids == null ? void 0 : resource_ids.length)) {
98
+ return (0, import_utils.disableAllDates)(dates);
99
+ }
100
+ try {
101
+ const res = await this.fetchResourceDates(params);
89
102
  if (!((_a = res == null ? void 0 : res.data) == null ? void 0 : _a.length)) {
90
103
  return (0, import_utils.disableAllDates)(dates);
91
104
  }
@@ -58,10 +58,10 @@ var RulesModule = class extends import_BaseModule.BaseModule {
58
58
  productList
59
59
  };
60
60
  }
61
- const mergedDiscountList = (0, import_utils.uniqueById)([
61
+ const mergedDiscountList = (0, import_utils.uniqueById)((0, import_utils.uniqueById)([
62
62
  ...oldDiscountList,
63
63
  ...newDiscountList
64
- ]);
64
+ ]), "product_id");
65
65
  const result = this.calcDiscount({
66
66
  discountList: mergedDiscountList,
67
67
  productList: [...productList]
@@ -6,6 +6,7 @@ export interface ScheduleState {
6
6
  export type ScheduleAvailabilityDateItem = {
7
7
  date: string;
8
8
  schedule_id: number[];
9
+ product_ids: number[];
9
10
  };
10
11
  export interface ScheduleModuleAPI {
11
12
  }
@@ -5,7 +5,7 @@ import { BookingByStepState } from './types';
5
5
  import { CartItem, IUpdateItemParams, ProductData, ProductResourceItem, ECartItemInfoType, ECartItemCheckType } from '../../modules';
6
6
  import { Account } from '../../modules/Account/types';
7
7
  import { IStep } from '../../modules/Step/tyeps';
8
- import { TimeSliceItem } from './utils/resources';
8
+ import { TimeSliceItem, ResourceItem } from './utils/resources';
9
9
  import { ITime } from '../../modules/Date/types';
10
10
  import dayjs from 'dayjs';
11
11
  import { IHolder, IFetchHolderAccountsParams } from '../../modules/AccountList/types';
@@ -153,7 +153,7 @@ export declare class BookingByStepImpl extends BaseModule implements Module {
153
153
  id: number | undefined;
154
154
  _id: string;
155
155
  product: ProductData | undefined;
156
- resources: import("./utils/resources").ResourceItem[];
156
+ resources: ResourceItem[];
157
157
  holder_id: string | number | undefined;
158
158
  holder_name: string | undefined;
159
159
  } | undefined;
@@ -189,7 +189,7 @@ export declare class BookingByStepImpl extends BaseModule implements Module {
189
189
  private getScheduleDataByIds;
190
190
  openProductDetail(productId: number): Promise<void>;
191
191
  closeProductDetail(): void;
192
- getTimeslotBySchedule({ date, scheduleIds, resources, product }: {
192
+ getTimeslotBySchedule({ date, scheduleIds, resources, product, }: {
193
193
  date: string;
194
194
  scheduleIds?: number[];
195
195
  resources?: ProductResourceItem[];
@@ -211,7 +211,7 @@ export declare class BookingByStepImpl extends BaseModule implements Module {
211
211
  }): void;
212
212
  setOtherData(key: string, value: any): void;
213
213
  getOtherData(key: string): any;
214
- getProductTypeById(id: number): Promise<"duration" | "session" | "normal">;
214
+ getProductTypeById(id: number): Promise<"normal" | "duration" | "session">;
215
215
  /**
216
216
  * 提供给 UI 的方法,减轻 UI 层的计算压力,UI 层只需要传递 cartItemId 和 resourceCode 即返回对应的 renderList
217
217
  *
@@ -221,9 +221,30 @@ export declare class BookingByStepImpl extends BaseModule implements Module {
221
221
  * @memberof BookingByStepImpl
222
222
  */
223
223
  getResourcesByCartItemAndCode(cartItemId: string, resourceCode: string): any;
224
+ /**
225
+ * 根据日期范围批量获取时间槽
226
+ * @param params 参数对象
227
+ * @returns Promise<Record<string, TimeSliceItem[]>> 返回日期到时间槽的映射
228
+ */
229
+ getTimeslotsScheduleByDateRange({ startDate, endDate, scheduleIds, resources, }: {
230
+ startDate: string;
231
+ endDate: string;
232
+ scheduleIds?: number[];
233
+ resources?: ProductResourceItem[];
234
+ }): Promise<Record<string, TimeSliceItem[]>>;
224
235
  getAvailableDateForSession(params?: {
225
236
  startDate?: string;
226
237
  endDate?: string;
227
238
  type?: 'month';
228
- }): Promise<ITime[]>;
239
+ }): Promise<{
240
+ dateList: ITime[];
241
+ firstAvailableDate: ITime | undefined;
242
+ }>;
243
+ getAvailableDateForSessionOptimize(params?: {
244
+ startDate?: string;
245
+ endDate?: string;
246
+ }): Promise<{
247
+ dateList: any;
248
+ firstAvailableDate: any;
249
+ }>;
229
250
  }