/**
   * 菜单树形结构
   *
   * @param response
   * @return
   */
  @RequestMapping(value = "/listMenus", produces = "application/json", method = RequestMethod.POST)
  public @ResponseBody ResponseEntity<String> listMenus(
      HttpServletResponse response, HttpServletRequest request) {
    JSONObject result = new JSONObject();
    String msg = "";
    JSONObject content = new JSONObject();
    String path = request.getContextPath();
    try {
      List<SysMenu> menus = sysMenuService.listAll(null);
      if (menus != null) {
        String operation =
            "<td><a href='"
                + path
                + "/menu/%s/toMenuDetail'>详情</a><i style=\"width: 20px;display: inline-block;\"></i><a href='"
                + path
                + "/menu/%s/toMenuEdit'>编辑</a></td>";
        String detail =
            "<td><i style=\"width: 100px;display: inline-block;\"></i>排序 : %s, ID : %s</td>";
        String item =
            "<table style='display: inline-block;'><tr><td style='width: 215px;'><i class=\"icon-file-text grey\"></i> %s</td>"
                + operation
                + detail
                + "</tr></table>";
        // String folder = " %s<i style=\"width: 100px;display: inline-block;\"></i>" + operation +
        // detail;
        String folder =
            "<table style='display: inline-block;'><tr><td style='width: 200px;'> %s</td>"
                + operation
                + detail
                + "</tr></table>";
        String subitem =
            "<table style='display: inline-block;'><tr><td style='width: 245px;'><i class=\"icon-file-text blue\"></i> %s</td>"
                + operation
                + detail
                + "</tr></table>";
        int size = menus.size();
        Map<String, Object> params = new HashMap<String, Object>();
        for (int i = 0; i < size; i++) {
          SysMenu menu_1 = menus.get(i);
          String level_1 = menu_1.getLevel();
          String id_1 = menu_1.getId();
          if ("1".equals(level_1)) {
            String url = menu_1.getUrl();
            String name = menu_1.getName();
            List<Map<String, Object>> childrenList = new ArrayList<Map<String, Object>>();
            for (int j = 0; j < size; j++) {
              SysMenu menu_2 = menus.get(j);
              String level_2 = menu_2.getLevel();
              if ("2".equals(level_2) && id_1.equals(menu_2.getParent_fk())) {
                Map<String, Object> child = new HashMap<String, Object>();
                child.put(
                    "name",
                    String.format(
                        subitem,
                        menu_2.getName(),
                        menu_2.getId(),
                        menu_2.getId(),
                        menu_2.getSorter(),
                        menu_2.getActiveId()));
                child.put("type", "item");
                childrenList.add(child);
              } else {
                continue;
              }
            }

            if (url != null && !"".equals(url)) {
              params.put(
                  "name",
                  String.format(item, name, id_1, id_1, menu_1.getSorter(), menu_1.getActiveId()));
              params.put("type", "item");
            } else {
              params.put(
                  "name",
                  String.format(
                      folder, name, id_1, id_1, menu_1.getSorter(), menu_1.getActiveId()));
              params.put("type", "folder");
              params.put("icon-class", "orange");
              Map<String, Object> children = new HashMap<String, Object>();
              children.put("children", childrenList);
              params.put("additionalParameters", children);
            }
            content.put(String.valueOf(i), params);
            params.clear();
          } else {
            continue;
          }
        }
      }
      msg = ResultEntity.KW_STATUS_SUCCESS;
    } catch (ServiceException e) {
      msg = ResultEntity.KW_STATUS_FAIL;
      logger.error(e);
    }
    result.put("msg", msg);
    result.put("content", content);
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);
    response.setCharacterEncoding("UTF-8");
    return new ResponseEntity<String>(result.toString(), headers, HttpStatus.OK);
  }
  /**
   * 左侧菜单展示
   *
   * @param response
   * @param request
   * @return
   */
  @RequestMapping(value = "/showMenus", produces = "application/json", method = RequestMethod.POST)
  public @ResponseBody ResponseEntity<String> showMenu(
      HttpServletResponse response, HttpServletRequest request) {
    JSONObject result = new JSONObject();
    String msg = "";
    String path = request.getContextPath();
    StringBuffer buffer = new StringBuffer();
    String content = "";
    try {
      Subject subject = SecurityUtils.getSubject();
      Map<String, Object> params = new HashMap<String, Object>();
      params.put("valid", 1);
      List<SysMenu> menuList = sysMenuService.listAll(params);
      String item =
          "<li id=\"%s\" class='menu-item'><a href='"
              + path
              + "%s' ><i class=\"%s\"></i><span class=\"menu-text\"> %s </span></a></li>";
      String folder =
          "<li id=\"%s\"><a href=\"#\" class=\"dropdown-toggle\"><i class=\"%s\"></i><span class=\"menu-text\"> %s</span><b class=\"arrow icon-angle-down\"></b></a>";
      String subitem =
          "<li id=\"%s\"><a href='"
              + path
              + "%s'><i class=\"icon-double-angle-right\"></i> %s</a></li>";
      if (menuList != null && menuList.size() > 0) {
        int size = menuList.size();
        for (int i = 0; i < size; i++) {
          SysMenu menu_1 = menuList.get(i);
          String level_1 = menu_1.getLevel();
          String id_1 = menu_1.getId();
          String permission_fk = menu_1.getPermission_fk();
          if (!checkMenuPermission(subject, permission_fk)) {
            continue;
          }
          if ("1".equals(level_1)) {
            String url = menu_1.getUrl();
            StringBuffer childrens = new StringBuffer();
            for (int j = 0; j < size; j++) {
              SysMenu menu_2 = menuList.get(j);
              String level_2 = menu_2.getLevel();
              if ("2".equals(level_2) && id_1.equals(menu_2.getParent_fk())) {
                String permission_fk_2 = menu_2.getPermission_fk();
                if (!checkMenuPermission(subject, permission_fk_2)) {
                  continue;
                }
                childrens.append(
                    String.format(
                        subitem, menu_2.getActiveId(), menu_2.getUrl(), menu_2.getName()));
              }
            }

            if (url != null && !"".equals(url)) {
              buffer.append(
                  String.format(
                      item,
                      menu_1.getActiveId(),
                      menu_1.getUrl(),
                      menu_1.getStyle(),
                      menu_1.getName()));
            } else {
              buffer
                  .append(
                      String.format(
                          folder, menu_1.getActiveId(), menu_1.getStyle(), menu_1.getName()))
                  .append("<ul class=\"submenu\">")
                  .append(childrens.toString())
                  .append("</ul></li>");
            }
          }
        }
      }
      User loginUser = (User) request.getSession().getAttribute(Constants.CURRENT_USER);
      String menuManager =
          "<li id=\"sys_menu\" class='menu-item'><a href='"
              + path
              + "/menu/toListMenus' ><i class=\"icon-cog\"></i><span class=\"menu-text\"> 菜单管理 </span></a></li>";
      if ("admin".equals(loginUser.getUsername())) {
        buffer.append(menuManager);
      }
      content = buffer.toString();
      msg = ResultEntity.KW_STATUS_SUCCESS;
    } catch (ServiceException e) {
      msg = ResultEntity.KW_STATUS_FAIL;
      logger.error(e);
    }
    result.put("msg", msg);
    result.put("content", content);
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);
    response.setCharacterEncoding("UTF-8");
    return new ResponseEntity<String>(result.toString(), headers, HttpStatus.OK);
  }