ServerInformationController.php
5.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
<?php
namespace App\Http\Controllers\Aside\Devops;
use App\Enums\Common\Code;
use App\Exceptions\AsideGlobalException;
use App\Exceptions\BsideGlobalException;
use App\Http\Controllers\Aside\BaseController;
use App\Http\Logic\Aside\Devops\ServerInformationLogic;
use App\Http\Requests\Aside\Devops\ServerInformationRequest;
use App\Models\Devops\ServerInformation;
use App\Models\Devops\ServerInformationLog;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
class ServerInformationController extends BaseController
{
/**
* @return JsonResponse
*/
public function lists($deleted = ServerInformation::DELETED_NORMAL)
{
$request = $this->param;
$search = [];
$search_array = [
'ip' => 'ip',
'title' => 'title'
];
foreach ($search_array as $key => $item) {
if (isset($request[$key]) && $request[$key]) {
$search[$item] = $request[$key];
}
}
$size = request()->input('size', $this->row);
$query = ServerInformation::query()->select(['id', 'title', 'ip']);
if ($search) {
foreach ($search as $key => $item) {
$query = $query->Where("{$key}", 'like', "%{$item}%");
}
}
$data = $query->where('deleted', $deleted)
->orderBy('id', 'desc')
->paginate($size);
return $this->response('success', Code::SUCCESS, $data);
}
/**
* 添加
* @param ServerInformationRequest $serverInformationRequest
* @param ServerInformationLogic $serverInformationLogic
* @return void
* @throws AsideGlobalException
* @throws BsideGlobalException
*/
public function add(ServerInformationRequest $serverInformationRequest, ServerInformationLogic $serverInformationLogic)
{
$serverInformationRequest->validated();
$serverInformationLogic->create();
$this->response('服务器添加成功!');
}
/**
* 编辑
* @param ServerInformationRequest $serverInformationRequest
* @param ServerInformationLogic $serverInformationLogic
* @return void
* @throws AsideGlobalException
* @throws BsideGlobalException
*/
public function edit(ServerInformationRequest $serverInformationRequest, ServerInformationLogic $serverInformationLogic)
{
$serverInformationRequest->validate([
'id' => 'required|integer',
], [
'id.required' => 'ID不能为空',
'id.integer' => 'ID必须为整数',
]);
$serverInformationLogic->update();
$this->response('服务器修改成功!');
}
/**
* 删除
* @param ServerInformationLogic $serverInformationLogic
* @return void
* @throws AsideGlobalException
* @throws BsideGlobalException
*/
public function delete(ServerInformationLogic $serverInformationLogic)
{
$serverInformationLogic->get_batch_update();
$this->response('服务器删除成功!');
}
/**
* 获取软删除的数据
* @return JsonResponse
*/
public function delete_list()
{
return $this->lists(ServerInformation::DELETED_DELETE);
}
/**
* 恢复数据
* @param ServerInformationLogic $serverInformationLogic
* @return void
* @throws AsideGlobalException
* @throws BsideGlobalException
*/
public function restore(ServerInformationLogic $serverInformationLogic)
{
$serverInformationLogic->get_batch_update(ServerInformationLog::ACTION_RECOVER, ServerInformation::DELETED_DELETE);
$this->response('服务器恢复成功!');
}
/**
* 搜索
* @param Request $request
* @return JsonResponse
*/
public function search(Request $request)
{
$search = [];
$ip = $request->input('ip');
if ($ip) {
$search['ip'] = $ip;
}
$remark = $request->input('title');
if ($remark) {
$search['title'] = $remark;
}
if (empty($search)) {
return $this->response('请输入搜索内容', Code::USER_ERROR);
}
$query = ServerInformation::query()->select(['id', 'title', 'ip']);
foreach ($search as $key => $item) {
$query = $query->Where("{$key}", 'like', "%{$item}%");
}
$size = $request->input('size', $this->row);
$query = $query->orderBy('id', 'desc')->paginate($size);
return $this->response('success', Code::SUCCESS, $query);
}
/**
* 服务器信息
* @param int $deleted
* @return JsonResponse
* @throws AsideGlobalException
* @throws BsideGlobalException
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function getServerInfo(int $deleted = ServerInformation::DELETED_NORMAL)
{
$serverInformationLogic = new ServerInformationLogic();
$data = $serverInformationLogic->serverInfo($deleted);
if (!$data) {
return $this->response('服务器信息不存在', Code::USER_ERROR);
}
return $this->success($data->toArray());
}
/**
* 获取软删除的服务器信息
* @return JsonResponse
* @throws AsideGlobalException
* @throws BsideGlobalException
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function getDeleteServerInfo()
{
return $this->getServerInfo(ServerInformation::DELETED_DELETE);
}
}