ctms/ctms-api/controllers/gm/v1/GpsController.php
fm453 314745edf8 优化ctms-api语法、修复已知BUG;
主要修复ctms-api、dacms对PHP新版本的支持问题
2025-04-10 23:19:15 +08:00

379 lines
11 KiB
PHP
Executable File

<?php
# @Author: 嗨噜客(三亚) <fm453>
# @Date: 2022-05-22T07:32:02+08:00
# @Email: fm453@lukegzs.com
# @Last modified by: fm453
# @Last modified time: 2024-08-09T09:18:34+08:00
# @Copyright: www.hiluker.cn
namespace api\controllers\gm\v1;
use Yii;
use yii\data\Pagination;
use yii\helpers\Url;
use addons\models\AcTruck;
use addons\models\AcDriver;
use addons\models\AcTruckOnline;
use addons\models\AcTruckCars;
use addons\models\AcStore;
use addons\models\AcCar;
use addons\models\AcCarBrand;
use addons\models\AcCarSeries;
use addons\models\AcOrder;
use addons\models\AcGps;
use addons\models\AcGpsRoute;
use addons\models\AcTruckRoute;
class GpsController extends Common
{
//替代常规的_construct 析构函数;其他方法调用前执行
public function init()
{
$session = Yii::$app->session;
if (isset($_GET['pid']) && (int)$_GET['pid'] > 0) {
$session->set('pid', (int)$_GET['pid']);
}
$pid = $session->get('pid');
if (!$pid) {
return Yii::$app->response->redirect(['index/index']);
}
$online_id = Yii::$app->request->get('online_id'); //发车任务ID
if (!$online_id) {
$online_id = $session->get('online_id');
} else {
$session->set('online_id', $online_id);
}
if (!$online_id) {
$post = Yii::$app->request->post();
$return = [];
$return['msg'] = '请先选择好要查看的出车大板';
$return['errorcode'] = 0;
// $return['url'] = Url::toRoute(['truckol/index',$post]);
// $return['buttons'] = [
// ['title'=>'好的,我知道了','class'=>'info','url'=>$return['url']]
// ];
$return['content'] = $return['msg'];
$return['class'] = 'warning';
Yii::$app->request->setBodyParams($return);
return Yii::$app->runAction('index/msg');
}
$truckol = $session->get('truckOnline');
$AcTruckOnline = new AcTruckOnline();
$res = TRUE;
if (!$truckol) {
$res = $AcTruckOnline->findOne($online_id);
if ($res) {
$truck_id = $res->truck_id;
$store_id = $res->from_store;
$store2_id = $res->end_store;
$truckol = $res->toArray();
$AcStore = new AcStore();
$store = $AcStore->findOne($store_id);
if ($store) {
$truckol['fromstore_title'] = $store->title;
}
$store2 = $AcStore->findOne($store2_id);
if ($store2) {
$truckol['endstore_title'] = $store2->title;
}
$session->set('truckOnline', $truckol);
}
}
if ($online_id != $truckol['id']) {
$res = $AcTruckOnline->findOne($online_id);
if ($res) {
$truck_id = $res->truck_id;
$store_id = $res->from_store;
$store2_id = $res->end_store;
$truckol = $res->toArray();
$AcStore = new AcStore();
$store = $AcStore->findOne($store_id);
if ($store) {
$truckol['fromstore_title'] = $store->title;
}
$store2 = $AcStore->findOne($store2_id);
if ($store2) {
$truckol['endstore_title'] = $store2->title;
}
$session->set('truckOnline', $truckol);
}
}
if (!$res) {
$post = Yii::$app->request->post();
$return = [];
$return['msg'] = '请先选择好要查看的出车大板';
$return['errorcode'] = 0;
$return['url'] = Url::toRoute(['truckol/index', $post]);
$return['buttons'] = [
['title' => '好的,我知道了', 'class' => 'info', 'url' => $return['url']]
];
$return['content'] = $return['msg'];
$return['class'] = 'warning';
Yii::$app->request->setBodyParams($return);
return Yii::$app->runAction('index/msg');
}
}
//主界面
public function actionIndex()
{
return $this->render('index', []);
}
//所有板车位置记录列表
public function actionList()
{
$pid = Yii::$app->session->get('pid');
$post = Yii::$app->request->post();
$online_id = Yii::$app->session->get('online_id'); //发车任务ID
$truckol = Yii::$app->session->get('truckOnline'); //发车任务
$AcTruckRoute = new AcTruckRoute();
$where = [];
$where[] = 'and';
$where[] = ['=', 'pid', Yii::$app->session->get('pid')];
if ($online_id) {
$where[] = ['=', 'online_id', $online_id];
}
$where[] = ['=', 'deleted', 0];
$data = $AcTruckRoute->find()->where($where)->orderby('orderby ASC,id DESC');
$pages = new Pagination(['totalCount' => $data->count(), 'pageSize' => '20']);
$res = $data->offset($pages->offset)->limit($pages->limit)->all();
$truck_ids = [];
foreach ($res as $r) {
$truck_ids[] = $r->truck_id;
}
$AcTruck = new AcTruck();
$where = [];
$where[] = 'and';
$where[] = ['=', 'pid', $pid];
$where[] = ['IN', 'id', $truck_ids];
$where[] = ['=', 'deleted', 0];
$trucks = $AcTruck->find()->where($where)->indexby('id')->all();
$status = Yii::$app->params['CommonStatus'];
return $this->render('list', [
'list' => $res,
'pager' => $pages,
'status' => $status,
'trucks' => $trucks,
'online_id' => $online_id,
'truckol' => $truckol
]);
}
public function actionNew()
{
$status = Yii::$app->params['CommonStatus'];
$AcTruckRoute = [];
$AcTruckRoute['status_code'] = 0;
$online_id = Yii::$app->session->get('online_id'); //发车任务ID
$truckol = Yii::$app->session->get('truckOnline'); //发车任务
$AcTruckRoute['truck_id'] = $truckol['truck_id'];
$AcTruckRoute['truck_num'] = $truckol['truck_num'];
return $this->render('modify', ['status' => $status, 'detail' => $AcTruckRoute, 'online_id' => $online_id, 'truckol' => $truckol]);
}
//编辑板车位置信息
public function actionEdit()
{
$id = Yii::$app->request->get('id'); //记录ID
$online_id = Yii::$app->session->get('online_id'); //发车任务ID
$truckol = Yii::$app->session->get('truckOnline'); //发车任务
$AcTruckRoute = new AcTruckRoute();
$route = $AcTruckRoute->findOne($id);
if (!$route) {
$post = Yii::$app->request->post();
$return = [];
$return['msg'] = '位置记录数据获取异常,请重新选择获取';
$return['errorcode'] = 0;
$return['url'] = '';
$return['buttons'] = [
['title' => '好的,我知道了', 'class' => 'info', 'url' => $return['url']]
];
$return['content'] = $return['msg'];
$return['class'] = 'warning';
Yii::$app->request->setBodyParams($return);
return Yii::$app->runAction('index/msg');
}
$route = $route->toArray();
$route['truck_id'] = $truckol['truck_id'];
$route['online_id'] = $online_id;
$route['truck_num'] = $truckol['truck_num'];
$status = Yii::$app->params['CommonStatus'];
return $this->render('modify', ['status' => $status, 'detail' => $route, 'online_id' => $online_id, 'truckol' => $truckol]);
}
//保存位置记录信息
public function actionSave()
{
$post = Yii::$app->request->post();
$id = (int)$post['id'];
$pid = Yii::$app->session->get('pid');
$online_id = Yii::$app->session->get('online_id');
$truckol = Yii::$app->session->get('truckOnline'); //发车任务
$data = [];
$data['truck_id'] = $truckol['truck_id'];
$data['online_id'] = $online_id;
//格式化数据
//目标键=》POST键
$cols = ['longt' => 'lng', 'lat' => 'lat', 'addr' => 'addr'];
foreach ($cols as $col => $key) {
$data[$col] = isset($post[$key]) ? trim($post[$key]) : '';
}
$cols = ['orderby' => 'orderby', 'status_code' => 'status_code'];
foreach ($cols as $col => $key) {
$data[$col] = isset($post[$key]) ? (int)$post[$key] : 0;
}
$_time = time();
$key = 'update_at';
$data[$key] = isset($post[$key]) ? strtotime($post[$key]) : $_time;
if (!$id) {
$data['create_at'] = $_time;
}
$data['pid'] = $pid;
//保存位置资料
$AcTruckRoute = new AcTruckRoute();
foreach ($data as $key => $val) {
$AcTruckRoute->$key = $val;
}
if ($id) {
$res = $AcTruckRoute->updateAll($data, ['id' => $id]);
} else {
$res = $AcTruckRoute->save();
$id = $AcTruckRoute->attributes['id']; //获取插入后id
}
if ($res) {
\Yii::$app->getSession()->setFlash('success', '信息保存成功!');
return $this->redirect(['edit', 'id' => $id]);
} else {
\Yii::$app->getSession()->setFlash('warning', '信息保存失败!');
return $this->goBack();
}
}
//删除记录
public function actionDelete()
{
$get = Yii::$app->request->get();
$post = Yii::$app->request->post();
$id = (int)$get['id'];
$pid = Yii::$app->session->get('pid');
$return = [];
$model = new AcTruckRoute();
$route = $model->findOne($id);
if (!$route) {
$return['msg'] = '数据不存在,删除失败';
$return['errorcode'] = 404;
exit(json_encode($return, JSON_UNESCAPED_UNICODE));
}
if ($route->pid != $pid) {
$return['msg'] = '非本平台数据,不允许操作';
$return['errorcode'] = 400;
exit(json_encode($return, JSON_UNESCAPED_UNICODE));
}
if ($route->deleted != 0) {
$return['msg'] = '数据已被删除过,操作无效';
$return['errorcode'] = 400;
exit(json_encode($return, JSON_UNESCAPED_UNICODE));
}
$route->deleted = 1;
$res = $route->save();
$return['url'] = '';
$return['timeout'] = 1; //n秒后自动跳转
$return['status'] = 0;
if ($res) {
$return['msg'] = '位置记录删除成功';
$return['errorcode'] = 200;
$return['data'] = $id;
} else {
$return['msg'] = '位置记录删除失败';
$return['errorcode'] = 0;
}
exit(json_encode($return, JSON_UNESCAPED_UNICODE));
}
public function actionAjax()
{
$get = Yii::$app->request->get();
$post = Yii::$app->request->post();
$id = (int)$get['id'];
$pid = Yii::$app->session->get('pid');
$return = [];
$model = new AcTruckRoute();
$route = $model->findOne($id);
if (!$route) {
$return['msg'] = '数据不存在,删除失败';
$return['errorcode'] = 404;
exit(json_encode($return, JSON_UNESCAPED_UNICODE));
}
if ($route->pid != $pid) {
$return['msg'] = '非本平台数据,不允许操作';
$return['errorcode'] = 400;
exit(json_encode($return, JSON_UNESCAPED_UNICODE));
}
if ($route->deleted != 0) {
$return['msg'] = '数据已被删除,操作无效';
$return['errorcode'] = 400;
exit(json_encode($return, JSON_UNESCAPED_UNICODE));
}
$return['timeout'] = 1; //n秒后自动跳转
$res = TRUE;
switch ($get['do']) {
case 'show':
if ($route->status_code != 1) {
$route->status_code = 1;
$res = $route->save();
}
if ($res) {
$return['msg'] = '记录显示标记成功';
$return['errorcode'] = 200;
$return['data'] = ['css' => 'success'];
} else {
$return['msg'] = '客车上板标记失败';
$return['errorcode'] = 0;
}
break;
case 'hide':
if ($route->status_code != 0) {
$route->status_code = 0;
$res = $route->save();
}
if ($res) {
$return['msg'] = '记录隐藏标记成功';
$return['errorcode'] = 200;
$return['data'] = ['css' => 'success'];
} else {
$return['msg'] = '客车上板标记失败';
$return['errorcode'] = 0;
}
break;
default:
break;
}
exit(json_encode($return, JSON_UNESCAPED_UNICODE));
}
}