Ver Fonte

异常处理

彭俊 há 6 anos atrás
pai
commit
34de1d498b

+ 100 - 0
app/Http/Controllers/Api/CrontabController.php

@@ -0,0 +1,100 @@
+<?php
+
+namespace App\Http\Controllers\Api;
+
+use Illuminate\Routing\Controller as BaseController;
+use App\Http\Response\Response;
+use Illuminate\Support\Facades\DB;
+use App\Lib\Biz\Sport\Common as commonFunction;
+use App\Http\Model\StZqMatch as StMatchModel;
+use App\Models\SportsNoteList as SportsNoteListModel;
+
+/**
+ * 定时处理任务
+ * Tank
+ * 2019/10/31
+ */
+class CrontabController extends BaseController{
+    //是否启用 数据写入记录
+    protected $isRecord;
+
+    public function __construct()
+    {
+        $this->isRecord = config('record.isRecord');
+    }
+
+    /**
+     * 定时执行 滚球投注危险球审核
+     * 每分钟执行一次
+     */
+    public function HandleOrder(){
+        try {
+            //开启事务
+            DB::beginTransaction();
+            $time =90;
+            //处理指定时间内的滚球待审核订单
+            SportsNoteListModel::getOrderData($time);
+            //提交事务
+            DB::commit();
+            //写请求数据 日志记录
+            if ($this->isRecord) commonFunction::SET_SPORTS_RECORD('zq', [], 'HandleOrder', Response::success());
+            return Response::success();
+        } catch (\Exception $e) {
+            //回滚事务
+            DB::rollBack();
+            //写请求数据 日志记录
+            if ($this->isRecord) commonFunction::SET_SPORTS_RECORD('zq', [], 'HandleOrder', $e->getMessage());
+            return $e->getMessage();
+        }
+
+    }
+
+    /**
+     * 定时执行 处理到时间应该开始而未开始的赛事
+     * 每分钟执行一次
+     */
+    public function HandleMatch(){
+        try {
+            //开启事务
+            DB::beginTransaction();
+            $time = 60*5;//60 秒
+            //处理指定时间内的赛事
+            StMatchModel::HandleMatch($time);
+            //提交事务
+            DB::commit();
+            //写请求数据 日志记录
+            if ($this->isRecord) commonFunction::SET_SPORTS_RECORD('all', [], 'HandleMatch', Response::success());
+            return Response::success();
+        } catch (\Exception $e) {
+            //回滚事务
+            DB::rollBack();
+            //写请求数据 日志记录
+            if ($this->isRecord) commonFunction::SET_SPORTS_RECORD('all', [], 'HandleMatch', $e->getMessage());
+            return $e->getMessage();
+        }
+    }
+
+    /**
+     * 定时执行 处理时间段内赛事已取消的注单
+     */
+    public function HandleOrderInvalid(){
+        try {
+            //开启事务
+            DB::beginTransaction();
+            $time = 60*30;//5分钟
+            //处理指定时间内已取消赛事的注单
+            SportsNoteListModel::HandleOrderInvalid($time);
+            //提交事务
+            DB::commit();
+            //写请求数据 日志记录
+            if ($this->isRecord) commonFunction::SET_SPORTS_RECORD('all', [], 'HandleOrderInvalid', Response::success());
+            return Response::success();
+        } catch (\Exception $e) {
+            //回滚事务
+            DB::rollBack();
+            //写请求数据 日志记录
+            if ($this->isRecord) commonFunction::SET_SPORTS_RECORD('all', [], 'HandleOrderInvalid', $e->getMessage());
+            return $e->getMessage();
+        }
+    }
+}

+ 1 - 1
app/Http/Controllers/Api/WriteSportsController.php

@@ -439,7 +439,7 @@ class WriteSportsController extends BaseController
                             //===追加查询 该赛事 本地是否存在 同联赛同球队 记录
                             //查询赛事 如果存在 则更新为 已取消
                             $matchData = $models['model_match']::where(['home_team' => $data['home_team'],'guest_team' => $data['guest_team'],'identity'=>$data['uuid']])
