ServerInformationLogic.php
8.3 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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
<?php
namespace App\Http\Logic\Aside\Devops;
use App\Enums\Common\Code;
use App\Exceptions\AsideGlobalException;
use App\Exceptions\BsideGlobalException;
use App\Http\Logic\Aside\BaseLogic;
use App\Models\Devops\ServerInformation;
use App\Models\Devops\ServerInformationLog;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
class ServerInformationLogic extends BaseLogic
{
/**
* @var array
*/
private $param;
public function __construct()
{
parent::__construct();
$this->model = new ServerInformation();
$this->param = $this->requestAll;
}
/**
* 添加数据
* @return array
* @throws AsideGlobalException
* @throws BsideGlobalException
*/
public function create()
{
$request = $this->param ?? [];
$service = new ServerInformation();
$this->extracted( $request, $service );
if ( $this->checkIp( $service->ip ) ) {
return $this->fail( '服务器信息添加失败,ip已存在' );
}
DB::beginTransaction();
if ( $service->save() ) {
$original = $service->getOriginal();
$original['type'] = $request['type'];
$original['belong_to'] = $request['belong_to'];
// 添加日志
$this->server_action_log( ServerInformationLog::ACTION_ADD, $original, $original, '添加服务器信息成功 - ID : ' . $service->id );
DB::commit();
return $this->success();
}
DB::rollBack();
return $this->fail( '服务器信息添加失败' );
}
/**
* 修改数据
* @return array
* @throws AsideGlobalException
* @throws BsideGlobalException
*/
public function update()
{
$service = $this->getService();
$fields_array = $service->FieldsArray();
$request = $this->param ?? [];
$original = $service->getOriginal();
$original['type'] = $service->ServiceStr( $original['type'] );
$original['belong_to'] = $service->BelongToStr( $original['belong_to'] );
$this->extracted( $request, $service, $original );
// 检查ip是否存在
if ( $service->ip != $request['ip'] ) {
if ( $this->checkIp( $request['ip'] ) ) {
$this->fail( '服务器信息修改失败,ip已存在', Code::USER_ERROR );
}
}
DB::beginTransaction();
if ( $service->save() ) {
$revised = $service->getAttributes();
$diff = array_diff_assoc( $original, $revised );
unset( $diff['created_at'] );
unset( $diff['updated_at'] );
unset( $diff['deleted'] );
$remarks = '';
if ( $diff ) {
$remarks .= '修改ID为 ' . $service->id . ' 的服务器信息,修改内容为:';
foreach ( $diff as $key => $value ) {
$remarks .= $fields_array[$key] . ' 由 ' . $value . ' 修改为 ' . $revised[$key] . '; ';
}
} else {
$remarks .= '修改ID为 ' . $service->id . ' 的服务器信息,无修改';
}
// 添加日志
$this->server_action_log( ServerInformationLog::ACTION_UPDATE, $original, $revised, $remarks );
DB::commit();
return $this->success();
}
DB::rollBack();
return $this->fail( '服务器信息修改失败' );
}
public function getService( int $deleted = ServerInformation::DELETED_NORMAL )
{
$id = $this->param['id'] ?? 0;
if ( !$id ) {
return $this->fail( 'ID不能为空' );
}
$data = ServerInformation::query()->where( 'deleted', $deleted )->find( $id );
if ( !$data ) {
return $this->fail( '数据不存在!' );
}
return $data;
}
/**
* 检查ip是否存在
* @param $ip
* @return bool
*/
public function checkIp( $ip )
{
$usIp = ServerInformation::query()->where( 'ip', $ip )->first();
if ( $usIp ) {
return true;
} else {
return false;
}
}
/**
* 详情
* @param int $deleted
* @return array|Builder|Collection|Model
* @throws AsideGlobalException
* @throws BsideGlobalException
*/
public function serverInfo( int $deleted = ServerInformation::DELETED_NORMAL )
{
$id = $this->param['id'] ?? 0;
if ( !$id ) {
return $this->fail( 'ID不能为空' );
}
$data = ServerInformation::query()->where( 'deleted', $deleted )->find( $id, [ 'type', 'ip', 'title', 'belong_to', 'ports', 'created_at', 'updated_at' ] );
if ( !$data ) {
return $this->fail( '数据不存在!' );
}
return $data;
}
/**
* 服务器操作日志
* @param int $action 1:添加 2:修改 3:删除 4:搜索 5:详情 6:列表
* @param array $original 原始数据
* @param array $revised 修改后数据
*/
public function server_action_log( int $action = ServerInformationLog::ACTION_ADD, array $original = [], array $revised = [], $remarks = '' )
{
$log = new ServerInformationLog();
$this->log( $log, $action, $original, $revised, $remarks );
}
/**
* @param array $request
* @param $service
* @return void
*/
public function extracted( array $request, $service, array $original = [] )
{
$service->type = trim( $request['type'] ) ?? $original['type']; // 服务器类型
$service->ip = trim( $request['ip'] ) ?? $original['ip']; // 服务器ip
$service->title = trim( $request['title'] ) ?? $original['title']; // 服务器标题
$service->belong_to = trim( $request['belong_to'] ) ?? $original['belong_to']; // 服务器归属
$service->sshpass = trim( $request['sshpass'] ) ?? $original['sshpass']; // ssh密码
$service->ports = trim( $request['ports'] ) ?? $original['ports']; // ssh端口
}
/**
* 批量获取数据删除
* @return array
* @throws AsideGlobalException
* @throws BsideGlobalException
*/
public function get_batch_update( $action = ServerInformationLog::ACTION_DELETE, $deleted = ServerInformation::DELETED_NORMAL )
{
$ids = $this->getIds();
$data = ServerInformation::query()->whereIn( 'id', $ids )->where( 'deleted', $deleted )->get();
$restore_ids = $data->pluck( 'id' )->toArray();
$actionArr = ServerInformationLog::actionArr();
$actionStr = $actionArr[$action];
if ( empty( $restore_ids ) ) {
$this->fail( $actionStr . '服务器信息不存在!', Code::USER_ERROR );
}
$original = $data->toArray();
DB::beginTransaction();
try {
$update = $deleted == ServerInformation::DELETED_NORMAL ? ServerInformation::DELETED_DELETE : ServerInformation::DELETED_NORMAL;
ServerInformation::query()->whereIn( 'id', $restore_ids )->update( [ 'deleted' => $update ] );
$this->server_action_log( $action, $original, $original, $actionStr . '服务器信息 - ID : ' . implode( ', ', $restore_ids ) );
DB::commit();
return $this->success();
} catch ( \Exception $e ) {
DB::rollBack();
return $this->fail( '服务器信息' . $actionStr . '失败', Code::USER_ERROR );
}
}
/**
* 批量获取数据恢复
* @return array
* @throws AsideGlobalException
* @throws BsideGlobalException
*/
public function getIds()
{
$id = $this->param['id'] ?? 0;
if ( !$id ) {
return $this->fail( '参数错误' );
}
$ids = [];
if ( !is_array( $id ) ) {
$ids = explode( ',', $id );
}
$ids = array_filter( $ids, 'intval' );
if ( empty( $ids ) ) {
return $this->fail( '参数错误' );
}
return $ids;
}
}