| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Administrator
- * Date: 2019/7/15
- * Time: 16:51
- */
- namespace App\Logic;
- use App\Models\Account;
- use Illuminate\Database\Capsule\Manager as DB;
- use datainf\pplus\Instance;
- use datainf\lib\GlobConfigs;
- use App\Lib\ModelBase;
- use App\Http\Response\Response;
- use App\Models\SportsNoteList as MoneyBuySimplexModel;
- use App\Models\MoneyBuyStr as MoneyBuyStrModel;
- use App\Models\MoneyBuyMatch as MoneyBuyMatchModel;
- use App\Models\Money_details as MoneyDetailsModel;
- use App\Models\Setinfo as SetinfoModel;
- //按赛事撤销操作(批量优化版)
- class UnSettmatchLogic
- {
- use Instance;
- protected $redis = '';
- private $fanshui = 0.1; //默认返水比例
- protected $game_code = '';
- protected $match_id = 0;
- protected $resultObj = ''; //比赛结果记录对像
- public function doUnsetMatch($request, $response, $datas)
- {
- $match_id = isset($datas['match_id']) ? intval($datas['match_id']) : 0;
- $game_code = isset($datas['game_code']) ? trim($datas['game_code']) : '';
- $result = $this->getResult($game_code, $match_id);
- if (empty($match_id) || empty($game_code) || empty($result) || !in_array($game_code, ['zq', 'lq', 'bq', 'wq'])) {
- $msg = date("Y-m-d H:i:s") . " 取消赛事处理--参数错误!\n";
- 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]!\n";
- $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") . " 正在处理中,不能重复提交!\n";
- $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 \n";
- $this->backandret($response, 1, $msg, $datas, 1);
- DB::beginTransaction();
- try {
- $this->doSimple();
- $this->doStr();
- $this->changeStatus($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 \n";
- $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 = []; //用户资金变化记录,好一次性更新
- foreach ($simples as $item) {
- $gain_money = floatval($item->gain_money);
- $accountIdent = $item->account_identity;
- $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 <= 0.01) {
- $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;
- } 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;
- }
- $mModel->save();
- $item->status = 3;
- $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->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 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)
- {
- $model = new MoneyBuySimplexModel();
- $rets = $model->with('matchdatas')->where(['game_code' => $game_code, 'match_id' => $match_id])->get();
- return $rets;
- }
- //得到 有此赛事的串式订单
- public function getStrOrders($game_code, $match_id)
- {
- $model = new MoneyBuyStrModel();
- $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;
- }
- //获取相关的订单费用及系统赚送的钱(用于返还)
- 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;
- }
- }
|