LogEmailSend.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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->loginfo($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 loginfo($email){
  52. $mine_count = DB::table('mine_list')->where('parent_id',0)->where('deleted_at',null)->get();
  53. $mine_array = explode(';',$email->mine_id_list);
  54. $mine_all = DB::table('mine_list')->where('deleted_at',null)->get();//所有区域
  55. $mine_use = [];//权限内矿区下所有区域
  56. foreach($mine_all as $k=>$v){
  57. if(in_array(explode('|',$v->degree)[0],$mine_array)){
  58. $mine_use[] = $v->id;
  59. }
  60. }
  61. $log_list = DB::table('log')
  62. ->select('log.*','mine_list.title','mine_list.degree','camera_list.camera_name')
  63. ->leftJoin('mine_list','mine_list.id','=','log.mine_id')
  64. ->leftJoin('camera_list','camera_list.id','=','log.camera_id')
  65. ->whereIn('log.mine_id',$mine_use)
  66. ->orderBy('camera_list.camera_name', 'desc');
  67. $mine_export = '';
  68. $mine_list = DB::table('mine_list')->whereIn('id',$mine_array)->get();
  69. foreach($mine_list as $k=>$v){
  70. $mine_export = $mine_export.$v->title.'和';
  71. }
  72. $mine_export = substr($mine_export,0,strlen($mine_export)-3).date("Y-m-d",strtotime("yesterday")).'访问记录';
  73. if(count($mine_list) == count($mine_count)){
  74. $mine_export = '全矿'.date("Y-m-d",strtotime("yesterday")).'访问记录';
  75. }
  76. $start_time = date("Y-m-d H:i:s",strtotime("yesterday"));
  77. $end_time = date("Y-m-d H:i:s",strtotime("today"));
  78. $log_list = $log_list->whereBetween('log.created_at',[$start_time,$end_time]);
  79. $log_list = $log_list->get()->toArray();
  80. if(count($log_list)>0){
  81. for($i=0;$i<count($log_list);$i++){
  82. $degree = explode('|',$log_list[$i]->degree);
  83. $mine = DB::table('mine_list')->where('id',$degree[0])->get();
  84. $log_list[$i]->mine_name = $mine[0]->title;
  85. if($log_list[$i]->status == 0){
  86. $log_list[$i]->status = '正常';
  87. }else{
  88. $log_list[$i]->status = '异常';
  89. }
  90. }
  91. }
  92. //去掉重复数据改为次数
  93. $new_log_list = [];
  94. foreach($log_list as $log){
  95. $repeat = 0;//不在新数组
  96. if(count($new_log_list)>0){
  97. for($i=0;$i<count($new_log_list);$i++){
  98. if($log->camera_name==$new_log_list[$i]['camera_name'] && $log->status==$new_log_list[$i]['status']){
  99. $new_log_list[$i]['count']++;
  100. $repeat = 1;//在新数组
  101. }
  102. }
  103. }
  104. if($repeat == 0){//不在新数组
  105. $size = count($new_log_list);
  106. $new_log_list[$size]['mine_name'] = $log->mine_name;
  107. $new_log_list[$size]['title'] = $log->title;
  108. $new_log_list[$size]['camera_id'] = $log->camera_id;
  109. $new_log_list[$size]['camera_name'] = $log->camera_name;
  110. $new_log_list[$size]['status'] = $log->status;
  111. $new_log_list[$size]['log'] = $log->log;
  112. $new_log_list[$size]['created_at'] = $log->created_at;
  113. $new_log_list[$size]['count'] = 1;
  114. }
  115. }
  116. $data['title'] = $mine_export;
  117. $data['content'] = $new_log_list;
  118. return $data;
  119. }
  120. }