86183
2022-09-09 0d999e33085c0a25c5525242748f6aa62a401159
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
package com.dsh.upms.controller;
 
 
import cn.mb.cloud.common.api.dto.MenuRouterTree;
import cn.mb.cloud.common.api.vo.MenuVO;
import cn.mb.cloud.common.api.vo.TreeUtil;
import cn.mb.cloud.common.core.constant.CommonConstants;
import cn.mb.cloud.common.core.constant.enums.ErrorCodeConstants;
import cn.mb.cloud.common.core.exception.BusinessException;
import cn.mb.cloud.common.core.util.ResponseData;
import cn.mb.cloud.common.log.annotation.SysLog;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dsh.upms.entity.SysMenu;
import com.dsh.upms.entity.SysRoleMenu;
import com.dsh.upms.model.vo.LoginUserVo;
import com.dsh.upms.service.ISysMenuService;
import com.dsh.utils.login.LoginHelper;
import com.dsh.utils.login.Tree;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
 
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
 
/**
 * <p>
 * 权限菜单 前端控制器
 * </p>
 *
 * @author null123
 * @since 2021-04-29
 */
@Api("权限菜单")
@RestController
@RequestMapping("/sysMenu")
public class SysMenuController {
 
 
    @Autowired
    private ISysMenuService sysMenuService;
    @Autowired
    private ISysDoctorMenuService sysDoctorMenuService;
    @Autowired
    private IDoctorService doctorService;
 
    /**
     * 分页查询
     *
     * @param page    分页对象
     * @param sysMenu 菜单权限表
     * @return
     */
    @GetMapping("/page")
    @ApiOperation(value = "分页查询", tags = {"权限菜单"}, notes = "", response = SysMenu.class)
    @ApiImplicitParams({
            @ApiImplicitParam(value = "每页条数", name = "size", required = true, dataType = "Integer"),
            @ApiImplicitParam(value = "当前页数", name = "current", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "Authorization", value = "Bearer +token", required = true, dataType = "String", paramType = "header", defaultValue = "Bearer eyJhbGciOiJIUzUxMiJ9.....")
    })
    public ResponseData getSysMenuPage(Page page, SysMenu sysMenu) {
        Page pages = sysMenuService.page(page, Wrappers.query(sysMenu));
        return ResponseData.success(pages);
    }
 
 
    /**
     * 通过id查询菜单权限
     *
     * @param id id
     * @return SysMenu
     */
    @GetMapping("/getById")
    @ApiOperation(value = "通过id查询菜单权限", tags = {"权限菜单"}, notes = "", response = SysMenu.class)
    @ApiImplicitParams({
            @ApiImplicitParam(value = "菜单ID", name = "id", required = true, dataType = "Long"),
            @ApiImplicitParam(name = "Authorization", value = "Bearer +token", required = true, dataType = "String", paramType = "header", defaultValue = "Bearer eyJhbGciOiJIUzUxMiJ9.....")
    })
    public ResponseData getById(Long id) {
        SysMenu sysMenu = sysMenuService.getById(id);
        return ResponseData.success(sysMenu);
    }
 
    /**
     * 新增菜单权限表
     *
     * @param sysMenu 菜单权限表
     * @return SysMenu
     */
    @SysLog("新增菜单权限表")
    @PostMapping("/create")
    public ResponseData save(@RequestBody SysMenu sysMenu) {
        boolean result = sysMenuService.save(sysMenu);
        return ResponseData.success(result);
    }
 
