| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Administrator
- * Date: 2019/7/15
- * Time: 16:51
- */
- namespace App\Logic;
- use Illuminate\Database\Capsule\Manager as DB;
- use App\Lib\Settlement\Adapter\ZqRule;
- use App\Lib\Settlement\Adapter\LqRule;
- use App\Lib\Settlement\Adapter\WqRule;
- use App\Lib\Settlement\Adapter\BqRule;
- use datainf\pplus\Instance;
- class WinfailLogic
- {
- use Instance;
- //类型映射
- public $gameAllMap = [
- 'zq' => 'ZqRule',
- 'lq' => 'LqRule',
- 'wq' => 'WqRule',
- 'bq' => 'BqRule',
- ];
- public function getNoticeDate($id)
- {
- $noticeMode = DB::table('comendnotice')->where(['id' => $id])->first();
- if (empty($noticeMode)) {
- throw new \Exception("没有找到notice记录-" . $id);
- }
- return $noticeMode;
- }
- //r按赛事 全部重计算输赢时,要清除手动设置的结果
- public function Reset_manualData($game_code, $match_id)
- {
- DB::table('money_buy_simplex')->where(['match_id' => $match_id, 'game_code' => $game_code])->update(['is_manual' => 0]);
- }
- //得到比赛最终结果记录
- public function getCompResult($type, $match_id)
- {
- $model = null;
- switch ($type) {
- case 'bq':
- $model = DB::table('st_bq_result')->where('match_id', $match_id)->orderby('id', 'asc')->get();
- break;
- case 'lq':
- $model = DB::table('st_lq_result')->where('match_id', $match_id)->orderby('id', 'asc')->get();
- break;
- case 'wq':
- $model = DB::table('st_wq_result')->where('match_id', $match_id)->orderby('id', 'asc')->get();
- break;
- case 'zq':
- $model = DB::table('st_zq_result')->where('match_id', $match_id)->orderby('id', 'asc')->get();
- break;
- }
- if (empty($model)) {
- throw new \Exception("没有找到赛事结果记录-" . $type . '-' . $match_id);
- }
- return $model;
- }
- //单式订单存在 单独设置比赛结果的情况,要分开处理
- public function getSimplexData($type, $match_id)
- {
- $return = [];
- $ret = DB::table("money_buy_simplex")->where(['game_code' => $type, 'match_id' => $match_id])->get();
- if (count($ret) <= 0) {
- return;
- }
- foreach ($ret as $val) {
- $return[$val->order_id][$val->match_id] = ['result_flag' => $val->result_flag, 'single_result' => $val->single_result];
- }
- return $return;
- }
- //找到订单详细数据
- public function getMatchRecords($type, $match_id, $bet_type)
- {
- $return = [];
- $ret = DB::table("money_buy_match")->where(['game_code' => $type, 'match_id' => $match_id, 'bet_type' => $bet_type])->get();
- if (count($ret) <= 0) {
- return [];
- }
- foreach ($ret as $val) {
- $return[] = $val;
- }
- return $return;
- }
- public function getAdapterObj($game_type)
- {
- $game_type = strtolower($game_type);
- if (!isset($this->gameAllMap)) {
- throw new \Exception('赛事类型错误-' . $game_type, 4002);
- return false;
- }
- switch ($game_type) {
- case 'bq':
- $apd = new BqRule();
- break;
- case 'lq':
- $apd = new LqRule();
- break;
- case 'wq':
- $apd = new WqRule();
- break;
- case 'zq':
- $apd = new ZqRule();
- break;
- }
- return $apd;
- }
- //冠军比赛结果
- public function getGjDatas($matchModel)
- {
- $table = 'st_' . $matchModel->game_code . '_league_result';
- $where = ['lg_id' => $matchModel->lg_id, 'game_name' => $matchModel->game_code];
- $model = DB::table($table)->where($where)->first();
- if (empty($model)) {
- return false;
- }
- return $model;
- }
- //冠军比赛结果
- public function getGjDatasV2($game_code, $lg_id)
- {
- $table = 'st_' . $game_code . '_league_result';
- $where = ['lg_id' => $lg_id, 'game_name' => $game_code];
- $model = DB::table($table)->where($where)->first();
- if (empty($model)) {
- return false;
- }
- return $model;
- }
- public function getOddsTypeData($game_code, $match_id)
- {
- $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();
- return $ret;
- }
- //胜负判断 NotiematchModel赛事结束通知model $result普通赛事结果对像数组[冠军赛事下可能为空] $GJresult冠军赛事结果对像数组[非冠军赛事为空]
- public function doLogic($NotiematchModel, $result, $GJresult)
- {
- $game_code = $NotiematchModel->game_code;
- $match_id = $NotiematchModel->match_id;
- $AdapterObj = $this->getAdapterObj($game_code);
- $groupDatas = $this->getOddsTypeData($game_code, $match_id);
- $fristRet = $this->getFristRecord($game_code, $match_id, $groupDatas);
- $RefClass = new \ReflectionClass($AdapterObj);
- $noticeId = $NotiematchModel->id;
- $nowtime = date("Y-m-d H:i:s");
- $winorfalsedef = ['result' => 2, 'matchResult' => 'noRuleOrError'];
- foreach ($fristRet as $matchModel) {
- $is_champion = $matchModel->is_champion;
- $fun1 = $matchModel->odds_code;
- $fun2 = $matchModel->p_code;
- if ($is_champion == 0) {
- //普通赛事
- $resultTypeNow = $result;
- if (empty($resultTypeNow)) {
- throw new \Exception("赛事结果不能为空! --lg_id=" . $matchModel->game_code . '_' . $matchModel->lg_id);
- }
- } else {
- //冠军赛事;
- $resultTypeNow = $GJresult;
- if (empty($resultTypeNow)) {
- throw new \Exception("冠军赛事结果不能为空! --lg_id=" . $matchModel->game_code . '_' . $matchModel->lg_id);
- }
- }
- try {
- if ($RefClass->hasMethod($fun1)) {
- $winorfalse = $AdapterObj->$fun1($matchModel, $resultTypeNow, []);
- } elseif ($RefClass->hasMethod($fun2)) {
- $winorfalse = $AdapterObj->$fun2($matchModel, $resultTypeNow, []);
- }
- } catch (\Exception $e) {
- }
- if (!isset($winorfalse['result']) || !isset($winorfalse['matchResult']) || !in_array($winorfalse['result'], [-1, 1, 2, 3, 4])) {
- $winorfalse = $winorfalsedef;
- }
- $win = intval($winorfalse['result']);
- $rwin = ($win == 1) ? 1 : ($win == -1 ? 2 : 3);
- $matword = $winorfalse['matchResult'];
- $odds_code = $fun1;
- $p_code = $fun2;
- $condition = $matchModel->condition;
- $bet_type = $matchModel->bet_type;
- 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]);
- $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 ";
- if ($bet_type == 1) {
- 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)");
- } else {
- DB::update("update money_buy_str set game_status=$rwin where order_id in ( $sql)");
- }
- }
- DB::update("update comendnotice set status=1,result=1,done_time='$nowtime',pcount=pcount+1 where id=$noticeId ");
- return true;
- }
- //得到每个玩法第一条记录 根据规则计算输赢
- public function getFristRecord($game_code, $match_id, $ret)
- {
- $return = [];
- foreach ($ret as $val) {
- $p_code = $val->p_code;
- $odds_code = $val->odds_code;
- $condition = $val->condition;
- $is_champion = $val->is_champion;
- $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();
- $return[] = $ret;
- }
- return $return;
- }
- //对单式订单 单个更改过比赛结果的订单进行输赢判断 $orderInfoArray一维数组 $matchArray二维数组
- public function WinFailOneOrder($orderInfo, $matchArray, $is_champion)
- {
- $game_code = $orderInfo->game_code;
- $match_id = $orderInfo->match_id;
- $order_id = $orderInfo->order_id;
- $AdapterObj = $this->getAdapterObj($game_code);
- $RefClass = new \ReflectionClass($AdapterObj);
- $result = json_decode($orderInfo->single_result);
- if ($is_champion == 0) {
- //还原回原来的类型,以兼容之前的处理
- switch (strtolower($orderInfo->game_code)) {
- case 'zq':
- $result['0']->first_score = json_encode($result['0']->first_score, 256);
- $result['0']->corner_ball = json_encode($result['0']->corner_ball, 256);
- $result['0']->penalty_card = json_encode($result['0']->penalty_card, 256);
- break;
- case 'lq':
- $result['0']->home_score = json_encode($result['0']->home_score, 256);
- $result['0']->guest_score = json_encode($result['0']->guest_score, 256);
- break;
- case 'wq':
- $result['0']->inning = json_encode($result['0']->inning, 256);
- break;
- case 'bq':
- $result['0']->match_score_t = json_encode($result['0']->match_score_t, 256);
- break;
- }
- $result['0']->warn_more = json_encode($result['0']->warn_more, 256);
- }
- $winorfalsedef = ['result' => 2, 'matchResult' => 'noRuleOrError'];
- foreach ($matchArray as $matchModel) {
- $fun1 = $matchModel->odds_code;
- $fun2 = $matchModel->p_code;
- $mid = $matchModel->id;
- try {
- if ($RefClass->hasMethod($fun1)) {
- $winorfalse = $AdapterObj->$fun1($matchModel, $result, []);
- } elseif ($RefClass->hasMethod($fun2)) {
- $winorfalse = $AdapterObj->$fun2($matchModel, $result, []);
- }
- } catch (\Exception $e) {
- echo 'excetption: ' . print_r([$order_id, $e->getMessage(), $e->getFile(), $e->getLine()], true) . "\n";
- }
- if (!isset($winorfalse['result']) || !isset($winorfalse['matchResult']) || !in_array($winorfalse['result'], [-1, 1, 2, 3, 4])) {
- $winorfalse = $winorfalsedef;
- }
- $win = intval($winorfalse['result']);
- $rwin = ($win == 1) ? 1 : ($win == -1 ? 2 : 3);
- $matword = $winorfalse['matchResult'];
- DB::update('update money_buy_match set "result"=?,matchresult=? where match_id=? and game_code=? and id=?', [$win, $matword, $match_id, $game_code, $mid]);
- 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' ");
- }
- return true;
- }
- }
|