Files
MeSHard b22d09bd39 init
2025-12-01 11:19:23 +08:00

244 lines
7.4 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace app\admin\controller\yq\vehicle;
use app\admin\model\yq\alarm\Alarm;
use app\admin\model\yq\base_config\Perimeter;
use app\common\controller\Backend;
use think\Db;
use think\exception\PDOException;
use think\exception\ValidateException;
/**
* G1-1车辆管理
*
* @icon fa fa-circle-o
*/
class Vehicle extends Backend
{
/**
* Vehicle模型对象
* @var \app\admin\model\yq\vehicle\Vehicle
*/
protected $model = null;
protected $childrenids = [];
public function _initialize()
{
parent::_initialize();
$this->model = new \app\admin\model\yq\vehicle\Vehicle;
}
/**
* 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
* 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
* 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
*/
public function index()
{
//设置过滤方法
$this->request->filter(['strip_tags', 'trim']);
if (false === $this->request->isAjax()) {
return $this->view->fetch();
}
//如果发送的来源是 Selectpage则转发到 Selectpage
if ($this->request->request('keyField')) {
return $this->selectpage();
}
[$where, $sort, $order, $offset, $limit] = $this->buildparams();
$new_where = [];
$perimeter_id = input('perimeter_id');
if (!empty($perimeter_id) && isset($perimeter_id)) {
if (empty($this->childrenids)) {
$ids = $perimeter_id;
} else {
$ids = $this->childrenids . ',' . $perimeter_id;
}
$new_where['perimeter_id'] = ['in', $ids];
}
$list = $this->model
->where($where)
->where($new_where)
->order($sort, $order)
->paginate($limit);
$result = ['total' => $list->total(), 'rows' => $list->items()];
return json($result);
}
/**
* 重置状态码
* @param $ids 车辆id
* @return void
* @throws \think\exception\DbException
*/
public function reset($ids = null)
{
$info = $this->model->get($ids);
Db::startTrans();
try {
Alarm::where('license', $info['vehicleNo'])->where('is_reset', 0)->update(['is_reset' => 1]);
$info->save(['qr_color' => 2]);
Db::commit();
$this->success('重置成功~');
} catch (ValidateException|PDOException|Exception $e) {
Db::rollback();
$this->error('重置失败' . $e->getMessage());
}
}
/**
* 周界关联车辆信息
* @param $id 周界id
* @return void
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public function getChildrenData($id)
{
$list = Perimeter::where('pid', $id)->select();
if (empty($list)) {
return;
}
foreach ($list as $k => $v) {
$this->childrenids[] = $v['id'];
$list = Perimeter::where('pid', $v['id'])->select();
if (empty($list)) {
continue;
} else {
$this->getChildrenData($v['id']);
}
}
}
//车辆历史记录
public function vehicle_history()
{
if (input('reservation')) {
$table = 'vehicle_' . date('Ymd', strtotime(input('reservation')));
} else {
$table = 'vehicle_' . date('Ymd');
}
$map['vehicleNo'] = input('vehicleNo');
$list = Db::name($table)->where($map)->field('longitude,latitude')->select();
$coordinate = '';
if ($list) {
foreach ($list as $k => $v) {
$str = $this->getWgs84($v['longitude'] . ',' . $v['latitude']);
$coordinate .= $str['lon'] . ',' . $str['lat'] . ';';
}
$coordinate = substr($coordinate, 0, strlen($coordinate) - 1);
} else {
$coordinate = '106.964108,29.793287';//默认初始值
}
$res = array(
'code' => 200,
'data' => $coordinate,
);
return json($res);
}
public function vehicle_after_list()
{
$vehicleNo = input('vehicleNo');
$table = 'vehicle_' . date('Ymd', strtotime('-1 day'));
//企业列表
$perimeter_list = Perimeter::where(['region_type' => 6, 'is_del' => 1])->field('id,name,info')->select();
//每日车辆列表
$list = Db::name($table)->where(['vehicleNo' => $vehicleNo])->field('longitude,latitude,positionTime,v_carettime')->select();
$lists = array_unique($list, SORT_REGULAR);
$v_list = [];
foreach ($lists as $i => $o) {
$str = $this->getWgs84($o['longitude'] . ',' . $o['latitude']);
$point = [
'lng' => $str['lon'],
'lat' => $str['lat'],
];
foreach ($perimeter_list as $k => $v) {
if ($v['info'] != '') {
$lv = explode(';', $v['info']);
$lv_arr = [];
foreach ($lv as $lvK => $lvV) {
$ar = explode(',', $lvV);
$lv_arr[] = [
'lng' => $ar[0],
'lat' => $ar[1],
];
}
$lvs = $this->is_point_in_polygon($point, $lv_arr);
if ($lvs) {
$v['point'] = $point['lng'] . ',' . $point['lat'];
$v['positionTime'] = $o['positionTime'];
$v['v_carettime'] = $o['v_carettime'];
$v['vehicleNo'] = $vehicleNo;
$v_list[] = $v;
}
}
}
}
$res = array(
'code' => 0,
'count' => 0,
'data' => $v_list
);
return json($res);
}
public function getWgs84($par)
{
$result = array(); // 转换后的结果
$tokens = preg_split('/[\r\n]+/', $par);
foreach ($tokens as $token) {
if (false !== strpos($token, '=')) {
list($key, $value) = explode('=', $token, 2);
$result[$key] = $value;
} else
$result[] = $token;
}
foreach ($result as $k => $v) {
if ($v) {
$Varr = explode(',', $v);
$res = $this->bd_decrypt($Varr[0], $Varr[1]);
$arr = [
'lon' => number_format($res['gg_lon'], 6),
'lat' => number_format($res['gg_lat'], 6),
];
}
}
return $arr;
}
//BD-09(百度) 坐标转换成 GCJ-02(火星,高德) 坐标
//@param bd_lon 百度经度
//@param bd_lat 百度纬度
public function bd_decrypt($bd_lon, $bd_lat)
{
$x_pi = 3.14159265358979324 * 3000.0 / 180.0;
$x = $bd_lon - 0.0065;
$y = $bd_lat - 0.006;
$z = sqrt($x * $x + $y * $y) - 0.00002 * sin($y * $x_pi);
$theta = atan2($y, $x) - 0.000003 * cos($x * $x_pi);
$data['gg_lon'] = $z * cos($theta);
$data['gg_lat'] = $z * sin($theta);
return $data;
}
}