package com.bonus.gs.sub.evaluate.manager.controller;

import java.util.List;

import com.bonus.gs.sub.evaluate.evaluate.beans.TeamEvaluateBean;
import com.bonus.gs.sub.evaluate.manager.dao.RoleDao;
import com.bonus.gs.sub.evaluate.manager.entity.RoleDto;
import com.bonus.gs.sub.evaluate.manager.model.Role;
import com.bonus.gs.sub.evaluate.manager.table.PageTableHandler;
import com.bonus.gs.sub.evaluate.manager.table.PageTableRequest;
import com.bonus.gs.sub.evaluate.manager.table.PageTableResponse;
import com.bonus.gs.sub.evaluate.manager.service.RoleService;
import com.bonus.gs.sub.evaluate.manager.annotation.LogAnnotation;
import com.bonus.gs.sub.evaluate.manager.utils.AjaxRes;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import com.google.common.collect.Maps;

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

/**
 * 角色相关接口
 * 
 * @author 小威老师 xiaoweijiagou@163.com
 *
 */
@Api(tags = "角色")
@RestController
@RequestMapping("/roles")
public class RoleController {

	@Autowired
	private RoleService roleService;
	@Autowired
	private RoleDao roleDao;

	@LogAnnotation
	@PostMapping
	@ApiOperation(value = "保存角色")
	@PreAuthorize("hasAuthority('sys:role:add')")
	public void saveRole(@RequestBody RoleDto roleDto) {
		roleService.saveRole(roleDto);
	}

	@GetMapping
	@ApiOperation(value = "角色列表")
	@PreAuthorize("hasAuthority('sys:role:query')")
	public PageTableResponse listRoles(PageTableRequest request) {
		return new PageTableHandler(new PageTableHandler.CountHandler() {

			@Override
			public int count(PageTableRequest request) {
				return roleDao.count(request.getParams());
			}
		}, new PageTableHandler.ListHandler() {

			@Override
			public List<Role> list(PageTableRequest request) {
				List<Role> list = roleDao.list(request.getParams(), request.getOffset(), request.getLimit());
				return list;
			}
		}).handle(request);
	}

	@GetMapping("getRoleList")
	@ResponseBody
	public AjaxRes getRoleList(Role o){
		AjaxRes ar = new AjaxRes();
		List<Role> list = roleDao.getRoleList(o);
		ar.setListSucceed(list);
		return ar;
	}

	@GetMapping("/{id}")
	@ApiOperation(value = "根据id获取角色")
	@PreAuthorize("hasAuthority('sys:role:query')")
	public Role get(@PathVariable Long id) {
		return roleDao.getById(id);
	}

	@GetMapping("/all")
	@ApiOperation(value = "所有角色")
	@PreAuthorize("hasAnyAuthority('sys:user:query','sys:role:query')")
	public List<Role> roles() {
		return roleDao.list(Maps.newHashMap(), null, null);
	}

	@GetMapping(params = "userId")
	@ApiOperation(value = "根据用户id获取拥有的角色")
	@PreAuthorize("hasAnyAuthority('sys:user:query','sys:role:query')")
	public List<Role> roles(Long userId) {
		return roleDao.listByUserId(userId);
	}

	@LogAnnotation
	@DeleteMapping("/{id}")
	@ApiOperation(value = "删除角色")
	@PreAuthorize("hasAuthority('sys:role:del')")
	public void delete(@PathVariable Long id) {
		roleService.deleteRole(id);
	}


	@PostMapping("/getRoleSelect")
	@ApiOperation(value = "获取角色下拉选")
	public List<Role> getRoleSelect(@RequestBody String id) {
		return roleDao.getRoleSelect(id.length() > 3 ? id.split("=")[1] : "");
	}
}
