From d1a2e7a29e4bf1c26f2384b9dd4b2f0003b33fad Mon Sep 17 00:00:00 2001 From: Tokumeikoi Date: Mon, 11 May 2020 02:13:20 +0800 Subject: [PATCH] order assign --- .../Controllers/Admin/OrderController.php | 40 +++++++++++++++++++ app/Http/Requests/Admin/OrderAssign.php | 34 ++++++++++++++++ app/Http/Routes/AdminRoute.php | 1 + 3 files changed, 75 insertions(+) create mode 100644 app/Http/Requests/Admin/OrderAssign.php diff --git a/app/Http/Controllers/Admin/OrderController.php b/app/Http/Controllers/Admin/OrderController.php index 96982fc5..ba796d91 100644 --- a/app/Http/Controllers/Admin/OrderController.php +++ b/app/Http/Controllers/Admin/OrderController.php @@ -2,13 +2,16 @@ namespace App\Http\Controllers\Admin; +use App\Http\Requests\Admin\OrderAssign; use App\Http\Requests\Admin\OrderUpdate; use App\Services\OrderService; +use App\Utils\Helper; use Illuminate\Http\Request; use App\Http\Controllers\Controller; use App\Models\Order; use App\Models\User; use App\Models\Plan; +use Illuminate\Support\Facades\DB; class OrderController extends Controller { @@ -100,4 +103,41 @@ class OrderController extends Controller 'data' => true ]); } + + public function assign(OrderAssign $request) + { + $plan = Plan::find($request->input('plan_id')); + $user = User::where('email', $request->input('email')); + + if (!$user) { + abort(500, '该用户不存在'); + } + + if (!$plan) { + abort(500, '该订阅不存在'); + } + + DB::beginTransaction(); + $order = new Order(); + $orderService = new OrderService($order); + $order->user_id = $user->id; + $order->plan_id = $plan->id; + $order->cycle = $request->input('cycle'); + $order->trade_no = Helper::guid(); + $order->total_amount = $request->input('total_amount'); + + $orderService->setOrderType($user); + $orderService->setInvite($user); + + if (!$order->save()) { + DB::rollback(); + abort(500, '订单创建失败'); + } + + DB::commit(); + + return response([ + 'data' => $order->trade_no + ]); + } } diff --git a/app/Http/Requests/Admin/OrderAssign.php b/app/Http/Requests/Admin/OrderAssign.php new file mode 100644 index 00000000..a6262fcf --- /dev/null +++ b/app/Http/Requests/Admin/OrderAssign.php @@ -0,0 +1,34 @@ + 'required', + 'email' => 'required', + 'total_amount' => 'required', + 'cycle' => 'required|in:month_price,quarter_price,half_year_price,year_price,onetime_price,reset_price' + ]; + } + + public function messages() + { + return [ + 'plan_id.required' => '订阅不能为空', + 'email.required' => '邮箱不能为空', + 'total_amount.required' => '支付金额不能为空', + 'cycle.required' => '订阅周期不能为空', + 'cycle.in' => '订阅周期格式有误' + ]; + } +} diff --git a/app/Http/Routes/AdminRoute.php b/app/Http/Routes/AdminRoute.php index b89e8359..bfbbf46c 100644 --- a/app/Http/Routes/AdminRoute.php +++ b/app/Http/Routes/AdminRoute.php @@ -36,6 +36,7 @@ class AdminRoute $router->get ('/order/fetch', 'Admin\\OrderController@fetch'); $router->post('/order/repair', 'Admin\\OrderController@repair'); $router->post('/order/update', 'Admin\\OrderController@update'); + $router->post('/order/assign', 'Admin\\OrderController@assign'); // User $router->get ('/user/fetch', 'Admin\\UserController@fetch'); $router->post('/user/update', 'Admin\\UserController@update');