SettelementLogic.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2019/7/15
  6. * Time: 16:52
  7. */
  8. namespace App\Logic;
  9. use App\Http\Response\Response;
  10. use App\Lib\Settlement\SettlementBase;
  11. use Illuminate\Database\Capsule\Manager as DB;
  12. use swoole;
  13. trait SettelementLogic
  14. {
  15. private $SettlementBase;
  16. private $PDatas = [];
  17. //全部结算 结束要改结算和比赛状态
  18. private function doSettelement($request, $response, $paras)
  19. {
  20. $this->PDatas = [];
  21. try {
  22. list($order_ids, $bettype, $settype, $game_code, $match_id, $change_status) = $this->requestpara($request, $response, $paras);
  23. if (empty($order_ids)) {
  24. $needs_matchs = DB::table('money_buy_match')->where(['game_code' => $game_code, 'match_id' => $match_id])->count();
  25. if ($needs_matchs <= 0) {
  26. $this->cgStatus($game_code, $match_id, $this->change_status);
  27. goto LABRETURN;
  28. return;
  29. } else {
  30. throw new \Exception('订单号不能为空!');
  31. }
  32. unset($needs_matchs);
  33. }
  34. if ($bettype == 1) {
  35. $chekArr = $this->Match_check($order_ids, $bettype);
  36. if (empty($chekArr) || count($chekArr) != 1) {
  37. throw new \Exception('不同场比赛不能同时结算!');
  38. }
  39. unset($chekArr);
  40. }
  41. //分页处理数据
  42. $PageOrder_ids_1 = array_chunk($order_ids, 500);
  43. unset($order_ids);
  44. //$PageOrder_ids[0] = $PageOrder_ids_1['0'];
  45. foreach ($PageOrder_ids_1 as $p_order_ids) {
  46. if (!$this->DataPre($p_order_ids, $bettype, $settype, $game_code, $match_id, 0)) {
  47. throw new \Exception('没找到订单信息');
  48. }
  49. $this->UndoSettlement();
  50. while (true) {
  51. if ($this->httpserver->taskWorkingNum->get() >= intval($this->config['sets']['task_worker_num'])) {
  52. //echo "Work coutn: " . $this->httpserver->taskWorkingNum->get() . "\n";
  53. \co::sleep(0.01);
  54. } else {
  55. $order_id = trim(array_shift($p_order_ids));
  56. if (empty($order_id)) {
  57. break;
  58. }
  59. $nowBuyDatasMainModel = $this->PDatas['Money_buy_Orders_Array'][$order_id];
  60. if ($nowBuyDatasMainModel->settle_status == 2 && $settype == 1) {
  61. continue;
  62. }
  63. $account_indent = $nowBuyDatasMainModel->account_identity;
  64. $batch_id = $nowBuyDatasMainModel->batch_id;
  65. $paras = [
  66. 'type' => 'Settelement',
  67. 'bet_type' => $bettype,
  68. 'set_type' => $settype,
  69. 'order_id' => $order_id,
  70. 'game_code' => $nowBuyDatasMainModel->game_code,
  71. 'match_id' => $match_id,
  72. 'simplexData' => $nowBuyDatasMainModel,
  73. 'matchDatas' => $this->PDatas['Money_buy_Match_array'][$batch_id],
  74. 'settelementMidDataID' => isset($this->PDatas['settlement_middle_detail_array'][$bettype][$order_id]) ? $this->PDatas['settlement_middle_detail_array'][$bettype][$order_id]->id : 0,
  75. 'account' => $this->PDatas['account_map_array'][$account_indent]->account,
  76. 'money_prize' => isset($this->PDatas['money_prize_map'][$order_id]) ? $this->PDatas['money_prize_map'][$order_id]->id : 0
  77. ];
  78. $this->onTask($this->httpserver, $paras);
  79. }
  80. }
  81. }
  82. $this->cgStatus($game_code, $match_id, $change_status);
  83. LABRETURN:
  84. $data = Response::generate('', 1, ['cost' => (microtime(true) - $request->server['request_time_float'])], 'succes ');
  85. $response->end($data);
  86. unset($bettype, $settype, $game_code, $match_id, $change_status, $PageOrder_ids_1, $p_order_id, $data);
  87. return;
  88. } catch (\Exception $e) {
  89. $this->PDatas = [];
  90. $data = Response::generate('', 10, '', $e->getMessage() . '--' . $e->getFile() . '--' . $e->getLine());
  91. $response->end($data);
  92. unset($data);
  93. return;
  94. }
  95. }
  96. private function Task_Settelement($serv, $task)
  97. {
  98. $order_id = $task['order_id'];
  99. $bet_type = $task['bet_type'];
  100. $table = ($bet_type == 1) ? "money_buy_simplex" : 'money_buy_str';
  101. try {
  102. if ($task['bet_type'] == 1) {
  103. $this->singOrder($serv, $task);
  104. } else {
  105. $this->ChuanOrder($serv, $task);
  106. }
  107. } catch (\Exception $e) {
  108. $e = str_replace(";", ";", $e->getMessage());
  109. $sql = "update $table set remark='$e' where order_id='$order_id' ";
  110. $this->PushSqlToArray($sql);
  111. unset($sql, $e);
  112. }
  113. unset($order_id, $bet_type, $table);
  114. }
  115. /**
  116. * 单式注单结算
  117. * @param mixed $order_id 注单ID
  118. */
  119. private function singOrder($serv, $task)
  120. {
  121. $matchDatas = $task['matchDatas'];
  122. $simplexData = $task['simplexData'];
  123. $account_identity = $simplexData->account_identity;
  124. $bet_type = $task['bet_type'];
  125. $order_id = $task['order_id'];
  126. $match_id = $task['match_id'];
  127. $game_code = $task['game_code'];
  128. $account = $task['account'];
  129. $money_prize = $task['money_prize'];
  130. foreach ($matchDatas as $val) {
  131. if (!in_array($val->result, [-1, 1, 2, 3, 4])) {
  132. throw new \Exception('match比赛结果异常或还没有输赢结果->' . $val->id);
  133. }
  134. }
  135. if (empty($this->SettlementBaseObj)) {
  136. $settlementBase = new \App\Lib\Settlement\SettlementBase();
  137. } else {
  138. $settlementBase = $this->SettlementBaseObj;
  139. }
  140. $returnMoney = 0;
  141. $oddsResult = [];
  142. foreach ($matchDatas as $k => $v) {
  143. if ($v->result == -1) {
  144. continue;
  145. }
  146. $oddsResult[0]['winOrLose'] = $v->result;
  147. $oddsResult[0]['odds'] = $v->odds;
  148. $getReturnMoney = $settlementBase->stringOdds($oddsResult);
  149. $returnMoney += $getReturnMoney['returnMoney'] * $v->bet_money;
  150. }
  151. $returnMoney = sprintf("%.2f", substr(sprintf("%.3f", $returnMoney), 0, -1));
  152. // 判断盈亏 1 赢 2 输 3 平
  153. $game_status = $returnMoney > $simplexData->money ? 1 : ($returnMoney == $simplexData->money ? 3 : 2);
  154. // 修改投注表状态及盈亏
  155. $this->PushSqlToArray(["update money_buy_simplex set settle_status=2, game_status=$game_status,gain_money=$returnMoney where order_id='$order_id' "]);
  156. $this->WriteOrAddSettlement($game_code, $match_id, $bet_type, $order_id, $account_identity, $returnMoney);
  157. $this->insertData(
  158. $order_id,
  159. $returnMoney,
  160. $account_identity,
  161. 1,
  162. $game_code,
  163. $simplexData->info_identity,
  164. $simplexData->money,
  165. $match_id,
  166. $account,
  167. $money_prize
  168. );
  169. unset($matchDatas, $simplexData, $account_identity, $bet_type, $order_id, $match_id, $game_code, $account, $money_prize, $returnMoney);
  170. }
  171. private function WriteOrAddSettlement($game_code, $match_id, $bet_type, $order_id, $account_ident, $money)
  172. {
  173. if (intval($money * 100) == 0) {
  174. return;
  175. }
  176. if ($this->set_type == 2 && isset($this->settlement_middle_detail_array[$bet_type][$order_id])) {
  177. $this->PushSqlToArray("update settlement_middle_detail set money=$money where game_code='$game_code' and bet_type=$bet_type and order_id='$order_id' ");
  178. } else {
  179. $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)");
  180. }
  181. return true;
  182. }
  183. private function ChuanOrder($serv, $task)
  184. {
  185. $matchDatas = $task['matchDatas'];
  186. $simplexData = $task['simplexData'];
  187. $account_identity = $simplexData->account_identity;
  188. $bet_type = $task['bet_type'];
  189. $order_id = $task['order_id'];
  190. $match_id = $task['match_id'];
  191. $game_code = $task['game_code'];
  192. $account = $task['account'];
  193. $money_prize = $task['money_prize'];
  194. foreach ($matchDatas as $val) {
  195. if (!in_array($val->result, [-1, 1, 2, 3, 4])) {
  196. throw new \Exception('match比赛结果异常或还没有输赢结果->' . $val->id);
  197. }
  198. }
  199. $batch_id = $simplexData->batch_id;
  200. $matchModels = $matchDatas;
  201. if (count($matchModels) <= 0) {
  202. throw new \Exception('match 数据异常-' . $batch_id);
  203. }
  204. if (empty($this->SettlementBaseObj)) {
  205. $settlementBase = new \App\Lib\Settlement\SettlementBase();
  206. } else {
  207. $settlementBase = $this->SettlementBaseObj;
  208. }
  209. $newTime = date('Y-m-d H:i:s');
  210. if ($simplexData->status == 1) {
  211. $in_array = [];
  212. foreach ($matchModels as $val) {
  213. if (!in_array($val->result, [-1, 1, 2, 3, 4])) {
  214. throw new \Exception('match 比赛结果异常或还没有输赢结果->' . $val->id);
  215. }
  216. if ($val->result == -1) {
  217. $this->PushSqlToArray("update money_buy_str set wait_match_num=0, prize_note=0, game_status=3, settle_status=2, gain_money=0,settlementTime='" . date('Y-m-d H:i:s') . "' where batch_id='$batch_id' ");
  218. return true;
  219. }
  220. $in_array[] = ['odds' => $val->odds, 'winOrLose' => $val->result];
  221. }
  222. $chuanNum = intval(substr($simplexData->str_type, 0, 1));
  223. $lasPeilv = $settlementBase->stringComputing([$in_array, $chuanNum]);
  224. $money = floatPointDigit($this->BuyDatasMainModel->money * $lasPeilv);
  225. unset($chuanNum, $lasPeilv);
  226. } else {
  227. $money = $simplexData->money;
  228. }
  229. $this->PushSqlToArray(" update money_buy_str set settle_status=2 , game_status=1 , settlementTime='$newTime' , gain_money=$money where order_id = '$order_id'");
  230. $this->WriteOrAddSettlement($this->game_code, $this->match_id, $this->orderType, $order_id, $this->BuyDatasMainModel->account_identity, $money);
  231. $this->insertData($order_id, $money, $this->BuyDatasMainModel->account_identity, 2, $val->game_code, $this->BuyDatasMainModel->info_identity, $this->BuyDatasMainModel->money, $this->match_id, $account, $money_prize);
  232. unset($matchDatas, $simplexData, $account_identity, $bet_type, $order_id, $match_id, $game_code, $account, $money_prize, $money);
  233. return true;
  234. }
  235. /**
  236. * 结算数据填入
  237. * @param mixed $order_id 注单ID
  238. * @param mixed $returnMoney 返现金额
  239. * @param mixed $account_identity 用户ID
  240. * @param mixed $type 1单式 2串式
  241. * @param mixed $game_name 游戏名(zq,lq)
  242. * @param mixed $buy_identity 游戏投注id
  243. * @param mixed $money 投注金额
  244. * @param mixed $match_id 赛场ID
  245. */
  246. private function insertData($order_id, $returnMoney, $account_identity, $type, $game_name, $buy_identity, $money, $match_id = 0, $account = '', $money_prizeID = 0)
  247. {
  248. // 添加流水记录
  249. $info_identity = UUID();
  250. $money_time = date('Y-m-d H:i:s', time());
  251. $trade_desc = $type == 1 ? '单式投注订单回款' : '串式投注订单回款';
  252. $reason = $type == 1 ? '单式投注订单回款' : '串式投注订单回款';
  253. $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(";
  254. $sql .= "'$info_identity','$order_id','$account','$account_identity',$returnMoney,'$money_time',1,0,4,'$trade_desc','$reason','系统',1)";
  255. $this->PushSqlToArray($sql);
  256. // 修改用余额
  257. $this->PushSqlToArray("update account_detailed set available_cash=available_cash+$money,cash=cash+$money where account_identity='$account_identity' ");
  258. // 新增用户中奖信息
  259. $content = $type == 1 ? '您的单式投注订单' . $order_id . '于' . $money_time . '成功回款' . $returnMoney . '该次投注流程结束,如有疑问请联系客服'
  260. : '您的串式投注订单' . $order_id . '于' . $money_time . '成功回款' . $returnMoney . '该次投注流程结束,如有疑问请联系客服';
  261. $sql = "insert into account_news(identity,account_identity,title,content,details,write_time,read_status,type) values ";
  262. $sql .= "('$info_identity','$account_identity','投注订单回款通知','$content','$content','$money_time',-1,1)";
  263. $this->PushSqlToArray($sql);
  264. // 新增中奖记录表
  265. $table = 'money_prize';
  266. if ($money_prizeID) {
  267. $this->PushSqlToArray("update $table set money=$money,prize_money=$returnMoney, get_money= $returnMoney - $money where id=$money_prizeID");
  268. } else {
  269. $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 ";
  270. $sql .= "('$info_identity','$order_id','$account_identity','$account','$game_name','$buy_identity',$money,'$money_time',1,$returnMoney,$returnMoney - $money)";
  271. $this->PushSqlToArray($sql);
  272. }
  273. unset($info_identity, $money_time, $trade_desc, $reason, $content, $sql);
  274. return true;
  275. }
  276. private function resetPDatas()
  277. {
  278. $bettype = $this->PDatas['bettype'];
  279. $settype = $this->PDatas['settype'];
  280. $game_code = $this->PDatas['game_code'];
  281. $match_id = $this->PDatas['match_id'];
  282. $this->PDatas = [];
  283. $this->PDatas['bettype'] = $bettype;
  284. $this->PDatas['settype'] = $settype;
  285. $this->PDatas['game_code'] = $game_code;
  286. $this->PDatas['match_id'] = $match_id;
  287. return;
  288. }
  289. //数据预批量获取
  290. private function DataPre($order_ids, $bettype, $settype, $game_code, $match_id, $change_status)
  291. {
  292. $this->resetPdatas();
  293. if ($bettype == 1) {
  294. $moneytable = 'money_buy_simplex';
  295. $ret = DB::table($moneytable)->where(['game_code' => $game_code, 'match_id' => $match_id])->whereIn('order_id', $order_ids)->get();
  296. } else {
  297. $moneytable = 'money_buy_str';
  298. $ret = DB::table($moneytable)->whereIn('order_id', $order_ids)->get();
  299. }
  300. if (empty($ret) || count($ret) <= 0) {
  301. return false;
  302. }
  303. foreach ($ret as $val) {
  304. $this->PDatas['Money_buy_Orders_Array'][$val->order_id] = $val;
  305. $orderarr[] = $val->order_id;
  306. $this->PDatas['Money_buy_Orders_batch_Array'][$val->batch_id][] = $val;
  307. $tmpbatchid[] = $val->batch_id;
  308. $userindentys[] = $val->account_identity;
  309. }
  310. $tmpbatchid = array_unique($tmpbatchid);
  311. $userindentys = array_unique($userindentys);
  312. if ($bettype == 1) {
  313. $ret = DB::table('money_buy_match')->where(['game_code' => $game_code, 'match_id' => $match_id])->whereIn('batch_id', $tmpbatchid)->get();
  314. } else {
  315. $ret = DB::table('money_buy_match')->where(['game_code' => $game_code])->whereIn('batch_id', $tmpbatchid)->get();
  316. }
  317. foreach ($ret as $val) {
  318. $this->PDatas['Money_buy_Match_array'][$val->batch_id][] = $val;
  319. }
  320. $ret = DB::table('account_detailed')->whereIn('account_identity', $userindentys)->get();
  321. foreach ($ret as $val) {
  322. $this->PDatas['account_indentity_map_array'][$val->account_identity] = $val;
  323. }
  324. $ret = DB::table('account')->whereIn('identity', $userindentys)->get();
  325. foreach ($ret as $val) {
  326. $this->PDatas['account_map_array'][$val->identity] = $val;
  327. }
  328. if ($settype == 2) {
  329. $ret = DB::table('settlement_middle_detail')->where(['game_code' => $game_code, 'match_id' => $match_id])->whereIn('order_id', $order_ids)->get();
  330. if ($ret) {
  331. foreach ($ret as $val) {
  332. $this->PDatas['settlement_middle_detail_array'][$val->bet_type][$val->order_id] = $val;
  333. }
  334. }
  335. $order_str_1 = array_map(function ($item) {
  336. return "'$item'";
  337. }, $orderarr);
  338. $order_str = implode(",", $order_str_1);
  339. if ($change_status) {
  340. if ($bettype == 1) {
  341. $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) ";
  342. } else {
  343. $sql_tmp = "update $moneytable set settle_status=1, gain_money=0 where order_id in ($order_str) ";
  344. }
  345. $this->PushSqlToArray($sql_tmp);
  346. unset($sql_tmp);
  347. }
  348. }
  349. $ret = DB::table('money_prize')->whereIn('order_id', $order_ids)->get();
  350. if ($ret) {
  351. foreach ($ret as $val) {
  352. $this->PDatas['money_prize_map'][$val->order_id] = $val;
  353. }
  354. }
  355. unset($ret, $tmpbatchid, $userindentys, $moneytable);
  356. return true;
  357. }
  358. private function requestpara($request, $response, $paras)
  359. {
  360. $idoarr = explode(",", $paras['order_ids']);
  361. $order_ids = array_map(function ($i) {
  362. return strval($i);
  363. }, $idoarr);
  364. $bettype = isset($paras['bettype']) ? $paras['bettype'] : 0;
  365. $settype = isset($paras['settype']) ? $paras['settype'] : 0;
  366. $game_code = isset($paras['game_code']) ? $paras['game_code'] : '';
  367. $match_id = isset($paras['match_id']) ? $paras['match_id'] : 0;
  368. $change_status = isset($paras['change_status']) ? $paras['change_status'] : 0;
  369. if (intval($match_id) <= 0) {
  370. throw new \Exception('赛事ID不能为空!');
  371. return;
  372. }
  373. if (empty($game_code)) {
  374. throw new \Exception('赛事类型不能为空!');
  375. }
  376. if (!in_array($bettype, [1, 2])) {
  377. throw new \Exception('订单类型参数错误!');
  378. }
  379. if (!in_array($settype, [1, 2])) {
  380. throw new \Exception('结算参数错误');
  381. }
  382. $this->PDatas['bettype'] = $bettype;
  383. $this->PDatas['settype'] = $settype;
  384. $this->PDatas['game_code'] = $game_code;
  385. $this->PDatas['match_id'] = $match_id;
  386. $ret = [$order_ids, $bettype, $settype, $game_code, $match_id, $change_status];
  387. unset($bettype, $idoarr, $settype, $game_code, $match_id, $change_status);
  388. return $ret;
  389. }
  390. private function getSBaseObj()
  391. {
  392. if (empty($this->SettlementBase)) {
  393. $this->SettlementBase = new SettlementBase();
  394. }
  395. return $this->SettlementBase;
  396. }
  397. //改状态
  398. private function cgStatus($game_code, $match_id, $change_status)
  399. {
  400. $table1 = "st_" . $game_code . "_result";
  401. $table2 = "st_" . $game_code . "_competition";
  402. if ($change_status) {
  403. $this->PushSqlToArray("update $table1 set status=3 where match_id=$match_id ");
  404. $this->PushSqlToArray("update $table2 set status=3 where match_id=$match_id ");
  405. }
  406. unset($table1, $table2);
  407. }
  408. //得么一个临时变量
  409. private function GP($name)
  410. {
  411. return $this->PDatas[$name];
  412. }
  413. private function Match_check($idsArray, $betType)
  414. {
  415. array_walk($idsArray, function (&$item, $key) {
  416. $item = "'" . $item . "'";
  417. });
  418. $idString = implode(",", $idsArray);
  419. $table = "money_buy_simplex";
  420. $sql = "select game_code,match_id from $table where status=1 and order_id in ($idString) group by game_code,match_id ";
  421. $ret = DB::select($sql);
  422. return $ret;
  423. }
  424. //重结算时,要先扣掉先前发的钱
  425. private function UndoSettlement()
  426. {
  427. if (empty($this->PDatas['settlement_middle_detail_array'])) {
  428. return true;
  429. }
  430. $bet_type = $this->GP('bettype');
  431. $nowArray = $this->PDatas['settlement_middle_detail_array'][$bet_type];
  432. foreach ($nowArray as $order => $val) {
  433. $money = abs(floatval($val->money));
  434. $acc = $val->account_identity;
  435. $sql1 = "update account_detailed set cash=cash-$money where identity='$acc' ";
  436. $sql2 = "update money_prize set prize_money=0,get_money=0 where order_id='$val->order_id' and account_identity='$acc'";
  437. $this->sqlArray[] = $sql1;
  438. $this->sqlArray[] = $sql2;
  439. $ids[] = $order;
  440. }
  441. $ids = array_map(function ($i) {
  442. return "'.$i.'";
  443. }, $ids);
  444. $ids_array = implode(",", $ids);
  445. $sql = "update settlement_middle_detail set money=0 where order_id in($ids_array) and bet_type=$bet_type ";
  446. $this->sqlArray[] = $sql;
  447. unset($nowArray, $bet_type, $ids, $ids_array, $sql);
  448. return true;
  449. }
  450. }