package com.huiju.estateagents.property.controller;

import com.huiju.estateagents.base.BaseController;
import com.huiju.estateagents.base.ResponseBean;
import com.huiju.estateagents.center.taUser.entity.TaUser;
import com.huiju.estateagents.property.common.UserElement;
import com.huiju.estateagents.property.model.SysRole;
import com.huiju.estateagents.property.service.RoleServiceI;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @author FXF
 * @date 2019-01-16
 */
@RestController

@RequestMapping("/")
@Api(value = "物业端角色API", description = "物业端角色API")
public class RoleController extends BaseController {
	
	@Autowired
	private RoleServiceI roleService;
	
	@ApiOperation(value = "根据搜索条件获取角色列表", notes = "根据搜索条件获取角色列表")
	@ApiImplicitParams({
			@ApiImplicitParam(paramType = "query", dataType = "String", name = "roleName", value = "帖子标题（模糊查询）"),
			@ApiImplicitParam(paramType = "query", dataType = "integer", name = "pageNum", value = "分页第几页"),
			@ApiImplicitParam(paramType = "query", dataType = "integer", name = "pageSize", value = "分页每页长度"),
			@ApiImplicitParam(paramType = "header", dataTypeClass = String.class, name = "X-Auth-Token", value = "Token"),
	})
	@RequestMapping(value = "/roles",method = RequestMethod.GET)
	public ResponseBean getRoleList(@RequestParam(value = "roleName", required = false) String roleName,
	                            @RequestParam(value ="pageNum",defaultValue = "1") Integer pageNum,
	                            @RequestParam(value ="pageSize",defaultValue = "10") Integer pageSize,
	                            HttpServletRequest request) {
		ResponseBean responseBean = new ResponseBean();
		TaUser userElement = getTaUser(request);
		responseBean = roleService.getRoleList(roleName,userElement,pageNum,pageSize);
		return responseBean;
	}
	
	@ApiOperation(value = "添加角色", notes = "添加角色")
	@ApiImplicitParams({
			@ApiImplicitParam(paramType = "header", dataTypeClass = String.class, name = "X-Auth-Token", value = "Token"),
			@ApiImplicitParam(paramType = "header", dataTypeClass = String.class, name = "Login-Type", value = "值为 web"),
			@ApiImplicitParam(paramType = "body", dataTypeClass = String.class, name = "sysRole", value = "roleName角色名称;description角色描述;menuArray菜单id集合")
	})
	@RequestMapping(value = "/role/add", method = RequestMethod.POST)
	public ResponseBean addRole(@RequestBody SysRole sysRole, HttpServletRequest request) {
		ResponseBean responseBean = new ResponseBean();
		TaUser userElement = getTaUser(request);
		responseBean = roleService.addRole(sysRole, userElement);
		return responseBean;
	}
	
	@ApiOperation(value = "获取角色详情", notes = "获取角色详情")
	@ApiImplicitParams({
			@ApiImplicitParam(paramType = "path", dataTypeClass = Integer.class, name = "id", value = "角色id"),
			@ApiImplicitParam(paramType = "header", dataTypeClass = String.class, name = "X-Auth-Token", value = "Token"),
			@ApiImplicitParam(paramType = "header", dataTypeClass = String.class, name = "Login-Type", value = "值为 web"),
			@ApiImplicitParam(paramType = "body", dataTypeClass = String.class, name = "sysRole", value = "roleName角色名称;description角色描述;menuArray菜单id集合")
	})
	@RequestMapping(value = "/role/{id}",method = RequestMethod.GET)
	public ResponseBean getRoleInfo(@PathVariable(value = "id", required = false) Integer id,
	                            HttpServletRequest request) {
		ResponseBean responseBean = new ResponseBean();
		TaUser userElement = getTaUser(request);
		responseBean = roleService.getRoleInfo(id,userElement);
		return responseBean;
	}
	
	@ApiOperation(value = "修改角色", notes = "修改角色")
	@ApiImplicitParams({
			@ApiImplicitParam(paramType = "path", dataTypeClass = Integer.class, name = "id", value = "角色id"),
			@ApiImplicitParam(paramType = "header", dataTypeClass = String.class, name = "X-Auth-Token", value = "Token"),
			@ApiImplicitParam(paramType = "header", dataTypeClass = String.class, name = "Login-Type", value = "值为 web"),
			@ApiImplicitParam(paramType = "body", dataTypeClass = String.class, name = "sysRole", value = "roleName角色名称;description角色描述;menuArray菜单id集合")
	})
	@RequestMapping(value = "/role/update/{id}", method = RequestMethod.PUT)
	public ResponseBean updateRole(@PathVariable Integer id, @RequestBody SysRole sysRole, HttpServletRequest request) {
		ResponseBean responseBean = new ResponseBean();
		TaUser userElement = getTaUser(request);
		sysRole.setId(id);
		responseBean = roleService.updateRole(sysRole, userElement);
		return responseBean;
	}
	
	@ApiOperation(value = "删除角色", notes = "删除角色")
	@ApiImplicitParams({
			@ApiImplicitParam(paramType = "header", dataTypeClass = String.class, name = "X-Auth-Token", value = "Token"),
			@ApiImplicitParam(paramType = "header", dataTypeClass = String.class, name = "Login-Type", value = "值为 web"),
			@ApiImplicitParam(paramType = "body", dataTypeClass = String.class, name = "sysRole", value = "idArray角色id集合")
	})
	@RequestMapping(value = "/role/delete", method = RequestMethod.POST)
	public ResponseBean deleteRole(@RequestBody SysRole sysRole, HttpServletRequest request) {
		ResponseBean responseBean = new ResponseBean();
		TaUser userElement = getTaUser(request);
		responseBean = roleService.deleteRole(sysRole, userElement);
		return responseBean;
	}
}
