| | |
| | | |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| | | import com.google.common.collect.Lists; |
| | | 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.domain.BaseParam; |
| | | import com.vincent.rsf.server.common.domain.KeyValVo; |
| | | import com.vincent.rsf.server.common.domain.PageParam; |
| | | import com.vincent.rsf.server.common.service.RedisService; |
| | | 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 org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.security.access.prepost.PreAuthorize; |
| | | import org.springframework.web.bind.annotation.*; |
| | | import com.vincent.rsf.server.manager.utils.buildPageRowsUtils; |
| | | |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import jakarta.servlet.http.HttpServletResponse; |
| | | import java.util.*; |
| | | |
| | | @RestController |
| | | @RequestMapping("") |
| | | public class MenuController extends BaseController { |
| | | |
| | | private static final String MENU_TREE_CACHE_FLAG = "MENU_TREE"; |
| | | private static final String MENU_TREE_CACHE_KEY = "FULL_TREE"; |
| | | private static final int MENU_TREE_CACHE_TTL_SECONDS = 300; |
| | | |
| | | @Autowired |
| | | private MenuService menuService; |
| | | @Autowired |
| | | private RedisService redisService; |
| | | |
| | | @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))); |
| | | return R.ok().add(buildPageRowsUtils.rowsMap(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()); |
| | | return R.ok().add(buildPageRowsUtils.rowsMap(menuService.list())); |
| | | } |
| | | |
| | | @PreAuthorize("hasAuthority('system:menu:list')") |
| | |
| | | // 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); |
| | | // return R.ok().add(buildPageRowsUtils.rowsMap(Utils.toTreeData(menus, 0L, Menu::getParentId, Menu::getId, Menu::setChildren))); |
| | | List<Menu> treeData = getMenuTreeData(); |
| | | 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); |
| | | return R.ok().add(buildPageRowsUtils.rowsMap(treeData)); |
| | | } |
| | | |
| | | @PreAuthorize("hasAuthority('system:menu:list')") |
| | | @GetMapping("/menu/{id}") |
| | | public R get(@PathVariable("id") Long id) { |
| | | return R.ok().add(menuService.getById(id)); |
| | | return R.ok().add(buildPageRowsUtils.rowsMap(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))); |
| | | return R.ok().add(buildPageRowsUtils.rowsMap(menuService.listByIds(Arrays.asList(ids)))); |
| | | } |
| | | |
| | | @PreAuthorize("hasAuthority('system:menu:save')") |
| | |
| | | if (!menuService.save(menu)) { |
| | | return R.error("Save Fail"); |
| | | } |
| | | return R.ok("Save Success").add(menu); |
| | | evictMenuTreeCache(); |
| | | return R.ok("Save Success").add(buildPageRowsUtils.rowsMap(menu)); |
| | | } |
| | | |
| | | @PreAuthorize("hasAuthority('system:menu:update')") |
| | |
| | | if (!menuService.updateById(menu)) { |
| | | return R.error("Update Fail"); |
| | | } |
| | | return R.ok("Update Success").add(menu); |
| | | evictMenuTreeCache(); |
| | | return R.ok("Update Success").add(buildPageRowsUtils.rowsMap(menu)); |
| | | } |
| | | |
| | | @PreAuthorize("hasAuthority('system:menu:remove')") |
| | |
| | | if (!menuService.removeByIds(Arrays.asList(ids))) { |
| | | return R.error("Delete Fail"); |
| | | } |
| | | return R.ok("Delete Success").add(ids); |
| | | evictMenuTreeCache(); |
| | | return R.ok("Delete Success").add(buildPageRowsUtils.rowsMap(ids)); |
| | | } |
| | | |
| | | @PreAuthorize("hasAuthority('system:menu:list')") |
| | |
| | | menuService.page(new Page<>(1, 30), wrapper).getRecords().forEach( |
| | | item -> vos.add(new KeyValVo(item.getId(), item.getName())) |
| | | ); |
| | | return R.ok().add(vos); |
| | | return R.ok().add(buildPageRowsUtils.rowsMap(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); |
| | | ExcelUtil.build(ExcelUtil.create(buildPageRowsUtils.rowsMap(menuService.list()), Menu.class), response); |
| | | } |
| | | |
| | | private List<Menu> getMenuTreeData() { |
| | | List<Menu> treeData = redisService.get(MENU_TREE_CACHE_FLAG, MENU_TREE_CACHE_KEY); |
| | | if (treeData != null) { |
| | | return treeData; |
| | | } |
| | | List<Menu> menuList = menuService.list(new LambdaQueryWrapper<Menu>().orderByAsc(Menu::getSort)); |
| | | treeData = Utils.toTreeData(menuList, 0L, Menu::getParentId, Menu::getId, Menu::setChildren); |
| | | redisService.set(MENU_TREE_CACHE_FLAG, MENU_TREE_CACHE_KEY, treeData, MENU_TREE_CACHE_TTL_SECONDS); |
| | | return treeData; |
| | | } |
| | | |
| | | private void evictMenuTreeCache() { |
| | | redisService.delete(MENU_TREE_CACHE_FLAG, MENU_TREE_CACHE_KEY); |
| | | } |
| | | |
| | | } |
| | | |