CarDownCurrInfoController.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 getCarCon($mineCode='zaoquan') {
  33. $sqlStr = /** @lang text */
  34. "SELECT count(1) con FROM down_car_site_status where out_time is null or out_time < in_time";
  35. return $this->executeSql($sqlStr, 4, $mineCode);
  36. }
  37. // 当前信息详情
  38. public function getCarInfoList($mineCode='zaoquan', $page=0, $perPage = 15) {
  39. if ($mineCode == null) return null;
  40. $sqlStr = /** @lang text */
  41. "SELECT numberplate,
  42. in_time,
  43. speed_avg,
  44. car_type,
  45. t1.task_info,
  46. t2.per_name
  47. FROM down_car_site_status t
  48. left join down_task_info t1 on t.task_num = t1.task_num
  49. left join down_personnel_info t2 on t.per_num = t2.per_num
  50. where site_tag = 1 ";
  51. $dbResult = $this->executeSql($sqlStr, 4, $mineCode);
  52. for($i = 0; $i<count($dbResult); $i++) {
  53. $val = $dbResult[$i];
  54. $data[] = [
  55. 'car_num' => $val->numberplate,
  56. // 'speed' => $val->speed_avg.'km/h',
  57. 'car_type' => $val->car_type,
  58. 'task_info' => $val->task_info,
  59. 'per_name' => $val->per_name,
  60. 'in_time' => $val->in_time
  61. ];
  62. }
  63. return $data ?? null;
  64. }
  65. public function executeSql($sqlStr, $modelname = -1, $mineCode='zaoquan') {
  66. $conn = 'etl_'.$mineCode;
  67. try{
  68. $opcDB = DB::connection($conn);
  69. $dbResult = $opcDB->select($sqlStr);
  70. return $dbResult;
  71. } catch (\Exception $e) {
  72. switch ($modelname) {
  73. case 1:
  74. return $this->error(-1, '统计超速数量出错!');
  75. case 2:
  76. return $this->error(-1, '统计日超速出错!');
  77. case 4:
  78. return $this->error(-1, '获取详细列表出错!');
  79. default:
  80. return $this->error(-1, '未知错误!');
  81. }
  82. }
  83. }
  84. }