HttpServerOnlySet.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833
  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. goto LABDOLAST;
  100. }
  101. $this->UndoSettlement($order_ids, $bettype, $settype, $game_code, $match_id, $change_status, $is_manual);
  102. while (true) {
  103. $order_id = trim(array_shift($order_ids));
  104. if (empty($order_id)) {
  105. break;
  106. }
  107. $nowBuyDatasMainModel = $this->PDatas['Money_buy_Orders_Array'][$order_id];
  108. $account_indent = $nowBuyDatasMainModel->account_identity;
  109. $paras = [
  110. 'type' => 'Settelement',
  111. 'bet_type' => $bettype,
  112. 'set_type' => $settype,
  113. 'order_id' => $order_id,
  114. 'game_code' => $game_code,
  115. 'match_id' => $match_id,
  116. 'simplexData' => $nowBuyDatasMainModel,
  117. 'matchDatas' => $this->PDatas['Money_buy_Match_array'][$order_id],
  118. 'settelementMidDataID' => isset($this->PDatas['settlement_middle_detail_array'][$bettype][$order_id]) ? $this->PDatas['settlement_middle_detail_array'][$bettype][$order_id]->id : 0,
  119. 'account' => $this->PDatas['account_map_array'][$account_indent]->account,
  120. 'money_prize' => isset($this->PDatas['money_prize_map'][$order_id]) ? $this->PDatas['money_prize_map'][$order_id]->id : 0
  121. ];
  122. try {
  123. if ($bettype == 1) {
  124. $this->singOrder($paras);
  125. } else {
  126. $this->ChuanOrder($paras);
  127. }
  128. } catch (\Exception $e) {
  129. }
  130. }
  131. $this->UserTotalMoneySql();
  132. LABDOLAST:
  133. $this->cgStatus($game_code, $match_id, $change_status);
  134. } catch (\Exception $e) {
  135. $this->PDatas = [];
  136. //$data = Response::generate('', $e->getCode(), '', $e->getMessage() . '--' . $e->getFile() . '--' . $e->getLine());
  137. $data = Response::generate('', $e->getCode(), '', $e->getMessage() . '--' . $e->getLine());
  138. echo $data . "\n";
  139. }
  140. $this->timeCost($begin);
  141. }
  142. private function requestpara($paras)
  143. {
  144. $this->PDatas = [];
  145. $paras = json_decode($paras, true);
  146. $idoarr = $paras['ids'];
  147. $order_ids = array_map(function ($i) {
  148. return strval($i);
  149. }, $idoarr);
  150. $bettype = isset($paras['bettype']) ? $paras['bettype'] : 0;
  151. //$settype = isset($paras['settype']) ? $paras['settype'] : 0;
  152. $settype = 2;
  153. $game_code = isset($paras['game_code']) ? $paras['game_code'] : '';
  154. $match_id = isset($paras['match_id']) ? $paras['match_id'] : 0;
  155. $change_status = isset($paras['change_status']) ? $paras['change_status'] : 0;
  156. $is_manual = isset($paras['is_manual']) ? intval($paras['is_manual']) : 0;
  157. if (intval($match_id) <= 0) {
  158. throw new \Exception('赛事ID不能为空!', 100);
  159. return;
  160. }
  161. if (empty($game_code)) {
  162. throw new \Exception('赛事类型不能为空!', 101);
  163. }
  164. if (!in_array($bettype, [1, 2])) {
  165. throw new \Exception('订单类型参数错误!', 102);
  166. }
  167. if (!in_array($settype, [1, 2])) {
  168. throw new \Exception('结算参数错误', 103);
  169. }
  170. $this->PDatas['bettype'] = $bettype;
  171. $this->PDatas['settype'] = $settype;
  172. $this->PDatas['game_code'] = $game_code;
  173. $this->PDatas['match_id'] = $match_id;
  174. $ret = [$order_ids, $bettype, $settype, $game_code, $match_id, $change_status, $is_manual];
  175. return $ret;
  176. }
  177. //重结算时,要先扣掉先前发的钱
  178. private function UndoSettlement($order_ids, $bettype, $settype, $game_code, $match_id, $change_status, $is_manual)
  179. {
  180. $bet_type = $this->GP('bettype');
  181. $ids = [];
  182. $userMonsys = [];
  183. //先前赢过钱的得先扣回来,写流水,再重新计算,
  184. $money_time = date("Y-m-d H:i:s");
  185. $moneys_this = $this->CountDetailsTounSet($order_ids);
  186. foreach ($moneys_this as $tmp_orderid => $ttttobj) {
  187. $now_order = $this->PDatas['Money_buy_Orders_Array'][$tmp_orderid];
  188. if ($now_order->status != 1) {
  189. //只处理状态为1的数据
  190. continue;
  191. }
  192. if ($ttttobj['cha'] !== 0) {
  193. $order_id = $now_order->order_id;
  194. $account_identity = $now_order->account_identity;
  195. $account = $this->PDatas['account_map_array'][$account_identity]->account;
  196. $returnMoney = abs($ttttobj['cha']);
  197. if ($returnMoney == 0) {
  198. continue;
  199. }
  200. $userMonsys[$account_identity] = isset($userMonsys[$account_identity]) ? $userMonsys[$account_identity] + $ttttobj['cha'] : $ttttobj['cha'];
  201. $ids[] = $tmp_orderid;
  202. $info_identity = uuid();
  203. $trade_desc = '重结算[撤销之前]';
  204. $reason = '重结算[撤销之前]';
  205. $money_type = $ttttobj['cha'] > 0 ? 1 : 2;
  206. $user_money_cash = $this->PDatas['account_indentity_map_array'][$account_identity]->cash - $now_order->gain_money;
  207. $this->PDatas['account_indentity_map_array'][$account_identity]->cash = $user_money_cash;
  208. $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(";
  209. $sql .= "'$info_identity','$order_id','$account','$account_identity',$returnMoney,'$money_time',$money_type,$user_money_cash,4,'$trade_desc','$reason','系统',1)";
  210. $this->PushSqlToArray($sql);
  211. }
  212. }
  213. //一个用户多次变化金额时,合并为一条语名,减少锁表的可能性
  214. if (empty($userMonsys)) {
  215. return true;
  216. }
  217. foreach ($userMonsys as $account => $money) {
  218. if ($money > 0) {
  219. $sql = "update account_detailed set cash=cash-$money where identity='$account' ";
  220. } else {
  221. $money = abs($money);
  222. $sql = "update account_detailed set cash=cash+$money where identity='$account' ";
  223. }
  224. //$sql = "update account_detailed set cash=cash-$money where identity='$account' ";
  225. $this->PushSqlToArray($sql);
  226. }
  227. if (count($ids) <= 0) {
  228. return true;
  229. }
  230. $ids_array = array_map(function ($i) {
  231. return "'$i'";
  232. }, $ids);
  233. $ids_array = implode(",", $ids_array);
  234. unset($ids_array_0);
  235. $sql = "update settlement_middle_detail set money=0 where order_id in($ids_array) and bet_type=$bet_type ";
  236. $this->PushSqlToArray($sql);
  237. $table = ($bettype == 1) ? 'money_buy_simplex' : 'money_buy_str';
  238. $sql = "update $table set gain_money=0 where order_id in($ids_array) ";
  239. $this->PushSqlToArray($sql);
  240. return true;
  241. }
  242. //统计流水表中 已结算的订单的 输赢数据,用于修重结算结果
  243. public function CountDetailsTounSet($ids)
  244. {
  245. $ids_array = array_map(function ($i) {
  246. return "'$i'";
  247. }, $ids);
  248. $ids_array = implode(",", $ids_array);
  249. $sql = "select trade_id,sum(money) as msum,money_type,account_identity from money_details where trade_id in ($ids_array) and trade_type = 4 group by trade_id,account_identity,money_type ";
  250. $ret = DB::select($sql);
  251. $return = [];
  252. foreach ($ret as $val) {
  253. $order_id = $val->trade_id;
  254. $money_type = $val->money_type;
  255. $money = $val->msum;
  256. $acc = $val->account_identity;
  257. if (!isset($return[$order_id][$money_type])) {
  258. $return[$order_id][$money_type] = $money;
  259. }
  260. if (!isset($return[$order_id]['account'])) {
  261. $return[$order_id]['account'] = $acc;
  262. }
  263. }
  264. foreach ($return as $order_id => $val) {
  265. if (!isset($return[$order_id]['1'])) {
  266. $return[$order_id]['1'] = 0;
  267. }
  268. if (!isset($return[$order_id]['2'])) {
  269. $return[$order_id]['2'] = 0;
  270. }
  271. }
  272. foreach ($return as $order_id => $val) {
  273. //收入 支出差 如果为正则扣多了要加钱,为负则加多了钱要扣回来
  274. $return[$order_id]['cha'] = -1 * ($return[$order_id]['1'] - $return[$order_id]['2']);
  275. }
  276. return $return;
  277. }
  278. //得么一个临时变量
  279. private function GP($name)
  280. {
  281. return $this->PDatas[$name];
  282. }
  283. private function TimeWork()
  284. {
  285. $this->httpserver->taskWorkingNum->add();
  286. try {
  287. $ret = $this->getRedis()->rpop(self::TASKQNUM);
  288. if (!empty($ret)) {
  289. echo date("Y-m-d H:i:s") . " 开始处理:" . $ret . "\n";
  290. $this->httpserver->account->add();
  291. $this->doSettelement($ret);
  292. }
  293. } catch (\Exception $e) {
  294. echo "Error: " . $e->getMessage() . '--' . $e->getFile() . '--' . $e->getLine() . "\n";
  295. }
  296. $this->httpserver->taskWorkingNum->sub();
  297. }
  298. private function cgStatus($game_code, $match_id, $change_status)
  299. {
  300. if (!$change_status) {
  301. return;
  302. }
  303. $result = $this->PDatas['game_result'];
  304. if (empty($result)){
  305. $table = 'st_' . $game_code . '_result';
  306. $result = DB::table($table)->where('match_id', $match_id)->first();
  307. }
  308. if ($result->status == 1 || $result->status == 0) {
  309. var_dump($result);
  310. echo '比赛未结束,不能更改为全部已结算状态-- ' . $game_code . ' -match_id ' . $match_id . "\n";
  311. //未结束的比赛不改状态
  312. return;
  313. }
  314. $table1 = "st_" . $game_code . "_result";
  315. $table2 = "st_" . $game_code . "_competition";
  316. $this->PushSqlToArray("update $table1 set status=3 where match_id=$match_id ");
  317. $this->PushSqlToArray("update $table2 set status=3 where id=$match_id ");
  318. unset($table1, $table2);
  319. }
  320. private function timeCost($begin, $now = 0, $txt = '')
  321. {
  322. if ($now == 0) {
  323. $now = microtime(true);
  324. }
  325. $cost = $now - $begin;
  326. echo " Cost: $txt " . $cost . " 秒\n";
  327. }
  328. private function TaskSQlToRedis()
  329. {
  330. if ($this->TaskSqlQueue->isEmpty()) {
  331. return;
  332. }
  333. $this->sqltoRedisqueue();
  334. }
  335. /**
  336. * 单式注单结算
  337. * @param mixed $order_id 注单ID
  338. */
  339. private function singOrder($task)
  340. {
  341. $matchDatas = $task['matchDatas'];
  342. $simplexData = $task['simplexData'];
  343. $account_identity = $simplexData->account_identity;
  344. $bet_type = $task['bet_type'];
  345. $order_id = $task['order_id'];
  346. $match_id = $task['match_id'];
  347. $game_code = $task['game_code'];
  348. $account = $task['account'];
  349. $money_prize = $task['money_prize'];
  350. foreach ($matchDatas as $val) {
  351. if (!in_array($val->result, [-1, 1, 2, 3, 4])) {
  352. throw new \Exception('match比赛结果异常或还没有输赢结果->' . $val->id, 104);
  353. }
  354. }
  355. if (empty($this->SettlementBaseObj)) {
  356. $settlementBase = $this->SettlementBaseObj = new SettlementBase();
  357. } else {
  358. $settlementBase = $this->SettlementBaseObj;
  359. }
  360. $returnMoney = 0;
  361. $oddsResult = [];
  362. if ($simplexData->status == 1) {
  363. foreach ($matchDatas as $k => $v) {
  364. if ($v->result == -1) {
  365. /*
  366. if (isset($this->PDatas['money_detail_array'][$order_id])) {
  367. $sql = "update money_details set money=0 where account_identity='$account_identity' and trade_id='$order_id' and trade_type=4 ";
  368. $this->PushSqlToArray([$sql]);
  369. }
  370. */
  371. continue;
  372. }
  373. $oddsResult[0]['winOrLose'] = $v->result;
  374. $oddsResult[0]['odds'] = $v->odds;
  375. $getReturnMoney = $settlementBase->stringOdds($oddsResult);
  376. if ($v->result == 2) {
  377. //返水钱扣掉
  378. $pv = $this->gethuishuilv();
  379. $getReturnMoney['returnMoney'] = 1 - $pv;
  380. }
  381. $returnMoney += $getReturnMoney['returnMoney'] * $v->bet_money;
  382. }
  383. } else {
  384. //$returnMoney = $simplexData->money;
  385. //$returnMoney = 0;
  386. return;
  387. }
  388. //$returnMoney = floatPointDigit($returnMoney - $simplexData->money * $this->fanshui);
  389. $returnMoney = floatPointDigit($returnMoney);
  390. // 判断盈亏 1 赢 2 输 3 平
  391. $game_status = $returnMoney > $simplexData->money ? 1 : ($returnMoney == $simplexData->money ? 3 : 2);
  392. // 修改投注表状态及盈亏
  393. $this->PushSqlToArray(["update money_buy_simplex set settle_status=2, game_status=$game_status,gain_money=$returnMoney where order_id='$order_id' "]);
  394. $this->userMoneyTotal[$account_identity] = isset($this->userMoneyTotal[$account_identity]) ? $this->userMoneyTotal[$account_identity] + $returnMoney : $returnMoney;
  395. $this->WriteOrAddSettlement($game_code, $match_id, $bet_type, $order_id, $account_identity, $returnMoney);
  396. $this->insertData(
  397. $order_id,
  398. $returnMoney,
  399. $account_identity,
  400. 1,
  401. $game_code,
  402. $simplexData->info_identity,
  403. $simplexData->money,
  404. $match_id,
  405. $account,
  406. $money_prize
  407. );
  408. }
  409. private function WriteOrAddSettlement($game_code, $match_id, $bet_type, $order_id, $account_ident, $money)
  410. {
  411. if (intval($money * 1000) < 10) {
  412. return;
  413. }
  414. if (isset($this->PDatas['settlement_middle_detail_array'][$bet_type][$order_id])) {
  415. $this->PushSqlToArray("update settlement_middle_detail set money=$money where game_code='$game_code' and bet_type=$bet_type and order_id='$order_id' ");
  416. } else {
  417. $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)");
  418. }
  419. return true;
  420. }
  421. private function PushSqlToArray($cmdArray)
  422. {
  423. if (is_string($cmdArray)) {
  424. $this->TaskSqlQueue->enqueue($cmdArray);
  425. } else {
  426. foreach ($cmdArray as $sql) {
  427. $this->TaskSqlQueue->enqueue($sql);
  428. }
  429. }
  430. }
  431. private function ChuanOrder($task)
  432. {
  433. $matchDatas = $task['matchDatas'];
  434. $simplexData = $task['simplexData'];
  435. $account_identity = $simplexData->account_identity;
  436. $bet_type = $task['bet_type'];
  437. $order_id = $task['order_id'];
  438. $match_id = $task['match_id'];
  439. $game_code = $task['game_code'];
  440. $account = $task['account'];
  441. $money_prize = $task['money_prize'];
  442. //如果有一条为输的,订单直接为输了
  443. foreach ($matchDatas as $val) {
  444. if ($val->result == -1) {
  445. $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' ");
  446. return;
  447. }
  448. }
  449. foreach ($matchDatas as $val) {
  450. if (!in_array($val->result, [1, 2, 3, 4])) {
  451. throw new \Exception('match比赛结果异常或还没有输赢结果->' . $val->id, 105);
  452. }
  453. }
  454. $matchModels = $matchDatas;
  455. if (count($matchModels) <= 0) {
  456. throw new \Exception('match 数据异常-' . $order_id, 106);
  457. }
  458. if (empty($this->SettlementBaseObj)) {
  459. $settlementBase = $this->SettlementBaseObj = new SettlementBase();
  460. } else {
  461. $settlementBase = $this->SettlementBaseObj;
  462. }
  463. $newTime = date('Y-m-d H:i:s');
  464. if ($simplexData->status == 1) {
  465. $in_array = [];
  466. foreach ($matchModels as $val) {
  467. if (!in_array($val->result, [-1, 1, 2, 3, 4])) {
  468. throw new \Exception('match 比赛结果异常或还没有输赢结果->' . $val->id, 107);
  469. }
  470. if ($val->result == -1) {
  471. $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' ");
  472. return true;
  473. }
  474. $in_array[] = ['odds' => $val->odds, 'winOrLose' => $val->result];
  475. }
  476. $chuanNum = intval(substr($simplexData->str_type, 0, 1));
  477. $lasPeilv = $settlementBase->stringComputing([$in_array, $chuanNum]);
  478. $money_s = floatPointDigit($simplexData->money * $lasPeilv);
  479. } else {
  480. // $money_s = $simplexData->money;
  481. //$money_s = floatPointDigit($money_s - $simplexData->money * $this->fanshui);
  482. return;
  483. }
  484. //$money = floatPointDigit($money_s - $simplexData->money * $this->fanshui);
  485. $money = floatPointDigit($money_s);
  486. $this->PushSqlToArray(" update money_buy_str set settle_status=2 , game_status=1 , settlement_time='$newTime' , gain_money=$money where order_id = '$order_id'");
  487. $this->WriteOrAddSettlement($game_code, $match_id, $bet_type, $order_id, $account_identity, $money);
  488. $useraccident = $simplexData->account_identity;
  489. $this->userMoneyTotal[$useraccident] = isset($this->userMoneyTotal[$useraccident]) ? $this->userMoneyTotal[$useraccident] + $money : $money;
  490. $this->insertData($order_id, $money, $useraccident, 2, $game_code, $simplexData->info_identity, $money, $match_id, $account, $money_prize);
  491. return true;
  492. }
  493. /**
  494. * 结算数据填入
  495. * @param mixed $order_id 注单ID
  496. * @param mixed $returnMoney 返现金额
  497. * @param mixed $account_identity 用户ID
  498. * @param mixed $type 1单式 2串式
  499. * @param mixed $game_name 游戏名(zq,lq)
  500. * @param mixed $buy_identity 游戏投注id
  501. * @param mixed $money 投注金额
  502. * @param mixed $match_id 赛场ID
  503. */
  504. private function insertData($order_id, $returnMoney, $account_identity, $type, $game_name, $buy_identity, $money, $match_id = 0, $account = '', $money_prizeID = 0)
  505. {
  506. // 添加流水记录
  507. $info_identity = UUID();
  508. $money_time = date('Y-m-d H:i:s', time());
  509. $trade_desc = $type == 1 ? '单式投注订单回款' : '串式投注订单回款';
  510. $reason = $type == 1 ? '单式投注订单回款' : '串式投注订单回款';
  511. //用于记录用户余额的数据
  512. if (!isset($this->PDatas['account_mash'][$account_identity])) {
  513. $this->PDatas['account_mash'][$account_identity] = $this->PDatas['account_indentity_map_array'][$account_identity]->cash;
  514. }
  515. if ($returnMoney <= 0) {
  516. return;
  517. }
  518. $this->PDatas['account_mash'][$account_identity] = $this->PDatas['account_mash'][$account_identity] + $returnMoney;
  519. $user_money_cash = $this->PDatas['account_mash'][$account_identity];
  520. $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(";
  521. $sql .= "'$info_identity','$order_id','$account','$account_identity',$returnMoney,'$money_time',1,$user_money_cash,4,'$trade_desc','$reason','系统',1)";
  522. $this->PushSqlToArray($sql);
  523. // 修改用余额 在外面合并了,不需要了
  524. //$this->PushSqlToArray("update account_detailed set available_cash=available_cash+$money,cash=cash+$money where account_identity='$account_identity' ");
  525. // 新增用户中奖信息
  526. $content = $type == 1 ? '您的单式投注订单' . $order_id . '于' . $money_time . '成功回款' . $returnMoney . '该次投注流程结束,如有疑问请联系客服'
  527. : '您的串式投注订单' . $order_id . '于' . $money_time . '成功回款' . $returnMoney . '该次投注流程结束,如有疑问请联系客服';
  528. $sql = "insert into account_news(identity,account_identity,title,content,details,write_time,read_status,type) values ";
  529. $sql .= "('$info_identity','$account_identity','投注订单回款通知','$content','$content','$money_time',-1,1)";
  530. $this->PushSqlToArray($sql);
  531. // 新增中奖记录表
  532. $table = 'money_prize';
  533. if ($money_prizeID) {
  534. $this->PushSqlToArray("update $table set money=$money,prize_money=$returnMoney, get_money= $returnMoney - $money where id=$money_prizeID");
  535. } else {
  536. $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 ";
  537. $sql .= "('$info_identity','$order_id','$account_identity','$account','$game_name','$buy_identity',$money,'$money_time',1,$returnMoney,$returnMoney - $money)";
  538. $this->PushSqlToArray($sql);
  539. }
  540. return true;
  541. }
  542. //对同一用户账号资金变化更改合并操作,减少锁表可能
  543. private function UserTotalMoneySql()
  544. {
  545. if (!empty($this->userMoneyTotal)) {
  546. foreach ($this->userMoneyTotal as $account_identity => $money) {
  547. $this->PushSqlToArray("update account_detailed set available_cash=available_cash+$money,cash=cash+$money where account_identity='$account_identity' ");
  548. }
  549. }
  550. }
  551. //数据预批量获取
  552. private function DataPre($order_ids, $bettype, $settype, $game_code, $match_id, $change_status, $is_manual = 0)
  553. {
  554. if ($bettype == 1) {
  555. $moneytable = 'money_buy_simplex';
  556. //$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();
  557. $tmpwhere = [['game_code', '=', $game_code], ['match_id', '=', $match_id], ['is_manual', '=', $is_manual], ['roll_ratify', '>=', 0]];
  558. $ret = DB::table('money_buy_simplex')->where($tmpwhere)->whereIn('order_id', $order_ids)->where('status', 1)->get();
  559. } else {
  560. $moneytable = 'money_buy_str';
  561. $ret = DB::table('money_buy_str')->whereIn('order_id', $order_ids)->where('status', 1)->get();
  562. }
  563. if (empty($ret) || count($ret) <= 0) {
  564. return false;
  565. }
  566. foreach ($ret as $val) {
  567. $this->PDatas['Money_buy_Orders_Array'][$val->order_id] = $val;
  568. $orderarr[] = $val->order_id;
  569. $userindentys[] = $val->account_identity;
  570. }
  571. $userindentys = array_unique($userindentys);
  572. if ($bettype == 1) {
  573. $ret = DB::table('money_buy_match')->where(['game_code' => $game_code, 'match_id' => $match_id, 'bet_type' => 1])->whereIn('order_id', $orderarr)->get();
  574. } else {
  575. $ret = DB::table('money_buy_match')->where(['game_code' => $game_code, 'bet_type' => 2])->whereIn('order_id', $orderarr)->get();
  576. }
  577. foreach ($ret as $val) {
  578. $this->PDatas['Money_buy_Match_array'][$val->order_id][] = $val;
  579. }
  580. $ret = DB::table('account_detailed')->whereIn('account_identity', $userindentys)->get();
  581. foreach ($ret as $val) {
  582. $this->PDatas['account_indentity_map_array'][$val->account_identity] = $val;
  583. }
  584. $ret = DB::table('account')->whereIn('identity', $userindentys)->get();
  585. foreach ($ret as $val) {
  586. $this->PDatas['account_map_array'][$val->identity] = $val;
  587. }
  588. if ($settype == 2) {
  589. //$ret = DB::table('settlement_middle_detail')->where(['game_code' => $game_code, 'match_id' => $match_id])->whereIn('order_id', $order_ids)->get();
  590. $ret = DB::table('settlement_middle_detail')->whereIn('order_id', $order_ids)->get();
  591. if ($ret) {
  592. foreach ($ret as $val) {
  593. $this->PDatas['settlement_middle_detail_array'][$val->bet_type][$val->order_id] = $val;
  594. }
  595. }
  596. $order_str_1 = array_map(function ($item) {
  597. return "'$item'";
  598. }, $orderarr);
  599. $order_str = implode(",", $order_str_1);
  600. if ($bettype == 1) {
  601. $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) ";
  602. } else {
  603. $sql_tmp = "update $moneytable set settle_status=1, gain_money=0 where order_id in ($order_str) and status=1 ";
  604. }
  605. $this->PushSqlToArray($sql_tmp);
  606. }
  607. //$ret = DB::table('money_prize')->whereIn('order_id', $order_ids)->where('money', '>', 0.01)->get();
  608. $ret = DB::table('money_prize')->whereIn('order_id', $order_ids)->get();
  609. if ($ret) {
  610. foreach ($ret as $val) {
  611. $this->PDatas['money_prize_map'][$val->order_id] = $val;
  612. }
  613. }
  614. $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();
  615. if ($ret) {
  616. foreach ($ret as $val) {
  617. $this->PDatas['money_detail_array'][$val->trade_id] = $val;
  618. }
  619. }
  620. $table = 'st_' . $game_code . '_result';
  621. $ret = DB::table($table)->where('match_id', $match_id)->first();
  622. if ($ret) {
  623. $this->PDatas['game_result'] = $ret;
  624. } else {
  625. throw new \Exception('result 结果为空 异常->' . $match_id, 117);
  626. }
  627. return true;
  628. }
  629. private function gethuishuilv()
  630. {
  631. $key = "HUISHUILV";
  632. $rv = $this->getRedis()->get($key);
  633. if ($rv) {
  634. return floatval($rv);
  635. }
  636. $rv = DB::table('setinfo')->where('identity', '1000')->first();
  637. if ($rv) {
  638. $pl = floatval(trim($rv->infocontent));
  639. $this->getRedis()->setex($key, 300, $pl);
  640. return $pl;
  641. }
  642. return 0;
  643. }
  644. private function sqltoRedisqueue()
  645. {
  646. $sqlQueue = $this->TaskSqlQueue;
  647. $redis = $this->getRedis();
  648. $i = 1;
  649. $maxStep = 10000;
  650. $sql = '';
  651. if (!$sqlQueue->isEmpty()) {
  652. $redis->pipeline();
  653. while ($i < $maxStep) {
  654. if (!$sqlQueue->isEmpty()) {
  655. $sql = $sqlQueue->dequeue();
  656. if (empty($sql)) {
  657. break;
  658. }
  659. $redis->lpush(self::SQLKEY, $sql);
  660. }
  661. $i++;
  662. }
  663. $redis->exec();
  664. }
  665. unset($sqlQueue, $sql, $i, $maxStep, $ret);
  666. return;
  667. }
  668. public function OnRequest($request, $response)
  669. {
  670. }
  671. public function onTask($serv, $task)
  672. {
  673. }
  674. public function onFinish($serv, int $task_id, $data)
  675. {
  676. }
  677. //得到返水的比例
  678. public function getFanshui()
  679. {
  680. $ret = (new SetinfoModel())->getInfo(1001);
  681. if (is_array($ret)) {
  682. $val = floatval($ret['infocontent']);
  683. $this->fanshui = $val;
  684. return $val;
  685. }
  686. }
  687. public function start()
  688. {
  689. $this->httpserver->start();
  690. }
  691. }