package com.community.huiju.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.community.commom.mode.ResponseBean;
import com.community.commom.session.UserElement;
import com.community.huiju.common.base.BaseController;
import com.community.huiju.model.SysMenu;
import com.community.huiju.model.SysRole;
import com.community.huiju.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.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

/**
 * @author FXF
 * @date 2019-01-16
 */
@RestController
@RefreshScope
@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,
	                            HttpSession session) {
		ResponseBean responseBean = new ResponseBean();
		UserElement userElement = getUserElement(session);
		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, HttpSession session) {
		ResponseBean responseBean = new ResponseBean();
		UserElement userElement = getUserElement(session);
		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,
	                            HttpSession session) {
		ResponseBean responseBean = new ResponseBean();
		UserElement userElement = getUserElement(session);
		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, HttpSession session) {
		ResponseBean responseBean = new ResponseBean();
		UserElement userElement = getUserElement(session);
		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, HttpSession session) {
		ResponseBean responseBean = new ResponseBean();
		UserElement userElement = getUserElement(session);
		responseBean = roleService.deleteRole(sysRole, userElement);
		return responseBean;
	}
}
