CameraStatusHistory.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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['created_at'] = date('Y-m-d H:i:s');
  61. $ningmei['updated_at'] = date('Y-m-d H:i:s');
  62. DB::table('camera_status_history')->insert($ningmei);
  63. $data = [];
  64. //宁煤下各矿在离线数量
  65. $mine_list = DB::table('mine_list')->where('parent_id',$ningmei[0]->id)->where('deleted_at',null)->get();
  66. foreach($mine_list as $k => $v){
  67. $mine_use = [];//宁煤下每个矿所有区域
  68. foreach($mine_all as $key=>$value){
  69. if(count(explode('|',$value->degree)) > 1){
  70. if(explode('|',$value->degree)[0] == $ningmei[0]->id && explode('|',$value->degree)[1] == $v->id){
  71. $mine_use[] = $value->id;
  72. }
  73. }
  74. }
  75. $total = DB::table('camera_list')->whereIn('mine_id',$mine_use)->where('is_show',1)->where('deleted_at',null)->count();
  76. $total_online = DB::table('camera_list')->whereIn('mine_id',$mine_use)->where('is_show',1)->where('camera_status',1)->where('deleted_at',null)->count();
  77. if($total == 0){
  78. $rate = "0";
  79. }else{
  80. $rate = round($total_online / $total * 100,2);
  81. }
  82. if($v->slug == 'NingXiaMeiYeTeShuZuoYeJianKongShiPin'){
  83. $res['title'] = $v->title;
  84. $res['mine_code'] = $v->slug."_jituan";
  85. $res['total'] = $total;
  86. $res['total_online'] = $total_online;
  87. $res['total_offline'] = $total - $total_online;
  88. $res['rate'] = $rate;
  89. }else{
  90. array_push($data,[
  91. 'title'=>$v->title,
  92. 'mine_code'=>$v->slug."_jituan",
  93. 'total'=>$total,
  94. 'total_online'=>$total_online,
  95. 'total_offline'=>$total - $total_online,
  96. 'rate'=>$rate
  97. ]);
  98. }
  99. }
  100. if(isset($res)){
  101. array_push($data,[
  102. 'title'=>$res['title'],
  103. 'mine_code'=>$res['mine_code'],
  104. 'total'=>$res['total'],
  105. 'total_online'=>$res['total_online'],
  106. 'total_offline'=>$res['total_offline'],
  107. 'rate'=>$res['rate']
  108. ]);
  109. if(strpos($res['title'], '煤矿') !== false || strpos($res['title'], '洗选') !== false){
  110. $res['type'] = 1;
  111. }elseif(strpos($data[$i]['title'], '煤制油') !== false || strpos($data[$i]['title'], '烯烃') !== false || strpos($data[$i]['title'], '甲醇') !== false || strpos($data[$i]['title'], '精蜡') !== false){
  112. $res['type'] = 2;
  113. }else{
  114. $res['type'] = 3;
  115. }
  116. $res['created_at'] = date('Y-m-d H:i:s');
  117. $res['updated_at'] = date('Y-m-d H:i:s');
  118. DB::table('camera_status_history')->insert($res);
  119. }
  120. }
  121. }