diff --git a/app/Console/Commands/CheckOrder.php b/app/Console/Commands/CheckOrder.php index 6b5d235f..75144c11 100755 --- a/app/Console/Commands/CheckOrder.php +++ b/app/Console/Commands/CheckOrder.php @@ -2,6 +2,7 @@ namespace App\Console\Commands; +use App\Services\OrderService; use Illuminate\Console\Command; use App\Models\Order; use App\Models\User; @@ -42,14 +43,14 @@ class CheckOrder extends Command */ public function handle() { - $order = Order::get(); - foreach ($order as $item) { + $orders = Order::get(); + foreach ($orders as $item) { switch ($item->status) { // cancel case 0: if (strtotime($item->created_at) <= (time() - 1800)) { - $item->status = 2; - $item->save(); + $orderService = new OrderService($item); + $orderService->cancel(); } break; case 1: diff --git a/app/Http/Controllers/User/OrderController.php b/app/Http/Controllers/User/OrderController.php index 5d986db8..3e1700db 100755 --- a/app/Http/Controllers/User/OrderController.php +++ b/app/Http/Controllers/User/OrderController.php @@ -4,6 +4,7 @@ namespace App\Http\Controllers\User; use App\Http\Controllers\Controller; use App\Http\Requests\User\OrderSave; +use App\Services\UserService; use Illuminate\Http\Request; use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\Log; @@ -162,8 +163,7 @@ class OrderController extends Controller $order->cycle = $request->input('cycle'); $order->trade_no = Helper::guid(); $order->total_amount = $plan[$request->input('cycle')]; - // discount start - // coupon + // coupon start if (isset($coupon)) { switch ($coupon->type) { case 1: @@ -181,13 +181,13 @@ class OrderController extends Controller } } } - // user + // coupon complete + // discount start if ($user->discount) { $order->discount_amount = $order->discount_amount + ($order->total_amount * ($user->discount / 100)); } - // discount complete - $order->total_amount = $order->total_amount - $order->discount_amount; // discount end + $order->total_amount = $order->total_amount - $order->discount_amount; // renew and change subscribe process if ($user->plan_id !== NULL && $order->plan_id !== $user->plan_id) { if (!(int)config('v2board.plan_change_enable', 1)) abort(500, '目前不允许更改订阅,请联系客服或提交工单'); @@ -217,6 +217,27 @@ class OrderController extends Controller } } } + // use balance + if ($user->balance && $order->total_amount > 0) { + $remainingBalance = $user->balance - $order->total_amount; + $userService = new UserService(); + if ($remainingBalance > 0) { + if (!$userService->addBalance($order->user_id, $order->total_amount)) { + DB::rollBack(); + abort(500, '余额不足'); + } + $order->balance_amount = $order->total_amount; + $order->total_amount = 0; + } else { + if (!$userService->addBalance($order->user_id, $user->balance)) { + DB::rollBack(); + abort(500, '余额不足'); + } + $order->balance_amount = $user->balance; + $order->total_amount = $order->total_amount - $user->balance; + } + } + if (!$order->save()) { DB::rollback(); abort(500, '订单创建失败'); diff --git a/app/Services/OrderService.php b/app/Services/OrderService.php index 100ad5af..803bde4d 100644 --- a/app/Services/OrderService.php +++ b/app/Services/OrderService.php @@ -3,6 +3,7 @@ namespace App\Services; use App\Models\Order; +use Illuminate\Support\Facades\DB; class OrderService { @@ -13,8 +14,20 @@ class OrderService $this->order = $order; } - public function cancel() + public function cancel():void { - + $order = $this->order; + DB::beginTransaction(); + $order->status = 2; + if (!$order->save()) { + DB::rollBack(); + } + if ($order->balance_amount) { + $userService = new UserService(); + if (!$userService->addBalance($order->user_id, $order->balance_amount)) { + DB::rollBack(); + } + } + DB::commit(); } } diff --git a/app/Services/UserService.php b/app/Services/UserService.php index cf2ecf5e..08889ba2 100644 --- a/app/Services/UserService.php +++ b/app/Services/UserService.php @@ -47,4 +47,20 @@ class UserService { return User::all(); } + + public function addBalance(int $userId, int $balance):bool + { + $user = User::find($userId); + if (!$user) { + return false; + } + $user->balance = $user->balance + $balance; + if ($user->balance < 0) { + return false; + } + if (!$user->save()) { + return false; + } + return true; + } }