HttpServerSettelement.php 21 KB

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