1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author Eddy <[email protected]> |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace App\Repository\Admin; |
7
|
|
|
|
8
|
|
|
use App\Model\Admin\Category; |
9
|
|
|
use App\Model\Admin\Comment; |
10
|
|
|
use App\Model\Admin\CommentOperateLog; |
11
|
|
|
use App\Model\Admin\ContentTag; |
12
|
|
|
use App\Model\Admin\Entity; |
13
|
|
|
use App\Model\Admin\EntityField; |
14
|
|
|
use App\Repository\Searchable; |
15
|
|
|
use Illuminate\Database\Schema\Blueprint; |
16
|
|
|
use Illuminate\Support\Facades\DB; |
17
|
|
|
use Illuminate\Support\Facades\Schema; |
18
|
|
|
use App\Exceptions\CreateTableException; |
19
|
|
|
|
20
|
|
|
class EntityRepository |
21
|
|
|
{ |
22
|
|
|
use Searchable; |
23
|
|
|
|
24
|
|
|
public static function list($perPage, $condition = []) |
25
|
|
|
{ |
26
|
|
|
$data = Entity::query() |
27
|
|
|
->where(function ($query) use ($condition) { |
28
|
|
|
Searchable::buildQuery($query, $condition); |
29
|
|
|
}) |
30
|
|
|
->orderBy('id', 'desc') |
31
|
|
|
->paginate($perPage); |
32
|
|
|
$data->transform(function ($item) { |
33
|
|
|
xssFilter($item); |
34
|
|
|
$item->editUrl = route('admin::entity.edit', ['id' => $item->id]); |
35
|
|
|
$item->deleteUrl = route('admin::entity.delete', ['id' => $item->id]); |
36
|
|
|
$item->copyUrl = route('admin::entity.copy', ['id' => $item->id]); |
37
|
|
|
$item->fieldUrl = route('admin::entityField.index') . '?entity_id=' . $item->id; |
38
|
|
|
$item->contentUrl = route('admin::content.index', ['entity' => $item->id]); |
39
|
|
|
$item->commentListUrl = route('admin::comment.index', ['entity_id' => $item->id]); |
40
|
|
|
return $item; |
41
|
|
|
}); |
42
|
|
|
|
43
|
|
|
return [ |
44
|
|
|
'code' => 0, |
45
|
|
|
'msg' => '', |
46
|
|
|
'count' => $data->total(), |
47
|
|
|
'data' => $data->items(), |
48
|
|
|
]; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* 新增模型 |
53
|
|
|
* |
54
|
|
|
* @param array $data |
55
|
|
|
* @param mixed $createDB |
56
|
|
|
* @throws CreateTableException|\Exception |
57
|
|
|
* @return Entity |
58
|
|
|
*/ |
59
|
|
|
public static function add($data, $createDB = true) |
60
|
|
|
{ |
61
|
|
|
$entity = Entity::query()->create($data); |
62
|
|
|
try { |
63
|
|
|
if (!$createDB) { |
64
|
|
|
return $entity; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
if (Schema::hasTable($data['table_name'])) { |
68
|
|
|
throw new \RuntimeException("数据库表已存在"); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
Schema::create($data['table_name'], function (Blueprint $table) { |
72
|
|
|
$table->increments('id'); |
73
|
|
|
$table->timestamps(); |
74
|
|
|
$table->engine = 'InnoDB'; |
75
|
|
|
}); |
76
|
|
|
|
77
|
|
|
return $entity; |
78
|
|
|
} catch (\Exception $e) { |
79
|
|
|
$entity->delete(); |
80
|
|
|
throw new CreateTableException("创建数据库表异常"); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public static function copy($tableName, $id) |
85
|
|
|
{ |
86
|
|
|
$entity = Entity::findOrFail($id); |
87
|
|
|
if (Schema::hasTable($tableName)) { |
88
|
|
|
throw new \RuntimeException("数据库表已存在"); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
try { |
92
|
|
|
// 仅在 Mysql 下测试通过,不支持 Sqlite |
93
|
|
|
DB::statement("CREATE TABLE {$tableName} LIKE {$entity->table_name}"); |
94
|
|
|
|
95
|
|
|
DB::beginTransaction(); |
96
|
|
|
|
97
|
|
|
$newEntity = $entity->replicate(); |
98
|
|
|
$newEntity->table_name = $tableName; |
99
|
|
|
$newEntity->name .= '_copy'; |
100
|
|
|
$newEntity->save(); |
101
|
|
|
|
102
|
|
|
EntityField::where('entity_id', $id)->get()->each(function ($item) use ($newEntity) { |
103
|
|
|
$m = $item->replicate(); |
104
|
|
|
$m->entity_id = $newEntity->id; |
105
|
|
|
$m->save(); |
106
|
|
|
}); |
107
|
|
|
|
108
|
|
|
DB::commit(); |
109
|
|
|
} catch (\Exception $e) { |
110
|
|
|
Schema::dropIfExists($tableName); |
111
|
|
|
throw $e; |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public static function update($id, $data) |
116
|
|
|
{ |
117
|
|
|
return Entity::query()->where('id', $id)->update($data); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public static function find($id) |
121
|
|
|
{ |
122
|
|
|
return Entity::query()->find($id); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public static function external($id) |
126
|
|
|
{ |
127
|
|
|
return Entity::query()->External()->find($id); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
public static function all() |
131
|
|
|
{ |
132
|
|
|
return Entity::query()->get(); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
public static function systemMenu() |
136
|
|
|
{ |
137
|
|
|
$entities = Entity::query()->where('is_show_content_manage', Entity::CONTENT_MANAGE_YES) |
138
|
|
|
->pluck('name', 'id')->all(); |
139
|
|
|
$autoMenu = []; |
140
|
|
|
foreach ($entities as $k => $v) { |
141
|
|
|
$autoMenu[] = [ |
142
|
|
|
'url' => route('admin::content.index', ['entity' => $k]), |
143
|
|
|
'name' => $v, |
144
|
|
|
'id' => $k, |
145
|
|
|
]; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
return $autoMenu; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
public static function delete($id) |
152
|
|
|
{ |
153
|
|
|
$table = Entity::query()->findOrFail($id); |
154
|
|
|
DB::beginTransaction(); |
155
|
|
|
|
156
|
|
|
Schema::dropIfExists($table->table_name); |
157
|
|
|
Entity::destroy($id); |
158
|
|
|
EntityField::query()->where('entity_id', $id)->delete(); |
159
|
|
|
Category::query()->where('model_id', $id)->delete(); |
160
|
|
|
ContentTag::query()->where('entity_id', $id)->delete(); |
161
|
|
|
CommentOperateLog::query()->join('comments', 'comment_operate_logs.comment_id', '=', 'comments.id') |
162
|
|
|
->where('entity_id', $id) |
163
|
|
|
->delete(); |
164
|
|
|
Comment::query()->where('entity_id', $id)->delete(); |
165
|
|
|
|
166
|
|
|
DB::commit(); |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|