更加 维护计划

This commit is contained in:
iVampireSP.com 2023-02-17 14:48:22 +08:00
parent c3e1471d3f
commit f9e2ee2bed
No known key found for this signature in database
GPG Key ID: 2F7B001CA27A8132
8 changed files with 343 additions and 0 deletions

View File

@ -0,0 +1,106 @@
<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Models\Maintenance;
use App\Models\Module;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\View\View;
class MaintenanceController extends Controller
{
/**
* Display a listing of the resource.
*
* @return View
*/
public function index(): View
{
$maintenances = (new Maintenance)->all();
return view('admin.maintenances.index', compact('maintenances'));
}
/**
* Show the form for creating a new resource.
*
* @return View
*/
public function create(): View
{
$modules = (new Module)->all();
return view('admin.maintenances.create', compact('modules'));
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request): RedirectResponse
{
$request->validate([
'name' => 'required|string|max:255',
'content' => 'nullable|string',
'module_id' => 'nullable|string|max:255',
'start_at' => 'nullable|date',
'end_at' => 'nullable|date',
]);
(new Maintenance())->create($request->all());
return redirect()->route('admin.maintenances.index')->with('success', '维护信息已创建');
}
/**
* Show the form for editing the specified resource.
*
* @param Maintenance $maintenance
* @return View
*/
public function edit(Maintenance $maintenance): View
{
$modules = (new Module)->all();
return view('admin.maintenances.edit', compact('maintenance', 'modules'));
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param Maintenance $maintenance
* @return \Illuminate\Http\Response
*/
public function update(Request $request, Maintenance $maintenance): RedirectResponse
{
$request->validate([
'name' => 'nullable|string|max:255',
'content' => 'nullable|string',
'module_id' => 'nullable|string|max:255',
'start_at' => 'nullable|date',
'end_at' => 'nullable|date',
]);
$maintenance->update($request->all());
return redirect()->route('admin.maintenances.index')->with('success', '维护信息已更新');
}
/**
* Remove the specified resource from storage.
*
* @param Maintenance $maintenance
* @return \Illuminate\Http\Response
*/
public function destroy(Maintenance $maintenance): RedirectResponse
{
$maintenance->delete();
return redirect()->route('admin.maintenances.index')->with('success', '维护信息已删除');
}
}

View File

@ -0,0 +1,37 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Maintenance extends Model
{
public $timestamps = false;
protected $fillable = [
'name',
'content',
'module_id',
'start_at',
'end_at',
];
protected $casts = [
'start_at' => 'datetime',
'end_at' => 'datetime',
];
protected $dates = [
'start_at',
'end_at',
];
protected $with = [
'module',
];
public function module()
{
return $this->belongsTo(Module::class);
}
}

View File

@ -0,0 +1,45 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up(): void
{
Schema::create('maintenances', function (Blueprint $table) {
$table->id();
// 维护名称
$table->string('name')->index();
// 内容
$table->text('content')->nullable();
// 模块 ID
$table->string('module_id')->index()->nullable();
$table->foreign('module_id')->references('id')->on('modules')->onDelete('set null');
// 开始于
$table->dateTime('start_at')->nullable();
$table->dateTime('end_at')->nullable();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down(): void
{
Schema::dropIfExists('maintenances');
}
};

View File

@ -0,0 +1,46 @@
@extends('layouts.admin')
@section('title', '计划维护')
@section('content')
<h3>计划维护</h3>
<a class="mt-3" href="{{ route('admin.maintenances.index') }}">返回计划列表</a>
<form method="POST" action="{{ route('admin.maintenances.store') }}">
@csrf
<div class="form-group mt-1">
<label for="name">维护名称</label>
<input type="text" class="form-control" id="name" name="name" placeholder="维护名称" required>
</div>
<div class="form-group mt-1">
<label for="content">维护内容</label>
<textarea class="form-control" id="content" name="content" placeholder="维护内容" required></textarea>
</div>
{{-- 模块 ID --}}
<div class="form-group mt-1">
<label for="module_id">模块</label>
<select class="form-control" id="module_id" name="module_id">
<option value=""></option>
@foreach ($modules as $m)
<option value="{{ $m->id }}">{{ $m->name }}</option>
@endforeach
</select>
</div>
<div class="form-group mt-1">
<label for="start_at">开始时间</label>
<input type="datetime-local" class="form-control" id="start_at" name="start_at" placeholder="开始时间">
</div>
<div class="form-group mt-1">
<label for="end_at">结束时间</label>
<input type="datetime-local" class="form-control" id="end_at" name="end_at" placeholder="结束时间">
</div>
<button type="submit" class="btn btn-primary mt-3">添加</button>
</form>
@endsection

View File

@ -0,0 +1,47 @@
@extends('layouts.admin')
@section('title', '编辑: ' . $maintenance->name)
@section('content')
<h3>{{ $maintenance->name }}</h3>
<a class="mt-3" href="{{ route('admin.maintenances.index') }}">返回计划列表</a>
<form method="POST" action="{{ route('admin.maintenances.update', $maintenance) }}">
@csrf
@method('PATCH')
<div class="form-group mt-1">
<label for="name">维护名称</label>
<input type="text" class="form-control" id="name" name="name" placeholder="维护名称" value={{ $maintenance->name }} required>
</div>
<div class="form-group mt-1">
<label for="content">维护内容</label>
<textarea class="form-control" id="content" name="content" placeholder="维护内容" required>{{ $maintenance->content }}</textarea>
</div>
{{-- 模块 ID --}}
<div class="form-group mt-1">
<label for="module_id">模块</label>
<select class="form-control" id="module_id" name="module_id">
<option value=""></option>
@foreach ($modules as $m)
<option value="{{ $m->id }}" @if($maintenance->module_id === $m->id) selected @endif>{{ $m->name }}</option>
@endforeach
</select>
</div>
<div class="form-group mt-1">
<label for="start_at">开始时间</label>
<input type="datetime-local" class="form-control" id="start_at" name="start_at" value="{{ $maintenance->start_at }}" placeholder="开始时间">
</div>
<div class="form-group mt-1">
<label for="end_at">结束时间</label>
<input type="datetime-local" class="form-control" id="end_at" name="end_at" value="{{ $maintenance->end_at }}" placeholder="结束时间">
</div>
<button type="submit" class="btn btn-primary mt-3">更新</button>
</form>
@endsection

View File

@ -0,0 +1,56 @@
@extends('layouts.admin')
@section('title', '维护计划')
@section('content')
<h3>维护计划</h3>
<a class="mt-3" href="{{ route('admin.maintenances.create') }}">添加计划</a>
<div class="overflow-auto">
<table class="table table-hover">
<thead>
<th>ID</th>
<th>名称</th>
<th>模块</th>
<th>开始于</th>
<th>结束于</th>
<th>操作</th>
</thead>
<tbody>
@foreach ($maintenances as $m)
<tr>
<td>
<a href="{{ route('admin.maintenances.edit', $m) }}">
{{ $m->id }}
</a>
</td>
<td>
<a href="{{ route('admin.maintenances.edit', $m) }}">
{{ $m->name }}
</a>
</td>
<td>
{{ $m->module?->name }}
</td>
<td>
{{ $m->start_at }}
</td>
<td>
{{ $m->end_at }}
</td>
<td>
<a href="{{ route('admin.maintenances.edit', $m) }}" class="btn btn-primary btn-sm">编辑</a>
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
@endsection

View File

@ -75,6 +75,9 @@
<li>
<a class="dropdown-item" href="{{ route('admin.notifications.create') }}">通知</a>
</li>
<li>
<a class="dropdown-item" href="{{ route('admin.maintenances.index') }}">维护</a>
</li>
</ul>
</li>

View File

@ -6,6 +6,7 @@
use App\Http\Controllers\Admin\DeviceController;
use App\Http\Controllers\Admin\HomeController;
use App\Http\Controllers\Admin\HostController;
use App\Http\Controllers\Admin\MaintenanceController;
use App\Http\Controllers\Admin\ModuleController;
use App\Http\Controllers\Admin\NodeController;
use App\Http\Controllers\Admin\NotificationController;
@ -39,6 +40,8 @@
Route::resource('user-groups', UserGroupController::class);
Route::resource('maintenances', MaintenanceController::class);
Route::get('devices', [DeviceController::class, 'index'])->name('devices.index');
Route::delete('devices', [DeviceController::class, 'destroy'])->name('devices.destroy');