    /**
     * 修改菜单权限表
     *
     * @param sysMenu 菜单权限表
     * @return R
     */
    @SysLog("修改菜单权限表")
    @PostMapping("updateById")
    @ApiOperation(value = "修改菜单权限表", tags = {"权限菜单"}, notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "Bearer +token", required = true, dataType = "String", paramType = "header", defaultValue = "Bearer eyJhbGciOiJIUzUxMiJ9.....")
    })
    public ResponseData updateById(@RequestBody SysMenu sysMenu) {
        boolean result = sysMenuService.updateById(sysMenu);
        return ResponseData.success(result);
    }
 
    /**
     * 通过id删除菜单权限表
     *
     * @param id id
     * @return R
     */
    @SysLog("删除菜单权限表")
    @DeleteMapping("/removeById")
    public ResponseData removeById(@PathVariable Long id) {
        boolean result = sysMenuService.removeById(id);
        return ResponseData.success(result);
    }
 
    /**
     * 启用禁用
     *
     * @param id
     * @return
     */
    @GetMapping("enable")
    @ApiOperation(value = "启用禁用", tags = {"权限菜单"}, notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "菜单ID", name = "id", required = true, dataType = "Long"),
            @ApiImplicitParam(name = "Authorization", value = "Bearer +token", required = true, dataType = "String", paramType = "header", defaultValue = "Bearer eyJhbGciOiJIUzUxMiJ9.....")
    })
    public ResponseData<Boolean> enable(@RequestParam("id") Long id) {
        SysMenu menu = sysMenuService.getById(id);
        if (null != menu) {
            menu.setEnable(!menu.getEnable());
        } else {
            return ResponseData.fail("权限不存在");
        }
        return ResponseData.success(sysMenuService.updateById(menu));
    }
 
    /**
     * 返回所有树形菜单集合
     *
     * @return 树形菜单
     */
    @GetMapping("/tree")
    @ApiOperation(value = "返回所有树形菜单集合", tags = {"权限菜单"}, notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "菜单名称", name = "title", required = false, dataType = "String"),
            @ApiImplicitParam(name = "Authorization", value = "Bearer +token", required = true, dataType = "String", paramType = "header", defaultValue = "Bearer eyJhbGciOiJIUzUxMiJ9.....")
    })
    public ResponseData getTree(@RequestParam(value = "name", required = false) String title) {
        try {
            List<SysMenu> menus = sysMenuService.getBaseMapper()
                    .selectList(Wrappers.<SysMenu>query().lambda()
                            .eq(SysMenu::getDelFlag, 0).like(StringUtils.isNotBlank(title), SysMenu::getName, title)
                            .orderByAsc(SysMenu::getSort));
            List<Tree> treeList = new ArrayList<Tree>();
            Tree rootTree = new Tree("系统菜单");
            for (SysMenu menu : menus) {
                Long parent = menu.getParentId();
                if (null == parent) {
                    Tree tree = new Tree(menu.getId().toString(), menu.getName(), Tree.TREE_NODE_STATE_DEFAULT);
                    tree.setPermission(menu.getPermission());
                    tree.setType(menu.getType());
                    tree.setName(menu.getName());
                    tree.setLable(menu.getName());
                    tree.setPath(menu.getPath());
                    tree.setIcon(menu.getIcon());
                    tree.setEnable(menu.getEnable());
                    tree.setSort(menu.getSort());
                    SysMenu.initTree(menus, tree);
                    treeList.add(tree);
                }
            }
            rootTree.setChildren(treeList);
            return new ResponseData<>(treeList);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseData.fail("操作失败");
        }
    }
 
    /**
     * 返回角色的菜单集合
     *
     * @param sysRoleMenu
     * @return
     */
    @PostMapping("/role")
    @ApiOperation(value = "返回角色的菜单集合", tags = {"权限菜单"}, notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "Bearer +token", required = true, dataType = "String", paramType = "header", defaultValue = "Bearer eyJhbGciOiJIUzUxMiJ9.....")
    })
    public ResponseData getRoleResource(@RequestBody SysRoleMenu sysRoleMenu) throws BusinessException {
        List<MenuVO> list = sysMenuService.findMenuByRoleId(doctorService.updateRole(sysRoleMenu.getRoleId().intValue()));
        return ResponseData.success(list.stream().filter(item -> list.stream().noneMatch(vo -> Objects.nonNull(vo) && Objects.nonNull(vo.getParentId())
                && vo.getParentId().equals(item.getId()))).map(item -> item.getId().toString()).collect(Collectors.toList()));
    }
 
    /**
     * 返回当前登录用户的树形菜单集合
     *
     * @return 当前用户的树形菜单
     */
    @GetMapping("/user/tree")
    @ApiOperation(value = "返回当前登录用户的树形菜单集合", tags = {"权限菜单"}, notes = "", response = MenuRouterTree.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "Bearer +token", required = true, dataType = "String", paramType = "header", defaultValue = "Bearer eyJhbGciOiJIUzUxMiJ9.....")
    })
    public ResponseData getUserMenu() throws BusinessException {
        // 获取当前登录用户信息
        LoginUserVo loginUserVo = LoginHelper.getUser();
        if (Objects.isNull(loginUserVo)) {
            throw new BusinessException("请登录", ErrorCodeConstants.FAIL.getValue());
        }
        // 获取符合条件的菜单
        Set<MenuVO> all = sysMenuService.findMenuByUserId(loginUserVo.getId());
        List<MenuRouterTree> menuTreeList = all.stream()
                .filter(menuVo -> CommonConstants.MENU.equals(menuVo.getType()))
                .map(MenuRouterTree::new)
                .sorted(Comparator.comparingInt(MenuRouterTree::getSort))
                .collect(Collectors.toList());
        return ResponseData.success(TreeUtil.bulidRouter(menuTreeList));
    }
 
    /**
     * 返回当前登录用户的权限码
     *
     * @return
     * @throws BusinessException
     */
    @GetMapping("/user/permission")
    @ApiOperation(value = "返回当前登录用户的权限码", tags = {"权限菜单"}, notes = "", response = String.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "Bearer +token", required = true, dataType = "String", paramType = "header", defaultValue = "Bearer eyJhbGciOiJIUzUxMiJ9.....")
    })
    public ResponseData getPermission() throws BusinessException {
        // 获取当前登录用户信息
        LoginUserVo loginUserVo = LoginHelper.getUser();
        if (Objects.isNull(loginUserVo)) {
            throw new BusinessException("请登录", ErrorCodeConstants.FAIL.getValue());
        }
        return ResponseData.success(sysDoctorMenuService.getPermission(loginUserVo.getId()));
    }
}