CarIllegalDownController.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. namespace Modules\OpcData\Http\Controllers\Api;
  3. use App\Http\Controllers\Api\BaseController;
  4. use Illuminate\Contracts\Support\Renderable;
  5. use Illuminate\Http\Request;
  6. use Illuminate\Routing\Controller;
  7. use Illuminate\Support\Facades\DB;
  8. class CarIllegalDownController extends BaseController
  9. {
  10. public function getList(Request $request) {
  11. $mineCode = isset($request->mine_code) ? $request->mine_code : null; // 矿分类
  12. $statisDate = isset($request->date) ? $request->date : date("Y-m-d"); // 统计日期
  13. $page = isset($request->page) ? $request->page : 0; // 分页
  14. $perPage = isset($request->perPage) ? $request->perPage : 20; // 每页显示几条
  15. // 超速违章统计
  16. for($i = 1; $i < 7; $i++) {
  17. // 今日(每4小时统计)
  18. $date = date("Y-m-d");
  19. $stime = sprintf("%02d",($i * 4 - 4)).":00:00";
  20. $etime = ($i * 4) == 24 ? "23:59:59":sprintf("%02d",($i * 4)).":00:00";
  21. $startTime = $date.' '.$stime;
  22. $endTime = $date.' '.$etime;
  23. // 返回统计数据
  24. $res = $this->getCarIllegalByTimeSpan($startTime, $endTime, $mineCode);
  25. $categories1[]= substr($etime, 0, -3);
  26. $data1[] = $res[0]->con;
  27. // 昨日(每4小时统计)
  28. $yesterday = date("Y-m-d", strtotime("-1 day"));
  29. $yStartTime = $yesterday.' '.$stime;
  30. $yEndTime = $yesterday.' '.$etime;
  31. $res = $this->getCarIllegalByTimeSpan($yStartTime, $yEndTime, $mineCode);
  32. $data2[] = $res[0]->con;
  33. }
  34. // 超速违章统计
  35. $res1 = [
  36. 'categories'=>$categories1,
  37. 'series'=>[
  38. [
  39. "name"=>"今日",
  40. "data"=> $data1
  41. ],
  42. [
  43. "name"=>"昨日",
  44. "data"=> $data2
  45. ],
  46. ],
  47. ];
  48. // 一周内超速趋势
  49. for($i = 1; $i <= 7; $i++) {
  50. $date = date("Y-m-d", strtotime("-".(7-$i)." day"));
  51. $res = $this->getCarIllegalCount($date, $mineCode);
  52. $categories3[]= substr($date, -5);
  53. $data3['data'][] = $res[0]->con ?? 0;
  54. }
  55. $data3['name']="数量";
  56. $res2 = [
  57. 'categories'=>$categories3,
  58. 'series'=>[$data3],
  59. ];
  60. // 违章数据列表
  61. $dataList = $this->getCarIllegalList($statisDate, $mineCode, $page, $perPage);
  62. $dataTotal = $this->getCarIllegalCount($statisDate, $mineCode);
  63. $data = [
  64. "total"=>reset($dataTotal[0]),
  65. "speeding_con"=>$res1,
  66. "speeding_week"=>$res2,
  67. "detail_list"=>$dataList
  68. ];
  69. return json_encode($data);
  70. }
  71. // 今日超速统计
  72. public function getCarIllegalCount($statisDate, $mineCode='zaoquan') {
  73. $sqlStr = /** @lang text */
  74. "select count(t.car_num) con from (
  75. select car_num from down_car_speed_warn where statis_date = '".date("Y-m-d", strtotime($statisDate))."'
  76. union all
  77. select car_num from down_car_run_red_light_warn where statis_date = '".date("Y-m-d", strtotime($statisDate))."'
  78. ) t";
  79. return $this->executeSql($sqlStr, 2, $mineCode);
  80. }
  81. // 详细列表
  82. public function getCarIllegalList($statisDate, $mineCode='zaoquan', $page=0, $perPage = 15) {
  83. if ($statisDate == null) return null;
  84. $sqlStr = /** @lang text */
  85. "select * from (
  86. select '闯红灯' illegal_type,
  87. b.numberplate,
  88. a.warn_start_time,
  89. department,
  90. c.light_name illegal_info
  91. from down_car_run_red_light_warn a
  92. join down_car_base_info b on a.car_num = b.car_num
  93. join down_light_info c on a.light_num = c.light_num
  94. where statis_date = '".date("Y-m-d", strtotime($statisDate))."'
  95. union all
  96. select '超速' illegal_type,
  97. b.numberplate,
  98. a.warn_start_time,
  99. b.department,
  100. concat(a.act_val, ' km/h')
  101. from down_car_speed_warn a
  102. join down_car_base_info b on a.car_num = b.car_num
  103. where statis_date = '".date("Y-m-d", strtotime($statisDate))."'
  104. ) t order by warn_start_time desc";
  105. $dbResult = $this->executeSql($sqlStr, 4, $mineCode);
  106. for($i = 0; $i<count($dbResult); $i++) {
  107. $val = $dbResult[$i];
  108. $data[] = [
  109. 'car_number' => $val->numberplate,
  110. 'type' => $val->illegal_type,
  111. 'speed' => $val->department,
  112. 'time' => $val->warn_start_time,
  113. 'place' => $val->illegal_info
  114. ];
  115. }
  116. return $data ?? null;
  117. }
  118. // 根据时段统计超速量
  119. public function getCarIllegalByTimeSpan($startTime, $endTime, $mineCode='zaoquan') {
  120. $sqlStr = /** @lang text */
  121. "select count(case when warn_start_time >= '{$startTime}'
  122. and warn_start_time < '{$endTime}' then car_num end)con from (
  123. select car_num, warn_start_time from down_car_speed_warn where statis_date = '".date("Y-m-d", strtotime($startTime))."'
  124. union all
  125. select car_num, warn_start_time from down_car_run_red_light_warn where statis_date = '".date("Y-m-d", strtotime($startTime))."'
  126. ) t";
  127. return $this->executeSql($sqlStr, 4, $mineCode);
  128. }
  129. public function executeSql($sqlStr, $modelname = -1, $mineCode='zaoquan') {
  130. $conn = 'etl_'.$mineCode;
  131. try{
  132. $opcDB = DB::connection($conn);
  133. $dbResult = $opcDB->select($sqlStr);
  134. return $dbResult;
  135. } catch (\Exception $e) {
  136. switch ($modelname) {
  137. case 1:
  138. return $this->error(-1, '统计超速数量出错!');
  139. case 2:
  140. return $this->error(-1, '统计日超速出错!');
  141. case 4:
  142. return $this->error(-1, '获取详细列表出错!');
  143. default:
  144. return $this->error(-1, '未知错误!');
  145. }
  146. }
  147. }
  148. }