vali 6 years ago
parent
commit
dbe4a10bc5

+ 7 - 3
app/Logic/WinfailLogic.php

@@ -15,8 +15,12 @@ use  App\Lib\Settlement\Adapter\LqRule;
 use  App\Lib\Settlement\Adapter\WqRule;
 use  App\Lib\Settlement\Adapter\BqRule;
 
+use datainf\pplus\Instance;
+
 class WinfailLogic
 {
+    use Instance;
+
     //类型映射
     public $gameAllMap = [
         'zq' => 'ZqRule',
@@ -120,10 +124,10 @@ class WinfailLogic
     public function getGjDatas($matchModel)
     {
         $table = 'st_' . $matchModel->game_code . '_league_result';
-        $where = ['lg_id' => $matchModel->lg_id, 'game_name' => $matchModel->odds_code];
+        $where = ['lg_id' => $matchModel->lg_id, 'game_name' => $matchModel->game_code];
         $model = DB::table($table)->where($where)->first();
-        if (count($model) <= 0) {
-            throw new  \Exception('冠军数据没找到!');
+        if (empty($model)) {
+            return false;
         }
         return $model;
     }

+ 1 - 1
commands/mysql_poole_client_test.php

@@ -18,7 +18,7 @@ $client    = new swoole_client(SWOOLE_SOCK_TCP);
 $client->set(array(
     'socket_buffer_size'     => 1024*1024*8, //2M缓存区
 ));
-$rts= $client->connect('192.168.2.200', $config['poole_port'], 10) or die("连接失败");//链接mysql客户端
+$rts= $client->connect('127.0.0.1', $config['poole_port'], 10) or die("连接失败");//链接mysql客户端
 
 
 

+ 163 - 10
datainf/logic/HttpServerSettelement.php

@@ -12,18 +12,25 @@ use App\Lib\ModelBase;
 use App\Http\Response\Response;
 
 use App\Logic\WinfailLogic;
+use datainf\lib\GlobConfigs;
 use Illuminate\Database\Capsule\Manager as DB;
+use swoole;
 
 class HttpServerSettelement
 {
     private $httpserver;
     private $config;
+    private $dbpooleconfig = [];
+    private $redisonfig = [];
+    const   SQLKEY = 'ALLSQLKEY';
 
     public function __construct($config)
     {
         $this->httpserver = new \swoole\http\server($config['host'], $config['port']);
         $this->httpserver->set($config['sets']);
         $this->config = $config;
+        $this->dbpooleconfig = GlobConfigs::getKey('pgsqlpoole');
+        $this->redisonfig = GlobConfigs::getKey('redis');
 
         $taskWorkingNum = new \swoole\Atomic();
         $this->httpserver->taskWorkingNum = $taskWorkingNum;
@@ -48,6 +55,12 @@ class HttpServerSettelement
             $this->InitDb();
         }
 
+        if ($worker_id == 0) {
+            $obj = $this;
+            Swoole\Timer::tick(1000, function ($id) use ($obj) {
+                $obj->BatchSqlRedisToDB();
+            });
+        }
     }
 
 
@@ -72,6 +85,17 @@ class HttpServerSettelement
             return;
         }
 
+        $check_token = true;
+        if ($check_token) {
+            $token = isset($paras['token']) ? $paras['token'] : '';
+            if (empty($token) || empty($this->Tokencheck($token))) {
+                $data = Response::generate('', 6, '', '无效的token');
+                $response->end($data);
+                return;
+            }
+        }
+
+
         if ($request_uri == 'WinFail') {
             return $this->doWinFailse($request, $response, $paras);
         }
@@ -100,29 +124,48 @@ class HttpServerSettelement
     }
 
 
+    private function Tokencheck($token)
+    {
+        $tokenvel = DB::table('system_user')->where(['token' => $token])->first();
+        return $tokenvel;
+    }
+
+
     //胜负计算
     private function doWinFailse($request, $response, $paras)
     {
-
         $logic_obj = new WinfailLogic();
         $id = intval($paras['noticeid']);
-
         try {
             $noticeModel = $logic_obj->getNoticeDate($id);
-            $AdapterObj = $logic_obj->getAdapterObj($noticeModel->game_code);
             $result = $logic_obj->getCompResult($noticeModel->game_code, $noticeModel->match_id);
             $moneySimples = $logic_obj->getSimplexData($noticeModel->game_code, $noticeModel->match_id);
             $matchs_1 = $logic_obj->getMatchRecords($noticeModel->game_code, $noticeModel->match_id, 1);
             $matchs_2 = $logic_obj->getMatchRecords($noticeModel->game_code, $noticeModel->match_id, 2);
-            $RefClass = new  \ReflectionClass($AdapterObj);
+
+            $match_firstModel = isset($matchs_1['0']) ? $matchs_1['0'] : (isset($matchs_2['0']) ? isset($matchs_2['0']) : false);
+            if (!$match_firstModel) {
+                return false;
+            }
+            $gjModel = $logic_obj->getGjDatas($match_firstModel);
+
+            if (strtolower($match_firstModel->p_code) == 'gj') {
+                if (empty($gjModel)) {
+                    throw new  \Exception("没有冠军数据数据--match_id" . $noticeModel->match_id);
+                }
+                $result = [$gjModel];
+            }
 
             if (!empty($matchs_1)) {
                 while (true) {
                     if ($this->httpserver->taskWorkingNum->get() > intval($this->config['sets']['task_worker_num'])) {
-                        co::sleep(100);
+                        co::sleep(50);
                     } else {
                         $now = array_shift($matchs_1);
-                        $paras = ['type' => 'doWinFailse', 'stype' => 1, 'matchModel' => $now, 'RefClass' => $RefClass, 'result' => $result, 'moneySimple' => $moneySimples[$now->batch_id][$now->match_id]];
+                        if (empty($now)) {
+                            break;
+                        }
+                        $paras = ['type' => 'doWinFailse', 'stype' => 1, 'matchModel' => $now, 'result' => $result, 'moneySimple' => $moneySimples[$now->batch_id][$now->match_id]];
                         $this->onTask($this->httpserver, $paras);
                     }
                 }
@@ -131,24 +174,33 @@ class HttpServerSettelement
             if (!empty($matchs_2)) {
                 while (true) {
                     if ($this->httpserver->taskWorkingNum->get() > intval($this->config['sets']['task_worker_num'])) {
-                        co::sleep(100);
+                        co::sleep(50);
                     } else {
                         $now = array_shift($matchs_2);
-                        $paras = ['type' => 'doWinFailse', 'stype' => 1, 'matchModel' => $now, 'RefClass' => $RefClass, 'result' => $result, 'moneySimple' => $moneySimples[$now->batch_id][$now->match_id]];
+                        if (empty($now)) {
+                            break;
+                        }
+                        $paras = ['type' => 'doWinFailse', 'stype' => 1, 'matchModel' => $now, 'result' => $result, 'moneySimple' => $moneySimples[$now->batch_id][$now->match_id]];
                         $this->onTask($this->httpserver, $paras);
                     }
                 }
             }
             unset($logic_obj, $noticeModel, $AdapterObj, $result, $moneySimples, $matchs_1, $matchs_2, $RefClass);
 
+            $data = Response::generate('', 1, '', 'ok ret' . $request->server['request_time_float'] . '-' . microtime(true));
+            $response->end($data);
+            return;
+
         } catch (\Exception $e) {
             unset($logic_obj);
-            $data = Response::generate('', 10, '', $e->getMessage());
+            $data = Response::generate('', 10, '', $e->getMessage() . '--' . $e->getFile() . '--' . $e->getLine());
             $response->end($data);
+            return;
         }
         return;
     }
 
+
     //部分结算
     private function doSettelementSome($request, $response, $paras)
     {
@@ -166,8 +218,109 @@ class HttpServerSettelement
     /////
     private function Task_winfalse($serv, $task)
     {
-            echo "IN Task_winfalse".print_r([$task])."\n";
+        $moneyBuy = $task['moneySimple'];
+        $matchModel = $task['matchModel'];
+
+        $logic = WinfailLogic::getInstance();
+        $AdapterObj = $logic->getAdapterObj($matchModel->game_code);
+        $RefClass = new  \ReflectionClass($AdapterObj);
+
+        //如果手动更改了结果,就用更改后的结果,否则用默认的结果
+        if ($matchModel->bet_type == 1 && $moneyBuy['result_flag'] == 1) {
+            $now_result = [json_decode($moneyBuy['single_result'])];
+        } else {
+            foreach ($task['result'] as $key => $val) {
+                $now_result[] = $val;
+            }
+        }
+
+        $fun1 = $matchModel->odds_code;
+        $fun2 = $matchModel->p_code;
+
+        if ($RefClass->hasMethod($fun1)) {
+            $winorfalse = $AdapterObj->$fun1($matchModel, $now_result, []);
+        } elseif ($RefClass->hasMethod($fun2)) {
+            $winorfalse = $AdapterObj->$fun2($matchModel, $now_result, []);
+        }
+        if (!in_array($winorfalse['result'], [-1, 1, 2, 3, 4])) {
+            $winorfalse = ['result' => 2, 'matchResult' => 'noRuleError'];
+        }
+
+        $sql_1 = $this->makesql_up_buymatch_winorfalse($matchModel->id, $winorfalse['result'], date("Y-m-d H:i:s"), $winorfalse['matchResult']);
+        $gs_val = ($winorfalse['result'] == 1) ? 1 : ($winorfalse['result'] == -1 ? 2 : 3);
+        if ($matchModel->bet_type == 1) {
+            $sql_2 = "update money_buy_simplex set  game_status=$gs_val where  batch_id='$matchModel->batch_id' and match_id='$matchModel->match_id' and  game_code='$matchModel->game_code' ";
+        } else {
+            $sql_2 = "update money_buy_str  set  wait_match_num=wait_match_num-1 where  batch_id='$matchModel->batch_id' ";
+        }
+        $this->sqltoRedisqueue([$sql_1, $sql_2]);
+        return true;
+    }
+
+    //生成更新sql;
+    private function makesql_up_buymatch_winorfalse($id, $result, $utime, $matchResult = '')
+    {
+        $matchResult = str_replace(";", ";", $matchResult);
+        $sql = "update money_buy_match set result=$result, utime='$utime',matchResult='$matchResult' where   id=$id ";
+        return $sql;
+    }
+
+
+    /*
+    private function DbpooleClient($sqlArray = [])
+    {
+        go(function () use ($sqlArray) {
+            $client = new \swoole_client(SWOOLE_SOCK_TCP);
+            if (!$client->connect('127.0.0.1', $this->dbpooleconfig['poole_port'], 1)) {
+                return false;
+            }
+            foreach ($sqlArray as $val) {
+                $cmds[] = $val;
+            }
+            $this->sqltoRedisqueue($cmds);
+            $client->close();
+        });
+    }
+    */
+
+    private function sqltoRedisqueue($cmdArray)
+    {
+        $redisconfig = $this->redisonfig;
+        go(function () use ($cmdArray, $redisconfig) {
+            $redis = new Swoole\Coroutine\Redis();
+            $redis->connect($redisconfig['host'], $redisconfig['port']);
+            foreach ($cmdArray as $cmd) {
+                $redis->rpush(self::SQLKEY, $cmd);
+            }
+        });
+    }
 
+    //sql批量写入数据库
+    private function BatchSqlRedisToDB()
+    {
+        $redisconfig = $this->redisonfig;
+        go(function () use ($redisconfig) {
+            $redis = new Swoole\Coroutine\Redis();
+            $redis->connect($redisconfig['host'], $redisconfig['port']);
+
+            $batchsql = [];
+            $max = 3000;
+
+            for ($i = 0; $i < $max; $i++) {
+                $now = $redis->lpop(self::SQLKEY);
+                if (empty($now)) {
+                    break;
+                } else {
+                    $batchsql[] = $now;
+                }
+            }
+            if (!empty($batchsql)) {
+                $pdo = DB::getPdo();
+                $sqlstr = implode(";", $batchsql);
+                $pdo->exec($sqlstr);
+            }
+            return;
+        });
     }