package com.ytpm.middle.controller;

import com.ytpm.general.Result;
import com.ytpm.general.ResultTable;
import com.ytpm.middle.param.MiddleRoleParam;
import com.ytpm.middle.service.RoleService;
import com.ytpm.middle.view.MiddleRoleVO;
import com.ytpm.middle.view.MiddleUserInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
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("/role")
public class RoleController {

    @Resource
    private RoleService roleService;

    @ApiOperation("查询角色列表")
    @PostMapping("/queryList")
    public ResultTable<MiddleRoleVO> queryList(@RequestBody MiddleRoleParam param) {
        return roleService.queryList(param);
    }

    @ApiOperation("新增角色")
    @PostMapping("/addOne")
    public Result<String> addOne(@RequestBody MiddleRoleParam param, @ApiIgnore @AuthenticationPrincipal MiddleUserInfo userInfo) {
        param.setDefaultParam(userInfo.getUserId());
        return roleService.addOne(param);
    }

    @ApiOperation("修改角色")
    @PostMapping("/updateOne")
    public Result<String> updateOne(@RequestBody MiddleRoleParam param, @ApiIgnore @AuthenticationPrincipal MiddleUserInfo userInfo) {
        param.setUpdateParam(userInfo.getUserId());
        return roleService.updateOne(param);
    }

    @ApiImplicitParam(name = "roleId", value = "角色ID", required = true)
    @ApiOperation("删除角色")
    @GetMapping("/delOne")
    public Result<String> delOne(@RequestParam(name = "roleId")Integer roleId, @ApiIgnore @AuthenticationPrincipal MiddleUserInfo userInfo) {
        return roleService.delOne(roleId,userInfo.getUserId());
    }

    @ApiOperation("角色授权用户")
    @GetMapping("/grantUserList")
    public Result<String> grantUserList(@RequestParam(name = "userIds")String userIds,@RequestParam(name = "roleId")Integer roleId,@ApiIgnore @AuthenticationPrincipal MiddleUserInfo userInfo) {
        return roleService.grantUsers(userIds,roleId,userInfo);
    }
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleId", value = "角色ID", required = true),
            @ApiImplicitParam(name = "permissionIds", value = "权限ID（,拼接）", required = true),
    })
    @ApiOperation("角色权限设置")
    @GetMapping("/savePermission")
    public Result<String> savePermission(@RequestParam(name = "roleId")Integer roleId,@RequestParam(name = "permissionIds")String permissionIds,@ApiIgnore @AuthenticationPrincipal MiddleUserInfo userInfo){
        return roleService.savePermission(roleId,permissionIds,userInfo);
    }
}
