#
vincentlu
2025-02-11 2d1b39fae6abed7cda7bd5722fcea23fefdb6e12
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
package com.vincent.rsf.server.system.controller;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.vincent.rsf.common.utils.Utils;
import com.vincent.rsf.framework.common.Cools;
import com.vincent.rsf.framework.common.R;
import com.vincent.rsf.server.common.annotation.OperationLog;
import com.vincent.rsf.server.common.domain.BaseParam;
import com.vincent.rsf.server.common.domain.KeyValVo;
import com.vincent.rsf.server.common.domain.PageParam;
import com.vincent.rsf.server.common.utils.ExcelUtil;
import com.vincent.rsf.server.common.utils.NodeUtils;
import com.vincent.rsf.server.system.entity.Menu;
import com.vincent.rsf.server.system.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
 
import javax.servlet.http.HttpServletResponse;
import java.util.*;
 
@RestController
public class MenuController extends BaseController {
 
    @Autowired
    private MenuService menuService;
 
    @PreAuthorize("hasAuthority('system:menu:list')")
    @PostMapping("/menu/page")
    public R page(@RequestBody Map<String, Object> map) {
        BaseParam baseParam = buildParam(map, BaseParam.class);
        PageParam<Menu, BaseParam> pageParam = new PageParam<>(baseParam, Menu.class);
        return R.ok().add(menuService.page(pageParam, pageParam.buildWrapper(true)));
    }
 
    @PreAuthorize("hasAuthority('system:menu:list')")
    @PostMapping("/menu/list")
    public R list(@RequestBody Map<String, Object> map) {
        return R.ok().add(menuService.list());
    }
 
    @PreAuthorize("hasAuthority('system:menu:list')")
    @PostMapping("/menu/tree")
    public R tree(@RequestBody Map<String, Object> map) {
//        PageParam<Menu, BaseParam> param = new PageParam<>(buildParam(map, BaseParam.class), Menu.class);
//        QueryWrapper<Menu> wrapper = param.buildWrapper(true, queryWrapper -> queryWrapper.orderByAsc("sort"));
//        List<Menu> menus = menuService.list(wrapper);
//        return R.ok().add(Utils.toTreeData(menus, 0L, Menu::getParentId, Menu::getId, Menu::setChildren));
        List<Menu> menuList = menuService.list(new LambdaQueryWrapper<Menu>().orderByAsc(Menu::getSort));
        List<Menu> treeData = Utils.toTreeData(menuList, 0L, Menu::getParentId, Menu::getId, Menu::setChildren);
        if (!Cools.isEmpty(map.get("condition"))) {
            Utils.treeRemove(treeData, String.valueOf(map.get("condition")), Menu::getName, Menu::getChildren);
            Utils.treeRemove(treeData, String.valueOf(map.get("condition")), Menu::getName, Menu::getChildren);
        }
        return R.ok().add(treeData);
    }
 
    @PreAuthorize("hasAuthority('system:menu:list')")
    @GetMapping("/menu/{id}")
    public R get(@PathVariable("id") Long id) {
        return R.ok().add(menuService.getById(id));
    }
 
    @PreAuthorize("hasAuthority('system:menu:list')")
    @PostMapping({"/menu/many/{ids}", "/menus/many/{ids}"})
    public R many(@PathVariable Long[] ids) {
        return R.ok().add(menuService.listByIds(Arrays.asList(ids)));
    }
 
    @PreAuthorize("hasAuthority('system:menu:save')")
    @OperationLog("Save Menu")
    @PostMapping("/menu/save")
    public R save(@RequestBody Menu menu) {
        if (menu.getParentId() != null && menu.getParentId() > 0) {
            Menu parent = menuService.getById(menu.getParentId());
            if (parent != null) {
                menu.setParentName(parent.getName());
            }
        } else {
            menu.setParentId(0L);
        }
 
        NodeUtils nodeUtils = new NodeUtils();
        nodeUtils.generatePath0(item -> menuService.getById(item.getParentId()), menu, Menu::getId, Menu::getName, Menu::getParentId);
        menu.setPath(nodeUtils.path.toString());
        menu.setPathName(nodeUtils.pathName.toString());
 
        menu.setCreateBy(getLoginUserId());
        menu.setCreateTime(new Date());
        menu.setUpdateBy(getLoginUserId());
        menu.setUpdateTime(new Date());
        if (!menuService.save(menu)) {
            return R.error("Save Fail");
        }
        return R.ok("Save Success").add(menu);
    }
 
    @PreAuthorize("hasAuthority('system:menu:update')")
    @OperationLog("Update Menu")
    @PostMapping("/menu/update")
    public R update(@RequestBody Menu menu) {
        if (menu.getParentId() != null && menu.getParentId() > 0) {
            Menu parent = menuService.getById(menu.getParentId());
            if (parent != null) {
                menu.setParentName(parent.getName());
            }
        } else {
            menu.setParentId(0L);
        }
 
        NodeUtils nodeUtils = new NodeUtils();
        nodeUtils.generatePath0(item -> menuService.getById(item.getParentId()), menu, Menu::getId, Menu::getName, Menu::getParentId);
        menu.setPath(nodeUtils.path.toString());
        menu.setPathName(nodeUtils.pathName.toString());
 
        menu.setUpdateBy(getLoginUserId());
        menu.setUpdateTime(new Date());
        if (!menuService.updateById(menu)) {
            return R.error("Update Fail");
        }
        return R.ok("Update Success").add(menu);
    }
 
    @PreAuthorize("hasAuthority('system:menu:remove')")
    @OperationLog("Delete Menu")
    @PostMapping("/menu/remove/{ids}")
    public R remove(@PathVariable Long[] ids) {
        if (!menuService.removeByIds(Arrays.asList(ids))) {
            return R.error("Delete Fail");
        }
        return R.ok("Delete Success").add(ids);
    }
 
    @PreAuthorize("hasAuthority('system:menu:list')")
    @PostMapping("/menu/query")
    public R query(@RequestParam(required = false) String condition) {
        List<KeyValVo> vos = new ArrayList<>();
        LambdaQueryWrapper<Menu> wrapper = new LambdaQueryWrapper<>();
        if (!Cools.isEmpty(condition)) {
            wrapper.like(Menu::getName, condition);
        }
        menuService.page(new Page<>(1, 30), wrapper).getRecords().forEach(
                item -> vos.add(new KeyValVo(item.getId(), item.getName()))
        );
        return R.ok().add(vos);
    }
 
    @PreAuthorize("hasAuthority('system:menu:list')")
    @PostMapping("/menu/export")
    public void export(@RequestBody Map<String, Object> map, HttpServletResponse response) throws Exception {
        ExcelUtil.build(ExcelUtil.create(menuService.list(), Menu.class), response);
    }
 
}