| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501 |
- <?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);
- }
- //按赛事结算时,存在手动更改赛事结果的问题,如果有手动设置结果,就用手动结果覆盖真实结果
- $this->FormatResult_all($type, $model['0']);
- return $model;
- }
- //格式化比赛结果为兼容以前的格式(主要因为有手动修改结果和自然采集到数据的问題)
- //如果比赛结果表中的 is_correct=1时 手动输入结果覆盖旧字段的数据
- //开始1-->
- public function FormatResult_all($type, $resultObj)
- {
- switch ($type) {
- case 'zq':
- $this->FormatResult_zq($resultObj);
- break;
- case 'lq':
- $this->FormatResult_lq($resultObj);
- break;
- case 'wq':
- $this->FormatResult_wq($resultObj);
- break;
- case 'bq':
- $this->FormatResult_bq($resultObj);
- break;
- default:
- break;
- }
- return;
- }
- public function FormatResult_zq($resultObj)
- {
- if ($resultObj->is_correct != 1) {
- return;
- }
- $handres = json_decode($resultObj->manual_result);
- $resultObj->u_home_score = intval($handres->half->home);
- $resultObj->u_guest_score = intval($handres->half->guest);
- $resultObj->home_score = intval($handres->all->home);
- $resultObj->guest_score = intval($handres->all->guest);
- $tmp = ["home" => intval($handres->all_corner->home), "guest" => intval($handres->all_corner->guest), "home_half" => intval($handres->half_corner->home), "guest_half" => intval($handres->half_corner->home)];
- $resultObj->corner_ball = json_encode($tmp, 256);
- //其它结果因为废弃其玩法,就不用管了
- return;
- }
- public function FormatResult_lq($resultObj)
- {
- if ($resultObj->is_correct != 1) {
- return;
- }
- $resultObj->inning = $resultObj->manual_result;
- return;
- }
- public function FormatResult_wq($resultObj)
- {
- if ($resultObj->is_correct != 1) {
- return;
- }
- $resultObj->inning = $resultObj->manual_result;
- return;
- }
- public function FormatResult_bq($resultObj)
- {
- if ($resultObj->is_correct != 1) {
- return;
- }
- $resultObj->match_score_t = $resultObj->manual_result;
- return;
- }
- //结束1 -->
- //单式订单存在 单独设置比赛结果的情况,要分开处理
- 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;
- }
- //普通单式或串式订单输赢处理
- public function WinFailOneOrderNomal($order_id, $bet_type = 1)
- {
- if ($bet_type == 1) {
- $orderInfo = DB::table('money_buy_simplex')->where('order_id', $order_id)->first();
- } else {
- $orderInfo = DB::table('money_buy_str')->where('order_id', $order_id)->first();
- }
- $matchArray = DB::table('money_buy_match')->where('order_id', $order_id)->get();
- if (empty($orderInfo) || empty($matchArray)) {
- return 0;
- }
- if ($bet_type == 1) {
- $game_code = $orderInfo->game_code;
- $match_id = $orderInfo->match_id;
- $table = 'st_' . $game_code . '_result';
- $result = DB::table($table)->where('match_id', $match_id)->get();
- if (empty($result)) {
- return 0;
- }
- $this->FormatResult_all($orderInfo->game_code, $result['0']);
- }
- $winorfalsedef = ['result' => 2, 'matchResult' => 'noRuleOrError'];
- $chuanSkip = 0; //串式跳过输赢结果处理标识
- foreach ($matchArray as $matchModel) {
- if ($bet_type == 2) {
- $game_code = $matchModel->game_code;
- $match_id = $matchModel->match_id;
- $table = 'st_' . $game_code . '_result';
- $result = DB::table($table)->where('match_id', $match_id)->get();
- if (empty($result)) {
- $chuanSkip = 1;
- continue;
- }
- if ($result['0']->status == 1) {
- //还未结束,暂不用输赢处理
- $chuanSkip = 1;
- continue;
- }
- $this->FormatResult_all($game_code, $result['0']);
- }
- $AdapterObj = $this->getAdapterObj($game_code);
- $RefClass = new \ReflectionClass($AdapterObj);
- $fun1 = $matchModel->odds_code;
- $fun2 = $matchModel->p_code;
- $mid = $matchModel->id;
- if ($result['0']->status == 4) {
- $winorfalse = ['result' => 2, 'matchResult' => '赛事取消和'];
- } else {
- 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);
- if ($bet_type == 2) {
- $rwinarr[] = $rwin;
- }
- $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]);
- }
- if ($bet_type == 1) {
- DB::update("update money_buy_simplex set game_status=$rwin where order_id='$order_id' ");
- } else {
- $rwin = 1;
- if ($chuanSkip) {
- return 1;
- }
- foreach ($rwinarr as $nwin) {
- if ($nwin == 2) {
- $rwin = 3;
- break;
- }
- }
- DB::update("update money_buy_str set game_status=$rwin where order_id='$order_id' ");
- }
- return 1;
- }
- }
|