SettlementWinFail.php 12 KB

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