HttpServerSettelement.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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. $this->httpserver->account = new \swoole\Atomic();
  33. $this->httpserver->taskWorkingNum = new \swoole\Atomic();
  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. if ($serv->worker_id == 0) {
  53. \Swoole\Timer::tick(60000, function () {
  54. $this->logRunStatus();
  55. if ($this->httpserver->taskWorkingNum->get() > 1) {
  56. $this->httpserver->taskWorkingNum->sub();
  57. }
  58. });
  59. }
  60. }
  61. private function logRunStatus()
  62. {
  63. echo date('Y-m-d H:i:s') . " 总请求数:" . $this->httpserver->account->get() . ' 运行任务数:' . $this->httpserver->taskWorkingNum->get();
  64. echo ' work_id:' . $this->httpserver->worker_id . " 内存使用量:" . (memory_get_usage() / 1000) . 'k 峰值:' . (memory_get_peak_usage() / 1000) . "k\n";
  65. }
  66. public function OnRequest($request, $response)
  67. {
  68. $response->header('Content-Type', 'text/html; charset=utf-8');
  69. $response->header('Server', 'DataInfaceServer');
  70. $paras = array_merge(['request_time' => date("H:i:s")], !empty($request->get) ? $request->get : [], !empty($request->post) ? $request->post : []);
  71. $request_uri = substr($request->server['request_uri'], 1);
  72. $urls = ['WinFail', 'Settelement', 'DoWinFailOneOrder'];
  73. $this->httpserver->account->add();
  74. echo "现在任务数:" . $this->httpserver->taskWorkingNum->get() . "\n";
  75. echo '请求参数是:' . $request_uri . ' - ' . print_r($paras, true) . "\n";
  76. if (!in_array($request_uri, $urls)) {
  77. $data = Response::generate('', 0, '', '无效的url');
  78. $response->end($data);
  79. return;
  80. }
  81. if ($this->httpserver->taskWorkingNum->get() > intval($this->config['sets']['worker_num'])) {
  82. $data = Response::generate('', 9, '', '还有未完成任务请稍等...' . $this->httpserver->taskWorkingNum->get());
  83. $response->end($data);
  84. return;
  85. }
  86. $this->httpserver->taskWorkingNum->add();
  87. $check_token = true;
  88. if ($check_token) {
  89. $token = isset($paras['token']) ? $paras['token'] : '';
  90. if (empty($token) || empty($this->Tokencheck($token))) {
  91. $data = Response::generate('', 6, '', '安全验证失败!');
  92. $response->end($data);
  93. return;
  94. }
  95. }
  96. if ($request_uri == 'WinFail') {
  97. $ret = $this->doWinFailse($request, $response, $paras);
  98. $this->httpserver->taskWorkingNum->sub();
  99. return $ret;
  100. }
  101. if ($request_uri == 'DoWinFailOneOrder') {
  102. $ret = $this->WinFailOneOrder($request, $response, $paras);
  103. $this->httpserver->taskWorkingNum->sub();
  104. return $ret;
  105. }
  106. if ($request_uri == 'Settelement') {
  107. $ret = $this->doSettelementIntoRedis($request, $response, $paras);
  108. $this->httpserver->taskWorkingNum->sub();
  109. return $ret;
  110. }
  111. return;
  112. }
  113. public function onTask($serv, $task)
  114. {
  115. //$this->httpserver->taskWorkingNum->add();
  116. //$this->httpserver->taskWorkingNum->sub();
  117. }
  118. private function Tokencheck($token)
  119. {
  120. $tokenvel = DB::table('system_user')->where(['token' => $token])->first();
  121. return $tokenvel;
  122. }
  123. //对单个订单的赛事结果进行过手动更改的订单,单独进行输赢判断处理
  124. private function WinFailOneOrder($request, $response, $paras)
  125. {
  126. $orderid = isset($paras['order_id']) ? $paras['order_id'] : '';
  127. if (empty($orderid)) {
  128. $data = Response::generate('', 10, $paras, 'order_id is empty');
  129. $response->end($data);
  130. return;
  131. }
  132. $orderInfo = DB::table('money_buy_simplex')->where(['order_id' => $orderid])->first();
  133. if (empty($orderInfo)) {
  134. $data = Response::generate('', 10, $paras, 'order info empty');
  135. $response->end($data);
  136. return;
  137. }
  138. if ($orderInfo->result_flag != 1) {
  139. $data = Response::generate('', 10, $paras, 'result_flag !=1');
  140. $response->end($data);
  141. return;
  142. }
  143. $match_datas = DB::table('money_buy_match')->where(['batch_id' => $orderInfo->batch_id, 'bet_type' => 1])->get();
  144. if (count($match_datas) <= 0) {
  145. $data = Response::generate('', 10, $paras, 'match info empty');
  146. $response->end($data);
  147. return;
  148. }
  149. $logic_obj = new WinfailLogic();
  150. $result = $logic_obj->getCompResult($orderInfo->game_code, $orderInfo->match_id);
  151. if (count($result) <= 0) {
  152. $data = Response::generate('', 10, $paras, 'result empty');
  153. $response->end($data);
  154. return;
  155. }
  156. try {
  157. $logic_obj->WinFailOneOrder($orderInfo, $match_datas, $result['0']);
  158. $data = Response::generate('', 1, ['cost' => (microtime(true) - $request->server['request_time_float'])], 'succes ');
  159. } catch (\Exception $e) {
  160. $data = Response::generate('', 10, '', $e->getMessage() . '--' . $e->getFile() . '--' . $e->getLine());
  161. }
  162. unset($logic_obj, $match_datas, $orderInfo);
  163. $response->end($data);
  164. return;
  165. }
  166. //胜负计算
  167. private function doWinFailse($request, $response, $paras)
  168. {
  169. $logic_obj = new WinfailLogic();
  170. $notice = isset($paras['noticeid']) ? $paras['noticeid'] : 0;
  171. $id = intval($notice);
  172. try {
  173. $noticeModel = $logic_obj->getNoticeDate($id);
  174. $result = $logic_obj->getCompResult($noticeModel->game_code, $noticeModel->match_id);
  175. $moneySimples = $logic_obj->getSimplexData($noticeModel->game_code, $noticeModel->match_id);
  176. $matchs_1 = $logic_obj->getMatchRecords($noticeModel->game_code, $noticeModel->match_id, 1);
  177. $matchs_2 = $logic_obj->getMatchRecords($noticeModel->game_code, $noticeModel->match_id, 2);
  178. $match_firstModel = isset($matchs_1['0']) ? $matchs_1['0'] : (isset($matchs_2['0']) ? $matchs_2['0'] : false);
  179. if (!$match_firstModel) {
  180. throw new \Exception('无数据异常-1!');
  181. }
  182. $gjModel = $logic_obj->getGjDatas($match_firstModel);
  183. if (count($matchs_1) > 0 && count($moneySimples) <= 0) {
  184. throw new \Exception('数据异常-2!');
  185. }
  186. if (strtolower($match_firstModel->p_code) == 'gj') {
  187. if (empty($gjModel)) {
  188. throw new \Exception("没有冠军数据数据--match_id" . $noticeModel->match_id);
  189. }
  190. $result = [$gjModel];
  191. }
  192. $grpDatas = $logic_obj->getOddsTypeData($noticeModel->game_code, $noticeModel->match_id);
  193. if (empty($grpDatas)) {
  194. goto ENDLABLE;
  195. }
  196. $logic_obj->doLogic($noticeModel, $result);
  197. ENDLABLE:
  198. unset($logic_obj, $noticeModel, $AdapterObj, $result, $moneySimples, $matchs_1, $matchs_2, $RefClass);
  199. $data = Response::generate('', 1, ['cost' => (microtime(true) - $request->server['request_time_float'])], 'succes ');
  200. $response->end($data);
  201. } catch (\Exception $e) {
  202. unset($logic_obj);
  203. $data = Response::generate('', 10, '', $e->getMessage() . '--' . $e->getFile() . '--' . $e->getLine());
  204. $response->end($data);
  205. }
  206. return;
  207. }
  208. private function doSettelementIntoRedis($request, $response, $paras)
  209. {
  210. try {
  211. list($order_ids, $bettype, $settype, $game_code, $match_id, $change_status) = $tmp = $this->requestpara($request, $response, $paras);
  212. if (empty($order_ids)) {
  213. goto LABRETURN;
  214. }
  215. if ($bettype == 1) {
  216. $chekArr = $this->Match_check($order_ids, $bettype);
  217. if (empty($chekArr) || count($chekArr) != 1) {
  218. throw new \Exception('不同场比赛不能同时结算!');
  219. }
  220. unset($chekArr);
  221. }
  222. //分页处理数据
  223. $PageOrder_ids_1 = array_chunk($order_ids, 500);
  224. $i = 1;
  225. unset($order_ids);
  226. $redisconfig = $this->redisonfig;
  227. foreach ($PageOrder_ids_1 as $p_order_ids) {
  228. $nowchange_status = ($i == 1) ? $change_status : 0;
  229. $data = json_encode(['ids' => $p_order_ids, 'bettype' => $bettype, 'settype' => $settype, 'game_code' => $game_code, 'match_id' => $match_id, 'change_status' => $nowchange_status], 256);
  230. //go(function () use ($data, $redisconfig) {
  231. $redis = new \Redis();
  232. $ret = $redis->connect($redisconfig['host'], $redisconfig['port']);
  233. if (!$ret) {
  234. throw new \Exception('redis 连接失败');
  235. }
  236. if (!empty($redisconfig['passwd'])) {
  237. $ret = $redis->auth($redisconfig['passwd']);
  238. if (!$ret) {
  239. throw new \Exception('redis auth 失败');
  240. }
  241. }
  242. $redis->select($redisconfig['db']);
  243. $redis->lpush(self::TASKQNUM, $data);
  244. return;
  245. // });
  246. unset($data);
  247. $i++;
  248. }
  249. LABRETURN:
  250. $data = Response::generate('', 1, ['cost' => (microtime(true) - $request->server['request_time_float'])], 'succes ');
  251. $response->end($data);
  252. } catch (\Exception $e) {
  253. $data = Response::generate('', 10, '', $e->getMessage() . '--' . $e->getFile() . '--' . $e->getLine());
  254. $response->end($data);
  255. unset($data);
  256. }
  257. }
  258. private function requestpara($request, $response, $paras)
  259. {
  260. $bettype = isset($paras['bettype']) ? $paras['bettype'] : 0;
  261. $settype = isset($paras['settype']) ? $paras['settype'] : 0;
  262. $game_code = isset($paras['game_code']) ? $paras['game_code'] : '';
  263. $match_id = isset($paras['match_id']) ? $paras['match_id'] : 0;
  264. $change_status = isset($paras['change_status']) ? $paras['change_status'] : 0;
  265. if (empty($paras['order_ids'])) {
  266. $idoarr = $this->getOrders($bettype, $game_code, $match_id);
  267. } else {
  268. $idoarr = explode(",", $paras['order_ids']);
  269. }
  270. $order_ids = array_map(function ($i) {
  271. return strval($i);
  272. }, $idoarr);
  273. if (intval($match_id) <= 0) {
  274. throw new \Exception('赛事ID不能为空!');
  275. return;
  276. }
  277. if (empty($game_code)) {
  278. throw new \Exception('赛事类型不能为空!');
  279. }
  280. if (!in_array($bettype, [1, 2])) {
  281. throw new \Exception('订单类型参数错误!');
  282. }
  283. if (!in_array($settype, [1, 2])) {
  284. throw new \Exception('结算参数错误');
  285. }
  286. $ret = [$order_ids, $bettype, $settype, $game_code, $match_id, $change_status];
  287. unset($bettype, $idoarr, $settype, $game_code, $match_id, $change_status);
  288. return $ret;
  289. }
  290. private function getOrders($bet_type, $game_code, $match_id)
  291. {
  292. if ($bet_type == 1) {
  293. $table = 'money_buy_simplex';
  294. $ret = DB::table($table)->select('order_id')->where(['game_code' => $game_code, 'match_id' => $match_id])->get();
  295. } else {
  296. $table = 'money_buy_str';
  297. $ret = DB::table($table)->select('order_id')->whereRaw(" batch_id in ( select batch_id from money_buy_match where match_id=$match_id and game_code='$game_code' and bet_type=2 )")->get();
  298. }
  299. $return = [];
  300. if ($ret) {
  301. foreach ($ret as $val) {
  302. $return[] = $val->order_id;
  303. }
  304. }
  305. return $return;
  306. }
  307. private function Match_check($idsArray, $betType)
  308. {
  309. array_walk($idsArray, function (&$item, $key) {
  310. $item = "'" . $item . "'";
  311. });
  312. $idString = implode(",", $idsArray);
  313. $table = "money_buy_simplex";
  314. $sql = "select game_code,match_id from $table where status=1 and order_id in ($idString) group by game_code,match_id ";
  315. $ret = DB::select($sql);
  316. return $ret;
  317. }
  318. public function onFinish($serv, int $task_id, $data)
  319. {
  320. }
  321. private function InitDb()
  322. {
  323. $over_time = 60 * 5;
  324. $now = microtime(true);
  325. if (!$GLOBALS['modeltime']) {
  326. $GLOBALS['modeltime'] = $now;
  327. $GLOBALS['model'] = $this->httpserver->worker_id;
  328. ModelBase::init();
  329. return;
  330. }
  331. if (($now - $GLOBALS['modeltime']) > $over_time) {
  332. $GLOBALS['modeltime'] = $now;
  333. $GLOBALS['model'] = $this->httpserver->worker_id;
  334. ModelBase::close();
  335. ModelBase::init();
  336. return;
  337. }
  338. }
  339. public function start()
  340. {
  341. $this->httpserver->start();
  342. }
  343. }