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

563 lines
18 KiB
PHP
Executable File

<?php
# @Author: 嗨噜客(三亚) <fm453>
# @Date: 2022-05-22T07:38:28+08:00
# @Email: fm453@lukegzs.com
# @Last modified by: fm453
# @Last modified time: 2024-08-09T09:20:02+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;
class TruckcarController extends Common
{
//主界面
public function actionIndex()
{
return $this->render('../layouts/dev', []);
}
//所有装车记录列表
public function actionList()
{
$pid = Yii::$app->session->get('pid');
$post = Yii::$app->request->post();
$online_id = Yii::$app->request->get('online_id'); //发车任务ID
$AcTruckCars = new AcTruckCars();
$where = [];
$where[] = 'and';
$where[] = ['=', 'pid', Yii::$app->session->get('pid')];
$search = isset($post['search']) ? $post['search'] : [];
$search['start_city'] = isset($search['start_city']) ? trim(htmlspecialchars_decode($search['start_city'])) : '';
if (!empty($search['start_city'])) {
$where[] = ['LIKE', 'from_city', $search['start_city']];
}
$search['aim_city'] = isset($search['aim_city']) ? trim(htmlspecialchars_decode($search['aim_city'])) : '';
if (!empty($search['aim_city'])) {
$where[] = ['LIKE', 'to_city', $search['aim_city']];
}
$search['car_id'] = isset($search['car_id']) ? (int)$search['car_id'] : 0;
$CarModel = new AcCar();
if (!empty($search['car_id'])) {
$where[] = ['=', 'car_id', $search['car_id']];
$car = $CarModel->findOne($search['car_id']);
$search['car_title'] = isset($car->title) ? $car->title : '';
}
$search['status_code'] = isset($search['status_code']) ? $search['status_code'] : 'all';
if ($search['status_code'] != 'all') {
$where[] = ['=', 'status_code', (int)$search['status_code']];
}
//按月份筛选
$search['start_m'] = isset($search['start_m']) ? $search['start_m'] : '';
if (!empty($search['start_m'])) {
$start = strtotime($search['start_m']);
$s = date("Y-m-d H:i:s", $start);
$ldm = strtotime("last day of " . $s);
$m = date("Y-m-d", $ldm) . " 23:59:59";
$end = strtotime($m);
$where[] = ['between', 'from_time', $start, $end];
}
$where[] = ['=', 'deleted', 0];
$data = $AcTruckCars->find()->where($where);
$pages = new Pagination(['totalCount' => $data->count(), 'pageSize' => '20']);
$res = $data->offset($pages->offset)->limit($pages->limit)->orderby('status_code ASC,id DESC')->all();
$car_ids = [];
foreach ($res as $r) {
$car_ids[] = $r->car_id;
}
$AcCar = new AcCar();
$where = [];
$where[] = 'and';
$where[] = ['=', 'pid', $pid];
$where[] = ['IN', 'id', $car_ids];
$where[] = ['=', 'deleted', 0];
$cars = $AcCar->find()->where($where)->indexby('id')->all();
$status = Yii::$app->params['TruckCarsStatus'];
return $this->render('list', [
'list' => $res,
'pager' => $pages,
'status' => $status,
'cars' => $cars,
'search' => $search
]);
}
public function actionNew()
{
$status = Yii::$app->params['TruckCarsStatus'];
$AcTruckCars = [];
$AcTruckCars['status_code'] = 0;
$online_id = Yii::$app->request->get('online_id'); //发车任务ID
$AcTruckCars['online_id'] = $online_id;
$order_id = Yii::$app->request->get('order_id'); //待承运订单ID
$AcTruckCars['order_id'] = $order_id;
$car_id = Yii::$app->request->get('car_id'); //待装客车ID
$AcTruckCars['car_id'] = $car_id;
return $this->render('create', ['status' => $status, 'detail' => $AcTruckCars]);
}
//编辑客车上板信息
public function actionEdit()
{
$id = Yii::$app->request->get('id'); //承运单ID
$truck_id = $online_id = $store_id = $store2_id = 0;
$truckCar = $truckol = [];
$AcTruckCars = new AcTruckCars();
$res = $AcTruckCars->findOne($id);
if ($res) {
$truckCar = $res->toArray();
$online_id = $res->online_id;
$store2_id = $res->to_store;
$store_id = $res->from_store;
$order_id = $res->order_id;
}
$AcOrder = new AcOrder();
$order = $AcOrder->findOne($order_id);
$AcTruckOnline = new AcTruckOnline();
$res = $AcTruckOnline->findOne($online_id);
if ($res) {
$truck_id = $res->truck_id;
$truckol = $res->toArray();
}
$AcTruck = new AcTruck();
$truck = $AcTruck->findOne($truck_id);
if (!$truck) {
$post = Yii::$app->request->post();
$return = [];
$return['msg'] = '关联板车数据获取异常,发车任务编辑失败';
$return['errorcode'] = 0;
$return['url'] = Url::toRoute(['truck/list', $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');
}
$truckCar['truck_id'] = $truck_id;
$truckCar['truck_title'] = $truck->num_p . $truck->num_area . $truck->num_left;
$truckCar['truck_num'] = $truckCar['truck_title'] ? $truckCar['truck_title'] : $truckol['truck_num'];
$AcStore = new AcStore();
$store = $AcStore->findOne($store_id);
if ($store) {
$truckCar['fromstore_title'] = $store->title;
}
$store2 = $AcStore->findOne($store2_id);
if ($store2) {
$truckCar['endstore_title'] = $store2->title;
}
$AcDriver = new AcDriver();
$driver = $AcDriver->find()->where(['id' => $truck->driver_id])->one();
$truckCar['driver_title'] = $truckCar['driver_mobile'] = '';
if ($driver) {
$truckCar['driver_title'] = $driver->name;
$truckCar['driver_mobile'] = $driver->mobile;
}
$truckCar['from_time'] = !empty($truckCar['from_time']) ? $truckCar['from_time'] : $truckol['from_date'];
$truckCar['to_time'] = !empty($truckCar['to_time']) ? $truckCar['to_time'] : $truckol['end_date'];
$from_city = $order->start_city;
$truckCar['from_city'] = !empty($truckCar['from_city']) ? $truckCar['from_city'] : $from_city;
$truckCar['from_city'] = $truckCar['from_city'] ? $truckCar['from_city'] : $truckol['from_city'];
$end_city = $order->aim_city;
$truckCar['to_city'] = !empty($truckCar['to_city']) ? $truckCar['to_city'] : $end_city;
$truckCar['to_city'] = $truckCar['to_city'] ? $truckCar['to_city'] : $truckol['end_city'];
$truckCar['driver_name'] = isset($truckCar['driver_name']) ? $truckCar['driver_name'] : $truckol['driver_name'];
$truckCar['driver_mobiles'] = isset($truckCar['driver_mobiles']) ? $truckCar['driver_mobiles'] : $truckol['driver_mobiles'];
$status = Yii::$app->params['TruckCarsStatus'];
return $this->render('modify', ['status' => $status, 'detail' => $truckCar]);
}
//保存客车上板信息(直接指派方式)
public function actionSave()
{
$post = Yii::$app->request->post();
$id = (int)$post['id'];
$pid = Yii::$app->session->get('pid');
//格式化数据
//目标键=》POST键
$cols = ['from_city' => 'from_city', 'to_city' => 'to_city'];
foreach ($cols as $col => $key) {
$data[$col] = isset($post[$key]) ? trim($post[$key]) : '';
}
$cols = ['online_id' => 'online_id', 'from_store' => 'from_store', 'to_store' => 'to_store'];
if (isset($_GET['debug']) && ($_GET['debug'] == 1)) {
$cols['status_code'] = 'status_code';
}
foreach ($cols as $col => $key) {
$data[$col] = isset($post[$key]) ? (int)$post[$key] : 0;
}
/*出车计划判断*/
$online_id = $data['online_id'];
if (!$online_id) {
$return['msg'] = '未关联板车出车计划信息,保存失败';
$return['errorcode'] = 400;
exit(json_encode($return, JSON_UNESCAPED_UNICODE));
}
$AcTruckOnline = new AcTruckOnline();
$truckol = $AcTruckOnline->findOne($online_id);
if (!$truckol) {
$return['msg'] = '未找到指定板车出车计划,保存失败';
$return['errorcode'] = 404;
exit(json_encode($return, JSON_UNESCAPED_UNICODE));
}
$key = 'remark';
$data[$key] = isset($post[$key]) ? htmlspecialchars(trim($post[$key])) : '';
$_time = time();
$key = 'to_time';
if (isset($post[$key]) && $post[$key] != "") {
$data[$key] = strtotime($post[$key]);
}
$key = 'from_time';
if (isset($post[$key]) && $post[$key] != "") {
$data[$key] = strtotime($post[$key]);
}
if (!$id) {
$data['create_at'] = $_time;
}
$data['update_at'] = $_time;
$data['pid'] = $pid;
//保存上板信息
$AcTruckCars = new AcTruckCars();
foreach ($data as $key => $val) {
$AcTruckCars->$key = $val;
}
if ($id) {
$res = $AcTruckCars->updateAll($data, ['id' => $id]);
} else {
$res = $AcTruckCars->save();
$id = $AcTruckCars->attributes['id']; //获取插入后id
}
if ($res) {
//修改对应运单状态
$_code = 0;
if (isset($data['status_code'])) {
if ($data['status_code'] == 1) {
$_code = 2;
} else if ($data['status_code'] == 99) {
$_code = 3;
}
}
if ($_code) {
$order_id = isset($post['order_id']) ? (int)$post['order_id'] : 0;
if (!$order_id) {
$car = $AcTruckCars->findOne($id);
$order_id = $car->order_id;
}
$AcOrder = new AcOrder();
$order = $AcOrder->findOne($order_id);
$order->update_at = $_time;
$order->status_code = $_code;
$order->save();
}
\Yii::$app->getSession()->setFlash('success', '信息保存成功!');
return $this->redirect(['edit', 'id' => $id]);
} else {
\Yii::$app->getSession()->setFlash('warning', '信息保存失败!');
return $this->goBack();
}
}
//保存客车上板信息(板车出车界面车辆管理方式)
public function actionSave2()
{
$post = Yii::$app->request->post();
$id = (int)$post['id'];
$pid = Yii::$app->session->get('pid');
//格式化数据
//目标键=》POST键
$cols = ['from_city' => 'from_city', 'to_city' => 'to_city'];
foreach ($cols as $col => $key) {
$data[$col] = isset($post[$key]) ? trim($post[$key]) : '';
}
$cols = ['online_id' => 'online_id', 'from_store' => 'from_store', 'to_store' => 'to_store', 'order_id' => 'order_id', 'car_id' => 'car_id'];
if (isset($_GET['debug']) && ($_GET['debug'] == 1)) {
$cols['status_code'] = 'status_code';
}
foreach ($cols as $col => $key) {
$data[$col] = isset($post[$key]) ? (int)$post[$key] : 0;
}
/*出车计划判断*/
$online_id = $data['online_id'];
if (!$online_id) {
$return['msg'] = '未关联板车出车计划信息,保存失败';
$return['errorcode'] = 400;
exit(json_encode($return, JSON_UNESCAPED_UNICODE));
}
$AcTruckOnline = new AcTruckOnline();
$truckol = $AcTruckOnline->findOne($online_id);
if (!$truckol) {
$return['msg'] = '未找到指定板车出车计划,保存失败';
$return['errorcode'] = 404;
exit(json_encode($return, JSON_UNESCAPED_UNICODE));
}
$key = 'remark';
$data[$key] = isset($post[$key]) ? htmlspecialchars(trim($post[$key])) : '';
$_time = time();
$key = 'to_time';
if (isset($post[$key]) && $post[$key] != "") {
$data[$key] = strtotime($post[$key]);
}
$key = 'from_time';
if (isset($post[$key]) && $post[$key] != "") {
$data[$key] = strtotime($post[$key]);
}
if (!$id) {
$data['create_at'] = $_time;
}
$data['update_at'] = $_time;
$data['pid'] = $pid;
//保存上板信息
$AcTruckCars = new AcTruckCars();
foreach ($data as $key => $val) {
$AcTruckCars->$key = $val;
}
if ($id) {
$res = $AcTruckCars->updateAll($data, ['id' => $id]);
} else {
$res = $AcTruckCars->save();
$id = $AcTruckCars->attributes['id']; //获取插入后id
}
if ($res) {
//修改对应运单状态
$_code = 0;
if (isset($data['status_code'])) {
if ($data['status_code'] == 1) {
$_code = 2;
} else if ($data['status_code'] == 99) {
$_code = 3;
}
}
if ($_code) {
$order_id = isset($post['order_id']) ? (int)$post['order_id'] : 0;
if (!$order_id) {
$car = $AcTruckCars->findOne($id);
$order_id = $car->order_id;
}
$AcOrder = new AcOrder();
$order = $AcOrder->findOne($order_id);
$order->update_at = $_time;
$order->status_code = $_code;
$order->save();
}
\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');
$data = $return = [];
$data['update_at'] = time();
$data['deleted'] = 1;
$model = new AcTruckCars();
$car = $model->findOne($id);
if (!$car) {
$return['msg'] = '数据不存在,删除失败';
$return['errorcode'] = 404;
exit(json_encode($return, JSON_UNESCAPED_UNICODE));
}
if ($car->pid != $pid) {
$return['msg'] = '非本平台数据,不允许操作';
$return['errorcode'] = 400;
exit(json_encode($return, JSON_UNESCAPED_UNICODE));
}
if ($car->deleted != 0) {
$return['msg'] = '数据已被删除过,操作无效';
$return['errorcode'] = 400;
exit(json_encode($return, JSON_UNESCAPED_UNICODE));
}
foreach ($data as $key => $val) {
$model->$key = $val;
}
$model->id = $id;
$res = $model->updateAll($data, ['id' => $id]);
$return['url'] = Url::toRoute('truckcar/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'];
$pid = Yii::$app->session->get('pid');
$data = $return = [];
$_time = time();
$data['update_at'] = $_time;
$model = new AcTruckCars();
$car = $model->findOne($id);
if (!$car) {
$return['msg'] = '数据不存在,删除失败';
$return['errorcode'] = 404;
exit(json_encode($return, JSON_UNESCAPED_UNICODE));
}
if ($car->pid != $pid) {
$return['msg'] = '非本平台数据,不允许操作';
$return['errorcode'] = 400;
exit(json_encode($return, JSON_UNESCAPED_UNICODE));
}
if ($car->deleted != 0) {
$return['msg'] = '数据已被删除,操作无效';
$return['errorcode'] = 400;
exit(json_encode($return, JSON_UNESCAPED_UNICODE));
}
$online_id = $car->online_id;
$AcTruckOnline = new AcTruckOnline();
$truckol = $AcTruckOnline->findOne($online_id);
$order_id = $car->order_id;
$AcOrder = new AcOrder();
$order = $AcOrder->findOne($order_id);
$return['timeout'] = 1; //n秒后自动跳转
$where = ['id' => $id];
switch ($get['do']) {
case 'intruck':
if ($car->status_code != 0) {
$return['msg'] = '当前运单并非待承运状态,操作无效';
$return['errorcode'] = 400;
exit(json_encode($return, JSON_UNESCAPED_UNICODE));
}
$data['status_code'] = 1;
$data['from_time'] = $_time;
$res = $model->updateAll($data, $where);
if ($res) {
//增加板车一辆装车统计
$truckol->update_at = $_time;
$truckol->updateCounters(['cars' => 1]);
$truckol->save();
//修改对应订单的状态
$order->update_at = $_time;
$order->status_code = 2;
$order->save();
$return['msg'] = '客车上板标记成功';
$return['errorcode'] = 200;
$return['data'] = ['css' => 'success'];
} else {
$return['msg'] = '客车上板标记失败';
$return['errorcode'] = 0;
}
break;
case 'arrive':
if ($car->status_code == 0) {
$return['msg'] = '当前任务尚待承运,操作无效';
$return['errorcode'] = 400;
exit(json_encode($return, JSON_UNESCAPED_UNICODE));
}
$data['status_code'] = 99;
$data['to_time'] = $_time;
$res = $model->updateAll($data, $where);
if ($res) {
//修改对应订单的状态
$order->update_at = $_time;
$order->status_code = 3;
$order->save();
$return['msg'] = '客车运达标记记成功';
$return['errorcode'] = 200;
$return['data'] = ['css' => 'primary'];
} else {
$return['msg'] = '客车运达标记失败';
$return['errorcode'] = 0;
}
break;
case 'fix':
$data['status_code'] = 80;
if ($car->status_code != 1) {
$return['msg'] = '当前运单并非运输中状态,操作无效';
$return['errorcode'] = 400;
exit(json_encode($return, JSON_UNESCAPED_UNICODE));
}
$res = $model->updateAll($data, $where);
if ($res) {
$return['msg'] = '客车运达标记记成功';
$return['errorcode'] = 200;
$return['data'] = ['css' => 'primary'];
} else {
$return['msg'] = '客车运达标记失败';
$return['errorcode'] = 0;
}
break;
default:
break;
}
exit(json_encode($return, JSON_UNESCAPED_UNICODE));
}
}