SettlementOrder.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. /**
  14. 按订单结算或重结算
  15. */
  16. class SettlementOrder extends SettlementBase
  17. {
  18. private $orderId = '' ; //订单ID
  19. private $orderType = 1 ; //订单类型 1单式 2串式;
  20. private $BuyDatasMainModel = [] ; //订单豪华版 单式一条 串式可能多条
  21. private $BuyDatas = [] ; //订单豪华版 单式一条 串式可能多条
  22. //返回数据
  23. public static function makeData($status=1,$message='success',$data=''){
  24. return [
  25. 'status' => $status ,
  26. 'message' =>$message,
  27. 'data' => $data ,
  28. ] ;
  29. }
  30. public function reSettlement($order_ids,$bettype=2){
  31. try{
  32. DB::beginTransaction ();
  33. foreach ($order_ids as $order_id){
  34. $this->BuyDatasMainModel = $this->orderTypeGet($order_id,$bettype);
  35. if (empty($this->BuyDatasMainModel)){ return self::makeData(2,'查无此订单数据'); }
  36. if ($this->orderType==1){
  37. $this->SingOrder($order_id);
  38. }else{
  39. $this->ChuanOrder($order_id);
  40. }
  41. }
  42. DB::commit();
  43. }catch (\Exception $e){
  44. DB::rollBack();
  45. return self::makeData(-2,$e->getMessage()); ;
  46. }
  47. return self::makeData();
  48. }
  49. public function orderTypeGet($order_id,$bettype){
  50. if ($bettype==1){
  51. $datas = DB::table('money_buy_simplex')->where('order_id',$order_id)->first();
  52. $this->orderType = 1 ;
  53. }else{
  54. $datas = DB::table('money_buy_str')->where('order_id',$order_id)->first();
  55. $this->orderType = 2 ;
  56. }
  57. if (!$datas){
  58. throw new \Exception('没有主订单信息');
  59. }
  60. return $datas ;
  61. }
  62. /**
  63. * 单式注单结算
  64. * @param mixed $order_id 注单ID
  65. */
  66. public function singOrder($order_id) {
  67. // 查询订单下所有的单式注单
  68. $simplexData = DB :: table('money_buy_simplex')
  69. -> select('batch_id', 'account_identity', 'order_id', 'money', 'game_code', 'info_identity')
  70. -> where(['order_id' => $order_id])
  71. -> first();
  72. // 查询单式注单下的所有玩法
  73. $matchData = DB :: table('money_buy_match') -> select('odds', 'result', 'batch_id') -> where(['bet_type' => 1, 'batch_id' => $simplexData -> batch_id]);
  74. $matchData = $matchData -> where(function($query) {
  75. $query = $query -> where(['result' => 1])
  76. -> orWhere(['result' => 2])
  77. -> orWhere(['result' => 3])
  78. -> orWhere(['result' => 4]);
  79. });
  80. $matchData = $matchData -> get() -> toArray();
  81. // 计算总回款
  82. $settlementBase = new \App\Lib\Settlement\SettlementBase;
  83. $returnMoney = 0;
  84. $earnMoney = 0;
  85. foreach ($matchData as $k => $v) {
  86. $oddsResult[0]['winOrLose'] = $v -> result;
  87. $oddsResult[0]['odds'] = $v -> odds;
  88. $getReturnMoney = $settlementBase -> winOddsCalculation($oddsResult);
  89. $returnMoney += $getReturnMoney['returnMoney'];
  90. $earnMoney += $getReturnMoney['earnMoney'];
  91. }
  92. // 判断盈亏 1 赢 2 输 3 平
  93. $game_status = $returnMoney > $simplexData -> money ? 1 : ($returnMoney == $simplexData -> money ? 3 : 2);
  94. // 修改投注表状态及盈亏
  95. DB :: table('money_buy_simplex')
  96. -> where(['order_id' => $order_id])
  97. -> update(['settle_status' => 2, 'game_status' => $game_status, 'gain_money' => $earnMoney]);
  98. $this -> insertData(
  99. $order_id,
  100. $returnMoney,
  101. $simplexData -> account_identity,
  102. 1,
  103. $simplexData -> game_code,
  104. $simplexData -> info_identity,
  105. $simplexData -> money
  106. );
  107. }
  108. /**
  109. * 结算数据填入
  110. * @param mixed $order_id 注单ID
  111. * @param mixed $returnMoney 返现金额
  112. * @param mixed $account_identity 用户ID
  113. * @param mixed $type 1单式 2串式
  114. * @param mixed $game_name 游戏名(zq,lq)
  115. * @param mixed $buy_identity 游戏投注id
  116. * @param mixed $money 投注金额
  117. */
  118. public function insertData($order_id, $returnMoney, $account_identity, $type, $game_name, $buy_identity, $money) {
  119. // 查询用户当前剩余金额
  120. $accountInfo = DB :: table('account_detailed')
  121. -> join('account', 'account_detailed.account_identity', 'account.identity')
  122. -> select(['available_cash', 'cash', 'account', 'account_identity'])
  123. -> where(['account_identity' => $account_identity])
  124. -> first();
  125. // 计算用户回账后余额
  126. $available_cash = $accountInfo -> available_cash + $returnMoney;
  127. $cash = $accountInfo -> cash + $returnMoney;
  128. // 添加流水记录
  129. $info_identity = UUID();
  130. $money_time = date('Y-m-d H:i:s', time());
  131. $trade_desc = $type == 1 ? '单式投注订单回款' : '串式投注订单回款';
  132. $reason = $type == 1 ? '单式投注订单回款' : '串式投注订单回款';
  133. DB :: table('money_details') -> insert([
  134. 'info_identity' => $info_identity,
  135. 'trade_id' => $order_id,
  136. 'account_name' => $accountInfo -> account,
  137. 'account_identity' => $accountInfo -> account_identity,
  138. 'money' => $returnMoney,
  139. 'money_time' => $money_time,
  140. 'money_type' => 1,
  141. 'money_cash' => $available_cash,
  142. 'trade_type' => 1,
  143. 'trade_desc' => $trade_desc,
  144. 'reason' => $reason,
  145. 'sysetem_user' => '系统',
  146. 'status' => '1',
  147. ]);
  148. // 修改用余额
  149. DB:: table('account_detailed')
  150. -> where(['account_identity' => $account_identity])
  151. -> update(['available_cash' => $available_cash, 'cash' => $cash]);
  152. // 新增用户中奖信息
  153. $content = $type == 1 ? '您的单式投注订单' . $order_id . '于' . $money_time . '成功回款' . $returnMoney . '该次投注流程结束,如有疑问请联系客服'
  154. : '您的串式投注订单' . $order_id . '于' . $money_time . '成功回款' . $returnMoney . '该次投注流程结束,如有疑问请联系客服';
  155. DB:: table('account_news') -> insert([
  156. 'identity' => $info_identity,
  157. 'account_identity' => $account_identity,
  158. 'title' => '投注订单回款通知',
  159. 'content' => $content,
  160. 'details' => $content,
  161. 'write_time' => $money_time,
  162. 'read_status' => -1,
  163. 'type' => 1,
  164. ]);
  165. // 新增中奖记录表
  166. DB:: table('money_prize') -> insert([
  167. 'info_identity' => $info_identity,
  168. 'order_id' => $order_id,
  169. 'account_identity' => $account_identity,
  170. 'account_name' => $accountInfo -> account,
  171. 'game_name' => $game_name,
  172. 'buy_identity' => $buy_identity,
  173. 'money' => $money,
  174. 'money_time' => $money_time,
  175. 'status' => 1,
  176. 'prize_money' => $returnMoney,
  177. 'get_money' => $returnMoney - $money,
  178. ]);
  179. }
  180. //单个串式订单的处理
  181. public function ChuanOrder($order_id){
  182. $batch_id = $this->BuyDatasMainModel->batch_id ;
  183. $matchModels = DB::table('money_buy_match')->where(['batch_id'=>$batch_id])->get();
  184. if (empty($matchModels)) { throw new \Exception('match 数据异常');}
  185. $in_array = [] ;
  186. foreach ($matchModels as $val){
  187. if (!in_array($val->result,[-1,1,2,3,4])){ throw new \Exception('match 比赛结果异常->'.$val->id); }
  188. if ($val->result == -1){
  189. $this->BuyDatasMainModel->wait_match_num = 0 ;
  190. $this->BuyDatasMainModel->prize_note = 0 ;
  191. $this->BuyDatasMainModel->settle_status = 2 ;
  192. $this->BuyDatasMainModel->gain_money = 0 ;
  193. $ret = $this->BuyDatasMainModel->save();
  194. if (!$ret){ throw new \Exception('更新数据出错1!'); }
  195. $ret = DB::update('update money_buy_str set settle_status=2,game_status=3,gain_money=0 where batch_id = ?', [$batch_id]);
  196. if(!($ret || $ret===0)){ throw new \Exception('更新数据出错2!'); }
  197. return true ;
  198. }
  199. $in_array[] = ['odds'=>$val->odds,'winOrLose'=>$val->result] ;
  200. }
  201. $chuanNum = intval(substr($this->BuyDatasMainModel->str_type,0,1));
  202. $lasPeilv = $this->stringComputing([$in_array,$chuanNum]);
  203. $money = sprintf('%.2f',$this->BuyDatasMainModel->money * ( $lasPeilv - 1) ) ;
  204. $this->insertData($order_id,$money,$this->BuyDatasMainModel->account_identity,2,$val->game_code,$this->BuyDatasMainModel->info_identity,$money);
  205. return true ;
  206. /*
  207. $ret = DB::update('update money_buy_str set settle_status=2 , game_status=1 , gain_money=? where order_id = ?', [$money,$order_id]);
  208. if(!($ret || $ret===0)){ throw new \Exception('更新数据出错3!'); }
  209. $account_identity = $this->BuyDatasMainModel->account_identity ;
  210. $ret1 = DB::update("update account_detailed set available_cash=available_cash+$money , cash=cash+$money where account_identity='$account_identity' ") ;
  211. $allbuyModel = $this->BuyDatasMainModel ;
  212. $dtime = date("Y-m-d H:i:s");
  213. $uuid = self::UUID();
  214. $userInfoArray = $this->getUserInfo($allbuyModel->account_name);
  215. $cashmoney = $userInfoArray['detail']->cash;
  216. $SQL = "insert into money_details(info_identity,trade_id,account_name,account_identity,money,money_time,money_type,trade_desc,status,money_cash,trade_type) values";
  217. $SQL .= "('$uuid','$allbuyModel->order_id','$allbuyModel->account_name','$allbuyModel->account_identity','$money','$dtime',1,'中奖收入','1','$cashmoney',4)" ;
  218. $ret2 = DB::insert($SQL);
  219. if ($ret1 && $ret2){
  220. return true;
  221. }
  222. throw new \Exception('更新数据出错4!');
  223. */
  224. }
  225. }