Xboard/app/Http/Controllers/V2/Admin/OrderController.php

190 lines
5.9 KiB
PHP
Raw Normal View History

2023-11-17 01:44:01 -05:00
<?php
2025-01-06 12:20:11 -05:00
namespace App\Http\Controllers\V2\Admin;
2023-11-17 01:44:01 -05:00
use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\OrderAssign;
use App\Http\Requests\Admin\OrderUpdate;
use App\Models\Order;
use App\Models\Plan;
use App\Models\User;
use App\Services\OrderService;
2025-01-06 12:20:11 -05:00
use App\Services\PlanService;
2023-11-17 01:44:01 -05:00
use App\Services\UserService;
use App\Utils\Helper;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class OrderController extends Controller
{
public function detail(Request $request)
{
2025-01-06 12:20:11 -05:00
$order = Order::with(['user', 'plan', 'commission_log'])->find($request->input('id'));
if (!$order)
return $this->fail([400202, '订单不存在']);
2023-11-17 01:44:01 -05:00
if ($order->surplus_order_ids) {
$order['surplus_orders'] = Order::whereIn('id', $order->surplus_order_ids)->get();
}
2025-01-06 12:20:11 -05:00
$order['period'] = PlanService::getLegacyPeriod($order->period);
return $this->success($order);
2023-11-17 01:44:01 -05:00
}
2025-01-06 12:20:11 -05:00
public function fetch(Request $request)
2023-11-17 01:44:01 -05:00
{
2025-01-06 12:20:11 -05:00
$current = $request->input('current', 1);
$pageSize = $request->input('pageSize', 10);
$orderModel = Order::with('plan:id,name');
if ($request->boolean('is_commission')) {
2025-01-06 12:20:11 -05:00
$orderModel->whereNotNull('invite_user_id')
->whereNotIn('status', [0, 2])
->where('commission_balance', '>', 0);
2023-11-17 01:44:01 -05:00
}
2025-01-06 12:20:11 -05:00
$this->applyFiltersAndSorts($request, $orderModel);
return response()->json(
$orderModel
->latest('created_at')
->paginate(
perPage: $pageSize,
page: $current
)->through(fn($order) => [
...$order->toArray(),
'period' => PlanService::getLegacyPeriod($order->period)
]),
);
2023-11-17 01:44:01 -05:00
}
2025-01-06 12:20:11 -05:00
private function applyFiltersAndSorts(Request $request, $builder)
{
$request->collect('filter')->each(function ($filter) use ($builder) {
$key = $filter['id'];
$value = $filter['value'];
2025-01-06 12:20:11 -05:00
$builder->where(function ($query) use ($key, $value) {
is_array($value)
? $query->whereIn($key, $value)
: $query->where($key, 'like', "%{$value}%");
2025-01-06 12:20:11 -05:00
});
});
$request->collect('sort')->each(function ($sort) use ($builder) {
$builder->orderBy(
$sort['id'],
$sort['desc'] ? 'DESC' : 'ASC'
);
});
2025-01-06 12:20:11 -05:00
}
2023-11-17 01:44:01 -05:00
public function paid(Request $request)
{
$order = Order::where('trade_no', $request->input('trade_no'))
->first();
if (!$order) {
2025-01-06 12:20:11 -05:00
return $this->fail([400202, '订单不存在']);
2023-11-17 01:44:01 -05:00
}
2025-01-06 12:20:11 -05:00
if ($order->status !== 0)
return $this->fail([400, '只能对待支付的订单进行操作']);
2023-11-17 01:44:01 -05:00
$orderService = new OrderService($order);
if (!$orderService->paid('manual_operation')) {
2025-01-06 12:20:11 -05:00
return $this->fail([500, '更新失败']);
2023-11-17 01:44:01 -05:00
}
return $this->success(true);
2023-11-17 01:44:01 -05:00
}
public function cancel(Request $request)
{
$order = Order::where('trade_no', $request->input('trade_no'))
->first();
if (!$order) {
2025-01-06 12:20:11 -05:00
return $this->fail([400202, '订单不存在']);
2023-11-17 01:44:01 -05:00
}
2025-01-06 12:20:11 -05:00
if ($order->status !== 0)
return $this->fail([400, '只能对待支付的订单进行操作']);
2023-11-17 01:44:01 -05:00
$orderService = new OrderService($order);
if (!$orderService->cancel()) {
2025-01-06 12:20:11 -05:00
return $this->fail([400, '更新失败']);
2023-11-17 01:44:01 -05:00
}
return $this->success(true);
2023-11-17 01:44:01 -05:00
}
public function update(OrderUpdate $request)
{
$params = $request->only([
'commission_status'
]);
$order = Order::where('trade_no', $request->input('trade_no'))
->first();
if (!$order) {
2025-01-06 12:20:11 -05:00
return $this->fail([400202, '订单不存在']);
2023-11-17 01:44:01 -05:00
}
try {
$order->update($params);
} catch (\Exception $e) {
\Log::error($e);
2025-01-06 12:20:11 -05:00
return $this->fail([500, '更新失败']);
2023-11-17 01:44:01 -05:00
}
return $this->success(true);
2023-11-17 01:44:01 -05:00
}
public function assign(OrderAssign $request)
{
$plan = Plan::find($request->input('plan_id'));
$user = User::where('email', $request->input('email'))->first();
if (!$user) {
2025-01-06 12:20:11 -05:00
return $this->fail([400202, '该用户不存在']);
2023-11-17 01:44:01 -05:00
}
if (!$plan) {
2025-01-06 12:20:11 -05:00
return $this->fail([400202, '该订阅不存在']);
2023-11-17 01:44:01 -05:00
}
$userService = new UserService();
if ($userService->isNotCompleteOrderByUserId($user->id)) {
2025-01-06 12:20:11 -05:00
return $this->fail([400, '该用户还有待支付的订单,无法分配']);
2023-11-17 01:44:01 -05:00
}
try {
DB::beginTransaction();
$order = new Order();
$orderService = new OrderService($order);
$order->user_id = $user->id;
$order->plan_id = $plan->id;
$order->period = $request->input('period');
$order->trade_no = Helper::guid();
$order->total_amount = $request->input('total_amount');
if ($order->period === 'reset_price') {
$order->type = Order::TYPE_RESET_TRAFFIC;
} else if ($user->plan_id !== NULL && $order->plan_id !== $user->plan_id) {
$order->type = Order::TYPE_UPGRADE;
} else if ($user->expired_at > time() && $order->plan_id == $user->plan_id) {
$order->type = Order::TYPE_RENEWAL;
} else {
$order->type = Order::TYPE_NEW_PURCHASE;
}
2023-11-17 01:44:01 -05:00
$orderService->setInvite($user);
2023-11-17 01:44:01 -05:00
if (!$order->save()) {
DB::rollBack();
2025-01-06 12:20:11 -05:00
return $this->fail([500, '订单创建失败']);
}
DB::commit();
2025-01-06 12:20:11 -05:00
} catch (\Exception $e) {
DB::rollBack();
throw $e;
2023-11-17 01:44:01 -05:00
}
return $this->success($order->trade_no);
2023-11-17 01:44:01 -05:00
}
}