HttpServerSettelement.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2019/6/26
  6. * Time: 12:03
  7. */
  8. namespace datainf\logic;
  9. use App\Lib\ModelBase;
  10. use App\Http\Response\Response;
  11. use App\Logic\WinfailLogic;
  12. use datainf\lib\GlobConfigs;
  13. use Illuminate\Database\Capsule\Manager as DB;
  14. use swoole;
  15. use App\Logic\SettelementLogic;
  16. class HttpServerSettelement
  17. {
  18. private $httpserver;
  19. private $config;
  20. private $dbpooleconfig = [];
  21. private $redisonfig = [];
  22. const SQLKEY = 'ALLSQLKEY';
  23. const TASKQNUM = 'TASKQNUM';
  24. private $TaskSqlQueue;
  25. public function __construct($config)
  26. {
  27. $this->httpserver = new \swoole\http\server($config['host'], $config['port']);
  28. $this->httpserver->set($config['sets']);
  29. $this->config = $config;
  30. $this->dbpooleconfig = GlobConfigs::getKey('pgsqlpoole');
  31. $this->redisonfig = GlobConfigs::getKey('redis');
  32. $taskWorkingNum = new \swoole\Atomic();
  33. $this->httpserver->taskWorkingNum = $taskWorkingNum;
  34. $this->httpserver->on('request', array($this, 'OnRequest'));
  35. $this->httpserver->on('WorkerStart', array($this, 'onWorkerStart'));
  36. $this->httpserver->on('task', array($this, 'onTask'));
  37. $this->httpserver->on('finish', array($this, 'onFinish'));
  38. }
  39. public function onWorkerStart($serv, $worker_id)
  40. {
  41. /*
  42. $name = $serv->taskworker ? 'Task_' : 'Worker_';
  43. $name = 'Settelement_' . $name . ($serv->worker_id < 10 ? '0' . $serv->worker_id : $serv->worker_id);
  44. swoole_set_process_name($name);
  45. */
  46. $this->TaskSqlQueue = new \SplQueue();
  47. $GLOBALS['model'] = '';
  48. $GLOBALS['modeltime'] = 0;
  49. if (!$serv->taskworker) {
  50. $this->InitDb();
  51. }
  52. }
  53. public function OnRequest($request, $response)
  54. {
  55. $response->header('Content-Type', 'text/html; charset=utf-8');
  56. $response->header('Server', 'DataInfaceServer');
  57. $paras = array_merge(['request_time' => date("H:i:s")], !empty($request->get) ? $request->get : [], !empty($request->post) ? $request->post : []);
  58. $request_uri = substr($request->server['request_uri'], 1);
  59. $urls = ['WinFail', 'Settelement'];
  60. echo "现在任务数:" . $this->httpserver->taskWorkingNum->get() . "\n";
  61. if (!in_array($request_uri, $urls)) {
  62. $data = Response::generate('', 0, '', '无效的url');
  63. $response->end($data);
  64. return;
  65. }
  66. if ($this->httpserver->taskWorkingNum->get() > 0) {
  67. $data = Response::generate('', 9, '', '还有未完成任务请稍等...');
  68. $response->end($data);
  69. return;
  70. }
  71. $check_token = true;
  72. if ($check_token) {
  73. $token = isset($paras['token']) ? $paras['token'] : '';
  74. if (empty($token) || empty($this->Tokencheck($token))) {
  75. $data = Response::generate('', 6, '', '安全验证失败!');
  76. $response->end($data);
  77. return;
  78. }
  79. }
  80. echo "计数器值:" . $this->httpserver->taskWorkingNum->get() . "\n";
  81. if ($request_uri == 'WinFail') {
  82. return $this->doWinFailse($request, $response, $paras);
  83. }
  84. if ($request_uri == 'Settelement') {
  85. return $this->doSettelementIntoRedis($request, $response, $paras);
  86. }
  87. return;
  88. }
  89. public function onTask($serv, $task)
  90. {
  91. $this->httpserver->taskWorkingNum->add();
  92. $this->httpserver->taskWorkingNum->sub();
  93. }
  94. private function Tokencheck($token)
  95. {
  96. $tokenvel = DB::table('system_user')->where(['token' => $token])->first();
  97. return $tokenvel;
  98. }
  99. //胜负计算
  100. private function doWinFailse($request, $response, $paras)
  101. {
  102. $logic_obj = new WinfailLogic();
  103. $notice = isset($paras['noticeid']) ? $paras['noticeid'] : 0;
  104. $id = intval($notice);
  105. try {
  106. $noticeModel = $logic_obj->getNoticeDate($id);
  107. $result = $logic_obj->getCompResult($noticeModel->game_code, $noticeModel->match_id);
  108. $moneySimples = $logic_obj->getSimplexData($noticeModel->game_code, $noticeModel->match_id);
  109. $matchs_1 = $logic_obj->getMatchRecords($noticeModel->game_code, $noticeModel->match_id, 1);
  110. $matchs_2 = $logic_obj->getMatchRecords($noticeModel->game_code, $noticeModel->match_id, 2);
  111. $match_firstModel = isset($matchs_1['0']) ? $matchs_1['0'] : (isset($matchs_2['0']) ? $matchs_2['0'] : false);
  112. if (!$match_firstModel) {
  113. throw new \Exception('无数据异常-1!');
  114. return true;
  115. }
  116. $gjModel = $logic_obj->getGjDatas($match_firstModel);
  117. if (count($matchs_1) > 0 && count($moneySimples) <= 0) {
  118. throw new \Exception('数据异常-2!');
  119. }
  120. if (strtolower($match_firstModel->p_code) == 'gj') {
  121. if (empty($gjModel)) {
  122. throw new \Exception("没有冠军数据数据--match_id" . $noticeModel->match_id);
  123. }
  124. $result = [$gjModel];
  125. }
  126. $grpDatas = $logic_obj->getOddsTypeData($noticeModel->game_code, $noticeModel->match_id);
  127. if (empty($grpDatas)) {
  128. goto ENDLABLE;
  129. }
  130. $logic_obj->doLogic($noticeModel, $result);
  131. ENDLABLE:
  132. unset($logic_obj, $noticeModel, $AdapterObj, $result, $moneySimples, $matchs_1, $matchs_2, $RefClass);
  133. $data = Response::generate('', 1, ['cost' => (microtime(true) - $request->server['request_time_float'])], 'succes ');
  134. $response->end($data);
  135. return;
  136. } catch (\Exception $e) {
  137. unset($logic_obj);
  138. $data = Response::generate('', 10, '', $e->getMessage() . '--' . $e->getFile() . '--' . $e->getLine());
  139. $response->end($data);
  140. return;
  141. }
  142. return;
  143. }
  144. private function doSettelementIntoRedis($request, $response, $paras)
  145. {
  146. try {
  147. list($order_ids, $bettype, $settype, $game_code, $match_id, $change_status) = $this->requestpara($request, $response, $paras);
  148. if (empty($order_ids)) {
  149. $needs_matchs = DB::table('money_buy_match')->where(['game_code' => $game_code, 'match_id' => $match_id])->count();
  150. if ($needs_matchs <= 0) {
  151. $this->cgStatus($game_code, $match_id, $this->change_status);
  152. goto LABRETURN;
  153. return;
  154. } else {
  155. throw new \Exception('订单号不能为空!');
  156. }
  157. unset($needs_matchs);
  158. }
  159. if ($bettype == 1) {
  160. $chekArr = $this->Match_check($order_ids, $bettype);
  161. if (empty($chekArr) || count($chekArr) != 1) {
  162. throw new \Exception('不同场比赛不能同时结算!');
  163. }
  164. unset($chekArr);
  165. }
  166. //分页处理数据
  167. $PageOrder_ids_1 = array_chunk($order_ids, 100);
  168. $tcount = ceil(count($order_ids) / 500);
  169. $i = 1;
  170. unset($order_ids);
  171. $redisconfig = $this->redisonfig;
  172. foreach ($PageOrder_ids_1 as $p_order_ids) {
  173. $nowchange_status = ($tcount == $i) ? $change_status : 0;
  174. $data = json_encode(['ids' => $p_order_ids, 'bettype' => $bettype, 'settype' => $settype, 'game_code' => $game_code, 'match_id' => $match_id, 'change_status' => $nowchange_status], 256);
  175. go(function () use ($data, $redisconfig) {
  176. $redis = new Swoole\Coroutine\Redis();
  177. $redis->connect($redisconfig['host'], $redisconfig['port']);
  178. $redis->lpush(self::TASKQNUM, $data);
  179. return;
  180. });
  181. unset($data);
  182. $i++;
  183. }
  184. LABRETURN:
  185. $data = Response::generate('', 1, ['cost' => (microtime(true) - $request->server['request_time_float'])], 'succes ');
  186. $response->end($data);
  187. return;
  188. } catch (\Exception $e) {
  189. $data = Response::generate('', 10, '', $e->getMessage() . '--' . $e->getFile() . '--' . $e->getLine());
  190. $response->end($data);
  191. unset($data);
  192. return;
  193. }
  194. }
  195. private function requestpara($request, $response, $paras)
  196. {
  197. $idoarr = explode(",", $paras['order_ids']);
  198. $order_ids = array_map(function ($i) {
  199. return strval($i);
  200. }, $idoarr);
  201. $bettype = isset($paras['bettype']) ? $paras['bettype'] : 0;
  202. $settype = isset($paras['settype']) ? $paras['settype'] : 0;
  203. $game_code = isset($paras['game_code']) ? $paras['game_code'] : '';
  204. $match_id = isset($paras['match_id']) ? $paras['match_id'] : 0;
  205. $change_status = isset($paras['change_status']) ? $paras['change_status'] : 0;
  206. if (intval($match_id) <= 0) {
  207. throw new \Exception('赛事ID不能为空!');
  208. return;
  209. }
  210. if (empty($game_code)) {
  211. throw new \Exception('赛事类型不能为空!');
  212. }
  213. if (!in_array($bettype, [1, 2])) {
  214. throw new \Exception('订单类型参数错误!');
  215. }
  216. if (!in_array($settype, [1, 2])) {
  217. throw new \Exception('结算参数错误');
  218. }
  219. $ret = [$order_ids, $bettype, $settype, $game_code, $match_id, $change_status];
  220. unset($bettype, $idoarr, $settype, $game_code, $match_id, $change_status);
  221. return $ret;
  222. }
  223. private function Match_check($idsArray, $betType)
  224. {
  225. array_walk($idsArray, function (&$item, $key) {
  226. $item = "'" . $item . "'";
  227. });
  228. $idString = implode(",", $idsArray);
  229. $table = "money_buy_simplex";
  230. $sql = "select game_code,match_id from $table where status=1 and order_id in ($idString) group by game_code,match_id ";
  231. $ret = DB::select($sql);
  232. return $ret;
  233. }
  234. public function onFinish($serv, int $task_id, $data)
  235. {
  236. }
  237. private function InitDb()
  238. {
  239. $over_time = 60 * 5;
  240. $now = microtime(true);
  241. if (!$GLOBALS['modeltime']) {
  242. $GLOBALS['modeltime'] = $now;
  243. $GLOBALS['model'] = $this->httpserver->worker_id;
  244. ModelBase::init();
  245. return;
  246. }
  247. if (($now - $GLOBALS['modeltime']) > $over_time) {
  248. $GLOBALS['modeltime'] = $now;
  249. $GLOBALS['model'] = $this->httpserver->worker_id;
  250. ModelBase::close();
  251. ModelBase::init();
  252. return;
  253. }
  254. }
  255. public function start()
  256. {
  257. $this->httpserver->start();
  258. }
  259. }