SettlementOrder.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2019/4/25
  6. * Time: 14:10
  7. */
  8. namespace App\Lib\Settlement;
  9. set_time_limit(600);
  10. ini_set('memory_limit', '256M');
  11. use Illuminate\Support\Facades\DB;
  12. use App\Lib\Settlement\SettlementBase;
  13. use App\Models\SettlementMiddleDetail as SettlementMiddleDetailModel;
  14. /**
  15. * 按订单结算或重结算
  16. */
  17. class SettlementOrder extends SettlementBase
  18. {
  19. private $orderType = 1; //订单类型 1单式 2串式;
  20. private $BuyDatasMainModel = []; //订单豪华版 单式一条 串式可能多条
  21. private $BuyDatas = []; //订单豪华版 单式一条 串式可能多条
  22. private $match_id = 0;
  23. private $game_code = '';
  24. private $SN = 0; //结算批次号
  25. private $set_type = 0;
  26. private $change_status = 1;
  27. private $SettlementBaseObj = '';
  28. private $sqlArray = []; //最终生成的执行sql
  29. private $Money_buy_Orders_Array = []; //订单主表映射
  30. private $Money_buy_Orders_batch_Array = []; //订单按批次号表映射
  31. private $Money_buy_Match_array = []; //单式订单表的映射
  32. private $account_map_array = []; //用户账号映射
  33. private $account_indentity_map_array = []; //用户账号indenty映射
  34. private $settlement_middle_detail_array = []; //已结算过的数据映射,按订单号及[单串式]
  35. private $money_prize_map = []; //中奖记录数据
  36. //返回数据
  37. public static function makeData($status = 1, $message = 'success', $data = '')
  38. {
  39. return [
  40. 'status' => $status,
  41. 'message' => $message,
  42. 'data' => $data,
  43. ];
  44. }
  45. ///$order_ids 某场比塞全部订单数组, $bettype=2 订单类型: 1单式 2串式(默认), $settype = 1 结算次数:1首次(默认) 2非首次或重结算
  46. /// $change_status 对单个订单结算时,是否改赛事状态和比赛结果状态 1要改 0不改
  47. public function reSettlement($order_ids, $bettype = 2, $settype = 1, $game_code = 0, $match_id = 0, $change_status = 1)
  48. {
  49. $order_ids = array_unique($order_ids);
  50. $this->change_status = $change_status;
  51. if (intval($match_id) <= 0) {
  52. return self::makeData(8, 'matchid不能为空或0!');
  53. }
  54. if (empty($game_code)) {
  55. return self::makeData(8, '赛事类型不能为空!');
  56. }
  57. if (!in_array($bettype, [1, 2])) {
  58. return self::makeData(6, '订单类型参数错误!');
  59. }
  60. $this->orderType = $bettype;
  61. if (!in_array($settype, [1, 2])) {
  62. return self::makeData(6, '结算参数错误');
  63. }
  64. $this->set_type = $settype;
  65. if (empty($order_ids)) {
  66. $needs_matchs = DB::table('money_buy_match')->where(['game_code' => $game_code, 'match_id' => $match_id])->count();
  67. if ($needs_matchs <= 0) {
  68. $this->cgStatus($game_code, $match_id, $this->change_status);
  69. return self::makeData();
  70. } else {
  71. return self::makeData(5, '订单号不能为空');
  72. }
  73. unset($needs_matchs);
  74. }
  75. if ($bettype == 1) {
  76. $chekArr = $this->Match_check($order_ids, $bettype);
  77. if (empty($chekArr) || count($chekArr) != 1) {
  78. return self::makeData(10, '不同场比赛不能同时结算!' . print_r($order_ids, true));
  79. }
  80. }
  81. $this->match_id = $match_id;
  82. $this->game_code = $game_code;
  83. if (!$this->DataPre($order_ids, $bettype, $settype, $game_code, $match_id, $change_status)) {
  84. return self::makeData(-1, 'false', '没找到订单信息');
  85. }
  86. try {
  87. DB::beginTransaction();
  88. $this->UndoSettlement();
  89. foreach ($order_ids as $order_id) {
  90. $this->BuyDatasMainModel = $this->Money_buy_Orders_Array[$order_id];
  91. if ($this->BuyDatasMainModel->settle_status == 2 && $settype == 1) {
  92. continue;
  93. }
  94. if ($bettype == 1) {
  95. $this->SingOrder($order_id);
  96. } else {
  97. $this->ChuanOrder($order_id);
  98. }
  99. }
  100. $this->cgStatus($game_code, $match_id, $this->change_status);
  101. $this->RunSql();
  102. DB::commit();
  103. } catch (\Exception $e) {
  104. DB::rollBack();
  105. return self::makeData(-2, $e->getMessage() . ' Line:' . $e->getLine());
  106. }
  107. return self::makeData();
  108. }
  109. public function orderTypeGet($order_id, $bettype)
  110. {
  111. if ($bettype == 1) {
  112. $datas = DB::table('money_buy_simplex')->where('order_id', $order_id)->first();
  113. $this->orderType = 1;
  114. } else {
  115. $datas = DB::table('money_buy_str')->where('order_id', $order_id)->first();
  116. $this->orderType = 2;
  117. }
  118. if (!$datas) {
  119. throw new \Exception('没有主订单信息');
  120. }
  121. return $datas;
  122. }
  123. /**
  124. * 单式注单结算
  125. * @param mixed $order_id 注单ID
  126. */
  127. private function singOrder($order_id)
  128. {
  129. // 查询订单下所有的单式注单
  130. $simplexData = $this->BuyDatasMainModel;
  131. $matchData = $this->Money_buy_Match_array[$simplexData->batch_id];
  132. if (count($matchData) <= 0) {
  133. throw new \Exception("没有数据的错误!");
  134. }
  135. foreach ($matchData as $val) {
  136. if (!in_array($val->result, [-1, 1, 2, 3, 4])) {
  137. throw new \Exception('match 比赛结果异常或还没有输赢结果->' . $val->id);
  138. }
  139. }
  140. // 计算总回款
  141. if (empty($this->SettlementBaseObj)) {
  142. $settlementBase = new \App\Lib\Settlement\SettlementBase();
  143. } else {
  144. $settlementBase = $this->SettlementBaseObj;
  145. }
  146. $returnMoney = 0;
  147. $oddsResult = [];
  148. foreach ($matchData as $k => $v) {
  149. if ($v->result == -1) {
  150. continue;
  151. }
  152. $oddsResult[0]['winOrLose'] = $v->result;
  153. $oddsResult[0]['odds'] = $v->odds;
  154. $getReturnMoney = $settlementBase->stringOdds($oddsResult);
  155. $returnMoney += $getReturnMoney['returnMoney'] * $v->bet_money;
  156. }
  157. $returnMoney = sprintf("%.2f", substr(sprintf("%.3f", $returnMoney), 0, -1));
  158. // 判断盈亏 1 赢 2 输 3 平
  159. $game_status = $returnMoney > $simplexData->money ? 1 : ($returnMoney == $simplexData->money ? 3 : 2);
  160. // 修改投注表状态及盈亏
  161. $this->sqlArray[] = "update money_buy_simplex set settle_status=2, game_status=$game_status,gain_money=$returnMoney where order_id='$order_id' ";
  162. $this->WriteOrAddSettlement($this->game_code, $this->match_id, $this->orderType, $order_id, $this->BuyDatasMainModel->account_identity, $returnMoney);
  163. $this->insertData(
  164. $order_id,
  165. $returnMoney,
  166. $simplexData->account_identity,
  167. 1,
  168. $simplexData->game_code,
  169. $simplexData->info_identity,
  170. $simplexData->money,
  171. $this->match_id
  172. );
  173. }
  174. private function ChuanOrder($order_id)
  175. {
  176. $batch_id = $this->BuyDatasMainModel->batch_id;
  177. $matchModels = $this->Money_buy_Match_array[$batch_id];
  178. if (count($matchModels) <= 0) {
  179. throw new \Exception('match 数据异常');
  180. }
  181. $newTime = date('Y-m-d H:i:s');
  182. if ($this->BuyDatasMainModel->status == 1) {
  183. $in_array = [];
  184. foreach ($matchModels as $val) {
  185. if (!in_array($val->result, [-1, 1, 2, 3, 4])) {
  186. throw new \Exception('match 比赛结果异常或还没有输赢结果->' . $val->id);
  187. }
  188. if ($val->result == -1) {
  189. $this->sqlArray[] = "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' ";
  190. return true;
  191. }
  192. $in_array[] = ['odds' => $val->odds, 'winOrLose' => $val->result];
  193. }
  194. $chuanNum = intval(substr($this->BuyDatasMainModel->str_type, 0, 1));
  195. $lasPeilv = $this->stringComputing([$in_array, $chuanNum]);
  196. $money = floatPointDigit($this->BuyDatasMainModel->money * $lasPeilv);
  197. } else {
  198. $money = $this->BuyDatasMainModel->money;
  199. }
  200. $this->sqlArray[] = " update money_buy_str set settle_status=2 , game_status=1 , settlementtime='$newTime' , gain_money=$money where order_id = '$order_id'";
  201. $this->WriteOrAddSettlement($this->game_code, $this->match_id, $this->orderType, $order_id, $this->BuyDatasMainModel->account_identity, $money);
  202. $this->insertData($order_id, $money, $this->BuyDatasMainModel->account_identity, 2, $val->game_code, $this->BuyDatasMainModel->info_identity, $this->BuyDatasMainModel->money, $this->match_id);
  203. return true;
  204. }
  205. //重结算时,要先扣掉先前发的钱
  206. private function UndoSettlement()
  207. {
  208. if (empty($this->settlement_middle_detail_array)) {
  209. return true;
  210. }
  211. $nowArray = $this->settlement_middle_detail_array[$this->orderType];
  212. foreach ($nowArray as $order => $val) {
  213. $money = abs(floatval($val->money));
  214. $acc = $val->account_identity;
  215. $sql = "update account_detailed set cash=cash-$money where identity='$acc' ";
  216. $this->sqlArray[] = $sql;
  217. $ids[] = $order;
  218. }
  219. $ids = array_map(function($i){return "'.$i.'";},$ids);
  220. $ids_array = implode(",", $ids);
  221. $sql = "update settlement_middle_detail set money=0 where order_id in($ids_array) and bet_type=$this->orderType ";
  222. $this->sqlArray[] = $sql;
  223. return true;
  224. }
  225. //数据预批量获取
  226. private function DataPre($order_ids, $bettype = 2, $settype = 1, $game_code = 0, $match_id = 0, $change_status = 1)
  227. {
  228. if ($bettype == 1) {
  229. $moneytable = 'money_buy_simplex';
  230. $ret = DB::table('money_buy_simplex')->where(['game_code' => $game_code, 'match_id' => $match_id])->whereIn('order_id', $order_ids)->get();
  231. } else {
  232. $moneytable = 'money_buy_str';
  233. $ret = DB::table('money_buy_str')->whereIn('order_id', $order_ids)->get();
  234. }
  235. if (empty($ret)) {
  236. return false;
  237. }
  238. $tmpbatchid = [];
  239. $userindentys = [];
  240. $orderarr = [];
  241. foreach ($ret as $val) {
  242. $this->Money_buy_Orders_Array[$val->order_id] = $val;
  243. $orderarr[] = $val->order_id;
  244. $this->Money_buy_Orders_batch_Array[$val->batch_id][] = $val;
  245. $tmpbatchid[] = $val->batch_id;
  246. $userindentys[] = $val->account_identity;
  247. }
  248. $tmpbatchid = array_unique($tmpbatchid);
  249. $userindentys = array_unique($userindentys);
  250. if ($bettype == 1) {
  251. $ret = DB::table('money_buy_match')->where(['game_code' => $game_code, 'match_id' => $match_id])->whereIn('batch_id', $tmpbatchid)->get();
  252. } else {
  253. $ret = DB::table('money_buy_match')->where(['game_code' => $game_code])->whereIn('batch_id', $tmpbatchid)->get();
  254. }
  255. foreach ($ret as $val) {
  256. $this->Money_buy_Match_array[$val->batch_id][] = $val;
  257. }
  258. $ret = DB::table('account_detailed')->whereIn('account_identity', $userindentys)->get();
  259. foreach ($ret as $val) {
  260. $this->account_indentity_map_array[$val->account_identity] = $val;
  261. }
  262. $ret = DB::table('account')->whereIn('identity', $userindentys)->get();
  263. foreach ($ret as $val) {
  264. $this->account_map_array[$val->identity] = $val;
  265. }
  266. if ($settype == 2) {
  267. $ret = DB::table('settlement_middle_detail')->where(['game_code' => $game_code, 'match_id' => $match_id])->whereIn('order_id', $order_ids)->get();
  268. if ($ret) {
  269. foreach ($ret as $val) {
  270. $this->settlement_middle_detail_array[$val->bet_type][$val->order_id] = $val;
  271. }
  272. }
  273. $maparr = array_map(function ($i) {
  274. return "'" . $i . "'";
  275. }, $orderarr);
  276. $order_str = implode(",", $maparr);
  277. if ($change_status) {
  278. if ($bettype == 1) {
  279. $this->sqlArray[] = "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) ";
  280. } else {
  281. $this->sqlArray[] = "update $moneytable set settle_status=1, gain_money=0 where order_id in ($order_str) ";
  282. }
  283. }
  284. }
  285. $ret = DB::table('money_prize')->whereIn('order_id', $order_ids)->get();
  286. if ($ret) {
  287. foreach ($ret as $val) {
  288. $this->money_prize_map[$val->order_id] = $val;
  289. }
  290. }
  291. return true;
  292. }
  293. private function WriteOrAddSettlement($game_code, $match_id, $bet_type, $order_id, $account_ident, $money)
  294. {
  295. if (intval($money * 100) == 0) {
  296. return;
  297. }
  298. if ($this->set_type == 2 && isset($this->settlement_middle_detail_array[$bet_type][$order_id])) {
  299. $this->sqlArray[] = "update settlement_middle_detail set money=$money where game_code='$game_code' and bet_type=$bet_type and order_id='$order_id' ";
  300. } else {
  301. $this->sqlArray[] = "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)";
  302. }
  303. return true;
  304. }
  305. /**
  306. * 结算数据填入
  307. * @param mixed $order_id 注单ID
  308. * @param mixed $returnMoney 返现金额
  309. * @param mixed $account_identity 用户ID
  310. * @param mixed $type 1单式 2串式
  311. * @param mixed $game_name 游戏名(zq,lq)
  312. * @param mixed $buy_identity 游戏投注id
  313. * @param mixed $money 投注金额
  314. * @param mixed $match_id 赛场ID
  315. */
  316. private function insertData($order_id, $returnMoney, $account_identity, $type, $game_name, $buy_identity, $money, $match_id = 0)
  317. {
  318. // 查询用户当前剩余金额
  319. $accountInfo = $this->account_indentity_map_array[$account_identity];
  320. // 计算用户回账后余额
  321. $this->account_indentity_map_array[$account_identity]->available_cash = $available_cash = $accountInfo->available_cash + $returnMoney;
  322. $this->account_indentity_map_array[$account_identity]->cash = $cash = $accountInfo->cash + $returnMoney;
  323. // 添加流水记录
  324. $info_identity = UUID();
  325. $money_time = date('Y-m-d H:i:s', time());
  326. $trade_desc = $type == 1 ? '单式投注订单回款' : '串式投注订单回款';
  327. $reason = $type == 1 ? '单式投注订单回款' : '串式投注订单回款';
  328. $accountInfobase = $this->account_map_array[$account_identity];
  329. $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(";
  330. $sql .= "'$info_identity','$order_id','$accountInfobase->account','$accountInfobase->identity',$returnMoney,'$money_time',1,$available_cash,4,'$trade_desc','$reason','系统',1)";
  331. $this->sqlArray[] = $sql;
  332. // 修改用余额
  333. $this->sqlArray[] = "update account_detailed set available_cash=$available_cash,cash=$cash where account_identity='$account_identity' ";
  334. // 新增用户中奖信息
  335. $content = $type == 1 ? '您的单式投注订单' . $order_id . '于' . $money_time . '成功回款' . $returnMoney . '该次投注流程结束,如有疑问请联系客服'
  336. : '您的串式投注订单' . $order_id . '于' . $money_time . '成功回款' . $returnMoney . '该次投注流程结束,如有疑问请联系客服';
  337. $sql = "insert into account_news(identity,account_identity,title,content,details,write_time,read_status,type) values ";
  338. $sql .= "('$info_identity','$account_identity','投注订单回款通知','$content','$content','$money_time',-1,1)";
  339. $this->sqlArray[] = $sql;
  340. // 新增中奖记录表
  341. //$old = $this->money_prize_map[$order_id];
  342. $table = 'money_prize';
  343. if (isset($this->money_prize_map[$order_id])) {
  344. $this->sqlArray[] = "update $table set money=$money,prize_money=$returnMoney, get_money= $returnMoney - $money where order_id='$order_id'";
  345. } else {
  346. $tmp_account = $this->account_map_array[$account_identity]->account ;
  347. $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 ";
  348. $sql .= "('$info_identity','$order_id','$account_identity','$tmp_account','$game_name','$buy_identity',$money,'$money_time',1,$returnMoney,$returnMoney - $money)";
  349. $this->sqlArray[] = $sql;
  350. }
  351. return true;
  352. }
  353. //改状态
  354. private function cgStatus($game_code, $match_id, $change_status)
  355. {
  356. $table1 = "st_" . $game_code . "_result";
  357. $table2 = "st_" . $game_code . "_competition";
  358. if ($change_status) {
  359. $this->sqlArray[] = "update $table1 set status=3 where match_id=$match_id ";
  360. $this->sqlArray[] = "update $table2 set status=3 where match_id=$match_id ";
  361. }
  362. }
  363. //校验是否同一场比赛的订单处理
  364. private function Match_check($idsArray, $betType)
  365. {
  366. array_walk($idsArray, function (&$item, $key) {
  367. $item = "'" . $item . "'";
  368. });
  369. $idString = implode(",", $idsArray);
  370. $table = "money_buy_simplex";
  371. $sql = "select game_code,match_id from $table where status=1 and order_id in ($idString) group by game_code,match_id ";
  372. $ret = DB::select($sql);
  373. return $ret;
  374. }
  375. private function RunSql()
  376. {
  377. $len = count($this->sqlArray);
  378. $step = 2000;
  379. $j = 1;
  380. $pdo = DB::getPdo();
  381. $tmp = [];
  382. for ($i = 0; $i < $len; $i++) {
  383. if ($j > $step || $i == ($len - 1)) {
  384. if (empty($tmp)) {
  385. return;
  386. }
  387. $sqlstr = implode(";", $tmp);
  388. $ret = $pdo->exec($sqlstr);
  389. if (!$ret) {
  390. throw new \Exception("运行sql发生错误!");
  391. }
  392. $tmp = [];
  393. $j = 1;
  394. } else {
  395. $tmp[] = $this->sqlArray[$i];
  396. $j++;
  397. }
  398. }
  399. return true;
  400. }
  401. }