SettlementWinFail.php 17 KB

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