2019-10-29 15:33:36 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
|
2020-05-11 02:13:20 +08:00
|
|
|
use App\Http\Requests\Admin\OrderAssign;
|
2019-11-29 01:58:20 +08:00
|
|
|
use App\Http\Requests\Admin\OrderUpdate;
|
2021-03-25 16:59:28 +08:00
|
|
|
use App\Http\Requests\Admin\OrderFetch;
|
2020-03-17 20:16:55 +08:00
|
|
|
use App\Services\OrderService;
|
2021-08-31 19:54:08 +08:00
|
|
|
use App\Services\UserService;
|
2020-05-11 02:13:20 +08:00
|
|
|
use App\Utils\Helper;
|
2019-10-29 15:33:36 +08:00
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
|
|
use App\Models\Order;
|
|
|
|
use App\Models\User;
|
2019-11-04 02:06:21 +08:00
|
|
|
use App\Models\Plan;
|
2020-05-11 02:13:20 +08:00
|
|
|
use Illuminate\Support\Facades\DB;
|
2019-10-29 15:33:36 +08:00
|
|
|
|
|
|
|
class OrderController extends Controller
|
|
|
|
{
|
2021-03-25 16:59:28 +08:00
|
|
|
private function filter(Request $request, &$builder)
|
|
|
|
{
|
|
|
|
if ($request->input('filter')) {
|
|
|
|
foreach ($request->input('filter') as $filter) {
|
|
|
|
if ($filter['key'] === 'email') {
|
|
|
|
$user = User::where('email', "%{$filter['value']}%")->first();
|
|
|
|
if (!$user) continue;
|
|
|
|
$builder->where('user_id', $user->id);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ($filter['condition'] === '模糊') {
|
|
|
|
$filter['condition'] = 'like';
|
|
|
|
$filter['value'] = "%{$filter['value']}%";
|
|
|
|
}
|
|
|
|
$builder->where($filter['key'], $filter['condition'], $filter['value']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function fetch(OrderFetch $request)
|
2020-01-11 13:36:52 +08:00
|
|
|
{
|
2019-10-29 15:33:36 +08:00
|
|
|
$current = $request->input('current') ? $request->input('current') : 1;
|
|
|
|
$pageSize = $request->input('pageSize') >= 10 ? $request->input('pageSize') : 10;
|
|
|
|
$orderModel = Order::orderBy('created_at', 'DESC');
|
2019-11-29 01:44:37 +08:00
|
|
|
if ($request->input('is_commission')) {
|
|
|
|
$orderModel->where('invite_user_id', '!=', NULL);
|
2021-03-13 00:03:53 +08:00
|
|
|
$orderModel->whereNotIn('status', [0, 2]);
|
2020-05-13 12:45:22 +08:00
|
|
|
$orderModel->where('commission_balance', '>', 0);
|
2019-11-29 01:44:37 +08:00
|
|
|
}
|
2021-03-25 16:59:28 +08:00
|
|
|
$this->filter($request, $orderModel);
|
2019-10-29 15:33:36 +08:00
|
|
|
$total = $orderModel->count();
|
2019-11-04 02:06:21 +08:00
|
|
|
$res = $orderModel->forPage($current, $pageSize)
|
|
|
|
->get();
|
|
|
|
$plan = Plan::get();
|
|
|
|
for ($i = 0; $i < count($res); $i++) {
|
|
|
|
for ($k = 0; $k < count($plan); $k++) {
|
|
|
|
if ($plan[$k]['id'] == $res[$i]['plan_id']) {
|
|
|
|
$res[$i]['plan_name'] = $plan[$k]['name'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-10-29 15:33:36 +08:00
|
|
|
return response([
|
2019-11-04 02:06:21 +08:00
|
|
|
'data' => $res,
|
2019-10-29 15:33:36 +08:00
|
|
|
'total' => $total
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2021-08-28 14:11:59 +08:00
|
|
|
public function paid(Request $request)
|
2020-01-11 13:36:52 +08:00
|
|
|
{
|
2019-11-29 01:58:20 +08:00
|
|
|
$order = Order::where('trade_no', $request->input('trade_no'))
|
|
|
|
->first();
|
|
|
|
if (!$order) {
|
|
|
|
abort(500, '订单不存在');
|
|
|
|
}
|
2021-08-28 14:11:59 +08:00
|
|
|
if ($order->status !== 0) abort(500, '只能对待支付的订单进行操作');
|
2019-11-29 01:58:20 +08:00
|
|
|
|
2021-08-28 14:11:59 +08:00
|
|
|
$orderService = new OrderService($order);
|
|
|
|
if (!$orderService->paid('manual_operation')) {
|
|
|
|
abort(500, '更新失败');
|
2020-03-17 20:16:55 +08:00
|
|
|
}
|
2021-08-28 14:11:59 +08:00
|
|
|
return response([
|
|
|
|
'data' => true
|
|
|
|
]);
|
|
|
|
}
|
2020-03-17 20:16:55 +08:00
|
|
|
|
2021-08-28 14:11:59 +08:00
|
|
|
public function cancel(Request $request)
|
|
|
|
{
|
|
|
|
$order = Order::where('trade_no', $request->input('trade_no'))
|
|
|
|
->first();
|
|
|
|
if (!$order) {
|
|
|
|
abort(500, '订单不存在');
|
2019-11-29 01:58:20 +08:00
|
|
|
}
|
2021-08-28 14:11:59 +08:00
|
|
|
if ($order->status !== 0) abort(500, '只能对待支付的订单进行操作');
|
2019-11-29 01:58:20 +08:00
|
|
|
|
2021-08-28 14:11:59 +08:00
|
|
|
$orderService = new OrderService($order);
|
|
|
|
if (!$orderService->cancel()) {
|
|
|
|
abort(500, '更新失败');
|
|
|
|
}
|
2019-11-29 01:58:20 +08:00
|
|
|
return response([
|
|
|
|
'data' => true
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2021-08-28 14:11:59 +08:00
|
|
|
public function update(OrderUpdate $request)
|
2020-01-11 13:36:52 +08:00
|
|
|
{
|
2021-08-28 14:11:59 +08:00
|
|
|
$params = $request->only([
|
|
|
|
'commission_status'
|
|
|
|
]);
|
|
|
|
|
2019-10-29 15:33:36 +08:00
|
|
|
$order = Order::where('trade_no', $request->input('trade_no'))
|
|
|
|
->first();
|
|
|
|
if (!$order) {
|
2021-08-28 14:11:59 +08:00
|
|
|
abort(500, '订单不存在');
|
2019-10-29 15:33:36 +08:00
|
|
|
}
|
2021-08-28 14:11:59 +08:00
|
|
|
|
|
|
|
try {
|
|
|
|
$order->update($params);
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
abort(500, '更新失败');
|
2019-10-29 15:33:36 +08:00
|
|
|
}
|
2021-08-28 14:11:59 +08:00
|
|
|
|
2019-10-29 15:33:36 +08:00
|
|
|
return response([
|
|
|
|
'data' => true
|
|
|
|
]);
|
|
|
|
}
|
2020-05-11 02:13:20 +08:00
|
|
|
|
|
|
|
public function assign(OrderAssign $request)
|
|
|
|
{
|
|
|
|
$plan = Plan::find($request->input('plan_id'));
|
2020-05-11 15:19:52 +08:00
|
|
|
$user = User::where('email', $request->input('email'))->first();
|
2020-05-11 02:13:20 +08:00
|
|
|
|
|
|
|
if (!$user) {
|
|
|
|
abort(500, '该用户不存在');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$plan) {
|
|
|
|
abort(500, '该订阅不存在');
|
|
|
|
}
|
|
|
|
|
2021-08-31 19:54:08 +08:00
|
|
|
$userService = new UserService();
|
|
|
|
if ($userService->isNotCompleteOrderByUserId($user->id)) {
|
|
|
|
abort(500, '该用户还有待支付的订单,无法分配');
|
|
|
|
}
|
|
|
|
|
2020-05-11 02:13:20 +08:00
|
|
|
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');
|
|
|
|
|
2020-05-11 18:26:16 +08:00
|
|
|
if ($order->cycle === 'reset_price') {
|
|
|
|
$order->type = 4;
|
|
|
|
} else if ($user->plan_id !== NULL && $order->plan_id !== $user->plan_id) {
|
|
|
|
$order->type = 3;
|
|
|
|
} else if ($user->expired_at > time() && $order->plan_id == $user->plan_id) {
|
|
|
|
$order->type = 2;
|
|
|
|
} else {
|
|
|
|
$order->type = 1;
|
|
|
|
}
|
|
|
|
|
2020-05-11 02:13:20 +08:00
|
|
|
$orderService->setInvite($user);
|
|
|
|
|
|
|
|
if (!$order->save()) {
|
|
|
|
DB::rollback();
|
|
|
|
abort(500, '订单创建失败');
|
|
|
|
}
|
|
|
|
|
|
|
|
DB::commit();
|
|
|
|
|
|
|
|
return response([
|
|
|
|
'data' => $order->trade_no
|
|
|
|
]);
|
|
|
|
}
|
2019-10-29 15:33:36 +08:00
|
|
|
}
|