<?php

namespace App\Modules\OfficialAccount\Models;

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

class TempForceSubscribeUsers extends Model
{
    protected $tables = 'temp_force_subscribe_users';

    protected $fillable = ['uid', 'distribution_channel_id', 'bid','is_subscribed','subscribe_time','unsubscribe_time','appid', 'openid', 'last_interactive_time'];

    /**
     * 根据openid和appid获取强关用户
     */
    static function forceSubscribeUsersByAppidAndOpenId($appid, $openid)
    {

        return self::where(['appid' => isset($appid) ? $appid : '', 'openid' => isset($openid) ? $openid : ''])->first();

    }
    
    /**
     * 删除N天前的数据，保持最近几天热数据
     */
    static function deleteTempForceSubscribeUsersByTime($end_time)
    {
    	return self::where('last_interactive_time','<=',$end_time)->delete();
    }

    /**
     * 获取所有强关用户
     */
    static function forceSubscribeUsersAllOf()
    {

        return self::where('is_subscribed', 1)->get();

    }
    
    /**
     * 根据uid获取强关用户
     */
    static function forceSubscribeUsersByUid($uid)
    {

        return self::where(['uid' => $uid, 'is_subscribed' => 1])->first();

    }


    /**
     * 根据openid获取一个强关用户
     */
    static function forceSubscribeUsersByOpenid($openid, $appid = '')
    {

        return self::where(['openid' => $openid, 'is_subscribed' => 1])->first();

    }

    /**
     * openid获取强关用户
     */
    static function getOneForceSubscribeUsersByOpenid($openid)
    {

        return self::where('openid', $openid)->first();

    }

    /**
     * 根据openid获取强关用户
     */
    static function forceSubscribeUsersByOpenidAndChannelId($openid, $distribution_channel_id)
    {

        return self::where(['openid' => $openid, 'distribution_channel_id' => $distribution_channel_id])->first();

    }

    /**
     * 根据openid获取一个取关用户
     */
    static function forceUnSubscribeUsersByOpenid($openid)
    {

        return self::where(['openid' => $openid, 'is_subscribed' => 0])->first();

    }

    static function forceSubscribeUsersByOpenidAndAC($openid, $appid = '', $distribution_channel_id = '')
    {
        $query = self::where('openid', $openid);
        if ($appid) {
            # code...
            $query->where('appid', $appid);
        }
        if ($distribution_channel_id) {
            # code...
            $query->where('distribution_channel_id', $distribution_channel_id);
        }


        return $query->first();

    }

    /**
     * 根据openid获取全部取关用户
     */
    static function forceAllUnSubscribeUsersByOpenid($openid)
    {

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

    }


    /**
     * 根据appid获取强关用户
     */
    static function forceSubscribeUsersByAppid($appid)
    {

        return self::where(['appid' => $appid, 'is_subscribed' => 1])->get();

    }
    
    /**
     * 根据appid获取强关用户数量
     */
    static function forceSubscribeUsersCountByAppid($appid)
    {
    
    	return self::where(['appid' => $appid, 'is_subscribed' => 1])->count();
    
    }
  

}
