HttpServerOnlySet.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721
  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 datainf\lib\GlobConfigs;
  11. use Illuminate\Database\Capsule\Manager as DB;
  12. use swoole;
  13. use App\Lib\Settlement\SettlementBase;
  14. use App\Http\Response\Response;
  15. class HttpServerOnlySet
  16. {
  17. const TASKQNUM = 'TASKQNUM';
  18. const SQLKEY = 'ALLSQLKEY';
  19. private $httpserver;
  20. private $config;
  21. private $redisconfig;
  22. private $redis;
  23. private $redisTime;
  24. private $PDatas = [];
  25. private $SettlementBaseObj;
  26. private $TaskSqlQueue;
  27. private $userMoneyTotal = [];
  28. private $fanshui = 0.01; //默认反水比例
  29. public function __construct($config)
  30. {
  31. $this->httpserver = new \swoole\http\server($config['host'], $config['port']);
  32. $this->httpserver->set($config['sets']);
  33. $this->config = $config;
  34. $this->redisconfig = GlobConfigs::getKey('redis');
  35. $this->httpserver->account = new \swoole\Atomic();
  36. $this->httpserver->taskWorkingNum = new \swoole\Atomic();
  37. $this->httpserver->on('request', array($this, 'OnRequest'));
  38. $this->httpserver->on('WorkerStart', array($this, 'onWorkerStart'));
  39. $this->httpserver->on('task', array($this, 'onTask'));
  40. $this->httpserver->on('finish', array($this, 'onFinish'));
  41. }
  42. public function onWorkerStart($serv, $worker_id)
  43. {
  44. ModelBase::init();
  45. $this->TaskSqlQueue = new \SplQueue();
  46. Swoole\Timer::tick(1000, function () {
  47. $this->TimeWork();
  48. });
  49. Swoole\Timer::tick(1000, function () {
  50. $this->TaskSQlToRedis();
  51. });
  52. if ($serv->worker_id == 0) {
  53. Swoole\Timer::tick(60000, function () {
  54. $this->logRunStatus();
  55. });
  56. }
  57. $this->getFanshui();
  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 getRedis()
  65. {
  66. if (time() - $this->redisTime <= 60 * 30) {
  67. if ($this->redis) {
  68. return $this->redis;
  69. }
  70. } else {
  71. if ($this->redis) {
  72. $this->redis->close();
  73. }
  74. }
  75. $this->redis = new \redis();
  76. $ret = $this->redis->connect($this->redisconfig['host'], $this->redisconfig['port']);
  77. if (!$ret) {
  78. $this->redis = false;
  79. throw new \Exception("连接redis失败 " . date("Y-m-d H:i:s"), 108);
  80. return;
  81. }
  82. if (!$this->redis->auth($this->redisconfig['passwd'])) {
  83. throw new \Exception("redis auth 失败 " . date("Y-m-d H:i:s"), 109);
  84. return;
  85. }
  86. $this->redis->select($this->redisconfig['db']);
  87. $this->redisTime = time();
  88. return $this->redis;
  89. }
  90. //全部结算 结束要改结算和比赛状态
  91. private function doSettelement($paras)
  92. {
  93. $begin = microtime(true);
  94. $this->userMoneyTotal = [];
  95. try {
  96. list($order_ids, $bettype, $settype, $game_code, $match_id, $change_status, $is_manual) = $this->requestpara($paras);
  97. if (!$this->DataPre($order_ids, $bettype, $settype, $game_code, $match_id, $change_status, $is_manual)) {
  98. $this->cgStatus($game_code, $match_id, $change_status);
  99. goto LABDOLAST;
  100. //throw new \Exception('没找到订单信息', 110);
  101. }
  102. $this->UndoSettlement();
  103. while (true) {
  104. $order_id = trim(array_shift($order_ids));
  105. if (empty($order_id)) {
  106. break;
  107. }
  108. $nowBuyDatasMainModel = $this->PDatas['Money_buy_Orders_Array'][$order_id];
  109. if ($nowBuyDatasMainModel->status != 1) {
  110. // continue;
  111. }
  112. $account_indent = $nowBuyDatasMainModel->account_identity;
  113. $paras = [
  114. 'type' => 'Settelement',
  115. 'bet_type' => $bettype,
  116. 'set_type' => $settype,
  117. 'order_id' => $order_id,
  118. 'game_code' => $game_code,
  119. 'match_id' => $match_id,
  120. 'simplexData' => $nowBuyDatasMainModel,
  121. 'matchDatas' => $this->PDatas['Money_buy_Match_array'][$order_id],
  122. 'settelementMidDataID' => isset($this->PDatas['settlement_middle_detail_array'][$bettype][$order_id]) ? $this->PDatas['settlement_middle_detail_array'][$bettype][$order_id]->id : 0,
  123. 'account' => $this->PDatas['account_map_array'][$account_indent]->account,
  124. 'money_prize' => isset($this->PDatas['money_prize_map'][$order_id]) ? $this->PDatas['money_prize_map'][$order_id]->id : 0
  125. ];
  126. if ($bettype == 1) {
  127. $this->singOrder($paras);
  128. $this->cgStatus($game_code, $match_id, $change_status);
  129. } else {
  130. $this->ChuanOrder($paras);
  131. $this->cgStatus($game_code, $match_id, $change_status);
  132. }
  133. $this->UserTotalMoneySql();
  134. }
  135. LABDOLAST:
  136. //$this->cgStatus($game_code, $match_id, $change_status);
  137. } catch (\Exception $e) {
  138. $this->PDatas = [];
  139. //$data = Response::generate('', $e->getCode(), '', $e->getMessage() . '--' . $e->getFile() . '--' . $e->getLine());
  140. $data = Response::generate('', $e->getCode(), '', $e->getMessage() . '--' . $e->getLine());
  141. echo $data . "\n";
  142. }
  143. $this->timeCost($begin);
  144. }
  145. private function requestpara($paras)
  146. {
  147. $this->PDatas = [];
  148. $paras = json_decode($paras, true);
  149. $idoarr = $paras['ids'];
  150. $order_ids = array_map(function ($i) {
  151. return strval($i);
  152. }, $idoarr);
  153. $bettype = isset($paras['bettype']) ? $paras['bettype'] : 0;
  154. //$settype = isset($paras['settype']) ? $paras['settype'] : 0;
  155. $settype = 2;
  156. $game_code = isset($paras['game_code']) ? $paras['game_code'] : '';
  157. $match_id = isset($paras['match_id']) ? $paras['match_id'] : 0;
  158. $change_status = isset($paras['change_status']) ? $paras['change_status'] : 0;
  159. $is_manual = isset($paras['is_manual']) ? intval($paras['is_manual']) : 0;
  160. if (intval($match_id) <= 0) {
  161. throw new \Exception('赛事ID不能为空!', 100);
  162. return;
  163. }
  164. if (empty($game_code)) {
  165. throw new \Exception('赛事类型不能为空!', 101);
  166. }
  167. if (!in_array($bettype, [1, 2])) {
  168. throw new \Exception('订单类型参数错误!', 102);
  169. }
  170. if (!in_array($settype, [1, 2])) {
  171. throw new \Exception('结算参数错误', 103);
  172. }
  173. $this->PDatas['bettype'] = $bettype;
  174. $this->PDatas['settype'] = $settype;
  175. $this->PDatas['game_code'] = $game_code;
  176. $this->PDatas['match_id'] = $match_id;
  177. $ret = [$order_ids, $bettype, $settype, $game_code, $match_id, $change_status, $is_manual];
  178. return $ret;
  179. }
  180. //重结算时,要先扣掉先前发的钱
  181. private function UndoSettlement()
  182. {
  183. if (empty($this->PDatas['settlement_middle_detail_array'])) {
  184. return true;
  185. }
  186. $bet_type = $this->GP('bettype');
  187. $nowArray = $this->PDatas['settlement_middle_detail_array'][$bet_type];
  188. $ids = [];
  189. $userMonsys = [];
  190. foreach ($nowArray as $order => $val) {
  191. $money = abs(floatval($val->money));
  192. if (100 * $money > 1) {
  193. $acc = $val->account_identity;
  194. $userMonsys[$acc] = isset($userMonsys[$acc]) ? $userMonsys[$acc] + $money : $money;
  195. $ids[] = $order;
  196. }
  197. }
  198. //一个用户多次变化金额时,合并为一条语名,减少锁表的可能性
  199. if (empty($userMonsys)) {
  200. return true;
  201. }
  202. foreach ($userMonsys as $account => $money) {
  203. $sql = "update account_detailed set cash=cash-$money where identity='$account' ";
  204. $this->PushSqlToArray($sql);
  205. }
  206. if (count($ids) <= 0) {
  207. return true;
  208. }
  209. $ids_array = array_map(function ($i) {
  210. return "'$i'";
  211. }, $ids);
  212. $ids_array = implode(",", $ids_array);
  213. unset($ids_array_0);
  214. $sql = "update settlement_middle_detail set money=0 where order_id in($ids_array) and bet_type=$bet_type ";
  215. $this->PushSqlToArray($sql);
  216. return true;
  217. }
  218. //得么一个临时变量
  219. private function GP($name)
  220. {
  221. return $this->PDatas[$name];
  222. }
  223. private function TimeWork()
  224. {
  225. $this->httpserver->taskWorkingNum->add();
  226. try {
  227. $ret = $this->getRedis()->rpop(self::TASKQNUM);
  228. if (!empty($ret)) {
  229. echo date("Y-m-d H:i:s") . " 开始处理:" . $ret . "\n";
  230. $this->httpserver->account->add();
  231. $this->doSettelement($ret);
  232. }
  233. } catch (\Exception $e) {
  234. echo "Error: " . $e->getMessage() . '--' . $e->getFile() . '--' . $e->getLine() . "\n";
  235. }
  236. $this->httpserver->taskWorkingNum->sub();
  237. }
  238. private function cgStatus($game_code, $match_id, $change_status)
  239. {
  240. if (!$change_status) {
  241. return;
  242. }
  243. $table1 = "st_" . $game_code . "_result";
  244. $table2 = "st_" . $game_code . "_competition";
  245. $this->PushSqlToArray("update $table1 set status=3 where match_id=$match_id ");
  246. $this->PushSqlToArray("update $table2 set status=3 where id=$match_id ");
  247. unset($table1, $table2);
  248. }
  249. private function timeCost($begin, $now = 0, $txt = '')
  250. {
  251. if ($now == 0) {
  252. $now = microtime(true);
  253. }
  254. $cost = $now - $begin;
  255. echo " Cost: $txt " . $cost . " 秒\n";
  256. }
  257. private function TaskSQlToRedis()
  258. {
  259. if ($this->TaskSqlQueue->isEmpty()) {
  260. return;
  261. }
  262. $this->sqltoRedisqueue();
  263. }
  264. /**
  265. * 单式注单结算
  266. * @param mixed $order_id 注单ID
  267. */
  268. private function singOrder($task)
  269. {
  270. $matchDatas = $task['matchDatas'];
  271. $simplexData = $task['simplexData'];
  272. $account_identity = $simplexData->account_identity;
  273. $bet_type = $task['bet_type'];
  274. $order_id = $task['order_id'];
  275. $match_id = $task['match_id'];
  276. $game_code = $task['game_code'];
  277. $account = $task['account'];
  278. $money_prize = $task['money_prize'];
  279. foreach ($matchDatas as $val) {
  280. if (!in_array($val->result, [-1, 1, 2, 3, 4])) {
  281. throw new \Exception('match比赛结果异常或还没有输赢结果->' . $val->id, 104);
  282. }
  283. }
  284. if (empty($this->SettlementBaseObj)) {
  285. $settlementBase = $this->SettlementBaseObj = new SettlementBase();
  286. } else {
  287. $settlementBase = $this->SettlementBaseObj;
  288. }
  289. $returnMoney = 0;
  290. $oddsResult = [];
  291. foreach ($matchDatas as $k => $v) {
  292. if ($v->result == -1) {
  293. continue;
  294. }
  295. $oddsResult[0]['winOrLose'] = $v->result;
  296. $oddsResult[0]['odds'] = $v->odds;
  297. $getReturnMoney = $settlementBase->stringOdds($oddsResult);
  298. if ($v->result == 2) {
  299. //返水钱扣掉
  300. $pv = $this->gethuishuilv();
  301. $getReturnMoney['returnMoney'] = 1 - $pv;
  302. }
  303. $returnMoney += $getReturnMoney['returnMoney'] * $v->bet_money;
  304. }
  305. $returnMoney = sprintf("%.2f", substr(sprintf("%.3f", $returnMoney), 0, -1));
  306. // 判断盈亏 1 赢 2 输 3 平
  307. $game_status = $returnMoney > $simplexData->money ? 1 : ($returnMoney == $simplexData->money ? 3 : 2);
  308. // 修改投注表状态及盈亏
  309. $this->PushSqlToArray(["update money_buy_simplex set settle_status=2, game_status=$game_status,gain_money=$returnMoney where order_id='$order_id' "]);
  310. $this->userMoneyTotal[$account_identity] = isset($this->userMoneyTotal[$account_identity]) ? $this->userMoneyTotal[$account_identity] + $returnMoney : $returnMoney;
  311. $this->WriteOrAddSettlement($game_code, $match_id, $bet_type, $order_id, $account_identity, $returnMoney);
  312. $this->insertData(
  313. $order_id,
  314. $returnMoney,
  315. $account_identity,
  316. 1,
  317. $game_code,
  318. $simplexData->info_identity,
  319. $simplexData->money,
  320. $match_id,
  321. $account,
  322. $money_prize
  323. );
  324. }
  325. private function WriteOrAddSettlement($game_code, $match_id, $bet_type, $order_id, $account_ident, $money)
  326. {
  327. if (intval($money * 1000) < 10) {
  328. return;
  329. }
  330. if (isset($this->PDatas['settlement_middle_detail_array'][$bet_type][$order_id])) {
  331. $this->PushSqlToArray("update settlement_middle_detail set money=$money where game_code='$game_code' and bet_type=$bet_type and order_id='$order_id' ");
  332. } else {
  333. $this->PushSqlToArray("insert into settlement_middle_detail(game_code,match_id,account_identity,bet_type,order_id,money) values('$game_code','$match_id','$account_ident',$bet_type,'$order_id',$money)");
  334. }
  335. return true;
  336. }
  337. private function PushSqlToArray($cmdArray)
  338. {
  339. if (is_string($cmdArray)) {
  340. $this->TaskSqlQueue->enqueue($cmdArray);
  341. } else {
  342. foreach ($cmdArray as $sql) {
  343. $this->TaskSqlQueue->enqueue($sql);
  344. }
  345. }
  346. }
  347. private function ChuanOrder($task)
  348. {
  349. $matchDatas = $task['matchDatas'];
  350. $simplexData = $task['simplexData'];
  351. $account_identity = $simplexData->account_identity;
  352. $bet_type = $task['bet_type'];
  353. $order_id = $task['order_id'];
  354. $match_id = $task['match_id'];
  355. $game_code = $task['game_code'];
  356. $account = $task['account'];
  357. $money_prize = $task['money_prize'];
  358. //如果有一条为输的,订单直接为输了
  359. foreach ($matchDatas as $val) {
  360. if ($val->result == -1) {
  361. $this->PushSqlToArray("update money_buy_str set wait_match_num=0, prize_note=0, game_status=3, settle_status=2, gain_money=0,settlement_time='" . date('Y-m-d H:i:s') . "' where order_id='$order_id' ");
  362. return;
  363. }
  364. }
  365. foreach ($matchDatas as $val) {
  366. if (!in_array($val->result, [1, 2, 3, 4])) {
  367. throw new \Exception('match比赛结果异常或还没有输赢结果->' . $val->id, 105);
  368. }
  369. }
  370. $matchModels = $matchDatas;
  371. if (count($matchModels) <= 0) {
  372. throw new \Exception('match 数据异常-' . $order_id, 106);
  373. }
  374. if (empty($this->SettlementBaseObj)) {
  375. $settlementBase = $this->SettlementBaseObj = new SettlementBase();
  376. } else {
  377. $settlementBase = $this->SettlementBaseObj;
  378. }
  379. $newTime = date('Y-m-d H:i:s');
  380. if ($simplexData->status == 1) {
  381. $in_array = [];
  382. foreach ($matchModels as $val) {
  383. if (!in_array($val->result, [-1, 1, 2, 3, 4])) {
  384. throw new \Exception('match 比赛结果异常或还没有输赢结果->' . $val->id, 107);
  385. }
  386. if ($val->result == -1) {
  387. $this->PushSqlToArray("update money_buy_str set wait_match_num=0, prize_note=0, game_status=3, settle_status=2, gain_money=0,settlement_time='" . date('Y-m-d H:i:s') . "' where order_id='$order_id' ");
  388. return true;
  389. }
  390. $in_array[] = ['odds' => $val->odds, 'winOrLose' => $val->result];
  391. }
  392. $chuanNum = intval(substr($simplexData->str_type, 0, 1));
  393. $lasPeilv = $settlementBase->stringComputing([$in_array, $chuanNum]);
  394. $money = floatPointDigit($simplexData->money * $lasPeilv);
  395. } else {
  396. $money = $simplexData->money;
  397. }
  398. $money = floatPointDigit($money - $simplexData->money * $this->fanshui);
  399. $this->PushSqlToArray(" update money_buy_str set settle_status=2 , game_status=1 , settlement_time='$newTime' , gain_money=$money where order_id = '$order_id'");
  400. $this->WriteOrAddSettlement($game_code, $match_id, $bet_type, $order_id, $account_identity, $money);
  401. $useraccident = $simplexData->account_identity;
  402. $this->userMoneyTotal[$useraccident] = isset($this->userMoneyTotal[$useraccident]) ? $this->userMoneyTotal[$useraccident] + $money : $money;
  403. $this->insertData($order_id, $money, $useraccident, 2, $game_code, $simplexData->info_identity, $money, $match_id, $account, $money_prize);
  404. return true;
  405. }
  406. /**
  407. * 结算数据填入
  408. * @param mixed $order_id 注单ID
  409. * @param mixed $returnMoney 返现金额
  410. * @param mixed $account_identity 用户ID
  411. * @param mixed $type 1单式 2串式
  412. * @param mixed $game_name 游戏名(zq,lq)
  413. * @param mixed $buy_identity 游戏投注id
  414. * @param mixed $money 投注金额
  415. * @param mixed $match_id 赛场ID
  416. */
  417. private function insertData($order_id, $returnMoney, $account_identity, $type, $game_name, $buy_identity, $money, $match_id = 0, $account = '', $money_prizeID = 0)
  418. {
  419. // 添加流水记录
  420. $info_identity = UUID();
  421. $money_time = date('Y-m-d H:i:s', time());
  422. $trade_desc = $type == 1 ? '单式投注订单回款' : '串式投注订单回款';
  423. $reason = $type == 1 ? '单式投注订单回款' : '串式投注订单回款';
  424. //用于记录用户余额的数据
  425. if (!isset($this->PDatas['account_mash'][$account_identity])) {
  426. $this->PDatas['account_mash'][$account_identity] = $this->PDatas['account_indentity_map_array'][$account_identity]->cash;
  427. }
  428. $this->PDatas['account_mash'][$account_identity] = $this->PDatas['account_mash'][$account_identity] + $returnMoney;
  429. $user_money_cash = $this->PDatas['account_mash'][$account_identity];
  430. if (isset($this->PDatas['money_detail_array'][$order_id])) {
  431. $sql = "update money_details set money=$returnMoney,money_cash=$user_money_cash where account_identity='$account_identity' and trade_id='$order_id' and trade_type=4 ";
  432. } else {
  433. $sql = "insert into money_details(info_identity,trade_id,account_name,account_identity,money,money_time,money_type,money_cash,trade_type,trade_desc,reason,sysetem_user,status) values(";
  434. $sql .= "'$info_identity','$order_id','$account','$account_identity',$returnMoney,'$money_time',1,$user_money_cash,4,'$trade_desc','$reason','系统',1)";
  435. }
  436. $this->PushSqlToArray($sql);
  437. // 修改用余额 在外面合并了,不需要了
  438. //$this->PushSqlToArray("update account_detailed set available_cash=available_cash+$money,cash=cash+$money where account_identity='$account_identity' ");
  439. // 新增用户中奖信息
  440. $content = $type == 1 ? '您的单式投注订单' . $order_id . '于' . $money_time . '成功回款' . $returnMoney . '该次投注流程结束,如有疑问请联系客服'
  441. : '您的串式投注订单' . $order_id . '于' . $money_time . '成功回款' . $returnMoney . '该次投注流程结束,如有疑问请联系客服';
  442. $sql = "insert into account_news(identity,account_identity,title,content,details,write_time,read_status,type) values ";
  443. $sql .= "('$info_identity','$account_identity','投注订单回款通知','$content','$content','$money_time',-1,1)";
  444. $this->PushSqlToArray($sql);
  445. // 新增中奖记录表
  446. $table = 'money_prize';
  447. if ($money_prizeID) {
  448. $this->PushSqlToArray("update $table set money=$money,prize_money=$returnMoney, get_money= $returnMoney - $money where id=$money_prizeID");
  449. } else {
  450. $sql = "insert into $table (info_identity,order_id,account_identity,account_name,game_name,buy_identity,money,money_time,status,prize_money,get_money) values ";
  451. $sql .= "('$info_identity','$order_id','$account_identity','$account','$game_name','$buy_identity',$money,'$money_time',1,$returnMoney,$returnMoney - $money)";
  452. $this->PushSqlToArray($sql);
  453. }
  454. return true;
  455. }
  456. //对同一用户账号资金变化更改合并操作,减少锁表可能
  457. private function UserTotalMoneySql()
  458. {
  459. if (!empty($this->userMoneyTotal)) {
  460. foreach ($this->userMoneyTotal as $account_identity => $money) {
  461. $this->PushSqlToArray("update account_detailed set available_cash=available_cash+$money,cash=cash+$money where account_identity='$account_identity' ");
  462. }
  463. }
  464. }
  465. //数据预批量获取
  466. private function DataPre($order_ids, $bettype, $settype, $game_code, $match_id, $change_status, $is_manual = 0)
  467. {
  468. if ($bettype == 1) {
  469. $moneytable = 'money_buy_simplex';
  470. //$ret = DB::table('money_buy_simplex')->where(['game_code' => $game_code, 'match_id' => $match_id, 'is_manual' => $is_manual])->whereIn('order_id', $order_ids)->get();
  471. $tmpwhere = [['game_code', '=', $game_code], ['match_id', '=', $match_id], ['is_manual', '=', $is_manual], ['roll_ratify', '>=', 0]];
  472. $ret = DB::table('money_buy_simplex')->where($tmpwhere)->whereIn('order_id', $order_ids)->get();
  473. } else {
  474. $moneytable = 'money_buy_str';
  475. $ret = DB::table('money_buy_str')->whereIn('order_id', $order_ids)->get();
  476. }
  477. if (empty($ret) || count($ret) <= 0) {
  478. return false;
  479. }
  480. foreach ($ret as $val) {
  481. $this->PDatas['Money_buy_Orders_Array'][$val->order_id] = $val;
  482. $orderarr[] = $val->order_id;
  483. $userindentys[] = $val->account_identity;
  484. }
  485. $userindentys = array_unique($userindentys);
  486. if ($bettype == 1) {
  487. $ret = DB::table('money_buy_match')->where(['game_code' => $game_code, 'match_id' => $match_id, 'bet_type' => 1])->whereIn('order_id', $orderarr)->get();
  488. } else {
  489. $ret = DB::table('money_buy_match')->where(['game_code' => $game_code, 'bet_type' => 2])->whereIn('order_id', $orderarr)->get();
  490. }
  491. foreach ($ret as $val) {
  492. $this->PDatas['Money_buy_Match_array'][$val->order_id][] = $val;
  493. }
  494. $ret = DB::table('account_detailed')->whereIn('account_identity', $userindentys)->get();
  495. foreach ($ret as $val) {
  496. $this->PDatas['account_indentity_map_array'][$val->account_identity] = $val;
  497. }
  498. $ret = DB::table('account')->whereIn('identity', $userindentys)->get();
  499. foreach ($ret as $val) {
  500. $this->PDatas['account_map_array'][$val->identity] = $val;
  501. }
  502. if ($settype == 2) {
  503. $ret = DB::table('settlement_middle_detail')->where(['game_code' => $game_code, 'match_id' => $match_id])->whereIn('order_id', $order_ids)->get();
  504. if ($ret) {
  505. foreach ($ret as $val) {
  506. $this->PDatas['settlement_middle_detail_array'][$val->bet_type][$val->order_id] = $val;
  507. }
  508. }
  509. $order_str_1 = array_map(function ($item) {
  510. return "'$item'";
  511. }, $orderarr);
  512. $order_str = implode(",", $order_str_1);
  513. if ($bettype == 1) {
  514. $sql_tmp = "update $moneytable set settle_status=1, gain_money=0, game_status=0 where game_code='$game_code' and match_id=$match_id and status=1 and order_id in ($order_str) ";
  515. } else {
  516. $sql_tmp = "update $moneytable set settle_status=1, gain_money=0 where order_id in ($order_str) ";
  517. }
  518. $this->PushSqlToArray($sql_tmp);
  519. }
  520. $ret = DB::table('money_prize')->whereIn('order_id', $order_ids)->where('money', '>', 0.01)->get();
  521. if ($ret) {
  522. foreach ($ret as $val) {
  523. $this->PDatas['money_prize_map'][$val->order_id] = $val;
  524. }
  525. }
  526. $ret = DB::table('money_details')->whereIn('account_identity', $userindentys)->where([['money_time', '>=', date("Y-m-d", time() - 86400 * 90) . ' 00:00:00'], ['trade_type', '=', 4]])->whereIn('trade_id', $order_ids)->get();
  527. if ($ret) {
  528. foreach ($ret as $val) {
  529. $this->PDatas['money_detail_array'][$val->trade_id] = $val;
  530. }
  531. }
  532. return true;
  533. }
  534. private function gethuishuilv()
  535. {
  536. $key = "HUISHUILV";
  537. $rv = $this->getRedis()->get($key);
  538. if ($rv) {
  539. return floatval($rv);
  540. }
  541. $rv = DB::table('setinfo')->where('identity', '1000')->first();
  542. if ($rv) {
  543. $pl = floatval(trim($rv->infocontent));
  544. $this->getRedis()->setex($key, 300, $pl);
  545. return $pl;
  546. }
  547. return 0;
  548. }
  549. private function sqltoRedisqueue()
  550. {
  551. $sqlQueue = $this->TaskSqlQueue;
  552. $redis = $this->getRedis();
  553. $i = 1;
  554. $maxStep = 10000;
  555. $sql = '';
  556. if (!$sqlQueue->isEmpty()) {
  557. $redis->pipeline();
  558. while ($i < $maxStep) {
  559. if (!$sqlQueue->isEmpty()) {
  560. $sql = $sqlQueue->dequeue();
  561. if (empty($sql)) {
  562. break;
  563. }
  564. $redis->lpush(self::SQLKEY, $sql);
  565. }
  566. $i++;
  567. }
  568. $redis->exec();
  569. }
  570. unset($sqlQueue, $sql, $i, $maxStep, $ret);
  571. return;
  572. }
  573. public function OnRequest($request, $response)
  574. {
  575. }
  576. public function onTask($serv, $task)
  577. {
  578. }
  579. public function onFinish($serv, int $task_id, $data)
  580. {
  581. }
  582. //得到返水的比例
  583. public function getFanshui()
  584. {
  585. $ret = (new SetinfoModel())->getInfo(1001);
  586. if (is_array($ret)) {
  587. $val = floatval($ret->infocontent);
  588. $this->fanshui = $val;
  589. return $val;
  590. }
  591. }
  592. public function start()
  593. {
  594. $this->httpserver->start();
  595. }
  596. }