CarIllageDownLightController.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 CarIllageDownLightController 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 = "select count(car_num) con from `down_car_run_red_light_warn` where statis_date = '".date("Y-m-d", strtotime($statisDate))."'";
  74. return $this->executeSql($sqlStr, 2, $mineCode);
  75. }
  76. // 超速详细列表
  77. public function getCarIllegalList($statisDate, $mineCode='zaoquan', $page=0, $perPage = 15) {
  78. if ($statisDate == null) return null;
  79. $sqlStr = /** @lang text */
  80. 'select b.numberplate,
  81. a.warn_start_time,
  82. b.department,
  83. c.light_name
  84. from down_car_run_red_light_warn a
  85. join down_car_base_info b on a.car_num = b.car_num
  86. join down_light_info c on a.light_num = c.light_num
  87. where statis_date = "'.$statisDate.'" order by a.warn_start_time desc ';
  88. $dbResult = $this->executeSql($sqlStr, 4, $mineCode);
  89. for($i = 0; $i<count($dbResult); $i++) {
  90. $val = $dbResult[$i];
  91. $data[] = [
  92. 'car_number' => $val->numberplate,
  93. 'speed' => $val->department,
  94. 'time' => $val->warn_start_time,
  95. 'place' => $val->light_name
  96. ];
  97. }
  98. return $data ?? null;
  99. }
  100. // 根据时段统计超速量
  101. public function getCarIllegalByTimeSpan($startTime, $endTime, $mineCode='zaoquan') {
  102. $sqlStr = /** @lang text */
  103. "select count(case when warn_start_time >= '{$startTime}'
  104. and warn_start_time < '{$endTime}' then car_num end) con
  105. from down_car_run_red_light_warn where statis_date = '".date("Y-m-d", strtotime($startTime))."'";
  106. // return $sqlStr;
  107. return $this->executeSql($sqlStr, 4, $mineCode);
  108. }
  109. public function executeSql($sqlStr, $modelname = -1, $mineCode='zaoquan') {
  110. $conn = 'etl_'.$mineCode;
  111. try{
  112. $opcDB = DB::connection($conn);
  113. $dbResult = $opcDB->select($sqlStr);
  114. return $dbResult;
  115. } catch (\Exception $e) {
  116. switch ($modelname) {
  117. case 1:
  118. return $this->error(-1, '统计超速数量出错!');
  119. case 2:
  120. return $this->error(-1, '统计日超速出错!');
  121. case 4:
  122. return $this->error(-1, '获取详细列表出错!');
  123. default:
  124. return $this->error(-1, '未知错误!');
  125. }
  126. }
  127. }
  128. }