package com.qmth.paper.library.api;

import com.qmth.boot.api.constant.ApiConstant;
import com.qmth.paper.library.common.contant.ApiPrefixConstant;
import com.qmth.paper.library.common.entity.SysPrivilege;
import com.qmth.paper.library.common.service.SysPrivilegeService;
import com.qmth.paper.library.common.util.Result;
import com.qmth.paper.library.common.util.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 * 菜单权限表 前端控制器
 * </p>
 *
 * @author xf
 * @since 2021-03-23
 */
@Api(tags = "菜单权限Controller")
@RestController
@RequestMapping(ApiConstant.DEFAULT_URI_PREFIX + ApiPrefixConstant.PREFIX_SYS + "/privilege")
public class SysPrivilegeController {

    @Resource
    private SysPrivilegeService sysPrivilegeService;

    /**
     * 查询
     */
    @ApiOperation(value = "查询")
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public Result list() {
        return ResultUtil.ok(sysPrivilegeService.listPrivilegeTreeNew());
    }

    /**
     * 查询需要授权的菜单
     */
    @ApiOperation(value = "查询需要授权的菜单")
    @RequestMapping(value = "/list_auth", method = RequestMethod.POST)
    public Result listAuth() {
        return ResultUtil.ok(sysPrivilegeService.listPrivilegeTreeAuth());
    }

    /**
     * 新增/修改
     *
     * @param privilege privilege对象
     */
    @ApiOperation(value = "新增/修改")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Result save(@RequestBody SysPrivilege privilege) {
        return ResultUtil.ok(sysPrivilegeService.savePrivilege(privilege));
    }

    /**
     * 删除
     *
     * @param privilege privilege对象
     */
    @ApiOperation(value = "删除")
    @RequestMapping(value = "/remove", method = RequestMethod.POST)
    public Result remove(@RequestBody SysPrivilege privilege) {
        return ResultUtil.ok(sysPrivilegeService.remove(privilege.getId()));
    }

    /**
     * 角色已绑定权限列表
     *
     * @param roleId 角色ID
     */
    @ApiOperation(value = "角色已绑定权限列表")
    @RequestMapping(value = "/get_role_privileges", method = RequestMethod.POST)
    public Result getRolePrivileges(@RequestParam(value = "roleId") Long roleId) {
        return ResultUtil.ok(sysPrivilegeService.getRolePrivileges(roleId));
    }
}

