SettlementWinFail.php 16 KB

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