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.contant.SystemConstant;
import com.qmth.paper.library.common.entity.SysRole;
import com.qmth.paper.library.common.service.SysRoleService;
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.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import java.security.NoSuchAlgorithmException;

/**
 * <p>
 * 角色表 前端控制器
 * </p>
 */
@Api(tags = "角色Controller")
@RestController
@RequestMapping(ApiConstant.DEFAULT_URI_PREFIX + ApiPrefixConstant.PREFIX_SYS + "/role")
@Validated
public class SysRoleController {

    @Resource
    private SysRoleService sysRoleService;

    /**
     * 查询
     *
     * @param name       名称
     * @param enable     是否启用
     * @param pageNumber 分页参数
     * @param pageSize   分页参数
     */
    @ApiOperation(value = "查询")
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public Result list(@RequestParam(value = "name", required = false) String name,
                       @RequestParam(value = "enable", required = false) Boolean enable,
                       @RequestParam @Min(SystemConstant.PAGE_NUMBER_MIN) Integer pageNumber,
                       @RequestParam @Min(SystemConstant.PAGE_SIZE_MIN) @Max(SystemConstant.PAGE_SIZE_MAX) Integer pageSize) {
        return ResultUtil.ok(sysRoleService.list(name, enable, pageNumber, pageSize));
    }

    /**
     * 新增用户时查询角色方法
     */
    @ApiOperation(value = "新增用户时查询角色方法")
    @RequestMapping(value = "/list_to_user", method = RequestMethod.POST)
    public Result listToUser() {
        return ResultUtil.ok(sysRoleService.listToUser());
    }

    /**
     * 新增/修改
     *
     * @param role role对象
     */
    @ApiOperation(value = "新增/修改")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Result save(@Valid @RequestBody SysRole role, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResultUtil.error(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        return ResultUtil.ok(sysRoleService.saveRoleNew(role));
    }

    /**
     * 启用/禁用
     *
     * @param role role对象
     */
    @ApiOperation(value = "启用/禁用")
    @RequestMapping(value = "/enable", method = RequestMethod.POST)
    public Result enable(@RequestBody SysRole role) throws NoSuchAlgorithmException {
        return ResultUtil.ok(sysRoleService.enableReport(role));
    }

    /**
     * 删除
     *
     * @param ids ids
     */
    @ApiOperation(value = "删除")
    @RequestMapping(value = "/remove", method = RequestMethod.POST)
    public Result remove(@RequestParam String ids) {
        return ResultUtil.ok(sysRoleService.removeReport(ids));
    }

    /**
     * 用户已绑定角色列表
     *
     * @param userId 用户ID
     */
    @ApiOperation(value = "用户已绑定角色列表")
    @RequestMapping(value = "/get_user_roles", method = RequestMethod.POST)
    public Result getUserRoles(@RequestParam(value = "userId") Long userId) {
        return ResultUtil.ok(sysRoleService.getUserRoles(userId));
    }

    /**
     * 设置内置角色
     *
     * @param
     */
    @ApiOperation(value = "内置角色")
    @RequestMapping(value = "/builtin", method = RequestMethod.POST)
    public Result builtin(@RequestParam Long id,
                         @RequestParam boolean defaultRole) {
        return ResultUtil.ok(sysRoleService.builtin(id, defaultRole));
    }

}

