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; if (empty($match_id) || empty($game_code) || empty($order_id) || !in_array($game_code, ['zq', 'lq', 'bq', 'wq'])) { 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 != 1) { $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->match_id == $match_id) { $matitem->result = 0; $matitem->matchresult = ''; $matitem->save(); } } $this->CancelOneOrder($order_main); DB::table('settlement_middle_detail')->where('order_id', $order_id)->update(['money' => 0]); $order_main->status = 2; $order_main->settlement_time = date('Y-m-d H:i:s'); $order_main->use_mark = ''; $order_main->game_status = 0; $order_main->settle_status = 1; $order_main->gain_money = 0; $order_main->save(); $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 RestartStringOneOrder($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; if (empty($match_id) || empty($game_code) || empty($order_id) || !in_array($game_code, ['zq', 'lq', 'bq', 'wq'])) { PARAEORROR: $msg = date("Y-m-d H:i:s") . " 取消赛事处理--参数错误(参数错或为空了)!"; unset($datas['token']); $this->backandret($response, 0, $msg, $datas); return false; } $OrderObj = $order_main = $this->getStrOrders($game_code, $match_id, $order_id); if (empty($order_main) || empty($order_main->matchdatas)) { goto PARAEORROR; return; } if ($order_main->status == 1) { $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; } $order_money = $order_main->money; $fanshuimodel = DB::table('setinfo')->where('identity', '1000')->first(); $fanshui = floatval(trim($fanshuimodel->infocontent)); $money = $order_money * (1 - $fanshui); $Accobj = (new AccountModel())->with('detail')->where('identity', $order_main->account_identity)->first(); $moneyDetail = new MoneyDetailsModel(); $moneyDetail->info_identity = UUID(); $moneyDetail->trade_id = $OrderObj->order_id; $moneyDetail->account_name = $Accobj->account; $moneyDetail->account_identity = $OrderObj->account_identity; $moneyDetail->money = abs($money); $moneyDetail->money_time = date("Y-m-d H:i:s"); $moneyDetail->money_type = 2; $moneyDetail->money_cash = $Accobj->detail->cash - $money; $moneyDetail->trade_type = 11; $moneyDetail->trade_desc = '由撤销转为下单状态'; $moneyDetail->reason = '由撤销转为下单状态'; $moneyDetail->status = 1; $moneyDetail->sysetem_user = 'system'; $moneyDetail->out_order_id = $OrderObj->order_id; $moneyDetail->remark = '由撤销转为下单状态'; $ret = $moneyDetail->save(); $nowmoney = $Accobj->detail->cash - $money; DB::table('account_detailed')->where('account_identity', $OrderObj->account_identity)->update(['available_cash' => $nowmoney, 'cash' => $nowmoney]); $order_main->status = 1; $order_main->settlement_time = date('Y-m-d H:i:s'); $order_main->use_mark = ''; $order_main->game_status = 0; $order_main->settle_status = 1; $order_main->gain_money = 0; $order_main->save(); $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 CancelOneOrder($OrderObj) { //撤单时应该为用户加钱还是扣钱 >0加钱 <0扣钱 ==0不用操作 $money = $this->CountDetailsTounSet($OrderObj->order_id); if ($money == 0) { return true; } $Accobj = (new AccountModel())->with('detail')->where('identity', $OrderObj->account_identity)->first(); $moneyDetail = new MoneyDetailsModel(); $moneyDetail->info_identity = UUID(); $moneyDetail->trade_id = $OrderObj->order_id; $moneyDetail->account_name = $Accobj->account; $moneyDetail->account_identity = $OrderObj->account_identity; $moneyDetail->money = abs($money); $moneyDetail->money_time = date("Y-m-d H:i:s"); $moneyDetail->money_type = $money > 0 ? 1 : 2; $moneyDetail->money_cash = $Accobj->detail->cash + $money; $moneyDetail->trade_type = 11; $moneyDetail->trade_desc = '撤单资金平账处理'; $moneyDetail->reason = '撤单资金平账处理'; $moneyDetail->status = 1; $moneyDetail->sysetem_user = 'system'; $moneyDetail->out_order_id = $OrderObj->order_id; $moneyDetail->remark = '撤单资金平账处理'; $ret = $moneyDetail->save(); $nowmoney = $Accobj->detail->cash + $money; DB::update('account_detail')->where('account_identity', $OrderObj->account_identity)->update(['money_cash' => $nowmoney, 'cash' => $nowmoney]); return $ret; } //统计流水表中 所有此订单的 出入总计,用于撤销订单使用 public function CountDetailsTounSet($order_id) { $sql = "select sum(money) as msum,money_type from money_details where trade_id='$order_id' group by money_type "; $rets = DB::select($sql); $ulose = $uadd = 0; foreach ($rets as $val) { if ($val->money_type == 1) { $uadd = $val->msum; } if ($val->money_type == 2) { $ulose = $val->msum; } } $return = $ulose - $uadd; return $return; } //撤销串式订单(整个订单撤销) public function UnSettelementOneStringOrder($request, $response, $datas) { return $this->doUnsetMatchOneOrder($request, $response, $datas); } //返回消息和写日志 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]) ? $userInents[$item->account_identity]->detail->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]) ? $userInents[$item->account_identity]->detail->cash + $userMoneyChange[$accountIdent] : $userMoneyChange_less[$accountIdent] - $return_money; } $mModel->money_cash = floatval($userMoneyChange_less[$accountIdent]); $mModel->save(); $item->status = 2; $item->gain_money = 0; $item->game_status = 2; $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; } $userMoneyArr = []; //这里只把结果改为平局,再提交到旧的重新结算订单接口处理 foreach ($strOrders as $item2) { //如果存在中奖的,要先扣回赢的钱,再以平处理 if ($item2->gain_money > 0) { $acc = $item2->account_indentity; $nowmoney = $item2->gain_money; $userMoneyArr[$acc] = isset($userMoneyArr[$acc]) ? $userMoneyArr[$acc] + $nowmoney : $nowmoney; } $item2->status = 2; $item2->gain_money = 0; $item2->game_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(); } } if ($userMoneyArr) { $this->AccountMoneydec($userMoneyArr); } $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 AccountMoneydec($account_identity_array) { foreach ($account_identity_array as $acc => $money) { $accarr[] = $acc; } $objs = $this->etAccountIdents($accarr); foreach ($account_identity_array as $acc2 => $money) { $nowmoney = $objs[$acc2]->detail->available_cash - $money; $objs[$acc2]->detail->available_cash = $nowmoney; $objs[$acc2]->detail->cash = $nowmoney; $objs[$acc2]->detail->save(); } return true; } //得到某个账户的信息,主要是余额情况 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 getByOrderString($order_id) { /* $ret = []; $model2 = new MoneyBuyMatchModel(); $rets2 = $model2->getByAttrs([['order_ID', '=', $order_id]]); if (empty($rets2)) { return $ret; } */ $model = new MoneyBuyStrModel(); $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; } }