CarDownCurrInfoController.php 4.5 KB

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