| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- <?php
- namespace App\Console\Commands;
- use Illuminate\Console\Command;
- use Illuminate\Support\Facades\Mail;
- use Illuminate\Support\Facades\DB;
- class LogEmailSend extends Command
- {
- /**
- * The name and signature of the console command.
- *
- * @var string
- */
- protected $signature = 'log:emailsend';
- /**
- * The console command description.
- *
- * @var string
- */
- protected $description = '摄像头访问记录邮件发送';
- /**
- * Create a new command instance.
- *
- * @return void
- */
- public function __construct()
- {
- parent::__construct();
- }
- /**
- * Execute the console command.
- *
- * @return mixed
- */
- public function handle()
- {
- $this->logSendEmail();
- }
- //同步区域列表
- public function logSendEmail()
- {
- $email = DB::table('email')->get();
- $date = date("Y-m-d",strtotime("yesterday"));
- foreach($email as $k=>$v){
- $to = $v->email;
- $info = $this->loginfo($v);
- Mail::send('mail-msg',['info'=>$info], function ($message) use ($to,$date) {
- $message->to($to)->subject($date.'摄像头访问记录');
- });
- }
- }
- public function loginfo($email){
- $mine_count = DB::table('mine_list')->where('parent_id',0)->where('deleted_at',null)->get();
- $mine_array = explode(';',$email->mine_id_list);
- $mine_all = DB::table('mine_list')->where('deleted_at',null)->get();//所有区域
- $mine_use = [];//权限内矿区下所有区域
- foreach($mine_all as $k=>$v){
- if(in_array(explode('|',$v->degree)[0],$mine_array)){
- $mine_use[] = $v->id;
- }
- }
- $log_list = DB::table('log')
- ->select('log.*','mine_list.title','mine_list.degree','camera_list.camera_name')
- ->leftJoin('mine_list','mine_list.id','=','log.mine_id')
- ->leftJoin('camera_list','camera_list.id','=','log.camera_id')
- ->whereIn('log.mine_id',$mine_use)
- ->orderBy('camera_list.camera_name', 'desc');
- $mine_export = '';
- $mine_list = DB::table('mine_list')->whereIn('id',$mine_array)->get();
- foreach($mine_list as $k=>$v){
- $mine_export = $mine_export.$v->title.'和';
- }
- $mine_export = substr($mine_export,0,strlen($mine_export)-3).date("Y-m-d",strtotime("yesterday")).'访问记录';
- if(count($mine_list) == count($mine_count)){
- $mine_export = '全矿'.date("Y-m-d",strtotime("yesterday")).'访问记录';
- }
- $start_time = date("Y-m-d H:i:s",strtotime("yesterday"));
- $end_time = date("Y-m-d H:i:s",strtotime("today"));
- $log_list = $log_list->whereBetween('log.created_at',[$start_time,$end_time]);
- $log_list = $log_list->get()->toArray();
- if(count($log_list)>0){
- for($i=0;$i<count($log_list);$i++){
- $degree = explode('|',$log_list[$i]->degree);
- $mine = DB::table('mine_list')->where('id',$degree[0])->get();
- $log_list[$i]->mine_name = $mine[0]->title;
- if($log_list[$i]->status == 0){
- $log_list[$i]->status = '正常';
- }else{
- $log_list[$i]->status = '异常';
- }
- }
- }
- //去掉重复数据改为次数
- $new_log_list = [];
- foreach($log_list as $log){
- $repeat = 0;//不在新数组
- if(count($new_log_list)>0){
- for($i=0;$i<count($new_log_list);$i++){
- if($log->camera_name==$new_log_list[$i]['camera_name'] && $log->status==$new_log_list[$i]['status']){
- $new_log_list[$i]['count']++;
- $repeat = 1;//在新数组
- }
- }
- }
- if($repeat == 0){//不在新数组
- $size = count($new_log_list);
- $new_log_list[$size]['mine_name'] = $log->mine_name;
- $new_log_list[$size]['title'] = $log->title;
- $new_log_list[$size]['camera_id'] = $log->camera_id;
- $new_log_list[$size]['camera_name'] = $log->camera_name;
- $new_log_list[$size]['status'] = $log->status;
- $new_log_list[$size]['log'] = $log->log;
- $new_log_list[$size]['created_at'] = $log->created_at;
- $new_log_list[$size]['count'] = 1;
- }
- }
- $data['title'] = $mine_export;
- $data['content'] = $new_log_list;
- return $data;
- }
- }
|