-                                ->update(['status'=>6,'handle_mark'=>'重复赛事取消']);
+                                ->update(['status'=>6,'utime'=>date('Y-m-d H:i:s'),'handle_mark'=>'重复赛事取消']);
                             //===end===
                             $half_match_id = $v['half_match_id'];
                             //获取 本地 联赛 ID

+ 1 - 0
app/Http/Middleware/VerifyCsrfToken.php

@@ -26,6 +26,7 @@ class VerifyCsrfToken extends Middleware
         'setMatchWarn',
         'HandleOrder',
         'HandleMatch',
+        'HandleOrderInvalid',
         //'upImg',
         //
     ];

+ 2 - 2
app/Http/Model/StZqMatch.php

@@ -35,8 +35,8 @@ class StZqMatch extends Model
      */
     public static function HandleMatch($time = 60,$time_sec = 100){
         //拼接查询条件
-        $time_unx_max = time() - $time;//查询最大时间 时间戳
-        $time_unx_min = $time_unx_max - $time_sec;//查询最小时间 时间戳
+        $time_unx_max = time() - $time;//查询最大时间 时间戳 5分钟前
+        $time_unx_min = $time_unx_max - $time;//查询最小时间 时间戳 十分钟前
 
         $where = [
             ['match_date','=',date('Y-m-d',$time_unx_min)],

+ 10 - 1
app/Models/MoneyBuyMatch.php

@@ -27,8 +27,9 @@ class MoneyBuyMatch extends Model
     }
 
     //赛事下所有下注单式下单
-    function allsimplexorder($ssid='', $code='',$select=[])
+    function allsimplexorder($ssid='', $code='',$select=[],$match_ids = [])
     {
+
         //单式下单
         $where = array();
         $where[] = array('money_buy_match.match_id', '=', $ssid);
@@ -36,8 +37,16 @@ class MoneyBuyMatch extends Model
         $where[] = array('money_buy_match.game_code', '=', $code);
 
         if(empty($select)){
+            //一个赛事所有注单
             $select = ['money_buy_simplex.order_id', 'money_buy_simplex.money', 'money_buy_simplex.account_identity', 'money_buy_simplex.info_identity', 'money_buy_simplex.match_id'];
             $data = $this->join('money_buy_simplex', 'money_buy_match.batch_id', '=', 'money_buy_simplex.batch_id')->select($select)->where($where)->get()->toArray();
+        }else if(!empty($match_ids) and !empty($select)){
+            //多赛事所有注单
+            $typeWhere = [
+                ['money_buy_match.bet_type', '=', 1],
+                ['money_buy_match.game_code', '=', $code],
+            ];
+            $data = $this->join('money_buy_simplex', 'money_buy_match.batch_id', '=', 'money_buy_simplex.batch_id')->select($select)->whereIn('money_buy_match.match_id',$match_ids)->where($typeWhere)->get();
         }else{
             $data = $this->join('money_buy_simplex', 'money_buy_match.batch_id', '=', 'money_buy_simplex.batch_id')->join('st_odds_code', 'money_buy_match.odds_code', '=', 'st_odds_code.odds_code')->select($select)->where($where)->get();
         }

+ 146 - 29
app/Models/SportsNoteList.php

@@ -14,6 +14,11 @@ use App\Models\MoneyBuyMatchb;
 use App\Http\Response\Response;
 use App\Http\Model\StZqResult;
 
+use App\Http\Model\StZqMatch as ZqMatchModel;
+use App\Http\Model\StLqMatch as LqMatchModel;
+use App\Http\Model\StWqMatch as WqMatchModel;
+use App\Http\Model\StBqMatch as BqMatchModel;
+
 
 class SportsNoteList extends BaseModel
 {
@@ -1190,7 +1195,6 @@ class SportsNoteList extends BaseModel
     }
     public static function getOrderData($time=60){
 
-        $time = 100;
         $db = new \App\Models\Setinfo();
         //获取设定需审核时间 秒
         $handle_time =  $db->getInfo(1003)['infocontent']?:90;
@@ -1370,9 +1374,9 @@ class SportsNoteList extends BaseModel
     /**
      * 滚球自动审核 账户资金处理
      * v2 
-     * 处理一个用户下 多个注单退款时的 余额计算
+     * 处理用户下 多个注单退款时的 余额计算
      */
-    public static function HandleMoney_v2($account_identitys=[],$order_ids=[],$orders=[]){
+    public static function HandleMoney_v2($account_identitys=[],$order_ids=[],$orders=[],$type = 0){
        
         //获取  用户 初始余额
         $account_money = \App\Models\Account_detailed::whereIn('account_identity',$account_identitys)->SELECT('account_identity','available_cash')->get()->toArray();
@@ -1408,37 +1412,74 @@ class SportsNoteList extends BaseModel
                 }
             }
 
-            foreach($orders as $k=>$v){
-                 //需退回金额 = 投注金额-反水金额
-                $money = abs($v['money'] - $v['fanshui']);
-                 //用户剩余金额
-                $new_available_cash = $v['available_cash'] + $money;
-                $models = new \App\Models\Money_details();
-                $models->info_identity = UUID();
-                $models->trade_id = $v['order_id'];
-                $models->account_name = $v['account_name'];
-                $models->account_identity = $v['account_identity'];
-                $models->money = $money;
-                $models->money_time = date("Y-m-d H:i:s", time());
-                $models->money_type = '1';
+            //滚球注单审核 处理
+            if($type == 0){
+                foreach($orders as $k=>$v){
+                    //需退回金额 = 投注金额-反水金额
+                    $money = abs($v['money'] - $v['fanshui']);
+                    //用户剩余金额
+                    $new_available_cash = $v['available_cash'] + $money;
+                    $models = new \App\Models\Money_details();
+                    $models->info_identity = UUID();
+                    $models->trade_id = $v['order_id'];
+                    $models->account_name = $v['account_name'];
+                    $models->account_identity = $v['account_identity'];
+                    $models->money = $money;
+                    $models->money_time = date("Y-m-d H:i:s", time());
+                    $models->money_type = '1';
+                    
+                    $models->money_cash = $new_available_cash;
+                    $models->trade_type = '25';
+                    $models->trade_desc = trans('handleOrder.handle_order_auto_fail').',退回投注资金:¥'.$money.';订单号:'.$v['order_id'].'';
+                    $models->status = '1';
                 
-                $models->money_cash = $new_available_cash;
-                $models->trade_type = '25';
-                $models->trade_desc = trans('handleOrder.handle_order_auto_fail').',退回投注资金:¥'.$money.';订单号:'.$v['order_id'].'';
-                $models->status = '1';
-               
-                //更新订单数据
-                $ret_order = self::where('id', $v['id'])->update(['roll_ratify' => '-1','status' => '2','use_mark'=>$v['use_mark']]);//trans('handleOrder.handle_order_auto_fail')
-                if($ret_order < 1)  throw new \Exception( Response::generate('',Response::UP_ORDER_ERR));
+                    //更新订单数据
+                    $ret_order = self::where('id', $v['id'])->update(['roll_ratify' => '-1','status' => '2','use_mark'=>$v['use_mark']]);//trans('handleOrder.handle_order_auto_fail')
+                    if($ret_order < 1)  throw new \Exception( Response::generate('',Response::UP_ORDER_ERR));
 
-                //更新用户数据
-                $ret_user = \App\Models\Account_detailed::where('account_identity', $v['account_identity'])->update(['available_cash' => $new_available_cash, 'cash' => $new_available_cash]);
-                if($ret_user < 1)  throw new \Exception( Response::generate('',Response::UP_USER_ERR));
+                    //更新用户数据
+                    $ret_user = \App\Models\Account_detailed::where('account_identity', $v['account_identity'])->update(['available_cash' => $new_available_cash, 'cash' => $new_available_cash]);
+                    if($ret_user < 1)  throw new \Exception( Response::generate('',Response::UP_USER_ERR));
 
-                //更新用户资金数据
-                if(!$models->save()) throw new \Exception( Response::generate('',Response::UP_MONEY_ERR));
+                    //更新用户资金数据
+                    if(!$models->save()) throw new \Exception( Response::generate('',Response::UP_MONEY_ERR));
+
+                }
+            }
+            //已取消赛事注单相关处理
+            else{
+                foreach($orders as $k=>$v){
+                    //需退回金额 = 投注金额-反水金额
+                    $money = abs($v['money'] - $v['fanshui']);
+                    //用户剩余金额
+                    $new_available_cash = $v['available_cash'] + $money;
+                    $models = new \App\Models\Money_details();
+                    $models->info_identity = UUID();
+                    $models->trade_id = $v['order_id'];//交易id
+                    $models->account_name = $v['account_name'];//用户名
+                    $models->account_identity = $v['account_identity'];//用户uuid
+                    $models->money = $money;//变动金额
+                    $models->money_time = date("Y-m-d H:i:s", time());//资金发生时间
+                    $models->money_type = '1';//1.增加 2.减少
+                    
+                    $models->money_cash = $new_available_cash;//剩余金额
+                    $models->trade_type = '3';//交易类型 3撤单
+                    $models->trade_desc = '因赛事取消,退回投注资金:¥'.$money.';订单号:'.$v['order_id'].'';//交易描述
+                    $models->status = '1';//1成功 2失败
+                
+                    //更新订单数据
+                    $ret_order = self::where('id', $v['id'])->update(['status' => '2','use_mark'=>'因赛事取消,作废注单']);
+                    if($ret_order < 1)  throw new \Exception( Response::generate('',Response::UP_ORDER_ERR));
 
+                    //更新用户数据
+                    $ret_user = \App\Models\Account_detailed::where('account_identity', $v['account_identity'])->update(['available_cash' => $new_available_cash, 'cash' => $new_available_cash]);
+                    if($ret_user < 1)  throw new \Exception( Response::generate('',Response::UP_USER_ERR));
+
+                    //更新用户资金数据
+                    if(!$models->save()) throw new \Exception( Response::generate('',Response::UP_MONEY_ERR));
+                }
             }
+
         }
    }
 
@@ -1458,5 +1499,81 @@ class SportsNoteList extends BaseModel
 
     }
 
+    /**
+     * 定时处理 已取消赛事相关注单
+     * $time 查询时间 默认60秒
+     */
+    public static function HandleOrderInvalid($time = 60){
+        //获取指定时间内已取消赛事
+        //拼接查询条件
+        $time_unx_max = time() - $time ;//查询最大时间 时间戳 5分钟前
+        $time_unx_min = $time_unx_max - $time;//查询最小时间 时间戳 10分钟前
+
+        $where = [
+            ['utime','>',date('Y-m-d H:i:s',$time_unx_min)],
+            ['utime','<',date('Y-m-d H:i:s',$time_unx_max)],
+            ['status','=',6]
+        ];
+        //足球
+        $ZqMatchData = ZqMatchModel::where($where)->SELECT('id')->get()->toArray();
+        //篮球
+        $LqMatchData = LqMatchModel::where($where)->SELECT('id')->get()->toArray();
+        //网球
+        $WqMatchData = WqMatchModel::where($where)->SELECT('id')->get()->toArray();
+        //棒球
+        $BqMatchData = BqMatchModel::where($where)->SELECT('id')->get()->toArray();
+        
+        //===取消注单处理===
+		$moneyBuyMatch = new \App\Models\MoneyBuyMatch();
+
+        //获取赛事下所有单式注单
+        $select = ['money_buy_simplex.id','money_buy_simplex.match_id','money_buy_simplex.account_name','money_buy_simplex.account_identity','money_buy_simplex.order_id','money_buy_simplex.money_time','money_buy_simplex.roll_ratify','money_buy_simplex.money','money_buy_simplex.gain_money','money_buy_match.p_code'];
+        //足球 单式注单
+        $zq_match_order_sim = [];
+        if(!empty($ZqMatchData)){
+            $zqMatch_ids = array_column($ZqMatchData,'id');
+            $zq_match_order_sim = $moneyBuyMatch->allsimplexorder('','zq',$select,$zqMatch_ids)->toArray();
+            //将所有赛事下串式注单 作废 (设置为 平)
+            \App\Models\MoneyBuyMatch::whereIn('match_id',$zqMatch_ids)->where('game_code','zq')->where('bet_type',2)->update(['result'=>2]);
+        }
+        //篮球 单式注单
+        $lq_match_order_sim = [];
+        if(!empty($LqMatchData)){
+            $lqMatch_ids = array_column($LqMatchData,'id');
+            $lq_match_order_sim = $moneyBuyMatch->allsimplexorder('','lq',$select,$lqMatch_ids)->toArray();
+            //将所有赛事下串式注单 作废 (设置为 平)
+            \App\Models\MoneyBuyMatch::whereIn('match_id',$lqMatch_ids)->where('game_code','lq')->where('bet_type',2)->update(['result'=>2]);
+        }
+        //网球 单式注单
+        $wq_match_order_sim = [];
+        if(!empty($WqMatchData)){
+            $wqMatch_ids = array_column($WqMatchData,'id');
+            $wq_match_order_sim = $moneyBuyMatch->allsimplexorder('','wq',$select,$wqMatch_ids)->toArray();
+            //将所有赛事下串式注单 作废 (设置为 平)
+            \App\Models\MoneyBuyMatch::whereIn('match_id',$wqMatch_ids)->where('game_code','wq')->where('bet_type',2)->update(['result'=>2]);
+        }
+        //棒球 单式注单
+        $bq_match_order_sim = [];
+        if(!empty($BqMatchData)){
+            $bqMatch_ids = array_column($BqMatchData,'id');
+            $bq_match_order_sim = $moneyBuyMatch->allsimplexorder('','bq',$select,$bqMatch_ids)->toArray();
+            //将所有赛事下串式注单 作废 (设置为 平)
+            \App\Models\MoneyBuyMatch::whereIn('match_id',$bqMatch_ids)->where('game_code','bq')->where('bet_type',2)->update(['result'=>2]);
+        }
 
+        //合并单式注单
+        $order_sim = array_merge($zq_match_order_sim,$lq_match_order_sim,$wq_match_order_sim,$bq_match_order_sim);
+        // dd($order_sim);
+        
+        //如果有单式注单
+        if(!empty($order_sim)){
+            //用户uuid
+            $account_identitys = array_column($order_sim,'account_identity');
+            //注单id
+            $order_ids = array_column($order_sim,'order_id');
+           
+            //注单作废处理
+            self::HandleMoney_v2($account_identitys,$order_ids,$order_sim,1);
+        }
+    }
 }

+ 6 - 2
routes/web.php

@@ -70,11 +70,15 @@ Route::post('/setMatchWarn', function () {
 });
 //定时审核滚球投注
 Route::get('/HandleOrder', function () {
-    return appExec('api', 'WriteSports', 'HandleOrder');
+    return appExec('api', 'Crontab', 'HandleOrder');
 });
 //定时处理赛事异常
 Route::get('/HandleMatch', function () {
-    return appExec('api', 'WriteSports', 'HandleMatch');
+    return appExec('api', 'Crontab', 'HandleMatch');
+});
+//定时处理已取消赛事相关注单
+Route::get('/HandleOrderInvalid', function () {
+    return appExec('api', 'Crontab', 'HandleOrderInvalid');
 });
 //获取联赛所属国家地区
 Route::post('/getArea', function () {