SettlementSql.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2019/4/25
  6. * Time: 14:10
  7. */
  8. namespace Biz\Settlement;
  9. set_time_limit(600);
  10. ini_set('memory_limit', '256M');
  11. class SettlementSql
  12. {
  13. //用户的期末基本资料表 [uid]['account'=>$obj,'detail'=>$obj];
  14. public $AccountArrays = [] ;
  15. //buy_match 里batch_id 相同的数据
  16. public $MatchBatchIdArrays = [] ;
  17. //buy_str 批次号相同的记录
  18. public $BuyStrBatchIdArrays = [] ;
  19. //所有比赛场次号相同的记录
  20. public $AllMatchIdArrays = [];
  21. //比赛结束消息通知模型
  22. public $ComendNoticModel = null ;
  23. //返回的sql数组
  24. private $sqlArray = [] ;
  25. public $gameType = '' ; //类型
  26. public $resultId = 0 ; //比赛最终结果ID
  27. public $resultModel = null ; //比赛最终结果对像MODEL
  28. public $resultRecords = [] ; //比赛中间结果对像数组;
  29. //类型映射
  30. public $gameAllMap = [
  31. 'zq'=>'ZqRule',
  32. 'lq'=>'LqRule',
  33. 'wq'=>'WqRule',
  34. 'bq'=>'BqRule',
  35. ];
  36. private $AdapterObj = null ;
  37. private $RefClass = null ;
  38. public function doRun(){
  39. try {
  40. _beginTransaction();
  41. $ComendNoticModel = $this->getComendNoticeModel();
  42. if (!$ComendNoticModel) {
  43. _commit();
  44. return $this->makeData();
  45. }
  46. $this->writeStatusBegin($ComendNoticModel);
  47. $allmatchs = $this->getAllMatchIdArrays($ComendNoticModel->game_code, $ComendNoticModel->match_id, 2);
  48. $this->setAdapterObj( $ComendNoticModel->game_code );
  49. $this->RefClass = new \ReflectionClass( $this->setAdapterObj ) ;
  50. $this->getCompResult($ComendNoticModel->game_code,$ComendNoticModel->match_id);
  51. if (!$allmatchs) {
  52. $this->writeStatusEndOk($ComendNoticModel);
  53. _commit();
  54. return $this->makeData();
  55. }
  56. foreach ($allmatchs as $buymatchmodel){
  57. $this->doSettlement($buymatchmodel);
  58. }
  59. _commit();
  60. return $this->makeData();
  61. }catch (\Exception $e){
  62. _rollBack();
  63. return $this->makeData(0,'false',['msg'=>$e->getMessage(),'code'=>$e->getCode()]) ;
  64. }
  65. }
  66. //拼装 逻辑 sql;
  67. private function doSettlement($model){
  68. $fun = $model->odds_code ;
  69. if ( $this->RefClass->hasMethod( $fun)){
  70. $winorfalse = $this->AdapterObj->$fun($model,$this->resultModel,$this->resultRecords);
  71. $this->makesql_up_buymatch_winorfalse($model->id,$winorfalse,date("Y-m-d H:i:s"));
  72. if ($winorfalse == -1){
  73. $this->masql_up_buymatch_false($model->batch_id);
  74. }
  75. }else{
  76. throw new \Exception('找不到此玩法的胜负规则逻辑!',4005);
  77. }
  78. }
  79. private function makesql_up_buymatch_winorfalse($id,$result,$utime){
  80. $sql = " update money_buy_match set result=$result,utime=$utime where id=$id limit 1";
  81. $this->sqlArray[] = $sql;
  82. return true;
  83. }
  84. private function masql_up_buymatch_false($batch_id){
  85. $uptime = date("Y-m-d H:i:s");
  86. $sql = " update money_buy_match set result=-1 ,utime=$uptime,status=1 where batch_id=$batch_id ";
  87. $this->sqlArray[] = $sql;
  88. $sql = "update money_buy_str set settle_status=1,game_status=3 ,wait_match_num=0 where batch_id=$batch_id " ;
  89. $this->sqlArray[] = $sql;
  90. return true ;
  91. }
  92. private function setAdapterObj($game_type){
  93. if ( isset($this->gameAllMap)){ throw new \Exception('赛事类型错误',4002); return false; }
  94. switch ($game_type){
  95. case 'bq':
  96. C()->set('BqRule','\Biz\Settlement\Adapter\BqRule');
  97. $this->AdapterObj = C()->get('BqRule');
  98. break;
  99. case 'lq':
  100. C()->set('LqRule','\Biz\Settlement\Adapter\LqRule');
  101. $this->AdapterObj = C()->get('LqRule');
  102. break;
  103. case 'wq':
  104. C()->set('WqRule','\Biz\Settlement\Adapter\WqRule');
  105. $this->AdapterObj = C()->get('WqRule');
  106. break;
  107. case 'zq':
  108. C()->set('ZqRule','\Biz\Settlement\Adapter\ZqRule');
  109. $this->AdapterObj = C()->get('ZqRule');
  110. break;
  111. }
  112. return true ;
  113. }
  114. //返回数据
  115. private function makeData($status=1,$message='success',$data=''){
  116. return [
  117. 'status' => $status ,
  118. 'message' =>$message,
  119. 'data' => $data ,
  120. ] ;
  121. }
  122. //写处理状态
  123. public function writeStatusBegin($comendnticeModel){
  124. $comendnticeModel->status = 1 ;
  125. $comendnticeModel->pupdatetime = date("Y-m-d H:i:s");
  126. $comendnticeModel->pcount ++ ;
  127. $comendnticeModel->logs = 'begin|';
  128. $ret = $comendnticeModel->save();
  129. return $ret;
  130. }
  131. //写处理状态结束
  132. public function writeStatusEndOk($comendnticeModel){
  133. $comendnticeModel->status = 4 ;
  134. $comendnticeModel->pret = 1 ;
  135. $comendnticeModel->puodateendtime = date("Y-m-d H:i:s");
  136. $comendnticeModel->logs = $comendnticeModel->logs.'end ok';
  137. $ret = $comendnticeModel->save();
  138. return $ret;
  139. }
  140. public function getComendNoticeModel(){
  141. $ret = lm('Comendnotice','Commons')->wherer('status',0)->order('id','asc')->first();
  142. $this->ComendNoticModel = $ret;
  143. return $ret;
  144. }
  145. public function getAllMatchIdArrays( $type,$matchid ,$bet_type=2,$result = 0 ){
  146. $ret = lm('Money_buy_match','Commons')->where(['game_code'=>$type,'match_id'=>$matchid,'bet_type'=>$bet_type,'result'=>$result])->find();
  147. $this->AllMatchIdArrays = $ret;
  148. return $ret;
  149. }
  150. public function getUserInfo($account_name){
  151. if (isset($this->AccountArrays[$account_name])){
  152. return $this->AccountArrays['$account_name'];
  153. }
  154. $accountModel = lm('Account','Commons')->where(['account',$account_name])->first();
  155. if (!$accountModel){
  156. throw new \Exception('查无此用户数据account='.$account_name,41002);
  157. }
  158. $detailModel = lm('Account_detailed','Commons')->where(['account_identity',$accountModel->identity])->first();
  159. if (!$detailModel){
  160. throw new \Exception('查无此用户数据account_detail='.$accountModel->identity,41002);
  161. }
  162. $ret = [
  163. 'account'=>$detailModel,
  164. 'detail' => $detailModel ,
  165. ];
  166. $this->AccountArrays[$account_name] = $ret ;
  167. $this->AccountArrays[$accountModel->identity] = $ret ;
  168. return $ret;
  169. }
  170. public function GetMatchBatchIdArrays($batch_id){
  171. if (isset($this->MatchBatchIdArrays[$batch_id])){
  172. return $this->MatchBatchIdArrays[$batch_id];
  173. }
  174. $all = lm("Money_buy_match")->getByBatchId($batch_id);
  175. if (!$all) {
  176. throw new \Exception('无效的Money_buy_str->batch_id='.$batch_id , 41001);
  177. }
  178. $this->MatchBatchIdArrays[$batch_id] = $all ;
  179. return $all ;
  180. }
  181. public function GetBuyStrBatchIdArrays($batch_id){
  182. if (isset($this->BuyStrBatchIdArrays[$batch_id])){
  183. return $this->BuyStrBatchIdArrays[$batch_id];
  184. }
  185. $all = lm("Money_buy_str")->getByBatchId($batch_id);
  186. if (!$all) {
  187. throw new \Exception('无效的Money_buy_str->batch_id='.$batch_id , 41001);
  188. }
  189. $this->BuyStrBatchIdArrays[$batch_id] = $all ;
  190. return $all ;
  191. }
  192. //得到比赛最终结果记录 和 中间结果记录
  193. public function getCompResult($type,$match_id){
  194. $model = null ;
  195. switch ($type){
  196. case 'bq':
  197. $model = lm('St_bq_result','Commons')->where('match_id',$match_id)->first();
  198. $models = lm('St_bq_result_record','Commons')->where('match_id',$match_id)->find();
  199. break;
  200. case 'lq':
  201. $model = lm('St_lq_resultn','Commons')->where('match_id',$match_id)->first();
  202. $models = lm('St_lq_result_record','Commons')->where('match_id',$match_id)->find();
  203. break;
  204. case 'wq':
  205. $model = lm('St_wq_result','Commons')->where('match_id',$match_id)->first();
  206. $models = lm('St_wq_result_record','Commons')->where('match_id',$match_id)->find();
  207. break;
  208. case 'zq':
  209. $model = lm('St_zq_result','Commons')->where('match_id',$match_id)->first();
  210. $models = lm('St_zq_result_record','Commons')->where('match_id',$match_id)->find();
  211. break;
  212. }
  213. if (empty($model)){
  214. throw new \Exception('没找到比赛结果记录match_id_'.$type.'-'.$match_id,4007);
  215. return false;
  216. }
  217. $this->resultModel = $model;
  218. $this->resultRecords = $models ;
  219. $ret = [
  220. 'result' => $model,
  221. 'records' => $models ,
  222. ];
  223. return $ret;
  224. }
  225. }