Xboard/app/Http/Controllers/V2/Admin/PlanController.php

135 lines
4.0 KiB
PHP
Raw Normal View History

2023-11-17 01:44:01 -05:00
<?php
2025-01-06 12:20:11 -05:00
namespace App\Http\Controllers\V2\Admin;
2023-11-17 01:44:01 -05:00
use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\PlanSort;
use App\Models\Order;
use App\Models\Plan;
2025-01-06 12:20:11 -05:00
use App\Models\ServerGroup;
2023-11-17 01:44:01 -05:00
use App\Models\User;
use App\Services\PlanService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class PlanController extends Controller
{
public function fetch(Request $request)
{
2025-01-06 12:20:11 -05:00
$plans = Plan::orderBy('sort', 'ASC')
->with([
'group:id,name'
])
->withCount('users')
->get();
return $this->success($plans);
2023-11-17 01:44:01 -05:00
}
2025-01-06 12:20:11 -05:00
public function save(Request $request)
2023-11-17 01:44:01 -05:00
{
2025-01-06 12:20:11 -05:00
$params = $request->validate([
'id' => 'nullable|integer',
'name' => 'required|string',
'content' => 'nullable|string',
'period_reset_method' => 'integer|required_if:type,0',
'transfer_enable' => 'integer|required',
'prices' => 'array|nullable',
'group_id' => 'integer|nullable',
'speed_limit' => 'integer|nullable',
'device_limit' => 'integer|nullable',
'capacity_limit' => 'integer|nullable',
]);
2023-11-17 01:44:01 -05:00
if ($request->input('id')) {
$plan = Plan::find($request->input('id'));
if (!$plan) {
2025-01-06 12:20:11 -05:00
return $this->fail([400202, '该订阅不存在']);
2023-11-17 01:44:01 -05:00
}
DB::beginTransaction();
// update user group id and transfer
try {
if ($request->input('force_update')) {
User::where('plan_id', $plan->id)->update([
'group_id' => $params['group_id'],
'transfer_enable' => $params['transfer_enable'] * 1073741824,
'speed_limit' => $params['speed_limit']
]);
}
$plan->update($params);
DB::commit();
return $this->success(true);
2023-11-17 01:44:01 -05:00
} catch (\Exception $e) {
DB::rollBack();
\Log::error($e);
2025-01-06 12:20:11 -05:00
return $this->fail([500, '保存失败']);
2023-11-17 01:44:01 -05:00
}
}
if (!Plan::create($params)) {
2025-01-06 12:20:11 -05:00
return $this->fail([500, '创建失败']);
2023-11-17 01:44:01 -05:00
}
return $this->success(true);
2023-11-17 01:44:01 -05:00
}
public function drop(Request $request)
{
if (Order::where('plan_id', $request->input('id'))->first()) {
2025-01-06 12:20:11 -05:00
return $this->fail([400201, '该订阅下存在订单无法删除']);
2023-11-17 01:44:01 -05:00
}
if (User::where('plan_id', $request->input('id'))->first()) {
2025-01-06 12:20:11 -05:00
return $this->fail([400201, '该订阅下存在用户无法删除']);
2023-11-17 01:44:01 -05:00
}
if ($request->input('id')) {
$plan = Plan::find($request->input('id'));
if (!$plan) {
2025-01-06 12:20:11 -05:00
return $this->fail([400202, '该订阅不存在']);
2023-11-17 01:44:01 -05:00
}
}
return $this->success($plan->delete());
2023-11-17 01:44:01 -05:00
}
2025-01-06 12:20:11 -05:00
public function update(Request $request)
2023-11-17 01:44:01 -05:00
{
$updateData = $request->only([
'show',
2025-01-06 12:20:11 -05:00
'renew',
'sell'
2023-11-17 01:44:01 -05:00
]);
$plan = Plan::find($request->input('id'));
if (!$plan) {
2025-01-06 12:20:11 -05:00
return $this->fail([400202, '该订阅不存在']);
2023-11-17 01:44:01 -05:00
}
try {
$plan->update($updateData);
} catch (\Exception $e) {
\Log::error($e);
2025-01-06 12:20:11 -05:00
return $this->fail([500, '保存失败']);
2023-11-17 01:44:01 -05:00
}
2025-01-06 12:20:11 -05:00
return $this->success(true);
2023-11-17 01:44:01 -05:00
}
2025-01-06 12:20:11 -05:00
public function sort(Request $request)
2023-11-17 01:44:01 -05:00
{
2025-01-06 12:20:11 -05:00
$params = $request->validate([
'ids' => 'required|array'
]);
try {
DB::beginTransaction();
2025-01-06 12:20:11 -05:00
foreach ($params['ids'] as $k => $v) {
if (!Plan::find($v)->update(['sort' => $k + 1])) {
throw new \Exception();
}
2023-11-17 01:44:01 -05:00
}
DB::commit();
2025-01-06 12:20:11 -05:00
} catch (\Exception $e) {
DB::rollBack();
\Log::error($e);
2025-01-06 12:20:11 -05:00
return $this->fail([500, '保存失败']);
2023-11-17 01:44:01 -05:00
}
return $this->success(true);
2023-11-17 01:44:01 -05:00
}
}