<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/5/21
 * Time: 16:57
 */

namespace app\logic\cmdpro;

use app\lib\DataPack;
use app\lib\GlobConfigs;
use app\logic\cmdpro\CmdInf as CmdInf;
use Illuminate\Database\Capsule\Manager as Capsule;
use Illuminate\Database\Capsule\Manager as DB;

use app\logic\MyPgsql as MyPgsql;

use swoole;

class CmdTest extends CmdInf
{
    static $pgsqlObj = '';

    //异步回调实现 mysql
    public function sqlold($serv, $task)
    {
        $db = new \swoole_mysql();
        $config = GlobConfigs::getKey('mysql');
        $db->connect($config, function (\swoole_mysql $db, $r) use ($serv, $task) {
            if ($r === false) {
                $serv->push($task->data->fd, json_encode(['data' => $task->data, 'err' => [$db->connect_errno, $db->connect_error], 'fd' => $task->data->fd], 256));
                return false;
            }
            $packdata = json_decode($task->data->data, true);
            $sql = 'select *  from bm_admin where admin_id=' . intval($packdata['data']);
            $db->query($sql, function (\swoole_mysql $db, $r2) use ($serv, $task) {
                if ($r2) {
                    $serv->push($task->data->fd, json_encode(['record' => $r2, 'fd' => $task->data->fd], 256));
                }
                $db->close();
            });
        });
    }

    //协程实现mysql
    public function mysqlgo($serv, $task)
    {
        $swoole_mysql = new \Swoole\Coroutine\MySQL();
        $config = GlobConfigs::getKey('mysql');
        $swoole_mysql->connect($config);

        $Pack = json_decode($task->data->data, true);
        $DataArr = $Pack['data'];
        $table = isset($DataArr['table']) ? $DataArr['table'] : '';
        $field = isset($DataArr['field']) ? $DataArr['field'] : '';
        $value = isset($DataArr['value']) ? $DataArr['value'] : '';
        if (empty($task) || empty($field)) {
            return;
        }

        try {
            $ret = $swoole_mysql->query("select  *  from   $table where  $field='$value' ");
            if ($ret) {
                $this->sendTo($serv, $task->data->fd, DataPack::toJson(['data' => $ret]));
            }
        } catch (\Exception $e) {
            echo "Exception:" . $e->getCode() . ' - ' . $e->getMessage();
        }
        $swoole_mysql->close();
    }


    //协程redis
    public function redis($serv, $task)
    {
        $config = GlobConfigs::getKey('redis');
        go(function () use ($serv, $task, $config) {
            $redis = new \Swoole\Coroutine\Redis();
            $redis->connect($config['host'], $config['port']);
            $redis->set('test', date('Y-m-d H:i:s'));
            $val = $redis->get('test');
            $this->sendTo($serv, $task->data->fd, DataPack::toJson(['data' => $val]));
        });
    }

    public function broad($serv, $task)
    {
        $pack = json_decode($task->data->data, true);
        $msg = $pack['data'];
        $this->onlineBroad($serv, $msg . ' ' . $this->dotest());
    }

    public function dotick($serv, $task)
    {
        $pack = json_decode($task->data->data, true);
        $data = $pack['data'];
        $fid = isset($data['fid']) ? intval($data['fid']) : 0;
        $msg = isset($data['msg']) ? $data['msg'] : '';
        if (!$serv->exist($fid)) {
            $this->sendTo($serv, $task->data->fd, '此用户不在线!' . $fid);
            return;
        }
        if ($fid == $task->data->fd) {
            $this->sendTo($serv, $task->data->fd, '自己不能踢自己!');
            return;
        }
        $this->sendTo($serv, $fid, $msg);
        $serv->disconnect($fid);
        return;
    }


    //时间同步用
    public function timeRsync($serv, $task)
    {
        $dataArr = json_decode($task->data->data, true);
        $msg = DataPack::toJson(['mtype' => 'system_msg', 'stype' => 'TimeSync', 'data' => $dataArr['data']]);
        $this->onlineBroad($serv, $msg);
        return;
    }


    public function pgsql($serv, $task)
    {
        $dataArr = json_decode($task->data->data, true);
        $config = GlobConfigs::getKey('pgsql');
        go(function () use ($serv, $task, $config, $dataArr) {
            $conf = $config;
            $mysql = new Capsule();
            $mysql->addConnection($conf);
            $mysql->setAsGlobal();
            $mysql->bootEloquent();

            $ret = DB::table('account')->where('id', 83859)->first();
            $this->sendTo($serv, $task->data->fd, DataPack::toJson(['mtype' => 'system', 'data' => $ret]));

        });

    }


    public function pgsqlquery($serv, $task)
    {
        $dataArr = json_decode($task->data->data, true);
        $ret = MyPgsql::getInstance()->getDb()->table('members')->where('id', intval($dataArr['data']))->first();
        $this->sendTo($serv, $task->data->fd, DataPack::toJson(['mtype' => 'system', 'data' => $ret]));
    }

    public function pgsqlexec($serv, $task)
    {
        $dataArr = json_decode($task->data->data, true);
        $ret = MyPgsql::getInstance()->getDb()->table('members')->where('id', 57)->update(['name' => '12-' . $dataArr['data']]);
        $this->sendTo($serv, $task->data->fd, DataPack::toJson(['mtype' => 'system', 'data' => $ret]));
    }


    private function dotest()
    {
        return " hellow,baby,at " . date("Y-m-d H:i:s");
    }


    public function odds($serv, $task)
    {

        echo "进来了。。。。。。。。。。。。。。。。。。\n";

        $now = microtime(true);
        $datas = json_decode($task->data->data, true)['data'];
        print_r($datas);
        echo "Cost: " . (microtime(true) - $now) * 1000 . " 毫秒\r\n";
        $pgsql = $this->getPgsqlObj();

        $ret1 = DB::table("st_zq_local_league")->where(['others_lg_id'=>138112,'source'=>'hg0088'])->first();
        $ret2 = DB::table("st_zq_local_match")->where(['others_match_id'=>3246051,'source'=>'hg3535'])->first();
        if (!$ret1 || !$ret2){
            echo "数据有错,返回..\n";
            return ;
        }
        $ret3 = DB::table("st_zq_league")->where(['id'=>$ret1->lg_id])->first();
        $ret4 = DB::table("st_zq_odds")->where(['odds_only'=>'41b995867e606e7bdea634d26d82a69b'])->first();
        $ret5 = DB::table("st_zq_odds")->where(['odds_only'=>'41b995867e606e7bdea634d26d82a69b'])->update(['ctime'=>date("Y-m-d H:i:s")]);


        print_r([$ret1,$ret2,$ret3,$ret4,$ret5]);


        echo "Cost: " . (microtime(true) - $now) * 1000 . " 毫秒\r\n";


    }

    private function getPgsqlObj()
    {
        if (self::$pgsqlObj == '') {
            echo '重建getPgsqlObj......'."\n";
            $config = GlobConfigs::getKey('pgsql');
            $mysql = new Capsule();
            $mysql->addConnection($config);
            $mysql->setAsGlobal();
            $mysql->bootEloquent();
            self::$pgsqlObj = $mysql;
            return $mysql;
        }
        echo '直接返回 getPgsqlObj......'."\n";
        return self::$pgsqlObj;
    }


}