SettelementLogic.php 19 KB

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