SettlementWinFail.php 12 KB

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