HttpServerSettelement.php 19 KB

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