CameraStatusHistory.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. DB::table('camera_status_history')->where('date', '<=', date('Y-m-d H:i:s', strtotime('-30 days')))->delete();
  44. //宁煤集团
  45. $ningmei = DB::table('mine_list')->where('title',config('mine_hls'))->get();
  46. $mine_all = DB::table('mine_list')->where('deleted_at',null);
  47. $xczyd = DB::table('mine_list')->where('deleted_at',null)->where('title','现场作业点')->pluck('degree');
  48. $ydjk = DB::table('mine_list')->where('deleted_at',null)->where('title','移动监控')->pluck('degree');
  49. if(count($xczyd) > 0){
  50. for($i=0;$i<count($xczyd);$i++){
  51. $mine_all->where('degree','not like',$xczyd[$i].'%');
  52. }
  53. }
  54. if(count($ydjk) > 0){
  55. for($i=0;$i<count($ydjk);$i++){
  56. $mine_all->where('degree','not like',$ydjk[$i].'%');
  57. }
  58. }
  59. $mine_all = $mine_all->get();
  60. $mine_ningmei = [];//宁煤集团所有区域
  61. foreach($mine_all as $k=>$v){
  62. if(explode('|',$v->degree)[0] == $ningmei[0]->id){
  63. $mine_ningmei[] = $v->id;
  64. }
  65. }
  66. //宁煤在离线数量
  67. $nm['title'] = '宁煤公司';
  68. $nm['mine_code'] = $ningmei[0]->slug;
  69. $nm['total'] = DB::table('camera_list')->whereIn('mine_id',$mine_ningmei)->where('deleted_at',null)->count();
  70. $nm['total_online'] = DB::table('camera_list')->whereIn('mine_id',$mine_ningmei)->where('camera_status',1)->where('deleted_at',null)->count();
  71. $nm['total_offline'] = $nm['total'] - $nm['total_online'];
  72. $nm['rate'] = round($nm['total_online'] / $nm['total'] * 100,2);
  73. $nm['type'] = 0;
  74. $nm['date'] = date('Y-m-d');
  75. DB::table('camera_status_history')->insert($nm);
  76. $data = [];
  77. //宁煤下各矿在离线数量
  78. $mine_list = DB::table('mine_list')->where('parent_id',$ningmei[0]->id)->where('deleted_at',null)->get();
  79. foreach($mine_list as $k => $v){
  80. $mine_use = [];//宁煤下每个矿所有区域
  81. foreach($mine_all as $key=>$value){
  82. if(count(explode('|',$value->degree)) > 1){
  83. if(explode('|',$value->degree)[0] == $ningmei[0]->id && explode('|',$value->degree)[1] == $v->id){
  84. $mine_use[] = $value->id;
  85. }
  86. }
  87. }
  88. $total = DB::table('camera_list')->whereIn('mine_id',$mine_use)->where('is_show',1)->where('deleted_at',null)->count();
  89. $total_online = DB::table('camera_list')->whereIn('mine_id',$mine_use)->where('is_show',1)->where('camera_status',1)->where('deleted_at',null)->count();
  90. if($total == 0){
  91. $rate = "0";
  92. }else{
  93. $rate = round($total_online / $total * 100,2);
  94. }
  95. if($v->slug == 'NingXiaMeiYeTeShuZuoYeJianKongShiPin'){
  96. $res['title'] = $v->title;
  97. $res['mine_code'] = $v->slug."_jituan";
  98. $res['total'] = $total;
  99. $res['total_online'] = $total_online;
  100. $res['total_offline'] = $total - $total_online;
  101. $res['rate'] = $rate;
  102. }else{
  103. array_push($data,[
  104. 'title'=>$v->title,
  105. 'mine_code'=>$v->slug."_jituan",
  106. 'total'=>$total,
  107. 'total_online'=>$total_online,
  108. 'total_offline'=>$total - $total_online,
  109. 'rate'=>$rate
  110. ]);
  111. }
  112. $ins['title'] = $v->title;
  113. $ins['mine_code'] = $v->slug;
  114. $ins['total'] = $total;
  115. $ins['total_online'] = $total_online;
  116. $ins['total_offline'] = $total - $total_online;
  117. $ins['rate'] = $rate;
  118. if(strpos($v->title, '煤矿') !== false || strpos($v->title, '洗选') !== false){
  119. $ins['type'] = 1;
  120. }elseif(strpos($v->title, '煤制油') !== false || strpos($v->title, '烯烃') !== false || strpos($v->title, '甲醇') !== false || strpos($v->title, '精蜡') !== false){
  121. $ins['type'] = 2;
  122. }else{
  123. $ins['type'] = 3;
  124. }
  125. $ins['date'] = date('Y-m-d');
  126. DB::table('camera_status_history')->insert($ins);
  127. }
  128. if(isset($res)){
  129. array_push($data,[
  130. 'title'=>$res['title'],
  131. 'mine_code'=>$res['mine_code'],
  132. 'total'=>$res['total'],
  133. 'total_online'=>$res['total_online'],
  134. 'total_offline'=>$res['total_offline'],
  135. 'rate'=>$res['rate']
  136. ]);
  137. }
  138. }
  139. }