|
|
@@ -0,0 +1,170 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace App\Http\Controllers\Admin;
|
|
|
+
|
|
|
+use App\Http\Controllers\Controller;
|
|
|
+use App\Models\RewardCode;
|
|
|
+use App\Services\RewardCodeService;
|
|
|
+use Carbon\Carbon;
|
|
|
+use Illuminate\Http\Request;
|
|
|
+use Illuminate\Support\Facades\DB;
|
|
|
+
|
|
|
+class RewardCodeController extends Controller
|
|
|
+{
|
|
|
+ /**
|
|
|
+ * Reward code list.
|
|
|
+ */
|
|
|
+ public function index(Request $request)
|
|
|
+ {
|
|
|
+ $query = RewardCode::query();
|
|
|
+
|
|
|
+ if ($code = $request->get('code')) {
|
|
|
+ $query->where('code', $code);
|
|
|
+ }
|
|
|
+ if ($status = $request->get('status', null)) {
|
|
|
+ $query->where('status', $status);
|
|
|
+ }
|
|
|
+ if ($expired = $request->get('expired', null)) {
|
|
|
+ if ($expired == 1) {
|
|
|
+ $query->whereNotNull('expire_at')->where('expire_at', '<', Carbon::now());
|
|
|
+ } elseif ($expired == 0) {
|
|
|
+ $query->where(function ($q) {
|
|
|
+ $q->whereNull('expire_at')->orWhere('expire_at', '>=', Carbon::now());
|
|
|
+ });
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ $list = $query->orderByDesc('id')->paginate(20);
|
|
|
+
|
|
|
+ return view('admin.reward_code.index', [
|
|
|
+ 'list' => $list,
|
|
|
+ 'code' => $code ?? '',
|
|
|
+ 'status' => $request->get('status', ''),
|
|
|
+ 'expired' => $request->get('expired', ''),
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Create single reward code.
|
|
|
+ */
|
|
|
+ public function store(Request $request)
|
|
|
+ {
|
|
|
+ $request->validate([
|
|
|
+ 'total_amount' => 'required|numeric|min:0.0001',
|
|
|
+ 'min_amount' => 'required|numeric|min:0.0001',
|
|
|
+ 'max_amount' => 'required|numeric|min:0.0001',
|
|
|
+ 'total_count' => 'required|integer|min:1',
|
|
|
+ 'expire_at' => 'nullable|date',
|
|
|
+ 'remark' => 'nullable|string|max:255',
|
|
|
+ ]);
|
|
|
+
|
|
|
+ $totalAmount = (float)$request->input('total_amount');
|
|
|
+ $minAmount = (float)$request->input('min_amount');
|
|
|
+ $maxAmount = (float)$request->input('max_amount');
|
|
|
+ $totalCount = (int)$request->input('total_count');
|
|
|
+ $expireAt = $request->input('expire_at') ? Carbon::parse($request->input('expire_at')) : null;
|
|
|
+ $remark = $request->input('remark', '');
|
|
|
+
|
|
|
+ if ($minAmount > $maxAmount) {
|
|
|
+ return apiReturnFail('min_amount_gt_max_amount');
|
|
|
+ }
|
|
|
+ if ($totalAmount < $minAmount * $totalCount) {
|
|
|
+ return apiReturnFail('total_amount_too_small_for_min');
|
|
|
+ }
|
|
|
+
|
|
|
+ $created = null;
|
|
|
+ DB::connection('write')->transaction(function () use ($totalAmount, $minAmount, $maxAmount, $totalCount, $expireAt, $remark, &$created) {
|
|
|
+ $code = RewardCodeService::generateUniqueCode();
|
|
|
+ $created = RewardCode::create([
|
|
|
+ 'code' => $code,
|
|
|
+ 'expire_at' => $expireAt,
|
|
|
+ 'total_amount' => $totalAmount,
|
|
|
+ 'min_amount' => $minAmount,
|
|
|
+ 'max_amount' => $maxAmount,
|
|
|
+ 'total_count' => $totalCount,
|
|
|
+ 'claimed_count' => 0,
|
|
|
+ 'claimed_amount' => 0,
|
|
|
+ 'status' => RewardCodeService::STATUS_ACTIVE,
|
|
|
+ 'remark' => $remark,
|
|
|
+ ]);
|
|
|
+ });
|
|
|
+
|
|
|
+ return redirect()->back()->with('success', 'Created, code: ' . $created->code);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Update status (enable/disable).
|
|
|
+ */
|
|
|
+ public function updateStatus(Request $request, $id)
|
|
|
+ {
|
|
|
+ $status = (int)$request->input('status', RewardCodeService::STATUS_ACTIVE);
|
|
|
+ if (!in_array($status, [RewardCodeService::STATUS_ACTIVE, RewardCodeService::STATUS_INACTIVE])) {
|
|
|
+ return apiReturnFail('invalid_status');
|
|
|
+ }
|
|
|
+ $code = RewardCode::find($id);
|
|
|
+ if (!$code) {
|
|
|
+ return apiReturnFail('code_not_found');
|
|
|
+ }
|
|
|
+ $code->status = $status;
|
|
|
+ $code->save();
|
|
|
+ return apiReturnSuc($code->toArray(), '', '¸üгɹ¦');
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Claim records list.
|
|
|
+ */
|
|
|
+ public function records(Request $request)
|
|
|
+ {
|
|
|
+ $code = $request->get('code');
|
|
|
+ $userId = (int)$request->input('user_id', 0);
|
|
|
+ $gameId = (int)$request->input('game_id', 0);
|
|
|
+ $startDate = $request->input('start_date', '');
|
|
|
+ $endDate = $request->input('end_date', '');
|
|
|
+
|
|
|
+ $query = DB::connection('write')
|
|
|
+ ->table('agent.dbo.reward_code_claims as rcc')
|
|
|
+ ->leftJoin('QPAccountsDB.dbo.AccountsInfo as ai', 'rcc.UserID', '=', 'ai.UserID')
|
|
|
+ ->select(
|
|
|
+ 'rcc.id',
|
|
|
+ 'rcc.code',
|
|
|
+ 'rcc.UserID',
|
|
|
+ 'ai.GameID',
|
|
|
+ 'ai.NickName',
|
|
|
+ 'rcc.amount',
|
|
|
+ 'rcc.client_ip',
|
|
|
+ 'rcc.created_at'
|
|
|
+ )
|
|
|
+ ->orderBy('rcc.id', 'desc');
|
|
|
+
|
|
|
+ if ($code) {
|
|
|
+ $query->where('rcc.code', $code);
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($userId > 0) {
|
|
|
+ $query->where('rcc.UserID', $userId);
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($gameId > 0) {
|
|
|
+ $query->where('ai.GameID', $gameId);
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($startDate) {
|
|
|
+ $query->where('rcc.created_at', '>=', $startDate . ' 00:00:00');
|
|
|
+ }
|
|
|
+ if ($endDate) {
|
|
|
+ $query->where('rcc.created_at', '<=', $endDate . ' 23:59:59');
|
|
|
+ }
|
|
|
+
|
|
|
+ $list = $query->paginate(20);
|
|
|
+
|
|
|
+ return view('admin.reward_code.history', [
|
|
|
+ 'list' => $list,
|
|
|
+ 'code' => $code ?? '',
|
|
|
+ 'user_id' => $userId,
|
|
|
+ 'game_id' => $gameId,
|
|
|
+ 'start_date' => $startDate,
|
|
|
+ 'end_date' => $endDate,
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|