package com.ischoolbar.programmer.controller.admin; import java.io.File; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.servlet.http.HttpServletRequest; 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.Menu; import com.ischoolbar.programmer.page.admin.Page; import com.ischoolbar.programmer.service.admin.MenuService; @RequestMapping("/admin/menu") @Controller public class MenuController { @Autowired private MenuService menuService; @RequestMapping(value="/list",method=RequestMethod.GET) public ModelAndView list(ModelAndView model){ model.addObject("topList", menuService.findTopList()); model.setViewName("menu/list"); return model; } @RequestMapping(value="/list",method=RequestMethod.POST) @ResponseBody public Map getMenuList(Page page, @RequestParam(name="name",required=false,defaultValue="") String name ){ Map ret = new HashMap(); Map queryMap = new HashMap(); queryMap.put("offset", page.getOffset()); queryMap.put("pageSize", page.getRows()); queryMap.put("name", name); List findList = menuService.findList(queryMap); ret.put("rows", findList); ret.put("total", menuService.getTotal(queryMap)); return ret; } @RequestMapping(value="/get_icons",method=RequestMethod.POST) @ResponseBody public Map getIconList(HttpServletRequest request){ Map ret = new HashMap(); String realPath = request.getServletContext().getRealPath("/"); File file = new File(realPath + "\\resources\\admin\\easyui\\css\\icons"); List icons = new ArrayList(); if(!file.exists()){ ret.put("type", "error"); ret.put("msg", "错误"); return ret; } File[] listFiles = file.listFiles(); for(File f:listFiles){ if(f!= null && f.getName().contains("png")){ icons.add("icon-" + f.getName().substring(0, f.getName().indexOf(".")).replace("_", "-")); } } ret.put("type", "success"); ret.put("content", icons); return ret; } @RequestMapping(value="/add",method=RequestMethod.POST) @ResponseBody public Map add(Menu menu){ Map ret = new HashMap(); if(menu == null){ ret.put("type", "error"); ret.put("msg", "错误!"); return ret; } if(StringUtils.isEmpty(menu.getName())){ ret.put("type", "error"); ret.put("msg", "错误!"); return ret; } if(StringUtils.isEmpty(menu.getIcon())){ ret.put("type", "error"); ret.put("msg", "错误!"); return ret; } if(menu.getParentId() == null){ menu.setParentId(0l); } if(menuService.add(menu) <= 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(Menu menu){ Map ret = new HashMap(); if(menu == null){ ret.put("type", "error"); ret.put("msg", "错误!"); return ret; } if(StringUtils.isEmpty(menu.getName())){ ret.put("type", "error"); ret.put("msg", "错误!"); return ret; } if(StringUtils.isEmpty(menu.getIcon())){ ret.put("type", "error"); ret.put("msg", "错误!"); return ret; } if(menu.getParentId() == null){ menu.setParentId(0l); } if(menuService.edit(menu) <= 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( @RequestParam(name="id",required=true) Long id ){ Map ret = new HashMap(); if(id == null){ ret.put("type", "error"); ret.put("msg", "错误!"); return ret; } List findChildernList = menuService.findChildernList(id); if(findChildernList != null && findChildernList.size() > 0){ ret.put("type", "error"); ret.put("msg", "错误!"); return ret; } if(menuService.delete(id) <= 0){ ret.put("type", "error"); ret.put("msg", "错误!"); return ret; } ret.put("type", "success"); ret.put("msg", "成功!"); return ret; } }