<?php

namespace App\Modules\PersonalOp\Models;

use Illuminate\Database\Eloquent\Model;
use DB;

class MediaPushUser extends Model
{
    protected  $table = 'media_push_users';
    protected  $fillable = ['uid','distribution_channel_id','appid','current_month','sub_time','unsub_time','active_hour'];
    
    public static function get_media_push_users($distribution_channel_id,$appid,$active_hour) {
    	$push_users = self::where(['distribution_channel_id'=>$distribution_channel_id,'appid'=>$appid,'active_hour'=>$active_hour])->get();

    	return $push_users;
    }
    
    public static function get_media_push_users_by_uid($uids,$send_time) {
    	$push_users = self::leftJoin('media_push_user_details','media_push_users.id','media_push_user_details.media_push_user_id')
    	->select('media_push_users.id','media_push_users.uid','media_push_users.openid','media_push_users.appid',
    			'media_push_users.distribution_channel_id','media_push_user_details.id as media_push_user_detail_id',
    			'media_push_user_details.is_send','media_push_user_details.status','media_push_user_details.sort_no')
    	->whereIn('media_push_users.uid',$uids)
    	->where('media_push_user_details.send_time',$send_time)
    	->get();
    
    	return $push_users;
    }
    
    public static function get_active_hour_media_push_users($active_hour) {
    	$now = date('Y-m-d H:i:s');
    	$today = date('Y-m-d');
    	$push_users = DB::select('select m.uid,m.appid,m.distribution_channel_id,d.sort_no,d.id as send_id
    			from media_push_users m 
    			left join media_push_user_details d ON m.id = d.media_push_user_id
    			where m.active_hour = '.$active_hour.'
    			and d.is_send = 0
    			and d.status = 1
    			and d.send_time = "'.$today.'"
    			');
    	$last_push_users = [];
    	if(!empty($push_users)){
    		foreach($push_users as $key=>$push_user){
    			$last_push_users[$push_user->appid][] = $push_user;
    		}
    	}
    
    	return $push_users;
    }

}
