| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223 |
- <?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;
- }
- //得到比赛最终结果记录
- 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->batch_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 getOddsTypeData($game_code, $match_id)
- {
- $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();
- return $ret;
- }
- public function doLogic($matchModel, $result)
- {
- $game_code = $matchModel->game_code;
- $match_id = $matchModel->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);
- $winorfalsedef = ['result' => 2, 'matchResult' => 'noRuleOrError'];
- foreach ($fristRet as $matchModel) {
- $fun1 = $matchModel->odds_code;
- $fun2 = $matchModel->p_code;
- try {
- if ($RefClass->hasMethod($fun1)) {
- $winorfalse = $AdapterObj->$fun1($matchModel, $result, []);
- } elseif ($RefClass->hasMethod($fun2)) {
- $winorfalse = $AdapterObj->$fun2($matchModel, $result, []);
- }
- } 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=?', [$win, $matword, $match_id, $game_code, $odds_code, $p_code, $condition]);
- $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' ";
- 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 batch_id in ( $sql)");
- } else {
- DB::update("update money_buy_simplex set game_status=$rwin where batch_id in ( $sql)");
- }
- }
- 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;
- $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();
- $return[] = $ret;
- }
- return $return;
- }
- public function get_orderIds($bet_type, $game_code, $match_id)
- {
- if ($bet_type == 1) {
- $table = 'money_buy_simplex';
- $ret = DB::table($table)->select('order_id')->where(['game_code' => $game_code, 'match_id' => $match_id])->get();
- } else {
- $table = 'money_buy_str';
- $ret = DB::table($table)->select('order_id')->whereRaw(" batch_id in ( select batch_id from money_buy_match where match_id=$match_id and game_code='$game_code' and bet_type=2 )")->get();
- }
- $return = [];
- if ($ret) {
- foreach ($ret as $val) {
- $return[] = $val->order_id;
- }
- }
- return implode(',', $return);
- }
- }
|