SettlementWinFail.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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\Models\Comendnotice as ComendnoticeModel;
  13. use App\Lib\Settlement\Adapter\ZqRule;
  14. use App\Lib\Settlement\Adapter\LqRule;
  15. use App\Lib\Settlement\Adapter\WqRule;
  16. use App\Lib\Settlement\Adapter\BqRule ;
  17. //一场比赛结束,计算出对应 串式单式下单数据的 输赢并更新到money_buy_match对应记录
  18. class SettlementWinFail
  19. {
  20. //比赛结束消息通知模型
  21. public $ComendNoticModel = null ;
  22. public $gameType = '' ; //类型
  23. public $match_id = 0 ; //比赛ID
  24. public $resultModel = null ; //比赛最终结果对像MODEL
  25. public $resultRecords = [] ; //比赛中间结果对像数组;
  26. public $gjModel = null ; //冠军比赛结果数据
  27. //类型映射
  28. public $gameAllMap = [
  29. 'zq'=>'ZqRule',
  30. 'lq'=>'LqRule',
  31. 'wq'=>'WqRule',
  32. 'bq'=>'BqRule',
  33. ];
  34. private $AdapterObj = null ;
  35. private $RefClass = null ;
  36. //按赛事批量结算
  37. public function doRun(){
  38. try {
  39. $ComendNoticModel = $this->getComendNoticeModel();
  40. if (!$ComendNoticModel) {
  41. return $this->makeData(1,'没有要处理的数据,直接退出');
  42. }
  43. $this->writeStatusBegin($ComendNoticModel);
  44. DB::beginTransaction ();
  45. $allmatchs = DB::table('money_buy_match')->where(['game_code'=>$ComendNoticModel->game_code,'match_id'=>$ComendNoticModel->match_id,'result'=>0])->get();
  46. if (empty($allmatchs)) {
  47. $this->writeStatusEndOk($ComendNoticModel);
  48. DB::commit();
  49. return $this->makeData(1,'本赛事无订单数据,退出');
  50. }
  51. $this->gameType = $ComendNoticModel->game_code ;
  52. $this->match_id = $ComendNoticModel->match_id ;
  53. $this->setAdapterObj($ComendNoticModel->game_code);
  54. $this->RefClass = new \ReflectionClass( $this->AdapterObj ) ;
  55. $this->getCompResult($ComendNoticModel->game_code,$ComendNoticModel->match_id);
  56. $this->Settlement_simplex($this->gameType,$this->match_id);
  57. $this->Settlement_str($this->gameType,$this->match_id);
  58. $this->writeStatusEndOk($ComendNoticModel);
  59. DB::commit();
  60. return $this->makeData(1,'success-all');
  61. }catch (\Exception $e){
  62. DB::rollBack();
  63. return $this->makeData(0,'false',['msg'=>$e->getMessage(),'code'=>$e->getCode()]) ;
  64. }
  65. }
  66. //单式订单结算处理
  67. private function Settlement_simplex($game_type,$match_id){
  68. $buyModels = DB::table('money_buy_simplex')->where(['match_id'=>$match_id,'game_status'=>0])->get();
  69. if (empty($buyModels)){ return true; }
  70. $buymatchModles = DB::table('money_buy_match')->where(['match_id'=>$match_id,'bet_type'=>1,'result'=>0,'game_code'=>$game_type])->orderby('batch_id','asc')->get();
  71. if (empty($buymatchModles)) { return true ; }
  72. $buyKeyModel = $buymatchKeyModles = [] ;
  73. foreach ($buyModels as $val){
  74. $batch_id = $val->batch_id;
  75. $buyKeyModel[$batch_id] = $val;
  76. }
  77. foreach ($buymatchModles as $val){
  78. $batch_id = $val->batch_id;
  79. $buymatchKeyModles[$batch_id][] = $val;
  80. }
  81. foreach ($buyKeyModel as $batch_id=>$val){
  82. foreach ($buymatchKeyModles as $sub_batch_id=>$fsval){
  83. foreach ($fsval as $sval) {
  84. $winorfalse = $this->winOrfalseInfo($sval);
  85. $this->makesql_up_buymatch_winorfalse($sval->id, $winorfalse, date("Y-m-d H:i:s"));
  86. }
  87. }
  88. }
  89. return true;
  90. }
  91. //串式订单结算处理
  92. private function Settlement_str($game_type,$match_id){
  93. $matchModels = DB::table('money_buy_match')->where(['match_id'=>$match_id,'bet_type'=>2,'game_code'=>$game_type])->get();
  94. if (empty($matchModels)) { return true; }
  95. $batch_ids_array = [] ;
  96. $matchKeyModels = [] ;
  97. foreach ($matchModels as $val){
  98. $matchKeyModels[$val->batch_id][] = $val ;
  99. if (!in_array($val->batch_id,$batch_ids_array)){
  100. $batch_ids_array[] = $val->batch_id;
  101. }
  102. }
  103. $buyModels = DB::table('money_buy_str')->whereIn( 'batch_id',$batch_ids_array)->get();
  104. $buyKeyModels = [] ;
  105. if (empty($buyModels)) { return true ; }
  106. foreach ($buyModels as $val){
  107. $buyKeyModels[$val->batch_id][] = $val ;
  108. }
  109. foreach ($matchKeyModels as $key=>$sval_2){
  110. foreach ($sval_2 as $sval){
  111. if ( $sval->match_id != $this->match_id ){ continue ; }
  112. $winorfalse = $this->winOrfalseInfo($sval);
  113. $this->makesql_up_buymatch_winorfalse($sval->id,$winorfalse,date("Y-m-d H:i:s"));
  114. $sql = "update money_buy_str set wait_match_num=wait_match_num-1 where batch_id='$sval->batch_id' ";
  115. DB::update($sql);
  116. }
  117. }
  118. }
  119. //输赢结果判断 $sval 为 money_buy_match Model; 结果为 -1输 1赢 2平 3赢半平半 4输半平半
  120. private function winOrfalseInfo($sval){
  121. $fun = $sval->odds_code ;
  122. $fun2 = $sval->p_code ;
  123. if ($sval->p_code != 'gj'){
  124. if (empty($this->resultModel) || empty($this->resultRecords)){
  125. throw new \Exception('非冠军比赛结果数据不能为空');
  126. }
  127. if ( $this->RefClass->hasMethod($fun) ){
  128. $winorfalse = $this->AdapterObj->$fun($sval,$this->resultModel,$this->resultRecords);
  129. }elseif ( $this->RefClass->hasMethod($fun2) ){
  130. $winorfalse = $this->AdapterObj->$fun2($sval,$this->resultModel,$this->resultRecords);
  131. }else{
  132. throw new \Exception('没有找到玩法输赢判断规则',40010);
  133. }
  134. }else{
  135. $this->getGjDatas($sval);
  136. if (empty($this->gjModel)){
  137. throw new \Exception('冠军比赛结果数据不能为空');
  138. }
  139. if ( $this->RefClass->hasMethod($fun) ){
  140. $winorfalse = $this->AdapterObj->$fun($sval,$this->gjModel,[]);
  141. }elseif ( $this->RefClass->hasMethod($fun2) ){
  142. $winorfalse = $this->AdapterObj->$fun2($sval,$this->gjModel,[]);
  143. }else{
  144. throw new \Exception('没有找到玩法输赢判断规则',40010);
  145. }
  146. }
  147. if (!in_array($winorfalse,[-1,1,2,3,4])) {
  148. throw new \Exception('输赢结果异常!');
  149. }
  150. return $winorfalse ;
  151. }
  152. private function makesql_up_buymatch_winorfalse($id,$result,$utime){
  153. $sql = " update money_buy_match set result=$result,utime='$utime' where id=$id ";
  154. DB::update($sql) ;
  155. return true;
  156. }
  157. //设置 输赢 解析规则适配器
  158. private function setAdapterObj($game_type){
  159. $game_type = strtolower($game_type);
  160. if ( !isset($this->gameAllMap)){ throw new \Exception('赛事类型错误-'.$game_type,4002); return false; }
  161. switch ($game_type){
  162. case 'bq':
  163. $this->AdapterObj = new BqRule();
  164. break;
  165. case 'lq':
  166. $this->AdapterObj = new LqRule();
  167. break;
  168. case 'wq':
  169. $this->AdapterObj = new WqRule();
  170. break;
  171. case 'zq':
  172. $this->AdapterObj = new ZqRule();
  173. break;
  174. }
  175. return true ;
  176. }
  177. //返回数据
  178. private function makeData($status=1,$message='success',$data=''){
  179. return [
  180. 'status' => $status ,
  181. 'message' =>$message,
  182. 'data' => $data ,
  183. ] ;
  184. }
  185. //写处理状态
  186. public function writeStatusBegin($comendnticeModel){
  187. $comendnticeModel->status = 1 ;
  188. $comendnticeModel->done_time = date("Y-m-d H:i:s");
  189. $comendnticeModel->pcount ++ ;
  190. $comendnticeModel->logs = 'begin|';
  191. $ret = $comendnticeModel->save();
  192. return $ret;
  193. }
  194. //写处理状态结束
  195. public function writeStatusEndOk($comendnticeModel){
  196. $comendnticeModel->status = 4 ;
  197. $comendnticeModel->done_time = date("Y-m-d H:i:s");
  198. $comendnticeModel->logs = $comendnticeModel->logs.'end ok';
  199. $ret = $comendnticeModel->save();
  200. return $ret;
  201. }
  202. //是否有需要进行结果算处理的赛事记录
  203. public function getComendNoticeModel(){
  204. $ret = (new ComendnoticeModel())->where('status',0)->orderby('id','asc')->first();
  205. $this->ComendNoticModel = $ret;
  206. return $ret;
  207. }
  208. //得到比赛最终结果记录 和 中间结果记录
  209. public function getCompResult($type,$match_id){
  210. $model = null ;
  211. switch ($type){
  212. case 'bq':
  213. $model = DB::table('st_bq_result')->where('match_id',$match_id)->first();
  214. $models = DB::table('st_bq_result_record')->where('match_id',$match_id)->orderBy('id','asc')->get()->toArray();
  215. break;
  216. case 'lq':
  217. $model = DB::table('st_lq_result')->where('match_id',$match_id)->first();
  218. $models = DB::table('st_lq_result_record')->where('match_id',$match_id)->orderBy('id','asc')->get()->toArray();
  219. break;
  220. case 'wq':
  221. $model = DB::table('st_wq_result')->where('match_id',$match_id)->first();
  222. $models = DB::table('st_wq_result_record')->where('match_id',$match_id)->orderBy('id','asc')->get()->toArray();
  223. break;
  224. case 'zq':
  225. $model = DB::table('st_zq_result')->where('match_id',$match_id)->first();
  226. $models = DB::table('st_zq_result_record')->where('match_id',$match_id)->orderBy('id','asc')->get()->toArray();
  227. break;
  228. }
  229. $this->resultModel = $model;
  230. $this->resultRecords = $models ;
  231. $ret = [
  232. 'result' => $model,
  233. 'records' => $models ,
  234. ];
  235. return $ret;
  236. }
  237. //冠军比赛结果
  238. public function getGjDatas($matchModel){
  239. if ( !empty($this->gjModel) ) { return $this->gjModel ; }
  240. $table = 'st_'.$matchModel->game_code.'_league_result';
  241. $where = ['lg_id'=>$matchModel->lg_id,'game_name'=>$matchModel->odds_code];
  242. $model = DB::table($table)->where($where)->first();
  243. if (!empty($model)){
  244. $this->gjModel = $model ;
  245. }
  246. return $model ;
  247. }
  248. }