CarDownCurrInfoController.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 CarDownCurrInfoController extends BaseController
  9. {
  10. public function getList(Request $request) {
  11. $mineCode = isset($request->mine_code) ? $request->mine_code : null; // 矿分类
  12. $carList = $this->getCarInfoList($mineCode);
  13. $dataTotal = $this->getCarCon($mineCode);
  14. $data = [
  15. "total"=>reset($dataTotal[0]),
  16. "list"=>$carList
  17. ];
  18. return json_encode($data);
  19. }
  20. public function driverList(Request $request) {
  21. $mineCode = isset($request->mine_code) ? $request->mine_code : null; // 矿分类
  22. $sqlStr = "select per_name, dep from down_personnel_info";
  23. $list = $this->executeSql($sqlStr);
  24. $sqlStr = 'SELECT dep name, count(1) value FROM `down_personnel_info` GROUP BY dep';
  25. $groupCon = $this->executeSql($sqlStr);
  26. $series[]=['data'=>$groupCon];
  27. $data = [
  28. 'charts'=>['series'=>$series],
  29. 'list'=>$list
  30. ];
  31. return json_encode($data);
  32. }
  33. public function taskList(Request $request) {
  34. $sqlStr = /** @lang text */
  35. "SELECT apply_time, task_state, task_info, dest, t1.car_model, t1.numberplate, t2.per_name, t2.dep
  36. FROM down_task_info t
  37. left join down_car_base_info t1 on t.car_num = t1.car_num
  38. left join down_personnel_info t2 on t.driver_num = t2.per_num
  39. where DATE_FORMAT(apply_time,'%Y-%m-%d') = (select max(DATE_FORMAT(apply_time,'%Y-%m-%d')) FROM down_task_info)
  40. and task_state <> '已完成'
  41. ORDER BY task_state desc, apply_time";
  42. $list = $this->executeSql($sqlStr);
  43. $sqlStr = /** @lang text */
  44. "select task_info name, count(1) value from down_task_info
  45. where DATE_FORMAT(apply_time,'%Y-%m-%d') =
  46. (select max(DATE_FORMAT(apply_time,'%Y-%m-%d'))
  47. FROM down_task_info)
  48. and task_state <> '已完成'
  49. GROUP BY task_info";
  50. $groupCon = $this->executeSql($sqlStr);
  51. $series[]=['data'=>$groupCon];
  52. $data = [
  53. 'charts'=>['series'=>$series],
  54. 'list'=>$list
  55. ];
  56. return json_encode($data);
  57. }
  58. public function getCarCon($mineCode='zaoquan') {
  59. $sqlStr = /** @lang text */
  60. "SELECT count(1) con FROM down_car_site_status where out_time is null or out_time < in_time";
  61. return $this->executeSql($sqlStr, 4, $mineCode);
  62. }
  63. // 当前信息详情
  64. public function getCarInfoList($mineCode='zaoquan', $page=0, $perPage = 15) {
  65. if ($mineCode == null) return null;
  66. $sqlStr = /** @lang text */
  67. "SELECT numberplate,
  68. in_time,
  69. speed_avg,
  70. car_type,
  71. t1.task_info,
  72. t2.per_name
  73. FROM down_car_site_status t
  74. left join down_task_info t1 on t.task_num = t1.task_num
  75. left join down_personnel_info t2 on t.per_num = t2.per_num
  76. where site_tag = 1 ";
  77. $dbResult = $this->executeSql($sqlStr, 4, $mineCode);
  78. for($i = 0; $i<count($dbResult); $i++) {
  79. $val = $dbResult[$i];
  80. $data[] = [
  81. 'car_num' => $val->numberplate,
  82. // 'speed' => $val->speed_avg.'km/h',
  83. 'car_type' => $val->car_type,
  84. 'task_info' => $val->task_info,
  85. 'per_name' => $val->per_name,
  86. 'in_time' => $val->in_time
  87. ];
  88. }
  89. return $data ?? null;
  90. }
  91. public function executeSql($sqlStr, $modelname = -1, $mineCode='zaoquan') {
  92. $conn = 'etl_'.$mineCode;
  93. try{
  94. $opcDB = DB::connection($conn);
  95. $dbResult = $opcDB->select($sqlStr);
  96. return $dbResult;
  97. } catch (\Exception $e) {
  98. switch ($modelname) {
  99. case 1:
  100. return $this->error(-1, '统计超速数量出错!');
  101. case 2:
  102. return $this->error(-1, '统计日超速出错!');
  103. case 4:
  104. return $this->error(-1, '获取详细列表出错!');
  105. default:
  106. return $this->error(-1, '未知错误!');
  107. }
  108. }
  109. }
  110. }