HttpServerSettelement.php 16 KB

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