392 lines
11 KiB
PHP
Executable File
392 lines
11 KiB
PHP
Executable File
<?php
|
||
|
||
namespace api\controllers\gm\v1;
|
||
|
||
use Yii;
|
||
use yii\data\Pagination;
|
||
use addons\models\AcTruck;
|
||
use addons\models\AcDriver;
|
||
|
||
class TruckController extends Common
|
||
{
|
||
public function beforeAction($action)
|
||
{
|
||
if (!$this->pid) {
|
||
$this->result('您正使用本系统内部接口,禁止非法链接使用!');
|
||
}
|
||
return parent::beforeAction($action);
|
||
}
|
||
//主界面
|
||
public function actionIndex()
|
||
{
|
||
$apis = [
|
||
'list' => '板车清单',
|
||
'detail' => '详情',
|
||
'get-status'=>'状态字典',
|
||
'fache'=>'发车',
|
||
'list-online'=>'装车中',
|
||
'add'=>'添加',
|
||
'edit'=>'编辑',
|
||
'delete'=>'删除',
|
||
'show'=>'显示',
|
||
'hide'=>'隐藏',
|
||
'ajax'=>'ajax操作'
|
||
];
|
||
$this->result('您正使用CMTS-GM系统板车管理接口!', $apis, 200);
|
||
}
|
||
|
||
public function actionGetStatus(){
|
||
$status = Yii::$app->params['TruckStatus'];
|
||
$this->result('板车状态字典', $status, 200);
|
||
}
|
||
|
||
//板车列表
|
||
public function actionList()
|
||
{
|
||
$s = $this->search();
|
||
$res = $s['res'];
|
||
if (!$res) {
|
||
$this->result('没有查询到相应的数据!', [], 0);
|
||
}
|
||
$data= [
|
||
'total'=>$s['query']->count(),
|
||
'trucks' => $res,
|
||
'drivers' => $s['drivers'],
|
||
'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 AcTruck();
|
||
$detail = $model->findOne($id)->toArray();
|
||
if(!$detail) $this->result('未查询到相应数据');
|
||
$data = $this->showTruck($detail);
|
||
$this->result('查询成功!', $data, 200);
|
||
}
|
||
|
||
public function actionAdd()
|
||
{
|
||
//数据预检查、编排
|
||
$data = $this->preSave('add');
|
||
if(!$data) $this->result('数据预检查未通过,保存失败', $data, 100);
|
||
|
||
//保存资料
|
||
$model = new AcTruck();
|
||
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();
|
||
$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;
|
||
$detail->update_at = time();
|
||
|
||
switch ($get['do']) {
|
||
case 'out':
|
||
$detail->status_code = 0;
|
||
$res = $detail->save();
|
||
if ($res) {
|
||
$msg = '板车停用标记成功';
|
||
$errorCode = 200;
|
||
} else {
|
||
$msg = '板车停用标记失败';
|
||
}
|
||
break;
|
||
case 'in':
|
||
$detail->status_code = 1;
|
||
$res = $detail->save();
|
||
if ($res) {
|
||
$msg = '板车启用标记成功';
|
||
$errorCode = 200;
|
||
} else {
|
||
$msg = '板车启用标记失败';
|
||
}
|
||
break;
|
||
case 'lock':
|
||
$detail->status_code = 99;
|
||
$res = $detail->save();
|
||
if ($res) {
|
||
$msg = '板车锁定标记成功';
|
||
$errorCode = 200;
|
||
} else {
|
||
$msg = '板车锁定标记失败';
|
||
}
|
||
break;
|
||
case 'repair':
|
||
$detail->status_code = 2;
|
||
$res = $detail->save();
|
||
if ($res) {
|
||
$msg = '板车检修标记成功';
|
||
$errorCode = 200;
|
||
} else {
|
||
$msg = '板车检修标记失败';
|
||
}
|
||
break;
|
||
case 'reset':
|
||
$detail->status_code = 3;
|
||
$res = $detail->save();
|
||
if ($res) {
|
||
$msg = '板车保养标记成功';
|
||
$errorCode = 200;
|
||
} else {
|
||
$msg = '板车保养标记失败';
|
||
}
|
||
break;
|
||
case 'working':
|
||
$detail->status_code = 1;
|
||
$res = $detail->save();
|
||
if ($res) {
|
||
$msg = '板车恢复标记成功';
|
||
$errorCode = 200;
|
||
} else {
|
||
$msg = '板车恢复标记失败';
|
||
}
|
||
break;
|
||
default:
|
||
break;
|
||
}
|
||
$this->result($msg,[], $errorCode);
|
||
}
|
||
|
||
//发车
|
||
public function actionFache()
|
||
{
|
||
$truck = $this->preUpdate();
|
||
$get = Yii::$app->request->get();
|
||
$msg = '';
|
||
$errorCode = 0;
|
||
if ($truck->status_code == 1) {
|
||
$truck->update_at = time();
|
||
$res = $truck->update();
|
||
if ($res) {
|
||
$msg = '发车成功,请进行下一步操作';
|
||
$errorCode = 200;
|
||
}else{
|
||
$msg = '发车标记保存失败';
|
||
$errorCode = 0;
|
||
}
|
||
}elseif ($truck->status_code == 4) {
|
||
$msg = '当前板车已在装车中,发车操作无效';
|
||
$errorCode = 400;
|
||
} else if ($truck->status_code == 5) {
|
||
$msg = '当前板车已在运输中,发车操作无效';
|
||
$errorCode = 400;
|
||
}else if ($truck->status_code != 1) {
|
||
$msg = '当前板车状态不允许发车,请先确认板车已归为正常状态';
|
||
$errorCode = 400;
|
||
}
|
||
|
||
$this->result($msg,[], $errorCode);
|
||
}
|
||
|
||
//装车中的板车(即在线板车列表,标记了发车状态为装车中的)
|
||
public function actionListOnline(){
|
||
$this->postdata['status'] = 4;
|
||
$s = $this->search();
|
||
$res = $s['res'];
|
||
if (!$res) {
|
||
$this->result('没有在装车中的板车!', [], 0);
|
||
}
|
||
$data= [
|
||
'total'=>$s['query']->count(),
|
||
'trucks' => $res,
|
||
'drivers' => $s['drivers'],
|
||
'page'=>$this->page
|
||
];
|
||
$this->result('在线板车列表获取成功!', $data, 200);
|
||
}
|
||
|
||
private function search(){
|
||
$pid = $this->pid;
|
||
$return = [];
|
||
$model = new AcTruck();
|
||
$where = $where2 = [];
|
||
$where['pid'] = $pid;
|
||
$where2[] = 'and';
|
||
$post = $this->postdata;
|
||
if(isset($post['status'])){
|
||
$where['status_code'] = (int)$post['status'];
|
||
}
|
||
|
||
$search = $post['search'] ?? [];
|
||
$search['title'] = isset($search['title']) ? trim(htmlspecialchars_decode($search['title'])) : '';
|
||
if (!empty($search['title'])) {
|
||
$where2[] = ['LIKE', 'num_left', $search['title']];
|
||
}
|
||
|
||
$where['deleted'] = 0;
|
||
$data = $model->find()->where($where);
|
||
if ($search['title']) {
|
||
$data = $data->andwhere($where2);
|
||
}
|
||
$pages = new Pagination(['totalCount' => $data->count(), 'pageSize' => $this->pageSize]);
|
||
$res = $data->offset($pages->offset)->limit($pages->limit)->all();
|
||
$dids = [];
|
||
if ($res) {
|
||
foreach ($res as $k => $v) {
|
||
if($v->driver_id) $dids[] = $v->driver_id;
|
||
if($v->driver2_id) $dids[] = $v->driver2_id;
|
||
}
|
||
}
|
||
$drivers = [];
|
||
if($dids){
|
||
$AcDriver = new AcDriver();
|
||
$where = [];
|
||
$where[] = ['=','pid',$this->pid];
|
||
$where[] = ['IN','id',$dids];
|
||
$_drivers = $AcDriver->find()->where($where)->all();
|
||
foreach ($_drivers as $s) {
|
||
$drivers[$s->id] = $s->toArray();
|
||
}
|
||
}
|
||
return ['res'=>$res,'query'=>$data,'drivers'=>$drivers];
|
||
}
|
||
|
||
/*
|
||
* 数据更新前的预检查,返回对应关联数据
|
||
* 必须确保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 AcTruck();
|
||
$res = $model->findOne($id);
|
||
if(!$res) $this->result('未查询到相应数据',[],404);
|
||
return $res;
|
||
}
|
||
|
||
/*
|
||
* 数据保存前的预检查(查重、参数校验等)
|
||
* 要保存的数据[],直接从post中取出
|
||
* @op,操作类型(add,edit……)
|
||
* 校验机制:
|
||
* 编辑数据时,必须用get方式传入参数id,并与post进来的数据id进行比对,只有一致时才能继续;
|
||
* 查重:禁止录入重复数据
|
||
* 返回:校验重组后的数据
|
||
* */
|
||
private function preSave($op)
|
||
{
|
||
$post = $this->postdata;
|
||
|
||
$data = [];
|
||
//格式化数据
|
||
//目标键=》POST键
|
||
$model = new AcTruck();
|
||
switch ($op) {
|
||
case 'add':
|
||
$cols = ['num_p' => 'num_p', 'num_area' => 'num_area', 'num_left' => 'num_left'];
|
||
foreach ($cols as $col => $key) {
|
||
$data[$col] = isset($post[$key]) ? trim($post[$key]) : '';
|
||
if(!$data[$col]) $this->result('请传入板车车牌号');
|
||
}
|
||
//查询车牌号是否在库
|
||
$hasOne = AcTruck::find()->where(['num_p' => $data['num_p'], 'num_area' => $data['num_area'], 'num_left' => $data['num_left']])->one();
|
||
if($hasOne) $this->result('保存失败,该车牌号已在库!',$hasOne,100);
|
||
$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 = ['driver_id' => 'driver', 'driver2_id' => 'driver2', 'status_code' => 'status_code', 'max_cars' => 'max_cars', 'deleted' => 'deleted'];
|
||
foreach ($cols as $col => $key) {
|
||
$data[$col] = isset($post[$key]) ? (int)$post[$key] : 0;
|
||
}
|
||
$cols = ['remark' => 'editor'];
|
||
foreach ($cols as $col => $key) {
|
||
$data[$col] = isset($post[$key]) ? htmlspecialchars($post[$key]) : '';
|
||
}
|
||
|
||
return $data;
|
||
}
|
||
|
||
private function showTruck($detail){
|
||
$data = $detail->toArray();
|
||
$AcDriver = new AcDriver();
|
||
$driver = $AcDriver->findOne($detail->driver_id);
|
||
if ($driver) {
|
||
$data['driver_title'] = $driver->name;
|
||
$data['driver_mobile'] = $driver->mobile;
|
||
}
|
||
$driver2 = $AcDriver->findOne($detail->driver2_id);
|
||
if ($driver2) {
|
||
$data['driver2_title'] = $driver2->name;
|
||
$data['driver2_mobile'] = $driver2->mobile;
|
||
}
|
||
return $data;
|
||
}
|
||
}
|