CarCurrInfoController.php 2.7 KB

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