423 lines
12 KiB
PHP
Executable File
423 lines
12 KiB
PHP
Executable File
<?php
|
|
|
|
# @Author: 嗨噜客(三亚) <fm453>
|
|
# @Date: 2022-05-22T07:38:08+08:00
|
|
# @Email: fm453@lukegzs.com
|
|
# @Last modified by: fm453
|
|
# @Last modified time: 2024-08-09T09:20:07+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;
|
|
|
|
class TruckController extends Common
|
|
{
|
|
//主界面
|
|
public function actionIndex()
|
|
{
|
|
return $this->render('../layouts/dev', []);
|
|
}
|
|
|
|
//板车列表
|
|
public function actionList()
|
|
{
|
|
$AcTruck = new AcTruck();
|
|
$where = $where2 = [];
|
|
$where2[] = 'and';
|
|
$where['pid'] = Yii::$app->session->get('pid');
|
|
$post = Yii::$app->request->post();
|
|
$search = [];
|
|
$search['title'] = isset($post['title']) ? trim(htmlspecialchars_decode($post['title'])) : '';
|
|
if (!empty($search['title'])) {
|
|
$where2[] = ['LIKE', 'num_left', $search['title']];
|
|
}
|
|
$where['deleted'] = 0;
|
|
$data = $AcTruck->find()->where($where);
|
|
if ($search['title']) {
|
|
$data = $data->andwhere($where2);
|
|
}
|
|
|
|
$pages = new Pagination(['totalCount' => $data->count(), 'pageSize' => '20']);
|
|
$res = $data->offset($pages->offset)->limit($pages->limit)->all();
|
|
$status = Yii::$app->params['TruckStatus'];
|
|
$AcDriver = new AcDriver();
|
|
$where = [];
|
|
$where['pid'] = Yii::$app->session->get('pid');
|
|
$_drivers = $AcDriver->find()->where($where)->all();
|
|
$drivers = [];
|
|
foreach ($_drivers as $s) {
|
|
$drivers[$s->id] = $s->toArray();
|
|
}
|
|
|
|
return $this->render('list', [
|
|
'trucks' => $res,
|
|
'pager' => $pages,
|
|
'drivers' => $drivers,
|
|
'status' => $status,
|
|
'search' => $search
|
|
]);
|
|
}
|
|
|
|
public function actionSelect()
|
|
{
|
|
$AcTruck = new AcTruck();
|
|
$where = $where2 = [];
|
|
$where2[] = 'and';
|
|
$where['pid'] = Yii::$app->session->get('pid');
|
|
$post = Yii::$app->request->post();
|
|
$search = [];
|
|
$search['title'] = isset($post['title']) ? trim(htmlspecialchars_decode($post['title'])) : '';
|
|
if (!empty($search['title'])) {
|
|
$where2[] = ['LIKE', 'num_left', $search['title']];
|
|
}
|
|
$where['deleted'] = 0;
|
|
$data = $AcTruck->find()->where($where);
|
|
if ($search['title']) {
|
|
$data = $data->andwhere($where2);
|
|
}
|
|
|
|
$pages = new Pagination(['totalCount' => $data->count(), 'pageSize' => '20']);
|
|
$res = $data->offset($pages->offset)->limit($pages->limit)->all();
|
|
$status = Yii::$app->params['TruckStatus'];
|
|
$AcDriver = new AcDriver();
|
|
$where = [];
|
|
$where['pid'] = Yii::$app->session->get('pid');
|
|
$_drivers = $AcDriver->find()->where($where)->all();
|
|
$drivers = [];
|
|
foreach ($_drivers as $s) {
|
|
$drivers[$s->id] = $s->toArray();
|
|
}
|
|
$callback = !empty(Yii::$app->request->get('callback')) ? Yii::$app->request->get('callback') : 'truck';
|
|
return $this->render('select', [
|
|
'trucks' => $res,
|
|
'pager' => $pages,
|
|
'drivers' => $drivers,
|
|
'status' => $status,
|
|
'search' => $search,
|
|
'callback' => $callback
|
|
]);
|
|
}
|
|
|
|
public function actionNew()
|
|
{
|
|
$status = Yii::$app->params['TruckStatus'];
|
|
$AcTruck = new AcTruck();
|
|
$AcTruck->status_code = 0;
|
|
return $this->render('modify', ['status' => $status, 'detail' => $AcTruck]);
|
|
}
|
|
|
|
//编辑板车资料
|
|
public function actionEdit()
|
|
{
|
|
$model = new AcTruck();
|
|
$id = Yii::$app->request->get('id');
|
|
$res = $model->find()->where(['id' => $id])->one();
|
|
if ($res) {
|
|
$res = $res->toArray();
|
|
}
|
|
$AcDriver = new AcDriver();
|
|
$driver = $AcDriver->find()->where(['id' => $res['driver_id']])->one();
|
|
if ($driver) {
|
|
$res['driver_title'] = $driver->name;
|
|
}
|
|
$driver2 = $AcDriver->find()->where(['id' => $res['driver2_id']])->one();
|
|
if ($driver2) {
|
|
$res['driver2_title'] = $driver2->name;
|
|
}
|
|
$status = Yii::$app->params['TruckStatus'];
|
|
return $this->render('modify', ['status' => $status, 'detail' => $res]);
|
|
}
|
|
|
|
//保存板车资料
|
|
public function actionSave()
|
|
{
|
|
$post = Yii::$app->request->post();
|
|
$id = (int)$post['id'];
|
|
$pid = Yii::$app->session->get('pid');
|
|
//格式化数据
|
|
//目标键=》POST键
|
|
$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]) : '';
|
|
}
|
|
//查询车牌号是否在库
|
|
$hasTruck = AcTruck::find()->where(['num_p' => $data['num_p'], 'num_area' => $data['num_area'], 'num_left' => $data['num_left']])->one();
|
|
if ($hasTruck) {
|
|
if ($id && $id != $hasTruck->id) {
|
|
\Yii::$app->getSession()->setFlash('danger', '保存失败,该车牌号已在库!');
|
|
return $this->goBack();
|
|
} else {
|
|
$id = $hasTruck->id;
|
|
}
|
|
}
|
|
|
|
$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]) : '';
|
|
}
|
|
$data['create_at'] = time();
|
|
$data['update_at'] = $data['create_at'];
|
|
// $data['status_code'] = 1;
|
|
$data['pid'] = $pid;
|
|
|
|
//保存板车资料
|
|
$AcTruck = new AcTruck();
|
|
foreach ($data as $key => $val) {
|
|
$AcTruck->$key = $val;
|
|
}
|
|
|
|
if ($id) {
|
|
$AcTruck->id = $id;
|
|
unset($data['create_at']);
|
|
$res = $AcTruck->updateAll($data, ['id' => $id]);
|
|
} else {
|
|
$res = $AcTruck->save();
|
|
$id = $AcTruck->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'];
|
|
$data = $where = [];
|
|
$where['pid'] = Yii::$app->session->get('pid');
|
|
$where['id'] = $id;
|
|
$data['update_at'] = time();
|
|
$data['deleted'] = 1;
|
|
|
|
$model = new AcTruck();
|
|
foreach ($data as $key => $val) {
|
|
$model->$key = $val;
|
|
}
|
|
|
|
$model->id = $id;
|
|
$res = $model->updateAll($data, $where);
|
|
|
|
$return = [];
|
|
$return['url'] = Url::toRoute('truck/list');
|
|
$return['timeout'] = 3; //3秒后自动跳转
|
|
$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'];
|
|
$data = $where = [];
|
|
$where['pid'] = Yii::$app->session->get('pid');
|
|
$where['id'] = $id;
|
|
$data['update_at'] = time();
|
|
|
|
$model = new AcTruck();
|
|
$res = $model->find()->where(['id' => $id])->one();
|
|
$return = [];
|
|
$return['timeout'] = 1; //3秒后自动跳转
|
|
$return['ajax'] = 1;
|
|
if ($res) {
|
|
$res = $res->toArray();
|
|
} else {
|
|
$return['msg'] = '板车数据不存在';
|
|
$return['errorcode'] = 404;
|
|
exit(json_encode($return, JSON_UNESCAPED_UNICODE));
|
|
}
|
|
|
|
switch ($get['do']) {
|
|
case 'hide':
|
|
$data['status_code'] = 0;
|
|
$model->id = $id;
|
|
$res = $model->updateAll($data, $where);
|
|
if ($res) {
|
|
$return['msg'] = '板车停用标记成功';
|
|
$return['errorcode'] = 200;
|
|
$return['data'] = ['css' => 'default'];
|
|
} else {
|
|
$return['msg'] = '板车停用标记失败';
|
|
$return['errorcode'] = 0;
|
|
}
|
|
break;
|
|
case 'show':
|
|
$data['status_code'] = 1;
|
|
$model->id = $id;
|
|
$res = $model->updateAll($data, $where);
|
|
if ($res) {
|
|
$return['msg'] = '板车入库记成功';
|
|
$return['errorcode'] = 200;
|
|
$return['data'] = ['css' => 'primary'];
|
|
} else {
|
|
$return['msg'] = '板车入库标记失败';
|
|
$return['errorcode'] = 0;
|
|
}
|
|
break;
|
|
case 'lock':
|
|
$data['status_code'] = 99;
|
|
$model->id = $id;
|
|
$res = $model->updateAll($data, $where);
|
|
if ($res) {
|
|
$return['msg'] = '板车锁定标记成功';
|
|
$return['errorcode'] = 200;
|
|
$return['data'] = ['css' => 'danger'];
|
|
} else {
|
|
$return['msg'] = '板车锁定标记失败';
|
|
$return['errorcode'] = 0;
|
|
}
|
|
break;
|
|
case 'repair':
|
|
$data['status_code'] = 2;
|
|
$model->id = $id;
|
|
$res = $model->updateAll($data, $where);
|
|
if ($res) {
|
|
$return['msg'] = '板车检修标记成功';
|
|
$return['errorcode'] = 200;
|
|
$return['data'] = ['css' => 'warning'];
|
|
} else {
|
|
$return['msg'] = '板车检修标记失败';
|
|
$return['errorcode'] = 0;
|
|
}
|
|
break;
|
|
case 'reset':
|
|
$data['status_code'] = 2;
|
|
$model->id = $id;
|
|
$res = $model->updateAll($data, $where);
|
|
if ($res) {
|
|
$return['msg'] = '板车保养标记成功';
|
|
$return['errorcode'] = 200;
|
|
$return['data'] = ['css' => 'info'];
|
|
} else {
|
|
$return['msg'] = '板车保养标记失败';
|
|
$return['errorcode'] = 0;
|
|
}
|
|
break;
|
|
case 'working':
|
|
$data['status_code'] = 1;
|
|
$model->id = $id;
|
|
$res = $model->updateAll($data, $where);
|
|
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));
|
|
}
|
|
|
|
//发车
|
|
public function actionFache()
|
|
{
|
|
$get = Yii::$app->request->get();
|
|
$post = Yii::$app->request->post();
|
|
$id = (int)$get['id'];
|
|
|
|
$AcTruck = new AcTruck();
|
|
$truck = $AcTruck->findOne($id);
|
|
if (!$truck) {
|
|
$return['msg'] = '板车数据不存在';
|
|
$return['errorcode'] = 404;
|
|
exit(json_encode($return, JSON_UNESCAPED_UNICODE));
|
|
}
|
|
if ($truck->status_code == 4) {
|
|
$return = [];
|
|
// $return['url'] = Url::toRoute('truckol/modify');
|
|
$return['timeout'] = 3; //3秒后自动跳转
|
|
$return['status'] = 0;
|
|
$return['msg'] = '当前板车已在装车中,发车操作无效';
|
|
$return['errorcode'] = 400;
|
|
exit(json_encode($return, JSON_UNESCAPED_UNICODE));
|
|
} else if ($truck->status_code == 5) {
|
|
$return = [];
|
|
// $return['url'] = Url::toRoute('truckol/modify');
|
|
$return['timeout'] = 3; //3秒后自动跳转
|
|
$return['status'] = 0;
|
|
$return['msg'] = '当前板车已在运输途中,发车操作无效';
|
|
$return['errorcode'] = 400;
|
|
exit(json_encode($return, JSON_UNESCAPED_UNICODE));
|
|
} else if ($truck->status_code != 1) {
|
|
$return['msg'] = '当前板车状态不允许发车,请先确认板车已归为正常状态';
|
|
$return['errorcode'] = 400;
|
|
exit(json_encode($return, JSON_UNESCAPED_UNICODE));
|
|
}
|
|
$data = $where = [];
|
|
$where['pid'] = Yii::$app->session->get('pid');
|
|
$where['id'] = $id;
|
|
$data['update_at'] = time();
|
|
$data['status_code'] = 4;
|
|
foreach ($data as $key => $val) {
|
|
$AcTruck->$key = $val;
|
|
}
|
|
$res = $AcTruck->updateAll($data, $where);
|
|
|
|
$return = [];
|
|
// $return['url'] = Url::toRoute('truckol/modify');
|
|
$return['timeout'] = 3; //3秒后自动跳转
|
|
$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 actionDetail()
|
|
{
|
|
$model = new AcTruck();
|
|
$id = Yii::$app->request->get('id');
|
|
$res = $model->find()->where(['id' => $id])->one();
|
|
if ($res) {
|
|
$res = $res->toArray();
|
|
}
|
|
$AcDriver = new AcDriver();
|
|
$driver = $AcDriver->find()->where(['id' => $res['driver_id']])->one();
|
|
if ($driver) {
|
|
$res['driver_title'] = $driver->name;
|
|
$res['driver_mobile'] = $driver->mobile;
|
|
}
|
|
$driver2 = $AcDriver->find()->where(['id' => $res['driver2_id']])->one();
|
|
if ($driver2) {
|
|
$res['driver2_title'] = $driver2->name;
|
|
$res['driver2_mobile'] = $driver2->mobile;
|
|
}
|
|
$status = Yii::$app->params['TruckStatus'];
|
|
return $this->render('detail', ['status' => $status, 'detail' => $res]);
|
|
}
|
|
}
|