ValueController.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  1. <?php
  2. namespace app\commands;
  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\console\Controller;
  8. use Yii;
  9. class ValueController extends Controller
  10. {
  11. public $carTypes;
  12. public $cityOptions;
  13. public $carUseTypes;
  14. public $carSourceTypes;
  15. public $carCity;
  16. public $carYears;
  17. public $province = '河北';//默认省份
  18. public $provinceOptions;
  19. public function init()
  20. {
  21. $this->cityOptions = ['石家庄','机动局','保定','唐山','廊坊','张家口','承德','沧州','秦皇岛','衡水','邢台','邯郸','雄安','省本部'];
  22. //北十省份
  23. $provinceResult = Yii::$app->db->createCommand("select distinct(province) from wz_f_ten_northern_provinces_stat")->queryAll();
  24. foreach($provinceResult as $uprovince)
  25. {
  26. $this->provinceOptions[] = $uprovince['province'];
  27. }
  28. $carUseTypesResult = FCarInfo::find()->select('distinct(using_tag)')->orderBy(['using_tag'=>SORT_ASC])->all();
  29. foreach($carUseTypesResult as $unit)
  30. {
  31. $this->carUseTypes[] = $unit['using_tag'];
  32. }
  33. $carSourceTypesResult = FCarInfo::find()->select('distinct(self_rent)')->orderBy(['self_rent'=>SORT_ASC])->all();
  34. foreach($carSourceTypesResult as $unit)
  35. {
  36. $this->carSourceTypes[] = $unit['self_rent'];
  37. }
  38. }
  39. public function actionIndex()
  40. {
  41. $this->_doreport();
  42. $this->_dowave();
  43. }
  44. //车辆专题报告
  45. private function _doreport()
  46. {
  47. $url = $this->createRealUrl('car/default/report');
  48. https_request($url);
  49. }
  50. //波动分析缓存
  51. private function _dowave()
  52. {
  53. $url = $this->createRealUrl('car/wave/index');
  54. for($i=1;$i<=9;$i++)
  55. {
  56. for($j=1;$j<=2;$j++)
  57. {
  58. $params[] = "type=$i";
  59. $params[] = "start_month=1";
  60. $params[] = "end_month=".intval(date('m'));
  61. $params[] = "datatype=".$j;
  62. $params[] = "year=".date('Y');
  63. $cacheurl = $url.'&'.join('&',$params);
  64. https_request($cacheurl);
  65. }
  66. }
  67. }
  68. //生成含入口脚本的URL地址
  69. public function createRealUrl($params)
  70. {
  71. $url = WEB_URL.'index.html' ;
  72. return $url.'?r='.$params.'&docache=1';
  73. }
  74. public function getWhere($query, $params=[], $isnull = [])
  75. {
  76. $where = [];
  77. $tables = [];
  78. $tablefrom = $query->getTablesUsedInFrom();
  79. foreach ($tablefrom as $v){
  80. $tables[] = str_replace(array('{{%','}}'),"",$v);
  81. }
  82. foreach ((array)$query->join as $v){
  83. $tables[] = str_replace(array('{{%','}}'),"",$v[1]);
  84. }
  85. $yeartables = [
  86. 'f_car_condition'=>'year',
  87. 'f_cars_detail_data'=>'year',
  88. 'f_deta_illegal_using_car'=>'year',
  89. 'f_fluc_cost_hkm'=>'year',
  90. 'f_fluc_fuel_hkm'=>'year',
  91. 'f_fluc_illegal_using_car'=>'year',
  92. 'f_fluc_inefficient_car'=>'year',
  93. 'f_fuel_hkm'=>'year',
  94. 'f_mileage_attend'=>'year',
  95. 'f_one_car_cost'=>'year',
  96. 'f_one_car_one_table'=>'year_info',
  97. 'f_operating_cost_monitor'=>'year',
  98. 'f_rent_car_cost'=>'year',
  99. 'f_stats_cost_avg'=>'year',
  100. 'f_stats_cost_hkm'=>'year',
  101. 'f_stats_fuel_hkm'=>'year',
  102. 'f_stats_illegal_using_car'=>'year',
  103. 'f_stats_inefficient_car'=>'year',
  104. 'f_t_car_status'=>'year',
  105. 'f_t_car_status_fuel'=>'year',
  106. 'f_t_one_car_avg'=>'year',
  107. 'f_violating_order'=>'year',
  108. 'f_year_car_cost'=>'year',
  109. ];
  110. $citytables = [
  111. 'f_car_info'=>'city',
  112. 'f_cars_detail_data'=>'city',
  113. 'f_cost_abn_order'=>'city',
  114. 'f_fluc_cost_hkm'=>'city',
  115. 'f_fluc_fuel_hkm'=>'city',
  116. 'f_fluc_illegal_using_car'=>'city',
  117. 'f_fluc_inefficient_car'=>'city',
  118. 'f_stats_cost_avg'=>'city',
  119. 'f_stats_cost_hkm'=>'city',
  120. 'f_stats_fuel_hkm'=>'city',
  121. 'f_stats_illegal_using_car'=>'city',
  122. 'f_stats_inefficient_car'=>'city',
  123. 'f_t_car_status'=>'city',
  124. 'f_t_one_car_avg'=>'city',
  125. 'f_year_car_cost'=>'city',
  126. ];
  127. //序列化查询语句
  128. $_where = [];
  129. foreach ($tables as $v){
  130. if($this->carCity && in_array($v,array_keys($citytables))){
  131. $_where[$citytables[$v]] = $this->carCity;
  132. }
  133. if($this->carYears && in_array($v,array_keys($yeartables))){
  134. $_where[$yeartables[$v]] = $this->carYears;
  135. }
  136. }
  137. foreach ($_where as $k=>$v){
  138. $where[] = $k . (is_array($v) ? ( count($v)>1 ? " in ('".implode("','",$v)."')" : " = '".current($v)."'") : " = '".$v."'");
  139. }
  140. //日期选择
  141. $yearField = 'year';
  142. $monthField = 'month';
  143. foreach ($tables as $v){
  144. if($v=='f_one_car_one_table') {
  145. $yearField = 'year_info';
  146. $monthField = 'month_info';
  147. }
  148. }
  149. if(!empty($params['start_date'])&&!empty($params['end_date']))
  150. {
  151. $startdateInfo = explode('-',$params['start_date']);
  152. $start_year = $startdateInfo[0];
  153. $start_month = intval($startdateInfo[1]);
  154. $enddateInfo = explode('-',$params['end_date']);
  155. $end_year = $enddateInfo[0];
  156. $end_month = intval($enddateInfo[1]);
  157. if(!empty($start_year)&&!empty($end_year))$where[] = " ($yearField>=$start_year and $yearField<=$end_year) ";
  158. if(!empty($start_month)&&!empty($end_month))$where[] = " ($monthField>=$start_month and $monthField<=$end_month) ";
  159. }
  160. if(!empty($params['date'])) {
  161. $dateInfo = explode('-', $params['date']);
  162. $year = $dateInfo[0];
  163. $month = intval($dateInfo[1]);
  164. if(!empty($year))$where[] = " $yearField = $year ";
  165. if(!empty($month))$where[] = " $monthField = $month ";
  166. }
  167. //地市选择
  168. if(!empty($params['city'])||!empty($params['city_1'])||!empty($params['city_2'])||!empty($params['city_3']))
  169. {
  170. if($params['city_3'])
  171. {
  172. $cityInfo = Linkmenu::getMenuNameRs($params['city_3']);
  173. }
  174. else if($params['city_2'])
  175. {
  176. $cityInfo = Linkmenu::getMenuNameRs($params['city_2']);
  177. }
  178. else if($params['city_1'])
  179. {
  180. $cityInfo = Linkmenu::getMenuNameRs($params['city_1']);
  181. }
  182. if(!empty($cityInfo[0]))$where[] = " city = '".$cityInfo[0]."' ";
  183. if(!empty($cityInfo[1])){
  184. $where[] = " dpt_sec = '".$cityInfo[1]."' ";
  185. }elseif(in_array('dpt_sec',$isnull)){
  186. $where[] = !empty($cityInfo[0]) ? " dpt_sec is not null " : " dpt_sec is null ";
  187. }
  188. if(!empty($cityInfo[2])){
  189. $where[] = " grid = '".$cityInfo[2]."' ";
  190. }elseif(in_array('grid',$isnull)){
  191. $where[] = !empty($cityInfo[1]) ? " grid is not null " : " grid is null ";
  192. }
  193. }
  194. if(!empty($params['card_num']))$where[] = " card_num = '".$params['card_num']."' ";
  195. if(!empty($params['car_type'])){
  196. $where[] = " car_type = '".$params['car_type']."' ";
  197. }elseif(in_array('car_type',$isnull)){
  198. $where[] = " car_type is null ";
  199. }
  200. if(!empty($params['using_tag'])){
  201. $where[] = " using_tag = '".$params['using_tag']."' ";
  202. }elseif(in_array('using_tag',$isnull)){
  203. $where[] = " using_tag is null ";
  204. }
  205. if(!empty($params['self_rent'])){
  206. $where[] = " self_rent = '".$params['self_rent']."' ";
  207. }elseif(in_array('self_rent',$isnull)){
  208. $where[] = " self_rent is null ";
  209. }
  210. if(!empty($params['car_src'])){
  211. $where[] = " car_src = '".$params['car_src']."' ";
  212. }elseif(in_array('car_src',$isnull)){
  213. $where[] = " car_src is null ";
  214. }
  215. return $where;
  216. }
  217. /*
  218. * 取得一车一表SQL语句或数据
  219. */
  220. public function getOneCarOneTable($params=[],$field = '*',$setYears=true,$ressql = true)
  221. {
  222. $params = $params ? $params : Yii::$app->request->get();
  223. $tableschema = FOneCarOneTable::getTableSchema();
  224. $columns = array_keys($tableschema->columns);
  225. $query = FOneCarOneTable::find();
  226. $where = [];
  227. //限定年份数据
  228. if($setYears && $this->carYears && $this->role_id>1 && in_array('year_info',$columns)){
  229. $where[] = 'year_info' . (is_array($this->carYears) ? ( count($this->carYears)>1 ? " in ('".implode("','",$this->carYears)."')" : " = '".current($this->carYears)."'") : " = '".$this->carYears."'");
  230. }
  231. //没有设定年份,取当年
  232. if(!empty($params['start_date'])&&!empty($params['end_date']))
  233. {
  234. $startdateInfo = explode('-',$params['start_date']);
  235. $start_year = $startdateInfo[0];
  236. $start_month = intval($startdateInfo[1]);
  237. $enddateInfo = explode('-',$params['end_date']);
  238. $end_year = $enddateInfo[0];
  239. $end_month = intval($enddateInfo[1]);
  240. if(!empty($start_year)&&!empty($end_year))$where[] = " (year_info>=$start_year and year_info<=$end_year) ";
  241. if(!empty($start_month)&&!empty($end_month))$where[] = " (month_info>=$start_month and month_info<=$end_month) ";
  242. }
  243. if(!empty($params['date'])) {
  244. $dateInfo = explode('-', $params['date']);
  245. $year = $dateInfo[0];
  246. $month = intval($dateInfo[1]);
  247. if(!empty($year))$where[] = " year_info = $year ";
  248. if(!empty($month))$where[] = " month_info = $month ";
  249. }
  250. foreach ($params as $k=>$v){
  251. if(in_array($k,$columns) && trim($v)!==''&&$k!='city'){
  252. if ($tableschema->columns[$k]->phpType=='string'){
  253. $where[] = $k . " = '".$v."'";
  254. }else{
  255. $where[] = $k . " = ".$v;
  256. }
  257. }
  258. }
  259. if(!empty($where)){
  260. $sql = join(" and ",$where);
  261. $query->where($sql);
  262. }
  263. $query->select($field);
  264. if($ressql){
  265. return $query->createCommand()->getRawSql();
  266. }else{
  267. return $query->asArray()->all();
  268. }
  269. }
  270. /*
  271. * 取得车辆基础表SQL或数据
  272. */
  273. public function getCarInfo($params=[],$field = '*',$setCity=true,$ressql = true)
  274. {
  275. $params = $params ? $params : Yii::$app->request->get();
  276. $tableschema = FCarInfo::getTableSchema();
  277. $columns = array_keys($tableschema->columns);
  278. $query = FCarInfo::find();
  279. $where = [];
  280. if($setCity && $this->carCity && $this->role_id>1 && in_array('city',$columns)){
  281. $where[] = 'city' . (is_array($this->carCity) ? ( count($this->carCity)>1 ? " in ('".implode("','",$this->carCity)."')" : " = '".current($this->carCity)."'") : " = '".$this->carCity."'");
  282. }
  283. foreach ($params as $k=>$v){
  284. if(in_array($k,$columns) && trim($v)!==''&&$k!='city'){
  285. if($k=='card_num'){
  286. $where[] = $k . " LIKE '%".$v."%'";
  287. }elseif ($tableschema->columns[$k]->phpType=='string'){
  288. $where[] = $k . " = '".$v."'";
  289. }else{
  290. $where[] = $k . " = ".$v;
  291. }
  292. }
  293. }
  294. //地市选择
  295. if(!empty($params['city'])||!empty($params['city_1'])||!empty($params['city_2'])||!empty($params['city_3']))
  296. {
  297. if($params['city_3'])
  298. {
  299. $cityInfo = Linkmenu::getMenuNameRs($params['city_3']);
  300. }
  301. else if($params['city_2'])
  302. {
  303. $cityInfo = Linkmenu::getMenuNameRs($params['city_2']);
  304. }
  305. else if($params['city_1'])
  306. {
  307. $cityInfo = Linkmenu::getMenuNameRs($params['city_1']);
  308. }
  309. if(!empty($cityInfo[0]))$where[] = " city = '".$cityInfo[0]."' ";
  310. if(!empty($cityInfo[1])) $where[] = " dpt_sec = '".$cityInfo[1]."' ";
  311. if(!empty($cityInfo[2]))$where[] = " grid = '".$cityInfo[2]."' ";
  312. }
  313. //附加的地市条件
  314. if(!empty($params['city_name_1'])||!empty($params['city_name_2'])||!empty($params['city_name_3']))
  315. {
  316. if(!empty($params['city_name_1']))$where[] = " city = '".$params['city_name_1']."' ";
  317. if(!empty($params['city_name_2'])) $where[] = " dpt_sec = '".$params['city_name_2']."' ";
  318. if(!empty($params['city_name_3']))$where[] = " grid = '".$params['city_name_3']."' ";
  319. }
  320. if(!empty($where)){
  321. $sql = join(" and ",$where);
  322. $query->where($sql);
  323. }
  324. $query->select($field);
  325. if($ressql){
  326. return $query->createCommand()->getRawSql();
  327. }else{
  328. return $query->asArray()->all();;
  329. }
  330. }
  331. /*
  332. * 拼接评价报表SQL及取得分页数据
  333. */
  334. public function getPingjiaData($table,$field='*',$params=[],$all=0)
  335. {
  336. $params = $params ? $params : Yii::$app->request->get();
  337. if(!empty($params['city_1'])||!empty($params['city_2'])||!empty($params['city_3']))
  338. {
  339. if($params['city_3'])
  340. {
  341. $cityInfo = Linkmenu::getMenuNameRs($params['city_3']);
  342. }
  343. else if($params['city_2'])
  344. {
  345. $cityInfo = Linkmenu::getMenuNameRs($params['city_2']);
  346. }
  347. else if($params['city_1'])
  348. {
  349. $cityInfo = Linkmenu::getMenuNameRs($params['city_1']);
  350. }
  351. $params['city'] = $cityInfo[0];
  352. $params['dpt_sec'] = $cityInfo[1];
  353. $params['grid'] = $cityInfo[2];
  354. }
  355. $groupby = 'ci.city';
  356. if(!empty($params['city'])){
  357. $groupby = 'ci.dpt_sec';
  358. $field .= ',ci.dpt_sec';
  359. }
  360. if(!empty($params['dpt_sec'])){
  361. $groupby = 'ci.grid';
  362. $field .= ',ci.grid';
  363. }
  364. if(!empty($params['card_num'])){
  365. $groupby = 'ci.card_num';
  366. $field .= ',ci.card_num,ci.self_rent,ci.car_type,ci.using_tag';
  367. }
  368. if(!empty($params['grid']) && stripos($table,'card_num')!==false){
  369. $groupby = 'ci.card_num';
  370. $field .= ',ci.card_num,ci.self_rent,ci.car_type,ci.using_tag';
  371. }
  372. if(!empty($params['car_type'])){
  373. $field .= ',ci.car_type';
  374. }
  375. if(!empty($params['using_tag'])){
  376. $field .= ',ci.using_tag';
  377. }
  378. if(!empty($params['self_rent'])){
  379. $field .= ',ci.self_rent';
  380. }
  381. if($all==0)
  382. {
  383. $count_sql = 'SELECT `city` FROM '.$table. ' GROUP BY ' . $groupby;
  384. $connection = Yii::$app->db;
  385. $count = count($connection->createCommand($count_sql)->queryAll());
  386. $sql = 'SELECT '. $field .' FROM '.$table. ' GROUP BY ' . $groupby;
  387. }
  388. else
  389. {
  390. $count_sql = 'SELECT `city` FROM '.$table;
  391. $connection = Yii::$app->db;
  392. $count = count($connection->createCommand($count_sql)->queryAll());
  393. $sql = 'SELECT '. $field .' FROM '.$table;
  394. }
  395. //没有设定年份,取当年
  396. if(!empty($params['start_date'])&&!empty($params['end_date']))
  397. {
  398. $startdateInfo = explode('-',$params['start_date']);
  399. $start_year = $startdateInfo[0];
  400. $start_month = intval($startdateInfo[1]);
  401. $enddateInfo = explode('-',$params['end_date']);
  402. $end_year = $enddateInfo[0];
  403. $end_month = intval($enddateInfo[1]);
  404. $year = !empty($end_year) ? $end_year : date('Y');
  405. }
  406. else
  407. {
  408. $year = !empty($params['year']) ? $params['year'] : date('Y');
  409. $start_month = intval($params['start_month'])?intval($params['start_month']):1;
  410. $end_month = intval($params['end_month'])?intval($params['end_month']):12;
  411. }
  412. $sql = str_replace(['年份','起始月份','终止月份'],[$year,$start_month,$end_month],$sql);
  413. //排序
  414. if(isset($params['sort']))
  415. {
  416. $sql .= ' ORDER BY ' .$params['sort'].' '.(isset($params['sortOrder']) && $params['sortOrder']=='asc'?'ASC':'DESC');
  417. }
  418. //分页
  419. if(isset($params['limit'])){
  420. $sql .= ' LIMIT ';
  421. if(isset($params['offset'])){
  422. $sql .= $params['offset']. ',';
  423. }
  424. $sql .= $params['limit'];
  425. }
  426. $data = $connection->createCommand($sql)->queryAll();
  427. if(!empty($data[0]['city']))
  428. {
  429. $data = $this->arrayByArraySort($data,$this->cityOptions,'city');
  430. }
  431. return ["total"=>$count,"totalNotFiltered"=>$count,"rows"=>$data];
  432. }
  433. function arrayByArraySort($data,$sort,$column=null){
  434. $temp = [];
  435. if (!is_null($column)){
  436. foreach ($data as $item){
  437. $temp[$item[$column]][] = $item;
  438. }
  439. }else{
  440. foreach ($data as $key => $item){
  441. $temp[$key] = [$item];
  442. }
  443. }
  444. $ret = [];
  445. foreach ($sort as $sortum){
  446. if (isset($temp[$sortum])){
  447. $ret = array_merge($ret,$temp[$sortum]);
  448. }
  449. }
  450. $ret = array_values($ret);
  451. return $ret;
  452. }
  453. /*
  454. * 取得车辆详情表SQL或数据
  455. */
  456. public function getCarsDetailData($params=[],$field = '*',$setYears=true,$ressql = true)
  457. {
  458. $params = $params ? $params : Yii::$app->request->get();
  459. $tableschema = FCarsDetailData::getTableSchema();
  460. $columns = array_keys($tableschema->columns);
  461. $query = FCarsDetailData::find();
  462. $where = [];
  463. if($setYears && $this->carYears && $this->role_id>1 && in_array('year',$columns)){
  464. $where[] = 'year' . (is_array($this->carYears) ? ( count($this->carYears)>1 ? " in ('".implode("','",$this->carYears)."')" : " = '".current($this->carYears)."'") : " = '".$this->carYears."'");
  465. }
  466. foreach ($params as $k=>$v){
  467. if(in_array($k,$columns) && trim($v)!==''){
  468. if ($tableschema->columns[$k]->phpType=='string'){
  469. $where[] = $k . " = '".$v."'";
  470. }else{
  471. $where[] = $k . " = ".$v;
  472. }
  473. }
  474. }
  475. //没有设定年份,取当年
  476. if(!empty($params['start_date'])&&!empty($params['end_date']))
  477. {
  478. $startdateInfo = explode('-',$params['start_date']);
  479. $start_year = $startdateInfo[0];
  480. $start_month = intval($startdateInfo[1]);
  481. $enddateInfo = explode('-',$params['end_date']);
  482. $end_year = $enddateInfo[0];
  483. $end_month = intval($enddateInfo[1]);
  484. if(!empty($start_year)&&!empty($end_year))$where[] = " (year>=$start_year and year<=$end_year) ";
  485. if(!empty($start_month)&&!empty($end_month))$where[] = " (month>=$start_month and month<=$end_month) ";
  486. }
  487. if(!empty($params['date'])) {
  488. $dateInfo = explode('-', $params['date']);
  489. $year = $dateInfo[0];
  490. $month = intval($dateInfo[1]);
  491. if(!empty($year))$where[] = " year = $year ";
  492. if(!empty($month))$where[] = " month = $month ";
  493. }
  494. if(!empty($where)){
  495. $sql = join(" and ",$where);
  496. $query->where($sql);
  497. }
  498. $query->select($field);
  499. if($ressql){
  500. return $query->createCommand()->getRawSql();
  501. }else{
  502. return $query->asArray()->all();
  503. }
  504. }
  505. }