package com.huatek.frame.base.role.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
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.RestController;

import com.alibaba.dubbo.config.annotation.Reference;
import com.huatek.frame.base.dto.RoleDTO;
import com.huatek.frame.base.entity.Role;
import com.huatek.frame.base.service.RoleService;
import com.huatek.frame.base.vo.RoleUserVO;
import com.huatek.frame.base.vo.RoleVO;
import com.huatek.torch.frame.log.KerLog;
import com.huatek.torch.frame.permission.KerPermission;
import com.huatek.torch.frame.tools.ConstantUtil;
import com.huatek.torch.frame.validate.KerValid;
import com.huatek.torch.util.jwt.TokenMgr;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * 角色管理
 * 
 * @author porter_zhang
 *
 */
@Api(tags={"角色管理接口"})
@RestController
@RequestMapping("/api/role")
public class RoleController {
 
	/**
	 * 角色
	 */
	@Reference(check = false)
	private RoleService roleService; 

	/**
	 * 角色列表
	 * 
	 * @return
	 */
	@ApiOperation(value = "角色列表",httpMethod="POST", notes= "角色列表", produces = "application/json;charset=utf-8",response=RoleVO.class)
	@KerLog(projectName = "PROJECTNAME", modelName = "ROLE.MODELNAME", methodName = "ROLE.METHODNAME.ROLELIST")
	@PostMapping(value = "/roles", produces = { "application/json;charset=utf-8"})
	public  @KerPermission("10006") Map<String, Object> queryPageList(@RequestBody RoleDTO dto) {
		Map<String, Object> map = roleService.findRolePage(dto);
		return map;
	}

	/**
	 * 新增角色
	 * 
	 * @return
	 */
	@ApiOperation(value = "新增角色",httpMethod="POST", notes= "新增角色", produces = "application/json;charset=utf-8",response=Map.class)
	@KerLog(projectName = "PROJECTNAME", modelName = "ROLE.MODELNAME", methodName = "ROLE.METHODNAME.ROLEADD")
	@PostMapping(value = "/addRole", produces = { "application/json;charset=utf-8"})
	public  @KerPermission("10008") @KerValid Map<String, Object> addRole(@RequestBody Role role) {
		Map<String, Object> map = roleService.addRole(role);
		return map;
	}

	/**
	 * 角色详情
	 * 
	 * @return
	 */
	@ApiOperation(value = "角色详情",httpMethod="POST", notes= "角色详情", produces = "application/json;charset=utf-8",response=RoleVO.class)
	@KerLog(projectName = "PROJECTNAME", modelName = "ROLE.MODELNAME", methodName = "ROLE.METHODNAME.ROLEFINE")
	@GetMapping(value = "/findRole/{id}", produces = { "application/json;charset=utf-8"})
	public  /*@KerPermission("10007")*/ Map<String, Object> findRole(@ApiParam(value="角色id") @PathVariable("id")String id) {
		Map<String, Object> map = roleService.findRole(id);
		return map;
	}

	/**
	 * 修改角色
	 * 
	 * @return
	 */
	@ApiOperation(value = "修改角色",httpMethod="POST", notes= "修改角色", produces = "application/json;charset=utf-8",response=Map.class)
	@KerLog(projectName = "PROJECTNAME", modelName = "ROLE.MODELNAME", methodName = "ROLE.METHODNAME.ROLEMODIFY")
	@PostMapping(value = "/modifyRole", produces = { "application/json;charset=utf-8"})
	public  @KerPermission("10009") Map<String, Object> modifyRole(@RequestBody Role role) {
		Map<String, Object> map = roleService.updateRole(role);
		return map;
	}

	/**
	 * 删除角色
	 * 
	 * @return
	 */
	@ApiOperation(value = "删除角色",httpMethod="POST", notes= "删除角色", produces = "application/json;charset=utf-8",response=Map.class)
	@KerLog(projectName = "PROJECTNAME", modelName = "ROLE.MODELNAME", methodName = "ROLE.METHODNAME.ROLESTATUSMODIFY")
	@PostMapping(value = "/deleteRole", produces = { "application/json;charset=utf-8"})
	public  @KerPermission("10010") Map<String, Object> modifyRoleStatus(@ApiParam(value="角色id集合") @RequestBody String[] ids) {
		Map<String, Object> map = roleService.deltetRole(ids);
		return map;
	} 
	
	/**
	 * 分配菜单
	 * 
	 * @return
	 */
	@ApiOperation(value = "分配菜单",httpMethod="POST", notes= "分配菜单", produces = "application/json;charset=utf-8",response=Map.class)
	@KerLog(projectName = "PROJECTNAME", modelName = "ROLE.MODELNAME", methodName = "ROLE.METHODNAME.ROLESTATUSMODIFY")
	@PostMapping(value = "/updateRolePermission/{rId}", produces = { "application/json;charset=utf-8"})
	public  @KerPermission("10011") Map<String, Object> updateRolePermission(@ApiParam(value="菜单id集合") @RequestBody String[] pIds,@ApiParam(value="角色id") @PathVariable("rId")String rId) {
		Map<String, Object> map = roleService.updateRolePermission(pIds,rId);
		return map;
	} 
	
	/**
	 * 角色集合接口
	 * 
	 * @return
	 */ 
	@ApiOperation(value = "角色集合接口",httpMethod="GET", notes= "角色集合接口", produces = "application/json;charset=utf-8",response=RoleUserVO.class)
	@GetMapping(value = "/findRoles/{uid}", produces = { "application/json;charset=utf-8"})
	public    Map<String, Object> findRoles(@ApiParam(value="用户id") @PathVariable("uid")String uid,HttpServletRequest request) {
		String token = TokenMgr.getToken(request, ConstantUtil.TOKEN);
		Map<String, Object> map = roleService.findRoles(uid,token);
		return map;
	} 
}
