123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474 |
- <?php
- namespace app\modules\car\controllers;
- use app\common\controllers\BController;
- use app\modules\car\models\FCarInfo;
- use app\modules\car\models\FDataImport;
- use app\modules\car\models\FInputPolicy;
- use app\modules\car\models\FSpcCar;
- use Yii;
- use yii\helpers\FileHelper;
- use yii\web\Response;
- use yii\web\UploadedFile;
- class BaseinfoController extends BController
- {
- public $layout = 'main';
- public function actionCelue()
- {
- if (Yii::$app->request->isAjax) {
- $query = FInputPolicy::find();
- $data = [];
- $params = Yii::$app->request->get('FInputPolicy',[]);
- $query = mergeParams($query,$params);
- $countQuery = clone $query;
- //分页
- if(isset($_GET['limit'])){ $query->limit(intval($_GET['limit']));}
- if(isset($_GET['offset'])){ $query->offset(intval($_GET['offset']));}
- //排序
- if(isset($_GET['sort'])&&isset($_GET['sortOrder']))
- {
- $sortWay = $_GET['sortOrder']=='asc'?SORT_ASC:SORT_DESC;
- $resultList = $query->orderBy([$_GET['sort']=>$sortWay])->all();
- }
- else
- {
- $resultList = $query->all();
- }
- foreach($resultList as $result)
- {
- $data[] = array('id'=>$result->id,'creator'=>$result->creator,'a_time'=>$result->a_time,'m_time'=>$result->m_time,'is_active'=>$result->is_active);
- }
- $result = ["total"=>$countQuery->count(),"totalNotFiltered"=>$countQuery->count(),"rows"=>$data];
- echo_json($result);
- }
- $this->tableTitle = array(
- array('field'=>'id','title'=>'ID','align'=>'center','sortable'=>true,'class'=>'col-md-2'),
- array('field'=>'creator','title'=>'创建人','align'=>'center','class'=>'col-md-2'),
- array('field'=>'a_time','title'=>'创建时间','align'=>'center','class'=>'col-md-2'),
- array('field'=>'m_time','title'=>'修改时间','align'=>'center','class'=>'col-md-2'),
- array('field'=>'is_active','title'=>'状态','align'=>'center','formatter'=>'switchFormatter','class'=>'col-md-2'),
- array('field'=>'operate','title'=>'操作','align'=>'center','events'=>'window.operateEvents','formatter'=>'operateFormatter','class'=>'col-md-2'),
- );
- $this->tableConfig = array('table'=>'f_input_policy','url'=>$this->createRealUrl(['car/baseinfo/celue']),'setFieldUrl'=>$this->createRealUrl(['car/baseinfo/setfield']),'idField'=>FInputPolicy::modelPrimaryKey(),'checkbox'=>0,'dropmenu'=>1,'pagination'=>true,'pagesize'=>20,'refresh'=>true);
- return $this->render('celue',array('model'=>new FInputPolicy()));
- }
- function actionCelueadd()
- {
- $model = new FInputPolicy();
- if(Yii::$app->request->isAjax&&$model->load(Yii::$app->request->post())){
- $post = Yii::$app->request->post();
- $model->is_active = $post['FInputPolicy']['is_active']?$post['FInputPolicy']['is_active']:0;
- $model->a_time = $model->m_time = date('Y-m-d H:i:s');
- $model->creator = $this->getIdentityInfo('real_name');
- if(!$model->validate())
- {
- $msgdata = ['error' => 1,'msg' => $model->returnFirstError()];
- }
- else
- {
- if($model->save())
- {
- $msgdata = ['error' => 0,'msg' => '操作成功'];
- }
- else
- {
- $msgdata = ['error' => 1,'msg' => '操作失败'];
- }
- }
- echo_json($msgdata);
- }
- return $this->renderAjax('celueadd',array('model'=>$model));
- }
- function actionCelueedit()
- {
- $id = $this->getKeyId('id');
- $model = FInputPolicy::findOne($id);
- check_record_exists($model);
- if(Yii::$app->request->isAjax&&$model->load(Yii::$app->request->post())){
- $post = Yii::$app->request->post();
- $model->is_active = $post['FInputPolicy']['is_active']?$post['FInputPolicy']['is_active']:0;
- $model->creator = $this->getIdentityInfo('real_name');
- $model->m_time = date('Y-m-d H:i:s');
- if(!$model->validate())
- {
- $msgdata = ['error' => 1,'msg' => $model->returnFirstError()];
- }
- else
- {
- if($model->save(false))
- {
- $msgdata = ['error' => 0,'msg' => '操作成功'];
- }
- else
- {
- $msgdata = ['error' => 1,'msg' => '操作失败'];
- }
- }
- echo_json($msgdata);
- }
- return $this->renderAjax('celueedit',array('model'=>$model));
- }
- function actionCeluedetail()
- {
- $id = $this->getKeyId('id');
- $model = FInputPolicy::findOne($id);
- check_record_exists($model);
- return $this->renderAjax('celuedetail',array('model'=>$model));
- }
- //删除
- public function actionCeluedel()
- {
- $id = $this->getKeyId('id');
- $model = FInputPolicy::findOne($id);
- check_record_exists($model);
- if($model->delete())
- {
- $msgdata = ['error' => 0,'msg' => '操作成功!'];
- }
- else
- {
- $msgdata = ['error' => 1,'msg' => '操作失败!'];
- }
- echo_json($msgdata);
- }
- public function actionFuzeren()
- {
- $query = FCarInfo::find();
- if (Yii::$app->request->isAjax) {
- $data = [];
- $params = Yii::$app->request->get();
- $where = $this->getWhere($query, $params);
- if(!empty($where)){
- $sql = join(" and ",$where);
- $query->where($sql);
- }
- $countQuery = clone $query;
- //分页
- if(isset($_GET['limit'])){ $query->limit(intval($_GET['limit']));}
- if(isset($_GET['offset'])){ $query->offset(intval($_GET['offset']));}
- //排序
- if(isset($_GET['sort'])&&isset($_GET['sortOrder']))
- {
- $resultList = $query->select('*')->orderBy([$_GET['sort']=>($_GET['sortOrder']=='asc'?SORT_ASC:SORT_DESC)])->asArray()->all();
- }
- else
- {
- $resultList = $query->select('*')->orderBy(['car_id'=>SORT_DESC])->asArray()->all();
- }
- foreach($resultList as $result)
- {
- $data[] = array('car_id'=>$result['car_id'],'card_num'=>$result['card_num'],'city'=>$result['city'],'dpt_sec'=>$result['dpt_sec'],'grid'=>$result['grid'],'self_rent'=>$result['self_rent'],'car_type'=>$result['car_type'],'using_tag'=>$result['using_tag'],'rps_dpt'=>$result['rps_dpt'],'rps_man'=>$result['rps_man'],'cell_num'=>$result['cell_num'],'is_special_car'=>$result['is_special_car']);
- }
- $result = ["total" => $countQuery->count(), "totalNotFiltered" => $countQuery->count(), "rows" => $resultList];
- echo_json($result);
- }
- $this->tableTitle = array(
- array('field'=>'card_num','title'=>'车牌号','align'=>'center'),
- array('field'=>'city','title'=>'单位','align'=>'center'),
- array('field'=>'dpt_sec','title'=>'二级单位','align'=>'center'),
- array('field'=>'grid','title'=>'三级单位','align'=>'center'),
- array('field'=>'own_dpt','title'=>'所属单位','align'=>'center'),
- array('field'=>'rps_man','title'=>'负责人姓名','align'=>'center','formatter'=>'editFormatter'),
- array('field'=>'cell_num','title'=>'电话','align'=>'center','formatter'=>'editFormatter'),
- );
- //$this->tableConfig = array('table'=>'f_car_info','url'=>$this->createRealUrl(['car/baseinfo/fuzeren']),'setFieldUrl'=>$this->createRealUrl(['car/baseinfo/setfield']),'idField'=>'car_id','checkbox'=>0,'dropmenu'=>1,'pagination'=>true,'pagesize'=>20,'refresh'=>true,'height'=>500);
- $this->tableConfig = array('table' => 'f_car_info', 'url' => $this->createRealUrl(['car/baseinfo/fuzeren']), 'setFieldUrl' => $this->createRealUrl(['car/baseinfo/setfield']), 'idField' => 'car_id', 'checkbox' => 0, 'dropmenu' => 1, 'pagination' => true, 'pagesize' => 20, 'refresh' => true, 'height' => 500, 'tableClass' => 'table-nowrap');
- return $this->render('fuzeren',array('model'=>new FCarInfo()));
- }
- //特殊车辆
- public function actionSpecar()
- {
- if (Yii::$app->request->isAjax) {
- $query = FSpcCar::find();
- $data = [];
- $params = Yii::$app->request->get('FSpcCar',[]);
- $query = mergeParams($query,$params);
- $countQuery = clone $query;
- //分页
- if(isset($_GET['limit'])){ $query->limit(intval($_GET['limit']));}
- if(isset($_GET['offset'])){ $query->offset(intval($_GET['offset']));}
- //排序
- if(isset($_GET['sort'])&&isset($_GET['sortOrder']))
- {
- $sortWay = $_GET['sortOrder']=='asc'?SORT_ASC:SORT_DESC;
- $resultList = $query->orderBy([$_GET['sort']=>$sortWay])->all();
- }
- else
- {
- $resultList = $query->all();
- }
- foreach($resultList as $result)
- {
- $data[] = array('id'=>$result->id,'card_num'=>$result->card_num,'is_spec'=>$result->is_spec,'bak'=>$result->bak,'dept'=>$result->dept);
- }
- $result = ["total"=>$countQuery->count(),"totalNotFiltered"=>$countQuery->count(),"rows"=>$data];
- echo_json($result);
- }
- $this->tableTitle = array(
- array('field'=>'id','title'=>FSpcCar::getAttributeName('id'),'align'=>'center','sortable'=>true,'class'=>'col-md-1'),
- array('field'=>'card_num','title'=>FSpcCar::getAttributeName('card_num'),'align'=>'center','class'=>'col-md-2'),
- array('field'=>'is_spec','title'=>FSpcCar::getAttributeName('is_spec'),'align'=>'center','formatter'=>'switchFormatter','class'=>'col-md-2'),
- array('field'=>'bak','title'=>FSpcCar::getAttributeName('bak'),'align'=>'center','class'=>'col-md-2'),
- array('field'=>'dept','title'=>FSpcCar::getAttributeName('dept'),'align'=>'center','class'=>'col-md-2'),
- array('field'=>'operate','title'=>FSpcCar::getAttributeName('operate'),'align'=>'center','events'=>'window.operateEvents','formatter'=>'operateFormatter','class'=>'col-md-2'),
- );
- $this->tableConfig = array('table'=>'f_spc_car','url'=>$this->createRealUrl(['car/baseinfo/specar']),'setFieldUrl'=>$this->createRealUrl(['car/baseinfo/setfield']),'idField'=>FSpcCar::modelPrimaryKey(),'checkbox'=>0,'dropmenu'=>1,'pagination'=>true,'pagesize'=>20,'refresh'=>true);
- return $this->render('spelist',array('model'=>new FSpcCar()));
- }
- function actionAddspec()
- {
- $model = new FSpcCar();
- if(Yii::$app->request->isAjax&&$model->load(Yii::$app->request->post())){
- if(!$model->validate())
- {
- $msgdata = ['error' => 1,'msg' => $model->returnFirstError()];
- }
- else
- {
- if($model->save())
- {
- $msgdata = ['error' => 0,'msg' => '操作成功'];
- }
- else
- {
- $msgdata = ['error' => 1,'msg' => '操作失败'];
- }
- }
- echo_json($msgdata);
- }
- return $this->renderAjax('addspec',array('model'=>$model));
- }
- function actionEditspec()
- {
- $id = $this->getKeyId();
- $model = FSpcCar::findOne($id);
- check_record_exists($model);
- if(Yii::$app->request->isAjax&&$model->load(Yii::$app->request->post())){
- if(!$model->validate())
- {
- $msgdata = ['error' => 1,'msg' => $model->returnFirstError()];
- }
- else
- {
- if($model->save())
- {
- $msgdata = ['error' => 0,'msg' => '操作成功'];
- }
- else
- {
- $msgdata = ['error' => 1,'msg' => '操作失败'];
- }
- }
- echo_json($msgdata);
- }
- return $this->renderAjax('addspec',array('model'=>$model));
- }
- //删除
- public function actionDelspec()
- {
- $id = $this->getKeyId();
- $model = FSpcCar::findOne($id);
- check_record_exists($model);
- if($model->delete())
- {
- $msgdata = ['error' => 0,'msg' => '操作成功!'];
- }
- else
- {
- $msgdata = ['error' => 1,'msg' => '操作失败!'];
- }
- echo_json($msgdata);
- }
- //数据导入查询
- public function actionDataimport()
- {
- $statusOption = [
- '-1' => '导入失败',
- '0' => '上传成功',
- '1' => '导入成功',
- ];
- if (Yii::$app->request->isAjax) {
- $query = FDataImport::find();
- $data = [];
- $params = Yii::$app->request->get();
- $where = [];
- if(!empty($params['template'])){
- $where['template'] = $params['template'];
- }
- if(!empty($params['statistical_month'])){
- $where['statistical_month'] = str_replace('-','',$params['statistical_month']);
- }
- $query->where($where);
- $countQuery = clone $query;
- //分页
- if(isset($_GET['limit'])){ $query->limit(intval($_GET['limit']));}
- if(isset($_GET['offset'])){ $query->offset(intval($_GET['offset']));}
- //排序
- if(isset($_GET['sort'])&&isset($_GET['sortOrder']))
- {
- $sortWay = $_GET['sortOrder']=='asc'?SORT_ASC:SORT_DESC;
- $resultList = $query->orderBy([$_GET['sort']=>$sortWay])->asArray()->all();
- }
- else
- {
- $resultList = $query->orderBy(['a_id'=>SORT_DESC])->asArray()->all();
- }
- foreach($resultList as $result)
- {
- $data[] = ['file_name'=>$result['file_name'],'file_path'=>UPLOAD_URL.$result['file_path'],'template'=>$result['template'],'statistical_month'=>$result['statistical_month'],'upload_time'=>$result['upload_time'],'status'=>$statusOption[$result['status']],'remark'=>$result['remark']];
- }
- $result = ["total"=>$countQuery->count(),"totalNotFiltered"=>$countQuery->count(),"rows"=>$data];
- echo_json($result);
- }
- $this->tableTitle = array(
- array('field'=>'template','title'=>'导入模板','align'=>'center','sortable'=>true),
- array('field'=>'statistical_month','title'=>'导入账期','align'=>'center','sortable'=>true),
- array('field'=>'upload_time','title'=>'上传时间','align'=>'center','sortable'=>true),
- array('field'=>'file_name','title'=>'文件名','align'=>'center','sortable'=>true),
- array('field'=>'status','title'=>'状态','align'=>'center','sortable'=>true),
- array('field'=>'remark','title'=>'失败原因','align'=>'center','sortable'=>true),
- );
- $this->tableConfig = array('table'=>'f_data_import','url'=>$this->createRealUrl(['car/baseinfo/dataimport']),'setFieldUrl'=>$this->createRealUrl(['car/baseinfo/setfield']),'idField'=>'a_id','checkbox'=>0,'dropmenu'=>1,'pagination'=>true,'pagesize'=>20,'refresh'=>true);
- return $this->render('dataimport',array('model'=>new FSpcCar()));
- }
- //数据上传
- public function actionUpload()
- {
- if (Yii::$app->request->isPost) {
- Yii::$app->response->format = Response::FORMAT_JSON;
- //try {
- //按名称获取实例
- $file = UploadedFile::getInstanceByName('file');
- if (empty($file)) {
- return ['code' => 1, 'msg' => '上传失败,没有文件!'];
- }
- //是否有错误
- if ($file->getHasError()) {
- return ['code' => 1, 'msg' => '文件异常!'];
- }
- //1M
- $default = 1048576;
- //匹配文件后缀
- $extension = $file->getExtension();
- if (!in_array($extension, ['xls','xlsx','csv'])) {
- return ['code' => 1, 'msg' => '暂不支持上传' . $extension . '文件'];
- }
- if ($file->size > 50 * $default) {
- return ['code' => 1, 'msg' => '上传文件过大!'];
- }
- $params = Yii::$app->request->post();
- if(empty($params['template'])){
- return ['code' => 1, 'msg' => '请选择上传模板!'];
- }
- if(empty($params['statistical_month'])){
- return ['code' => 1, 'msg' => '请选择导入账期!'];
- }
- $params['statistical_month'] = safe_replace(str_replace('-','',$params['statistical_month']));
- if(strpos($file->name,$params['template'])===false){
- return ['code' => 1, 'msg' => '导入文件名与导入模板名称不匹配,导入文件名需包含导入模板名称('.$params['template'].')!'];
- }
- if($res = FDataImport::find()->where(['template'=>$params['template'],'statistical_month'=>$params['statistical_month']])->one()){
- return ['code' => 1, 'msg' => $params['template'].','.$params['statistical_month'].'账期已导入,请联系管理员处理!'];
- }
- $childPath = 'car-data'.DIRECTORY_SEPARATOR.date('Ymd',TIMESTAMP).DIRECTORY_SEPARATOR;
- $targetPath = UPLOAD_PATH.$childPath;
- $fileName = get_unique_file_name($targetPath,$file->extension);//文件重命名
- if (!is_dir($targetPath)) {
- FileHelper::createDirectory($targetPath);
- }
- $targetFile = $targetPath . $fileName;
- $filePath = str_replace(DIRECTORY_SEPARATOR,'/',$childPath.$fileName);
- $fileUrl = UPLOAD_URL.$filePath;
- if ($file->saveAs($targetFile)) {
- if (!file_exists($targetFile)) {
- return ['code' => 1, 'msg' => '文件保存失败!'];
- }
- //写入附件表
- $attachment = new FDataImport();
- $attachment->file_name = $file->name;
- $attachment->file_path = $filePath;
- $attachment->file_size = $file->size;
- $attachment->file_ext = $file->extension;
- $attachment->hash = md5($filePath);
- $attachment->upload_time = date('Y-m-d H:i:s');
- $attachment->upload_ip = self::getClientIp();
- $attachment->template = $params['template'];
- $attachment->statistical_month = $params['statistical_month'];
- $attachment->status = 0;
- $attachment->save();
- return [
- 'code' => 0,
- 'url' =>$fileUrl,
- 'attachment' => $filePath,
- 'name'=>$file->name,
- 'size'=>$file->size
- ];
- }
- return ['code' => 1, 'msg' => '文件保存异常!'];
- //} catch (Exception $e) {
- //return ['code' => 1, 'msg' => '调用失败'];
- //}
- }
- return ['code' => 1, 'msg' => '没有文件!'];
- }
- /**获取客户端ip
- * @return string
- */
- protected function getClientIp ()
- {
- if (getenv('HTTP_CLIENT_IP')) {
- $ip = getenv('HTTP_CLIENT_IP');
- } else if (getenv('HTTP_X_FORWARDED_FOR')) {
- $ip = getenv('HTTP_X_FORWARDED_FOR');
- } else if (getenv('REMOTE_ADDR')) {
- $ip = getenv('REMOTE_ADDR');
- } else {
- $ip = $_SERVER['REMOTE_ADDR'];
- }
- return $ip;
- }
- }
|