You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

119 lines
3.6 KiB

<?php
namespace App\Service\v3\Implementations;
use App\Constants\v3\ErrorCode;
use App\Exception\ErrorCodeException;
use App\Model\v3\ShoppingCart;
use App\Model\v3\Store;
use App\Service\v3\Interfaces\AppointmentTimeServiceInterface;
use phpDocumentor\Reflection\Types\Object_;
class AppointmentTimeService implements AppointmentTimeServiceInterface
{
public function do()
{
return [
'尽快送达',
'08:30 - 09:00',
'09:00 - 09:30',
'09:30 - 10:00',
'10:00 - 10:30'
];
}
public function check()
{
// TODO: Implement check() method.
}
public function undo()
{
// TODO: Implement undo() method.
}
public function get($shopcartIds)
{
$shopcartIds = explode(',',$shopcartIds);
$sotreIds = ShoppingCart::query()->whereIn('id',$shopcartIds)->pluck('store_id');
$stores = Store::query()->whereIn('id',$sotreIds)->get()->toArray();
$time1Arr = [];
$time2Arr = [];
$time3Arr = [];
$time4Arr = [];
$nowTime = time();
$closedTime = strtotime('19:30');
//取得所有店铺营业时间交集
foreach($stores as $store){
//店铺手动 点击歇业
if($store['is_rest'] == 1){
throw new ErrorCodeException(ErrorCode::STORE_REST);
}
//当前时间小于 开始营业时间
$time1 = strtotime(($store['time1']));
if($nowTime < $time1){
throw new ErrorCodeException(ErrorCode::STORE_REST);
}
if($nowTime > $closedTime){
throw new ErrorCodeException(ErrorCode::STORE_REST);
}
$time2 = strtotime(($store['time2']));
if(!empty($store['time3']) && !empty($store['time4'])){
$time3 = strtotime(($store['time3']));
$time4 = strtotime(($store['time4']));
array_push($time3Arr,$time3);
array_push($time4Arr,$time4);
$endTime = $time4;
}else{
$endTime = $time2;
}
if(($nowTime+60*30) > $endTime){
throw new ErrorCodeException(ErrorCode::STORE_REST);
}
array_push($time1Arr,$time1);
array_push($time2Arr,$time2);
}
$res = [];
$time1Max = max($time1Arr);
$time2Min = min($time2Arr);
if($time2Min > $nowTime) {
$res[] = $this->calculateTime($time1Max, $time2Min);
}
if(!empty($time3Arr) && !empty($time4Arr)){
$time3Max = max($time3Arr);
$time4Min = min($time4Arr);
if($time4Min > $nowTime) {
$res[] = $this->calculateTime($time3Max, $time4Min);
}
}
return array_shift($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);
}
$date['title'] = date('H:i',$addTime) . ' - ' . date('H:i',$addTime + (30*60));
$date['value'] = date('H:i',$addTime) . ' - ' . date('H:i',$addTime + (30*60));
$res[] = $date;
}
return $res;
}
}