<?php

namespace App\Modules\OfficialAccount\Models;

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

class BatchCustomSendMsgs extends Model
{
    protected $tables = 'batch_custom_send_msgs';
    protected $fillable = ['name','send_time','content','redirect_url','status','subscribe_time','sex','balance','order_type','category_id','del_flag','is_full_send'];
    
    
    /**
     * 判断1小时内是否有相同的客服消息插入过
     */
    static function isSendCustomerAtSameTime($send_time)
    {
    
    	return self::where(['del_flag'=>0])->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();
    
    }
    
    /**
     * 判断1小时内是否有相同的客服消息插入过，不同性别
     */
    static function isSendCustomerAtSameTimeSex($send_time,$sex)
    {
    
    	return self::where(['del_flag'=>0])->where('sex',$sex)->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();
    
    }

    /**
     * 判断1小时内是否有相同的客服消息插入过，不同性别
     */
    static function isSendCustomerAtSameTimeSexName($send_time,$sex,$name)
    {

        return self::where(['del_flag'=>0])->where('sex',$sex)->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))
            ->where('name',$name)->first();

    }
    /**
     * 根据渠道获取关键字列表
     */
    static function batchCustomMsgList()
    {
    	return self::where('del_flag', 0)->select('batch_custom_send_msgs.*','batch_custom_send_msgs.created_at as create_time')->orderBy('id','desc')->paginate(2);
    }
    
  
    /**
     * 根据id获取
     */
    static function batchCustomMsgById($id)
    {
        return self::where('id', $id)->first();
    }
    
    /**
    * 更新状态
    */
    static function updateBatchCustomSendMsgStatusById($id,$status,$del_flag=0)
    {
    	return self::where('id', $id)->update(['status'=>$status,'del_flag'=>$del_flag,'updated_at'=>date('Y-m-d H:i:s')]);
    }
    

}


