@RequestMapping(value = "/role/edit")
  public String roleEdit(Long id, String __VIEWSTATE, ModelMap map, HttpServletRequest req) {
    String username = (String) req.getSession().getAttribute("manager");
    if (null == username) {
      return "redirect:/Verwalter/login";
    }

    map.addAttribute("__VIEWSTATE", __VIEWSTATE);

    if (null != id) {
      map.addAttribute("tdRole", tdManagerRoleService.findOne(id));
    }

    // 根菜单列表
    List<TdNavigationMenu> rootMenuList = tdNavigationMenuService.findByParentIdAndSort(0L);

    if (null != rootMenuList && rootMenuList.size() > 0) {
      for (int i = 0; i < rootMenuList.size(); i++) {
        TdNavigationMenu rootMenu = rootMenuList.get(i);

        // 取一级菜单列表
        List<TdNavigationMenu> level0MenuList =
            tdNavigationMenuService.findByParentIdAndSort(rootMenu.getId());

        if (null != level0MenuList && level0MenuList.size() > 0) {
          map.addAttribute("level_" + i + "_menu_list", level0MenuList);

          for (int j = 0; j < level0MenuList.size(); j++) {
            TdNavigationMenu level0Menu = level0MenuList.get(j);

            // 取二级菜单列表
            List<TdNavigationMenu> level1MenuList =
                tdNavigationMenuService.findByParentIdAndSort(level0Menu.getId());

            if (null != level1MenuList && level1MenuList.size() > 0) {
              map.addAttribute("level_" + i + j + "_menu_list", level1MenuList);
            }
          }
        }
      }
    }

    map.addAttribute("root_menu_list", rootMenuList);

    return "/site_mag/manager_role_edit";
  }
  @RequestMapping(value = "/role/save")
  public String roleSave(
      TdManagerRole tdManagerRole,
      TdManagerPermissionList tdManagerPermissionList,
      String __VIEWSTATE,
      ModelMap map,
      HttpServletRequest req) {
    String username = (String) req.getSession().getAttribute("manager");
    if (null == username) {
      return "redirect:/Verwalter/login";
    }

    map.addAttribute("__VIEWSTATE", __VIEWSTATE);

    //        for(int i = 0; i < tdManagerPermissionList.getPermissionlist().size(); i++){
    //        	System.out.println(tdManagerPermissionList.getPermissionlist().get(i).getIsView()+"
    // "+i);
    //        }
    //
    if (null != tdManagerRole.getPermissionList()) {
      for (int i = 0; i < tdManagerRole.getPermissionList().size(); i++) {
        if (null == tdManagerRole.getPermissionList().get(i).getIsView()) {
          tdManagerRole.getPermissionList().get(i).setIsView(false);
        }
      }
    }
    if (null == tdManagerRole.getPermissionList()
        && null != tdManagerPermissionList
        && null != tdManagerPermissionList.getPermissionlist()) {
      tdManagerRole.setPermissionList(tdManagerPermissionList.getPermissionlist());
    } else {

      if (null != tdManagerPermissionList && null != tdManagerPermissionList.getPermissionlist()) {

        if (tdManagerPermissionList.getPermissionlist().size()
            < tdManagerRole.getPermissionList().size()) {
          for (int i = 0; i < tdManagerPermissionList.getPermissionlist().size(); i++) {
            if (null != tdManagerPermissionList.getPermissionlist().get(i).getIsView()
                && tdManagerPermissionList.getPermissionlist().get(i).getIsView()) {
              tdManagerRole.getPermissionList().get(i).setIsView(true);
            } else {
              tdManagerRole.getPermissionList().get(i).setIsView(false);
            }
          }
          for (int i = tdManagerPermissionList.getPermissionlist().size();
              i < tdManagerRole.getPermissionList().size();
              i++) {
            tdManagerRole.getPermissionList().get(i).setIsView(false);
          }
        } else {
          for (int i = 0; i < tdManagerPermissionList.getPermissionlist().size(); i++) {
            if (i >= tdManagerRole.getPermissionList().size()) {
              TdManagerPermission tdManagerPermission = new TdManagerPermission();
              if (null != tdManagerPermissionList.getPermissionlist().get(i).getIsView()
                  && tdManagerPermissionList.getPermissionlist().get(i).getIsView()) {
                tdManagerPermission.setIsView(true);
              } else {
                tdManagerPermission.setIsView(false);
              }

              tdManagerRole.getPermissionList().add(tdManagerPermission);
            } else {
              if (null != tdManagerPermissionList.getPermissionlist().get(i).getIsView()
                  && tdManagerPermissionList.getPermissionlist().get(i).getIsView()) {
                tdManagerRole.getPermissionList().get(i).setIsView(true);
              } else {
                tdManagerRole.getPermissionList().get(i).setIsView(false);
              }
            }
          }
        }
      } else {
        if (null != tdManagerRole.getPermissionList()) {
          for (int i = 0; i < tdManagerRole.getPermissionList().size(); i++) {
            tdManagerRole.getPermissionList().get(i).setIsView(false);
          }
        }
      }
    }
    if (null == tdManagerRole.getPermissionList()
        && null == tdManagerPermissionList.getPermissionlist()) {
      List<TdManagerPermission> tdManagerPermissions = new ArrayList<>();
      tdManagerRole.setPermissionList(tdManagerPermissions);
      int totalsize = tdNavigationMenuService.findAll().size();
      for (int i = 0; i < totalsize; i++) {
        TdManagerPermission tdManagerPermission = new TdManagerPermission();
        tdManagerPermission.setIsView(false);
        tdManagerRole.getPermissionList().add(tdManagerPermission);
      }
    }
    // 将为空的权限设为false
    int totalsize = tdNavigationMenuService.findAll().size();
    for (int i = 0; i < totalsize; i++) {
      if (i < tdManagerRole.getPermissionList().size()) {
        if (null == tdManagerRole.getPermissionList().get(i).getIsView()) {
          tdManagerRole.getPermissionList().get(i).setIsView(false);
        }
      } else {
        TdManagerPermission tdManagerPermission = new TdManagerPermission();
        tdManagerPermission.setIsView(false);
        tdManagerRole.getPermissionList().add(tdManagerPermission);
      }
    }

    tdManagerRoleService.save(tdManagerRole);

    if (null == tdManagerRole.getId()) {
      tdManagerLogService.addLog("add", "添加管理角色权限", req);
    } else {
      tdManagerLogService.addLog("edit", "修改管理角色权限", req);
    }

    return "redirect:/Verwalter/manager/role/list";
  }