BController.php 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899
  1. <?php
  2. namespace app\common\controllers;
  3. use app\models\Linkmenu;
  4. use app\modules\car\models\FCarInfo;
  5. use app\modules\car\models\FCarsDetailData;
  6. use app\modules\car\models\FOneCarOneTable;
  7. use Yii;
  8. use \app\modules\admin\models\Resource;
  9. use \app\modules\admin\models\Adminlog;
  10. use app\modules\admin\models\Lang;
  11. /**
  12. * 后台基类
  13. */
  14. class BController extends CController
  15. {
  16. public $resourceBlock;
  17. public $currentResource;//当前资源
  18. public $tableConfig;//列表页配置
  19. public $tableTitle;//列表页表头
  20. public $topLeftTableMenu;
  21. public $topRightTableMenu;
  22. public $operTableMenu;
  23. public $admin_id;
  24. public $carTypes;
  25. public $colors;
  26. public $cityOptions;
  27. public $citySort;
  28. public $carUseTypes;
  29. public $carSourceTypes;
  30. public $carCity;
  31. public $carYears;
  32. public $role_id;
  33. public $province = '河北';//默认省份
  34. public $result = [];
  35. public function init()
  36. {
  37. parent::init();
  38. $this->resourceBlock = array('main'=>'主体');
  39. $this->admin_id = $this->getIdentityInfo('admin_id');
  40. $this->carTypes = ['皮卡车','微型面包车','越野车','轿车','其他'];
  41. $this->colors = ['#485ec4','#2ca67a','#cd9941','#cf5a5a','#ff0000','#ff9900','#8f9a79','#b3b3a4','#c89063','#50be34','#efded4'];
  42. /* $cityResult = FCarInfo::find()->select('distinct(city)')->orderBy(['city'=>SORT_ASC])->all();
  43. foreach($cityResult as $ucity)
  44. {
  45. $this->cityOptions[] = $ucity['city'];
  46. }*/
  47. $this->cityOptions = ['石家庄','唐山','秦皇岛','邯郸','邢台','保定','张家口','承德','廊坊','沧州','衡水','雄安','机动局','省公司本部'];
  48. //排序
  49. $this->citySort = ' CASE "city"';
  50. foreach ($this->cityOptions as $k=>$v){
  51. $this->citySort .= " WHEN '".$v."' THEN ".$k;
  52. }
  53. $this->citySort .= " ELSE ".($k+1)." END";
  54. $carUseTypesResult = FCarInfo::find()->select('distinct(using_tag)')->orderBy(['using_tag'=>SORT_ASC])->all();
  55. foreach($carUseTypesResult as $unit)
  56. {
  57. $this->carUseTypes[] = $unit['using_tag'];
  58. }
  59. $carSourceTypesResult = FCarInfo::find()->select('distinct(self_rent)')->orderBy(['self_rent'=>SORT_ASC])->all();
  60. foreach($carSourceTypesResult as $unit)
  61. {
  62. $this->carSourceTypes[] = $unit['self_rent'];
  63. }
  64. }
  65. public function behaviors()
  66. {
  67. parent::behaviors();
  68. $module = $this->moduleName;
  69. $controller = $this->controllerName;
  70. $actionName = $this->actionName;
  71. //获取当前管理页面的菜单
  72. $this->currentResource = Resource::find()->where('module = :module and controller=:controller and action=:action', [':module' => $module,':controller' => $controller,':action' => $actionName])->one();
  73. $userInfo = $this->getIdentityInfo();
  74. if($this->currentResource&&!empty($userInfo['role_id']))
  75. {
  76. if($userInfo['role_id']==1)
  77. {
  78. $this->topLeftTableMenu = Resource::find()->where("parent_hash=:parent_hash and menu_type in(2,3,4,10) and disabled=0",[':parent_hash'=>$this->currentResource->hash])->orderBy(['list_order' => SORT_ASC])->all();
  79. $this->topRightTableMenu = Resource::find()->where("parent_hash=:parent_hash and menu_type in(5,6,7,8,9) and disabled=0",[':parent_hash'=>$this->currentResource->hash])->orderBy(['list_order' => SORT_ASC])->all();
  80. $this->operTableMenu = Resource::find()->where("parent_hash=:parent_hash and menu_type in(11,12,13,14,15,16,17) and disabled=0",[':parent_hash'=>$this->currentResource->hash])->orderBy(['list_order' => SORT_ASC])->all();
  81. }
  82. else
  83. {
  84. $this->topLeftTableMenu = Resource::find()->where("parent_hash=:parent_hash and menu_type in(2,3,4,10) and disabled=0 and hash in(select hash from {{%role_resource}} where role_id=".$userInfo['role_id'].")",[':parent_hash'=>$this->currentResource->hash])->orderBy(['list_order' => SORT_ASC])->all();
  85. $this->topRightTableMenu = Resource::find()->where("parent_hash=:parent_hash and menu_type in(5,6,7,8,9) and disabled=0 and hash in(select hash from {{%role_resource}} where role_id=".$userInfo['role_id'].")",[':parent_hash'=>$this->currentResource->hash])->orderBy(['list_order' => SORT_ASC])->all();
  86. $this->operTableMenu = Resource::find()->where("parent_hash=:parent_hash and menu_type in(11,12,13,14,15,16,17) and disabled=0 and hash in(select hash from {{%role_resource}} where role_id=".$userInfo['role_id'].")",[':parent_hash'=>$this->currentResource->hash])->orderBy(['list_order' => SORT_ASC])->all();
  87. }
  88. }
  89. //写后台操作日志
  90. if(!empty($this->admin_id))
  91. {
  92. $log = new Adminlog();
  93. $log->module = $this->moduleName;
  94. $log->controller = $this->controllerName;
  95. $log->action = $this->actionName;
  96. $log->query_string = $_SERVER["QUERY_STRING"];
  97. $log->admin_id = $this->admin_id;
  98. $log->ip = MYIP;
  99. $log->create_time = TIMESTAMP;
  100. $log->save();
  101. }
  102. //如果不是生成缓存,需要权限校验
  103. if($_GET['docache'])
  104. {
  105. return [];
  106. }
  107. else
  108. {
  109. return [
  110. [
  111. 'class' => 'app\common\filters\BackEndAuthFilter',
  112. 'except' => ['login','logout','lockscreen','syndata']
  113. ],
  114. [
  115. 'class' => 'app\common\filters\BackEndResourceFilter',
  116. 'except' => ['login','logout','lockscreen','testfunction','fieldsetting','syndata']
  117. ],
  118. ];
  119. }
  120. }
  121. public function getMyResource()
  122. {
  123. $userInfo = $this->getIdentityInfo();
  124. //获取左侧菜单
  125. if($userInfo['role_id'])
  126. {
  127. if($userInfo['role_id']==1)
  128. {
  129. $resultList = Resource::find()->where('disabled = :disabled', [':disabled' => 0])->orderBy(['list_order' => SORT_ASC])->all();
  130. }
  131. else
  132. {
  133. $resultList = Resource::findBySql('SELECT * FROM {{%resource}} where hash in(select hash from {{%role_resource}} where role_id='.$userInfo['role_id'].' order by role_resource_id asc) order by list_order asc' )->all();
  134. }
  135. }
  136. $resourceList = [];
  137. if(is_array($resultList))foreach($resultList as $result){
  138. $resourceList[$result['hash']] = single_object_to_array($result);
  139. }
  140. foreach($resourceList as $k=>$resource)
  141. {
  142. $sonList = [];
  143. $menuSonList = [];
  144. foreach($resourceList as $temp)
  145. {
  146. if($temp['parent_hash']==$resource['hash'])
  147. {
  148. $sonList[] = $temp;
  149. if($temp['menu_type']==1)
  150. {
  151. $menuSonList[] = $temp;
  152. }
  153. }
  154. }
  155. $resourceList[$k]['sonList'] = $sonList;
  156. $resourceList[$k]['menuSonList'] = $menuSonList;
  157. }
  158. return $resourceList;
  159. }
  160. //更新语言表
  161. public function updateLang($key,$value,$file)
  162. {
  163. $lang = Yii::$app->language;
  164. //更新语言表数据
  165. $model = Lang::find()->where("lang='".$lang."' and lang_key='".$key."' and lang_file='".$file."'")->one();
  166. if(!$model)$model = new Lang;
  167. $model->lang = $lang;
  168. $model->lang_key = $key;
  169. $model->lang_value = $value;
  170. $model->lang_file = $file;
  171. $model->save();
  172. }
  173. //更新语言包文件
  174. public function updateMessageFile($file)
  175. {
  176. $resourceDir = BASE_PATH.'messages'.DIRECTORY_SEPARATOR.Yii::$app->language.DIRECTORY_SEPARATOR;
  177. $resourcefile = $resourceDir.$file.'.php';
  178. if(!dir_writeable($resourceDir)) exit('Please chmod '.$resourceDir.' to 0777 !');
  179. $resourceList = Lang::find()->where("lang='".Yii::$app->language."' and lang_file='".$file."'")->all();
  180. $array = array();
  181. if(is_array($resourceList))foreach($resourceList as $resource) {
  182. $array[$resource['lang_key']] = trim($resource['lang_value']);
  183. }
  184. $resourceStr ="<?php ".PHP_EOL.'return '. array2string($array).';'.PHP_EOL."?>";
  185. Yii::$app->params['lockEx'] ? file_put_contents($resourcefile, $resourceStr, LOCK_EX) : file_put_contents($resourcefile, $resourceStr);
  186. }
  187. //单元格修改
  188. public function actionSetfield()
  189. {
  190. $table = strtolower(safe_replace($_GET['table']));
  191. $fileldName = safe_replace($_GET['field']);
  192. $fieldValue = safe_replace($_GET['value']);
  193. $pk = safe_replace($_GET['pk']);
  194. $pkField = \app\common\models\EActiveRecord::getPrikey($table);
  195. if(empty($pkField)) $pkField = 'id';
  196. if(!empty($pk)&&!empty($fileldName))
  197. {
  198. $result = Yii::$app->db->createCommand("UPDATE {{%$table}} SET $fileldName='$fieldValue' WHERE $pkField='$pk'")->execute();
  199. if($result)
  200. {
  201. $msgdata = ['error' => 0,'msg' => '操作成功'];
  202. }
  203. else
  204. {
  205. $msgdata = ['error' => 1,'msg' => '操作失败'];
  206. }
  207. }
  208. else
  209. {
  210. $msgdata = ['error' => 1,'msg' => '操作失败'];
  211. }
  212. echo_json($msgdata);
  213. }
  214. public function getWhere($query, $params=[], $isnull = [])
  215. {
  216. $where = [];
  217. $tables = [];
  218. $tablefrom = $query->getTablesUsedInFrom();
  219. foreach ($tablefrom as $v){
  220. $tables[] = str_replace(array('{{%','}}'),"",$v);
  221. }
  222. foreach ((array)$query->join as $v){
  223. $tables[] = str_replace(array('{{%','}}'),"",$v[1]);
  224. }
  225. $yeartables = [
  226. 'f_car_condition'=>'year',
  227. 'f_cars_detail_data'=>'year',
  228. 'f_deta_illegal_using_car'=>'year',
  229. 'f_fluc_cost_hkm'=>'year',
  230. 'f_fluc_fuel_hkm'=>'year',
  231. 'f_fluc_illegal_using_car'=>'year',
  232. 'f_fluc_inefficient_car'=>'year',
  233. 'f_fuel_hkm'=>'year',
  234. 'f_mileage_attend'=>'year',
  235. 'f_one_car_cost'=>'year',
  236. 'f_one_car_one_table'=>'year_info',
  237. 'f_operating_cost_monitor'=>'year',
  238. 'f_rent_car_cost'=>'year',
  239. 'f_stats_cost_avg'=>'year',
  240. 'f_stats_cost_hkm'=>'year',
  241. 'f_stats_fuel_hkm'=>'year',
  242. 'f_stats_illegal_using_car'=>'year',
  243. 'f_stats_inefficient_car'=>'year',
  244. 'f_t_car_status'=>'year',
  245. 'f_t_car_status_fuel'=>'year',
  246. 'f_t_one_car_avg'=>'year',
  247. 'f_violating_order'=>'year',
  248. 'f_year_car_cost'=>'year',
  249. ];
  250. $citytables = [
  251. 'f_car_info'=>'city',
  252. 'f_cars_detail_data'=>'city',
  253. 'f_cost_abn_order'=>'city',
  254. 'f_fluc_cost_hkm'=>'city',
  255. 'f_fluc_fuel_hkm'=>'city',
  256. 'f_fluc_illegal_using_car'=>'city',
  257. 'f_fluc_inefficient_car'=>'city',
  258. 'f_stats_cost_avg'=>'city',
  259. 'f_stats_cost_hkm'=>'city',
  260. 'f_stats_fuel_hkm'=>'city',
  261. 'f_stats_illegal_using_car'=>'city',
  262. 'f_stats_inefficient_car'=>'city',
  263. 'f_t_car_status'=>'city',
  264. 'f_t_one_car_avg'=>'city',
  265. 'f_year_car_cost'=>'city',
  266. ];
  267. //序列化查询语句
  268. $_where = [];
  269. foreach ($tables as $v){
  270. if($this->carCity && in_array($v,array_keys($citytables))){
  271. $_where[$citytables[$v]] = $this->carCity;
  272. }
  273. if($this->carYears && in_array($v,array_keys($yeartables))){
  274. $_where[$yeartables[$v]] = $this->carYears;
  275. }
  276. }
  277. foreach ($_where as $k=>$v){
  278. $where[] = $k . (is_array($v) ? ( count($v)>1 ? " in ('".implode("','",$v)."')" : " = '".current($v)."'") : " = '".$v."'");
  279. }
  280. //日期选择
  281. $yearField = 'year';
  282. $monthField = 'month';
  283. foreach ($tables as $v){
  284. if($v=='f_one_car_one_table') {
  285. $yearField = 'year_info';
  286. $monthField = 'month_info';
  287. }
  288. }
  289. if(!empty($params['start_date'])&&!empty($params['end_date']))
  290. {
  291. $startdateInfo = explode('-',$params['start_date']);
  292. $start_year = $startdateInfo[0];
  293. $start_month = intval($startdateInfo[1]);
  294. $enddateInfo = explode('-',$params['end_date']);
  295. $end_year = $enddateInfo[0];
  296. $end_month = intval($enddateInfo[1]);
  297. if(!empty($start_year)&&!empty($end_year))$where[] = " ($yearField>=$start_year and $yearField<=$end_year) ";
  298. if(!empty($start_month)&&!empty($end_month))$where[] = " ($monthField>=$start_month and $monthField<=$end_month) ";
  299. }
  300. if(!empty($params['date'])) {
  301. $dateInfo = explode('-', $params['date']);
  302. $year = $dateInfo[0];
  303. $month = intval($dateInfo[1]);
  304. if(!empty($year))$where[] = " $yearField = $year ";
  305. if(!empty($month))$where[] = " $monthField = $month ";
  306. }
  307. //地市选择
  308. if(!empty($params['city'])||!empty($params['city_1'])||!empty($params['city_2'])||!empty($params['city_3']))
  309. {
  310. if($params['city_3'])
  311. {
  312. $cityInfo = Linkmenu::getMenuNameRs($params['city_3']);
  313. }
  314. else if($params['city_2'])
  315. {
  316. $cityInfo = Linkmenu::getMenuNameRs($params['city_2']);
  317. }
  318. else if($params['city_1'])
  319. {
  320. $cityInfo = Linkmenu::getMenuNameRs($params['city_1']);
  321. }
  322. if(!empty($cityInfo[0]))$where[] = " city = '".$cityInfo[0]."' ";
  323. if(!empty($cityInfo[1])){
  324. $where[] = " dpt_sec = '".$cityInfo[1]."' ";
  325. }elseif(in_array('dpt_sec',$isnull)){
  326. $where[] = !empty($cityInfo[0]) ? " dpt_sec is not null " : " dpt_sec is null ";
  327. }
  328. if(!empty($cityInfo[2])){
  329. $where[] = " grid = '".$cityInfo[2]."' ";
  330. }elseif(in_array('grid',$isnull)){
  331. $where[] = !empty($cityInfo[1]) ? " grid is not null " : " grid is null ";
  332. }
  333. }
  334. if(!empty($params['card_num']))$where[] = " card_num = '".$params['card_num']."' ";
  335. if(!empty($params['car_type'])){
  336. $where[] = " car_type = '".$params['car_type']."' ";
  337. }elseif(in_array('car_type',$isnull)){
  338. $where[] = " car_type is null ";
  339. }
  340. if(!empty($params['using_tag'])){
  341. $where[] = " using_tag = '".$params['using_tag']."' ";
  342. }elseif(in_array('using_tag',$isnull)){
  343. $where[] = " using_tag is null ";
  344. }
  345. if(!empty($params['self_rent'])){
  346. $where[] = " self_rent = '".$params['self_rent']."' ";
  347. }elseif(in_array('self_rent',$isnull)){
  348. $where[] = " self_rent is null ";
  349. }
  350. if(!empty($params['car_src'])){
  351. $where[] = " car_src = '".$params['car_src']."' ";
  352. }elseif(in_array('car_src',$isnull)){
  353. $where[] = " car_src is null ";
  354. }
  355. return $where;
  356. }
  357. /*
  358. * 取得一车一表SQL语句或数据
  359. */
  360. public function getOneCarOneTable($params=[],$field = '*',$setYears=true,$ressql = true)
  361. {
  362. $params = $params ? $params : Yii::$app->request->get();
  363. $tableschema = FOneCarOneTable::getTableSchema();
  364. $columns = array_keys($tableschema->columns);
  365. $query = FOneCarOneTable::find();
  366. $where = [];
  367. //限定年份数据
  368. if($setYears && $this->carYears && $this->role_id>1 && in_array('year_info',$columns)){
  369. $where[] = 'year_info' . (is_array($this->carYears) ? ( count($this->carYears)>1 ? " in ('".implode("','",$this->carYears)."')" : " = '".current($this->carYears)."'") : " = '".$this->carYears."'");
  370. }
  371. //没有设定年份,取当年
  372. if(!empty($params['start_date'])&&!empty($params['end_date']))
  373. {
  374. $startdateInfo = explode('-',$params['start_date']);
  375. $start_year = $startdateInfo[0];
  376. $start_month = intval($startdateInfo[1]);
  377. $enddateInfo = explode('-',$params['end_date']);
  378. $end_year = $enddateInfo[0];
  379. $end_month = intval($enddateInfo[1]);
  380. if(!empty($start_year)&&!empty($end_year))$where[] = " (year_info>=$start_year and year_info<=$end_year) ";
  381. if(!empty($start_month)&&!empty($end_month))$where[] = " (month_info>=$start_month and month_info<=$end_month) ";
  382. }
  383. if(!empty($params['date'])) {
  384. $dateInfo = explode('-', $params['date']);
  385. $year = $dateInfo[0];
  386. $month = intval($dateInfo[1]);
  387. if(!empty($year))$where[] = " year_info = $year ";
  388. if(!empty($month))$where[] = " month_info = $month ";
  389. }
  390. foreach ($params as $k=>$v){
  391. if(in_array($k,$columns) && trim($v)!==''&&$k!='city'){
  392. if ($tableschema->columns[$k]->phpType=='string'){
  393. $where[] = $k . " = '".$v."'";
  394. }else{
  395. $where[] = $k . " = ".$v;
  396. }
  397. }
  398. }
  399. if(!empty($where)){
  400. $sql = join(" and ",$where);
  401. $query->where($sql);
  402. }
  403. $query->select($field);
  404. if($ressql){
  405. return $query->createCommand()->getRawSql();
  406. }else{
  407. return $query->asArray()->all();
  408. }
  409. }
  410. /*
  411. * 取得车辆基础表SQL或数据
  412. */
  413. public function getCarInfo($params=[],$field = '*',$setCity=true,$ressql = true)
  414. {
  415. $params = $params ? $params : Yii::$app->request->get();
  416. $tableschema = FCarInfo::getTableSchema();
  417. $columns = array_keys($tableschema->columns);
  418. $query = FCarInfo::find();
  419. $where = [];
  420. if($setCity && $this->carCity && $this->role_id>1 && in_array('city',$columns)){
  421. $where[] = 'city' . (is_array($this->carCity) ? ( count($this->carCity)>1 ? " in ('".implode("','",$this->carCity)."')" : " = '".current($this->carCity)."'") : " = '".$this->carCity."'");
  422. }
  423. foreach ($params as $k=>$v){
  424. if(in_array($k,$columns) && trim($v)!==''&&$k!='city'){
  425. if($k=='card_num'){
  426. $where[] = $k . " LIKE '%".$v."%'";
  427. }elseif ($tableschema->columns[$k]->phpType=='string'){
  428. $where[] = $k . " = '".$v."'";
  429. }else{
  430. $where[] = $k . " = ".$v;
  431. }
  432. }
  433. }
  434. //地市选择
  435. if(!empty($params['city'])||!empty($params['city_1'])||!empty($params['city_2'])||!empty($params['city_3']))
  436. {
  437. if($params['city_3'])
  438. {
  439. $cityInfo = Linkmenu::getMenuNameRs($params['city_3']);
  440. }
  441. else if($params['city_2'])
  442. {
  443. $cityInfo = Linkmenu::getMenuNameRs($params['city_2']);
  444. }
  445. else if($params['city_1'])
  446. {
  447. $cityInfo = Linkmenu::getMenuNameRs($params['city_1']);
  448. }
  449. if(!empty($cityInfo[0]))$where[] = " city = '".$cityInfo[0]."' ";
  450. if(!empty($cityInfo[1])) $where[] = " dpt_sec = '".$cityInfo[1]."' ";
  451. if(!empty($cityInfo[2]))$where[] = " grid = '".$cityInfo[2]."' ";
  452. }
  453. //附加的地市条件
  454. if(!empty($params['city_name_1'])||!empty($params['city_name_2'])||!empty($params['city_name_3']))
  455. {
  456. if(!empty($params['city_name_1']))$where[] = " city = '".$params['city_name_1']."' ";
  457. if(!empty($params['city_name_2'])) $where[] = " dpt_sec = '".$params['city_name_2']."' ";
  458. if(!empty($params['city_name_3']))$where[] = " grid = '".$params['city_name_3']."' ";
  459. }
  460. if(!empty($where)){
  461. $sql = join(" and ",$where);
  462. $query->where($sql);
  463. }
  464. $query->select($field);
  465. if($ressql){
  466. return $query->createCommand()->getRawSql();
  467. }else{
  468. return $query->asArray()->all();;
  469. }
  470. }
  471. /*
  472. * 拼接评价报表SQL及取得分页数据
  473. */
  474. public function getPingjiaData($table,$field='*',$params=[],$all=0)
  475. {
  476. $params = $params ? $params : Yii::$app->request->get();
  477. if(!empty($params['city_1'])||!empty($params['city_2'])||!empty($params['city_3']))
  478. {
  479. if($params['city_3'])
  480. {
  481. $cityInfo = Linkmenu::getMenuNameRs($params['city_3']);
  482. }
  483. else if($params['city_2'])
  484. {
  485. $cityInfo = Linkmenu::getMenuNameRs($params['city_2']);
  486. }
  487. else if($params['city_1'])
  488. {
  489. $cityInfo = Linkmenu::getMenuNameRs($params['city_1']);
  490. }
  491. $params['city'] = $cityInfo[0];
  492. $params['dpt_sec'] = $cityInfo[1];
  493. $params['grid'] = $cityInfo[2];
  494. }
  495. if(empty($params['city']))unset($params['city']);
  496. $groupby[] = 'ci.city';
  497. if(!empty($params['city'])){
  498. $groupby[] = 'ci.dpt_sec';
  499. $field .= ',ci.dpt_sec';
  500. }
  501. if(!empty($params['dpt_sec'])){
  502. $groupby[] = 'ci.grid';
  503. $field .= ',ci.grid';
  504. }
  505. if(!empty($params['card_num'])){
  506. $groupby[] = 'ci.card_num';
  507. $field .= ',ci.card_num';
  508. }
  509. // && stripos($table,'card_num')!==false
  510. if(!empty($params['grid'])){
  511. // $groupby[] = 'ci.card_num';
  512. //$field .= ',ci.card_num,ci.self_rent,ci.car_type,ci.using_tag';
  513. $field .= ',ci.grid';
  514. }
  515. if(!empty($params['car_type'])){
  516. $groupby[] = 'ci.car_type';
  517. $field .= ',ci.car_type';
  518. }
  519. if(!empty($params['using_tag'])){
  520. $groupby[] = 'ci.using_tag';
  521. $field .= ',ci.using_tag';
  522. }
  523. if(!empty($params['self_rent'])){
  524. $groupby[] = 'ci.self_rent';
  525. $field .= ',ci.self_rent';
  526. }
  527. if($all==0)
  528. {
  529. $groupby = join(",",$groupby);
  530. $connection = Yii::$app->db;
  531. //$count_sql = 'SELECT ci.city as city FROM '.$table. ' GROUP BY ' . $groupby;
  532. //$count = count($connection->createCommand($count_sql)->queryAll());
  533. $sql = 'SELECT '. $field .' FROM '.$table. ' GROUP BY ' . $groupby;
  534. }
  535. else
  536. {
  537. $connection = Yii::$app->db;
  538. //$count_sql = 'SELECT ci.city as city FROM '.$table. ' GROUP BY ' . $groupby;
  539. //$count = count($connection->createCommand($count_sql)->queryAll());
  540. $sql = 'SELECT '. $field .' FROM '.$table;
  541. }
  542. //没有设定年份,取当年
  543. if(!empty($params['start_date'])&&!empty($params['end_date']))
  544. {
  545. $startdateInfo = explode('-',$params['start_date']);
  546. $start_year = $startdateInfo[0];
  547. $start_month = intval($startdateInfo[1]);
  548. $enddateInfo = explode('-',$params['end_date']);
  549. $end_year = $enddateInfo[0];
  550. $end_month = intval($enddateInfo[1]);
  551. $year = !empty($end_year) ? $end_year : date('Y');
  552. }
  553. else
  554. {
  555. $year = !empty($params['year']) ? $params['year'] : date('Y');
  556. $start_month = intval($params['start_month'])?intval($params['start_month']):1;
  557. $end_month = intval($params['end_month'])?intval($params['end_month']):12;
  558. }
  559. $sql = str_replace(['年份','起始月份','终止月份'],[$year,$start_month,$end_month],$sql);
  560. //排序
  561. if(isset($params['sort']))
  562. {
  563. $sql .= ' ORDER BY ' .$params['sort'].' '.(isset($params['sortOrder']) && $params['sortOrder']=='asc'?'ASC':'DESC');
  564. }
  565. //分页
  566. if(isset($params['limit'])){
  567. $sql .= ' LIMIT ';
  568. if(isset($params['offset'])){
  569. $sql .= $params['offset']. ',';
  570. }
  571. $sql .= $params['limit'];
  572. }
  573. $data = $connection->createCommand($sql)->queryAll();
  574. if(!empty($data[0]['city']))
  575. {
  576. $data = $this->arrayByArraySort($data,$this->cityOptions,'city');
  577. }
  578. return ["total"=>$count,"totalNotFiltered"=>$count,"rows"=>$data];
  579. }
  580. function arrayByArraySort($data,$sort,$column=null){
  581. $temp = [];
  582. if (!is_null($column)){
  583. foreach ($data as $item){
  584. $temp[$item[$column]][] = $item;
  585. }
  586. }else{
  587. foreach ($data as $key => $item){
  588. $temp[$key] = [$item];
  589. }
  590. }
  591. $ret = [];
  592. foreach ($sort as $sortum){
  593. if (isset($temp[$sortum])){
  594. $ret = array_merge($ret,$temp[$sortum]);
  595. }
  596. }
  597. $ret = array_values($ret);
  598. return $ret;
  599. }
  600. /*
  601. * 取得车辆详情表SQL或数据
  602. */
  603. public function getCarsDetailData($params=[],$field = '*',$setYears=true,$ressql = true)
  604. {
  605. $params = $params ? $params : Yii::$app->request->get();
  606. if(empty($params['city']))unset($params['city']);
  607. $tableschema = FCarsDetailData::getTableSchema();
  608. $columns = array_keys($tableschema->columns);
  609. $query = FCarsDetailData::find();
  610. $where = [];
  611. if($setYears && $this->carYears && $this->role_id>1 && in_array('year',$columns)){
  612. $where[] = 'year' . (is_array($this->carYears) ? ( count($this->carYears)>1 ? " in ('".implode("','",$this->carYears)."')" : " = '".current($this->carYears)."'") : " = '".$this->carYears."'");
  613. }
  614. foreach ($params as $k=>$v){
  615. if(in_array($k,$columns) && trim($v)!==''){
  616. if ($tableschema->columns[$k]->phpType=='string'){
  617. $where[] = $k . " = '".$v."'";
  618. }else{
  619. $where[] = $k . " = ".$v;
  620. }
  621. }
  622. }
  623. //没有设定年份,取当年
  624. if(!empty($params['start_date'])&&!empty($params['end_date']))
  625. {
  626. $startdateInfo = explode('-',$params['start_date']);
  627. $start_year = $startdateInfo[0];
  628. $start_month = intval($startdateInfo[1]);
  629. $enddateInfo = explode('-',$params['end_date']);
  630. $end_year = $enddateInfo[0];
  631. $end_month = intval($enddateInfo[1]);
  632. if(!empty($start_year)&&!empty($end_year))$where[] = " (year>=$start_year and year<=$end_year) ";
  633. if(!empty($start_month)&&!empty($end_month))$where[] = " (month>=$start_month and month<=$end_month) ";
  634. }
  635. if(!empty($params['date'])) {
  636. $dateInfo = explode('-', $params['date']);
  637. $year = $dateInfo[0];
  638. $month = intval($dateInfo[1]);
  639. if(!empty($year))$where[] = " year = $year ";
  640. if(!empty($month))$where[] = " month = $month ";
  641. }
  642. if(!empty($where)){
  643. $sql = join(" and ",$where);
  644. $query->where($sql);
  645. }
  646. $query->select($field);
  647. if($ressql){
  648. return $query->createCommand()->getRawSql();
  649. }else{
  650. return $query->asArray()->all();
  651. }
  652. }
  653. /*
  654. *取得地市条件
  655. */
  656. public function getCityWhere($params)
  657. {
  658. $where = [];
  659. //地市选择
  660. if(!empty($params['city'])||!empty($params['city_1'])||!empty($params['city_2'])||!empty($params['city_3']))
  661. {
  662. if($params['city_3'])
  663. {
  664. $cityInfo = Linkmenu::getMenuNameRs($params['city_3']);
  665. }
  666. else if($params['city_2'])
  667. {
  668. $cityInfo = Linkmenu::getMenuNameRs($params['city_2']);
  669. }
  670. else if($params['city_1'])
  671. {
  672. $cityInfo = Linkmenu::getMenuNameRs($params['city_1']);
  673. }
  674. //unset($params['city'],$params['city_1'],$params['city_2'],$params['city_3']);
  675. if(!empty($cityInfo[0])){
  676. $where['city'] = $cityInfo[0];
  677. }
  678. if(!empty($cityInfo[1])){
  679. $where['dpt_sec'] = $cityInfo[1];
  680. }
  681. if(!empty($cityInfo[2])){
  682. $where['grid'] = $cityInfo[2];
  683. }
  684. }
  685. return $where;
  686. }
  687. /*
  688. *pie图表设置
  689. */
  690. public function getChart1Option($option)
  691. {
  692. $def = [
  693. 'title' => [
  694. 'text' => '',
  695. 'textStyle' => [
  696. 'fontSize' => 14,
  697. ],
  698. 'subtext' => '',
  699. 'left' => 'left'
  700. ],
  701. 'tooltip' => [
  702. 'trigger' => 'item',
  703. 'formatter' => '{a} <br/>{b} : {c} ({d}%)'
  704. ],
  705. 'legend' => [
  706. 'left' => 'center',
  707. 'top' => 'bottom',
  708. 'data' => []
  709. ],
  710. 'toolbox' => [
  711. 'show' => true,
  712. 'feature' => [
  713. 'mark' => ['show' => true],
  714. 'dataView' => ['show' => true, 'readOnly' => false],
  715. 'restore' => ['show' => false],
  716. 'saveAsImage' => ['show' => true]
  717. ]
  718. ],
  719. 'series' => [
  720. [
  721. 'name' => '',
  722. 'type' => 'pie',
  723. 'radius' => [20, 130],
  724. 'center' => ['45%', '50%'],
  725. 'roseType' => 'area',
  726. 'itemStyle' => [
  727. 'borderRadius' => 5
  728. ],
  729. 'data' => [
  730. ]
  731. ]
  732. ]
  733. ];
  734. return array_replace_recursive($def,$option);
  735. }
  736. /*
  737. *bar图表设置
  738. */
  739. public function getChart2Option($option)
  740. {
  741. $def = [
  742. 'title' => [
  743. 'text' => '',
  744. 'textStyle' => [
  745. 'fontSize' => 14,
  746. ],
  747. 'subtext' => '',
  748. 'left' => 'left'
  749. ],
  750. 'tooltip' => [
  751. 'trigger' => 'axis',
  752. 'axisPointer' => [
  753. 'type' => 'shadow'
  754. ]
  755. ],
  756. 'grid' => [
  757. 'left' => '3%',
  758. 'right' => '7%',
  759. 'bottom' => '3%',
  760. 'containLabel' => true,
  761. ],
  762. 'toolbox' => [
  763. 'show' => true,
  764. 'feature' => [
  765. 'dataZoom' => ['yAxisIndex' => 'none'],
  766. 'dataView' => ['show' => true, 'readOnly' => false],
  767. 'magicType' => ['type' => ['line', 'bar']],
  768. 'restore' => ['show' => false],
  769. 'saveAsImage' => ['show' => true]
  770. ]
  771. ],
  772. 'xAxis' => [
  773. [
  774. 'type' => 'category',
  775. 'data' => [],
  776. 'axisTick' => [
  777. 'alignWithLabel' => true
  778. ],
  779. 'axisLabel' => [
  780. 'interval' => 0
  781. ]
  782. ]
  783. ],
  784. 'yAxis' => [
  785. [
  786. 'type' => 'value'
  787. ]
  788. ],
  789. 'series' => [
  790. [
  791. 'name' => '',
  792. 'type' => 'bar',
  793. 'barWidth' => '60%',
  794. 'data' => [],
  795. 'markLine' => [
  796. 'data' => [
  797. ['type' => 'average', 'name' => '平均值']
  798. ]
  799. ]
  800. ]
  801. ]
  802. ];
  803. return array_replace_recursive($def,$option);
  804. }
  805. }