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'); $this->httpserver->account = new \swoole\Atomic(); $this->httpserver->taskWorkingNum = new \swoole\Atomic(); $this->httpserver->on('request', array($this, 'OnRequest')); $this->httpserver->on('WorkerStart', array($this, 'onWorkerStart')); $this->httpserver->on('task', array($this, 'onTask')); $this->httpserver->on('finish', array($this, 'onFinish')); } public function onWorkerStart($serv, $worker_id) { /* $name = $serv->taskworker ? 'Task_' : 'Worker_'; $name = 'Settelement_' . $name . ($serv->worker_id < 10 ? '0' . $serv->worker_id : $serv->worker_id); swoole_set_process_name($name); */ $this->TaskSqlQueue = new \SplQueue(); $GLOBALS['model'] = ''; $GLOBALS['modeltime'] = 0; if (!$serv->taskworker) { $this->InitDb(); } if ($serv->worker_id == 0) { \Swoole\Timer::tick(60000, function () { $this->logRunStatus(); $this->dosubcount(); }); } } private function logRunStatus() { echo date('Y-m-d H:i:s') . " 总请求数:" . $this->httpserver->account->get() . ' 运行任务数:' . $this->httpserver->taskWorkingNum->get(); echo ' work_id:' . $this->httpserver->worker_id . " 内存使用量:" . (memory_get_usage() / 1000) . 'k 峰值:' . (memory_get_peak_usage() / 1000) . "k\n"; } private function dosubcount() { if ($this->httpserver->taskWorkingNum->get() > 0) { $this->httpserver->taskWorkingNum->sub(); } } public function OnRequest($request, $response) { $response->header('Content-Type', 'text/html; charset=utf-8'); $response->header('Server', 'DataInfaceServer'); $paras = array_merge(['request_time' => date("H:i:s")], !empty($request->get) ? $request->get : [], !empty($request->post) ? $request->post : []); $request_uri = substr($request->server['request_uri'], 1); $urls = ['WinFail', 'Settelement', 'DoWinFailOneOrder', 'WinfailoneNomal', 'UnSettelement', 'UnsetOneOrder', 'UnsetOneStringOrder', 'ResStringOneOrder']; $this->httpserver->account->add(); echo "现在任务数:" . $this->httpserver->taskWorkingNum->get() . "\n"; echo '请求参数是:' . $request_uri . ' - ' . print_r($paras, true) . "\n"; if (!in_array($request_uri, $urls)) { $data = Response::generate('', 0, '', '无效的url'); $response->end($data); return; } if ($this->httpserver->taskWorkingNum->get() > intval($this->config['sets']['worker_num'])) { $data = Response::generate('', 9, '', '还有未完成任务请稍等...' . $this->httpserver->taskWorkingNum->get()); $response->end($data); return; } $check_token = true; if ($check_token) { $token = isset($paras['token']) ? $paras['token'] : ''; if (empty($token) || empty($this->Tokencheck($token))) { $data = Response::generate('', 6, '', '安全验证失败!'); $response->end($data); return; } } $this->httpserver->taskWorkingNum->add(); if ($request_uri == 'WinFail') { $this->dosubcount(); return $this->doWinFailse($request, $response, $paras); } //手动改结果单式 订单输赢计算 参数order_id if ($request_uri == 'DoWinFailOneOrder') { $this->dosubcount(); return $this->WinFailOneOrder($request, $response, $paras); } //普通订单输赢计算 参数order_id bet_type[1单式 2串式] if ($request_uri == 'WinfailoneNomal') { $this->dosubcount(); return $this->WinFailOneOrderNomal($request, $response, $paras); } if ($request_uri == 'Settelement') { $this->dosubcount(); return $this->doSettelementIntoRedis($request, $response, $paras); } //撤销某个订事所有的订单 if ($request_uri == 'UnSettelement') { $this->dosubcount(); return $this->UnSettelement($request, $response, $paras); } //把一个串式撤销订单改为重下单状态 if ($request_uri == 'ResStringOneOrder') { $this->dosubcount(); return $this->RestartStringOneOrder($request, $response, $paras); } //撤销某个赛事相关的订单 if ($request_uri == 'UnsetOneOrder') { $this->dosubcount(); return $this->UnSettelementOneOrder($request, $response, $paras); } //撤销某个串式的订单 if ($request_uri == 'UnsetOneStringOrder') { $this->dosubcount(); return $this->UnSettelementOneStringOrder($request, $response, $paras); } return; } public function onTask($serv, $task) { } private function Tokencheck($token) { $tokenvel = DB::table('system_user')->where(['token' => $token])->first(); return $tokenvel; } //处理一个非手动的单式或串输赢结果 private function WinFailOneOrderNomal($request, $response, $paras) { $orderid = isset($paras['order_id']) ? $paras['order_id'] : ''; $bet_type = isset($paras['bet_type']) ? $paras['bet_type'] : 1; if (empty($orderid)) { $data = Response::generate('', 10, $paras, 'order_id is empty'); $response->end($data); return; } $logic_obj = new WinfailLogic(); try { $ret = $logic_obj->WinFailOneOrderNomal($orderid, $bet_type); $data = Response::generate('', $ret, ['cost' => (microtime(true) - $request->server['request_time_float'])], $ret ? 'succes ' : 'false'); } catch (\Exception $e) { $data = Response::generate('', 10, '', $e->getMessage() . '--' . $e->getFile() . '--' . $e->getLine()); } unset($logic_obj, $match_datas, $orderInfo); $response->end($data); echo $data . "\n"; } //对单个订单的赛事结果进行过手动更改的订单,单独进行输赢判断处理 private function WinFailOneOrder($request, $response, $paras) { $orderid = isset($paras['order_id']) ? $paras['order_id'] : ''; if (empty($orderid)) { $data = Response::generate('', 10, $paras, 'order_id is empty'); $response->end($data); return; } $orderInfo = DB::table('money_buy_simplex')->where(['order_id' => $orderid])->first(); if (empty($orderInfo)) { $data = Response::generate('', 10, $paras, 'order info empty'); $response->end($data); return; } if ($orderInfo->result_flag != 1) { $data = Response::generate('', 10, $paras, 'result_flag !=1'); $response->end($data); return; } $match_datas = DB::table('money_buy_match')->where(['order_id' => $orderid, 'bet_type' => 1])->get(); if (count($match_datas) <= 0) { $data = Response::generate('', 10, $paras, 'match info empty'); $response->end($data); return; } $logic_obj = new WinfailLogic(); $result = []; if ($orderInfo->is_champion == 0) { $result = $logic_obj->getCompResult($orderInfo->game_code, $orderInfo->match_id); } else { $resulttmp = $logic_obj->getGjDatasV2($orderInfo->game_code, $orderInfo->lg_id); if ($resulttmp) { $result['0'] = $resulttmp; } } if (count($result) <= 0) { $data = Response::generate('', 10, $paras, 'result empty'); $response->end($data); return; } try { $logic_obj->WinFailOneOrder($orderInfo, $match_datas, $orderInfo->is_champion); $data = Response::generate('', 1, ['cost' => (microtime(true) - $request->server['request_time_float'])], 'succes '); } catch (\Exception $e) { $data = Response::generate('', 10, '', $e->getMessage() . '--' . $e->getFile() . '--' . $e->getLine()); } unset($logic_obj, $match_datas, $orderInfo); $response->end($data); echo $data . "\n"; return; } //胜负计算 private function doWinFailse($request, $response, $paras) { $logic_obj = new WinfailLogic(); $notice = isset($paras['noticeid']) ? $paras['noticeid'] : 0; $id = intval($notice); DB::beginTransaction(); try { $noticeModel = $logic_obj->getNoticeDate($id); $logic_obj->Reset_manualData($noticeModel->game_code, $noticeModel->match_id); $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); $match_firstModel = isset($matchs_1['0']) ? $matchs_1['0'] : (isset($matchs_2['0']) ? $matchs_2['0'] : false); if (!$match_firstModel) { goto ENDLABLE; } $gjModel = $logic_obj->getGjDatas($match_firstModel); if (count($matchs_1) > 0 && count($moneySimples) <= 0) { throw new \Exception('数据异常-2!', 1101); } if (strtolower($match_firstModel->p_code) == 'gj') { if (empty($gjModel)) { throw new \Exception("没有冠军数据数据--match_id" . $noticeModel->match_id, 1102); } $result = [$gjModel]; } $grpDatas = $logic_obj->getOddsTypeData($noticeModel->game_code, $noticeModel->match_id); if (empty($grpDatas)) { goto ENDLABLE; } $logic_obj->doLogic($noticeModel, $result, [$gjModel]); ENDLABLE: DB::commit(); unset($logic_obj, $noticeModel, $AdapterObj, $result, $moneySimples, $matchs_1, $matchs_2, $RefClass); $data = Response::generate('', 1, ['cost' => (microtime(true) - $request->server['request_time_float'])], 'succes '); echo $data . "\n"; $response->end($data); } catch (\Exception $e) { DB::rollBack(); unset($logic_obj); $data = Response::generate('', 10, '', $e->getMessage() . '--' . $e->getFile() . '--' . $e->getLine()); echo $data . "\n"; $response->end($data); } return; } //按赛事撤销 ,(用户赢钱的要还回平台,输钱的还给用户) 存在已结算和未结算的情况 ,单式订单和串式订单,串式订单比较复杂 // 暂时没用 。 因为单式在后台处理了,串不不处理。。 private function UnSettelement($request, $response, $paras) { UnSettmatchLogic::getInstance()->doUnsetMatch($request, $response, $paras); } //把一个串式订单 由撤单状态改为 下单状态 (扣钱,写流水,改订单状态) private function RestartStringOneOrder($request, $response, $paras) { UnSettmatchLogic::getInstance()->RestartStringOneOrder($request, $response, $paras); } //按订单撤销单个已结算订单 private function UnSettelementOneOrder($request, $response, $paras) { UnSettmatchLogic::getInstance()->doUnsetMatchOneOrder($request, $response, $paras); } //撤销某个串式订单(整个订单) private function UnSettelementOneStringOrder($request, $response, $paras) { UnSettmatchLogic::getInstance()->doUnsetMatchOneOrder($request, $response, $paras); } private function doSettelementIntoRedis($request, $response, $paras) { try { list($order_ids, $bettype, $settype, $game_code, $match_id, $change_status, $is_manual) = $tmp = $this->requestpara($request, $response, $paras); if (empty($order_ids)) { goto LABRETURN; } if ($bettype == 1) { $chekArr = $this->Match_check($order_ids, $bettype); if (empty($chekArr) || count($chekArr) != 1) { throw new \Exception('不同场比赛不能同时结算(或作废撤单)!', 1105); } unset($chekArr); } // $this->cgStatusSett($bettype, $game_code, $match_id, $change_status); //分页处理数据 $PageOrder_ids_1 = array_chunk($order_ids, 500); $i = 1; unset($order_ids); $redisconfig = $this->redisonfig; $redis = new \Redis(); foreach ($PageOrder_ids_1 as $p_order_ids) { $nowchange_status = ($i == 1) ? $change_status : 0; $data = json_encode(['ids' => $p_order_ids, 'bettype' => $bettype, 'settype' => $settype, 'game_code' => $game_code, 'match_id' => $match_id, 'change_status' => $nowchange_status, 'is_manual' => $is_manual], 256); //go(function () use ($data, $redisconfig) { // $redis = new Swoole\Coroutine\Redis(); $ret = $redis->connect($redisconfig['host'], $redisconfig['port']); if (!$ret) { throw new \Exception('redis 连接失败', 1106); } if (!empty($redisconfig['passwd'])) { $ret = $redis->auth($redisconfig['passwd']); if (!$ret) { throw new \Exception('redis auth 失败', 1107); } } $redis->select($redisconfig['db']); $redis->lpush(self::TASKQNUM, $data); // return; // }); unset($data); $i++; } $redis->close(); LABRETURN: $data = Response::generate('', 1, ['cost' => (microtime(true) - $request->server['request_time_float'])], 'succes'); $response->end($data); echo $data . "\n"; } catch (\Exception $e) { $data = Response::generate('', $e->getCode(), '', $e->getMessage() . '--' . '--' . $e->getLine()); $response->end($data); echo $data . "\n"; unset($data); } } private function requestpara($request, $response, $paras) { $bettype = isset($paras['bettype']) ? $paras['bettype'] : 0; $settype = isset($paras['settype']) ? $paras['settype'] : 0; $is_manaue = isset($paras['is_manual']) ? $paras['is_manual'] : 0; $game_code = isset($paras['game_code']) ? $paras['game_code'] : ''; $match_id = isset($paras['match_id']) ? $paras['match_id'] : 0; $change_status = isset($paras['change_status']) ? $paras['change_status'] : 0; if (empty($paras['order_ids'])) { $idoarr = $this->getOrders($bettype, $game_code, $match_id, $is_manaue); } else { $idoarr = explode(",", $paras['order_ids']); } $order_ids = array_map(function ($i) { return strval($i); }, $idoarr); if (intval($match_id) <= 0) { throw new \Exception('赛事ID不能为空!', 1108); return; } if (empty($game_code)) { throw new \Exception('赛事类型不能为空!', 1109); } if (!in_array($bettype, [1, 2])) { throw new \Exception('订单类型参数错误!', 1110); } if (!in_array($settype, [1, 2])) { throw new \Exception('结算参数错误', 1111); } $ret = [$order_ids, $bettype, $settype, $game_code, $match_id, $change_status, $is_manaue]; unset($bettype, $idoarr, $settype, $game_code, $match_id, $change_status, $is_manaue); return $ret; } private function getOrders($bet_type, $game_code, $match_id, $is_manual = 0) { if ($bet_type == 1) { $table = 'money_buy_simplex'; //$ret = DB::table($table)->select('order_id')->where(['game_code' => $game_code, 'match_id' => $match_id, 'is_manual' => $is_manual])->get(); $ret = DB::table($table)->select('order_id')->where([['game_code', '=', $game_code], ['match_id', '=', $match_id], ['is_manual', '=', $is_manual]])->whereIn('roll_ratify', [0, 1])->get(); } else { $table = 'money_buy_str'; $ret = DB::table($table)->select('order_id')->whereRaw(" order_id in ( select order_id from money_buy_match where match_id=$match_id and game_code='$game_code' and bet_type=2 )")->get(); } $return = []; if ($ret) { foreach ($ret as $val) { $return[] = $val->order_id; } } return $return; } //改状态为正在结算中 private function cgStatusSett($bet_type, $game_code, $match_id, $change_status) { if ($bet_type == 1 && $change_status == 1) { $table1 = "st_" . $game_code . "_result"; $table2 = "st_" . $game_code . "_competition"; DB::table($table1)->where('match_id', $match_id)->update(['status' => 5]); DB::table($table2)->where('id', $match_id)->update(['status' => 5]); } return; } private function Match_check($idsArray, $betType) { array_walk($idsArray, function (&$item, $key) { $item = "'" . $item . "'"; }); $idString = implode(",", $idsArray); $table = "money_buy_simplex"; $sql = "select game_code,match_id from $table where status=1 and order_id in ($idString) group by game_code,match_id "; //$sql = "select game_code,match_id from $table where order_id in ($idString) group by game_code,match_id "; $ret = DB::select($sql); return $ret; } public function onFinish($serv, int $task_id, $data) { } private function InitDb() { $over_time = 60 * 5; $now = microtime(true); if (!$GLOBALS['modeltime']) { $GLOBALS['modeltime'] = $now; $GLOBALS['model'] = $this->httpserver->worker_id; ModelBase::init(); return; } if (($now - $GLOBALS['modeltime']) > $over_time) { $GLOBALS['modeltime'] = $now; $GLOBALS['model'] = $this->httpserver->worker_id; ModelBase::close(); ModelBase::init(); return; } } public function start() { $this->httpserver->start(); } }