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 saveAPI(string $title, string $tableName, int $originalId, array $extra = []) |
13
|
|
|
{ |
14
|
|
|
return (new Revision($tableName, (int)$originalId, $extra))->save($title); |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
public function readAPI(int $id) |
18
|
|
|
{ |
19
|
|
|
$data = Db::table('revision')->where('id', (int)$id)->find(); |
20
|
|
|
if ($data) { |
21
|
|
|
return [ |
22
|
|
|
'success' => true, |
23
|
|
|
'message' => '', |
24
|
|
|
'data' => [ |
25
|
|
|
'dataSource' => $data |
26
|
|
|
] |
27
|
|
|
]; |
28
|
|
|
} |
29
|
|
|
return [ |
30
|
|
|
'success' => false, |
31
|
|
|
'message' => __('get revision data failed'), |
32
|
|
|
'data' => [] |
33
|
|
|
]; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function restoreAPI(string $tableName, int $originalId, int $revisionId, array $extra = []) |
37
|
|
|
{ |
38
|
|
|
try { |
39
|
|
|
(new Revision($tableName, $originalId, $extra))->save('restore (autosave)'); |
40
|
|
|
(new Revision($tableName, $originalId, $extra))->restore($revisionId); |
41
|
|
|
return [ |
42
|
|
|
'success' => true, |
43
|
|
|
'message' => __('revision restore successfully'), |
44
|
|
|
'data' => [] |
45
|
|
|
]; |
46
|
|
|
} catch (Exception $e) { |
47
|
|
|
return [ |
48
|
|
|
'success' => false, |
49
|
|
|
'message' => $e->getMessage(), |
50
|
|
|
'data' => [] |
51
|
|
|
]; |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function listAPI(string $tableName, int $recordId, int $page = 1, int $perPage = 5) |
56
|
|
|
{ |
57
|
|
|
$data = $this->getListData($tableName, $recordId, $page, $perPage); |
58
|
|
|
|
59
|
|
|
if ($data['total'] === 0) { |
60
|
|
|
return [ |
61
|
|
|
'success' => false, |
62
|
|
|
'message' => __('record is empty'), |
63
|
|
|
'data' => [] |
64
|
|
|
]; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return [ |
68
|
|
|
'success' => true, |
69
|
|
|
'message' => '', |
70
|
|
|
'data' => [ |
71
|
|
|
'dataSource' => $data['dataSource'], |
72
|
|
|
'meta' => [ |
73
|
|
|
'total' => $data['total'], |
74
|
|
|
'page' => $data['page'], |
75
|
|
|
] |
76
|
|
|
] |
77
|
|
|
]; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
private function getListData(string $tableName, int $recordId, int $page, int $perPage) |
81
|
|
|
{ |
82
|
|
|
if (empty($tableName) || empty($recordId)) { |
83
|
|
|
throw new \InvalidArgumentException('Table name and record id should not be empty.'); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$list = Db::name('revision') |
87
|
|
|
->where('table_name', $tableName) |
88
|
|
|
->where('original_id', $recordId) |
89
|
|
|
->where('status', 1) |
90
|
|
|
->order('id', 'desc') |
91
|
|
|
->paginate([ |
92
|
|
|
'list_rows' => $perPage, |
93
|
|
|
'page' => $page |
94
|
|
|
])->toArray(); |
95
|
|
|
|
96
|
|
|
return [ |
97
|
|
|
'dataSource' => $list['data'] ?? $list['dataSource'] ?? [], |
98
|
|
|
'total' => $list['total'] ?? $list['pagination']['total'] ?? 0, |
99
|
|
|
'page' => $list['current_page'] ?? $list['pagination']['page'] ?? 1, |
100
|
|
|
]; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|