Explorar el Código

修改足球游戏下注

lip hace 2 días
padre
commit
1d87f5e2b3

+ 27 - 0
app/Http/Controllers/admin/SportGame.php

@@ -5,6 +5,7 @@ namespace App\Http\Controllers\admin;
 use App\Http\Controllers\Controller;
 use App\Http\Controllers\Controller;
 use App\Models\SportGame as SportGameModel;
 use App\Models\SportGame as SportGameModel;
 use App\Models\SportGameplay;
 use App\Models\SportGameplay;
+use App\Models\SportGameLog;
 use Exception;
 use Exception;
 use App\Constants\HttpStatus;
 use App\Constants\HttpStatus;
 
 
@@ -56,4 +57,30 @@ class SportGame extends Controller
         }
         }
     }
     }
 
 
+    //开奖
+    public function openCode() { 
+        try {
+            $params = request()->validate([
+                'id' => ['required','integer'],
+            ]);
+            $id = $params['id'];
+            $info = SportGameModel::find($id);
+            if (!$info) {
+                return $this->error(HttpStatus::NOT_FOUND,'数据不存在');
+            }
+            //记录开奖日志
+            $log = new SportGameLog();
+            $log->game_id = $info->game_id;
+            $log->gameplay_id = $info->id;
+            $log->name = $info->name;
+            $log->name_en = $info->name_en;
+            $log->save();
+            
+            //订单结算
+            return $this->success();
+        } catch (Exception $e) {
+            return $this->error(HttpStatus::CUSTOM_ERROR,$e->getMessage());
+        }
+    }
+
 }
 }

+ 1 - 0
app/Http/Controllers/admin/SportGameOrder.php

@@ -114,6 +114,7 @@ class SportGameOrder extends Controller
             DB::beginTransaction();
             DB::beginTransaction();
             $params = request()->validate([
             $params = request()->validate([
                 'id' => ['required', 'array', 'min:1'],
                 'id' => ['required', 'array', 'min:1'],
+                'remark' => ['nullable', 'string'],
             ]);
             ]);
             $orderList = SportGameOrderModel::whereIn('id', $params['id'])->get();
             $orderList = SportGameOrderModel::whereIn('id', $params['id'])->get();
             foreach ($orderList as $order) {
             foreach ($orderList as $order) {

+ 95 - 0
app/Jobs/SportGameOrderJob.php

@@ -0,0 +1,95 @@
+<?php
+
+
+namespace App\Jobs;
+
+use Illuminate\Bus\Queueable;
+use Illuminate\Contracts\Queue\ShouldQueue;
+use Illuminate\Foundation\Bus\Dispatchable;
+use Illuminate\Queue\InteractsWithQueue;
+use Illuminate\Queue\SerializesModels;
+use Illuminate\Support\Facades\Log;
+use App\Models\SportGameOrder;
+use App\Models\Wallet;
+use App\Models\FundsRecord;
+use Illuminate\Support\Facades\DB;
+
+class SportGameOrderJob implements ShouldQueue
+{
+    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
+
+    /**
+     * 任务尝试次数
+     */
+    public $tries = 3;
+
+    /**
+     * 任务超时时间(秒)
+     */
+    public $timeout = 30;
+    
+    public $gameplayId = 0;
+
+    /**
+     * Create a new job instance.
+     *
+     * @return void
+     */
+    public function __construct(int $gameplayId)
+    {
+        $this->gameplayId = $gameplayId;
+    }
+
+    /**
+     * Execute the job.
+     *
+     * @return void
+     */
+    public function handle()
+    {
+        Log::error('开始执行足球游戏订单开奖结算任务: ' . now());
+        
+        //先把未中奖的标记为未中奖
+        SportGameOrder::where('gameplay_id', '<>', $this->gameplayId)->where('status', 0)->update(['status' => 1]);
+
+        $list = SportGameOrder::where('gameplay_id', $this->gameplayId)->where('status', 0)->get();
+        foreach ($list as $item) {
+            try {
+                DB::beginTransaction();
+                $item->status = 2;
+                $item->win_amount = bcmul($item->amount, $item->odds, 2);
+                $item->profit_and_loss = $item->win_amount - $item->amount;
+                $item->updated_at = now();
+                $item->save();
+
+                $walletInfo = Wallet::where('member_id', $item->member_id)->first();
+                if (!$walletInfo) continue;
+                
+                $before = $walletInfo->available_balance;
+                $after = bcadd($walletInfo->available_balance, $item->win_amount, 2);
+                $walletInfo->available_balance = $after;
+                $walletInfo->save();
+
+                FundsRecord::addData([
+                    'change_type' => '足球游戏中奖',
+                    'amount' => $item->win_amount,
+                    'before_balance' => $before,
+                    'after_balance' => $after,
+                    'member_id' => $item->member_id,
+                    'related_id' => $item->id,
+                    'remark' => '足球游戏中奖',
+                ]);
+                DB::commit();
+            } catch (\Exception $e) {
+                DB::rollBack();
+                Log::error('足球游戏订单开奖结算任务执行异常: ' . $e->getMessage());
+            }
+        }
+    }
+
+    // 可选:失败处理
+    public function failed(\Throwable $exception)
+    {
+        Log::error('足球游戏订单开奖结算任务执行异常: ' . $exception->getMessage());
+    }
+}

+ 9 - 0
app/Models/SportGameLog.php

@@ -0,0 +1,9 @@
+<?php
+
+namespace App\Models;
+
+class SportGameLog extends BaseModel
+{
+    protected $table = 'sport_game_log';
+    protected $fillable = ['game_id', 'gameplay_id', 'name', 'name_en'];
+}