|
@@ -0,0 +1,326 @@
|
|
|
+package pay.platform.api.system.servcie;
|
|
|
+
|
|
|
+import cn.hutool.core.bean.BeanUtil;
|
|
|
+import cn.hutool.core.collection.CollectionUtil;
|
|
|
+import cn.hutool.core.util.StrUtil;
|
|
|
+import com.mybatisflex.core.query.QueryWrapper;
|
|
|
+import jakarta.annotation.Nonnull;
|
|
|
+import jakarta.annotation.Nullable;
|
|
|
+import jakarta.annotation.Resource;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.context.annotation.Lazy;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+import pay.platform.api.system.model.dto.PermissionTree;
|
|
|
+import pay.platform.api.system.model.query.PermissionTreeQuery;
|
|
|
+import pay.platform.api.system.model.vo.PermissionVO;
|
|
|
+import pay.platform.api.system.model.vo.VueMenuRouteMetaVO;
|
|
|
+import pay.platform.api.system.model.vo.VueMenuRouteVO;
|
|
|
+import pay.platform.core.common.Result;
|
|
|
+import pay.platform.core.common.domain.BaseService;
|
|
|
+import pay.platform.core.enums.MenuTypeEnums;
|
|
|
+import pay.platform.domain.SysPermission;
|
|
|
+import pay.platform.domain.table.SysPermissionTableDef;
|
|
|
+import pay.platform.mapper.SysPermissionMapper;
|
|
|
+
|
|
|
+import java.util.*;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 系统 - 菜单与权限 服务层实现。
|
|
|
+ *
|
|
|
+ * @author ADMIN
|
|
|
+ * @since 1.0
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@Service
|
|
|
+public class SysPermissionService extends BaseService<SysPermissionMapper, SysPermission> {
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ @Lazy
|
|
|
+ private SysUserService sysUserService;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取权限ID集合 BY 角色ID结合
|
|
|
+ *
|
|
|
+ * @param roleIds 角色ID集合
|
|
|
+ * @return 权限ID集合
|
|
|
+ */
|
|
|
+ @Nonnull
|
|
|
+ public Set<String> listPermissionIdsByRoleIds(@Nonnull List<String> roleIds) {
|
|
|
+ List<SysPermission> permissions = mapper.listByRoleIds(roleIds, true);
|
|
|
+ if (null == permissions) {
|
|
|
+ return Collections.emptySet();
|
|
|
+ }
|
|
|
+ return permissions.stream().map(SysPermission::getId).collect(Collectors.toSet());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据ID查询已启用的权限code
|
|
|
+ *
|
|
|
+ * @param permissionIds ID
|
|
|
+ * @return 已启用的权限code
|
|
|
+ */
|
|
|
+ @Nonnull
|
|
|
+ public Set<String> listPermissionPreByIds(@Nonnull List<String> permissionIds) {
|
|
|
+ List<SysPermission> permissions = mapper.listPermission(permissionIds, true);
|
|
|
+ if (permissions == null) {
|
|
|
+ return Collections.emptySet();
|
|
|
+ }
|
|
|
+ return permissions.stream().map(SysPermission::getPerms).collect(Collectors.toSet());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取全部的权限信息
|
|
|
+ *
|
|
|
+ * @return .
|
|
|
+ */
|
|
|
+ @Nonnull
|
|
|
+ public Set<String> allPermissionPre() {
|
|
|
+ List<SysPermission> permissions = mapper.listPermission(null, true);
|
|
|
+ if (null == permissions) {
|
|
|
+ return Collections.emptySet();
|
|
|
+ }
|
|
|
+ return permissions.stream().map(SysPermission::getPerms).collect(Collectors.toSet());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取全部的ID
|
|
|
+ *
|
|
|
+ * @return .
|
|
|
+ */
|
|
|
+ @Nonnull
|
|
|
+ public Set<String> allPermissionIds() {
|
|
|
+ List<SysPermission> permissions = mapper.list(true);
|
|
|
+ if (null == permissions) {
|
|
|
+ return Collections.emptySet();
|
|
|
+ }
|
|
|
+ return permissions.stream().map(SysPermission::getId).collect(Collectors.toSet());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据用户获取菜单路由
|
|
|
+ *
|
|
|
+ * @param userid 用户ID
|
|
|
+ * @return 菜单路由
|
|
|
+ */
|
|
|
+ public List<VueMenuRouteVO> queryRouteByUserid(String userid) {
|
|
|
+ Set<String> roleIds = new HashSet<>();
|
|
|
+ roleIds.add("151835715022966784394251");
|
|
|
+ Set<String> permissionsIds = listPermissionIdsByRoleIds(List.copyOf(roleIds));
|
|
|
+ return queryRouteByIds(List.copyOf(permissionsIds));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据ID查询已启用的路由信息
|
|
|
+ *
|
|
|
+ * @param ids ID
|
|
|
+ * @return 路由信息
|
|
|
+ */
|
|
|
+ @Nonnull
|
|
|
+ public List<VueMenuRouteVO> queryRouteByIds(@Nonnull List<String> ids) {
|
|
|
+ List<SysPermission> sysPermissions = mapper.listMenuByIds(ids, true);
|
|
|
+ if (null == sysPermissions) {
|
|
|
+ return Collections.emptyList();
|
|
|
+ }
|
|
|
+ List<PermissionTree> trees = buildTree(sysPermissions);
|
|
|
+ return buildRoute(trees);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将菜单路由权限集合构建为树形
|
|
|
+ *
|
|
|
+ * @param permissions 菜单路由权限
|
|
|
+ * @return .
|
|
|
+ */
|
|
|
+ public List<PermissionTree> buildTree(@Nonnull List<SysPermission> permissions) {
|
|
|
+ List<PermissionTree> permissionTrees = BeanUtil.copyToList(permissions, PermissionTree.class);
|
|
|
+ List<PermissionTree> trees = new ArrayList<>();
|
|
|
+ Set<String> ids = new HashSet<>();
|
|
|
+ for (PermissionTree permissionTree : permissionTrees) {
|
|
|
+ if (StrUtil.isBlank(permissionTree.getParentId())) {
|
|
|
+ trees.add(permissionTree);
|
|
|
+ }
|
|
|
+ for (PermissionTree tree : permissionTrees) {
|
|
|
+ if (permissionTree.getId().equals(tree.getParentId())) {
|
|
|
+ if (permissionTree.getChildren() == null) {
|
|
|
+ permissionTree.setChildren(new ArrayList<>());
|
|
|
+ }
|
|
|
+ permissionTree.getChildren().add(tree);
|
|
|
+
|
|
|
+ ids.add(tree.getId());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (trees.isEmpty()) {
|
|
|
+ trees = permissionTrees.stream().filter(s -> !ids.contains(s.getId())).collect(Collectors.toList());
|
|
|
+ }
|
|
|
+ return trees;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 构建菜单路由
|
|
|
+ *
|
|
|
+ * @param permissionTrees 菜单路由权限树集合
|
|
|
+ * @return 菜单路由
|
|
|
+ */
|
|
|
+ public List<VueMenuRouteVO> buildRoute(@Nonnull List<PermissionTree> permissionTrees) {
|
|
|
+ List<VueMenuRouteVO> menuRouteList = new ArrayList<>();
|
|
|
+ for (PermissionTree permissionTree : permissionTrees) {
|
|
|
+ menuRouteList.add(buildRoute(permissionTree));
|
|
|
+ }
|
|
|
+ return menuRouteList;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 构建菜单路由
|
|
|
+ *
|
|
|
+ * @param permissionTree 菜单路由权限树
|
|
|
+ * @return 菜单路由
|
|
|
+ */
|
|
|
+ public VueMenuRouteVO buildRoute(PermissionTree permissionTree) {
|
|
|
+ VueMenuRouteVO menuRoute = new VueMenuRouteVO();
|
|
|
+ Integer menuType = permissionTree.getMenuType();
|
|
|
+ // 路由地址 必须有个 `/`
|
|
|
+ menuRoute.setPath(StrUtil.addPrefixIfNot(permissionTree.getPath(), "/"));
|
|
|
+ // 路由名字(必须保持唯一)
|
|
|
+ menuRoute.setName(permissionTree.getName());
|
|
|
+ // 元信息
|
|
|
+ VueMenuRouteMetaVO vo = new VueMenuRouteMetaVO();
|
|
|
+ // 菜单名称
|
|
|
+ vo.setTitle(permissionTree.getTitle());
|
|
|
+ // 菜单图标
|
|
|
+ vo.setIcon(permissionTree.getIcon());
|
|
|
+ // 是否显示
|
|
|
+ vo.setShowLink(permissionTree.getShowLink() == 1);
|
|
|
+ // 目录
|
|
|
+ if (MenuTypeEnums.dir.getValue().equals(menuType)) {
|
|
|
+ // 菜单排序,值越高排的越后(只针对顶级路由)
|
|
|
+ // TODO 其实这里已经时有序取出了,所以可以不需要给RouteMeta设置rank
|
|
|
+ vo.setRank(permissionTree.getRank());
|
|
|
+ }
|
|
|
+ //菜单
|
|
|
+ else if (MenuTypeEnums.menu.getValue().equals(menuType)) {
|
|
|
+ // 按需加载需要展示的页面 不需要 '/'
|
|
|
+ menuRoute.setComponent(StrUtil.removePrefix(permissionTree.getComponent(), "/"));
|
|
|
+ // 是否显示父级菜单
|
|
|
+ vo.setShowParent(permissionTree.getShowParent() == 1);
|
|
|
+ // 是否缓存该路由页面
|
|
|
+ vo.setKeepAlive(permissionTree.getKeepAlive() == 1);
|
|
|
+ // 需要内嵌的iframe链接地址
|
|
|
+ if (permissionTree.getIsFrame() == 1) {
|
|
|
+ vo.setFrameSrc(permissionTree.getPath());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ menuRoute.setMeta(vo);
|
|
|
+
|
|
|
+ if (CollectionUtil.isNotEmpty(permissionTree.getChildren())) {
|
|
|
+ menuRoute.setChildren(buildRoute(permissionTree.getChildren()));
|
|
|
+ }
|
|
|
+ return menuRoute;
|
|
|
+ }
|
|
|
+ /*==============================================================================*/
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 查询菜单与权限列表
|
|
|
+ *
|
|
|
+ * @param query .
|
|
|
+ * @return .
|
|
|
+ */
|
|
|
+
|
|
|
+ public Optional<List<SysPermission>> listPermission(PermissionTreeQuery query) {
|
|
|
+ QueryWrapper queryWrapper = buildQuery(query);
|
|
|
+ return Optional.ofNullable(list(queryWrapper));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 只查询菜单列表
|
|
|
+ *
|
|
|
+ * @param query .
|
|
|
+ * @return .
|
|
|
+ */
|
|
|
+ public Optional<List<SysPermission>> listMenu(PermissionTreeQuery query) {
|
|
|
+ QueryWrapper queryWrapper = buildQuery(query);
|
|
|
+ queryWrapper.ne(SysPermission::getMenuType, MenuTypeEnums.permission.getValue());
|
|
|
+ return Optional.ofNullable(list(queryWrapper));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 保存
|
|
|
+ *
|
|
|
+ * @param vo .
|
|
|
+ * @return .
|
|
|
+ */
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public Optional<PermissionTree> save(PermissionVO vo) {
|
|
|
+ SysPermission permission = BeanUtil.toBean(vo, SysPermission.class);
|
|
|
+ log.info("【菜单与权限】新增菜单与权限,参数: {}", permission);
|
|
|
+ save(permission);
|
|
|
+ PermissionTree bean = BeanUtil.toBean(permission, PermissionTree.class);
|
|
|
+ return Optional.of(bean);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据ID更新
|
|
|
+ *
|
|
|
+ * @param vo .
|
|
|
+ * @param id .
|
|
|
+ * @return ,
|
|
|
+ */
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public Optional<PermissionTree> updateById(PermissionVO vo, @Nonnull String id) {
|
|
|
+ SysPermission permission = BeanUtil.toBean(vo, SysPermission.class);
|
|
|
+ permission.setId(id);
|
|
|
+ log.info("【菜单与权限】根据ID更新菜单与权限,参数: id--->{} data:---> {}", id, permission);
|
|
|
+ updateById(permission);
|
|
|
+ PermissionTree bean = BeanUtil.toBean(permission, PermissionTree.class);
|
|
|
+ return Optional.of(bean);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据ID删除
|
|
|
+ *
|
|
|
+ * @param query 查询条件
|
|
|
+ * @return .
|
|
|
+ */
|
|
|
+ public QueryWrapper buildQuery(@Nullable PermissionTreeQuery query) {
|
|
|
+ QueryWrapper queryWrapper = QueryWrapper.create().from(SysPermissionTableDef.SYS_PERMISSION);
|
|
|
+ queryWrapper.orderBy(SysPermission::getRank, true);
|
|
|
+ if (null == query) {
|
|
|
+ return queryWrapper;
|
|
|
+ }
|
|
|
+ queryWrapper.like(SysPermission::getTitle, query.getTitle(), StrUtil.isNotBlank(query.getTitle()));
|
|
|
+ queryWrapper.eq(SysPermission::getEnable, query.getEnable(), null != query.getEnable());
|
|
|
+ return queryWrapper;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 批量删除
|
|
|
+ *
|
|
|
+ * @param ids ID集合
|
|
|
+ * @return .
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public Result<String> deleteByIds(List<String> ids) {
|
|
|
+ if (CollectionUtil.isEmpty(ids)) {
|
|
|
+ return Result.NG("删除失败");
|
|
|
+ }
|
|
|
+ QueryWrapper queryWrapper = QueryWrapper.create().from(SysPermissionTableDef.SYS_PERMISSION);
|
|
|
+ queryWrapper.in(SysPermission::getParentId, ids);
|
|
|
+ List<SysPermission> sysPermissionList = mapper.selectListByQuery(queryWrapper);
|
|
|
+ if (CollectionUtil.isNotEmpty(sysPermissionList)) {
|
|
|
+ List<String> list = sysPermissionList.stream().map(SysPermission::getId).toList();
|
|
|
+ ids = new ArrayList<>(ids);
|
|
|
+ ids.addAll(list);
|
|
|
+ }
|
|
|
+ boolean remove = removeByIds(ids);
|
|
|
+ if (remove) {
|
|
|
+ return Result.OK("删除成功");
|
|
|
+ }
|
|
|
+ return Result.NG("删除失败");
|
|
|
+
|
|
|
+ }
|
|
|
+}
|