|
1
|
-<?php
|
|
|
|
2
|
-
|
|
|
|
3
|
-namespace App\Http\Logic\Aside\Devops;
|
|
|
|
4
|
-
|
|
|
|
5
|
-
|
|
|
|
6
|
-use App\Enums\Common\Code;
|
|
|
|
7
|
-use App\Exceptions\AsideGlobalException;
|
|
|
|
8
|
-use App\Exceptions\BsideGlobalException;
|
|
|
|
9
|
-use App\Http\Logic\Aside\BaseLogic;
|
|
|
|
10
|
-use App\Models\Devops\ServerInformation;
|
|
|
|
11
|
-use App\Models\Devops\ServerInformationLog;
|
|
|
|
12
|
-use Illuminate\Database\Eloquent\Builder;
|
|
|
|
13
|
-use Illuminate\Database\Eloquent\Collection;
|
|
|
|
14
|
-use Illuminate\Database\Eloquent\Model;
|
|
|
|
15
|
-use Illuminate\Support\Facades\DB;
|
|
|
|
16
|
-
|
|
|
|
17
|
-class ServerInformationLogic extends BaseLogic
|
|
|
|
18
|
-{
|
|
|
|
19
|
- /**
|
|
|
|
20
|
- * @var array
|
|
|
|
21
|
- */
|
|
|
|
22
|
- private $param;
|
|
|
|
23
|
-
|
|
|
|
24
|
- public function __construct()
|
|
|
|
25
|
- {
|
|
|
|
26
|
- parent::__construct();
|
|
|
|
27
|
-
|
|
|
|
28
|
- $this->param = $this->requestAll;
|
|
|
|
29
|
-
|
|
|
|
30
|
- }
|
|
|
|
31
|
-
|
|
|
|
32
|
- /**
|
|
|
|
33
|
- * 添加数据
|
|
|
|
34
|
- * @return array
|
|
|
|
35
|
- * @throws AsideGlobalException
|
|
|
|
36
|
- * @throws BsideGlobalException
|
|
|
|
37
|
- */
|
|
|
|
38
|
- public function create()
|
|
|
|
39
|
- {
|
|
|
|
40
|
- $request = $this->param ?? [];
|
|
|
|
41
|
- $service = new ServerInformation();
|
|
|
|
42
|
- $this->extracted($request, $service, $service->FieldsArray());
|
|
|
|
43
|
- if ($this->checkIp($request['ip'])) {
|
|
|
|
44
|
- return $this->fail('服务器信息添加失败,ip已存在');
|
|
|
|
45
|
- }
|
|
|
|
46
|
- DB::beginTransaction();
|
|
|
|
47
|
- if ($service->save()) {
|
|
|
|
48
|
- $original = $service->getOriginal();
|
|
|
|
49
|
- $original['type'] = $request['type'];
|
|
|
|
50
|
- $original['belong_to'] = $request['belong_to'];
|
|
|
|
51
|
- // 添加日志
|
|
|
|
52
|
- $this->server_action_log(ServerInformationLog::ACTION_ADD, $original, $original, '添加服务器信息成功 - ID : ' . $service->id);
|
|
|
|
53
|
- DB::commit();
|
|
|
|
54
|
- return $this->success();
|
|
|
|
55
|
- }
|
|
|
|
56
|
- DB::rollBack();
|
|
|
|
57
|
- return $this->fail('服务器信息添加失败');
|
|
|
|
58
|
-
|
|
|
|
59
|
- }
|
|
|
|
60
|
-
|
|
|
|
61
|
- /**
|
|
|
|
62
|
- * 修改数据
|
|
|
|
63
|
- * @return array
|
|
|
|
64
|
- * @throws AsideGlobalException
|
|
|
|
65
|
- * @throws BsideGlobalException
|
|
|
|
66
|
- */
|
|
|
|
67
|
- public function update()
|
|
|
|
68
|
- {
|
|
|
|
69
|
- $service = $this->getService();
|
|
|
|
70
|
- $fields_array = $service->FieldsArray();
|
|
|
|
71
|
- $request = $this->param ?? [];
|
|
|
|
72
|
- $original = $service->getOriginal();
|
|
|
|
73
|
- $original['type'] = $service->ServiceStr($original['type']);
|
|
|
|
74
|
- $original['belong_to'] = $service->BelongToStr($original['belong_to']);
|
|
|
|
75
|
- $this->extracted($request, $service, $original);
|
|
|
|
76
|
- // 检查ip是否存在
|
|
|
|
77
|
- if ($service->ip != $request['ip']) {
|
|
|
|
78
|
- if ($this->checkIp($request['ip'])) {
|
|
|
|
79
|
- $this->fail('服务器信息修改失败,ip已存在', Code::USER_ERROR);
|
|
|
|
80
|
- }
|
|
|
|
81
|
- }
|
|
|
|
82
|
- DB::beginTransaction();
|
|
|
|
83
|
- if ($service->save()) {
|
|
|
|
84
|
- $revised = $service->getAttributes();
|
|
|
|
85
|
- $diff = array_diff_assoc($original, $revised);
|
|
|
|
86
|
- unset($diff['created_at']);
|
|
|
|
87
|
- unset($diff['updated_at']);
|
|
|
|
88
|
- unset($diff['deleted']);
|
|
|
|
89
|
- $remarks = '';
|
|
|
|
90
|
- if ($diff) {
|
|
|
|
91
|
- $remarks .= '修改ID为 ' . $service->id . ' 的服务器信息,修改内容为:';
|
|
|
|
92
|
- foreach ($diff as $key => $value) {
|
|
|
|
93
|
- $remarks .= $fields_array[$key] . ' 由 ' . $value . ' 修改为 ' . $revised[$key] . '; ';
|
|
|
|
94
|
- }
|
|
|
|
95
|
- } else {
|
|
|
|
96
|
- $remarks .= '修改ID为 ' . $service->id . ' 的服务器信息,无修改';
|
|
|
|
97
|
- }
|
|
|
|
98
|
- // 添加日志
|
|
|
|
99
|
- $this->server_action_log(ServerInformationLog::ACTION_UPDATE, $original, $revised, $remarks);
|
|
|
|
100
|
- DB::commit();
|
|
|
|
101
|
- return $this->success();
|
|
|
|
102
|
- }
|
|
|
|
103
|
- DB::rollBack();
|
|
|
|
104
|
- return $this->fail('服务器信息修改失败');
|
|
|
|
105
|
- }
|
|
|
|
106
|
-
|
|
|
|
107
|
- public function getService(int $deleted = ServerInformation::DELETED_NORMAL)
|
|
|
|
108
|
- {
|
|
|
|
109
|
- $id = $this->param['id'] ?? 0;
|
|
|
|
110
|
- if (!$id) {
|
|
|
|
111
|
- return $this->fail('ID不能为空');
|
|
|
|
112
|
- }
|
|
|
|
113
|
- $data = ServerInformation::query()->where('deleted', $deleted)->find($id);
|
|
|
|
114
|
- if (!$data) {
|
|
|
|
115
|
- return $this->fail('数据不存在!');
|
|
|
|
116
|
- }
|
|
|
|
117
|
- return $data;
|
|
|
|
118
|
- }
|
|
|
|
119
|
-
|
|
|
|
120
|
- /**
|
|
|
|
121
|
- * 检查ip是否存在
|
|
|
|
122
|
- * @param $ip
|
|
|
|
123
|
- * @return bool
|
|
|
|
124
|
- */
|
|
|
|
125
|
- public function checkIp($ip)
|
|
|
|
126
|
- {
|
|
|
|
127
|
- $usIp = ServerInformation::query()->where('ip', $ip)->first();
|
|
|
|
128
|
- if ($usIp) {
|
|
|
|
129
|
- return true;
|
|
|
|
130
|
- } else {
|
|
|
|
131
|
- return false;
|
|
|
|
132
|
- }
|
|
|
|
133
|
- }
|
|
|
|
134
|
-
|
|
|
|
135
|
- /**
|
|
|
|
136
|
- * 详情
|
|
|
|
137
|
- * @param int $deleted
|
|
|
|
138
|
- * @return array|Builder|Collection|Model
|
|
|
|
139
|
- * @throws AsideGlobalException
|
|
|
|
140
|
- * @throws BsideGlobalException
|
|
|
|
141
|
- */
|
|
|
|
142
|
- public function serverInfo(int $deleted = ServerInformation::DELETED_NORMAL)
|
|
|
|
143
|
- {
|
|
|
|
144
|
- $id = $this->param['id'] ?? 0;
|
|
|
|
145
|
- if (!$id) {
|
|
|
|
146
|
- return $this->fail('ID不能为空');
|
|
|
|
147
|
- }
|
|
|
|
148
|
- $data = ServerInformation::query()->where('deleted', $deleted)->find($id, ['type', 'ip', 'title', 'belong_to', 'ports', 'created_at', 'updated_at']);
|
|
|
|
149
|
- if (!$data) {
|
|
|
|
150
|
- return $this->fail('数据不存在!');
|
|
|
|
151
|
- }
|
|
|
|
152
|
- return $data;
|
|
|
|
153
|
- }
|
|
|
|
154
|
-
|
|
|
|
155
|
- /**
|
|
|
|
156
|
- * 服务器操作日志
|
|
|
|
157
|
- * @param int $action 1:添加 2:修改 3:删除 4:搜索 5:详情 6:列表
|
|
|
|
158
|
- * @param array $original 原始数据
|
|
|
|
159
|
- * @param array $revised 修改后数据
|
|
|
|
160
|
- */
|
|
|
|
161
|
- public function server_action_log(int $action = ServerInformationLog::ACTION_ADD, array $original = [], array $revised = [], $remarks = '')
|
|
|
|
162
|
- {
|
|
|
|
163
|
- $log = new ServerInformationLog();
|
|
|
|
164
|
- $this->log($log, $action, $original, $revised, $remarks);
|
|
|
|
165
|
- }
|
|
|
|
166
|
-
|
|
|
|
167
|
- /**
|
|
|
|
168
|
- * 提取数据
|
|
|
|
169
|
- * @param array $request
|
|
|
|
170
|
- * @param $service
|
|
|
|
171
|
- * @param array $original
|
|
|
|
172
|
- * @return void
|
|
|
|
173
|
- */
|
|
|
|
174
|
- public function extracted(array $request, $service, array $original)
|
|
|
|
175
|
- {
|
|
|
|
176
|
- $request = array_intersect_key($request, $original);
|
|
|
|
177
|
- foreach ($request as $key => $value) {
|
|
|
|
178
|
- $service->$key = trim($value) ?? $original[$key];
|
|
|
|
179
|
- }
|
|
|
|
180
|
- }
|
|
|
|
181
|
-
|
|
|
|
182
|
- /**
|
|
|
|
183
|
- * 批量获取数据删除
|
|
|
|
184
|
- * @return array
|
|
|
|
185
|
- * @throws AsideGlobalException
|
|
|
|
186
|
- * @throws BsideGlobalException
|
|
|
|
187
|
- */
|
|
|
|
188
|
- public function get_batch_update($action = ServerInformationLog::ACTION_DELETE, $deleted = ServerInformation::DELETED_NORMAL)
|
|
|
|
189
|
- {
|
|
|
|
190
|
- $ids = $this->getIds();
|
|
|
|
191
|
- $data = ServerInformation::query()->whereIn('id', $ids)->where('deleted', $deleted)->get();
|
|
|
|
192
|
- $restore_ids = $data->pluck('id')->toArray();
|
|
|
|
193
|
- $actionArr = ServerInformationLog::actionArr();
|
|
|
|
194
|
- $actionStr = $actionArr[$action];
|
|
|
|
195
|
- if (empty($restore_ids)) {
|
|
|
|
196
|
- $this->fail($actionStr . '服务器信息不存在!', Code::USER_ERROR);
|
|
|
|
197
|
- }
|
|
|
|
198
|
- $original = $data->toArray();
|
|
|
|
199
|
- DB::beginTransaction();
|
|
|
|
200
|
- try {
|
|
|
|
201
|
- $update = $deleted == ServerInformation::DELETED_NORMAL ? ServerInformation::DELETED_DELETE : ServerInformation::DELETED_NORMAL;
|
|
|
|
202
|
- ServerInformation::query()->whereIn('id', $restore_ids)->update(['deleted' => $update]);
|
|
|
|
203
|
- $this->server_action_log($action, $original, $original, $actionStr . '服务器信息 - ID : ' . implode(', ', $restore_ids));
|
|
|
|
204
|
- DB::commit();
|
|
|
|
205
|
- return $this->success();
|
|
|
|
206
|
- } catch (\Exception $e) {
|
|
|
|
207
|
- DB::rollBack();
|
|
|
|
208
|
- return $this->fail('服务器信息' . $actionStr . '失败', Code::USER_ERROR);
|
|
|
|
209
|
- }
|
|
|
|
210
|
- }
|
|
|
|
211
|
-
|
|
|
|
212
|
- /**
|
|
|
|
213
|
- * 批量获取数据恢复
|
|
|
|
214
|
- * @return array
|
|
|
|
215
|
- * @throws AsideGlobalException
|
|
|
|
216
|
- * @throws BsideGlobalException
|
|
|
|
217
|
- */
|
|
|
|
218
|
- public function getIds()
|
|
|
|
219
|
- {
|
|
|
|
220
|
- $id = $this->param['id'] ?? 0;
|
|
|
|
221
|
- if (!$id) {
|
|
|
|
222
|
- return $this->fail('参数错误');
|
|
|
|
223
|
- }
|
|
|
|
224
|
- $ids = [];
|
|
|
|
225
|
- if (!is_array($id)) {
|
|
|
|
226
|
- $ids = explode(',', $id);
|
|
|
|
227
|
- }
|
|
|
|
228
|
- $ids = array_filter($ids, 'intval');
|
|
|
|
229
|
- if (empty($ids)) {
|
|
|
|
230
|
- return $this->fail('参数错误');
|
|
|
|
231
|
- }
|
|
|
|
232
|
- return $ids;
|
|
|
|
233
|
- }
|
|
|
|
234
|
-} |
|
|