2019-10-29 15:33:36 +08:00
|
|
|
<?php
|
|
|
|
|
2020-01-29 16:08:50 +08:00
|
|
|
namespace App\Http\Controllers\User;
|
2019-10-29 15:33:36 +08:00
|
|
|
|
|
|
|
use App\Http\Controllers\Controller;
|
2022-04-19 04:10:02 +08:00
|
|
|
use App\Models\User;
|
2020-01-29 16:08:50 +08:00
|
|
|
use Illuminate\Http\Request;
|
2019-10-29 15:33:36 +08:00
|
|
|
use App\Models\Plan;
|
2022-07-08 02:36:33 +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)
|
|
|
|
{
|
2022-04-19 04:10:02 +08:00
|
|
|
$user = User::find($request->session()->get('id'));
|
2020-01-03 21:04:44 +08:00
|
|
|
if ($request->input('id')) {
|
2022-04-19 04:10:02 +08:00
|
|
|
$plan = Plan::where('id', $request->input('id'))->first();
|
2020-01-03 21:04:44 +08:00
|
|
|
if (!$plan) {
|
2021-06-12 00:56:39 +08:00
|
|
|
abort(500, __('Subscription plan does not exist'));
|
2020-01-03 21:04:44 +08:00
|
|
|
}
|
2022-04-19 04:10:02 +08:00
|
|
|
if ((!$plan->show && !$plan->renew) || (!$plan->show && $user->plan_id !== $plan->id)) {
|
|
|
|
abort(500, __('Subscription plan does not exist'));
|
|
|
|
}
|
2020-01-03 21:04:44 +08:00
|
|
|
return response([
|
|
|
|
'data' => $plan
|
|
|
|
]);
|
2022-07-08 02:36:33 +08:00
|
|
|
} else {
|
|
|
|
$counts = User::select(
|
|
|
|
DB::raw("plan_id"),
|
|
|
|
DB::raw("count(*) as count")
|
|
|
|
)
|
|
|
|
->where('plan_id', '!=', NULL)
|
|
|
|
->where(function ($query) {
|
|
|
|
$query->where('expired_at', '>=', time())
|
|
|
|
->orWhere('expired_at', NULL);
|
|
|
|
})
|
|
|
|
->groupBy("plan_id")
|
|
|
|
->get()
|
|
|
|
->keyBy('plan_id');
|
2019-10-29 15:33:36 +08:00
|
|
|
}
|
2022-07-08 02:36:33 +08:00
|
|
|
$plans = Plan::where('show', 1)
|
2020-04-21 00:22:05 +08:00
|
|
|
->orderBy('sort', 'ASC')
|
2020-01-03 21:04:44 +08:00
|
|
|
->get();
|
2022-07-08 02:36:33 +08:00
|
|
|
if (isset($counts)) {
|
|
|
|
foreach ($plans as $k => $v) {
|
|
|
|
if (isset($counts[$plans[$k]->id])) {
|
|
|
|
$plans[$k]->capacity_limit = $plans[$k]->capacity_limit - $counts[$plans[$k]->id]->count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-10-29 15:33:36 +08:00
|
|
|
return response([
|
2022-07-08 02:36:33 +08:00
|
|
|
'data' => $plans
|
2019-10-29 15:33:36 +08:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|