Initial commit,修正
This commit is contained in:
Executable
+538
@@ -0,0 +1,538 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @Author: fm453
|
||||
* @Date: 2018-08-17 19:39:41
|
||||
# @Last modified by: fm453
|
||||
# @Last modified time: 2021-09-14T11:44:34+08:00
|
||||
* @Email: fm453@lukegzs.com
|
||||
*/
|
||||
namespace backend\controllers;
|
||||
|
||||
use Yii;
|
||||
use yii\data\Pagination;
|
||||
use yii\helpers\Url;
|
||||
use addons\models\AcCar;
|
||||
use addons\models\AcCarOwner;
|
||||
use addons\models\AcCarBrand;
|
||||
use addons\models\AcCarSeries;
|
||||
use common\models\Fans;
|
||||
|
||||
class CarController extends \yii\web\Controller
|
||||
{
|
||||
//替代常规的_construct 析构函数;其他方法调用前执行
|
||||
public function init()
|
||||
{
|
||||
parent::init();
|
||||
$session = Yii::$app->session;
|
||||
if(isset($_GET['pid']) && (int)$_GET['pid']>0){
|
||||
$session->set('pid',(int)$_GET['pid']);
|
||||
}
|
||||
$pid = $session->get('pid');
|
||||
if(!$pid){
|
||||
return Yii::$app->response->redirect(['index/index']);
|
||||
}
|
||||
}
|
||||
|
||||
//主界面
|
||||
public function actionIndex()
|
||||
{
|
||||
return $this->render('../layouts/dev',[]);
|
||||
}
|
||||
|
||||
//车辆列表
|
||||
public function actionList()
|
||||
{
|
||||
$pid = Yii::$app->session->get('pid');
|
||||
$session = Yii::$app->session;
|
||||
$AcCar = new AcCar();
|
||||
$where = $where2 = [];
|
||||
$where['pid'] = $pid;
|
||||
$post = Yii::$app->request->post();
|
||||
$search = [];
|
||||
$searchSession = 'cars::search';
|
||||
if(isset($_GET['reset']) && $_GET['reset']==1){
|
||||
$post['search'] = [];
|
||||
}
|
||||
if(isset($post['search'])){
|
||||
$search = $post['search'];
|
||||
$session->set($searchSession,$search);
|
||||
// }elseif($session->get($searchSession)){
|
||||
// $search = $session->get($searchSession);
|
||||
}
|
||||
|
||||
$search['title'] = isset($search['title']) ? trim(htmlspecialchars_decode($search['title'])) : '';
|
||||
if(!empty($search['title'])){
|
||||
$where2[] = 'or';
|
||||
$where2[] = ['LIKE','num_left',$search['title']];
|
||||
$where2[] = ['LIKE','num_frame',$search['title']];
|
||||
}
|
||||
$search['brand'] = isset($search['brand_id']) ? trim(htmlspecialchars_decode($search['brand'])) : '';
|
||||
$search['brand_id'] = isset($search['brand_id']) ? (int)$search['brand_id'] : 0;
|
||||
if(!empty($search['brand_id'])){
|
||||
$where['brand_id'] = $search['brand_id'];
|
||||
}
|
||||
$where['deleted'] = 0;
|
||||
$data = $AcCar->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)->orderby('id DESC')->all();
|
||||
|
||||
$oids = $mids = $bids = $sids = [];
|
||||
if($res){
|
||||
foreach($res as $r){
|
||||
$oids[] = $r->owner_id;
|
||||
$mids[] = $r->mid;
|
||||
$bids[] = $r->brand_id;
|
||||
$sids[] = $r->series_id;
|
||||
}
|
||||
}
|
||||
$status = Yii::$app->params['CommonStatus'];
|
||||
$AcCarOwner = new AcCarOwner();
|
||||
$where = [];
|
||||
$where['pid'] = $pid;
|
||||
$_owners = $AcCarOwner->find()->where($where)->andwhere(['in','id',$oids])->all();
|
||||
$owners = [];
|
||||
foreach($_owners as $s){
|
||||
$owners[$s->id] = $s->toArray();
|
||||
}
|
||||
$User = new Fans();
|
||||
$where = [];
|
||||
$_users = $User->find()->where(['in','id',$mids])->all();
|
||||
$users = [];
|
||||
foreach($_users as $s){
|
||||
$users[$s->id] = $s->toArray();
|
||||
}
|
||||
|
||||
$AcCarSeries = new AcCarSeries();
|
||||
$where = [];
|
||||
$series = [];
|
||||
$_series = $AcCarSeries->find()->where($where)->andwhere(['in','id',$sids])->all();
|
||||
if($_series){
|
||||
foreach($_series as $r){
|
||||
$series[$r->id] = $r->toArray();
|
||||
}
|
||||
}
|
||||
|
||||
$AcCarBrand = new AcCarBrand();
|
||||
$where = [];
|
||||
$_brands = $AcCarBrand->find()->where($where)->andwhere(['in','id',$bids])->all();
|
||||
if($_brands){
|
||||
foreach($_brands as $r){
|
||||
$brands[$r->id] = $r->toArray();
|
||||
}
|
||||
}
|
||||
$cars = [];
|
||||
if($res){
|
||||
foreach($res as $r){
|
||||
$car = $r->toArray();
|
||||
$t = '';
|
||||
if(isset($series[$r->series_id]['title'])){
|
||||
$t .= $brands[$r->brand_id]['title'];
|
||||
}
|
||||
$t .= '-';
|
||||
if(isset($series[$r->series_id]['title'])){
|
||||
$t .= $series[$r->series_id]['title'];
|
||||
}
|
||||
$car['title2'] = $t;
|
||||
$car['numbers'] = $car['num_p'].$car['num_area'].$car['num_left'];
|
||||
$car['numbers'] = $car['numbers']?$car['numbers']:$car['num_frame'];
|
||||
$cars[$r->id] = $car;
|
||||
}
|
||||
}
|
||||
|
||||
return $this->render('list',[
|
||||
'cars'=>$cars,
|
||||
'pager' => $pages,
|
||||
'owners'=>$owners,
|
||||
'users'=>$users,
|
||||
'status'=>$status,
|
||||
'search'=>$search
|
||||
]);
|
||||
}
|
||||
|
||||
public function actionSelect()
|
||||
{
|
||||
$pid = Yii::$app->session->get('pid');
|
||||
$session = Yii::$app->session;
|
||||
$AcCar = new AcCar();
|
||||
$where = $where2 = [];
|
||||
$where['pid'] = $pid;
|
||||
$post = Yii::$app->request->post();
|
||||
$search = [];
|
||||
$searchSession = 'cars::search';
|
||||
if(isset($_GET['reset']) && $_GET['reset']==1){
|
||||
$post['search'] = [];
|
||||
}
|
||||
if(isset($post['search'])){
|
||||
$search = $post['search'];
|
||||
$session->set($searchSession,$search);
|
||||
// }elseif($session->get($searchSession)){
|
||||
// $search = $session->get($searchSession);
|
||||
}
|
||||
|
||||
$search['title'] = isset($search['title']) ? trim(htmlspecialchars_decode($search['title'])) : '';
|
||||
if(!empty($search['title'])){
|
||||
$where2[] = 'or';
|
||||
$where2[] = ['LIKE','num_left',$search['title']];
|
||||
$where2[] = ['LIKE','num_frame',$search['title']];
|
||||
}
|
||||
$search['brand'] = isset($search['brand_id']) ? trim(htmlspecialchars_decode($search['brand'])) : '';
|
||||
$search['brand_id'] = isset($search['brand_id']) ? (int)$search['brand_id'] : 0;
|
||||
if(!empty($search['brand_id'])){
|
||||
$where['brand_id'] = $search['brand_id'];
|
||||
}
|
||||
$where['deleted'] = 0;
|
||||
$data = $AcCar->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)->orderby(' id DESC')->all();
|
||||
|
||||
$oids = $mids = $bids = $sids = [];
|
||||
if($res){
|
||||
foreach($res as $r){
|
||||
$oids[] = $r->owner_id;
|
||||
$mids[] = $r->mid;
|
||||
$bids[] = $r->brand_id;
|
||||
$sids[] = $r->series_id;
|
||||
}
|
||||
}
|
||||
|
||||
$status = Yii::$app->params['CommonStatus'];
|
||||
$AcCarOwner = new AcCarOwner();
|
||||
$where = [];
|
||||
$where['pid'] = $pid;
|
||||
$_owners = $AcCarOwner->find()->where($where)->andwhere(['in','id',$oids])->all();
|
||||
$owners = [];
|
||||
foreach($_owners as $s){
|
||||
$owners[$s->id] = $s->toArray();
|
||||
}
|
||||
$User = new Fans();
|
||||
$where = [];
|
||||
$_users = $User->find()->where(['in','id',$mids])->all();
|
||||
$users = [];
|
||||
foreach($_users as $s){
|
||||
$users[$s->id] = $s->toArray();
|
||||
}
|
||||
|
||||
$AcCarSeries = new AcCarSeries();
|
||||
$where = [];
|
||||
$series = [];
|
||||
$_series = $AcCarSeries->find()->where($where)->andwhere(['in','id',$sids])->all();
|
||||
if($_series){
|
||||
foreach($_series as $r){
|
||||
$series[$r->id] = $r->toArray();
|
||||
}
|
||||
}
|
||||
|
||||
$AcCarBrand = new AcCarBrand();
|
||||
$where = [];
|
||||
$_brands = $AcCarBrand->find()->where($where)->andwhere(['in','id',$bids])->all();
|
||||
if($_brands){
|
||||
foreach($_brands as $r){
|
||||
$brands[$r->id] = $r->toArray();
|
||||
}
|
||||
}
|
||||
$cars = [];
|
||||
if($res){
|
||||
foreach($res as $r){
|
||||
$car = $r->toArray();
|
||||
$t = '';
|
||||
if(isset($series[$r->series_id]['title'])){
|
||||
$t .= $brands[$r->brand_id]['title'];
|
||||
}
|
||||
$t .= '-';
|
||||
if(isset($series[$r->series_id]['title'])){
|
||||
$t .= $series[$r->series_id]['title'];
|
||||
}
|
||||
$car['title2'] = $t;
|
||||
$car['numbers'] = $car['num_p'].$car['num_area'].$car['num_left'];
|
||||
$car['numbers'] = $car['numbers']?$car['numbers']:$car['num_frame'];
|
||||
$cars[$r->id] = $car;
|
||||
}
|
||||
}
|
||||
|
||||
$callback = !empty(Yii::$app->request->get('callback')) ? Yii::$app->request->get('callback') : 'truck';
|
||||
return $this->render('select',[
|
||||
'cars'=>$cars,
|
||||
'pager' => $pages,
|
||||
'owners'=>$owners,
|
||||
'users'=>$users,
|
||||
'status'=>$status,
|
||||
'search'=>$search,
|
||||
'callback'=>$callback
|
||||
]);
|
||||
}
|
||||
|
||||
public function actionNew()
|
||||
{
|
||||
$status = Yii::$app->params['CommonStatus'];
|
||||
$AcCar = new AcCar();
|
||||
$AcCar->status_code = 0;
|
||||
return $this->render('modify',['status'=>$status,'detail'=>$AcCar]);
|
||||
}
|
||||
|
||||
//编辑客车资料
|
||||
public function actionEdit()
|
||||
{
|
||||
$model = new AcCar();
|
||||
$id = Yii::$app->request->get('id');
|
||||
$res = $model->find()->where(['id' => $id])->one();
|
||||
if($res){
|
||||
$res = $res->toArray();
|
||||
}
|
||||
$pid = Yii::$app->session->get('pid');
|
||||
|
||||
$AcCarOwner = new AcCarOwner();
|
||||
$owner = $AcCarOwner->find()->where(['id' => $res['owner_id']])->one();
|
||||
if($owner){
|
||||
$mid = $owner->mid;
|
||||
$user = Fans::findIdentity($mid);
|
||||
if($user){
|
||||
$res['owner_title'] = $user->mobile;
|
||||
}
|
||||
}
|
||||
$AcCarSeries = new AcCarSeries();
|
||||
$series = $AcCarSeries->find()->where(['id' => $res['series_id']])->one();
|
||||
if($series){
|
||||
$res['series_title'] = $series->title;
|
||||
}
|
||||
$AcCarBrand = new AcCarBrand();
|
||||
$brand = $AcCarBrand->find()->where(['id' => $res['brand_id']])->one();
|
||||
if($brand){
|
||||
$res['brand_title'] = $brand->title;
|
||||
}
|
||||
$status = Yii::$app->params['CommonStatus'];
|
||||
return $this->render('modify',['status'=>$status,'detail'=>$res]);
|
||||
}
|
||||
|
||||
//复制客车资料后新建
|
||||
public function actionCopy()
|
||||
{
|
||||
$model = new AcCar();
|
||||
$id = Yii::$app->request->get('id');
|
||||
$res = $model->find()->where(['id' => $id])->one();
|
||||
if($res){
|
||||
$res = $res->toArray();
|
||||
}
|
||||
$pid = Yii::$app->session->get('pid');
|
||||
|
||||
$AcCarSeries = new AcCarSeries();
|
||||
$series = $AcCarSeries->find()->where(['id' => $res['series_id']])->one();
|
||||
if($series){
|
||||
$res['series_title'] = $series->title;
|
||||
}
|
||||
$AcCarBrand = new AcCarBrand();
|
||||
$brand = $AcCarBrand->find()->where(['id' => $res['brand_id']])->one();
|
||||
if($brand){
|
||||
$res['brand_title'] = $brand->title;
|
||||
}
|
||||
$status = Yii::$app->params['CommonStatus'];
|
||||
$res['id'] = 0;
|
||||
$res['num_p'] = $res['num_area'] = $res['num_left'] = $res['num_frame'] = '';
|
||||
return $this->render('modify',['status'=>$status,'detail'=>$res]);
|
||||
}
|
||||
|
||||
//浏览客车详情
|
||||
public function actionDetail()
|
||||
{
|
||||
$model = new AcCar();
|
||||
$id = Yii::$app->request->get('id');
|
||||
$res = $model->find()->where(['id' => $id])->one();
|
||||
if($res){
|
||||
$res = $res->toArray();
|
||||
}
|
||||
$pid = Yii::$app->session->get('pid');
|
||||
|
||||
$AcCarOwner = new AcCarOwner();
|
||||
$owner = $AcCarOwner->find()->where(['id' => $res['owner_id']])->one();
|
||||
$user = Fans::findIdentity($mid);
|
||||
if($user){
|
||||
$res['owner_title'] = $user->mobile;
|
||||
}
|
||||
|
||||
$AcCarSeries = new AcCarSeries();
|
||||
$series = $AcCarSeries->find()->where(['id' => $res['series_id']])->one();
|
||||
if($series){
|
||||
$res['series_title'] = $series->title;
|
||||
}
|
||||
$AcCarBrand = new AcCarBrand();
|
||||
$brand = $AcCarBrand->find()->where(['id' => $res['brand_id']])->one();
|
||||
if($brand){
|
||||
$res['brand_title'] = $brand->title;
|
||||
}
|
||||
$status = Yii::$app->params['CommonStatus'];
|
||||
return $this->render('detail',['status'=>$status,'detail'=>$res]);
|
||||
}
|
||||
|
||||
//保存车辆资料
|
||||
public function actionSave()
|
||||
{
|
||||
$post = Yii::$app->request->post();
|
||||
$id = (int)$post['id'];
|
||||
$pid = Yii::$app->session->get('pid');
|
||||
//格式化数据
|
||||
//目标键=》POST键
|
||||
$cols = ['title'=>'title','num_frame'=>'num_frame','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]) : '';
|
||||
}
|
||||
//查询车牌号是否在库
|
||||
$hasCar = false;
|
||||
if(!$data['num_frame']){
|
||||
$hasCar = AcCar::find()->where(['num_p'=>$data['num_p'],'num_area'=>$data['num_area'],'num_left'=>$data['num_left']])->one();
|
||||
}
|
||||
|
||||
if($hasCar){
|
||||
if($id && $id!=$hasCar->id){
|
||||
\Yii::$app->getSession()->setFlash('danger', '保存失败,该车牌号已在库!');
|
||||
return $this->goBack();
|
||||
}else{
|
||||
$id = $hasCar->id;
|
||||
}
|
||||
}
|
||||
|
||||
$cols = ['brand_id'=>'brand_id','series_id'=>'series_id','owner_id'=>'owner_id','status_code'=>'status_code','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;
|
||||
|
||||
//填补mid参数
|
||||
$AcCarOwner = new AcCarOwner();
|
||||
$owner = $AcCarOwner->findOne($data['owner_id']);
|
||||
if($owner){
|
||||
$data['mid'] = $owner->mid;
|
||||
}
|
||||
|
||||
//保存车辆资料
|
||||
$AcCar = new AcCar();
|
||||
foreach($data as $key=>$val){
|
||||
$AcCar->$key = $val;
|
||||
}
|
||||
|
||||
if($id){
|
||||
$AcCar->id = $id;
|
||||
unset($data['create_at']);
|
||||
$res = $AcCar->updateAll($data,['id'=>$id]);
|
||||
}else{
|
||||
$res = $AcCar->save();
|
||||
$id = $AcCar->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 AcCar();
|
||||
foreach($data as $key=>$val){
|
||||
$model->$key = $val;
|
||||
}
|
||||
|
||||
$model->id = $id;
|
||||
$res = $model->updateAll($data,$where);
|
||||
|
||||
$return = [];
|
||||
$return['url'] = Url::toRoute('car/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 AcCar();
|
||||
$res = $model->find()->where(['id' => $id])->one();
|
||||
$return = [];
|
||||
$return['timeout'] = 1; //3秒后自动跳转
|
||||
// $return['status_code'] = 0;
|
||||
$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;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
exit(json_encode($return,JSON_UNESCAPED_UNICODE));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user