WinfailLogic.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. //得到比赛最终结果记录
  34. public function getCompResult($type, $match_id)
  35. {
  36. $model = null;
  37. switch ($type) {
  38. case 'bq':
  39. $model = DB::table('st_bq_result')->where('match_id', $match_id)->orderby('id', 'asc')->get();
  40. break;
  41. case 'lq':
  42. $model = DB::table('st_lq_result')->where('match_id', $match_id)->orderby('id', 'asc')->get();
  43. break;
  44. case 'wq':
  45. $model = DB::table('st_wq_result')->where('match_id', $match_id)->orderby('id', 'asc')->get();
  46. break;
  47. case 'zq':
  48. $model = DB::table('st_zq_result')->where('match_id', $match_id)->orderby('id', 'asc')->get();
  49. break;
  50. }
  51. if (empty($model)) {
  52. throw new \Exception("没有找到赛事结果记录-" . $type . '-' . $match_id);
  53. }
  54. return $model;
  55. }
  56. //单式订单存在 单独设置比赛结果的情况,要分开处理
  57. public function getSimplexData($type, $match_id)
  58. {
  59. $return = [];
  60. $ret = DB::table("money_buy_simplex")->where(['game_code' => $type, 'match_id' => $match_id])->get();
  61. if (count($ret) <= 0) {
  62. return;
  63. }
  64. foreach ($ret as $val) {
  65. $return[$val->batch_id][$val->match_id] = ['result_flag' => $val->result_flag, 'single_result' => $val->single_result];
  66. }
  67. return $return;
  68. }
  69. //找到订单详细数据
  70. public function getMatchRecords($type, $match_id, $bet_type)
  71. {
  72. $return = [];
  73. $ret = DB::table("money_buy_match")->where(['game_code' => $type, 'match_id' => $match_id, 'bet_type' => $bet_type])->get();
  74. if (count($ret) <= 0) {
  75. return;
  76. }
  77. foreach ($ret as $val) {
  78. $return[] = $val;
  79. }
  80. return $return;
  81. }
  82. public function getAdapterObj($game_type)
  83. {
  84. $game_type = strtolower($game_type);
  85. if (!isset($this->gameAllMap)) {
  86. throw new \Exception('赛事类型错误-' . $game_type, 4002);
  87. return false;
  88. }
  89. switch ($game_type) {
  90. case 'bq':
  91. $apd = new BqRule();
  92. break;
  93. case 'lq':
  94. $apd = new LqRule();
  95. break;
  96. case 'wq':
  97. $apd = new WqRule();
  98. break;
  99. case 'zq':
  100. $apd = new ZqRule();
  101. break;
  102. }
  103. return $apd;
  104. }
  105. //冠军比赛结果
  106. public function getGjDatas($matchModel)
  107. {
  108. $table = 'st_' . $matchModel->game_code . '_league_result';
  109. $where = ['lg_id' => $matchModel->lg_id, 'game_name' => $matchModel->game_code];
  110. $model = DB::table($table)->where($where)->first();
  111. if (empty($model)) {
  112. return false;
  113. }
  114. return $model;
  115. }
  116. public function getOddsTypeData($game_code, $match_id)
  117. {
  118. $ret = DB::table('money_buy_match')->select("p_code", "odds_code", "condition", "bet_type")->where(['game_code' => $game_code, 'match_id' => $match_id])->groupBy('p_code', 'odds_code', 'condition', 'bet_type')->get();
  119. return $ret;
  120. }
  121. public function doLogic($matchModel, $result)
  122. {
  123. $game_code = $matchModel->game_code;
  124. $match_id = $matchModel->match_id;
  125. $AdapterObj = $this->getAdapterObj($game_code);
  126. $groupDatas = $this->getOddsTypeData($game_code, $match_id);
  127. $fristRet = $this->getFristRecord($game_code, $match_id, $groupDatas);
  128. $RefClass = new \ReflectionClass($AdapterObj);
  129. $noticeId = $matchModel->id;
  130. $nowtime = date("Y-m-d H:i:s");
  131. $winorfalsedef = ['result' => 2, 'matchResult' => 'noRuleOrError'];
  132. foreach ($fristRet as $matchModel) {
  133. $fun1 = $matchModel->odds_code;
  134. $fun2 = $matchModel->p_code;
  135. try {
  136. if ($RefClass->hasMethod($fun1)) {
  137. $winorfalse = $AdapterObj->$fun1($matchModel, $result, []);
  138. } elseif ($RefClass->hasMethod($fun2)) {
  139. $winorfalse = $AdapterObj->$fun2($matchModel, $result, []);
  140. }
  141. } catch (\Exception $e) {
  142. }
  143. if (!isset($winorfalse['result']) || !isset($winorfalse['matchResult']) || !in_array($winorfalse['result'], [-1, 1, 2, 3, 4])) {
  144. $winorfalse = $winorfalsedef;
  145. }
  146. $win = intval($winorfalse['result']);
  147. $rwin = ($win == 1) ? 1 : ($win == -1 ? 2 : 3);
  148. $matword = $winorfalse['matchResult'];
  149. $odds_code = $fun1;
  150. $p_code = $fun2;
  151. $condition = $matchModel->condition;
  152. $bet_type = $matchModel->bet_type;
  153. DB::update('update money_buy_match set "result"=?,matchresult=? where match_id=? and game_code=? and odds_code=? and p_code=? and condition=?', [$win, $matword, $match_id, $game_code, $odds_code, $p_code, $condition]);
  154. $sql = "select batch_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' ";
  155. if ($bet_type == 1) {
  156. DB::update("update money_buy_simplex set game_status=$rwin where game_code='$game_code' and match_id=$match_id and batch_id in ( $sql)");
  157. } else {
  158. DB::update("update money_buy_simplex set game_status=$rwin where batch_id in ( $sql)");
  159. }
  160. }
  161. DB::update("update comendnotice set status=1,result=1,done_time='$nowtime',pcount=pcount+1 where id=$noticeId ");
  162. return true;
  163. }
  164. //得到每个玩法第一条记录 根据规则计算输赢
  165. public function getFristRecord($game_code, $match_id, $ret)
  166. {
  167. $return = [];
  168. foreach ($ret as $val) {
  169. $p_code = $val->p_code;
  170. $odds_code = $val->odds_code;
  171. $condition = $val->condition;
  172. $ret = DB::table('money_buy_match')->where(['game_code' => $game_code, 'match_id' => $match_id, 'p_code' => $p_code, 'odds_code' => $odds_code, 'condition' => $condition])->first();
  173. $return[] = $ret;
  174. }
  175. return $return;
  176. }
  177. //对单式订单 单个更改过比赛结果的订单进行输赢判断 $orderInfoArray一维数组 $matchArray二维数组
  178. public function WinFailOneOrder($orderInfo, $matchArray, $result)
  179. {
  180. $game_code = $orderInfo->game_code;
  181. $match_id = $orderInfo->match_id;
  182. $order_id = $orderInfo->order_id;
  183. $AdapterObj = $this->getAdapterObj($game_code);
  184. $RefClass = new \ReflectionClass($AdapterObj);
  185. $fun1 = $orderInfo->odds_code;
  186. $fun2 = $orderInfo->p_code;
  187. $result = array_merge(json_decode(json_encode($result), true), json_decode($orderInfo->single_result,true));
  188. $winorfalsedef = ['result' => 2, 'matchResult' => 'noRuleOrError'];
  189. foreach ($matchArray as $matchModel) {
  190. $mid = $matchModel->id;
  191. try {
  192. if ($RefClass->hasMethod($fun1)) {
  193. $winorfalse = $AdapterObj->$fun1($matchModel, $result, []);
  194. } elseif ($RefClass->hasMethod($fun2)) {
  195. $winorfalse = $AdapterObj->$fun2($matchModel, $result, []);
  196. }
  197. } catch (\Exception $e) {
  198. }
  199. if (!isset($winorfalse['result']) || !isset($winorfalse['matchResult']) || !in_array($winorfalse['result'], [-1, 1, 2, 3, 4])) {
  200. $winorfalse = $winorfalsedef;
  201. }
  202. $win = intval($winorfalse['result']);
  203. $rwin = ($win == 1) ? 1 : ($win == -1 ? 2 : 3);
  204. $matword = $winorfalse['matchResult'];
  205. DB::update('update money_buy_match set "result"=?,matchresult=? where match_id=? and game_code=? and id=?', [$win, $matword, $match_id, $game_code, $mid]);
  206. 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 ");
  207. }
  208. return true;
  209. }
  210. }