SettlementWinFail.php 18 KB

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