1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace aspirantzhang\octopusRevision; |
6
|
|
|
|
7
|
|
|
use think\facade\Db; |
8
|
|
|
use think\Exception; |
9
|
|
|
|
10
|
|
|
class RevisionAPI |
11
|
|
|
{ |
12
|
|
|
public function restoreAPI(string $tableName, int $originalId, int $revisionId) |
13
|
|
|
{ |
14
|
|
|
try { |
15
|
|
|
(new Revision($tableName, $originalId))->restore($revisionId); |
16
|
|
|
return [ |
17
|
|
|
'success' => true, |
18
|
|
|
'message' => __('revision restore successfully'), |
19
|
|
|
'data' => [] |
20
|
|
|
]; |
21
|
|
|
} catch (Exception $e) { |
22
|
|
|
return [ |
23
|
|
|
'success' => false, |
24
|
|
|
'message' => $e->getMessage(), |
25
|
|
|
'data' => [] |
26
|
|
|
]; |
27
|
|
|
} |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function listAPI(string $tableName, int $recordId, int $page = 1, int $perPage = 5) |
31
|
|
|
{ |
32
|
|
|
$data = $this->getListData($tableName, $recordId, $page, $perPage); |
33
|
|
|
|
34
|
|
|
if ($data['total'] === 0) { |
35
|
|
|
return [ |
36
|
|
|
'success' => false, |
37
|
|
|
'message' => __('record is empty'), |
38
|
|
|
'data' => [] |
39
|
|
|
]; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
return [ |
43
|
|
|
'success' => true, |
44
|
|
|
'message' => '', |
45
|
|
|
'data' => [ |
46
|
|
|
'dataSource' => $data['dataSource'], |
47
|
|
|
'meta' => [ |
48
|
|
|
'total' => $data['total'], |
49
|
|
|
'page' => $data['page'], |
50
|
|
|
] |
51
|
|
|
] |
52
|
|
|
]; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
private function getListData(string $tableName, int $recordId, int $page, int $perPage) |
56
|
|
|
{ |
57
|
|
|
if (empty($tableName) || empty($recordId)) { |
58
|
|
|
throw new \InvalidArgumentException('Table name and record id should not be empty.'); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$list = Db::name('revision') |
62
|
|
|
->where('table_name', $tableName) |
63
|
|
|
->where('original_id', $recordId) |
64
|
|
|
->where('status', 1) |
65
|
|
|
->order('id', 'desc') |
66
|
|
|
->paginate([ |
67
|
|
|
'list_rows' => $perPage, |
68
|
|
|
'page' => $page |
69
|
|
|
])->toArray(); |
70
|
|
|
|
71
|
|
|
return [ |
72
|
|
|
'dataSource' => $list['data'] ?? $list['dataSource'] ?? [], |
73
|
|
|
'total' => $list['total'] ?? $list['pagination']['total'] ?? 0, |
74
|
|
|
'page' => $list['current_page'] ?? $list['pagination']['page'] ?? 1, |
75
|
|
|
]; |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|