private void fillSubMenus(List<Menu> menus) {
    List<Menu> parentMenus = menus;
    while (parentMenus != null && parentMenus.size() > 0) {
      List<Menu> newParentMenus = new ArrayList<Menu>();
      for (Menu menuEle : parentMenus) {
        CoreMenuModel exampleCoreMenuEntity = new CoreMenuModel();
        exampleCoreMenuEntity.setParentId(menuEle.getId());
        List<CoreMenuModel> subMenuEntities = coreMenuDao.getModelList(exampleCoreMenuEntity);
        if (subMenuEntities != null && subMenuEntities.size() > 0) {
          List<Menu> subMenus = new ArrayList<Menu>();
          for (CoreMenuModel subMenuEntity : subMenuEntities) {
            CoreMenuModel menuEntityFull = coreMenuDao.getModelById(subMenuEntity.getId());
            if (menuEntityFull != null) {
              Menu tmpMenu = new Menu();
              BeanUtils.copyProperties(menuEntityFull, tmpMenu);
              subMenus.add(tmpMenu);
            }
          }

          menuEle.setSubMenus(subMenus);
          newParentMenus.addAll(subMenus);
        }
      }
      parentMenus = newParentMenus;
    }
  }
  private List<RtnMenuValue> fillSubRtnMenuValues(
      List<Menu> menus, Set<Integer> menuIdSet, Integer userId) {
    List<RtnMenuValue> rtnMenus = new ArrayList<RtnMenuValue>();
    List<Menu> parentMenus = menus;
    if (parentMenus != null && parentMenus.size() > 0) {
      for (Menu menuEle : parentMenus) {
        RtnMenuValue rtnParent = new RtnMenuValue();
        BeanUtils.copyProperties(menuEle, rtnParent);

        CoreMenuModel exampleCoreMenuEntity = new CoreMenuModel();
        exampleCoreMenuEntity.setParentId(menuEle.getId());
        List<CoreMenuModel> subMenuEntities = coreMenuDao.getModelList(exampleCoreMenuEntity);
        if (subMenuEntities != null && subMenuEntities.size() > 0) {
          List<RtnMenuValue> subMenus = new ArrayList<RtnMenuValue>();
          for (CoreMenuModel subMenuEntity : subMenuEntities) {
            // 超级管理员拥有所有菜单权限
            if (menuIdSet.contains(subMenuEntity.getId())
                || (userId != null && userId.equals(-1))
                || (subMenuEntity.getType() != null && subMenuEntity.getType().equals('0'))) {
              CoreMenuModel menuEntityFull = coreMenuDao.getModelById(subMenuEntity.getId());
              if (menuEntityFull != null) {
                RtnMenuValue tmpMenu = new RtnMenuValue();
                BeanUtils.copyProperties(menuEntityFull, tmpMenu);
                subMenus.add(tmpMenu);
              }
            }
          }

          rtnParent.setSubMenus(subMenus);
        }
        rtnMenus.add(rtnParent);
      }
    }
    return rtnMenus;
  }
  @Override
  public List<Menu> findMenusByExample(Menu menu) throws BusinessException {
    List<Menu> menus = new ArrayList<Menu>();
    CoreMenuModel exampleCoreMenuEntity = new CoreMenuModel();

    if (menu != null) {
      if (menu.getId() != null) exampleCoreMenuEntity.setId(menu.getId());
      if (menu.getName() != null && !"".equals(menu.getName().trim())) {
        menu.setName(menu.getName().trim());
        exampleCoreMenuEntity.setName("%" + menu.getName() + "%");
      }
    }

    List<CoreMenuModel> menuEntities = coreMenuDao.getModelList(exampleCoreMenuEntity);

    if (menuEntities != null && menuEntities.size() > 0) {
      for (CoreMenuModel entity : menuEntities) {

        if (entity != null && (menu.getId() == null || menu.getId().equals(entity.getId()))) {
          Menu tmpMenu = new Menu();
          BeanUtils.copyProperties(entity, tmpMenu);
          menus.add(tmpMenu);
        }
      }
    }

    if (menus != null && menus.size() > 0) {
      fillSubMenus(menus);
    }

    return menus;
  }
  @Override
  public void removeMenu(Menu menu) throws BusinessException {

    if (menu.getId() == null) return;

    CoreMenuModel coreMenuEntity = new CoreMenuModel();
    coreMenuEntity.setId(menu.getId());
    coreMenuDao.delete(coreMenuEntity);

    User2MenuModel user2MenuModel = new User2MenuModel();
    user2MenuModel.setMenuId(menu.getId());
    user2MenuDao.deleteByModel(user2MenuModel);
  }
  private void menuCheck(Menu menu) throws BusinessException {
    Map<String, String> errors = new HashMap<String, String>(2);

    if (menu.getName() == null || "".equals(menu.getName().trim())) {
      errors.put("name", "菜单名称不能为空!");
    } else if (menu.getName().trim().length() > 20) {
      errors.put("name", "菜单名称不能大于20个字符!");
    }

    if (menu.getTip() != null && menu.getTip().trim().length() > 64) {
      errors.put("tip", "菜单描述不能大于64个字符!");
    }

    if (menu.getAction() != null && menu.getAction().trim().length() > 256) {
      errors.put("action", "菜单动作不能大于256个字符!");
    }

    if (menu.getSorting() == null) {
      // errors.put("sorting", "菜单排序不能为空!");
      // 默认排序为1
      menu.setSorting(1);
    }

    if (menu.getSystemId() == null) {
      errors.put("systemid", "菜单的系统ID不能为空!");
    }

    if (menu.getImg() != null && menu.getImg().trim().length() > 256) {
      errors.put("img", "菜单图片不能大于256个字符!");
    }

    if (!errors.isEmpty()) {
      throw new BusinessException(errors);
    }
  }