|
@@ -0,0 +1,237 @@
|
|
|
|
+package com.fdkankan.sale.service.impl;
|
|
|
|
+
|
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
|
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
|
+import com.fdkankan.redis.constant.RedisKey;
|
|
|
|
+import com.fdkankan.redis.util.RedisUtil;
|
|
|
|
+import com.fdkankan.sale.common.PageInfo;
|
|
|
|
+import com.fdkankan.sale.common.RequestBase;
|
|
|
|
+import com.fdkankan.sale.entity.SysMenu;
|
|
|
|
+import com.fdkankan.sale.entity.SysRole;
|
|
|
|
+import com.fdkankan.sale.entity.SysRoleMenu;
|
|
|
|
+import com.fdkankan.sale.entity.SysUser;
|
|
|
|
+import com.fdkankan.sale.mapper.ISysMenuMapper;
|
|
|
|
+import com.fdkankan.sale.service.ISysMenuService;
|
|
|
|
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
|
+import com.fdkankan.sale.service.ISysRoleMenuService;
|
|
|
|
+import com.fdkankan.sale.service.ISysUserService;
|
|
|
|
+import com.fdkankan.sale.vo.response.Meta;
|
|
|
|
+import com.fdkankan.sale.vo.response.SysMenuVo;
|
|
|
|
+import org.springframework.beans.BeanUtils;
|
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
|
+
|
|
|
|
+import java.util.*;
|
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * <p>
|
|
|
|
+ * 服务实现类
|
|
|
|
+ * </p>
|
|
|
|
+ *
|
|
|
|
+ * @author
|
|
|
|
+ * @since 2022-12-09
|
|
|
|
+ */
|
|
|
|
+@Service
|
|
|
|
+public class SysMenuServiceImpl extends ServiceImpl<ISysMenuMapper, SysMenu> implements ISysMenuService {
|
|
|
|
+ @Autowired
|
|
|
|
+ ISysRoleMenuService roleMenuService;
|
|
|
|
+ @Autowired
|
|
|
|
+ ISysUserService userService;
|
|
|
|
+ @Autowired
|
|
|
|
+ RedisUtil redisUtil;
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public List<SysMenu> getListByUserId(Object loginId) {
|
|
|
|
+ return getBaseMapper().getListByUserId(loginId);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public PageInfo pageList(RequestBase param) {
|
|
|
|
+ LambdaQueryWrapper<SysMenu> wrapper = new LambdaQueryWrapper<>();
|
|
|
|
+ wrapper.isNull(SysMenu::getParentId);
|
|
|
|
+ wrapper.orderByAsc(SysMenu::getSort);
|
|
|
|
+ Page<SysMenu> page = this.page(new Page<>(param.getPageNum(),param.getPageSize()), wrapper);
|
|
|
|
+ List<SysMenuVo> voList = getByParentId(null,page.getRecords(),false,null,null);
|
|
|
|
+
|
|
|
|
+// List<SysMenuVo> allVos = setTreeMenuVo(false,null,null);
|
|
|
|
+// List<Long> ids = page.getRecords().parallelStream().map(SysMenu::getId).collect(Collectors.toList());
|
|
|
|
+// List<SysMenuVo> voList = allVos.parallelStream().filter(vo -> ids.contains(vo.getId())).collect(Collectors.toList());
|
|
|
|
+
|
|
|
|
+ Page<SysMenuVo> resultPage = new Page<>(page.getCurrent(),page.getSize());
|
|
|
|
+ resultPage.setRecords(voList);
|
|
|
|
+ resultPage.setTotal(page.getTotal());
|
|
|
|
+ return PageInfo.PageInfo(resultPage);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public List<SysMenuVo> allShowList() {
|
|
|
|
+ return getByParentId(null,null,true,null,null);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public List<SysMenuVo> getByUserId(Long userId) {
|
|
|
|
+ SysUser user = userService.getById(userId);
|
|
|
|
+ List<SysRoleMenu> roleMenus = roleMenuService.getByRoleId(user.getRoleId());
|
|
|
|
+ if(roleMenus == null || roleMenus.size() <=0){
|
|
|
|
+ return new ArrayList<>();
|
|
|
|
+ }
|
|
|
|
+ List<SysMenu> menuList = this.getListByUserId(userId);
|
|
|
|
+ List<Long> menuIds = menuList.parallelStream().map(SysMenu::getId).collect(Collectors.toList());
|
|
|
|
+ //menuList.forEach(sysMenu -> setMenuIds(menuIds,sysMenu));
|
|
|
|
+ return getByRoleId(menuIds, user.getRoleId());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+// private void setMenuIds( List<Long> menuIds ,SysMenu sysMenu){
|
|
|
|
+// if(sysMenu.getParentId()!= null && !menuIds.contains(sysMenu.getParentId())) {
|
|
|
|
+// menuIds.add(sysMenu.getParentId());
|
|
|
|
+// SysMenu menu = this.getById(sysMenu.getParentId());
|
|
|
|
+// this.setMenuIds(menuIds,menu);
|
|
|
|
+// }
|
|
|
|
+// }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public List<SysMenuVo> getByRoleId(List<Long> menuIds ,Long roleId) {
|
|
|
|
+ if(menuIds == null){
|
|
|
|
+ List<SysRoleMenu> roleMenus = roleMenuService.getByRoleId(roleId);
|
|
|
|
+ if(roleMenus == null || roleMenus.size() <=0){
|
|
|
|
+ return new ArrayList<>();
|
|
|
|
+ }
|
|
|
|
+ menuIds = roleMenus.parallelStream().map(SysRoleMenu::getMenuId).collect(Collectors.toList());
|
|
|
|
+ }
|
|
|
|
+ return getByParentId(null,null,true,menuIds,2);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //递归设置menuTree
|
|
|
|
+ private List<SysMenuVo> getByParentId(Long parentId, List<SysMenu> list, boolean show ,List<Long> menuIds,Integer notType){
|
|
|
|
+ if(list == null){
|
|
|
|
+ LambdaQueryWrapper<SysMenu> wrapper = new LambdaQueryWrapper<>();
|
|
|
|
+ if(show){
|
|
|
|
+ wrapper.eq(SysMenu::getIsShow,1);
|
|
|
|
+ }
|
|
|
|
+ if(parentId == null){
|
|
|
|
+ wrapper.isNull(SysMenu::getParentId);
|
|
|
|
+ }else {
|
|
|
|
+ wrapper.eq(SysMenu::getParentId,parentId);
|
|
|
|
+ }
|
|
|
|
+ if(menuIds != null){
|
|
|
|
+ wrapper.in(SysMenu::getId,menuIds);
|
|
|
|
+ }
|
|
|
|
+ if(notType!=null){
|
|
|
|
+ wrapper.notIn(SysMenu::getType, Arrays.asList(notType));
|
|
|
|
+ }
|
|
|
|
+ wrapper.orderByAsc(SysMenu::getSort);
|
|
|
|
+ list = this.list(wrapper);
|
|
|
|
+ }
|
|
|
|
+ List<SysMenuVo> sysMenuVos = new ArrayList<>();
|
|
|
|
+ if(list.size() <=0){
|
|
|
|
+ return sysMenuVos;
|
|
|
|
+ }
|
|
|
|
+ for (SysMenu record : list) {
|
|
|
|
+ SysMenuVo vo = new SysMenuVo();
|
|
|
|
+ BeanUtils.copyProperties(record,vo);
|
|
|
|
+ Meta meta = new Meta();
|
|
|
|
+ meta.setIcon(record.getIcon());
|
|
|
|
+ meta.setTitle(record.getName());
|
|
|
|
+ meta.setHideMenu(record.getHideMenu() == 1);
|
|
|
|
+ meta.setFrameSrc(null);
|
|
|
|
+ vo.setMeta(meta);
|
|
|
|
+
|
|
|
|
+ sysMenuVos.add(vo);
|
|
|
|
+ List<SysMenuVo> children = getByParentId(record.getId(),null,show,menuIds,notType);
|
|
|
|
+ vo.setChildren(children);
|
|
|
|
+ }
|
|
|
|
+ return sysMenuVos;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public List<SysMenu> getButtonByUserId(Long userId) {
|
|
|
|
+ SysUser user = userService.getById(userId);
|
|
|
|
+ List<SysRoleMenu> roleMenus = roleMenuService.getByRoleId(user.getRoleId());
|
|
|
|
+ if(roleMenus == null || roleMenus.size() <=0){
|
|
|
|
+ return new ArrayList<>();
|
|
|
|
+ }
|
|
|
|
+ List<Long> menuId = roleMenus.parallelStream().map(SysRoleMenu::getMenuId).collect(Collectors.toList());
|
|
|
|
+ LambdaQueryWrapper<SysMenu> wrapper = new LambdaQueryWrapper<>();
|
|
|
|
+ wrapper.in(SysMenu::getId,menuId);
|
|
|
|
+ wrapper.eq(SysMenu::getType,2);
|
|
|
|
+ return this.list(wrapper);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public HashMap<Long, SysMenu> getByIds(List<Long> menuIds) {
|
|
|
|
+ HashMap<Long, SysMenu> map = new HashMap<>();
|
|
|
|
+ if(menuIds.size() >0){
|
|
|
|
+ LambdaQueryWrapper<SysMenu> wrapper = new LambdaQueryWrapper<>();
|
|
|
|
+ wrapper.in(SysMenu::getId,menuIds);
|
|
|
|
+ List<SysMenu> list = this.list(wrapper);
|
|
|
|
+ list.forEach(entity-> map.put(entity.getId(),entity) );
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return map;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public List<Long> getMenuIdsByRoleId(Long roleId) {
|
|
|
|
+ List<SysRoleMenu> roleMenus = roleMenuService.getByRoleId(roleId);
|
|
|
|
+ if(roleMenus.size() <=0){
|
|
|
|
+ return new ArrayList<>();
|
|
|
|
+ }
|
|
|
|
+ Set<Long> menuIds = roleMenus.stream().map(SysRoleMenu::getMenuId).collect(Collectors.toSet());
|
|
|
|
+ HashMap<Long, SysMenu> menuMap = this.getByIds(new ArrayList<>(menuIds));
|
|
|
|
+
|
|
|
|
+ Set<Long> setParentIds = new HashSet<>();
|
|
|
|
+ List<Long> returnMenuIds = new ArrayList<>();
|
|
|
|
+ List<Long> delMenuIds = new ArrayList<>();
|
|
|
|
+ List<Long> delParentIds = new ArrayList<>();
|
|
|
|
+ //如果父菜单不包含全部子菜单,将父菜单移除,不返回前端
|
|
|
|
+ for (Long menuId : menuMap.keySet()) {
|
|
|
|
+ Long parentId = menuMap.get(menuId).getParentId();
|
|
|
|
+ if(parentId != null && menuMap.containsKey(parentId) && !setParentIds.contains(parentId)){
|
|
|
|
+ setParentIds.add(parentId);
|
|
|
|
+ List<SysMenu> menuList = this.getBySonById(parentId);
|
|
|
|
+ for (SysMenu sysMenu : menuList) {
|
|
|
|
+ if(!menuMap.containsKey(sysMenu.getId()) ){
|
|
|
|
+ delMenuIds.add(parentId);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ for (Long delMenuId : delMenuIds) {
|
|
|
|
+ Long parentId = menuMap.get(delMenuId).getParentId();
|
|
|
|
+ if(parentId!= null && !delMenuIds.contains(parentId)){
|
|
|
|
+ delParentIds.add(parentId);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ for (Long menuId : menuIds) {
|
|
|
|
+ if(!delMenuIds.contains(menuId) && !delParentIds.contains(menuId)){
|
|
|
|
+ returnMenuIds.add(menuId);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return returnMenuIds;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private List<SysMenu> getBySonById(Long parentId) {
|
|
|
|
+ LambdaQueryWrapper<SysMenu> wrapper = new LambdaQueryWrapper<>();
|
|
|
|
+ wrapper.eq(SysMenu::getParentId,parentId);
|
|
|
|
+ return this.list(wrapper);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public void setRoleAndMenuCache(SysRole role, SysUser sysUser) {
|
|
|
|
+ //设置redis role
|
|
|
|
+ String roleType = role.getRoleType();
|
|
|
|
+ List<String> roleList = Collections.singletonList(roleType);
|
|
|
|
+ redisUtil.set(String.format(RedisKey.MANAGE_ROLE_USER,sysUser.getId()), JSONObject.toJSONString(roleList));
|
|
|
|
+ //设置redis perm
|
|
|
|
+ List<SysMenu> menus = this.getListByUserId(sysUser.getId());
|
|
|
|
+ List<String> menuList = menus.parallelStream().map(SysMenu::getPerms).collect(Collectors.toList());
|
|
|
|
+ redisUtil.set(String.format(RedisKey.MANAGE_PERM_USER,sysUser.getId()), JSONObject.toJSONString(menuList));
|
|
|
|
+ }
|
|
|
|
+}
|