diff --git a/app/Service/v3/Implementations/AppointmentTimeService.php b/app/Service/v3/Implementations/AppointmentTimeService.php index 43a9bf8..18b8ad8 100644 --- a/app/Service/v3/Implementations/AppointmentTimeService.php +++ b/app/Service/v3/Implementations/AppointmentTimeService.php @@ -41,9 +41,11 @@ class AppointmentTimeService implements AppointmentTimeServiceInterface $time4Arr = []; $nowTime = time(); foreach($stores as $store){ + //店铺手动 点击歇业 if($store['is_rest'] == 1){ return $store['name'].'店铺已打烊'; } + //当前时间小于 开始营业时间 $time1 = strtotime(($store['time1'])); if($nowTime < $time1){ return $store['name'].'店铺已打烊'; @@ -64,28 +66,39 @@ class AppointmentTimeService implements AppointmentTimeServiceInterface array_push($time1Arr,$time1); array_push($time2Arr,$time2); } + $res = []; $time1Max = max($time1Arr); - if($nowTime > $time1Max){ - $time1Max = ceil($nowTime / 1800) * 1800; - } $time2Min = min($time2Arr); - $length = ceil(($time2Min - $time1Max) / 1800); - for ($i=1;$i<=$length;$i++){ - $addTime = $time1Max + (30*60*$i); - $res[] = date('h:i',$addTime); - } + array_push($res,$this->calculateTime($time1Max,$time2Min)); if(!empty($time3Arr) && !empty($time4Arr)){ - $time3Max = max($time3Arr); - if($nowTime > $time3Max){ - $time3Max = ceil($nowTime / 1800) * 1800; - } - $time4Min = min($time4Arr); - $length = ceil(($time4Min - $time3Max) / 1800); - for ($i=1;$i<=$length;$i++){ - $addTime = $time3Max + (30*60*$i); - $res[] = date('H:i',$addTime); - } + $time3Max = max($time3Arr); + $time4Min = min($time4Arr); + array_push($res,$this->calculateTime($time3Max,$time4Min)); } return $res; } + + function calculateTime($startTime,$endTime) + { + $nowTime = time(); + //如果开始时间小于当前时间则取当前时间 + if($nowTime > $startTime){ + $startTime = ceil($nowTime / (30*60)) * (30*60); + } + //高峰段 区间 + $peakPeriod = strtotime('16:00'); + $peakPeriodEnd = strtotime('18:00'); + $span = ($peakPeriodEnd - $peakPeriod)/(30*60); + //取得订单下所有店铺时间段 交集 并计算时间段横跨单位区间长度 + $length = ceil(($endTime - $startTime) / (30*60)); + for ($i=1;$i<=$length;$i++){ + $addTime = $startTime + (30*60*$i); + if($addTime >= $peakPeriod && $addTime < $peakPeriodEnd){ + $i+=$span; + $addTime = $startTime + (30*60*$i); + } + $res[] = date('H:i',$addTime) . ' - ' . date('H:i',$addTime + (30*60)); + } + return $res; + } } \ No newline at end of file