package com.youtoshow.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.management.relation.RoleList;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import com.youtoshow.po.Role;
import com.youtoshow.service.IRoleService;

@Controller
@RequestMapping("/role")//访问浏览器地址
public class RoleController {
	@Resource
	IRoleService roleService;
	@RequestMapping("/query")//访问浏览器地址
	public String selectSelective(Role role,Map<String, Object> roleMap){
		//System.out.println("!!!!!");
		//role.setRolename("超级管理员");
		List<Role> RoleList = roleService.selectSelective(role);
		
		roleMap.put("roleList", RoleList);
		
		return "/back/roleHome";		
	}
//	@RequestMapping("/delete")
//	public void delete(Role role,HttpServletResponse response) throws IOException{
//		roleService.deleteByPrimaryKey(role.getRoleid());
//		PrintWriter out=response.getWriter();
//		out.append("delete");
//		out.flush();
//		out.close();
//	}
//	@RequestMapping("/update")
//	public void update(Role role,HttpServletResponse response) throws IOException{
//		Role r=new Role();
//		r.setRoleid(4l);
//		r.setRolename("李四");
//		
//	//System.out.println(r);
//		roleService.updateByPrimaryKeySelective(r);
//		PrintWriter out=response.getWriter();
//		out.append("update");
//		out.flush();
//		out.close();
//		
//	}
//	@RequestMapping("/insert")
//	public void insert(Role role,HttpServletResponse response) throws IOException{
//		Role r=new Role();
//		r.setRoleid(4l);
//		r.setRolename("张三");
//		roleService.insert(r);
//		PrintWriter out=response.getWriter();
//		out.append("insert");
//		out.flush();
//		out.close();
//	}
	@RequestMapping("/insert")
	public String insertSelective(Role role){
		 roleService.insertSelective(role);
		return "redirect:/role/query";
	}
	//更新前查询出要更新对象
	@RequestMapping("/beforeUpdate")
	public String beforeUpdate(Long roleid,Map<String, Object> roMap){
		Role role=roleService.selectByPrimaryKey(roleid);
		return "back/roleUpdate";
	
	}
	/**
	 * 更新角色
	 * @param role用户与更新的对象
	 * @return 重定向到查询方法
	 */
	@RequestMapping("/update")
	public String update(Role role){
		roleService.selectSelective(role);
		return "redirect:/role/query";
	}
	@RequestMapping("/delete")
	public String delete(Long roleid){
		roleService.deleteByPrimaryKey(roleid);
		return "redirect:/role/query";
	}
}
