HttpServerOnlySet.php 27 KB

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