SettlementWinFail.php 13 KB

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