<?php

namespace App\Modules\OfficialAccount\Models;

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

class WechatTemplateMsgs extends Model
{
    protected $tables = 'wechat_template_msgs';

    protected $fillable = ['template_id','name','send_time','appid','template_content','redirect_url','status','remark','distribution_channel_id','subscribe_time','sex','balance','order_type','category_id','user_num','del_flag','is_full_send','is_show_list','description','book_name','chapter_name','is_activity'];

    /**
     * 获取单个模板消息，根据模板id
     */
    static function wechatTemplateMsgsByTemplateId($template_id)
    {
        
       	return self::where('template_id', isset($template_id) ? $template_id : '')->first();
    
    }
    
    /**
     * 获取全部模板消息，根据模板id
     */
    static function allWechatTemplateMsgsByTemplateId($template_id)
    {
    
    	return self::where('template_id', $template_id)->get();
    
    }
    
    /**
     * 更新模板消息template_id
     */
    static function updateWechatTemplateMsgsByTemplateId($new_template_id,$old_template_id)
    {
    	return DB::table('wechat_template_msgs')->where('template_id', $old_template_id)->update(['template_id'=>$new_template_id,'updated_at'=>date('Y-m-d H:i:s')]);
    
    }

    /**
     * 根据状态获取模板消息
     */
    static function wechatTemplateMsgsByStatusStandBy($status)
    {

        return self::where(['status'=>$status,'del_flag'=>0])->get();

    }

    /**
     * 根据id获取模板消息
     */
    static function wechatTemplateMsgsById($id)
    {
        
        return self::where('id', isset($id) ? $id : '')->first();
    
    }

    /**
     * 判断1小时内是否有相同的模板消息插入过
     */
    static function isSendTemplateAtSameTime($distribution_channel_id,$appid,$send_time,$template_id)
    {
        
        return self::where(['distribution_channel_id'=>$distribution_channel_id,'appid'=>$appid,'del_flag'=>0,'template_id'=>$template_id])->where('status','!=','4')->where('send_time','<',date('Y-m-d H:i:s',strtotime($send_time)+3600))->where('send_time','>',date('Y-m-d H:i:s',strtotime($send_time)-3600))->first();
    
    }

    /**
     * 根据appid获取模板消息 is_show_list为1则界面展示
     */
    static function wechatTemplateMsgsBydistributionChannelId($distribution_channel_id)
    {
        
       	return self::where('distribution_channel_id', isset($distribution_channel_id) ? $distribution_channel_id : '')->where('del_flag',0)->where('is_show_list',1)->orderBy('id','desc')->paginate();
    	
    }
    
    /**
     * 根据分页获取模板消息
     */
    static function wechatTemplateMsgsPaginate($start_time,$end_time,$is_all=false)
    {
    	\Log::info('$wechatTemplateMsgsPaginate');
    	$search_obj = self::select('wechat_template_msgs.distribution_channel_id as site_id',
    			'wechat_template_msgs.name as task_name',
    			DB::raw('date(wechat_template_msgs.created_at) as created_date'),
    			'wechat_template_msgs.send_time',
    			'wechat_template_msgs.user_num as suc_send_num',
//     			DB::raw('"" as template_name'),
    			DB::raw('(select t.title from wechat_templates w inner join wechat_public_templates t ON w.common_template_id=t.common_template_id where w.template_id=wechat_template_msgs.template_id limit 1) as template_name'),
    			DB::raw('"" as tag'),
    			DB::raw('(select group_concat(nickname) from official_accounts where distribution_channel_id=wechat_template_msgs.distribution_channel_id group by distribution_channel_id)  as  official_account_name')//official_account_name
    	)
    	->where(['del_flag'=>0,'status'=>3])
    	->where('wechat_template_msgs.created_at','>=',$start_time)
    	->where('wechat_template_msgs.created_at','<',$end_time)
    	->orderBy('wechat_template_msgs.id','desc');
    	
    	if($is_all)
    	{
    		return $search_obj->get();
    	}else{
    		return $search_obj->paginate();
    	}
    }

    function wechatTemplates(){

        return $this->hasOne('App\Modules\OfficialAccount\Models\WechatTemplates','template_id','template_id');
    
    }
    
    static function searchWechatTemplateMsgs($param,$is_all=false){
    	 
    	$res =  self::where('distribution_channel_id', $param['distribution_channel_id'])
    	->select('*')
    	->where('del_flag',0)
    	->where('is_show_list',1);
    
    	if(isset($param['start_time']) && $param['start_time']){
    		$res->where('wechat_template_msgs.created_at', '>=', $param['start_time']);
    	}
    	if(isset($param['end_time']) && $param['end_time']){
    		$res->where('wechat_template_msgs.created_at', '<=', $param['end_time']);
    	}
    	if(isset($param['book_name']) && $param['book_name']){
    		$res->where('wechat_template_msgs.book_name', 'like', '%'.$param['book_name'].'%');
    	}
    	if(isset($param['task_name']) && $param['task_name']){
    		$res->where('wechat_template_msgs.name', 'like', '%'.$param['task_name'].'%');
    	}

    	if ($is_all){
            $wechat_template_msgs = $res->orderBy('id','desc')->get();
        }else{
            $wechat_template_msgs = $res->orderBy('id','desc')->paginate();
        }
    	return $wechat_template_msgs;
    }

}


