ctms/ctms-api/controllers/gm/v1/TruckcarController.php
fm453 4b842ebf3d ADD:添加后台管理端接口;
DEL:删除不必要的头部注释;
FIX:修正若干已知错误;
2025-06-30 09:40:13 +08:00

361 lines
10 KiB
PHP
Executable File
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?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\AcCar;
use addons\models\AcOrder;
class TruckcarController extends Common
{
public function beforeAction($action)
{
if (!$this->pid) {
$this->result('您正使用本系统内部接口,禁止非法链接使用!');
}
return parent::beforeAction($action);
}
//主界面
public function actionIndex()
{
$apis = [
'list' => '承运清单',
'detail' => '承运详情',
'get-status'=>'获取运输状态字典',
'add'=>'添加',
'edit'=>'编辑',
'delete'=>'删除',
'show'=>'显示',
'hide'=>'隐藏',
'ajax'=>'ajax操作'
];
$this->result('您正使用CMTS-GM系统板车承运管理接口', $apis, 200);
}
public function actionGetStatus(){
$status = Yii::$app->params['TruckCarsStatus'];
$this->result('板车装载状态字典', $status, 200);
}
//所有装车记录列表
public function actionList()
{
$s = $this->search();
$res = $s['res'];
if (!$res) {
$this->result('没有查询到相应的数据!', [], 0);
}
$data= [
'total'=>$s['query']->count(),
'truck_cars' => $res,
'cars' => $s['cars'],
'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 AcTruckCars();
$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 AcTruckCars();
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;
$online_id = $detail->online_id;
$AcTruckOnline = new AcTruckOnline();
$truckol = $AcTruckOnline->findOne($online_id);
$order_id = $detail->order_id;
$AcOrder = new AcOrder();
$order = $AcOrder->findOne($order_id);
switch ($get['do']) {
case 'intruck':
//上板
if ($detail->status_code != 0) {
$this->result('当前运单并非待承运状态,操作无效',[], 400);
}
$detail->status_code = 1;
$res = $detail->save();
if ($res) {
//增加板车一辆装车统计
$truckol->update_at = $_time;
$truckol->updateCounters(['cars' => 1]);
$truckol->save();
//修改对应订单的状态
$order->update_at = $_time;
$order->status_code = 2;
$order->save();
$msg = '客车上板标记成功';
$errorCode = 200;
} else {
$msg = '客车上板标记失败';
$errorCode = 100;
}
break;
case 'arrive':
//运达
if ($detail->status_code == 0) {
$this->result('当前运单尚待承运,操作无效',[], 400);
}
$detail->status_code = 99;
$detail->to_time = $_time;
$res = $detail->save();
if ($res) {
//修改对应订单的状态
$order->update_at = $_time;
$order->status_code = 3;
$order->save();
$msg = '客车运达标记记成功';
$errorCode = 200;
} else {
$this->result('客车运达标记失败',[], 100);
}
break;
case 'fix':
//处理质损
$data['status_code'] = 80;
if ($detail->status_code != 1) {
$this->result('当前运单并非运输中状态,操作无效',[], 400);
}
$res = $detail->save();
if ($res) {
$msg = '客车运达标记记成功';
$errorCode = 200;
} else {
$this->result('客车运损标记失败',[], 100);
}
break;
default:
break;
}
$this->result($msg,[], $errorCode);
}
private function search(){
$pid = $this->pid;
$online_id = Yii::$app->request->get('online_id'); //发车任务ID
$return = [];
$model = new AcTruckCars();
$where = [];
$where[] = ['=', 'pid', $pid];
$post = $this->postdata;
$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;
if (!$search['car_id']) {
$where[] = ['=', 'car_id', $search['car_id']];
}
$search['status_code'] = isset($search['status_code']) ? (int)$search['status_code'] : 0;
if ($search['status_code']) {
$where[] = ['=', 'status_code', $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 = $model->find()->where($where);
$pages = new Pagination(['totalCount' => $data->count(), 'pageSize' => $this->pageSize]);
$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();
return ['res'=>$res,'query'=>$data,'cars'=>$cars];
}
/*
* 数据更新前的预检查,返回对应关联数据
* 必须确保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 AcTruckCars();
$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;
/*出车计划判断*/
$online_id = $post['online_id'] ?? 0;
if (!(int)$online_id) {
$this->result('未关联板车出车计划信息,保存失败',[],400);
}
$AcTruckOnline = new AcTruckOnline();
$truckol = $AcTruckOnline->findOne($online_id);
if (!$truckol) {
$this->result('未找到指定板车出车计划,保存失败',[],404);
}
$data = [];
//格式化数据
//目标键=》POST键
$model = new AcTruckCars();
switch ($op) {
case 'add':
$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;
$key = 'remark';
$data[$key] = isset($post[$key]) ? htmlspecialchars(trim($post[$key])) : '';
$cols = ['from_city' => 'from_city', 'to_city' => 'to_city'];
foreach ($cols as $col => $key) {
$data[$col] = isset($post[$key]) ? trim($post[$key]) : '';
}
$cols = ['to_time' => 'to_time', 'from_time' => 'from_time'];
foreach ($cols as $col => $key) {
if (isset($post[$key]) && $post[$key] != "") $data[$key] = strtotime($post[$key]);
}
$cols = ['from_store' => 'from_store', 'to_store' => 'to_store', 'order_id' => 'order_id', 'car_id' => 'car_id','status_code'=>'status_code'];
foreach ($cols as $col => $key) {
$data[$col] = isset($post[$key]) ? (int)$post[$key] : 0;
}
return $data;
}
}