Files
charge-pile-serve/app/controller/Navigation.php
MeSHard 94f7e83679 init
2025-11-10 16:12:07 +08:00

140 lines
5.6 KiB
PHP

<?php
namespace app\controller;
use think\facade\Db;
class Navigation
{
public function NavMessage($lat,$lng,$station_number,$openid){
$station_id = Db::table('charge_station')->where("charge_station_number",$station_number)->value("charge_station_id");
$table_user = 'zxc_user';
$hd = new HardMessage();
$interval = $hd->GetPrice($station_id,$openid,$table_user);
$station_type = \app\model\ChargeStation::where('charge_station_id',$station_id)->value('station_type');
$free = 0;
$occupy = 0;
$fault = 0;
$i = 1;
$mess = Db::table('charge_pile')->where('charge_station_id',$station_id)->select();
foreach ($mess as $m){
if ($m['status']==1){
$free+=1;
}elseif ($m['status']==2){
$occupy+=1;
}elseif ($m['status']==3){
$occupy+=1;
}elseif ($m['status']==255){
$fault+=1;
}elseif ($m['status']==0){
$fault+=1;
}
$no = Db::table('charge_pile')->where('charge_pile_id',$m['charge_pile_id'])->value('no');
if($no==null){
Db::table('charge_pile')->where('charge_pile_id',$m['charge_pile_id'])->update(['no' => $i]);
}
$i+=1;
}
$kk['free'] = $free;
$kk['occupy'] = $occupy;
$kk['fault'] = $fault;
$kk['all'] = $free+$occupy+$fault;
// $time = strtotime(date('H:i:s','00:00:00'));
// $time = date('H:i:s',strtotime("2023-03-24 21:59:59"));
$time = date('H:i:s',time());
if ((int)$station_type==0){
$SPrice = $interval['SPrice'];
$interval = $interval['message'];
$i = 0;
foreach ($interval as $in){
$st = date('H:i:s',strtotime($in['start']));
// $en = date('H:i:s',strtotime('-1 seconds',strtotime($in['end'])));
$en = date('H:i:s',strtotime($in['end']));
$check = date('H:i:s',strtotime('23:59:59'));
$check1 = date('H:i:s',strtotime('00:00:00'));
if ($i<=7 && $time>=$st && $time<$en && $time!=$check1){
$kk['Elect'] = $in['univalence'];
$kk['Serve'] = $SPrice;
break;
}elseif($i==7 && ($time>=$st || $time<=$check )|| $time==$check1){
$kk['Elect'] = $in['univalence'];
$kk['Serve'] = $SPrice;
break;
}
$i+=1;
}
}else{
$SPrice = $interval['SPrice'];
$EPrice = $interval['EPrice'];
$kk['Elect'] = $EPrice;
$kk['Serve'] = $SPrice;
}
$kk['total'] = number_format($kk['Elect'] + $kk['Serve'],6);
$address = Db::table('charge_station')->where("charge_station_number",$station_number)->value("address");
$station_name = Db::table('charge_station')->where("charge_station_number",$station_number)->value("charge_station_name");
$kk['address'] = $address;
$kk['station_name'] = $station_name;
$Type = Db::table('charge_station')->where('charge_station_id',$station_id)->value('station_type');
if ((int)$Type == 0){
$kk['type'] = '快充';
}elseif((int)$Type == 1){
$kk['type'] = '慢充';
}
$lat1 = $lat;
$lng1 = $lng;
$lat2 = Db::table('charge_station')->where('charge_station_id',$station_id)->value('latitude');
$lng2 = Db::table('charge_station')->where('charge_station_id',$station_id)->value('longitude');
$distance = $this->getDistance($lat1, $lng1, $lat2, $lng2);
$kk['distance'] = $distance;
return json($kk);
}
public function GetLastStation($lat,$lng){
$lat1 = $lat;
$lng1 = $lng;
$list = Db::table('charge_station')->select();
$mess = array();
foreach ($list as $l){
$charge_station_id = $l['charge_station_id'];
$lat2 = $l['latitude'];
$lng2 = $l['longitude'];
$distance = $this->getDistance($lat1, $lng1, $lat2, $lng2);
$mess[$charge_station_id] = $distance;
}
asort($mess);
$kk = array();
foreach ($mess as $key=>$value){
$station_id = $key;
$distance =$value;
$station_number = Db::table('charge_station')->where('charge_station_id',$station_id)->value('charge_station_number');
$s_latitude = Db::table('charge_station')->where('charge_station_id',$station_id)->value('latitude');
$s_longitude = Db::table('charge_station')->where('charge_station_id',$station_id)->value('longitude');
$kk['station_number'] = $station_number;
$kk['distance'] = $distance;
$kk['s_latitude'] = $s_latitude;
$kk['s_longitude'] = $s_longitude;
break;
}
return json($kk);
}
private function getDistance($lat1, $lng1, $lat2, $lng2): float
{
$earthRadius = 6367000;
$lat1 = ($lat1 * pi() ) / 180;
$lng1 = ($lng1 * pi() ) / 180;
$lat2 = ($lat2 * pi() ) / 180;
$lng2 = ($lng2 * pi() ) / 180;
$calcLongitude = $lng2 - $lng1;
$calcLatitude = $lat2 - $lat1;
$stepOne = pow(sin($calcLatitude / 2), 2) + cos($lat1) * cos($lat2) * pow(sin($calcLongitude / 2), 2);
$stepTwo = 2 * asin(min(1, sqrt($stepOne)));
$calculatedDistance = $earthRadius * $stepTwo;
return round($calculatedDistance/1000,2);
}
}