HttpServerSettelement.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  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. use App\Logic\UnSettmatchLogic;
  17. class HttpServerSettelement
  18. {
  19. private $httpserver;
  20. private $config;
  21. private $dbpooleconfig = [];
  22. private $redisonfig = [];
  23. const SQLKEY = 'ALLSQLKEY';
  24. const TASKQNUM = 'TASKQNUM';
  25. private $TaskSqlQueue;
  26. public function __construct($config)
  27. {
  28. $this->httpserver = new \swoole\http\server($config['host'], $config['port']);
  29. $this->httpserver->set($config['sets']);
  30. $this->config = $config;
  31. $this->dbpooleconfig = GlobConfigs::getKey('pgsqlpoole');
  32. $this->redisonfig = GlobConfigs::getKey('redis');
  33. $this->httpserver->account = new \swoole\Atomic();
  34. $this->httpserver->taskWorkingNum = new \swoole\Atomic();
  35. $this->httpserver->on('request', array($this, 'OnRequest'));
  36. $this->httpserver->on('WorkerStart', array($this, 'onWorkerStart'));
  37. $this->httpserver->on('task', array($this, 'onTask'));
  38. $this->httpserver->on('finish', array($this, 'onFinish'));
  39. }
  40. public function onWorkerStart($serv, $worker_id)
  41. {
  42. /*
  43. $name = $serv->taskworker ? 'Task_' : 'Worker_';
  44. $name = 'Settelement_' . $name . ($serv->worker_id < 10 ? '0' . $serv->worker_id : $serv->worker_id);
  45. swoole_set_process_name($name);
  46. */
  47. $this->TaskSqlQueue = new \SplQueue();
  48. $GLOBALS['model'] = '';
  49. $GLOBALS['modeltime'] = 0;
  50. if (!$serv->taskworker) {
  51. $this->InitDb();
  52. }
  53. if ($serv->worker_id == 0) {
  54. \Swoole\Timer::tick(60000, function () {
  55. $this->logRunStatus();
  56. $this->dosubcount();
  57. });
  58. }
  59. }
  60. private function logRunStatus()
  61. {
  62. echo date('Y-m-d H:i:s') . " 总请求数:" . $this->httpserver->account->get() . ' 运行任务数:' . $this->httpserver->taskWorkingNum->get();
  63. echo ' work_id:' . $this->httpserver->worker_id . " 内存使用量:" . (memory_get_usage() / 1000) . 'k 峰值:' . (memory_get_peak_usage() / 1000) . "k\n";
  64. }
  65. private function dosubcount()
  66. {
  67. if ($this->httpserver->taskWorkingNum->get() > 0) {
  68. $this->httpserver->taskWorkingNum->sub();
  69. }
  70. }
  71. public function OnRequest($request, $response)
  72. {
  73. $response->header('Content-Type', 'text/html; charset=utf-8');
  74. $response->header('Server', 'DataInfaceServer');
  75. $mictime = microtime(true);
  76. $paras = array_merge(['request_time' => date("H:i:s"),'request_time_mic'=>$mictime], !empty($request->get) ? $request->get : [], !empty($request->post) ? $request->post : []);
  77. $request_uri = substr($request->server['request_uri'], 1);
  78. $urls = ['WinFail', 'Settelement', 'DoWinFailOneOrder', 'WinfailoneNomal', 'WinfailMulNomal', 'UnSettelement', 'UnsetOneOrder', 'UnsetOneStringOrder', 'ResStringOneOrder'];
  79. $this->httpserver->account->add();
  80. echo "现在任务数:" . $this->httpserver->taskWorkingNum->get() . "\n";
  81. echo '请求参数是:' . $request_uri . ' - ' . print_r($paras, true) . "\n";
  82. if (!in_array($request_uri, $urls)) {
  83. $data = Response::generate('', 0, '', '无效的url');
  84. $response->end($data);
  85. return;
  86. }
  87. if ($this->httpserver->taskWorkingNum->get() > intval($this->config['sets']['worker_num'])) {
  88. $data = Response::generate('', 9, '', '还有未完成任务请稍等...' . $this->httpserver->taskWorkingNum->get());
  89. $response->end($data);
  90. return;
  91. }
  92. $check_token = true;
  93. if ($check_token) {
  94. $token = isset($paras['token']) ? $paras['token'] : '';
  95. if (empty($token) || empty($this->Tokencheck($token))) {
  96. $data = Response::generate('', 6, '', '安全验证失败!');
  97. $response->end($data);
  98. return;
  99. }
  100. }
  101. $this->httpserver->taskWorkingNum->add();
  102. if ($request_uri == 'WinFail') {
  103. $this->dosubcount();
  104. return $this->doWinFailse($request, $response, $paras);
  105. }
  106. //手动改结果单式 订单输赢计算 参数order_id
  107. if ($request_uri == 'DoWinFailOneOrder') {
  108. $this->dosubcount();
  109. return $this->WinFailOneOrder($request, $response, $paras);
  110. }
  111. //普通订单输赢计算 参数order_id bet_type[1单式 2串式]
  112. if ($request_uri == 'WinfailoneNomal') {
  113. $this->dosubcount();
  114. return $this->WinFailOneOrderNomal($request, $response, $paras);
  115. }
  116. //普通订单输赢计算 参数order_ids 多个订单用半角逗号分割 bet_type[1单式 2串式] 待实现
  117. if ($request_uri == 'WinfailMulNomal') {
  118. $this->dosubcount();
  119. return $this->WinfailMulNomal($request, $response, $paras);
  120. }
  121. if ($request_uri == 'Settelement') {
  122. $this->dosubcount();
  123. return $this->doSettelementIntoRedis($request, $response, $paras);
  124. }
  125. //撤销某个订事所有的订单
  126. if ($request_uri == 'UnSettelement') {
  127. $this->dosubcount();
  128. return $this->UnSettelement($request, $response, $paras);
  129. }
  130. //把一个串式撤销订单改为重下单状态
  131. if ($request_uri == 'ResStringOneOrder') {
  132. $this->dosubcount();
  133. return $this->RestartStringOneOrder($request, $response, $paras);
  134. }
  135. //撤销某个赛事相关的订单
  136. if ($request_uri == 'UnsetOneOrder') {
  137. $this->dosubcount();
  138. return $this->UnSettelementOneOrder($request, $response, $paras);
  139. }
  140. //撤销某个串式的订单
  141. if ($request_uri == 'UnsetOneStringOrder') {
  142. $this->dosubcount();
  143. return $this->UnSettelementOneStringOrder($request, $response, $paras);
  144. }
  145. return;
  146. }
  147. public function onTask($serv, $task)
  148. {
  149. }
  150. private function Tokencheck($token)
  151. {
  152. $tokenvel = DB::table('system_user')->where(['token' => $token])->first();
  153. return $tokenvel;
  154. }
  155. //处理一个非手动的单式或串输赢结果
  156. private function WinFailOneOrderNomal($request, $response, $paras)
  157. {
  158. $orderid = isset($paras['order_id']) ? $paras['order_id'] : '';
  159. $bet_type = isset($paras['bet_type']) ? $paras['bet_type'] : 1;
  160. if (empty($orderid)) {
  161. $data = Response::generate('', 10, $paras, 'order_id is empty');
  162. $response->end($data);
  163. return;
  164. }
  165. $logic_obj = new WinfailLogic();
  166. try {
  167. $ret = $logic_obj->WinFailOneOrderNomal($orderid, $bet_type);
  168. $data = Response::generate('', $ret, ['cost' => (microtime(true) - $request->server['request_time_float'])], $ret ? 'succes ' : 'false');
  169. } catch (\Exception $e) {
  170. $data = Response::generate('', 10, '', $e->getMessage() . '--' . $e->getFile() . '--' . $e->getLine());
  171. }
  172. unset($logic_obj, $match_datas, $orderInfo);
  173. $response->end($data);
  174. echo $data . "\n";
  175. }
  176. //同一赛事多个订单输赢判断处理, 要么单式,要么串式..
  177. public function WinfailMulNomal($request, $response, $paras)
  178. {
  179. $orderids = isset($paras['order_ids']) ? $paras['order_ids'] : '';
  180. $bet_type = isset($paras['bet_type']) ? $paras['bet_type'] : 1;
  181. if (empty($orderids)) {
  182. $data = Response::generate('', 10, $paras, 'order_id is empty');
  183. $response->end($data);
  184. return;
  185. }
  186. $logic_obj = new WinfailLogic();
  187. $orderidsArr = explode(",", $orderids);
  188. $rets = [];
  189. foreach ($orderidsArr as $orderid) {
  190. try {
  191. $ret = $logic_obj->WinFailOneOrderNomal($orderid, $bet_type);
  192. $rets[$orderid] = ['code' => $ret, 'msg' => $ret ? 'success' : 'false'];
  193. } catch (\Exception $e) {
  194. $rets[$orderid] = ['code' => 0, 'msg' => $e->getFile() . '--' . $e->getLine() . '--' . $e->getMessage()];
  195. }
  196. }
  197. $data = Response::generate('', 1, $rets);
  198. unset($logic_obj, $match_datas, $orderInfo, $rets);
  199. $response->end($data);
  200. echo $data .' ' . microtime(true) . "\n";
  201. return;
  202. }
  203. //对单个订单的赛事结果进行过手动更改的订单,单独进行输赢判断处理
  204. private function WinFailOneOrder($request, $response, $paras)
  205. {
  206. $orderid = isset($paras['order_id']) ? $paras['order_id'] : '';
  207. if (empty($orderid)) {
  208. $data = Response::generate('', 10, $paras, 'order_id is empty');
  209. $response->end($data);
  210. return;
  211. }
  212. $orderInfo = DB::table('money_buy_simplex')->where(['order_id' => $orderid])->first();
  213. if (empty($orderInfo)) {
  214. $data = Response::generate('', 10, $paras, 'order info empty');
  215. $response->end($data);
  216. return;
  217. }
  218. if ($orderInfo->result_flag != 1) {
  219. $data = Response::generate('', 10, $paras, 'result_flag !=1');
  220. $response->end($data);
  221. return;
  222. }
  223. $match_datas = DB::table('money_buy_match')->where(['order_id' => $orderid, 'bet_type' => 1])->get();
  224. if (count($match_datas) <= 0) {
  225. $data = Response::generate('', 10, $paras, 'match info empty');
  226. $response->end($data);
  227. return;
  228. }
  229. $logic_obj = new WinfailLogic();
  230. $result = [];
  231. if ($orderInfo->is_champion == 0) {
  232. $result = $logic_obj->getCompResult($orderInfo->game_code, $orderInfo->match_id);
  233. } else {
  234. $resulttmp = $logic_obj->getGjDatasV2($orderInfo->game_code, $orderInfo->lg_id);
  235. if ($resulttmp) {
  236. $result['0'] = $resulttmp;
  237. }
  238. }
  239. if (count($result) <= 0) {
  240. $data = Response::generate('', 10, $paras, 'result empty');
  241. $response->end($data);
  242. return;
  243. }
  244. try {
  245. $logic_obj->WinFailOneOrder($orderInfo, $match_datas, $orderInfo->is_champion);
  246. $data = Response::generate('', 1, ['cost' => (microtime(true) - $request->server['request_time_float'])], 'succes ');
  247. } catch (\Exception $e) {
  248. $data = Response::generate('', 10, '', $e->getMessage() . '--' . $e->getFile() . '--' . $e->getLine());
  249. }
  250. unset($logic_obj, $match_datas, $orderInfo);
  251. $response->end($data);
  252. echo $data . "\n";
  253. return;
  254. }
  255. //胜负计算
  256. private function doWinFailse($request, $response, $paras)
  257. {
  258. $logic_obj = new WinfailLogic();
  259. $notice = isset($paras['noticeid']) ? $paras['noticeid'] : 0;
  260. $id = intval($notice);
  261. DB::beginTransaction();
  262. try {
  263. $noticeModel = $logic_obj->getNoticeDate($id);
  264. $logic_obj->Reset_manualData($noticeModel->game_code, $noticeModel->match_id);
  265. $result = $logic_obj->getCompResult($noticeModel->game_code, $noticeModel->match_id);
  266. $moneySimples = $logic_obj->getSimplexData($noticeModel->game_code, $noticeModel->match_id);
  267. $matchs_1 = $logic_obj->getMatchRecords($noticeModel->game_code, $noticeModel->match_id, 1);
  268. $matchs_2 = $logic_obj->getMatchRecords($noticeModel->game_code, $noticeModel->match_id, 2);
  269. $match_firstModel = isset($matchs_1['0']) ? $matchs_1['0'] : (isset($matchs_2['0']) ? $matchs_2['0'] : false);
  270. if (!$match_firstModel) {
  271. goto ENDLABLE;
  272. }
  273. $gjModel = $logic_obj->getGjDatas($match_firstModel);
  274. if (count($matchs_1) > 0 && count($moneySimples) <= 0) {
  275. throw new \Exception('数据异常-2!', 1101);
  276. }
  277. if (strtolower($match_firstModel->p_code) == 'gj') {
  278. if (empty($gjModel)) {
  279. throw new \Exception("没有冠军数据数据--match_id" . $noticeModel->match_id, 1102);
  280. }
  281. $result = [$gjModel];
  282. }
  283. $grpDatas = $logic_obj->getOddsTypeData($noticeModel->game_code, $noticeModel->match_id);
  284. if (empty($grpDatas)) {
  285. goto ENDLABLE;
  286. }
  287. $logic_obj->doLogic($noticeModel, $result, [$gjModel]);
  288. ENDLABLE:
  289. DB::commit();
  290. unset($logic_obj, $noticeModel, $AdapterObj, $result, $moneySimples, $matchs_1, $matchs_2, $RefClass);
  291. $data = Response::generate('', 1, ['cost' => (microtime(true) - $request->server['request_time_float'])], 'succes ');
  292. echo $data . "\n";
  293. $response->end($data);
  294. } catch (\Exception $e) {
  295. DB::rollBack();
  296. unset($logic_obj);
  297. $data = Response::generate('', 10, '', $e->getMessage() . '--' . $e->getFile() . '--' . $e->getLine());
  298. echo $data . "\n";
  299. $response->end($data);
  300. }
  301. return;
  302. }
  303. //按赛事撤销 ,(用户赢钱的要还回平台,输钱的还给用户) 存在已结算和未结算的情况 ,单式订单和串式订单,串式订单比较复杂
  304. // 暂时没用 。 因为单式在后台处理了,串不不处理。。
  305. private function UnSettelement($request, $response, $paras)
  306. {
  307. UnSettmatchLogic::getInstance()->doUnsetMatch($request, $response, $paras);
  308. }
  309. //把一个串式订单 由撤单状态改为 下单状态 (扣钱,写流水,改订单状态)
  310. private function RestartStringOneOrder($request, $response, $paras)
  311. {
  312. UnSettmatchLogic::getInstance()->RestartStringOneOrder($request, $response, $paras);
  313. }
  314. //按订单撤销单个已结算订单
  315. private function UnSettelementOneOrder($request, $response, $paras)
  316. {
  317. UnSettmatchLogic::getInstance()->doUnsetMatchOneOrder($request, $response, $paras);
  318. }
  319. //撤销某个串式订单(整个订单)
  320. private function UnSettelementOneStringOrder($request, $response, $paras)
  321. {
  322. UnSettmatchLogic::getInstance()->doUnsetMatchOneOrder($request, $response, $paras);
  323. }
  324. private function doSettelementIntoRedis($request, $response, $paras)
  325. {
  326. try {
  327. list($order_ids, $bettype, $settype, $game_code, $match_id, $change_status, $is_manual) = $tmp = $this->requestpara($request, $response, $paras);
  328. if (empty($order_ids)) {
  329. goto LABRETURN;
  330. }
  331. if ($bettype == 1) {
  332. $chekArr = $this->Match_check($order_ids, $bettype);
  333. if (empty($chekArr) || count($chekArr) != 1) {
  334. throw new \Exception('不同场比赛不能同时结算(或作废撤单)!', 1105);
  335. }
  336. unset($chekArr);
  337. }
  338. // $this->cgStatusSett($bettype, $game_code, $match_id, $change_status);
  339. //分页处理数据
  340. $PageOrder_ids_1 = array_chunk($order_ids, 500);
  341. $i = 1;
  342. unset($order_ids);
  343. $redisconfig = $this->redisonfig;
  344. $redis = new \Redis();
  345. foreach ($PageOrder_ids_1 as $p_order_ids) {
  346. $nowchange_status = ($i == 1) ? $change_status : 0;
  347. $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);
  348. //go(function () use ($data, $redisconfig) {
  349. // $redis = new Swoole\Coroutine\Redis();
  350. $ret = $redis->connect($redisconfig['host'], $redisconfig['port']);
  351. if (!$ret) {
  352. throw new \Exception('redis 连接失败', 1106);
  353. }
  354. if (!empty($redisconfig['passwd'])) {
  355. $ret = $redis->auth($redisconfig['passwd']);
  356. if (!$ret) {
  357. throw new \Exception('redis auth 失败', 1107);
  358. }
  359. }
  360. $redis->select($redisconfig['db']);
  361. $redis->lpush(self::TASKQNUM, $data);
  362. // return;
  363. // });
  364. unset($data);
  365. $i++;
  366. }
  367. $redis->close();
  368. LABRETURN:
  369. $data = Response::generate('', 1, ['cost' => (microtime(true) - $request->server['request_time_float'])], 'succes');
  370. $response->end($data);
  371. echo $data . "\n";
  372. } catch (\Exception $e) {
  373. $data = Response::generate('', $e->getCode(), '', $e->getMessage() . '--' . '--' . $e->getLine());
  374. $response->end($data);
  375. echo $data . "\n";
  376. unset($data);
  377. }
  378. }
  379. private function requestpara($request, $response, $paras)
  380. {
  381. $bettype = isset($paras['bettype']) ? $paras['bettype'] : 0;
  382. $settype = isset($paras['settype']) ? $paras['settype'] : 0;
  383. $is_manaue = isset($paras['is_manual']) ? $paras['is_manual'] : 0;
  384. $game_code = isset($paras['game_code']) ? $paras['game_code'] : '';
  385. $match_id = isset($paras['match_id']) ? $paras['match_id'] : 0;
  386. $change_status = isset($paras['change_status']) ? $paras['change_status'] : 0;
  387. if (empty($paras['order_ids'])) {
  388. $idoarr = $this->getOrders($bettype, $game_code, $match_id, $is_manaue);
  389. } else {
  390. $idoarr = explode(",", $paras['order_ids']);
  391. }
  392. $order_ids = array_map(function ($i) {
  393. return strval($i);
  394. }, $idoarr);
  395. if (intval($match_id) <= 0) {
  396. throw new \Exception('赛事ID不能为空!', 1108);
  397. return;
  398. }
  399. if (empty($game_code)) {
  400. throw new \Exception('赛事类型不能为空!', 1109);
  401. }
  402. if (!in_array($bettype, [1, 2])) {
  403. throw new \Exception('订单类型参数错误!', 1110);
  404. }
  405. if (!in_array($settype, [1, 2])) {
  406. throw new \Exception('结算参数错误', 1111);
  407. }
  408. $ret = [$order_ids, $bettype, $settype, $game_code, $match_id, $change_status, $is_manaue];
  409. unset($bettype, $idoarr, $settype, $game_code, $match_id, $change_status, $is_manaue);
  410. return $ret;
  411. }
  412. private function getOrders($bet_type, $game_code, $match_id, $is_manual = 0)
  413. {
  414. if ($bet_type == 1) {
  415. $table = 'money_buy_simplex';
  416. //$ret = DB::table($table)->select('order_id')->where(['game_code' => $game_code, 'match_id' => $match_id, 'is_manual' => $is_manual])->get();
  417. $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();
  418. } else {
  419. $table = 'money_buy_str';
  420. $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();
  421. }
  422. $return = [];
  423. if ($ret) {
  424. foreach ($ret as $val) {
  425. $return[] = $val->order_id;
  426. }
  427. }
  428. return $return;
  429. }
  430. //改状态为正在结算中
  431. private function cgStatusSett($bet_type, $game_code, $match_id, $change_status)
  432. {
  433. if ($bet_type == 1 && $change_status == 1) {
  434. $table1 = "st_" . $game_code . "_result";
  435. $table2 = "st_" . $game_code . "_competition";
  436. DB::table($table1)->where('match_id', $match_id)->update(['status' => 5]);
  437. DB::table($table2)->where('id', $match_id)->update(['status' => 5]);
  438. }
  439. return;
  440. }
  441. private function Match_check($idsArray, $betType)
  442. {
  443. array_walk($idsArray, function (&$item, $key) {
  444. $item = "'" . $item . "'";
  445. });
  446. $idString = implode(",", $idsArray);
  447. $table = "money_buy_simplex";
  448. $sql = "select game_code,match_id from $table where status=1 and order_id in ($idString) group by game_code,match_id ";
  449. //$sql = "select game_code,match_id from $table where order_id in ($idString) group by game_code,match_id ";
  450. $ret = DB::select($sql);
  451. return $ret;
  452. }
  453. public function onFinish($serv, int $task_id, $data)
  454. {
  455. }
  456. private function InitDb()
  457. {
  458. $over_time = 60 * 5;
  459. $now = microtime(true);
  460. if (!$GLOBALS['modeltime']) {
  461. $GLOBALS['modeltime'] = $now;
  462. $GLOBALS['model'] = $this->httpserver->worker_id;
  463. ModelBase::init();
  464. return;
  465. }
  466. if (($now - $GLOBALS['modeltime']) > $over_time) {
  467. $GLOBALS['modeltime'] = $now;
  468. $GLOBALS['model'] = $this->httpserver->worker_id;
  469. ModelBase::close();
  470. ModelBase::init();
  471. return;
  472. }
  473. }
  474. public function start()
  475. {
  476. $this->httpserver->start();
  477. }
  478. }