作者 sgj
1 个管道 的构建 通过 耗费 0 秒

reset free time

@@ -245,6 +245,145 @@ class OrderController extends HomeBaseController{ @@ -245,6 +245,145 @@ class OrderController extends HomeBaseController{
245 return $numPrice; 245 return $numPrice;
246 } 246 }
247 247
  248 + /**
  249 + * 只是获取价格信息不会进行操作
  250 + * @param $users_id
  251 + * @param $start_time
  252 + * @param $end_time
  253 + * @return float|int|mixed
  254 + * @throws \think\db\exception\DataNotFoundException
  255 + * @throws \think\db\exception\ModelNotFoundException
  256 + * @throws \think\exception\DbException
  257 + */
  258 +
  259 + public function onlyGetPrice($users_id,$start_time,$end_time){
  260 + $cost=Db::name('cost')->where('id',1)->find();
  261 + $interval = Db::name('interval')->where('id',1)->find();
  262 + $is_free=Db::name('users')->where('id',$users_id)->find()['is_free'];
  263 + //一小时免费状态
  264 + $free=$cost['free'];
  265 +
  266 + //获取整天的价格
  267 + $dayPrice=$this->getDayPrice();
  268 +
  269 + //用了多少天
  270 + $day=floor(($end_time-$start_time)/86400);
  271 + $dayPrice=$day*$dayPrice;
  272 + //去掉整天的金额
  273 + $new_start_time=$start_time+$day*86400;//时间戳
  274 + $new_end_time= $end_time;//时间戳
  275 + $hours_start_time=date('His',$new_start_time);
  276 + $hours_end_time=date('His',$new_end_time);
  277 +
  278 + $stateTime=strtotime($interval['start_time']);
  279 + $endTime=strtotime($interval['end_time']);
  280 + //判断预设时间段是否跨天
  281 + $price=0;
  282 + if ($endTime < $stateTime){
  283 + //设备使用时间段是否跨天(不跨天)
  284 + if ($hours_end_time > $hours_start_time){
  285 +// dump(date('Y-m-d H:i:s',$new_start_time));
  286 +// dump(date('Y-m-d H:i:s',$new_end_time));
  287 +// dump(date('Y-m-d H:i:s',$stateTime));
  288 +// dump(date('Y-m-d H:i:s',$endTime));
  289 + //在预设时间段左边并不与之重合
  290 + if (($new_start_time > strtotime('00:00:00') && $new_start_time < $endTime) && ($new_end_time > $new_start_time && $new_end_time < $endTime)){
  291 + $price +=(ceil(($new_end_time-$new_start_time)/3600) * $interval['price']);
  292 + }
  293 + //与预设时间段的尾部重合
  294 + if (($new_start_time > strtotime('00:00:00') && $new_start_time < $endTime) && ($new_end_time > $endTime && $new_end_time < $stateTime)){
  295 + $price +=(ceil(($endTime-$new_start_time)/3600) * $interval['price'] + ceil(($new_end_time-$endTime)/3600) * $cost['cost']);
  296 + }
  297 + //与预设时间段的头尾都重合
  298 + if (($new_start_time > strtotime('00:00:00') && $new_start_time < $endTime) && $new_end_time > $stateTime){
  299 + $price +=(ceil(($endTime-$new_start_time)/3600) * $interval['price'] + ceil(($new_end_time-$stateTime)/3600) * $interval['price'] + ceil(($stateTime-$endTime)/3600) * $cost['cost']);
  300 + }
  301 + //不与预设时间段重合
  302 + if (($new_start_time > $endTime && $new_start_time < $stateTime) && ($new_end_time > $new_start_time && $new_end_time < $stateTime)){
  303 + //存在首小时免费
  304 + $price +=(ceil(($new_end_time-$new_start_time)/3600) * $cost['cost']);
  305 + if ($free==1 && $is_free==0){
  306 + $price-=$cost['cost'];
  307 + Log::init([
  308 + 'type' => 'File',
  309 + 'path' => APP_PATH.'logs/'
  310 + ]);
  311 + Log::write($price);
  312 + }
  313 + }
  314 + //与预设时间段的头部重合
  315 + if (($new_start_time > $endTime && $new_start_time < $stateTime) && ($new_end_time > $stateTime && $new_end_time < strtotime('24:00:00'))){
  316 + //存在首小时免费
  317 + $price += (ceil(($stateTime-$new_start_time)/3600) * $cost['cost'] + ceil(($new_end_time-$stateTime)/3600) * $interval['price']);
  318 + if ($free==1 && $is_free==0){
  319 + $price-=$cost['cost'];
  320 +
  321 + }
  322 + }
  323 + //在预设时间段右边并不与之重合
  324 + if (($new_start_time > $stateTime && $new_start_time < strtotime('24:00:00')) && ($new_end_time > $new_start_time && $new_end_time < strtotime('24:00:00'))){
  325 + $price +=(ceil(($new_end_time-$new_start_time)/3600) * $interval['price']);
  326 + }
  327 + //设备使用时间段是否跨天(跨天:开始时间在昨天,结束时间在今天)
  328 + }else{
  329 + $YesterdayStateTime=$stateTime-86400;
  330 + $YesterdayEndTime=$endTime-86400;
  331 + //昨天预设结束前使用,今天预设结束前停止
  332 + if(($new_start_time > (strtotime('00:00:00')-86400) && $new_start_time < $YesterdayEndTime) && ($new_end_time > strtotime('00:00:00') && $new_end_time < $endTime)){
  333 +
  334 + $price+= ceil(($YesterdayEndTime-$new_start_time)/3600) * $interval['price']
  335 + +ceil(($YesterdayStateTime-$YesterdayEndTime)/3600) * $cost['cost']
  336 + +ceil(($new_end_time-$YesterdayStateTime)/3600) * $interval['price'];
  337 + }
  338 + //昨天预设开始前使用,今天预设结束前停止
  339 + if(($new_start_time > $YesterdayEndTime && $new_start_time < $YesterdayStateTime) && ($new_end_time > strtotime('00:00:00') && $new_end_time < $endTime)){
  340 + //存在首小时免费
  341 +
  342 + $price+= ceil(($YesterdayStateTime-$new_start_time)/3600) * $cost['cost']
  343 + +ceil(($new_end_time-$YesterdayStateTime)/3600) * $interval['price'];
  344 + if ($free==1 && $is_free==0){
  345 + $price-=$cost['cost'];
  346 + }
  347 + }
  348 + //昨天预设开始前使用,今天预设结束后停止
  349 + if(($new_start_time > $YesterdayEndTime && $new_start_time < $YesterdayStateTime) && ($new_end_time > $endTime && $new_end_time < $stateTime)){
  350 + //存在首小时免费
  351 +
  352 + $price+= ceil(($YesterdayStateTime-$new_start_time)/3600) * $cost['cost']
  353 + +ceil(($endTime-$YesterdayStateTime)/3600) * $interval['price']
  354 + +ceil(($new_end_time-$endTime)/3600) * $cost['cost'];
  355 + if ($free==1 && $is_free==0){
  356 + $price-=$cost['cost'];
  357 + }
  358 + }
  359 + //昨天预设开始后使用,今天预设结束前停止
  360 + if(($new_start_time > $YesterdayStateTime && $new_start_time < strtotime('00:00:00')) && ($new_end_time > strtotime('00:00:00') && $new_end_time < $endTime)){
  361 +
  362 + $price+= ceil(($new_end_time-$new_start_time)/3600) * $interval['price'];
  363 + }
  364 + //昨天预设开始后使用,今天预设结束后停止
  365 + if(($new_start_time > $YesterdayStateTime && $new_start_time < strtotime('00:00:00')) && ($new_end_time > $endTime && $new_end_time < $stateTime)){
  366 +
  367 + $price+= ceil(($endTime-$new_start_time)/3600) * $interval['price']
  368 + +ceil(($new_end_time-$endTime)/3600) * $cost['cost'];
  369 + }
  370 + //昨天预设开始后使用,今天预设开始前停止
  371 + if(($new_start_time > $YesterdayStateTime && $new_start_time < strtotime('00:00:00')) && ($new_end_time > $stateTime && $new_end_time < ($new_start_time+86400))){
  372 +
  373 + $price+= ceil(($endTime-$new_start_time)/3600) * $interval['price']
  374 + +ceil(($stateTime-$endTime)/3600) * $cost['cost']
  375 + +ceil(($new_end_time-$stateTime)/3600) * $interval['price'];
  376 + }
  377 + }
  378 + }
  379 + if ($price > $cost['ceiling']){
  380 + $price = $cost['ceiling'];
  381 + }
  382 + $numPrice = $dayPrice+$price;
  383 + log::write($numPrice);
  384 + return $numPrice;
  385 + }
  386 +
248 //获取整天的价格 387 //获取整天的价格
249 public function getDayPrice(){ 388 public function getDayPrice(){
250 $cost=Db::name('cost')->where('id',1)->find(); 389 $cost=Db::name('cost')->where('id',1)->find();
@@ -216,7 +216,7 @@ class UsersController extends HomeBaseController{ @@ -216,7 +216,7 @@ class UsersController extends HomeBaseController{
216 $order['end_time']=time(); 216 $order['end_time']=time();
217 $order['time']=ceil(($order['end_time']-$order['start_time'])/3600); 217 $order['time']=ceil(($order['end_time']-$order['start_time'])/3600);
218 $price=new OrderController(); 218 $price=new OrderController();
219 - $order['price']=$price->getPrice($users_id,$order['start_time'],$order['end_time']); 219 + $order['price']=$price->onlyGetPrice($users_id,$order['start_time'],$order['end_time']);
220 $time_info=$this->getTimeInfo($order['start_time'],$order['end_time']); 220 $time_info=$this->getTimeInfo($order['start_time'],$order['end_time']);
221 $order=array_merge($order,$time_info); 221 $order=array_merge($order,$time_info);
222 $this->apiResponse(200,'success',$order); 222 $this->apiResponse(200,'success',$order);