SettlementWinFail.php 16 KB

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