package com.sunda.spmsweb.usercontroller;


import com.alibaba.fastjson.JSONObject;
import com.sunda.spmscommon.ResponseResult;
import com.sunda.spmsuser.service.ISpmsRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Wayne
 * @since 2021-01-25
 */
@RestController
@RequestMapping("/spmsRole")
@Api(tags = "系统角色信息表", description = "系统角色信息表接口")
public class SpmsRoleController {

    @Autowired
    ISpmsRoleService iSpmsRoleService;

    @RequestMapping("/getRoleList")
    @ApiOperation(value = "获取系统全部角色信息", notes = "获取系统全部角色详细信息", httpMethod = "GET")
    @RequiresPermissions("spmsRole-getRoleList")
    public ResponseResult getRoleList(){
        try {
            return ResponseResult.success().add("roleList", iSpmsRoleService.getRoleList());
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error("请求数据失败").add("error", e.getMessage());
        }
    }
    
    @RequestMapping("/findRoleListByPage")
    @ApiOperation(value = "分页获取系统角色信息", notes = "分页获取系统角色详细信息。\n" +
    		"{\n" +
            "\t\t\t\"roleCnName\": \"角色名称\",\n" +
            "\t\t\t\"roleId\": \"角色ID\",\n" +
            "\t\t\t\"companyCode\": \"企业\",\n" +
            "\t\t\t\"pageNo\": 1,\n" +
            "\t\t\t\"pageSize\": 20\n" +
            "\t\t}", httpMethod = "POST")
    @RequiresPermissions("spmsRole-findRoleListByPage")
    public ResponseResult findRoleListByPage(@RequestBody JSONObject jsonObject){
    	try {
    		if (!jsonObject.containsKey("pageNo") || !jsonObject.containsKey("pageSize")){
                return ResponseResult.error("请求参数错误");
            }
    		return ResponseResult.success().add("roleList", iSpmsRoleService.getRoleListByParam(jsonObject));
    	}catch (Exception e){
    		e.printStackTrace();
    		return ResponseResult.error("请求数据失败").add("error", e.getMessage());
    	}
    }

    @RequestMapping("/createRole")
    @ApiOperation(value = "新增/更新角色信息", notes = "新增/更新角色信息。\n" +
            "uuid 为空则新增，新增数据时；uuid 不为空则更新角色信息；\n" +
            "{\n" +
            "\t\t\t\"roleCnName\": \"需求提报员\",\n" +
            "\t\t\t\"roleInfo\": \"需求提报员\",\n" +
            "\t\t\t\"updateUser\": \"999947\",\n" +
            "\t\t\t\"createUser\": \"999947\",\n" +
            "\t\t\t\"roleEnName\": \"Demand Requester\",\n" +
            "\t\t\t\"companyCode\": \"sunda\",\n" +
            "\t\t\t\"uuid\": \"161556d97ae44139ad21659c5fb98e00\"\n" +
            "\t\t}", httpMethod = "POST")
    @RequiresPermissions("spmsRole-createRole")
    public ResponseResult createRole(@RequestBody JSONObject jsonObject){
        try {
            return iSpmsRoleService.createRole(jsonObject);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error("请求数据失败").add("error", e.getMessage());
        }
    }

    @RequestMapping("/createRolePerms")
    @ApiOperation(value = "新增/更新角色权限关系信息", notes = "新增/更新角色权限关系信息。\n" +
            "roleUuid 必输，先删除 roleUuid 下所有权限编码；新建 roleUuid - permissionId 关系；\n" +
            "{\n" +
            "\t\"rolePermList\": [{\n" +
            "\t\t\"roleUuid\": \"38a089dbe2c44545ab1f101cedeec0dc\",\n" +
            "\t\t\"permissionId\": [\"10001\", \"10002\", \"10003\", \"10004\", \"10005\"]\n" +
            "\t}, {\n" +
            "\t\t\"roleUuid\": \"ff60bd7e73294417ab68bda207e2b5b0\",\n" +
            "\t\t\"permissionId\": [\"10001\", \"10002\", \"10003\", \"10004\"]\n" +
            "\t}]\n" +
            "}", httpMethod = "POST")
    @RequiresPermissions("spmsRole-createRolePerms")
    public ResponseResult createRolePerms(@RequestBody JSONObject jsonObject){
        try {
            return iSpmsRoleService.createRolePerms(jsonObject);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error("请求数据失败").add("error", e.getMessage());
        }
    }
    
    @RequestMapping("/createRoleMenus")
    @ApiOperation(value = "新增/更新角色菜单关系信息", notes = "新增/更新角色菜单关系信息。\n" +
            "roleUuid 必输，先删除 roleUuid 下所有菜单编码；新建 roleUuid - menuId 关系；\n" +
            "{\n" +
            "\t\"roleMenuList\": [{\n" +
            "\t\t\"roleUuid\": \"38a089dbe2c44545ab1f101cedeec0dc\",\n" +
            "\t\t\"menuIds\": [\"100001\", \"100002\", \"100003\", \"100004\", \"100005\"]\n" +
            "\t}, {\n" +
            "\t\t\"roleUuid\": \"ff60bd7e73294417ab68bda207e2b5b0\",\n" +
            "\t\t\"menuIds\": [\"200001\", \"200002\", \"200003\", \"200004\"]\n" +
            "\t}]\n" +
            "}", httpMethod = "POST")
    @RequiresPermissions("spmsRole-createRoleMenus")
    public ResponseResult createRoleMenus(@RequestBody JSONObject jsonObject){
        try {
            return iSpmsRoleService.createRoleMenus(jsonObject);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error("请求数据失败").add("error", e.getMessage());
        }
    }

}
