RoleController.java 2.63 KB
package vion.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
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.RUserRole;
import vion.model.Role;
import vion.model.User;
import vion.service.IRUserRoleService;
import vion.service.IRoleService;
import vion.service.IUserService;
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 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, new RoleVO());
    }

    @PostMapping("/role")
    @SaCheckPermission(value = "role:save", orRole="admin")
    private String save(@RequestBody RoleDTO dto) {
        Role role = converter.convert(dto, new Role());
        return roleService.save(role) ? "创建成功" : "创建失败";
    }

    @PostMapping("/role/{id}")
    @SaCheckPermission(value = "role:edit", orRole="admin")
    private String update(@PathVariable Long id, @RequestBody RoleDTO dto) {
        Role role = converter.convert(dto, new Role());
        role.setId(id);
        return roleService.updateById(role) ? "更新成功" : "更新失败";
    }

    @DeleteMapping("/role/{id}")
    @SaCheckPermission(value = "role:remove", orRole="admin")
    private String remove(@PathVariable Long id) {
        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<Long> userIdList = userRoleList.stream().map(RUserRole::getUserId).collect(Collectors.toList());
        List<User> userList = userService.lambdaQuery().in(User::getId, userIdList).list();
        return converter.convert(userList, UserVO.class);
    }
}