package cn.qinys.platform.upms.api.controller;

import cn.qinys.platform.base.response.Result;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import cn.qinys.platform.entity.upms.UpmsRole;
import cn.qinys.platform.upms.api.req.*;
import cn.qinys.platform.upms.api.resp.MenuSelectedResp;
import cn.qinys.platform.upms.api.resp.RoleMenusTreeResp;
import cn.qinys.platform.upms.api.service.IRoleService;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*;



import java.util.List;

/**
 * 角色和范围
 *
 * @author tanlie
 * Date: 2024/2/27 10:43
 */
@RestController
@RequestMapping("/upms/role")
public class RoleController {

    @Resource
    IRoleService roleService;

    /**
     * 角色分页
     *
     * @param req
     * @return
     */
    @GetMapping("/page")
    public Result<Page<UpmsRole>> getRolePage(RolePageReq req) {
        Page<UpmsRole> page = roleService.getRolePage(req);
        return new Result<>(page);
    }

    /**
     * 角色新增
     *
     * @param req
     * @return
     */
    @PostMapping("/create")
    public Result<String> createRole(@RequestBody @Valid RoleCreateReq req) {
        roleService.createRole(req);
        return new Result<>("操作成功");
    }

    /**
     * 角色修改
     *
     * @param req
     * @return
     */
    @PostMapping("/update")
    public Result<String> updateRole(@RequestBody @Valid RoleUpdateReq req) {
        roleService.updateRole(req);
        return new Result<>("操作成功");
    }

    /**
     * 角色删除
     *
     * @param req
     * @return
     */
    @PostMapping("/remove")
    public Result<String> removeRole(@RequestBody @Valid RoleRemoveReq req) {
        roleService.removeRole(req);
        return new Result<>("操作成功");
    }

    /**
     * 角色列表
     *
     * @return
     */
    @GetMapping("/list")
    public Result<List<UpmsRole>> RoleList() {
        List<UpmsRole> roles = roleService.getRoleList();
        return new Result<>(roles);
    }

    /**
     * 获取后台登录菜单
     *
     * @return
     */
    @GetMapping("/menu/tree")
    public Result<List<RoleMenusTreeResp>> getMenuTree() {
        List<RoleMenusTreeResp> tree = roleService.getMenusTree();
        return new Result<>(tree);
    }

    /**
     * 新增权限
     *
     * @return
     */
    @GetMapping("/menu/tree/selected")
    public Result<MenuSelectedResp> getMenuSelected(String roleId) {
        MenuSelectedResp selected = roleService.getMenuSelected(roleId);
        return new Result<>(selected);
    }

    /**
     * 新增权限
     *
     * @return
     */
    @PostMapping("/auth/create")
    public Result<String> createResource(@RequestBody @Valid RoleMenuAuthReq req) {
        roleService.createResource(req);
        return new Result<>("操作成功");
    }

    /**
     * 新增权限码
     *
     * @return
     */
    @PostMapping("/permission/create")
    public Result<String> createPermission(@RequestBody @Valid RolePermissionReq req) {
        roleService.createAuthPermission(req);
        return new Result<>("操作成功");
    }



}
