Files
fm453 270b881437 ADD:添加后台管理端接口;
DEL:删除不必要的头部注释;
FIX:修正若干已知错误;
2025-07-13 19:40:10 +08:00

316 lines
8.7 KiB
PHP
Executable File

<?php
namespace api\controllers\gm\v1;
use Yii;
use yii\data\Pagination;
use addons\models\AcTruck;
use addons\models\AcTruckOnline;
use addons\models\AcTruckCars;
use addons\models\AcTruckRoute;
class TruckrouteController extends Common
{
public $online_id = 0;
public $truckOl;
public function beforeAction($action)
{
if (!$this->pid) {
$this->result('您正使用本系统内部接口,禁止非法链接使用!');
}
$online_id = Yii::$app->request->get('online_id'); //发车任务ID
if($online_id > 0){
$AcTruckOnline = new AcTruckOnline();
$truckOl = $AcTruckOnline->findOne($online_id);
if(!$truckOl) $this->result('未找到的关联板车任务!',[],404);
$this->truckOl = $truckOl;
$this->online_id = $online_id;
}
return parent::beforeAction($action);
}
//主界面
public function actionIndex()
{
$apis = [
'list' => '板车路线记录列表',
'detail' => '路线记录详情',
'add'=>'添加',
'edit'=>'编辑',
'delete'=>'删除',
'show'=>'显示',
'hide'=>'隐藏',
'ajax'=>'ajax操作'
];
$this->result('您正使用CMTS-GM系统在途板车路线管理接口!', $apis, 200);
}
//所有板车位置记录列表
public function actionList()
{
$s = $this->search();
$res = $s['res'];
if (!$res) {
$this->result('没有查询到相应的数据!', [], 0);
}
$data= [
'total'=>$s['query']->count(),
'routes' => $res,
'truckOls' => $s['truckOls'],
'trucks' => $s['trucks'],
'page'=>$this->page
];
$this->result('板车路线记录查询成功!', $data, 200);
}
public function actionDetail()
{
$pid = $this->pid;
$post = $this->postdata;
$id = isset($post['id']) ? $post['id'] : 0;
if ($id <= 0) {
$this->result('查询参数错误!');
}
$model = new AcTruckRoute();
$detail = $model->findOne($id)->toArray();
if(!$detail) $this->result('未查询到相应数据');
$this->result('查询成功!', $detail, 200);
}
public function actionAdd()
{
//数据预检查、编排
$data = $this->preSave('add');
if(!$data) $this->result('数据预检查未通过,保存失败', $data, 100);
//保存资料
$model = new AcTruckRoute();
foreach ($data as $key=>$val) {
$model->$key = $val;
}
$res = $model->save();
$msg = '数据保存失败!';
if(!$res) $this->result($msg, [], 100);
$msg = '数据保存成功!';
$return = [];
$return['id']= $model->attributes['id']; //获取插入后id;
$this->result($msg,$return, 200);
}
//编辑板车位置信息
public function actionEdit()
{
$detail = $this->preUpdate();
//数据预检查、编排
$data = $this->preSave('edit');
if(!$data) $this->result('数据预检查未通过,保存失败', $data, 100);
//保存资料
foreach ($data as $key=>$val) {
$detail->$key = $val;
}
$res = $detail->save();
$msg = '数据编辑失败!';
if(!$res) $this->result($msg, [], 100);
$msg = '数据编辑成功!';
$return = [];
$return['data']= $detail;
$this->result($msg,$return, 200);
}
//删除记录
public function actionDelete()
{
$detail = $this->preUpdate();
if ($detail->deleted != 0) {
$this->result('数据已被删除过,操作无效', [], 400);
}
$data = [];
$data['update_at'] = time();
$data['deleted'] = $detail->deleted + 1;
//保存资料
foreach ($data as $key=>$val) {
$detail->$key = $val;
}
$res = $detail->save();
$msg = '板车路线记录删除失败!';
if(!$res) $this->result($msg, [], 100);
$msg = '板车路线记录删除成功!';
$this->result($msg,[], 200);
}
public function actionAjax()
{
$detail = $this->preUpdate();
$get = Yii::$app->request->get();
$msg = '';
$errorCode = 0;
$_time = time();
$detail->update_at = $_time;
$res = true;
switch ($get['do']) {
case 'show':
if ($detail->status_code != 1) {
$detail->status_code = 1;
$res = $detail->save();
}
if ($res) {
$msg = '记录显示标记成功';
$errorCode = 200;
} else {
$msg = '记录显示标记失败';
$errorCode = 0;
}
break;
case 'hide':
if ($detail->status_code != 0) {
$detail->status_code = 0;
$res = $detail->save();
}
if ($res) {
$msg = '记录隐藏标记成功';
$errorCode = 200;
} else {
$msg = '记录隐藏标记失败';
$errorCode = 0;
}
break;
default:
break;
}
$this->result($msg, [], $errorCode);
}
private function search(){
$pid = $this->pid;
$model = new AcTruckRoute();
$where = [];
$where[] = ['=', 'pid', $pid];
if($this->online_id){
$where[] = ['=', 'online_id', $this->online_id];
}
$post = $this->postdata;
$search = $post['search'] ?? [];
//板车
$search['truck_id'] = isset($search['truck_id']) ? (int)$search['turck_id'] : 0;
if ($search['truck_id']) {
$where[] = ['=', 'truck_id', $search['truck_id']];
}
$where[] = ['=', 'deleted', 0];
$data = $model->find()->where($where);
$pages = new Pagination(['totalCount' => $data->count(), 'pageSize' => $this->pageSize]);
$res = $data->offset($pages->offset)->limit($pages->limit)->orderby('orderby ASC')->all();
$AcTruck = new AcTruck();
if ($search['truck_id']) {
$trucks[$search['truck_id']] = $AcTruck->findOne($search['truck_id']);
}else{
$truck_ids = [];
foreach ($res as $r) {
$truck_ids[] = $r->truck_id;
}
$truck_ids = array_unique($truck_ids);
$where = [];
$where[] = 'and';
$where[] = ['=', 'pid', $pid];
$where[] = ['IN', 'id', $truck_ids];
$where[] = ['=', 'deleted', 0];
$trucks = $AcTruck->find()->where($where)->indexby('id')->all();
}
$AcTruckOnline = new AcTruckOnline();
if ($this->online_id) {
$truckOls[$this->online_id] = $this->truckOl;
}else{
$truckOl_ids = [];
foreach ($res as $r) {
$truckOl_ids[] = $r->online_id;
}
$truckOl_ids = array_unique($truckOl_ids);
$where = [];
$where[] = 'and';
$where[] = ['=', 'pid', $pid];
$where[] = ['IN', 'id', $truckOl_ids];
$where[] = ['=', 'deleted', 0];
$truckOls = $AcTruckOnline->find()->where($where)->indexby('id')->all();
}
return ['res'=>$res,'query'=>$data,'trucks'=>$trucks,'truckOls'=>$truckOls];
}
/*
* 数据更新前的预检查,返回对应关联数据
* 必须确保get与post数据中均包含需更新的数据id且一致
* */
private function preUpdate()
{
$id = (int)Yii::$app->request->get('id');
if(!$id) $this->result('请求错误,未携带ID参数');
$post = $this->postdata;
if($post['id'] != $id) $this->result('传参id与请求数据不匹配',[],403);
$model = new AcTruckRoute();
$res = $model->findOne($id);
if(!$res) $this->result('未查询到相应数据',[],404);
if($res->pid != $this->pid) $this->result('非本平台数据,不允许操作',[],401);
return $res;
}
/*
* 数据保存前的预检查(查重、参数校验等)
* 要保存的数据[],直接从post中取出
* @op,操作类型(add,edit……)
* 校验机制:
* 编辑数据时,必须用get方式传入参数id,并与post进来的数据id进行比对,只有一致时才能继续;
* 查重:禁止录入重复数据
* 返回:校验重组后的数据
* */
private function preSave($op)
{
$post = $this->postdata;
$data = [];
$_time = time();
//格式化数据
//目标键=》POST键
switch ($op) {
case 'add':
if(!$this->online_id) $this->result('未关联板车装载任务,保存失败',[],403);
$data['online_id'] = $this->online_id;
$data['truck_id'] = $this->truckOl->truck_id;
$data['create_at'] = time();
$data['update_at'] = $data['create_at'];
break;
case 'edit':
$id = (int)Yii::$app->request->get('id');
$_id = (int)$post['id'];
if(!$id != $_id) $this->result('id参数不匹配,请检查');
$data['update_at'] = $_time;
break;
}
$data['pid'] = $this->pid;
$cols = ['longt' => 'lng', 'lat' => 'lat', 'addr' => 'addr','province'=>'province','city'=>'city'];
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;
}
return $data;
}
}