update sql

This commit is contained in:
Tokumeikoi 2020-04-16 23:01:18 +08:00
parent 39ae037080
commit 50b5ed6b8e
5 changed files with 73 additions and 0 deletions

View File

@ -3,9 +3,11 @@
namespace App\Http\Controllers\Admin; namespace App\Http\Controllers\Admin;
use App\Http\Requests\Admin\TutorialSave; use App\Http\Requests\Admin\TutorialSave;
use App\Http\Requests\Admin\TutorialSort;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use App\Http\Controllers\Controller; use App\Http\Controllers\Controller;
use App\Models\Tutorial; use App\Models\Tutorial;
use Illuminate\Support\Facades\DB;
class TutorialController extends Controller class TutorialController extends Controller
{ {
@ -56,6 +58,33 @@ class TutorialController extends Controller
]); ]);
} }
public function sort(TutorialSort $request)
{
$sort = $request->input('sort');
$tutorial = Tutorial::find($request->input('id'))->first();
if (!$tutorial) {
abort(500, '教程不存在');
}
DB::beginTransaction();
$tutorial->sort = $sort;
if (!$tutorial->save()) {
DB::rollBack();
abort(500, '保存失败');
}
$tutorials = Tutorial::where('sort', '>', $sort)->get();
foreach ($tutorials as $tutorial) {
$sort++;
if (!$tutorial->save(['sort' => $sort])) {
abort(500, '保存失败');
}
}
DB::commit();
return response([
'data' => true
]);
}
public function drop(Request $request) public function drop(Request $request)
{ {
if (empty($request->input('id'))) { if (empty($request->input('id'))) {

View File

@ -6,6 +6,7 @@ use App\Http\Controllers\Controller;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use App\Models\User; use App\Models\User;
use App\Models\Tutorial; use App\Models\Tutorial;
use Illuminate\Support\Facades\DB;
class TutorialController extends Controller class TutorialController extends Controller
{ {

View File

@ -0,0 +1,30 @@
<?php
namespace App\Http\Requests\Admin;
use Illuminate\Foundation\Http\FormRequest;
class TutorialSort extends FormRequest
{
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'id' => 'required',
'sort' => 'required|integer'
];
}
public function messages()
{
return [
'id.required' => '教程ID不能为空',
'sort.required' => '排序不能为空',
'sort.integer' => '排序格式有误'
];
}
}

View File

@ -60,6 +60,7 @@ class AdminRoute
$router->post('/tutorial/save', 'Admin\\TutorialController@save'); $router->post('/tutorial/save', 'Admin\\TutorialController@save');
$router->post('/tutorial/show', 'Admin\\TutorialController@show'); $router->post('/tutorial/show', 'Admin\\TutorialController@show');
$router->post('/tutorial/drop', 'Admin\\TutorialController@drop'); $router->post('/tutorial/drop', 'Admin\\TutorialController@drop');
$router->post('/tutorial/sort', 'Admin\\TutorialController@sort');
}); });
} }
} }

View File

@ -210,3 +210,15 @@ ADD `surplus_order_ids` varchar(255) NULL COMMENT '折抵订单' AFTER `balance_
ALTER TABLE `v2_order` ALTER TABLE `v2_order`
CHANGE `status` `status` tinyint(1) NOT NULL DEFAULT '0' COMMENT '0待支付1开通中2已取消3已完成4已折抵' AFTER `surplus_order_ids`; CHANGE `status` `status` tinyint(1) NOT NULL DEFAULT '0' COMMENT '0待支付1开通中2已取消3已完成4已折抵' AFTER `surplus_order_ids`;
CREATE TABLE `v2_server_stat` (
`id` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
`server_id` int(11) NOT NULL,
`u` varchar(255) NOT NULL,
`d` varchar(25) NOT NULL,
`created_at` int(11) NOT NULL,
`updated_at` int(11) NOT NULL
);
ALTER TABLE `v2_tutorial`
ADD `sort` int(11) NULL AFTER `show`;