WinfailLogic.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2019/7/15
  6. * Time: 16:51
  7. */
  8. namespace App\Logic;
  9. use Illuminate\Database\Capsule\Manager as DB;
  10. use App\Lib\Settlement\Adapter\ZqRule;
  11. use App\Lib\Settlement\Adapter\LqRule;
  12. use App\Lib\Settlement\Adapter\WqRule;
  13. use App\Lib\Settlement\Adapter\BqRule;
  14. use datainf\pplus\Instance;
  15. class WinfailLogic
  16. {
  17. use Instance;
  18. //类型映射
  19. public $gameAllMap = [
  20. 'zq' => 'ZqRule',
  21. 'lq' => 'LqRule',
  22. 'wq' => 'WqRule',
  23. 'bq' => 'BqRule',
  24. ];
  25. public function getNoticeDate($id)
  26. {
  27. $noticeMode = DB::table('comendnotice')->where(['id' => $id])->first();
  28. if (empty($noticeMode)) {
  29. throw new \Exception("没有找到notice记录-" . $id);
  30. }
  31. return $noticeMode;
  32. }
  33. //r按赛事 全部重计算输赢时,要清除手动设置的结果
  34. public function Reset_manualData($game_code, $match_id)
  35. {
  36. DB::table('money_buy_simplex')->where(['match_id' => $match_id, 'game_code' => $game_code])->update(['is_manual' => 0]);
  37. }
  38. //得到比赛最终结果记录
  39. public function getCompResult($type, $match_id)
  40. {
  41. $model = null;
  42. switch ($type) {
  43. case 'bq':
  44. $model = DB::table('st_bq_result')->where('match_id', $match_id)->orderby('id', 'asc')->get();
  45. break;
  46. case 'lq':
  47. $model = DB::table('st_lq_result')->where('match_id', $match_id)->orderby('id', 'asc')->get();
  48. break;
  49. case 'wq':
  50. $model = DB::table('st_wq_result')->where('match_id', $match_id)->orderby('id', 'asc')->get();
  51. break;
  52. case 'zq':
  53. $model = DB::table('st_zq_result')->where('match_id', $match_id)->orderby('id', 'asc')->get();
  54. break;
  55. }
  56. if (empty($model)) {
  57. throw new \Exception("没有找到赛事结果记录-" . $type . '-' . $match_id);
  58. }
  59. return $model;
  60. }
  61. //单式订单存在 单独设置比赛结果的情况,要分开处理
  62. public function getSimplexData($type, $match_id)
  63. {
  64. $return = [];
  65. $ret = DB::table("money_buy_simplex")->where(['game_code' => $type, 'match_id' => $match_id])->get();
  66. if (count($ret) <= 0) {
  67. return;
  68. }
  69. foreach ($ret as $val) {
  70. $return[$val->order_id][$val->match_id] = ['result_flag' => $val->result_flag, 'single_result' => $val->single_result];
  71. }
  72. return $return;
  73. }
  74. //找到订单详细数据
  75. public function getMatchRecords($type, $match_id, $bet_type)
  76. {
  77. $return = [];
  78. $ret = DB::table("money_buy_match")->where(['game_code' => $type, 'match_id' => $match_id, 'bet_type' => $bet_type])->get();
  79. if (count($ret) <= 0) {
  80. return [];
  81. }
  82. foreach ($ret as $val) {
  83. $return[] = $val;
  84. }
  85. return $return;
  86. }
  87. public function getAdapterObj($game_type)
  88. {
  89. $game_type = strtolower($game_type);
  90. if (!isset($this->gameAllMap)) {
  91. throw new \Exception('赛事类型错误-' . $game_type, 4002);
  92. return false;
  93. }
  94. switch ($game_type) {
  95. case 'bq':
  96. $apd = new BqRule();
  97. break;
  98. case 'lq':
  99. $apd = new LqRule();
  100. break;
  101. case 'wq':
  102. $apd = new WqRule();
  103. break;
  104. case 'zq':
  105. $apd = new ZqRule();
  106. break;
  107. }
  108. return $apd;
  109. }
  110. //冠军比赛结果
  111. public function getGjDatas($matchModel)
  112. {
  113. $table = 'st_' . $matchModel->game_code . '_league_result';
  114. $where = ['lg_id' => $matchModel->lg_id, 'game_name' => $matchModel->game_code];
  115. $model = DB::table($table)->where($where)->first();
  116. if (empty($model)) {
  117. return false;
  118. }
  119. return $model;
  120. }
  121. //冠军比赛结果
  122. public function getGjDatasV2($game_code, $lg_id)
  123. {
  124. $table = 'st_' . $game_code . '_league_result';
  125. $where = ['lg_id' => $lg_id, 'game_name' => $game_code];
  126. $model = DB::table($table)->where($where)->first();
  127. if (empty($model)) {
  128. return false;
  129. }
  130. return $model;
  131. }
  132. public function getOddsTypeData($game_code, $match_id)
  133. {
  134. $ret = DB::table('money_buy_match')->select("p_code", "odds_code", "condition", "bet_type", "is_champion")->where(['match_id' => $match_id, 'game_code' => $game_code])->groupBy('p_code', 'odds_code', 'condition', 'bet_type', 'is_champion')->get();
  135. return $ret;
  136. }
  137. //胜负判断 NotiematchModel赛事结束通知model $result普通赛事结果对像数组[冠军赛事下可能为空] $GJresult冠军赛事结果对像数组[非冠军赛事为空]
  138. public function doLogic($NotiematchModel, $result, $GJresult)
  139. {
  140. $game_code = $NotiematchModel->game_code;
  141. $match_id = $NotiematchModel->match_id;
  142. $AdapterObj = $this->getAdapterObj($game_code);
  143. $groupDatas = $this->getOddsTypeData($game_code, $match_id);
  144. $fristRet = $this->getFristRecord($game_code, $match_id, $groupDatas);
  145. $RefClass = new \ReflectionClass($AdapterObj);
  146. $noticeId = $NotiematchModel->id;
  147. $nowtime = date("Y-m-d H:i:s");
  148. $winorfalsedef = ['result' => 2, 'matchResult' => 'noRuleOrError'];
  149. foreach ($fristRet as $matchModel) {
  150. $is_champion = $matchModel->is_champion;
  151. $fun1 = $matchModel->odds_code;
  152. $fun2 = $matchModel->p_code;
  153. if ($is_champion == 0) {
  154. //普通赛事
  155. $resultTypeNow = $result;
  156. if (empty($resultTypeNow)) {
  157. throw new \Exception("赛事结果不能为空! --lg_id=" . $matchModel->game_code . '_' . $matchModel->lg_id);
  158. }
  159. } else {
  160. //冠军赛事;
  161. $resultTypeNow = $GJresult;
  162. if (empty($resultTypeNow)) {
  163. throw new \Exception("冠军赛事结果不能为空! --lg_id=" . $matchModel->game_code . '_' . $matchModel->lg_id);
  164. }
  165. }
  166. try {
  167. if ($RefClass->hasMethod($fun1)) {
  168. $winorfalse = $AdapterObj->$fun1($matchModel, $resultTypeNow, []);
  169. } elseif ($RefClass->hasMethod($fun2)) {
  170. $winorfalse = $AdapterObj->$fun2($matchModel, $resultTypeNow, []);
  171. }
  172. } catch (\Exception $e) {
  173. }
  174. if (!isset($winorfalse['result']) || !isset($winorfalse['matchResult']) || !in_array($winorfalse['result'], [-1, 1, 2, 3, 4])) {
  175. $winorfalse = $winorfalsedef;
  176. }
  177. $win = intval($winorfalse['result']);
  178. $rwin = ($win == 1) ? 1 : ($win == -1 ? 2 : 3);
  179. $matword = $winorfalse['matchResult'];
  180. $odds_code = $fun1;
  181. $p_code = $fun2;
  182. $condition = $matchModel->condition;
  183. $bet_type = $matchModel->bet_type;
  184. DB::update('update money_buy_match set "result"=?,matchresult=? where match_id=? and game_code=? and odds_code=? and p_code=? and condition=? and is_champion=?', [$win, $matword, $match_id, $game_code, $odds_code, $p_code, $condition, $is_champion]);
  185. $sql = "select order_id from money_buy_match where match_id=$match_id and game_code='$game_code' and p_code='$p_code'and odds_code='$odds_code'and condition='$condition' and is_champion=$is_champion ";
  186. if ($bet_type == 1) {
  187. DB::update("update money_buy_simplex set game_status=$rwin where game_code='$game_code' and match_id=$match_id and order_id in ( $sql)");
  188. } else {
  189. DB::update("update money_buy_str set game_status=$rwin where order_id in ( $sql)");
  190. }
  191. }
  192. DB::update("update comendnotice set status=1,result=1,done_time='$nowtime',pcount=pcount+1 where id=$noticeId ");
  193. return true;
  194. }
  195. //得到每个玩法第一条记录 根据规则计算输赢
  196. public function getFristRecord($game_code, $match_id, $ret)
  197. {
  198. $return = [];
  199. foreach ($ret as $val) {
  200. $p_code = $val->p_code;
  201. $odds_code = $val->odds_code;
  202. $condition = $val->condition;
  203. $is_champion = $val->is_champion;
  204. $ret = DB::table('money_buy_match')->where(['match_id' => $match_id, 'game_code' => $game_code, 'p_code' => $p_code, 'odds_code' => $odds_code, 'condition' => $condition, 'is_champion' => $is_champion])->first();
  205. $return[] = $ret;
  206. }
  207. return $return;
  208. }
  209. //对单式订单 单个更改过比赛结果的订单进行输赢判断 $orderInfoArray一维数组 $matchArray二维数组
  210. public function WinFailOneOrder($orderInfo, $matchArray, $is_champion)
  211. {
  212. $game_code = $orderInfo->game_code;
  213. $match_id = $orderInfo->match_id;
  214. $order_id = $orderInfo->order_id;
  215. $AdapterObj = $this->getAdapterObj($game_code);
  216. $RefClass = new \ReflectionClass($AdapterObj);
  217. $result = json_decode($orderInfo->single_result);
  218. if ($is_champion == 0) {
  219. //还原回原来的类型,以兼容之前的处理
  220. switch (strtolower($orderInfo->game_code)) {
  221. case 'zq':
  222. $result['0']->first_score = json_encode($result['0']->first_score, 256);
  223. $result['0']->corner_ball = json_encode($result['0']->corner_ball, 256);
  224. $result['0']->penalty_card = json_encode($result['0']->penalty_card, 256);
  225. break;
  226. case 'lq':
  227. $result['0']->home_score = json_encode($result['0']->home_score, 256);
  228. $result['0']->guest_score = json_encode($result['0']->guest_score, 256);
  229. break;
  230. case 'wq':
  231. $result['0']->inning = json_encode($result['0']->inning, 256);
  232. break;
  233. case 'bq':
  234. $result['0']->match_score_t = json_encode($result['0']->match_score_t, 256);
  235. break;
  236. }
  237. $result['0']->warn_more = json_encode($result['0']->warn_more, 256);
  238. }
  239. $winorfalsedef = ['result' => 2, 'matchResult' => 'noRuleOrError'];
  240. foreach ($matchArray as $matchModel) {
  241. $fun1 = $matchModel->odds_code;
  242. $fun2 = $matchModel->p_code;
  243. $mid = $matchModel->id;
  244. try {
  245. if ($RefClass->hasMethod($fun1)) {
  246. $winorfalse = $AdapterObj->$fun1($matchModel, $result, []);
  247. } elseif ($RefClass->hasMethod($fun2)) {
  248. $winorfalse = $AdapterObj->$fun2($matchModel, $result, []);
  249. }
  250. } catch (\Exception $e) {
  251. echo 'excetption: ' . print_r([$order_id, $e->getMessage(), $e->getFile(), $e->getLine()], true) . "\n";
  252. }
  253. if (!isset($winorfalse['result']) || !isset($winorfalse['matchResult']) || !in_array($winorfalse['result'], [-1, 1, 2, 3, 4])) {
  254. $winorfalse = $winorfalsedef;
  255. }
  256. $win = intval($winorfalse['result']);
  257. $rwin = ($win == 1) ? 1 : ($win == -1 ? 2 : 3);
  258. $matword = $winorfalse['matchResult'];
  259. DB::update('update money_buy_match set "result"=?,matchresult=? where match_id=? and game_code=? and id=?', [$win, $matword, $match_id, $game_code, $mid]);
  260. }
  261. DB::update("update money_buy_simplex set game_status=$rwin where order_id='$order_id' and game_code='$game_code' and match_id='$match_id' ");
  262. return true;
  263. }
  264. //普通单式或串式订单输赢处理
  265. public function WinFailOneOrderNomal($order_id, $bet_type = 1)
  266. {
  267. if ($bet_type == 1) {
  268. $orderInfo = DB::table('money_buy_simplex')->where('order_id', $order_id)->first();
  269. } else {
  270. $orderInfo = DB::table('money_buy_str')->where('order_id', $order_id)->first();
  271. }
  272. $matchArray = DB::table('money_buy_match')->where('order_id', $order_id)->get();
  273. if (empty($orderInfo) || empty($matchArray)) {
  274. return 0;
  275. }
  276. if ($bet_type == 1) {
  277. $game_code = $orderInfo->game_code;
  278. $match_id = $orderInfo->match_id;
  279. $table = 'st_' . $game_code . '_result';
  280. $result = DB::table($table)->where('match_id', $match_id)->get();
  281. if (empty($result)) {
  282. return 0;
  283. }
  284. }
  285. $winorfalsedef = ['result' => 2, 'matchResult' => 'noRuleOrError'];
  286. foreach ($matchArray as $matchModel) {
  287. if ($bet_type == 2) {
  288. $game_code = $matchModel->game_code;
  289. $match_id = $matchModel->match_id;
  290. $table = 'st_' . $game_code . '_result';
  291. $result = DB::table($table)->where('match_id', $match_id)->get();
  292. if (empty($result)) {
  293. continue;
  294. }
  295. }
  296. $AdapterObj = $this->getAdapterObj($game_code);
  297. $RefClass = new \ReflectionClass($AdapterObj);
  298. $fun1 = $matchModel->odds_code;
  299. $fun2 = $matchModel->p_code;
  300. $mid = $matchModel->id;
  301. try {
  302. if ($RefClass->hasMethod($fun1)) {
  303. $winorfalse = $AdapterObj->$fun1($matchModel, $result, []);
  304. } elseif ($RefClass->hasMethod($fun2)) {
  305. $winorfalse = $AdapterObj->$fun2($matchModel, $result, []);
  306. }
  307. } catch (\Exception $e) {
  308. echo 'excetption: ' . print_r([$order_id, $e->getMessage(), $e->getFile(), $e->getLine()], true) . "\n";
  309. }
  310. if (!isset($winorfalse['result']) || !isset($winorfalse['matchResult']) || !in_array($winorfalse['result'], [-1, 1, 2, 3, 4])) {
  311. $winorfalse = $winorfalsedef;
  312. }
  313. $win = intval($winorfalse['result']);
  314. $rwin = ($win == 1) ? 1 : ($win == -1 ? 2 : 3);
  315. if ($bet_type == 2) {
  316. $rwinarr[] = $rwin;
  317. }
  318. $matword = $winorfalse['matchResult'];
  319. DB::update('update money_buy_match set "result"=?,matchresult=? where match_id=? and game_code=? and id=?', [$win, $matword, $match_id, $game_code, $mid]);
  320. }
  321. if ($bet_type == 1) {
  322. DB::update("update money_buy_simplex set game_status=$rwin where order_id='$order_id' ");
  323. } else {
  324. $rwin = 2;
  325. foreach ($rwinarr as $nwin) {
  326. if ($nwin == -1) {
  327. $rwin = -1;
  328. break;
  329. }
  330. }
  331. DB::update("update money_buy_str set game_status=$rwin where order_id='$order_id' ");
  332. }
  333. return 1;
  334. }
  335. }