package com.ytpm.middle.controller;

import com.ytpm.general.Result;
import com.ytpm.general.ResultTable;
import com.ytpm.middle.param.MiddlePermissionParam;
import com.ytpm.middle.service.PermissionService;
import com.ytpm.middle.view.MiddlePermissionVO;
import com.ytpm.middle.view.MiddleUserInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;

@Api(tags = "权限管理模块")
@RestController
@RequestMapping("/permission")
public class PermissionController {

    @Resource
    private PermissionService permissionService;

    @ApiOperation("查询权限菜单列表")
    @PostMapping("/queryTree")
    public ResultTable<MiddlePermissionVO> queryTree(@RequestBody MiddlePermissionParam param){
        return permissionService.queryTree(param);
    }

    @ApiOperation("新增权限")
    @PostMapping("/addOne")
    public Result<String> addOne(@RequestBody MiddlePermissionParam param, @ApiIgnore @AuthenticationPrincipal MiddleUserInfo userInfo){
        param.setDefaultParam(userInfo.getUserId());
        return permissionService.addOne(param);
    }

    @ApiOperation("修改权限")
    @PostMapping("/updateOne")
    public Result<String> updateOne(@RequestBody MiddlePermissionParam param, @ApiIgnore @AuthenticationPrincipal MiddleUserInfo userInfo){
        param.setUpdateParam(userInfo.getUserId());
        return permissionService.updateOne(param);
    }

    @ApiImplicitParam(name = "permissionId", value = "权限ID", required = true)
    @ApiOperation("删除权限")
    @GetMapping("/delOne")
    public Result<String> delOne(@RequestParam(name = "permissionId")int permissionId, @ApiIgnore @AuthenticationPrincipal MiddleUserInfo userInfo){
        return permissionService.delOne(permissionId,userInfo.getUserId());
    }

    @ApiOperation("根据角色查询菜单列表")
    @GetMapping("/getMenuByRole")
    public ResultTable<MiddlePermissionVO> getMenuByRole(@ApiIgnore @AuthenticationPrincipal MiddleUserInfo userInfo){
        return permissionService.getMenuByRole(userInfo);
    }

    @ApiImplicitParam(name = "permissionId", value = "权限ID", required = true)
    @ApiOperation("根据菜单获取按钮列表")
    @GetMapping("/getPermissionByMenu")
    public ResultTable<MiddlePermissionVO> getPermissionByMenu(@RequestParam(name = "permissionId")Integer permissionId,@ApiIgnore @AuthenticationPrincipal MiddleUserInfo userInfo){
        return permissionService.getPermissionByMenu(permissionId,userInfo);
    }

    @ApiOperation("根据角色查询树状权限列表")
    @GetMapping("/queryTreeByRole")
    public ResultTable<MiddlePermissionVO> queryTreeByRole(@RequestParam(name = "roleId",required = true)Integer roleId){
        return permissionService.queryTreeByRole(roleId);
    }

}
