菜单功能修改

This commit is contained in:
ludj
2022-11-30 11:08:08 +08:00
parent 4fe3925f8c
commit 35c296996f
7 changed files with 169 additions and 103 deletions

View File

@@ -45,8 +45,8 @@ import java.util.*;
import java.util.stream.Collectors;
/**
* @author Zheng Jie
* @date 2018-12-03
* @author ludj
* @date 2022-12-30
*/
@RestController
@@ -56,8 +56,6 @@ import java.util.stream.Collectors;
public class MenuController {
private final MenuService menuService;
private final MenuMapper menuMapper;
private static final String ENTITY_NAME = "menu";
@GetMapping(value = "/build")
@ApiOperation("根据用户获取菜单")
@@ -95,12 +93,12 @@ public class MenuController {
@GetMapping(value = "/child")
@SaCheckPermission(value = {"menu:list", "roles:list"}, mode = SaMode.AND)
public ResponseEntity<Object> child(@RequestParam Long id) {
/* Set<Menu> menuSet = new HashSet<>();
Set<MenuDto> menuSet = new HashSet<>();
List<MenuDto> menuList = menuService.getMenus(id);
menuSet.add(menuService.findOne(id));
menuSet = menuService.getChildMenus(menuMapper.toEntity(menuList), menuSet);
Set<Long> ids = menuSet.stream().map(Menu::getId).collect(Collectors.toSet());*/
return new ResponseEntity<>(id, HttpStatus.OK);
menuSet.add(menuService.findById(id));
menuSet = menuService.getChildMenus(menuList, menuSet);
Set<Long> ids = menuSet.stream().map(MenuDto::getMenu_id).collect(Collectors.toSet());
return new ResponseEntity<>(ids, HttpStatus.OK);
}
@GetMapping
@@ -150,10 +148,10 @@ public class MenuController {
@DeleteMapping
@SaCheckPermission("menu:del")
public ResponseEntity<Object> delete(@RequestBody Set<Long> ids) {
Set<Menu> menuSet = new HashSet<>();
Set<MenuDto> menuSet = new HashSet<>();
for (Long id : ids) {
/* List<MenuDto> menuList = menuService.getMenus(id);*/
menuSet.add(menuService.findOne(id));
menuSet.add(menuService.findById(id));
/*menuSet = menuService.getChildMenus(menuMapper.toEntity(menuList), menuSet);*/
}
menuService.delete(menuSet);

View File

@@ -58,6 +58,13 @@ public interface MenuService {
*/
JSONArray getMenusByRole(String role_id, String system_type, String category);
/**
* 菜单json对象转实体对象
* @param menuJson
*/
MenuDto menuJsonToMenuDto(JSONObject menuJson);
/**
* 根据ID查询
*
@@ -87,7 +94,7 @@ public interface MenuService {
* @param menuSet /
* @return /
*/
Set<Menu> getChildMenus(List<Menu> menuList, Set<Menu> menuSet);
Set<MenuDto> getChildMenus(List<MenuDto> menuList, Set<MenuDto> menuSet);
/**
* 构建菜单树
@@ -105,20 +112,13 @@ public interface MenuService {
*/
Object buildMenus(List<MenuDto> menuDtos);
/**
* 根据ID查询
*
* @param id /
* @return /
*/
Menu findOne(Long id);
/**
* 删除
*
* @param menuSet /
*/
void delete(Set<Menu> menuSet);
void delete(Set<MenuDto> menuSet);
/**
* 懒加载菜单数据
@@ -126,7 +126,7 @@ public interface MenuService {
* @param pid /
* @return /
*/
JSONArray getMenus(Long pid);
List<MenuDto> getMenus(Long pid);
/**
* 根据ID获取同级与上级数据
@@ -135,7 +135,7 @@ public interface MenuService {
* @param objects /
* @return /
*/
List<MenuDto> getSuperior(MenuDto menuDto, List<Menu> objects);
List<MenuDto> getSuperior(MenuDto menuDto, List<MenuDto> objects);
/**
* 根据当前用户获取菜单

View File

@@ -15,6 +15,7 @@
*/
package org.nl.modules.system.service.dto;
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;
import org.nl.modules.common.base.BaseDTO;
@@ -31,7 +32,7 @@ import java.util.Objects;
@Setter
public class MenuDto extends BaseDTO implements Serializable {
private Long id;
private Long menu_id;
private List<MenuDto> children;
@@ -41,7 +42,7 @@ public class MenuDto extends BaseDTO implements Serializable {
private String title;
private Integer menuSort;
private Integer menu_sort;
private String path;
@@ -49,25 +50,29 @@ public class MenuDto extends BaseDTO implements Serializable {
private Long pid;
private Integer subCount;
private Integer sub_count;
private Boolean iFrame;
private Boolean i_frame;
private Boolean cache;
private Boolean hidden;
private String componentName;
private String component_name;
private String icon;
private boolean hasChildren;
private boolean leaf;
public Boolean getHasChildren() {
return subCount > 0;
return sub_count > 0;
}
public Boolean getLeaf() {
return subCount <= 0;
return sub_count <= 0;
}
public String getLabel() {
@@ -83,11 +88,11 @@ public class MenuDto extends BaseDTO implements Serializable {
return false;
}
MenuDto menuDto = (MenuDto) o;
return Objects.equals(id, menuDto.id);
return Objects.equals(menu_id, menuDto.menu_id);
}
@Override
public int hashCode() {
return Objects.hash(id);
return Objects.hash(menu_id);
}
}

View File

@@ -21,35 +21,25 @@ import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.RequiredArgsConstructor;
import org.nl.modules.common.exception.BadRequestException;
import org.nl.modules.common.exception.EntityExistException;
import org.nl.modules.common.utils.*;
import org.nl.modules.system.domain.Menu;
import org.nl.modules.system.domain.Role;
import org.nl.modules.system.domain.User;
import org.nl.modules.system.domain.vo.MenuMetaVo;
import org.nl.modules.system.domain.vo.MenuVo;
import org.nl.modules.system.repository.MenuRepository;
import org.nl.modules.system.repository.UserRepository;
import org.nl.modules.system.service.MenuService;
import org.nl.modules.system.service.RoleService;
import org.nl.modules.system.service.dto.MenuDto;
import org.nl.modules.system.service.dto.MenuQueryCriteria;
import org.nl.modules.system.service.dto.RoleSmallDto;
import org.nl.modules.system.service.mapstruct.MenuMapper;
import org.nl.modules.wql.WQL;
import org.nl.modules.wql.core.bean.ResultBean;
import org.nl.modules.wql.core.bean.WQLObject;
import org.nl.modules.wql.util.WqlUtil;
import org.nl.wms.util.IdUtil;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.lang.reflect.Field;
import java.util.*;
import java.util.stream.Collectors;
@@ -61,16 +51,35 @@ import java.util.stream.Collectors;
@CacheConfig(cacheNames = "menu")
public class MenuServiceImpl implements MenuService {
private final MenuRepository menuRepository;
private final UserRepository userRepository;
private final MenuMapper menuMapper;
private final RoleService roleService;
private final RedisUtils redisUtils;
@Override
public JSONObject queryAll(JSONObject param, Pageable page) throws Exception {
ResultBean rb = WQLObject.getWQLObject("sys_menu").pagequery(WqlUtil.getHttpContext(page), "pid is null", "");
return rb.pageResult();
JSONObject json = WQL.getWO("QSYS_MENU01").addParam("flag", "0").addParam("pid", param.getString("pid")).pageQuery(WqlUtil.getHttpContext(page), "");
JSONArray content = json.getJSONArray("content");
JSONArray res = new JSONArray();
for (int i = 0; i < content.size(); i++) {
JSONObject obj = content.getJSONObject(i);
obj.put("menu_id", obj.getLong("menu_id"));
obj.put("pid", obj.getLong("pid"));
//构建前端需要的数据结构树
Integer sub_count = obj.getInteger("sub_count");
if (sub_count <= 0) {
obj.put("leaf", true);
obj.put("hasChildren", false);
} else {
obj.put("leaf", false);
obj.put("hasChildren", true);
}
res.add(obj);
}
json.put("content", res);
return json;
}
@Override
@@ -112,11 +121,42 @@ public class MenuServiceImpl implements MenuService {
}
@Override
@Cacheable(key = "'id:' + #p0")
public MenuDto menuJsonToMenuDto(JSONObject json) {
MenuDto menuDto = new MenuDto();
menuDto.setMenu_id(json.getLong("menu_id"));
menuDto.setType(json.getInteger("type"));
menuDto.setPermission(json.getString("permission"));
menuDto.setTitle(json.getString("title"));
menuDto.setMenu_sort(json.getInteger("menu_sort"));
menuDto.setPath(json.getString("path"));
menuDto.setComponent(json.getString("component"));
menuDto.setPid(json.getLong("pid"));
menuDto.setSub_count(json.getInteger("sub_count"));
menuDto.setI_frame("1".equals(json.getString("i_frame")));
menuDto.setCache("1".equals(json.getString("cache")));
menuDto.setHidden("1".equals(json.getString("hidden")));
menuDto.setComponent_name(json.getString("component_name"));
//构建前端需要的数据结构树
Integer sub_count = json.getInteger("sub_count");
if (sub_count <= 0) {
menuDto.setLeaf(true);
menuDto.setHasChildren(false);
} else {
menuDto.setLeaf(false);
menuDto.setHasChildren(true);
}
return menuDto;
}
@Override
// @Cacheable(key = "'id:' + #p0")
public MenuDto findById(long id) {
Menu menu = menuRepository.findById(id).orElseGet(Menu::new);
ValidationUtil.isNull(menu.getId(), "Menu", "id", id);
return menuMapper.toDto(menu);
WQLObject menuTab = WQLObject.getWQLObject("sys_menu");
JSONObject json = menuTab.query("menu_id = '" + id + "'").uniqueResult(0);
return this.menuJsonToMenuDto(json);
}
/**
@@ -134,16 +174,16 @@ public class MenuServiceImpl implements MenuService {
JSONObject json = arr.getJSONObject(i);
MenuDto dto = new MenuDto();
dto.setId(json.getLong("menu_id"));
dto.setMenu_id(json.getLong("menu_id"));
dto.setType(json.getInteger("type"));
dto.setPermission(json.getString("permission"));
dto.setTitle(json.getString("title"));
dto.setPath(json.getString("path"));
dto.setComponentName(json.getString("name"));
dto.setComponent_name(json.getString("name"));
dto.setComponent(json.getString("component"));
dto.setIcon(json.getString("icon"));
dto.setMenuSort(json.getInteger("menu_sort"));
dto.setSubCount(json.getInteger("sub_count"));
dto.setMenu_sort(json.getInteger("menu_sort"));
dto.setSub_count(json.getInteger("sub_count"));
dto.setPid(json.getLong("pid"));
@@ -157,9 +197,9 @@ public class MenuServiceImpl implements MenuService {
}
if (StrUtil.equals(iFrame, "1")) {
dto.setIFrame(true);
dto.setI_frame(true);
} else {
dto.setIFrame(false);
dto.setI_frame(false);
}
if (StrUtil.equals(hidden, "1")) {
dto.setHidden(true);
@@ -187,6 +227,8 @@ public class MenuServiceImpl implements MenuService {
form.put("update_time", DateUtil.now());
menuTab.insert(form);
//TODO 更新子节点数量
}
@Override
@@ -196,10 +238,12 @@ public class MenuServiceImpl implements MenuService {
}
@Override
public Set<Menu> getChildMenus(List<Menu> menuList, Set<Menu> menuSet) {
for (Menu menu : menuList) {
public Set<MenuDto> getChildMenus(List<MenuDto> menuList, Set<MenuDto> menuSet) {
for (MenuDto menu : menuList) {
menuSet.add(menu);
List<Menu> menus = menuRepository.findByPid(menu.getId());
List<MenuDto> menus = new ArrayList<>();
WQLObject menuTab = WQLObject.getWQLObject("sys_menu");
menuTab.query("pid = '" + menu.getPid() + "'");
if (menus != null && menus.size() != 0) {
getChildMenus(menus, menuSet);
}
@@ -209,18 +253,18 @@ public class MenuServiceImpl implements MenuService {
@Override
@Transactional(rollbackFor = Exception.class)
public void delete(Set<Menu> menuSet) {
for (Menu menu : menuSet) {
public void delete(Set<MenuDto> menuSet) {
for (MenuDto menu : menuSet) {
// 清理缓存
delCaches(menu.getId());
roleService.untiedMenu(menu.getId());
menuRepository.deleteById(menu.getId());
delCaches(menu.getMenu_id());
roleService.untiedMenu(menu.getMenu_id());
WQLObject.getWQLObject("sys_menu").delete("menu_id = '" + menu.getMenu_id() + "'");
updateSubCnt(menu.getPid());
}
}
@Override
public JSONArray getMenus(Long pid) {
public List<MenuDto> getMenus(Long pid) {
// 菜单表【sys_menu】
WQLObject menuTab = WQLObject.getWQLObject("sys_menu");
JSONArray menus;
@@ -229,28 +273,29 @@ public class MenuServiceImpl implements MenuService {
} else {
menus = menuTab.query("(pid =0 or pid is null)").getResultJSONArray(0);
}
List<MenuDto> list = new ArrayList<>();
//判断是否叶子节点,用于前端构建树
for (int i = 0; i < menus.size(); i++) {
JSONObject json = menus.getJSONObject(i);
Integer sub_count = json.getInteger("sub_count");
if (sub_count <= 0) {
json.put("leaf", true);
json.put("hasChildren", false);
} else {
json.put("leaf", false);
json.put("hasChildren", true);
}
list.add(this.menuJsonToMenuDto(json));
}
return menus;
return list;
}
@Override
public List<MenuDto> getSuperior(MenuDto menuDto, List<Menu> menus) {
public List<MenuDto> getSuperior(MenuDto menuDto, List<MenuDto> menus) {
WQLObject menuTab = WQLObject.getWQLObject("sys_menu");
if (menuDto.getPid() == null) {
menus.addAll(menuRepository.findByPidIsNull());
return menuMapper.toDto(menus);
JSONArray arr = menuTab.query("pid is null").getResultJSONArray(0);
for (int i = 0; i < arr.size(); i++) {
JSONObject json = arr.getJSONObject(i);
menus.add(this.menuJsonToMenuDto(json));
}
return menus;
}
menus.addAll(menuRepository.findByPid(menuDto.getPid()));
MenuDto dto = this.findById(menuDto.getPid());
menus.addAll((Collection<? extends MenuDto>) dto);
return getSuperior(findById(menuDto.getPid()), menus);
}
@@ -263,17 +308,17 @@ public class MenuServiceImpl implements MenuService {
trees.add(menuDTO);
}
for (MenuDto it : menuDtos) {
if (menuDTO.getId().equals(it.getPid())) {
if (menuDTO.getMenu_id().equals(it.getPid())) {
if (menuDTO.getChildren() == null) {
menuDTO.setChildren(new ArrayList<>());
}
menuDTO.getChildren().add(it);
ids.add(it.getId());
ids.add(it.getMenu_id());
}
}
}
if (trees.size() == 0) {
trees = menuDtos.stream().filter(s -> !ids.contains(s.getId())).collect(Collectors.toList());
trees = menuDtos.stream().filter(s -> !ids.contains(s.getMenu_id())).collect(Collectors.toList());
}
return trees;
}
@@ -285,12 +330,12 @@ public class MenuServiceImpl implements MenuService {
if (menuDTO != null) {
List<MenuDto> menuDtoList = menuDTO.getChildren();
MenuVo menuVo = new MenuVo();
menuVo.setName(ObjectUtil.isNotEmpty(menuDTO.getComponentName()) ? menuDTO.getComponentName() : menuDTO.getTitle());
menuVo.setName(ObjectUtil.isNotEmpty(menuDTO.getComponent_name()) ? menuDTO.getComponent_name() : menuDTO.getTitle());
// 一级目录需要加斜杠,不然会报警告
menuVo.setPath(menuDTO.getPid() == null ? "/" + menuDTO.getPath() : menuDTO.getPath());
menuVo.setHidden(menuDTO.getHidden());
// 如果不是外链
if (!menuDTO.getIFrame()) {
if (!menuDTO.getI_frame()) {
if (menuDTO.getPid() == null) {
menuVo.setComponent(StrUtil.isEmpty(menuDTO.getComponent()) ? "Layout" : menuDTO.getComponent());
} else if (menuDTO.getPid() != null && menuDTO.getType() == 0) {
@@ -310,7 +355,7 @@ public class MenuServiceImpl implements MenuService {
MenuVo menuVo1 = new MenuVo();
menuVo1.setMeta(menuVo.getMeta());
// 非外链
if (!menuDTO.getIFrame()) {
if (!menuDTO.getI_frame()) {
menuVo1.setPath("index");
menuVo1.setName(menuVo.getName());
menuVo1.setComponent(menuVo.getComponent());
@@ -331,17 +376,13 @@ public class MenuServiceImpl implements MenuService {
return list;
}
@Override
public Menu findOne(Long id) {
Menu menu = menuRepository.findById(id).orElseGet(Menu::new);
ValidationUtil.isNull(menu.getId(), "Menu", "id", id);
return menu;
}
private void updateSubCnt(Long menuId) {
if (menuId != null) {
int count = menuRepository.countByPid(menuId);
menuRepository.updateSubCntById(count, menuId);
WQLObject menuTab = WQLObject.getWQLObject("sys_menu");
JSONArray arr = menuTab.query("pid = '" + menuId + "'").getResultJSONArray(0);
JSONObject param = new JSONObject();
param.put("sub_count", arr.size());
menuTab.update(param, "menu_id = '" + menuId + "'");
}
}
@@ -353,7 +394,7 @@ public class MenuServiceImpl implements MenuService {
public void delCaches(Long id) {
List<User> users = userRepository.findByMenuId(id);
redisUtils.del("menu::id:" + id);
redisUtils.delByKeys("menu::user:", users.stream().map(User::getUser_id).collect(Collectors.toSet()));
redisUtils.delByKeys("menu::user:", users.stream().map(User::getId).collect(Collectors.toSet()));
// 清除 Role 缓存
List<Role> roles = roleService.findInMenuId(new ArrayList<Long>() {{
add(id);

View File

@@ -17,6 +17,7 @@
输入.system_type TYPEAS s_string
输入.category TYPEAS s_string
输入.user_id TYPEAS s_string
输入.pid TYPEAS s_string
@@ -41,6 +42,27 @@
##########################################
# 3、业务主过程 #
##########################################
IF 输入.flag = "0"
PAGEQUERY
SELECT
* ,
(CASE WHEN sub_count<=0 THEN 0 ELSE 1 END ) AS hasChildren
FROM
sys_menu
where 1=1
OPTION 输入.pid <> ""
pid = 输入.pid
ENDOPTION
OPTION 输入.pid = ""
pid is null
ENDOPTION
ENDSELECT
ENDPAGEQUERY
ENDIF
IF 输入.flag = "1"
QUERY
SELECT system_type from sys_menu GROUP BY system_type