v2board/app/Http/Controllers/Admin/OrderController.php

93 lines
2.6 KiB
PHP
Raw Normal View History

2019-10-29 15:33:36 +08:00
<?php
namespace App\Http\Controllers\Admin;
2019-11-29 01:58:20 +08:00
use App\Http\Requests\Admin\OrderUpdate;
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;
2019-10-29 15:33:36 +08:00
class OrderController extends Controller
{
2020-01-11 13:36:52 +08:00
public function fetch(Request $request)
{
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');
if ($request->input('trade_no')) {
$orderModel->where('trade_no', $request->input('trade_no'));
}
2019-11-29 01:44:37 +08:00
if ($request->input('is_commission')) {
$orderModel->where('invite_user_id', '!=', NULL);
2019-11-29 02:06:25 +08:00
$orderModel->where('status', 3);
2019-11-29 01:44:37 +08:00
}
2019-11-29 01:45:26 +08:00
if ($request->input('id')) {
$orderModel->where('id', $request->input('id'));
}
2020-02-15 22:58:31 +08:00
if ($request->input('user_id')) {
$orderModel->where('user_id', $request->input('user_id'));
}
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
]);
}
2020-01-11 13:36:52 +08:00
public function update(OrderUpdate $request)
{
2019-11-29 01:58:20 +08:00
$updateData = $request->only([
'status',
'commission_status'
]);
$order = Order::where('trade_no', $request->input('trade_no'))
->first();
if (!$order) {
abort(500, '订单不存在');
}
2020-03-17 14:28:47 +08:00
try {
$order->update($updateData);
} catch (\Exception $e) {
2019-11-29 02:32:09 +08:00
abort(500, '更新失败');
2019-11-29 01:58:20 +08:00
}
return response([
'data' => true
]);
}
2020-01-11 13:36:52 +08:00
public function repair(Request $request)
{
2019-10-29 15:33:36 +08:00
if (empty($request->input('trade_no'))) {
abort(500, '参数错误');
}
$order = Order::where('trade_no', $request->input('trade_no'))
->where('status', 0)
->first();
if (!$order) {
abort(500, '订单不存在或订单已支付');
}
$order->status = 1;
if (!$order->save()) {
abort(500, '保存失败');
}
return response([
'data' => true
]);
}
}