LogEmailSend.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use Illuminate\Support\Facades\Mail;
  5. use Illuminate\Support\Facades\DB;
  6. use Illuminate\Support\Facades\Input;
  7. class LogEmailSend extends Command
  8. {
  9. /**
  10. * The name and signature of the console command.
  11. *
  12. * @var string
  13. */
  14. protected $signature = 'log:emailsend';
  15. /**
  16. * The console command description.
  17. *
  18. * @var string
  19. */
  20. protected $description = '摄像头访问记录邮件发送';
  21. /**
  22. * Create a new command instance.
  23. *
  24. * @return void
  25. */
  26. public function __construct()
  27. {
  28. parent::__construct();
  29. }
  30. /**
  31. * Execute the console command.
  32. *
  33. * @return mixed
  34. */
  35. public function handle()
  36. {
  37. $this->logSendEmail();
  38. }
  39. //同步区域列表
  40. public function logSendEmail()
  41. {
  42. $email = DB::table('email')->get();
  43. $date = date("Y-m-d",strtotime("yesterday"));
  44. foreach($email as $k=>$v){
  45. $to = $v->email;
  46. $info = $this->loginfo($v);
  47. Mail::send('mail-msg',['info'=>$info], function ($message) use ($to,$date) {
  48. $message->to($to)->subject($date.'摄像头访问记录');
  49. });
  50. }
  51. }
  52. public function loginfo($email){
  53. $mine_count = DB::table('mine_list')->where('parent_id',0)->where('deleted_at',null)->get();
  54. $mine_array = explode(';',$email->mine_id_list);
  55. $mine_all = DB::table('mine_list')->where('deleted_at',null)->get();//所有区域
  56. $mine_use = [];//权限内矿区下所有区域
  57. foreach($mine_all as $k=>$v){
  58. if(in_array(explode('|',$v->degree)[0],$mine_array)){
  59. $mine_use[] = $v->id;
  60. }
  61. }
  62. $log_list = DB::table('log')
  63. ->select('log.*','mine_list.title','mine_list.degree','camera_list.camera_name')
  64. ->leftJoin('mine_list','mine_list.id','=','log.mine_id')
  65. ->leftJoin('camera_list','camera_list.id','=','log.camera_id')
  66. ->whereIn('log.mine_id',$mine_use)
  67. ->orderBy('camera_list.camera_name', 'desc');
  68. $mine_export = '';
  69. $mine_list = DB::table('mine_list')->whereIn('id',$mine_array)->get();
  70. foreach($mine_list as $k=>$v){
  71. $mine_export = $mine_export.$v->title.'和';
  72. }
  73. $mine_export = substr($mine_export,0,strlen($mine_export)-3).date("Y-m-d",strtotime("yesterday")).'访问记录';
  74. if(count($mine_list) == count($mine_count)){
  75. $mine_export = '全矿'.date("Y-m-d",strtotime("yesterday")).'访问记录';
  76. }
  77. $start_time = date("Y-m-d H:i:s",strtotime("yesterday"));
  78. $end_time = date("Y-m-d H:i:s",strtotime("today"));
  79. $log_list = $log_list->whereBetween('log.created_at',[$start_time,$end_time]);
  80. $log_list = $log_list->get()->toArray();
  81. if(count($log_list)>0){
  82. for($i=0;$i<count($log_list);$i++){
  83. $degree = explode('|',$log_list[$i]->degree);
  84. $mine = DB::table('mine_list')->where('id',$degree[0])->get();
  85. $log_list[$i]->mine_name = $mine[0]->title;
  86. if($log_list[$i]->status == 0){
  87. $log_list[$i]->status = '正常';
  88. }else{
  89. $log_list[$i]->status = '异常';
  90. }
  91. }
  92. }
  93. //去掉重复数据改为次数
  94. $new_log_list = [];
  95. foreach($log_list as $log){
  96. $repeat = 0;//不在新数组
  97. if(count($new_log_list)>0){
  98. for($i=0;$i<count($new_log_list);$i++){
  99. if($log->camera_name==$new_log_list[$i]['camera_name'] && $log->status==$new_log_list[$i]['status']){
  100. $new_log_list[$i]['count']++;
  101. $repeat = 1;//在新数组
  102. }
  103. }
  104. }
  105. if($repeat == 0){//不在新数组
  106. $size = count($new_log_list);
  107. $new_log_list[$size]['mine_name'] = $log->mine_name;
  108. $new_log_list[$size]['title'] = $log->title;
  109. $new_log_list[$size]['camera_id'] = $log->camera_id;
  110. $new_log_list[$size]['camera_name'] = $log->camera_name;
  111. $new_log_list[$size]['status'] = $log->status;
  112. $new_log_list[$size]['log'] = $log->log;
  113. $new_log_list[$size]['created_at'] = $log->created_at;
  114. $new_log_list[$size]['count'] = 1;
  115. }
  116. }
  117. $data['title'] = $mine_export;
  118. $data['content'] = $new_log_list;
  119. return $data;
  120. }
  121. }