MenuRepository::update()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 3
rs 10
1
<?php
2
/**
3
 * Date: 2019/2/25 Time: 16:15
4
 *
5
 * @author  Eddy <[email protected]>
6
 * @version v1.0.0
7
 */
8
9
namespace App\Repository\Admin;
10
11
use App\Model\Admin\Menu;
12
use App\Repository\Searchable;
13
use Illuminate\Support\Facades\Cache;
14
15
class MenuRepository
16
{
17
    use Searchable;
18
19
    public static function list($perPage, $condition = [])
20
    {
21
        $data = Menu::query()
22
            ->where(function ($query) use ($condition) {
23
                Searchable::buildQuery($query, $condition);
24
            })
25
            ->orderBy('status', 'desc')
26
            ->orderBy('id', 'desc')
27
            ->with('parent')
28
            ->paginate($perPage);
29
        $data->transform(function ($item) {
30
            xssFilter($item);
31
            $item->editUrl = route('admin::menu.edit', ['id' => $item->id]);
32
            $item->deleteUrl = route('admin::menu.delete', ['id' => $item->id]);
33
            $item->parentName = $item->pid == 0 ? '顶级菜单' : $item->parent->name;
34
            return $item;
35
        });
36
37
        return [
38
            'code' => 0,
39
            'msg' => '',
40
            'count' => $data->total(),
41
            'data' => $data->items(),
42
        ];
43
    }
44
45
    public static function add($data)
46
    {
47
        return Menu::query()->create($data);
48
    }
49
50
    public static function update($id, $data)
51
    {
52
        return Menu::query()->where('id', $id)->update($data);
53
    }
54
55
    public static function find($id)
56
    {
57
        return Menu::query()->find($id);
58
    }
59
60
    public static function delete($id)
61
    {
62
        // 不能删除非空的父菜单
63
        if (!Menu::query()->where('pid', $id)->get()->isEmpty()) {
64
            throw new \RuntimeException('不能直接删除非空的父菜单,请先删除当前菜单的所有子菜单');
65
        }
66
        return Menu::destroy($id);
67
    }
68
69
    public static function get(array $ids)
70
    {
71
        return Menu::query()->whereIn('id', $ids)->get();
72
    }
73
74
    public static function exist($route)
75
    {
76
        return Menu::query()->where('route', $route)->where('route_params', '')->first();
77
    }
78
79
    public static function tree($pid = 0, $allMenus = null, $level = 0, $path = [])
80
    {
81
        if (is_null($allMenus)) {
82
            $allMenus = Menu::select('id', 'pid', 'name', 'route', 'group', 'status', 'url', 'order')->get();
83
        }
84
        return $allMenus->where('pid', $pid)
85
            ->map(function (Menu $menu) use ($allMenus, $level, $path) {
86
                $data = [
87
                    'id' => $menu->id,
88
                    'name' => $menu->name,
89
                    'level' => $level,
90
                    'pid' => $menu->pid,
91
                    'path' => $path,
92
                    'route' => $menu->route,
93
                    'group' => $menu->group,
94
                    'status' => $menu->status,
95
                    'url' => $menu->url,
96
                    'order' => $menu->order,
97
                ];
98
99
                $child = $allMenus->where('pid', $menu->id);
100
                if ($child->isEmpty()) {
101
                    return $data;
102
                }
103
104
                array_push($path, $menu->id);
105
                $data['children'] = self::tree($menu->id, $allMenus, $level + 1, $path);
106
                return $data;
107
            })->sortBy('order');
108
    }
109
110
    public static function allRoot()
111
    {
112
        return Menu::query()->where('pid', 0)->where('status', 1)->orderBy('order')->get();
113
    }
114
115
    /**
116
     * 根据分组名称进行数据分组
117
     *
118
     * @return \Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection
119
     */
120
    public static function group()
121
    {
122
        return Menu::all()->groupBy('group');
123
    }
124
125
    /**
126
     * 根据指定路由名获取根菜单
127
     *
128
     * @param string $route
129
     * @param null|array $tree
130
     * @throws \RuntimeException
131
     * @return array|null
132
     */
133
    public static function root($route, $tree = null)
134
    {
135
        if (is_null($tree)) {
136
            $tree = self::getTree();
137
        }
138
139
        foreach ($tree as $menu) {
140
            if ($menu['route'] === $route) {
141
                if (empty($menu['path'])) {
142
                    return $menu;
143
                }
144
145
                $rootId = current($menu['path']);
146
                foreach (self::getTree() as $v) {
147
                    if ($v['id'] == $rootId) {
148
                        return $v;
149
                    }
150
                }
151
            }
152
153
            if (isset($menu['children'])) {
154
                $m = self::root($route, $menu['children']);
155
                if (!is_null($m)) {
156
                    return $m;
157
                }
158
            }
159
        }
160
161
        return null;
162
    }
163
164
    public static function getTree()
165
    {
166
        static $tree = null;
167
168
        if (!is_null($tree)) {
169
            return $tree;
170
        }
171
172
        $tree = Cache::remember('menu:tree', 60 * 60 * 24, function () {
173
            return self::tree();
174
        });
175
        return $tree;
176
    }
177
}
178