2019-10-29 15:33:36 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
|
|
|
|
use App\Http\Requests\Admin\PlanSave;
|
2020-04-20 16:07:06 +08:00
|
|
|
use App\Http\Requests\Admin\PlanSort;
|
2019-10-29 15:33:36 +08:00
|
|
|
use App\Http\Requests\Admin\PlanUpdate;
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
|
|
use App\Models\Plan;
|
|
|
|
use App\Models\Order;
|
|
|
|
use App\Models\User;
|
2020-02-24 02:04:42 +08:00
|
|
|
use Illuminate\Support\Facades\DB;
|
2019-10-29 15:33:36 +08:00
|
|
|
|
|
|
|
class PlanController extends Controller
|
|
|
|
{
|
2020-01-11 13:36:52 +08:00
|
|
|
public function fetch(Request $request)
|
|
|
|
{
|
2020-06-24 13:47:25 +08:00
|
|
|
|
|
|
|
$counts = User::select(
|
|
|
|
DB::raw("plan_id"),
|
|
|
|
DB::raw("count(*) as count")
|
|
|
|
)
|
|
|
|
->where('plan_id', '!=', NULL)
|
2020-06-24 14:26:43 +08:00
|
|
|
->where(function ($query) {
|
|
|
|
$query->where('expired_at', '>=', time())
|
|
|
|
->orWhere('expired_at', NULL);
|
|
|
|
})
|
2020-06-24 13:47:25 +08:00
|
|
|
->groupBy("plan_id")
|
|
|
|
->get();
|
|
|
|
$plans = Plan::orderBy('sort', 'ASC')->get();
|
|
|
|
foreach ($plans as $k => $v) {
|
|
|
|
$plans[$k]->count = 0;
|
|
|
|
foreach ($counts as $kk => $vv) {
|
|
|
|
if ($plans[$k]->id === $counts[$kk]->plan_id) $plans[$k]->count = $counts[$kk]->count;
|
|
|
|
}
|
|
|
|
}
|
2019-10-29 15:33:36 +08:00
|
|
|
return response([
|
2020-06-24 13:47:25 +08:00
|
|
|
'data' => $plans
|
2019-10-29 15:33:36 +08:00
|
|
|
]);
|
|
|
|
}
|
2020-01-11 13:36:52 +08:00
|
|
|
|
|
|
|
public function save(PlanSave $request)
|
|
|
|
{
|
2020-07-15 15:57:12 +08:00
|
|
|
$params = $request->validated();
|
2019-10-29 15:33:36 +08:00
|
|
|
if ($request->input('id')) {
|
|
|
|
$plan = Plan::find($request->input('id'));
|
|
|
|
if (!$plan) {
|
|
|
|
abort(500, '该订阅不存在');
|
|
|
|
}
|
2020-02-24 02:04:42 +08:00
|
|
|
DB::beginTransaction();
|
2020-05-23 01:38:27 +08:00
|
|
|
// update user group id and transfer
|
2020-03-17 14:28:47 +08:00
|
|
|
try {
|
2020-05-23 01:38:27 +08:00
|
|
|
User::where('plan_id', $plan->id)->update([
|
2021-07-01 17:02:13 +08:00
|
|
|
'group_id' => $params['group_id'],
|
|
|
|
'transfer_enable' => $params['transfer_enable'] * 1073741824
|
2020-05-23 01:38:27 +08:00
|
|
|
]);
|
2020-03-17 14:28:47 +08:00
|
|
|
$plan->update($params);
|
|
|
|
} catch (\Exception $e) {
|
2020-02-24 02:04:42 +08:00
|
|
|
DB::rollBack();
|
2020-02-23 02:37:52 +08:00
|
|
|
abort(500, '保存失败');
|
|
|
|
}
|
2020-02-24 02:04:42 +08:00
|
|
|
DB::commit();
|
2020-02-23 02:37:52 +08:00
|
|
|
return response([
|
|
|
|
'data' => true
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
if (!Plan::create($params)) {
|
|
|
|
abort(500, '创建失败');
|
2019-10-29 15:33:36 +08:00
|
|
|
}
|
|
|
|
return response([
|
2020-02-23 02:37:52 +08:00
|
|
|
'data' => true
|
2019-10-29 15:33:36 +08:00
|
|
|
]);
|
|
|
|
}
|
2020-01-11 13:36:52 +08:00
|
|
|
|
|
|
|
public function drop(Request $request)
|
|
|
|
{
|
2019-10-29 15:33:36 +08:00
|
|
|
if (Order::where('plan_id', $request->input('id'))->first()) {
|
|
|
|
abort(500, '该订阅下存在订单无法删除');
|
|
|
|
}
|
|
|
|
if (User::where('plan_id', $request->input('id'))->first()) {
|
|
|
|
abort(500, '该订阅下存在用户无法删除');
|
|
|
|
}
|
|
|
|
if ($request->input('id')) {
|
|
|
|
$plan = Plan::find($request->input('id'));
|
|
|
|
if (!$plan) {
|
|
|
|
abort(500, '该订阅ID不存在');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return response([
|
|
|
|
'data' => $plan->delete()
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2020-01-11 13:36:52 +08:00
|
|
|
public function update(PlanUpdate $request)
|
|
|
|
{
|
2019-10-29 15:33:36 +08:00
|
|
|
$updateData = $request->only([
|
|
|
|
'show',
|
|
|
|
'renew'
|
|
|
|
]);
|
2020-01-11 13:36:52 +08:00
|
|
|
|
2019-10-29 15:33:36 +08:00
|
|
|
$plan = Plan::find($request->input('id'));
|
|
|
|
if (!$plan) {
|
|
|
|
abort(500, '该订阅不存在');
|
|
|
|
}
|
2020-03-17 14:28:47 +08:00
|
|
|
|
|
|
|
try {
|
|
|
|
$plan->update($updateData);
|
|
|
|
} catch (\Exception $e) {
|
2019-10-29 15:33:36 +08:00
|
|
|
abort(500, '保存失败');
|
|
|
|
}
|
|
|
|
|
|
|
|
return response([
|
|
|
|
'data' => true
|
|
|
|
]);
|
|
|
|
}
|
2020-04-20 16:07:06 +08:00
|
|
|
|
|
|
|
public function sort(PlanSort $request)
|
|
|
|
{
|
|
|
|
DB::beginTransaction();
|
|
|
|
foreach ($request->input('plan_ids') as $k => $v) {
|
|
|
|
if (!Plan::find($v)->update(['sort' => $k + 1])) {
|
|
|
|
DB::rollBack();
|
|
|
|
abort(500, '保存失败');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
DB::commit();
|
|
|
|
return response([
|
|
|
|
'data' => true
|
|
|
|
]);
|
|
|
|
}
|
2019-10-29 15:33:36 +08:00
|
|
|
}
|