CarDownCurrInfoController.php 4.3 KB

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