BaseinfoController.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. <?php
  2. namespace app\modules\car\controllers;
  3. use app\common\controllers\BController;
  4. use app\modules\car\models\FCarInfo;
  5. use app\modules\car\models\FDataImport;
  6. use app\modules\car\models\FInputPolicy;
  7. use app\modules\car\models\FSpcCar;
  8. use Yii;
  9. use yii\helpers\FileHelper;
  10. use yii\web\Response;
  11. use yii\web\UploadedFile;
  12. class BaseinfoController extends BController
  13. {
  14. public $layout = 'main';
  15. public function actionCelue()
  16. {
  17. if (Yii::$app->request->isAjax) {
  18. $query = FInputPolicy::find();
  19. $data = [];
  20. $params = Yii::$app->request->get('FInputPolicy',[]);
  21. $query = mergeParams($query,$params);
  22. $countQuery = clone $query;
  23. //分页
  24. if(isset($_GET['limit'])){ $query->limit(intval($_GET['limit']));}
  25. if(isset($_GET['offset'])){ $query->offset(intval($_GET['offset']));}
  26. //排序
  27. if(isset($_GET['sort'])&&isset($_GET['sortOrder']))
  28. {
  29. $sortWay = $_GET['sortOrder']=='asc'?SORT_ASC:SORT_DESC;
  30. $resultList = $query->orderBy([$_GET['sort']=>$sortWay])->all();
  31. }
  32. else
  33. {
  34. $resultList = $query->all();
  35. }
  36. foreach($resultList as $result)
  37. {
  38. $data[] = array('id'=>$result->id,'creator'=>$result->creator,'a_time'=>$result->a_time,'m_time'=>$result->m_time,'is_active'=>$result->is_active);
  39. }
  40. $result = ["total"=>$countQuery->count(),"totalNotFiltered"=>$countQuery->count(),"rows"=>$data];
  41. echo_json($result);
  42. }
  43. $this->tableTitle = array(
  44. array('field'=>'id','title'=>'ID','align'=>'center','sortable'=>true,'class'=>'col-md-2'),
  45. array('field'=>'creator','title'=>'创建人','align'=>'center','class'=>'col-md-2'),
  46. array('field'=>'a_time','title'=>'创建时间','align'=>'center','class'=>'col-md-2'),
  47. array('field'=>'m_time','title'=>'修改时间','align'=>'center','class'=>'col-md-2'),
  48. array('field'=>'is_active','title'=>'状态','align'=>'center','formatter'=>'switchFormatter','class'=>'col-md-2'),
  49. array('field'=>'operate','title'=>'操作','align'=>'center','events'=>'window.operateEvents','formatter'=>'operateFormatter','class'=>'col-md-2'),
  50. );
  51. $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);
  52. return $this->render('celue',array('model'=>new FInputPolicy()));
  53. }
  54. function actionCelueadd()
  55. {
  56. $model = new FInputPolicy();
  57. if(Yii::$app->request->isAjax&&$model->load(Yii::$app->request->post())){
  58. $post = Yii::$app->request->post();
  59. $model->is_active = $post['FInputPolicy']['is_active']?$post['FInputPolicy']['is_active']:0;
  60. $model->a_time = $model->m_time = date('Y-m-d H:i:s');
  61. $model->creator = $this->getIdentityInfo('real_name');
  62. if(!$model->validate())
  63. {
  64. $msgdata = ['error' => 1,'msg' => $model->returnFirstError()];
  65. }
  66. else
  67. {
  68. if($model->save())
  69. {
  70. $msgdata = ['error' => 0,'msg' => '操作成功'];
  71. }
  72. else
  73. {
  74. $msgdata = ['error' => 1,'msg' => '操作失败'];
  75. }
  76. }
  77. echo_json($msgdata);
  78. }
  79. return $this->renderAjax('celueadd',array('model'=>$model));
  80. }
  81. function actionCelueedit()
  82. {
  83. $id = $this->getKeyId('id');
  84. $model = FInputPolicy::findOne($id);
  85. check_record_exists($model);
  86. if(Yii::$app->request->isAjax&&$model->load(Yii::$app->request->post())){
  87. $post = Yii::$app->request->post();
  88. $model->is_active = $post['FInputPolicy']['is_active']?$post['FInputPolicy']['is_active']:0;
  89. $model->creator = $this->getIdentityInfo('real_name');
  90. $model->m_time = date('Y-m-d H:i:s');
  91. if(!$model->validate())
  92. {
  93. $msgdata = ['error' => 1,'msg' => $model->returnFirstError()];
  94. }
  95. else
  96. {
  97. if($model->save(false))
  98. {
  99. $msgdata = ['error' => 0,'msg' => '操作成功'];
  100. }
  101. else
  102. {
  103. $msgdata = ['error' => 1,'msg' => '操作失败'];
  104. }
  105. }
  106. echo_json($msgdata);
  107. }
  108. return $this->renderAjax('celueedit',array('model'=>$model));
  109. }
  110. function actionCeluedetail()
  111. {
  112. $id = $this->getKeyId('id');
  113. $model = FInputPolicy::findOne($id);
  114. check_record_exists($model);
  115. return $this->renderAjax('celuedetail',array('model'=>$model));
  116. }
  117. //删除
  118. public function actionCeluedel()
  119. {
  120. $id = $this->getKeyId('id');
  121. $model = FInputPolicy::findOne($id);
  122. check_record_exists($model);
  123. if($model->delete())
  124. {
  125. $msgdata = ['error' => 0,'msg' => '操作成功!'];
  126. }
  127. else
  128. {
  129. $msgdata = ['error' => 1,'msg' => '操作失败!'];
  130. }
  131. echo_json($msgdata);
  132. }
  133. public function actionFuzeren()
  134. {
  135. $query = FCarInfo::find();
  136. if (Yii::$app->request->isAjax) {
  137. $data = [];
  138. $params = Yii::$app->request->get();
  139. $where = $this->getWhere($query, $params);
  140. if(!empty($where)){
  141. $sql = join(" and ",$where);
  142. $query->where($sql);
  143. }
  144. $countQuery = clone $query;
  145. //分页
  146. if(isset($_GET['limit'])){ $query->limit(intval($_GET['limit']));}
  147. if(isset($_GET['offset'])){ $query->offset(intval($_GET['offset']));}
  148. //排序
  149. if(isset($_GET['sort'])&&isset($_GET['sortOrder']))
  150. {
  151. $resultList = $query->select('*')->orderBy([$_GET['sort']=>($_GET['sortOrder']=='asc'?SORT_ASC:SORT_DESC)])->asArray()->all();
  152. }
  153. else
  154. {
  155. $resultList = $query->select('*')->orderBy(['car_id'=>SORT_DESC])->asArray()->all();
  156. }
  157. foreach($resultList as $result)
  158. {
  159. $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']);
  160. }
  161. $result = ["total" => $countQuery->count(), "totalNotFiltered" => $countQuery->count(), "rows" => $resultList];
  162. echo_json($result);
  163. }
  164. $this->tableTitle = array(
  165. array('field'=>'card_num','title'=>'车牌号','align'=>'center'),
  166. array('field'=>'city','title'=>'单位','align'=>'center'),
  167. array('field'=>'dpt_sec','title'=>'二级单位','align'=>'center'),
  168. array('field'=>'grid','title'=>'三级单位','align'=>'center'),
  169. array('field'=>'own_dpt','title'=>'所属单位','align'=>'center'),
  170. array('field'=>'rps_man','title'=>'负责人姓名','align'=>'center','formatter'=>'editFormatter'),
  171. array('field'=>'cell_num','title'=>'电话','align'=>'center','formatter'=>'editFormatter'),
  172. );
  173. //$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);
  174. $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');
  175. return $this->render('fuzeren',array('model'=>new FCarInfo()));
  176. }
  177. //特殊车辆
  178. public function actionSpecar()
  179. {
  180. if (Yii::$app->request->isAjax) {
  181. $query = FSpcCar::find();
  182. $data = [];
  183. $params = Yii::$app->request->get('FSpcCar',[]);
  184. $query = mergeParams($query,$params);
  185. $countQuery = clone $query;
  186. //分页
  187. if(isset($_GET['limit'])){ $query->limit(intval($_GET['limit']));}
  188. if(isset($_GET['offset'])){ $query->offset(intval($_GET['offset']));}
  189. //排序
  190. if(isset($_GET['sort'])&&isset($_GET['sortOrder']))
  191. {
  192. $sortWay = $_GET['sortOrder']=='asc'?SORT_ASC:SORT_DESC;
  193. $resultList = $query->orderBy([$_GET['sort']=>$sortWay])->all();
  194. }
  195. else
  196. {
  197. $resultList = $query->all();
  198. }
  199. foreach($resultList as $result)
  200. {
  201. $data[] = array('id'=>$result->id,'card_num'=>$result->card_num,'is_spec'=>$result->is_spec,'bak'=>$result->bak,'dept'=>$result->dept);
  202. }
  203. $result = ["total"=>$countQuery->count(),"totalNotFiltered"=>$countQuery->count(),"rows"=>$data];
  204. echo_json($result);
  205. }
  206. $this->tableTitle = array(
  207. array('field'=>'id','title'=>FSpcCar::getAttributeName('id'),'align'=>'center','sortable'=>true,'class'=>'col-md-1'),
  208. array('field'=>'card_num','title'=>FSpcCar::getAttributeName('card_num'),'align'=>'center','class'=>'col-md-2'),
  209. array('field'=>'is_spec','title'=>FSpcCar::getAttributeName('is_spec'),'align'=>'center','formatter'=>'switchFormatter','class'=>'col-md-2'),
  210. array('field'=>'bak','title'=>FSpcCar::getAttributeName('bak'),'align'=>'center','class'=>'col-md-2'),
  211. array('field'=>'dept','title'=>FSpcCar::getAttributeName('dept'),'align'=>'center','class'=>'col-md-2'),
  212. array('field'=>'operate','title'=>FSpcCar::getAttributeName('operate'),'align'=>'center','events'=>'window.operateEvents','formatter'=>'operateFormatter','class'=>'col-md-2'),
  213. );
  214. $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);
  215. return $this->render('spelist',array('model'=>new FSpcCar()));
  216. }
  217. function actionAddspec()
  218. {
  219. $model = new FSpcCar();
  220. if(Yii::$app->request->isAjax&&$model->load(Yii::$app->request->post())){
  221. if(!$model->validate())
  222. {
  223. $msgdata = ['error' => 1,'msg' => $model->returnFirstError()];
  224. }
  225. else
  226. {
  227. if($model->save())
  228. {
  229. $msgdata = ['error' => 0,'msg' => '操作成功'];
  230. }
  231. else
  232. {
  233. $msgdata = ['error' => 1,'msg' => '操作失败'];
  234. }
  235. }
  236. echo_json($msgdata);
  237. }
  238. return $this->renderAjax('addspec',array('model'=>$model));
  239. }
  240. function actionEditspec()
  241. {
  242. $id = $this->getKeyId();
  243. $model = FSpcCar::findOne($id);
  244. check_record_exists($model);
  245. if(Yii::$app->request->isAjax&&$model->load(Yii::$app->request->post())){
  246. if(!$model->validate())
  247. {
  248. $msgdata = ['error' => 1,'msg' => $model->returnFirstError()];
  249. }
  250. else
  251. {
  252. if($model->save())
  253. {
  254. $msgdata = ['error' => 0,'msg' => '操作成功'];
  255. }
  256. else
  257. {
  258. $msgdata = ['error' => 1,'msg' => '操作失败'];
  259. }
  260. }
  261. echo_json($msgdata);
  262. }
  263. return $this->renderAjax('addspec',array('model'=>$model));
  264. }
  265. //删除
  266. public function actionDelspec()
  267. {
  268. $id = $this->getKeyId();
  269. $model = FSpcCar::findOne($id);
  270. check_record_exists($model);
  271. if($model->delete())
  272. {
  273. $msgdata = ['error' => 0,'msg' => '操作成功!'];
  274. }
  275. else
  276. {
  277. $msgdata = ['error' => 1,'msg' => '操作失败!'];
  278. }
  279. echo_json($msgdata);
  280. }
  281. //数据导入查询
  282. public function actionDataimport()
  283. {
  284. $statusOption = [
  285. '-1' => '导入失败',
  286. '0' => '上传成功',
  287. '1' => '导入成功',
  288. ];
  289. if (Yii::$app->request->isAjax) {
  290. $query = FDataImport::find();
  291. $data = [];
  292. $params = Yii::$app->request->get();
  293. $where = [];
  294. if(!empty($params['template'])){
  295. $where['template'] = $params['template'];
  296. }
  297. if(!empty($params['statistical_month'])){
  298. $where['statistical_month'] = str_replace('-','',$params['statistical_month']);
  299. }
  300. $query->where($where);
  301. $countQuery = clone $query;
  302. //分页
  303. if(isset($_GET['limit'])){ $query->limit(intval($_GET['limit']));}
  304. if(isset($_GET['offset'])){ $query->offset(intval($_GET['offset']));}
  305. //排序
  306. if(isset($_GET['sort'])&&isset($_GET['sortOrder']))
  307. {
  308. $sortWay = $_GET['sortOrder']=='asc'?SORT_ASC:SORT_DESC;
  309. $resultList = $query->orderBy([$_GET['sort']=>$sortWay])->asArray()->all();
  310. }
  311. else
  312. {
  313. $resultList = $query->orderBy(['a_id'=>SORT_DESC])->asArray()->all();
  314. }
  315. foreach($resultList as $result)
  316. {
  317. $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']];
  318. }
  319. $result = ["total"=>$countQuery->count(),"totalNotFiltered"=>$countQuery->count(),"rows"=>$data];
  320. echo_json($result);
  321. }
  322. $this->tableTitle = array(
  323. array('field'=>'template','title'=>'导入模板','align'=>'center','sortable'=>true),
  324. array('field'=>'statistical_month','title'=>'导入账期','align'=>'center','sortable'=>true),
  325. array('field'=>'upload_time','title'=>'上传时间','align'=>'center','sortable'=>true),
  326. array('field'=>'file_name','title'=>'文件名','align'=>'center','sortable'=>true),
  327. array('field'=>'status','title'=>'状态','align'=>'center','sortable'=>true),
  328. array('field'=>'remark','title'=>'失败原因','align'=>'center','sortable'=>true),
  329. );
  330. $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);
  331. return $this->render('dataimport',array('model'=>new FSpcCar()));
  332. }
  333. //数据上传
  334. public function actionUpload()
  335. {
  336. if (Yii::$app->request->isPost) {
  337. Yii::$app->response->format = Response::FORMAT_JSON;
  338. //try {
  339. //按名称获取实例
  340. $file = UploadedFile::getInstanceByName('file');
  341. if (empty($file)) {
  342. return ['code' => 1, 'msg' => '上传失败,没有文件!'];
  343. }
  344. //是否有错误
  345. if ($file->getHasError()) {
  346. return ['code' => 1, 'msg' => '文件异常!'];
  347. }
  348. //1M
  349. $default = 1048576;
  350. //匹配文件后缀
  351. $extension = $file->getExtension();
  352. if (!in_array($extension, ['xls','xlsx','csv'])) {
  353. return ['code' => 1, 'msg' => '暂不支持上传' . $extension . '文件'];
  354. }
  355. if ($file->size > 50 * $default) {
  356. return ['code' => 1, 'msg' => '上传文件过大!'];
  357. }
  358. $params = Yii::$app->request->post();
  359. if(empty($params['template'])){
  360. return ['code' => 1, 'msg' => '请选择上传模板!'];
  361. }
  362. if(empty($params['statistical_month'])){
  363. return ['code' => 1, 'msg' => '请选择导入账期!'];
  364. }
  365. $params['statistical_month'] = safe_replace(str_replace('-','',$params['statistical_month']));
  366. if(strpos($file->name,$params['template'])===false){
  367. return ['code' => 1, 'msg' => '导入文件名与导入模板名称不匹配,导入文件名需包含导入模板名称('.$params['template'].')!'];
  368. }
  369. if($res = FDataImport::find()->where(['template'=>$params['template'],'statistical_month'=>$params['statistical_month']])->one()){
  370. return ['code' => 1, 'msg' => $params['template'].','.$params['statistical_month'].'账期已导入,请联系管理员处理!'];
  371. }
  372. $childPath = 'car-data'.DIRECTORY_SEPARATOR.date('Ymd',TIMESTAMP).DIRECTORY_SEPARATOR;
  373. $targetPath = UPLOAD_PATH.$childPath;
  374. $fileName = get_unique_file_name($targetPath,$file->extension);//文件重命名
  375. if (!is_dir($targetPath)) {
  376. FileHelper::createDirectory($targetPath);
  377. }
  378. $targetFile = $targetPath . $fileName;
  379. $filePath = str_replace(DIRECTORY_SEPARATOR,'/',$childPath.$fileName);
  380. $fileUrl = UPLOAD_URL.$filePath;
  381. if ($file->saveAs($targetFile)) {
  382. if (!file_exists($targetFile)) {
  383. return ['code' => 1, 'msg' => '文件保存失败!'];
  384. }
  385. //写入附件表
  386. $attachment = new FDataImport();
  387. $attachment->file_name = $file->name;
  388. $attachment->file_path = $filePath;
  389. $attachment->file_size = $file->size;
  390. $attachment->file_ext = $file->extension;
  391. $attachment->hash = md5($filePath);
  392. $attachment->upload_time = date('Y-m-d H:i:s');
  393. $attachment->upload_ip = self::getClientIp();
  394. $attachment->template = $params['template'];
  395. $attachment->statistical_month = $params['statistical_month'];
  396. $attachment->status = 0;
  397. $attachment->save();
  398. return [
  399. 'code' => 0,
  400. 'url' =>$fileUrl,
  401. 'attachment' => $filePath,
  402. 'name'=>$file->name,
  403. 'size'=>$file->size
  404. ];
  405. }
  406. return ['code' => 1, 'msg' => '文件保存异常!'];
  407. //} catch (Exception $e) {
  408. //return ['code' => 1, 'msg' => '调用失败'];
  409. //}
  410. }
  411. return ['code' => 1, 'msg' => '没有文件!'];
  412. }
  413. /**获取客户端ip
  414. * @return string
  415. */
  416. protected function getClientIp ()
  417. {
  418. if (getenv('HTTP_CLIENT_IP')) {
  419. $ip = getenv('HTTP_CLIENT_IP');
  420. } else if (getenv('HTTP_X_FORWARDED_FOR')) {
  421. $ip = getenv('HTTP_X_FORWARDED_FOR');
  422. } else if (getenv('REMOTE_ADDR')) {
  423. $ip = getenv('REMOTE_ADDR');
  424. } else {
  425. $ip = $_SERVER['REMOTE_ADDR'];
  426. }
  427. return $ip;
  428. }
  429. }