@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 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;
  }