SettlementWinFail.php 14 KB

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