package com.ischoolbar.programmer.controller.admin; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.commons.lang.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; 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.ResponseBody; import org.springframework.web.servlet.ModelAndView; import com.ischoolbar.programmer.entity.admin.Authority; import com.ischoolbar.programmer.entity.admin.Menu; import com.ischoolbar.programmer.entity.admin.Role; import com.ischoolbar.programmer.page.admin.Page; import com.ischoolbar.programmer.service.admin.AuthorityService; import com.ischoolbar.programmer.service.admin.MenuService; import com.ischoolbar.programmer.service.admin.RoleService; @RequestMapping("/admin/role") @Controller public class RoleController { @Autowired private RoleService roleService; @Autowired private AuthorityService authorityService; @Autowired private MenuService menuService; @RequestMapping(value="/list",method=RequestMethod.GET) public ModelAndView list(ModelAndView model){ model.setViewName("/role/list"); return model; } @RequestMapping(value="/list",method=RequestMethod.POST) @ResponseBody public Map getList(Page page, @RequestParam(name="name",required=false,defaultValue="") String name ){ Map ret = new HashMap(); Map queryMap = new HashMap(); queryMap.put("name", name); queryMap.put("offset", page.getOffset()); queryMap.put("pageSize", page.getRows()); ret.put("rows", roleService.findList(queryMap)); ret.put("total", roleService.getTotal(queryMap)); return ret; } @RequestMapping(value="/add",method=RequestMethod.POST) @ResponseBody public Map add(Role role){ Map ret = new HashMap(); if(role == null){ ret.put("type", "error"); ret.put("msg", "错误"); return ret; } if(StringUtils.isEmpty(role.getName())){ ret.put("type", "error"); ret.put("msg", "错误"); return ret; } if(roleService.add(role) <= 0){ ret.put("type", "error"); ret.put("msg", "错误"); return ret; } ret.put("type", "success"); ret.put("msg", "成功"); return ret; } @RequestMapping(value="/edit",method=RequestMethod.POST) @ResponseBody public Map edit(Role role){ Map ret = new HashMap(); if(role == null){ ret.put("type", "error"); ret.put("msg", "错误"); return ret; } if(StringUtils.isEmpty(role.getName())){ ret.put("type", "error"); ret.put("msg", "错误"); return ret; } if(roleService.edit(role) <= 0){ ret.put("type", "error"); ret.put("msg", "错误"); return ret; } ret.put("type", "success"); ret.put("msg", "成功"); return ret; } @RequestMapping(value="/delete",method=RequestMethod.POST) @ResponseBody public Map delete(Long id){ Map ret = new HashMap(); if(id == null){ ret.put("type", "error"); ret.put("msg", "错误"); return ret; } try { if(roleService.delete(id) <= 0){ ret.put("type", "error"); ret.put("msg", "错误"); return ret; } } catch (Exception e) { // TODO: handle exception ret.put("type", "error"); ret.put("msg", "错误"); return ret; } ret.put("type", "success"); ret.put("msg", "成功"); return ret; } @RequestMapping(value="/get_all_menu",method=RequestMethod.POST) @ResponseBody public List getAllMenu(){ Map queryMap = new HashMap(); queryMap.put("offset", 0); queryMap.put("pageSize", 99999); return menuService.findList(queryMap); } @RequestMapping(value="/add_authority",method=RequestMethod.POST) @ResponseBody public Map addAuthority( @RequestParam(name="ids",required=true) String ids, @RequestParam(name="roleId",required=true) Long roleId ){ Map ret = new HashMap(); if(StringUtils.isEmpty(ids)){ ret.put("type", "error"); ret.put("msg", "错误"); return ret; } if(roleId == null){ ret.put("type", "error"); ret.put("msg", "错误"); return ret; } if(ids.contains(",")){ ids = ids.substring(0,ids.length()-1); } String[] idArr = ids.split(","); if(idArr.length > 0){ authorityService.deleteByRoleId(roleId); } for(String id:idArr){ Authority authority = new Authority(); authority.setMenuId(Long.valueOf(id)); authority.setRoleId(roleId); authorityService.add(authority); } ret.put("type", "success"); ret.put("msg", "成功"); return ret; } @RequestMapping(value="/get_role_authority",method=RequestMethod.POST) @ResponseBody public List getRoleAuthority( @RequestParam(name="roleId",required=true) Long roleId ){ return authorityService.findListByRoleId(roleId); } }