CameraStatusHistory.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Enum\ApiEnum;
  4. use Illuminate\Console\Command;
  5. use Illuminate\Support\Facades\DB;
  6. class CameraStatusHistory extends Command
  7. {
  8. /**
  9. * The name and signature of the console command.
  10. *
  11. * @var string
  12. */
  13. protected $signature = 'camera:statushistory';
  14. /**
  15. * The console command description.
  16. *
  17. * @var string
  18. */
  19. protected $description = '摄像头统计历史数据';
  20. /**
  21. * Create a new command instance.
  22. *
  23. * @return void
  24. */
  25. public function __construct()
  26. {
  27. parent::__construct();
  28. }
  29. /**
  30. * Execute the console command.
  31. *
  32. * @return mixed
  33. */
  34. public function handle()
  35. {
  36. $this->cameraStatusHistory();
  37. }
  38. //摄像头统计历史数据
  39. public function cameraStatusHistory()
  40. {
  41. $result['status'] = true;
  42. $result['msg'] = ApiEnum::RETURN_SUCCESS;
  43. //宁煤集团
  44. $ningmei = DB::table('mine_list')->where('title',config('mine_hls'))->get();
  45. $mine_all = DB::table('mine_list')->where('deleted_at',null)->get();//所有区域
  46. $mine_ningmei = [];//宁煤集团所有区域
  47. foreach($mine_all as $k=>$v){
  48. if(explode('|',$v->degree)[0] == $ningmei[0]->id){
  49. $mine_ningmei[] = $v->id;
  50. }
  51. }
  52. //宁煤在离线数量
  53. $ningmei['title'] = '宁煤公司';
  54. $ningmei['mine_code'] = $ningmei[0]->slug;
  55. $ningmei['total'] = DB::table('camera_list')->whereIn('mine_id',$mine_ningmei)->where('deleted_at',null)->count();
  56. $ningmei['total_online'] = DB::table('camera_list')->whereIn('mine_id',$mine_ningmei)->where('camera_status',1)->where('deleted_at',null)->count();
  57. $ningmei['total_offline'] = $ningmei['total'] - $ningmei['total_online'];
  58. $ningmei['rate'] = round($ningmei['total_online'] / $ningmei['total'] * 100,2);
  59. $ningmei['type'] = 0;
  60. $ningmei['date'] = date('Y-m-d');
  61. DB::table('camera_status_history')->insert($ningmei);
  62. $data = [];
  63. //宁煤下各矿在离线数量
  64. $mine_list = DB::table('mine_list')->where('parent_id',$ningmei[0]->id)->where('deleted_at',null)->get();
  65. foreach($mine_list as $k => $v){
  66. $mine_use = [];//宁煤下每个矿所有区域
  67. foreach($mine_all as $key=>$value){
  68. if(count(explode('|',$value->degree)) > 1){
  69. if(explode('|',$value->degree)[0] == $ningmei[0]->id && explode('|',$value->degree)[1] == $v->id){
  70. $mine_use[] = $value->id;
  71. }
  72. }
  73. }
  74. $total = DB::table('camera_list')->whereIn('mine_id',$mine_use)->where('is_show',1)->where('deleted_at',null)->count();
  75. $total_online = DB::table('camera_list')->whereIn('mine_id',$mine_use)->where('is_show',1)->where('camera_status',1)->where('deleted_at',null)->count();
  76. if($total == 0){
  77. $rate = "0";
  78. }else{
  79. $rate = round($total_online / $total * 100,2);
  80. }
  81. if($v->slug == 'NingXiaMeiYeTeShuZuoYeJianKongShiPin'){
  82. $res['title'] = $v->title;
  83. $res['mine_code'] = $v->slug."_jituan";
  84. $res['total'] = $total;
  85. $res['total_online'] = $total_online;
  86. $res['total_offline'] = $total - $total_online;
  87. $res['rate'] = $rate;
  88. }else{
  89. array_push($data,[
  90. 'title'=>$v->title,
  91. 'mine_code'=>$v->slug."_jituan",
  92. 'total'=>$total,
  93. 'total_online'=>$total_online,
  94. 'total_offline'=>$total - $total_online,
  95. 'rate'=>$rate
  96. ]);
  97. }
  98. }
  99. if(isset($res)){
  100. array_push($data,[
  101. 'title'=>$res['title'],
  102. 'mine_code'=>$res['mine_code'],
  103. 'total'=>$res['total'],
  104. 'total_online'=>$res['total_online'],
  105. 'total_offline'=>$res['total_offline'],
  106. 'rate'=>$res['rate']
  107. ]);
  108. if(strpos($res['title'], '煤矿') !== false || strpos($res['title'], '洗选') !== false){
  109. $res['type'] = 1;
  110. }elseif(strpos($data[$i]['title'], '煤制油') !== false || strpos($data[$i]['title'], '烯烃') !== false || strpos($data[$i]['title'], '甲醇') !== false || strpos($data[$i]['title'], '精蜡') !== false){
  111. $res['type'] = 2;
  112. }else{
  113. $res['type'] = 3;
  114. }
  115. $res['date'] = date('Y-m-d');
  116. DB::table('camera_status_history')->insert($res);
  117. }
  118. }
  119. }