<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/7/25
 * Time: 14:24
 */


use \GatewayWorker\Lib\Gateway;
use Workerman\Lib\Timer;

class Mlogic
{
    private static $Instance = null;
    private static $db = null;
    private static $redis = null;
    private static $redisTime = null;
    public static $global = null;
    private static $configPath = null;

    public static function GetInstance()
    {
        if (!empty(self::$Instance)) {
            return self::$Instance;
        }

        self::$Instance = new self();
        self::$Instance->Init();

        return self::$Instance;
    }

    private static function Init()
    {
        $mds = DIRECTORY_SEPARATOR;
        if (strtolower(substr(PHP_OS, 0, 3)) == 'win') {
            self::$configPath = realpath(dirname(__FILE__) . $mds . '..' . $mds . '..' . $mds . '..' . $mds . '..') . $mds . 'application' . $mds;
        } else {
            self::$configPath = realpath(dirname(__FILE__) . $mds . '..' . $mds . '..' . $mds . '..' . $mds . '..' . $mds . '..') . $mds . 'application' . $mds;
        }

        self::getDb();
        self::getRedis();
        self::getGlbData();
    }

    //workman 的共享数据插件
    public static function getGlbData($cache = 1)
    {
        if ($cache) {
            if (!empty(self::$global)) {
                return self::$global;
            }
        }
        self::$global = new \GlobalData\Client('127.0.0.1:2207');
        return self::$global;
    }

    public static function getDb($cache = 1)
    {
        if ($cache) {
            if (!empty(self::$db)) {
                return self::$db;
            }
        }

        $conf = require(self::$configPath . 'database.php');
        self::$db = new \Workerman\MySQL\Connection($conf['hostname'], $conf['hostport'], $conf['username'], $conf['password'], $conf['database']);


        return self::$db;
    }

    //实例化redis
    public static function getRedis($cache = 1)
    {
        if ($cache) {
            if (!empty(self::$redis)) {
                return self::$redis;
            }
        }
        $conf = require(self::$configPath . 'redis.php');
        $redis = new \Redis();
        $ret = $redis->connect($conf['host'], $conf['port']);
        if (!$ret) {
            return false;
        }
        if (!empty($conf['passwd'])) {
            $ret = $redis->auth($conf['passwd']);
            if (!$ret) {
                return false;
            }
        }
        $redis->select($conf['db']);
        self::$redisTime = time();
        echo "Redis Reconnect : " . date("Y-m-d H:i:s") . "\n";

        self::$redis = $redis;
        return self::$redis;
    }

    //redis ping ，看是否断线
    public static function RedisPing()
    {
        $ret = self::$redis->ping();
        //echo "redisPing: " . date("Y-m-d H:i:s") . ' - ' . $ret . "\n";
        if (strpos(strtoupper($ret), "PONG") !== false) {
            return true;
        }
        return false;
    }

    //发送消息的代理
    public static function MySendMsg($clientId, $msg)
    {
        Gateway::sendToClient($clientId, $msg);
    }

    //客服是否正在登陆，或已经登陆 0未登陆   1正在登陆  2已登陆成功
    public static function userIsLogin($client_id, $kfuid, $groupid)
    {
        $hakey = self::$redis->hget("loginTmp:" . $kfuid, $kfuid);
        if ($hakey) {
            return 1;
        }

        $cids = Gateway::getClientIdByUid($kfuid);
        if (count($cids) > 0) {
            return 2;
        }

        return 0;
    }


}