getResult($game_code, $match_id); if (empty($match_id) || empty($game_code) || empty($result)) { PARAEORROR: $msg = date("Y-m-d H:i:s") . " 取消赛事处理--参数错误!"; unset($datas['token']); $this->backandret($response, 0, $msg, $datas); return false; } $this->game_code = $game_code; $this->match_id = $match_id; $this->resultObj = $request; if (!in_array($result->status, [3])) { unset($datas['token']); $msg = date("Y-m-d H:i:s") . " 取消赛事处理--数据状态值不为[3]!"; $this->backandret($response, 0, $msg, $datas); return false; } if ($this->optFlag($game_code, $match_id, 2)) { unset($datas['token']); $msg = date("Y-m-d H:i:s") . " 正在处理中,不能重复提交!"; $this->backandret($response, 0, $msg, $datas); return false; } $this->getFanshui(); $this->optFlag($game_code, $match_id, 1); $msg = date("Y-m-d H:i:s") . " 取消赛事处理--开始begin:game_code= $game_code match_id= $match_id "; $this->backandret($response, 1, $msg, $datas, 1); DB::beginTransaction(); try { $this->doSimple(); $this->doStr(); $this->changeStatus($game_code, $match_id); $this->changeMatchResult($game_code, $match_id); DB::commit(); } catch (\Exception $e) { DB::rollBack(); unset($datas['token']); $this->optFlag($game_code, $match_id, 0); $msg = date("Y-m-d H:i:s") . " 取消赛事处理--异常结束:game_code= $game_code match_id= $match_id Emsg: " . $e->getMessage(); $this->backandret($response, 0, '撤单失败!' . $msg, $datas); return; } $this->optFlag($game_code, $match_id, 0); $msg = date("Y-m-d H:i:s") . " 取消赛事处理--结束end:game_code= $game_code match_id= $match_id "; $this->backandret($response, 1, '撤销成功!' . $msg, []); return; } //撤销单个订单 public function doUnsetMatchOneOrder($request, $response, $datas) { $match_id = isset($datas['match_id']) ? intval($datas['match_id']) : 0; $game_code = isset($datas['game_code']) ? trim($datas['game_code']) : ''; $order_id = isset($datas['order_id']) ? trim($datas['order_id']) : 0; $token = isset($datas['token']) ? trim($datas['token']) : ''; if (!in_array($game_code, ['zq', 'lq', 'bq', 'wq'])) { goto PARAEORROR; } $result = $this->getResult($game_code, $match_id); if (empty($match_id) || empty($game_code) || empty($result) || empty($order_id) || empty($token)) { PARAEORROR: $msg = date("Y-m-d H:i:s") . " 取消赛事处理--参数错误(参数错或为空了)!"; unset($datas['token']); $this->backandret($response, 0, $msg, $datas); return false; } $bet_type = 1; $order_main = $this->getSimpleOrders($game_code, $match_id, $order_id); if (empty($order_main)) { $order_main = $this->getStrOrders($game_code, $match_id, $order_id); $bet_type = 2; } if (empty($order_main) || empty($order_main->matchdatas)) { goto PARAEORROR; return; } if ($order_main->status == 2) { $msg = date("Y-m-d H:i:s") . " 已撤销过的订单(不用处理)!--end:game_code= $game_code match_id= $match_id order_id=$order_id "; $this->backandret($response, 1, '撤销成功!' . $msg, []); return; } foreach ($order_main->matchdatas as $matitem) { if ($matitem->order_id == $order_id) { $matitem->result = 2; $matitem->matchresult = '手动订单取消(平局)'; $matitem->save(); } } $dataArr = [ 'game_code' => $game_code, 'match_id' => $match_id, 'settype' => 2, 'bettype' => $bet_type, 'change_status' => 0, 'order_ids' => $order_id, 'token' => $token, ]; $url = 'http://127.0.0.1:9094/Settelement'; $ret_str = post_curls($url, $dataArr); if (empty($ret_str)) { goto ERRORCURL; } else { $ret = json_decode($ret_str, true); if (empty($ret) || !isset($ret['status']) || $ret['status'] != 1) { goto ERRORCURL; } else { $order_main->status = 2; $order_main->settlement_time = date('Y-m-d H:i:s'); $order_main->use_mark = '注单所属赛事作废(作废)'; $order_main->save(); goto SUCCESSCUL; } } ERRORCURL: $msg = date("Y-m-d H:i:s") . " 取消赛事处理--end:game_code= $game_code match_id= $match_id .$ret_str "; $this->backandret($response, 0, '撤销失败!' . $msg, []); return; SUCCESSCUL: $msg = date("Y-m-d H:i:s") . " 取消赛事处理--end:game_code= $game_code match_id= $match_id order_id=$order_id "; $this->backandret($response, 1, '撤销成功!' . $msg, []); return; } //返回消息和写日志 public function backandret($response, $code, $msg, $data, $onlylog = 0) { $dataRet = Response::generate('', $code, $data, $msg); echo $dataRet . "\n"; if ($onlylog) { return; } $response->end($dataRet); return; } //得到返水的比例 public function getFanshui() { $ret = (new SetinfoModel())->getInfo(1001); if (is_array($ret)) { $val = floatval($ret->infocontent); $this->fanshui = $val; return $val; } } //处理单式订单相关逻辑 public function doSimple() { $simples = $this->getSimpleOrders($this->game_code, $this->match_id); if (empty($simples)) { return; } $useraccs = []; foreach ($simples as $item) { if (!isset($useraccs[$item->account_identity])) { $useraccs[$item->account_identity] = $item->account_identity; } } //关联用户 $userInents = $this->getAccountIdents($useraccs); //用户资金变化记录,以后好一次性更新用户余额 $time = date("Y-m-d H:i:s"); $userMoneyChange = []; //用户资金变化记录,好一次性更新 $userMoneyChange_less = []; //用户余额情况 $orders_array = []; foreach ($simples as $item) { $gain_money = floatval($item->gain_money); $accountIdent = $item->account_identity; $orders_array[] = $item->order_id; $mModel = new MoneyDetailsModel(); $mModel->info_identity = UUID(); $mModel->trade_id = $item->order_id; $mModel->account_name = $userInents[$accountIdent]->account; $mModel->account_identity = $accountIdent; $mModel->money_time = $time; $mModel->money_type = 3; $mModel->trade_type = 3; $mModel->trade_desc = '赛事取消返款--matchid=' . $this->match_id; if ($gain_money * 100 <= 1) { $return_money = $item->money * (1 - $this->fanshui); $mModel->money_type = 1; $mModel->money = $return_money; $userMoneyChange[$accountIdent] = isset($userMoneyChange[$accountIdent]) ? ($userMoneyChange[$accountIdent] + $return_money) : $return_money; $userMoneyChange_less[$accountIdent] = !isset($userMoneyChange_less[$accountIdent]) ? $useraccs[$item->account_identity]->cash + $userMoneyChange[$accountIdent] : $userMoneyChange_less[$accountIdent] + $userMoneyChange[$accountIdent]; } else { $return_money = $gain_money - $item->money * (1 - $this->fanshui); $mModel->money_type = 2; $mModel->money = $return_money; $userMoneyChange[$accountIdent] = isset($userMoneyChange[$accountIdent]) ? ($userMoneyChange[$accountIdent] - $return_money) : -1 * $return_money; $userMoneyChange_less[$accountIdent] = !isset($userMoneyChange_less[$accountIdent]) ? $useraccs[$item->account_identity]->cash + $userMoneyChange[$accountIdent] : $userMoneyChange_less[$accountIdent] - $return_money; } $mModel->money_cash = floatval($userMoneyChange_less[$accountIdent]); $mModel->save(); $item->status = 3; $item->settlement_time = date('Y-m-d H:i:s'); $item->use_mark = '通过设置赛事比分作废!'; $item->save(); } if ($userMoneyChange) { foreach ($userMoneyChange as $uident => $money) { if (abs($money) > 0) { $userInents[$uident]->detail->available_cash = $userInents[$uident]->detail->available_cash + $money; $userInents[$uident]->detail->cash = $userInents[$uident]->detail->cash + $money; $userInents[$uident]->detail->save(); } } } return true; } //处理串式订单相关逻辑 public function doStr() { $strOrders = $this->getStrOrders($this->game_code, $this->match_id); if (empty($strOrders)) { return; } //这里只把结果改为平局,再提交到旧的重新结算订单接口处理 foreach ($strOrders as $item2) { $item2->status = 2; $item2->settlement_time = date('Y-m-d H:i:s'); $item2->save(); $mats = $item2->matchdatas; foreach ($mats as $item3) { $item3->result = 2; $item3->matchresult = '通过设置赛事比分作废!'; $item3->save(); } } $dataArr = [ 'game_code' => $this->game_code, 'match_id' => $this->match_id, 'settype' => 2, 'bettype' => 2, 'change_status' => 0, 'order_ids' => '', ]; $url = 'http://127.0.0.1:9094/Settelement'; post_curls($url, $dataArr); /* foreach ($strOrders as $item) { $dataArr['order_ids'] = $item->order_id; post_curls($url, $dataArr); } */ return; } //得到某个账户的信息,主要是余额情况 public function getAccountIdents($account_identity_array) { $model = new Account(); $ret = []; $return = $model->with('detail')->whereIn('identity', $account_identity_array)->get(); if ($return) { foreach ($return as $item) { $ret[$item->identity] = $item; } } return $ret; } public function changeStatus($game_code, $match_id) { $table = 'st_' . $game_code . '_result'; $ret = DB::table($table)->where('match_id', $match_id)->update(['status' => 4]); return $ret; } public function changeMatchResult($game_code, $match_id, $order_id = 0) { $table = 'money_buy_match'; if (empty($order_id)) { $ret = DB::table($table)->where('match_id', $match_id)->where('game_code', $game_code)->update(['result' => 2, 'matchresult' => '撤单平局!']); } else { $ret = DB::table($table)->where([['match_id', '=', $match_id], ['game_code', '=', $game_code], ['order_id', '=', $order_id]])->update(['result' => 2, 'matchresult' => '撤单平局.']); } return $ret; } //得到比赛结果 ,看是不已结算等信息 public function getResult($game_code, $match_id) { $table = 'st_' . $game_code . '_result'; $ret = DB::table($table)->where('match_id', $match_id)->first(); return $ret; } //得到 有此赛事的单式订单 public function getSimpleOrders($game_code, $match_id, $order_id = 0) { $model = new MoneyBuySimplexModel(); if (empty($order_id)) { $rets = $model->with('matchdatas')->where(['game_code' => $game_code, 'match_id' => $match_id])->get(); } else { $rets = $model->with('matchdatas')->where(['game_code' => $game_code, 'match_id' => $match_id, 'order_id' => $order_id])->first(); } return $rets; } //得到 有此赛事的串式订单 public function getStrOrders($game_code, $match_id, $order_id = 0) { $model = new MoneyBuyStrModel(); if (empty($order_id)) { $model2 = new MoneyBuyMatchModel(); $rets2 = $model2->getByAttrs([['match_id', '=', $match_id], ['bet_type', '=', 2], ['game_code', '=', $game_code]]); $orders = []; if ($rets2) { foreach ($rets2 as $item) { $orders[] = $item->order_id; } } else { return []; } $rets = $model->with('matchdatas')->whereIn('order_id', $orders)->get(); return $rets; } $rets = $model->with('matchdatas')->where('order_id', $order_id)->first(); return $rets; } //获取相关的订单费用及系统赚送的钱(用于返还) public function getMoneyDetail($ordesArray) { $model = new MoneyDetailsModel(); $ret = $model->whereIn('trace_id', $ordesArray)->get(); return $ret; } //设置操作标志位,防止事务未处理完,再次提交的情况 //opt 0删除 1设置 2查询 public function optFlag($game_code, $match_id, $opt = 2) { $key = 'Unsetmatch:' . $game_code . '_' . $match_id; $redis = $this->getRedis(); if ($opt == 0) { $redis->del($key); return true; } if ($opt == 1) { $redis->setex($key, 300, time()); return; } if ($opt == 2) { $ret = $redis->get($key); return $ret; } } public function getRedis() { if ($this->redis) { return $this->redis; } $config = GlobConfigs::getKey('redis'); $redis = new \Redis(); $redis->connect($config['host'], $config['port']); $redis->auth($config['passwd']); $redis->select($config['db']); $this->redis = $redis; return $redis; } }