package com.bonus.hn.bearing.capacity.backstage.manager.controller;

import java.util.List;

import com.bonus.hn.bearing.capacity.backstage.manager.dao.RoleDao;
import com.bonus.hn.bearing.capacity.backstage.manager.entity.PublicBean;
import com.bonus.hn.bearing.capacity.backstage.manager.entity.RoleDto;
import com.bonus.hn.bearing.capacity.backstage.manager.model.Role;
import com.bonus.hn.bearing.capacity.backstage.manager.model.SysUser;
import com.bonus.hn.bearing.capacity.backstage.manager.table.PageTableHandler;
import com.bonus.hn.bearing.capacity.backstage.manager.table.PageTableRequest;
import com.bonus.hn.bearing.capacity.backstage.manager.table.PageTableResponse;
import com.bonus.hn.bearing.capacity.backstage.manager.service.RoleService;
import com.bonus.hn.bearing.capacity.backstage.manager.utils.AjaxRes;
import com.bonus.hn.bearing.capacity.backstage.manager.utils.GlobalConst;
import com.bonus.hn.bearing.capacity.backstage.manager.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
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.bonus.hn.bearing.capacity.backstage.manager.annotation.LogAnnotation;
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("/{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("getMsgContent")
	@ApiOperation(value = "角色管理-列表")
	public R getList(PublicBean o){
		List<PublicBean> list = roleService.getList(o);
		return list.size() > 0 ? R.okTable(list, list.size()):R.failTable("暂无数据");
	}

	@LogAnnotation
	@PostMapping("delById")
	@ApiOperation(value = "用户管理-删除")
	public AjaxRes delById(PublicBean o) throws Exception {
		AjaxRes ar = new AjaxRes();
		//删除之前先查询出来
//		Long userId = bean.getId();
//		SysUser userDto = userDao.getEyId(userId);
		int result  = roleService.delById(o);
		if(result==1){
			ar.setSucceedMsg(GlobalConst.DATA_SUCCEED);
		}else {
			ar.setFailMsg(GlobalConst.DATA_FAIL);
		}

		return ar;
	}


}
