SettlementOrder.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. if (empty($order_ids)){ return self::makeData(5,'订单号不能为空'); }
  32. try{
  33. DB::beginTransaction ();
  34. foreach ($order_ids as $order_id){
  35. $this->BuyDatasMainModel = $this->orderTypeGet($order_id,$bettype);
  36. if ( $this->BuyDatasMainModel->settle_status==2 ){ continue ; }
  37. if ($this->orderType==1){
  38. $this->SingOrder($order_id);
  39. }else{
  40. $this->ChuanOrder($order_id);
  41. }
  42. }
  43. DB::commit();
  44. }catch (\Exception $e){
  45. DB::rollBack();
  46. return self::makeData(-2,$e->getMessage()); ;
  47. }
  48. return self::makeData();
  49. }
  50. public function orderTypeGet($order_id,$bettype){
  51. if ($bettype==1){
  52. $datas = DB::table('money_buy_simplex')->where('order_id',$order_id)->first();
  53. $this->orderType = 1 ;
  54. }else{
  55. $datas = DB::table('money_buy_str')->where('order_id',$order_id)->first();
  56. $this->orderType = 2 ;
  57. }
  58. if (!$datas){
  59. throw new \Exception('没有主订单信息');
  60. }
  61. return $datas ;
  62. }
  63. /**
  64. * 单式注单结算
  65. * @param mixed $order_id 注单ID
  66. */
  67. public function singOrder($order_id) {
  68. // 查询订单下所有的单式注单
  69. $simplexData = DB :: table('money_buy_simplex')
  70. -> select('batch_id', 'account_identity', 'order_id', 'money', 'game_code', 'info_identity','match_id')
  71. -> where(['order_id' => $order_id])
  72. -> first();
  73. // 查询单式注单下的所有玩法
  74. $matchData = DB :: table('money_buy_match') -> select('odds', 'result', 'batch_id', 'bet_money') -> where(['bet_type' => 1, 'batch_id' => $simplexData -> batch_id, 'match_id' => $simplexData->match_id]);
  75. $matchData = $matchData -> where(function($query) {
  76. $query = $query -> where(['result' => 1])
  77. -> orWhere(['result' => 2])
  78. -> orWhere(['result' => 3])
  79. -> orWhere(['result' => 4]);
  80. });
  81. $matchData = $matchData -> get() -> toArray();
  82. // 计算总回款
  83. $settlementBase = new \App\Lib\Settlement\SettlementBase;
  84. $returnMoney = 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'] + 1) * $v -> bet_money;
  90. }
  91. $returnMoney = sprintf("%.2f",substr(sprintf("%.3f", $returnMoney), 0, -1));
  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' => $returnMoney]);
  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,'bet_type' => 2])->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. /*
  190. $ret = DB::table('money_buy_str')->where('id',$this->BuyDatasMainModel->id)->update(['wait_match_num'=>0,'prize_note'=>0,'settle_status'=>2,'gain_money'=>0]);
  191. if (!$ret){ throw new \Exception('更新数据出错1!'); }
  192. $ret = DB::update('update money_buy_str set settle_status=2,game_status=3,gain_money=0 where batch_id = ?', [$batch_id]);
  193. */
  194. DB::table('money_buy_str')->where('batch_id',$batch_id)->update(['wait_match_num'=>0,'prize_note'=>0,'game_status'=>3,'settle_status'=>2,'gain_money'=>0]);
  195. //if(!($ret || $ret===0)){ throw new \Exception('更新数据出错2!'); }
  196. return true ;
  197. }
  198. $in_array[] = ['odds'=>$val->odds,'winOrLose'=>$val->result] ;
  199. }
  200. $chuanNum = intval(substr($this->BuyDatasMainModel->str_type,0,1));
  201. $lasPeilv = $this->stringComputing([$in_array,$chuanNum]);
  202. $money = floatPointDigit($this->BuyDatasMainModel->money * $lasPeilv ) ;
  203. $ret = DB::update('update money_buy_str set settle_status=2 , game_status=1 , gain_money=? where order_id = ?', [$money,$order_id]);
  204. if(!($ret || $ret===0)){ throw new \Exception('更新数据出错3!'); }
  205. $this->insertData($order_id,$money,$this->BuyDatasMainModel->account_identity,2,$val->game_code,$this->BuyDatasMainModel->info_identity,$money);
  206. return true ;
  207. }
  208. }