RoleController.java 3.96 KB
package vion.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.lang.Opt;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.github.linpeilie.Converter;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import vion.dto.RoleDTO;
import vion.model.*;
import vion.service.*;
import vion.vo.ResourceVO;
import vion.vo.RoleVO;
import vion.vo.UserVO;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 角色管理
 */
@RestController
@RequestMapping("/api")
@RequiredArgsConstructor
public class RoleController {

    private final IRoleService roleService;
    private final IRUserRoleService userRoleService;
    private final IUserService userService;
    private final IResourceService resourceService;
    private final IRRoleResourceService roleResourceService;
    private final Converter converter;

    @GetMapping("/role")
    @SaCheckPermission(value = "role:list", orRole = "admin")
    public Page<RoleVO> list(RoleDTO dto) {
        return roleService.list(dto);
    }

    @GetMapping("/role/{id}")
    @SaCheckPermission(value = "role:query", orRole = "admin")
    public RoleVO getRoleById(@PathVariable Long id) {
        Role role = roleService.getById(id);
        return converter.convert(role, RoleVO.class);
    }

    @PostMapping("/role")
    @SaCheckPermission(value = "role:save", orRole = "admin")
    private String save(@RequestBody RoleDTO dto) {
        if (StrUtil.equals(dto.getCode(), "admin")) {
            return "管理员角色编码[admin]已存在,不允许再次创建";
        }
        Role role = converter.convert(dto, Role.class);
        return roleService.save(role) ? "创建成功" : "创建失败";
    }

    @PostMapping("/role/{id}")
    @SaCheckPermission(value = "role:edit", orRole = "admin")
    private String update(@PathVariable Long id, @RequestBody RoleDTO dto) {
        if (id == 1) {
            return "管理员角色不允许修改";
        }
        Role role = converter.convert(dto, Role.class);
        role.setId(id);
        return roleService.updateById(role) ? "更新成功" : "更新失败";
    }

    @DeleteMapping("/role/{id}")
    @SaCheckPermission(value = "role:remove", orRole = "admin")
    private String remove(@PathVariable Long id) {
        if (id == 1) {
            return "管理员角色不允许删除";
        }
        return roleService.removeById(id) ? "删除成功" : "删除失败";
    }

    @GetMapping("/role/user/{id}")
    @SaCheckPermission(value = "role:user", orRole = "admin")
    public List<UserVO> listUserById(@PathVariable Long id) {
        List<RUserRole> userRoleList = userRoleService.lambdaQuery().eq(RUserRole::getRoleId, id).list();
        List<User> userList = Opt.ofEmptyAble(userRoleList)
                .map(l -> l.stream().map(RUserRole::getUserId).collect(Collectors.toList()))
                .filter(CollUtil::isNotEmpty)
                .map(userIdList -> userService.lambdaQuery().in(User::getId, userIdList).list())
                .orElse(ListUtil.empty());
        return converter.convert(userList, UserVO.class);
    }

    @GetMapping("/role/resource/{id}")
    @SaCheckPermission(value = "role:resource", orRole = "admin")
    public List<ResourceVO> listResourceById(@PathVariable Long id) {
        List<RRoleResource> roleResourceList = roleResourceService.lambdaQuery().eq(RRoleResource::getRoleId, id).list();
        List<Resource> resourceList = Opt.ofEmptyAble(roleResourceList)
                .map(l -> l.stream().map(RRoleResource::getResourceId).collect(Collectors.toList()))
                .filter(CollUtil::isNotEmpty)
                .map(resourceIdList -> resourceService.lambdaQuery().in(Resource::getId, resourceIdList).list())
                .orElse(ListUtil.empty());

        return converter.convert(resourceList, ResourceVO.class);
    }
}