SettlementBase.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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 SettlementBase
  12. {
  13. const USER_WIN = 1 ; //用户赢钱
  14. const USER_LOSE = -1; //用户输钱
  15. const USER_FLAT = 2 ; //持平,用户钱原路返回
  16. public $match_id = 0 ; //比赛场次ID
  17. public $matchModel = null ; //比赛场次Model对像
  18. public $lg_id = 0 ; //联赛ID
  19. public $lgModel = null ; //联赛Model对像
  20. public $resultId = 0 ; //比赛最终结果ID
  21. public $resultModel = null ; //比赛最终结果对像MODEL
  22. public $resultRecords = [] ; //比赛中间结果对像数组;
  23. public $buyArrays = [] ; //订单记录表
  24. public $gameType = '' ; //类型
  25. //类型映射
  26. public $gameAllMap = [
  27. 'zq'=>'ZqRule',
  28. 'lq'=>'LqRule',
  29. 'wq'=>'WqRule',
  30. 'bq'=>'BqRule',
  31. ];
  32. private $AdapterObj = null ;
  33. public function __construct()
  34. {
  35. C()->set('BqRule','\Biz\Settlement\Adapter\BqRule');
  36. C()->set('LqRule','\Biz\Settlement\Adapter\LqRule');
  37. C()->set('WqRule','\Biz\Settlement\Adapter\WqRule');
  38. C()->set('ZqRule','\Biz\Settlement\Adapter\ZqRule');
  39. return true;
  40. }
  41. //返回数据
  42. private function makeData($status=1,$message='success',$data=''){
  43. return [
  44. 'status' => $status ,
  45. 'message' =>$message,
  46. 'data' => $data ,
  47. ] ;
  48. }
  49. //开始结算处理
  50. public function doSettlement(){
  51. $model = $this->getNoticeData();
  52. if ($model) { return $this->makeData(1,'no data to process') ; }
  53. try{
  54. _beginTransaction();
  55. $ret1 = $this->doInit($model->type,$model->competionid);
  56. if (!$ret1){ _rollBack(); return $this->makeData(0,'false_1') ; }
  57. $ret2 = $this->writeStatusBegin($model);
  58. if (!$ret2){ _rollBack(); return $this->makeData(0,'false_2') ; }
  59. $ret3 = $this->getOrdersByMatchid($this->gameType,$this->match_id);
  60. if (!$ret3){
  61. //没有下单数据,本场赛事结算处理完毕
  62. $this->writeStatusEndOk($model);
  63. _commit();
  64. return $this->makeData();
  65. }
  66. $ret4 = $this->BatchModesResult($ret3);
  67. if (!$ret4){
  68. _rollBack(); return $this->makeData(0,'false_4') ;
  69. }
  70. _commit();
  71. return $this->makeData();
  72. }catch(\Exception $e){
  73. _rollBack();
  74. return $this->makeData(0,'false',['msg'=>$e->getMessage(),'code'=>$e->getCode()]) ;
  75. }
  76. }
  77. //写处理状态
  78. public function writeStatusBegin($comendnticeModel){
  79. $comendnticeModel->status = 1 ;
  80. $comendnticeModel->pupdatetime = date("Y-m-d H:i:s");
  81. $comendnticeModel->pcount ++ ;
  82. $comendnticeModel->logs = 'begin|';
  83. $ret = $comendnticeModel->save();
  84. return $ret;
  85. }
  86. //写处理状态结束
  87. public function writeStatusEndOk($comendnticeModel){
  88. $comendnticeModel->status = 4 ;
  89. $comendnticeModel->pret = 1 ;
  90. $comendnticeModel->puodateendtime = date("Y-m-d H:i:s");
  91. $comendnticeModel->logs = $comendnticeModel->logs.'end ok';
  92. $ret = $comendnticeModel->save();
  93. return $ret;
  94. }
  95. //查询是否有未处理的数据
  96. public function getNoticeData($where = ['status'=>0]){
  97. $model = lm('Comendnotice','Commons')->where($where)->first();
  98. if (!$model){
  99. return false;
  100. }
  101. return $model ;
  102. }
  103. //实现结算功能
  104. public function doInit($type,$match_id){
  105. if ( isset($this->gameAllMap)){ throw new \Exception('赛事类型错误',4002); return false; }
  106. switch ($type){
  107. case 'bq':
  108. $this->AdapterObj = C()->get('BqRule');
  109. break;
  110. case 'lq':
  111. $this->AdapterObj = C()->get('LqRule');
  112. break;
  113. case 'wq':
  114. $this->AdapterObj = C()->get('WqRule');
  115. break;
  116. case 'zq':
  117. $this->AdapterObj = C()->get('ZqRule');
  118. break;
  119. }
  120. $this->gameType = $type ;
  121. $this->match_id = $match_id ;
  122. $this->getCompetitionData($type,$match_id);
  123. $this->getCompResult($type,$match_id);
  124. return true ;
  125. }
  126. //获取比赛数据模型
  127. public function getCompetitionData($type,$match_id){
  128. $model = null ;
  129. switch ($type){
  130. case 'bq':
  131. $model = lm('St_bq_competition','Commons')->where('match_id',$match_id)->first();
  132. break;
  133. case 'lq':
  134. $model = lm('St_lq_competition','Commons')->where('match_id',$match_id)->first();
  135. break;
  136. case 'wq':
  137. $model = lm('St_wq_competition','Commons')->where('match_id',$match_id)->first();
  138. break;
  139. case 'zq':
  140. $model = lm('St_zq_competition','Commons')->where('match_id',$match_id)->first();
  141. break;
  142. }
  143. if (empty($model)){
  144. throw new \Exception('无效match_id_'.$type.'-'.$match_id,4004);
  145. return false;
  146. }
  147. $this->matchModel = $model;
  148. return $model ;
  149. }
  150. //查询此比较有下单记录
  151. public function getOrdersByMatchid($type,$marchid){
  152. $modes = lm('Money_buy_match','Commons')->getByTypeMatch($type,$marchid);
  153. return $$modes;
  154. }
  155. //批量结算处理 [只处理状态]
  156. public function BatchModesResult($type,$models){
  157. $pclass = $this->AdapterObj ;
  158. $RefClass = new \ReflectionClass($pclass) ;
  159. foreach ($models as $model){
  160. $oddscode = $model->odds_code;
  161. if ( $RefClass->hasMethod($oddscode)){
  162. $model->result = $pclass->$oddscode($model,$this->resultModel,$this->resultRecords);
  163. $model->utime = date("Y-m-d H:i:s");
  164. $ret = $model->save();
  165. if (!$ret){
  166. throw new \Exception('更新记录buy_match失败!',4006);
  167. }
  168. $this->getBuyDatas($model->order_id);
  169. }else{
  170. throw new \Exception('找不到此玩法的胜负规则逻辑!',4005);
  171. }
  172. }
  173. return true;
  174. }
  175. //某个订单下面的所有比赛是否全部处理完成
  176. public function OrderIsPressFinish($orderId){
  177. }
  178. //得到比赛最终结果记录 和 中间结果记录
  179. public function getCompResult($type,$match_id){
  180. $model = null ;
  181. switch ($type){
  182. case 'bq':
  183. $model = lm('St_bq_result','Commons')->where('match_id',$match_id)->first();
  184. $models = lm('St_bq_result_record','Commons')->where('match_id',$match_id)->find();
  185. break;
  186. case 'lq':
  187. $model = lm('St_lq_resultn','Commons')->where('match_id',$match_id)->first();
  188. $models = lm('St_lq_result_record','Commons')->where('match_id',$match_id)->find();
  189. break;
  190. case 'wq':
  191. $model = lm('St_wq_result','Commons')->where('match_id',$match_id)->first();
  192. $models = lm('St_wq_result_record','Commons')->where('match_id',$match_id)->find();
  193. break;
  194. case 'zq':
  195. $model = lm('St_zq_result','Commons')->where('match_id',$match_id)->first();
  196. $models = lm('St_zq_result_record','Commons')->where('match_id',$match_id)->find();
  197. break;
  198. }
  199. if (empty($model)){
  200. throw new \Exception('没找到比赛结果记录match_id_'.$type.'-'.$match_id,4007);
  201. return false;
  202. }
  203. $this->resultModel = $model;
  204. $this->resultRecords = $models ;
  205. $ret = [
  206. 'result' => $model,
  207. 'records' => $models ,
  208. ];
  209. return $ret;
  210. }
  211. //得到订单信息
  212. public function getBuyDatas($orderId){
  213. if ( isset($this->buyArrays[$orderId]) ){
  214. return $this->buyArrays[$orderId];
  215. }
  216. $model = lm("Money_buy")->where(['order_id',$orderId])->first();
  217. if (!$model){
  218. throw new \Exception('没有订单基本信息_'.$orderId, 4008);
  219. }
  220. $details = lm("Money_buy_detail")->where(['order_id',$orderId])->find();
  221. if (!$details){
  222. throw new \Exception('没有订单detail信息_'.$orderId, 4009);
  223. }
  224. $ret = [
  225. 'buy' =>$model ,
  226. 'details' => $details ,
  227. ];
  228. $this->buyArrays[$orderId] = $ret ;
  229. return $ret;
  230. }
  231. }