HttpServerSettelement.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  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. }
  201. //对单个订单的赛事结果进行过手动更改的订单,单独进行输赢判断处理
  202. private function WinFailOneOrder($request, $response, $paras)
  203. {
  204. $orderid = isset($paras['order_id']) ? $paras['order_id'] : '';
  205. if (empty($orderid)) {
  206. $data = Response::generate('', 10, $paras, 'order_id is empty');
  207. $response->end($data);
  208. return;
  209. }
  210. $orderInfo = DB::table('money_buy_simplex')->where(['order_id' => $orderid])->first();
  211. if (empty($orderInfo)) {
  212. $data = Response::generate('', 10, $paras, 'order info empty');
  213. $response->end($data);
  214. return;
  215. }
  216. if ($orderInfo->result_flag != 1) {
  217. $data = Response::generate('', 10, $paras, 'result_flag !=1');
  218. $response->end($data);
  219. return;
  220. }
  221. $match_datas = DB::table('money_buy_match')->where(['order_id' => $orderid, 'bet_type' => 1])->get();
  222. if (count($match_datas) <= 0) {
  223. $data = Response::generate('', 10, $paras, 'match info empty');
  224. $response->end($data);
  225. return;
  226. }
  227. $logic_obj = new WinfailLogic();
  228. $result = [];
  229. if ($orderInfo->is_champion == 0) {
  230. $result = $logic_obj->getCompResult($orderInfo->game_code, $orderInfo->match_id);
  231. } else {
  232. $resulttmp = $logic_obj->getGjDatasV2($orderInfo->game_code, $orderInfo->lg_id);
  233. if ($resulttmp) {
  234. $result['0'] = $resulttmp;
  235. }
  236. }
  237. if (count($result) <= 0) {
  238. $data = Response::generate('', 10, $paras, 'result empty');
  239. $response->end($data);
  240. return;
  241. }
  242. try {
  243. $logic_obj->WinFailOneOrder($orderInfo, $match_datas, $orderInfo->is_champion);
  244. $data = Response::generate('', 1, ['cost' => (microtime(true) - $request->server['request_time_float'])], 'succes ');
  245. } catch (\Exception $e) {
  246. $data = Response::generate('', 10, '', $e->getMessage() . '--' . $e->getFile() . '--' . $e->getLine());
  247. }
  248. unset($logic_obj, $match_datas, $orderInfo);
  249. $response->end($data);
  250. echo $data . "\n";
  251. return;
  252. }
  253. //胜负计算
  254. private function doWinFailse($request, $response, $paras)
  255. {
  256. $logic_obj = new WinfailLogic();
  257. $notice = isset($paras['noticeid']) ? $paras['noticeid'] : 0;
  258. $id = intval($notice);
  259. DB::beginTransaction();
  260. try {
  261. $noticeModel = $logic_obj->getNoticeDate($id);
  262. $logic_obj->Reset_manualData($noticeModel->game_code, $noticeModel->match_id);
  263. $result = $logic_obj->getCompResult($noticeModel->game_code, $noticeModel->match_id);
  264. $moneySimples = $logic_obj->getSimplexData($noticeModel->game_code, $noticeModel->match_id);
  265. $matchs_1 = $logic_obj->getMatchRecords($noticeModel->game_code, $noticeModel->match_id, 1);
  266. $matchs_2 = $logic_obj->getMatchRecords($noticeModel->game_code, $noticeModel->match_id, 2);
  267. $match_firstModel = isset($matchs_1['0']) ? $matchs_1['0'] : (isset($matchs_2['0']) ? $matchs_2['0'] : false);
  268. if (!$match_firstModel) {
  269. goto ENDLABLE;
  270. }
  271. $gjModel = $logic_obj->getGjDatas($match_firstModel);
  272. if (count($matchs_1) > 0 && count($moneySimples) <= 0) {
  273. throw new \Exception('数据异常-2!', 1101);
  274. }
  275. if (strtolower($match_firstModel->p_code) == 'gj') {
  276. if (empty($gjModel)) {
  277. throw new \Exception("没有冠军数据数据--match_id" . $noticeModel->match_id, 1102);
  278. }
  279. $result = [$gjModel];
  280. }
  281. $grpDatas = $logic_obj->getOddsTypeData($noticeModel->game_code, $noticeModel->match_id);
  282. if (empty($grpDatas)) {
  283. goto ENDLABLE;
  284. }
  285. $logic_obj->doLogic($noticeModel, $result, [$gjModel]);
  286. ENDLABLE:
  287. DB::commit();
  288. unset($logic_obj, $noticeModel, $AdapterObj, $result, $moneySimples, $matchs_1, $matchs_2, $RefClass);
  289. $data = Response::generate('', 1, ['cost' => (microtime(true) - $request->server['request_time_float'])], 'succes ');
  290. echo $data . "\n";
  291. $response->end($data);
  292. } catch (\Exception $e) {
  293. DB::rollBack();
  294. unset($logic_obj);
  295. $data = Response::generate('', 10, '', $e->getMessage() . '--' . $e->getFile() . '--' . $e->getLine());
  296. echo $data . "\n";
  297. $response->end($data);
  298. }
  299. return;
  300. }
  301. //按赛事撤销 ,(用户赢钱的要还回平台,输钱的还给用户) 存在已结算和未结算的情况 ,单式订单和串式订单,串式订单比较复杂
  302. // 暂时没用 。 因为单式在后台处理了,串不不处理。。
  303. private function UnSettelement($request, $response, $paras)
  304. {
  305. UnSettmatchLogic::getInstance()->doUnsetMatch($request, $response, $paras);
  306. }
  307. //把一个串式订单 由撤单状态改为 下单状态 (扣钱,写流水,改订单状态)
  308. private function RestartStringOneOrder($request, $response, $paras)
  309. {
  310. UnSettmatchLogic::getInstance()->RestartStringOneOrder($request, $response, $paras);
  311. }
  312. //按订单撤销单个已结算订单
  313. private function UnSettelementOneOrder($request, $response, $paras)
  314. {
  315. UnSettmatchLogic::getInstance()->doUnsetMatchOneOrder($request, $response, $paras);
  316. }
  317. //撤销某个串式订单(整个订单)
  318. private function UnSettelementOneStringOrder($request, $response, $paras)
  319. {
  320. UnSettmatchLogic::getInstance()->doUnsetMatchOneOrder($request, $response, $paras);
  321. }
  322. private function doSettelementIntoRedis($request, $response, $paras)
  323. {
  324. try {
  325. list($order_ids, $bettype, $settype, $game_code, $match_id, $change_status, $is_manual) = $tmp = $this->requestpara($request, $response, $paras);
  326. if (empty($order_ids)) {
  327. goto LABRETURN;
  328. }
  329. if ($bettype == 1) {
  330. $chekArr = $this->Match_check($order_ids, $bettype);
  331. if (empty($chekArr) || count($chekArr) != 1) {
  332. throw new \Exception('不同场比赛不能同时结算(或作废撤单)!', 1105);
  333. }
  334. unset($chekArr);
  335. }
  336. // $this->cgStatusSett($bettype, $game_code, $match_id, $change_status);
  337. //分页处理数据
  338. $PageOrder_ids_1 = array_chunk($order_ids, 500);
  339. $i = 1;
  340. unset($order_ids);
  341. $redisconfig = $this->redisonfig;
  342. $redis = new \Redis();
  343. foreach ($PageOrder_ids_1 as $p_order_ids) {
  344. $nowchange_status = ($i == 1) ? $change_status : 0;
  345. $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);
  346. //go(function () use ($data, $redisconfig) {
  347. // $redis = new Swoole\Coroutine\Redis();
  348. $ret = $redis->connect($redisconfig['host'], $redisconfig['port']);
  349. if (!$ret) {
  350. throw new \Exception('redis 连接失败', 1106);
  351. }
  352. if (!empty($redisconfig['passwd'])) {
  353. $ret = $redis->auth($redisconfig['passwd']);
  354. if (!$ret) {
  355. throw new \Exception('redis auth 失败', 1107);
  356. }
  357. }
  358. $redis->select($redisconfig['db']);
  359. $redis->lpush(self::TASKQNUM, $data);
  360. // return;
  361. // });
  362. unset($data);
  363. $i++;
  364. }
  365. $redis->close();
  366. LABRETURN:
  367. $data = Response::generate('', 1, ['cost' => (microtime(true) - $request->server['request_time_float'])], 'succes');
  368. $response->end($data);
  369. echo $data . "\n";
  370. } catch (\Exception $e) {
  371. $data = Response::generate('', $e->getCode(), '', $e->getMessage() . '--' . '--' . $e->getLine());
  372. $response->end($data);
  373. echo $data . "\n";
  374. unset($data);
  375. }
  376. }
  377. private function requestpara($request, $response, $paras)
  378. {
  379. $bettype = isset($paras['bettype']) ? $paras['bettype'] : 0;
  380. $settype = isset($paras['settype']) ? $paras['settype'] : 0;
  381. $is_manaue = isset($paras['is_manual']) ? $paras['is_manual'] : 0;
  382. $game_code = isset($paras['game_code']) ? $paras['game_code'] : '';
  383. $match_id = isset($paras['match_id']) ? $paras['match_id'] : 0;
  384. $change_status = isset($paras['change_status']) ? $paras['change_status'] : 0;
  385. if (empty($paras['order_ids'])) {
  386. $idoarr = $this->getOrders($bettype, $game_code, $match_id, $is_manaue);
  387. } else {
  388. $idoarr = explode(",", $paras['order_ids']);
  389. }
  390. $order_ids = array_map(function ($i) {
  391. return strval($i);
  392. }, $idoarr);
  393. if (intval($match_id) <= 0) {
  394. throw new \Exception('赛事ID不能为空!', 1108);
  395. return;
  396. }
  397. if (empty($game_code)) {
  398. throw new \Exception('赛事类型不能为空!', 1109);
  399. }
  400. if (!in_array($bettype, [1, 2])) {
  401. throw new \Exception('订单类型参数错误!', 1110);
  402. }
  403. if (!in_array($settype, [1, 2])) {
  404. throw new \Exception('结算参数错误', 1111);
  405. }
  406. $ret = [$order_ids, $bettype, $settype, $game_code, $match_id, $change_status, $is_manaue];
  407. unset($bettype, $idoarr, $settype, $game_code, $match_id, $change_status, $is_manaue);
  408. return $ret;
  409. }
  410. private function getOrders($bet_type, $game_code, $match_id, $is_manual = 0)
  411. {
  412. if ($bet_type == 1) {
  413. $table = 'money_buy_simplex';
  414. //$ret = DB::table($table)->select('order_id')->where(['game_code' => $game_code, 'match_id' => $match_id, 'is_manual' => $is_manual])->get();
  415. $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();
  416. } else {
  417. $table = 'money_buy_str';
  418. $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();
  419. }
  420. $return = [];
  421. if ($ret) {
  422. foreach ($ret as $val) {
  423. $return[] = $val->order_id;
  424. }
  425. }
  426. return $return;
  427. }
  428. //改状态为正在结算中
  429. private function cgStatusSett($bet_type, $game_code, $match_id, $change_status)
  430. {
  431. if ($bet_type == 1 && $change_status == 1) {
  432. $table1 = "st_" . $game_code . "_result";
  433. $table2 = "st_" . $game_code . "_competition";
  434. DB::table($table1)->where('match_id', $match_id)->update(['status' => 5]);
  435. DB::table($table2)->where('id', $match_id)->update(['status' => 5]);
  436. }
  437. return;
  438. }
  439. private function Match_check($idsArray, $betType)
  440. {
  441. array_walk($idsArray, function (&$item, $key) {
  442. $item = "'" . $item . "'";
  443. });
  444. $idString = implode(",", $idsArray);
  445. $table = "money_buy_simplex";
  446. $sql = "select game_code,match_id from $table where status=1 and order_id in ($idString) group by game_code,match_id ";
  447. //$sql = "select game_code,match_id from $table where order_id in ($idString) group by game_code,match_id ";
  448. $ret = DB::select($sql);
  449. return $ret;
  450. }
  451. public function onFinish($serv, int $task_id, $data)
  452. {
  453. }
  454. private function InitDb()
  455. {
  456. $over_time = 60 * 5;
  457. $now = microtime(true);
  458. if (!$GLOBALS['modeltime']) {
  459. $GLOBALS['modeltime'] = $now;
  460. $GLOBALS['model'] = $this->httpserver->worker_id;
  461. ModelBase::init();
  462. return;
  463. }
  464. if (($now - $GLOBALS['modeltime']) > $over_time) {
  465. $GLOBALS['modeltime'] = $now;
  466. $GLOBALS['model'] = $this->httpserver->worker_id;
  467. ModelBase::close();
  468. ModelBase::init();
  469. return;
  470. }
  471. }
  472. public function start()
  473. {
  474. $this->httpserver->start();
  475. }
  476. }