From 0f9cb9696d538b13df15d1cea9025b7adc2a44ab Mon Sep 17 00:00:00 2001 From: Tokumeikoi Date: Fri, 28 Feb 2020 01:03:05 +0800 Subject: [PATCH] add onetime plan --- app/Console/Commands/CheckOrder.php | 31 +++++++++++++++++++++++---- app/Console/Commands/ResetTraffic.php | 5 ++++- app/Services/ServerService.php | 31 +++++++++++++++++++++++++++ 3 files changed, 62 insertions(+), 5 deletions(-) create mode 100644 app/Services/ServerService.php diff --git a/app/Console/Commands/CheckOrder.php b/app/Console/Commands/CheckOrder.php index 7dc298a5..9ac1fbe1 100755 --- a/app/Console/Commands/CheckOrder.php +++ b/app/Console/Commands/CheckOrder.php @@ -60,15 +60,21 @@ class CheckOrder extends Command } } - private function orderHandle($order) + private function orderHandle(Order $order) { $user = User::find($order->user_id); - return $this->buy($order, $user); + $plan = Plan::find($order->plan_id); + switch ($plan->type) { + // cycle + case 0: return $this->buyByCycle($order, $user, $plan); + // onetime + case 1: return $this->buyByOneTime($order, $user, $plan); + } + return $this->buy($order, $user, $plan); } - private function buy($order, $user) + private function buyByCycle(Order $order, User $user, Plan $plan) { - $plan = Plan::find($order->plan_id); // change plan process if ($order->type == 3) { $user->expired_at = time(); @@ -83,7 +89,24 @@ class CheckOrder extends Command $user->plan_id = $plan->id; $user->group_id = $plan->group_id; $user->expired_at = $this->getTime($order->cycle, $user->expired_at); + if ($user->save()) { + $order->status = 3; + $order->save(); + } + } + private function buyByOneTime(Order $order, User $user, Plan $plan) + { + if ($order->refund_amount) { + $user->balance = $user->balance + $order->refund_amount; + } + $user->transfer_enable = $plan->transfer_enable * 1073741824; + $user->enable = 1; + $user->u = 0; + $user->d = 0; + $user->plan_id = $plan->id; + $user->group_id = $plan->group_id; + $user->expired_at = 0; if ($user->save()) { $order->status = 3; $order->save(); diff --git a/app/Console/Commands/ResetTraffic.php b/app/Console/Commands/ResetTraffic.php index f534ef21..1486737d 100644 --- a/app/Console/Commands/ResetTraffic.php +++ b/app/Console/Commands/ResetTraffic.php @@ -38,7 +38,10 @@ class ResetTraffic extends Command */ public function handle() { - DB::table('v2_user')->update([ + // get plans of cycle type + $plans = Plan::where('type', 0)->get(); + $users = User::whereIn('plan_id', $plans)->get(); + $users->update([ 'u' => 0, 'd' => 0 ]); diff --git a/app/Services/ServerService.php b/app/Services/ServerService.php new file mode 100644 index 00000000..3815512a --- /dev/null +++ b/app/Services/ServerService.php @@ -0,0 +1,31 @@ +whereRaw('u + d < transfer_enable') + ->where(function ($query) { + $query->where('expired_at', '>=', time()) + ->orWhere('expired_at', 0); + }) + ->select([ + 'id', + 'email', + 't', + 'u', + 'd', + 'transfer_enable', + 'enable', + 'v2ray_uuid', + 'v2ray_alter_id', + 'v2ray_level' + ]) + ->get(); + } +}