|
@@ -0,0 +1,178 @@
|
|
|
+/*
|
|
|
+ * 爱组搭 http://aizuda.com 低代码组件化开发平台
|
|
|
+ * ------------------------------------------
|
|
|
+ * 受知识产权保护,请勿删除版权申明
|
|
|
+ */
|
|
|
+package com.aizuda.boot.system.service.impl;
|
|
|
+
|
|
|
+import com.aizuda.core.api.ApiAssert;
|
|
|
+import com.aizuda.service.service.BaseServiceImpl;
|
|
|
+import com.aizuda.boot.system.entity.SysResource;
|
|
|
+import com.aizuda.boot.system.entity.SysResourceApi;
|
|
|
+import com.aizuda.boot.system.entity.enums.ResourceType;
|
|
|
+import com.aizuda.boot.system.entity.param.ResourceParam;
|
|
|
+import com.aizuda.boot.system.entity.vo.MenuVO;
|
|
|
+import com.aizuda.boot.system.entity.vo.ResourceTreeVO;
|
|
|
+import com.aizuda.boot.system.mapper.SysResourceMapper;
|
|
|
+import com.aizuda.boot.system.service.ISysResourceApiService;
|
|
|
+import com.aizuda.boot.system.service.ISysResourceService;
|
|
|
+import com.aizuda.service.web.UserSession;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
+import jakarta.annotation.Resource;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+
|
|
|
+import java.util.*;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 系统资源 服务实现类
|
|
|
+ *
|
|
|
+ * @author 青苗
|
|
|
+ * @since 2021-11-07
|
|
|
+ */
|
|
|
+@Service
|
|
|
+public class SysResourceServiceImpl extends BaseServiceImpl<SysResourceMapper, SysResource> implements ISysResourceService {
|
|
|
+ @Resource
|
|
|
+ private ISysResourceApiService sysResourceApiService;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Page<SysResource> page(Page<SysResource> page, SysResource sysResource) {
|
|
|
+ LambdaQueryWrapper<SysResource> lqw = Wrappers.lambdaQuery(sysResource);
|
|
|
+ return super.page(page, lqw);
|
|
|
+ }
|
|
|
+
|
|
|
+ private List<SysResource> listAll(boolean menu) {
|
|
|
+ return lambdaQuery().eq(SysResource::getStatus, 1)
|
|
|
+ // 菜单不查按钮
|
|
|
+ .ne(menu, SysResource::getType, 3)
|
|
|
+ .orderByDesc(SysResource::getSort).list();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<ResourceTreeVO> listTree() {
|
|
|
+ List<SysResource> sysResourceList = this.listAll(false);
|
|
|
+ if (CollectionUtils.isEmpty(sysResourceList)) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ return sysResourceList.stream().filter(e -> Objects.equals(0L, e.getPid())).map(e -> {
|
|
|
+ ResourceTreeVO vo = e.convert(ResourceTreeVO.class);
|
|
|
+ vo.setChildren(this.getChild(vo.getId(), vo.getTitle(), sysResourceList));
|
|
|
+ return vo;
|
|
|
+ }).collect(Collectors.toList());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取子节点
|
|
|
+ */
|
|
|
+ protected List<ResourceTreeVO> getChild(Long id, String parentName, List<SysResource> sysResourceList) {
|
|
|
+ // 遍历所有节点,将所有菜单的父id与传过来的根节点的id比较
|
|
|
+ List<SysResource> childList = sysResourceList.stream().filter(e -> Objects.equals(id, e.getPid()))
|
|
|
+ .collect(Collectors.toList());
|
|
|
+ if (childList.isEmpty()) {
|
|
|
+ // 没有子节点,返回一个空 List(递归退出)
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ // 递归
|
|
|
+ return childList.stream().map(e -> {
|
|
|
+ ResourceTreeVO vo = e.convert(ResourceTreeVO.class);
|
|
|
+ vo.setParentName(parentName);
|
|
|
+ vo.setChildren(this.getChild(vo.getId(), vo.getTitle(), sysResourceList));
|
|
|
+ return vo;
|
|
|
+ }).collect(Collectors.toList());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Map<String, Object> listMenuPermissions(UserSession userSession) {
|
|
|
+ Map<String, Object> menuMap = new HashMap<>(2);
|
|
|
+ menuMap.put("menu", this.listMenuVO(userSession));
|
|
|
+ menuMap.put("permissions", Arrays.asList("list.add", "list.edit", "list.delete",
|
|
|
+ "user.add", "user.edit", "user.delete"));
|
|
|
+ return menuMap;
|
|
|
+ }
|
|
|
+
|
|
|
+ protected List<MenuVO> listMenuVO(UserSession userSession) {
|
|
|
+ List<SysResource> sysResourceList;
|
|
|
+ if (UserSession.isAdmin(userSession.getId())) {
|
|
|
+ // 管理员设置为所有权限
|
|
|
+ sysResourceList = this.listAll(true);
|
|
|
+ } else {
|
|
|
+ sysResourceList = baseMapper.selectMenuByUserId(userSession.getId());
|
|
|
+ }
|
|
|
+ if (CollectionUtils.isEmpty(sysResourceList)) {
|
|
|
+ return Collections.EMPTY_LIST;
|
|
|
+ }
|
|
|
+ return sysResourceList.stream().filter(e -> Objects.equals(0L, e.getPid()))
|
|
|
+ .map(e -> this.getMenuVO(e, sysResourceList))
|
|
|
+ .collect(Collectors.toList());
|
|
|
+ }
|
|
|
+
|
|
|
+ protected MenuVO getMenuVO(SysResource sysResource, List<SysResource> sysResourceList) {
|
|
|
+ MenuVO vo = new MenuVO();
|
|
|
+ vo.setName(sysResource.getAlias());
|
|
|
+ vo.setPath(sysResource.getPath());
|
|
|
+ vo.setComponent(sysResource.getComponent());
|
|
|
+ vo.setMeta(new HashMap<String, Object>(5) {{
|
|
|
+ put("title", sysResource.getTitle());
|
|
|
+ put("icon", sysResource.getIcon());
|
|
|
+ put("type", ResourceType.convert(sysResource.getType()));
|
|
|
+ if (sysResource.getHidden()) {
|
|
|
+ put("hidden", true);
|
|
|
+ }
|
|
|
+ // put("hiddenBreadcrumb", true);
|
|
|
+ }});
|
|
|
+ vo.setChildren(this.getMenuChild(sysResource.getId(), sysResourceList));
|
|
|
+ return vo;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取子节点
|
|
|
+ */
|
|
|
+ protected List<MenuVO> getMenuChild(Long id, List<SysResource> sysResourceList) {
|
|
|
+ // 遍历所有节点,将所有菜单的父id与传过来的根节点的id比较
|
|
|
+ List<SysResource> childList = sysResourceList.stream().filter(e -> Objects.equals(id, e.getPid()))
|
|
|
+ .collect(Collectors.toList());
|
|
|
+ if (childList.isEmpty()) {
|
|
|
+ // 没有子节点,返回一个空 List(递归退出)
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ // 递归
|
|
|
+ return childList.stream().map(e -> this.getMenuVO(e, sysResourceList)).collect(Collectors.toList());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean removeByIds(Collection<?> ids) {
|
|
|
+ this.checkExists(Wrappers.<SysResource>lambdaQuery().select(SysResource::getId)
|
|
|
+ .in(SysResource::getPid, ids), "存在子类不允许删除");
|
|
|
+ return super.removeByIds(ids);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ @Override
|
|
|
+ public boolean updateByResourceParam(ResourceParam param) {
|
|
|
+ ApiAssert.isEmpty(param.getId(), "主键不存在无法更新");
|
|
|
+ SysResource sysResource = param.convert(SysResource.class);
|
|
|
+ ApiAssert.fail(!super.updateById(sysResource), "更新失败");
|
|
|
+ // 保存资源接口
|
|
|
+ List<SysResourceApi> apiList = param.getApiList();
|
|
|
+ if (CollectionUtils.isNotEmpty(apiList)) {
|
|
|
+ // 移除空字符串
|
|
|
+ apiList = apiList.stream().filter(t -> StringUtils.isNoneBlank(t.getUrl()) && StringUtils.isNoneBlank(t.getCode()))
|
|
|
+ .collect(Collectors.toList());
|
|
|
+ if (CollectionUtils.isNotEmpty(apiList)) {
|
|
|
+ sysResourceApiService.removeByResourceId(param.getId());
|
|
|
+ apiList.forEach(t -> {
|
|
|
+ t.setResourceId(param.getId());
|
|
|
+ t.setUrl(t.getUrl().trim());
|
|
|
+ t.setCode(t.getCode().trim());
|
|
|
+ });
|
|
|
+ ApiAssert.fail(!sysResourceApiService.saveBatch(apiList), "接口权限保存失败");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+}
|