-
Notifications
You must be signed in to change notification settings - Fork 84
Expand file tree
/
Copy pathCategoryRepository.php
More file actions
121 lines (106 loc) · 3.8 KB
/
CategoryRepository.php
File metadata and controls
121 lines (106 loc) · 3.8 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
<?php
/**
* @author Eddy <[email protected]>
*/
namespace App\Repository\Admin;
use App\Model\Admin\Category;
use App\Repository\Searchable;
class CategoryRepository
{
use Searchable;
public static function listSingle($perPage, $condition = [])
{
$data = Category::query()
->where(function ($query) use ($condition) {
Searchable::buildQuery($query, $condition);
})
->orderBy('id', 'desc')
->paginate($perPage);
$data->transform(function ($item) {
xssFilter($item);
$item->editUrl = route('admin::category.edit', ['id' => $item->id]);
$item->parentName = $item->pid == 0 ? '顶级菜单' : $item->parent->name;
$item->entityName = $item->entity ? $item->entity->name : '';
unset($item->entity);
return $item;
});
return [
'code' => 0,
'msg' => '',
'count' => $data->total(),
'data' => $data->items(),
];
}
public static function list($perPage, $condition = [])
{
$list = [];
$data = Category::query()
->where(function ($query) use ($condition) {
Searchable::buildQuery($query, $condition);
})
->orderBy('id', 'desc')
->paginate($perPage);
$data->each(function ($item, $key) use (&$list) {
xssFilter($item);
$item->editUrl = route('admin::category.edit', ['id' => $item->id]);
$item->parentName = $item->pid == 0 ? '顶级菜单' : $item->parent->name;
$item->entityName = $item->entity ? $item->entity->name : '';
unset($item->entity);
array_push($list, $item);
$item->children->each(function ($v, $k) use (&$list) {
$v->editUrl = route('admin::category.edit', ['id' => $v->id]);
//$v->parentName = $v->pid == 0 ? '顶级菜单' : $v->parent->name;
//$v->entityName = $v->entity ? $v->entity->name : '';
$v->name = '|--------' . $v->name;
array_push($list, $v);
});
unset($item->children);
});
return [
'code' => 0,
'msg' => '',
'count' => count($list),
'data' => $list,
];
}
public static function add($data)
{
return Category::query()->create($data);
}
public static function update($id, $data)
{
return Category::query()->where('id', $id)->update($data);
}
public static function find($id)
{
return Category::query()->find($id);
}
public static function tree($entity_id = null, $pid = 0, $all = null, $level = 0, $path = [])
{
if (is_null($all)) {
if (is_null($entity_id)) {
$all = Category::select('id', 'pid', 'name', 'order')->get();
} else {
$all = Category::select('id', 'pid', 'name', 'order')->where('model_id', $entity_id)->get();
}
}
return $all->where('pid', $pid)
->map(function (Category $model) use ($all, $level, $path, $entity_id) {
$data = [
'id' => $model->id,
'name' => $model->name,
'level' => $level,
'pid' => $model->pid,
'path' => $path,
'order' => $model->order,
];
$child = $all->where('pid', $model->id);
if ($child->isEmpty()) {
return $data;
}
array_push($path, $model->id);
$data['children'] = self::tree($entity_id, $model->id, $all, $level + 1, $path);
return $data;
})->sortBy('order');
}
}