双日历关联空闲时段校验代码修复求助,预约顺序为先Bob后Alice
代码修复方案
原有代码核心问题
- 仅计算了两段忙期中间的空闲时段,遗漏了首个忙期前和最后一个忙期后的可用空闲
- 忙段未做排序处理,若服务端返回顺序错乱会直接导致空闲计算错误
- 可用性校验逻辑完全错误:没有按照「Bob时段SS+20min、Alice时段S+20minS+90min都完全空闲」的规则校验,错误判断两个空闲结束时间的差值
Object.keys返回的是字符串类型的时间戳,运算前未做数字转换容易出现异常- 最后嵌套循环的遍历对象写错,两层都遍历了
arr2的长度
修复后完整代码
let min1 = 20; // Bob服务时长20分钟 let min2 = 70; // Alice服务时长70分钟 let serviceLength1 = min1 * 60 * 1000; let serviceLength2 = min2 * 60 * 1000; // 忙段数据先转时间戳再按开始时间升序排序 const parseAndSortBusy = (busyArr) => { return busyArr .map(item => ({ start: Date.parse(item.start), end: Date.parse(item.end) })) .sort((a, b) => a.start - b.start); }; let busy1 = parseAndSortBusy([ { start: "2021-05-25T09:00:00+02:00", end: "2021-05-25T09:45:00+02:00" }, { start: "2021-05-25T12:00:00+02:00", end: "2021-05-25T13:15:00+02:00" }, { start: "2021-05-25T16:00:00+02:00", end: "2021-05-25T16:45:00+02:00" }, { start: "2021-05-26T09:00:00+02:00", end: "2021-05-26T09:45:00+02:00" }, { start: "2021-05-26T11:00:00+02:00", end: "2021-05-26T11:45:00+02:00" }, { start: "2021-05-26T15:00:00+02:00", end: "2021-05-26T15:45:00+02:00" }, { start: "2021-05-27T09:00:00+02:00", end: "2021-05-27T09:45:00+02:00" }, ]); let busy2 = parseAndSortBusy([ { start: "2021-05-25T09:45:00+02:00", end: "2021-05-25T10:00:00+02:00" }, { start: "2021-05-25T11:00:00+02:00", end: "2021-05-25T11:45:00+02:00" }, { start: "2021-05-25T14:00:00+02:00", end: "2021-05-25T14:40:00+02:00" }, { start: "2021-05-25T16:45:00+02:00", end: "2021-05-25T17:00:00+02:00" }, { start: "2021-05-26T09:45:00+02:00", end: "2021-05-26T10:00:00+02:00" }, { start: "2021-05-26T11:45:00+02:00", end: "2021-05-26T12:00:00+02:00" }, { start: "2021-05-26T13:00:00+02:00", end: "2021-05-26T13:35:00+02:00" }, ]); // 计算单人所有满足最小时长要求的空闲时段 const getFreeSlots = (busyList, minDuration) => { let freeSlots = []; if (busyList.length === 0) { freeSlots.push({ start: Date.now(), end: Infinity }); return freeSlots; } // 首个忙段之前的空闲 const firstFreeEnd = busyList[0].start; if (firstFreeEnd - Date.now() >= minDuration) { freeSlots.push({ start: Date.now(), end: firstFreeEnd }); } // 两个忙段中间的空闲 for (let i = 0; i < busyList.length - 1; i++) { const prevEnd = busyList[i].end; const nextStart = busyList[i+1].start; if (nextStart - prevEnd >= minDuration) { freeSlots.push({ start: prevEnd, end: nextStart }); } } // 最后一个忙段之后的空闲 const lastFreeStart = busyList.at(-1).end; freeSlots.push({ start: lastFreeStart, end: Infinity }); return freeSlots; }; const bobFreeSlots = getFreeSlots(busy1, serviceLength1); const aliceFreeSlots = getFreeSlots(busy2, serviceLength2); // 匹配符合业务规则的预约时段 const availableSlots = []; for (const bobFree of bobFreeSlots) { // Bob可承载的预约最早/最晚开始时间 const bobStartMin = bobFree.start; const bobStartMax = bobFree.end - serviceLength1; if (bobStartMax < bobStartMin) continue; // 对应Alice需要满足的可用时段范围 const aliceNeedStartMin = bobStartMin + serviceLength1; const aliceNeedStartMax = bobStartMax + serviceLength1; for (const aliceFree of aliceFreeSlots) { // Alice可承载的对应时段最早/最晚开始时间 const aliceAvailableStartMin = aliceFree.start; const aliceAvailableStartMax = aliceFree.end - serviceLength2; // 计算两个范围的交集 const overlapStart = Math.max(aliceNeedStartMin, aliceAvailableStartMin); const overlapEnd = Math.min(aliceNeedStartMax, aliceAvailableStartMax); if (overlapEnd >= overlapStart) { availableSlots.push({ serviceStart: new Date(overlapStart - serviceLength1), serviceEnd: new Date(overlapStart + serviceLength2) }); } } } // 打印可读结果 console.log('可用预约时段:', availableSlots.map(item => `${item.serviceStart.toLocaleString()} 至 ${item.serviceEnd.toLocaleString()}` ));
逻辑说明
- 先对所有忙段做标准化处理,确保不管服务端返回顺序如何,都能正确计算空闲
- 空闲计算覆盖全时段,不会遗漏首尾的可用时间
- 校验逻辑严格匹配业务规则:仅当
[S, S+20min]属于Bob空闲、[S+20min, S+90min]属于Alice空闲时,才判定S为有效预约开始时间 - 代码预留了营业时间限制、时间粒度切分的扩展位置,可根据实际业务调整
内容的提问来源于stack exchange,提问作者ambrix
相关产品推荐
相关产品推荐

