feature: order filter

This commit is contained in:
tokumeikoi 2021-03-25 17:59:28 +09:00
parent ff30d3dcb2
commit 488eafdd67
5 changed files with 79 additions and 29 deletions

View File

@ -4,6 +4,7 @@ namespace App\Http\Controllers\Admin;
use App\Http\Requests\Admin\OrderAssign; use App\Http\Requests\Admin\OrderAssign;
use App\Http\Requests\Admin\OrderUpdate; use App\Http\Requests\Admin\OrderUpdate;
use App\Http\Requests\Admin\OrderFetch;
use App\Services\OrderService; use App\Services\OrderService;
use App\Utils\Helper; use App\Utils\Helper;
use Illuminate\Http\Request; use Illuminate\Http\Request;
@ -15,25 +16,36 @@ use Illuminate\Support\Facades\DB;
class OrderController extends Controller class OrderController extends Controller
{ {
public function fetch(Request $request) 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)
{ {
$current = $request->input('current') ? $request->input('current') : 1; $current = $request->input('current') ? $request->input('current') : 1;
$pageSize = $request->input('pageSize') >= 10 ? $request->input('pageSize') : 10; $pageSize = $request->input('pageSize') >= 10 ? $request->input('pageSize') : 10;
$orderModel = Order::orderBy('created_at', 'DESC'); $orderModel = Order::orderBy('created_at', 'DESC');
if ($request->input('trade_no')) {
$orderModel->where('trade_no', $request->input('trade_no'));
}
if ($request->input('is_commission')) { if ($request->input('is_commission')) {
$orderModel->where('invite_user_id', '!=', NULL); $orderModel->where('invite_user_id', '!=', NULL);
$orderModel->whereNotIn('status', [0, 2]); $orderModel->whereNotIn('status', [0, 2]);
$orderModel->where('commission_balance', '>', 0); $orderModel->where('commission_balance', '>', 0);
} }
if ($request->input('id')) { $this->filter($request, $orderModel);
$orderModel->where('id', $request->input('id'));
}
if ($request->input('user_id')) {
$orderModel->where('user_id', $request->input('user_id'));
}
$total = $orderModel->count(); $total = $orderModel->count();
$res = $orderModel->forPage($current, $pageSize) $res = $orderModel->forPage($current, $pageSize)
->get(); ->get();

View File

@ -0,0 +1,33 @@
<?php
namespace App\Http\Requests\Admin;
use Illuminate\Foundation\Http\FormRequest;
class OrderFetch extends FormRequest
{
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'filter.*.key' => 'required|in:email,trade_no,status,commission_status,user_id',
'filter.*.condition' => 'required|in:>,<,=,>=,<=,模糊,!=',
'filter.*.value' => 'required'
];
}
public function messages()
{
return [
'filter.*.key.required' => '过滤键不能为空',
'filter.*.key.in' => '过滤键参数有误',
'filter.*.condition.required' => '过滤条件不能为空',
'filter.*.condition.in' => '过滤条件参数有误',
'filter.*.value.required' => '过滤值不能为空'
];
}
}

View File

@ -15,7 +15,7 @@ class UserFetch extends FormRequest
{ {
return [ return [
'filter.*.key' => 'required|in:id,email,transfer_enable,d,expired_at,uuid,token,invite_by_email,invite_user_id,plan_id,banned', 'filter.*.key' => 'required|in:id,email,transfer_enable,d,expired_at,uuid,token,invite_by_email,invite_user_id,plan_id,banned',
'filter.*.condition' => 'required|in:>,<,=,>=,<=,模糊', 'filter.*.condition' => 'required|in:>,<,=,>=,<=,模糊,!=',
'filter.*.value' => 'required' 'filter.*.value' => 'required'
]; ];
} }
@ -23,6 +23,11 @@ class UserFetch extends FormRequest
public function messages() public function messages()
{ {
return [ return [
'filter.*.key.required' => '过滤键不能为空',
'filter.*.key.in' => '过滤键参数有误',
'filter.*.condition.required' => '过滤条件不能为空',
'filter.*.condition.in' => '过滤条件参数有误',
'filter.*.value.required' => '过滤值不能为空'
]; ];
} }
} }

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long