LogEmailSend.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. class LogEmailSend extends Command
  7. {
  8. /**
  9. * The name and signature of the console command.
  10. *
  11. * @var string
  12. */
  13. protected $signature = 'log:emailsend';
  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->logSendEmail();
  37. }
  38. //同步区域列表
  39. public function logSendEmail()
  40. {
  41. $email = DB::table('email')->get();
  42. $date = date("Y-m-d",strtotime("yesterday"));
  43. foreach($email as $k=>$v){
  44. $to = $v->email;
  45. $info = $this->info($v);
  46. Mail::send('mail-msg',['info'=>$info], function ($message) use ($to,$date) {
  47. $message->to($to)->subject($date.'摄像头访问记录');
  48. });
  49. }
  50. }
  51. public function info($email){
  52. $mine_array = explode(';',$email->mine_id_list);
  53. $mine_all = DB::table('mine_list')->where('deleted_at',null)->get();//所有区域
  54. $mine_use = [];//权限内矿区下所有区域
  55. foreach($mine_all as $k=>$v){
  56. if(in_array(explode('|',$v->degree)[0],$mine_array)){
  57. $mine_use[] = $v->id;
  58. }
  59. }
  60. $log_list = DB::table('log')
  61. ->select('log.*','mine_list.title','mine_list.degree','camera_list.camera_name')
  62. ->leftJoin('mine_list','mine_list.id','=','log.mine_id')
  63. ->leftJoin('camera_list','camera_list.id','=','log.camera_id')
  64. ->whereIn('log.mine_id',$mine_use)
  65. ->orderBy('camera_list.camera_name', 'desc');
  66. $mine_export = '';
  67. $mine_list = DB::table('mine_list')->whereIn('id',$mine_array)->get();
  68. foreach($mine_list as $k=>$v){
  69. $mine_export = $mine_export.$v->title.'和';
  70. }
  71. $mine_export = substr($mine_export,0,strlen($mine_export)-3).date("Y-m-d",strtotime("yesterday")).'访问记录';
  72. $start_time = date("Y-m-d H:i:s",strtotime("yesterday"));
  73. $end_time = date("Y-m-d H:i:s",strtotime("today"));
  74. $log_list = $log_list->whereBetween('log.created_at',[$start_time,$end_time]);
  75. $log_list = $log_list->get()->toArray();
  76. if(count($log_list)>0){
  77. for($i=0;$i<count($log_list);$i++){
  78. $degree = explode('|',$log_list[$i]->degree);
  79. $mine = DB::table('mine_list')->where('id',$degree[0])->get();
  80. $log_list[$i]->mine_name = $mine[0]->title;
  81. if($log_list[$i]->status == 0){
  82. $log_list[$i]->status = '正常';
  83. }else{
  84. $log_list[$i]->status = '异常';
  85. }
  86. }
  87. }
  88. //去掉重复数据改为次数
  89. $new_log_list = [];
  90. foreach($log_list as $log){
  91. $repeat = 0;//不在新数组
  92. if(count($new_log_list)>0){
  93. for($i=0;$i<count($new_log_list);$i++){
  94. if($log->camera_name==$new_log_list[$i]['camera_name'] && $log->status==$new_log_list[$i]['status']){
  95. $new_log_list[$i]['count']++;
  96. $repeat = 1;//在新数组
  97. }
  98. }
  99. }
  100. if($repeat == 0){//不在新数组
  101. $size = count($new_log_list);
  102. $new_log_list[$size]['mine_name'] = $log->mine_name;
  103. $new_log_list[$size]['title'] = $log->title;
  104. $new_log_list[$size]['camera_id'] = $log->camera_id;
  105. $new_log_list[$size]['camera_name'] = $log->camera_name;
  106. $new_log_list[$size]['status'] = $log->status;
  107. $new_log_list[$size]['log'] = $log->log;
  108. $new_log_list[$size]['created_at'] = $log->created_at;
  109. $new_log_list[$size]['count'] = 1;
  110. }
  111. }
  112. $data['title'] = $mine_export;
  113. $data['content'] = $new_log_list;
  114. return $data;
  115. }
  116. }