CameraStatusHistory.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. $data = [];
  60. //宁煤下各矿在离线数量
  61. $mine_list = DB::table('mine_list')->where('parent_id',$ningmei[0]->id)->where('deleted_at',null)->get();
  62. foreach($mine_list as $k => $v){
  63. $mine_use = [];//宁煤下每个矿所有区域
  64. foreach($mine_all as $key=>$value){
  65. if(count(explode('|',$value->degree)) > 1){
  66. if(explode('|',$value->degree)[0] == $ningmei[0]->id && explode('|',$value->degree)[1] == $v->id){
  67. $mine_use[] = $value->id;
  68. }
  69. }
  70. }
  71. $total = DB::table('camera_list')->whereIn('mine_id',$mine_use)->where('is_show',1)->where('deleted_at',null)->count();
  72. $total_online = DB::table('camera_list')->whereIn('mine_id',$mine_use)->where('is_show',1)->where('camera_status',1)->where('deleted_at',null)->count();
  73. if($total == 0){
  74. $rate = "0";
  75. }else{
  76. $rate = round($total_online / $total * 100,2);
  77. }
  78. if($v->slug == 'NingXiaMeiYeTeShuZuoYeJianKongShiPin'){
  79. $res['title'] = $v->title;
  80. $res['mine_code'] = $v->slug."_jituan";
  81. $res['total'] = $total;
  82. $res['total_online'] = $total_online;
  83. $res['total_offline'] = $total - $total_online;
  84. $res['rate'] = $rate;
  85. }else{
  86. array_push($data,[
  87. 'title'=>$v->title,
  88. 'mine_code'=>$v->slug."_jituan",
  89. 'total'=>$total,
  90. 'total_online'=>$total_online,
  91. 'total_offline'=>$total - $total_online,
  92. 'rate'=>$rate
  93. ]);
  94. }
  95. }
  96. if(isset($res)){
  97. array_push($data,[
  98. 'title'=>$res['title'],
  99. 'mine_code'=>$res['mine_code'],
  100. 'total'=>$res['total'],
  101. 'total_online'=>$res['total_online'],
  102. 'total_offline'=>$res['total_offline'],
  103. 'rate'=>$res['rate']
  104. ]);
  105. }
  106. $timestamp = strtotime(date('Y-m-d H:i:s'));
  107. $hour = date('G', $timestamp);
  108. $minute = date('i', $timestamp);
  109. $month = date('n', $timestamp);
  110. $day = date('j', $timestamp);
  111. if($minute == '00'){
  112. $minute = '';
  113. }
  114. if ($hour < 12) {
  115. $time = $month."月".$day."号上午".$hour."时".$minute;
  116. } elseif ($hour >= 12 && $hour < 18) {
  117. $time = $month."月".$day."号下午".$hour."时".$minute;
  118. } else {
  119. $time = $month."月".$day."号晚上".$hour."时".$minute;
  120. }
  121. $unit_max_name = '';
  122. $unit_max_rate = 0;
  123. $unit_min_name = '';
  124. $unit_min_rate = 100;
  125. $mzy_max_name = '';
  126. $mzy_max_rate = 0;
  127. $mzy_min_name = '';
  128. $mzy_min_rate = 100;
  129. $other_max_name = '';
  130. $other_max_rate = 0;
  131. $other_min_name = '';
  132. $other_min_rate = 100;
  133. if(count($data)>0){
  134. for($i=0;$i<count($data);$i++){
  135. if (strpos($data[$i]['title'], '煤矿') !== false || strpos($data[$i]['title'], '洗选') !== false) {
  136. if($data[$i]['rate'] > $unit_max_rate){
  137. $unit_max_rate = $data[$i]['rate'];
  138. }
  139. if($data[$i]['rate'] < $unit_min_rate){
  140. $unit_min_rate = $data[$i]['rate'];
  141. }
  142. }elseif(strpos($data[$i]['title'], '煤制油') !== false || strpos($data[$i]['title'], '烯烃') !== false || strpos($data[$i]['title'], '甲醇') !== false || strpos($data[$i]['title'], '精蜡') !== false){
  143. if($data[$i]['rate'] > $mzy_max_rate){
  144. $mzy_max_rate = $data[$i]['rate'];
  145. }
  146. if($data[$i]['rate'] < $mzy_min_rate){
  147. $mzy_min_rate = $data[$i]['rate'];
  148. }
  149. }else{
  150. if($data[$i]['rate'] > $other_max_rate){
  151. $other_max_rate = $data[$i]['rate'];
  152. }
  153. if($data[$i]['rate'] < $other_min_rate){
  154. $other_min_rate = $data[$i]['rate'];
  155. }
  156. }
  157. }
  158. for($i=0;$i<count($data);$i++){
  159. if (strpos($data[$i]['title'], '煤矿') !== false || strpos($data[$i]['title'], '洗选') !== false) {
  160. if($data[$i]['rate'] == $unit_max_rate){
  161. $unit_max_name = $unit_max_name.'、'.$data[$i]['title'];
  162. }
  163. if($data[$i]['rate'] == $unit_min_rate){
  164. $unit_min_name = $unit_max_name.'、'.$data[$i]['title'];
  165. }
  166. }elseif(strpos($data[$i]['title'], '煤制油') !== false || strpos($data[$i]['title'], '烯烃') !== false || strpos($data[$i]['title'], '甲醇') !== false || strpos($data[$i]['title'], '精蜡') !== false){
  167. if($data[$i]['rate'] == $mzy_max_rate){
  168. $mzy_max_name = $mzy_max_name.'、'.$data[$i]['title'];
  169. }
  170. if($data[$i]['rate'] == $mzy_min_rate){
  171. $mzy_min_name = $mzy_max_name.'、'.$data[$i]['title'];
  172. }
  173. }else{
  174. if($data[$i]['rate'] == $other_max_rate){
  175. $other_max_name = $other_max_name.'、'.$data[$i]['title'];
  176. }
  177. if($data[$i]['rate'] == $other_min_rate){
  178. $other_min_name = $other_max_name.'、'.$data[$i]['title'];
  179. }
  180. }
  181. }
  182. }
  183. $unit_max_name = mb_substr($unit_max_name, 1);
  184. $unit_min_name = mb_substr($unit_min_name, 1);
  185. $mzy_max_name = mb_substr($mzy_max_name, 1);
  186. $mzy_min_name = mb_substr($mzy_min_name, 1);
  187. $other_max_name = mb_substr($other_max_name, 1);
  188. $other_min_name = mb_substr($other_min_name, 1);
  189. $result['data'] = '本周公司视频摄像头在线数'.$ningmei['total_online'].'个,'.'在线率为'.$ningmei['rate'].'%('.$time
  190. .'统计)。其中:①煤炭生产建设单位在线率排名第一的单位是'.$unit_max_name.',在线率达到'.$unit_max_rate
  191. .'%;排名末位的单位是'.$unit_min_name.',在线率达到'.$unit_min_rate
  192. .'%。②煤制油化工单位在线率排名第一的单位是'.$mzy_max_name.',在线率达到'.$mzy_max_rate.'%;排名末位的单位是'
  193. .$mzy_min_name.',在线率达到'.$mzy_min_rate.'%。③其他单位在线率排名第一的单位是'.$other_max_name
  194. .',在线率达到'.$other_max_rate.'%;排名末位的单位是'.$other_min_name.',在线率达到'.$other_min_rate.'%。';
  195. $ins['content'] = $result['data'];
  196. $ins['created_at'] = date('Y-m-d H:i:s');
  197. $ins['updated_at'] = date('Y-m-d H:i:s');
  198. DB::table('camera_status_history')->insert(['content'=>$ins]);
  199. }
  200. }