public SysApplication findById(long id) {
    String cacheKey = "sys_app_" + id;

    if (SystemConfig.getBoolean("use_query_cache") && CacheFactory.getString(cacheKey) != null) {
      String text = CacheFactory.getString(cacheKey);
      com.alibaba.fastjson.JSONObject json = JSON.parseObject(text);
      SysApplication app = SysApplicationJsonFactory.jsonToObject(json);
      if (app != null && app.getNodeId() > 0) {
        SysTree node = sysTreeService.findById(app.getNodeId());
        app.setNode(node);
      }
      return app;
    }

    SysApplication app = sysApplicationMapper.getSysApplicationById(id);
    if (app != null && app.getNodeId() > 0) {
      SysTree node = sysTreeService.findById(app.getNodeId());
      app.setNode(node);
    }

    if (app != null && SystemConfig.getBoolean("use_query_cache")) {
      com.alibaba.fastjson.JSONObject json = app.toJsonObject();
      CacheFactory.put(cacheKey, json.toJSONString());
    }

    return app;
  }
  /**
   * 向前移动排序
   *
   * @param bean
   */
  private void sortByPrevious(long parentId, SysApplication bean) {
    // 查找前一个对象

    SysApplicationQuery query = new SysApplicationQuery();
    query.parentId(Long.valueOf(parentId));
    query.setSortGreaterThan(bean.getSort());
    query.setOrderBy(" E.SORT asc ");

    List<SysApplication> list = this.list(query);
    if (list != null && list.size() > 0) { // 有记录
      SysApplication temp = (SysApplication) list.get(0);
      int sort = bean.getSort();
      bean.setSort(temp.getSort() + 1);
      this.update(bean); // 更新bean
      SysTree node = sysTreeService.findById(bean.getNodeId());
      node.setSort(bean.getSort());
      sysTreeService.update(node);

      temp.setSort(sort - 1);
      this.update(temp); // 更新temp
      node = sysTreeService.findById(temp.getNodeId());
      node.setSort(temp.getSort());
      sysTreeService.update(node);
    }
  }
 /**
  * 获取用户某个分类下的全部分类节点
  *
  * @param parentId 父节点编号
  * @param userId 用户登录账号
  * @return
  */
 public List<TreeModel> getTreeModels(long parentId, String actorId) {
   List<TreeModel> treeModels = new java.util.ArrayList<TreeModel>();
   SysUser user = sysUserService.findByAccount(actorId);
   if (user != null) {
     user = sysUserService.getUserPrivileges(user);
     TreeModel root = sysTreeService.findById(parentId);
     if (user.isSystemAdministrator()) {
       this.loadChildrenTreeModels(treeModels, parentId, user);
       // SysTreeQuery query = new SysTreeQuery();
       // query.treeIdLike(root.getTreeId() + "%");
       // List<SysTree> trees = sysTreeMapper.getSysTrees(query);
       // if (trees != null && !trees.isEmpty()) {
       // for (SysTree tree : trees) {
       // treeModels.add(tree);
       // }
       // }
     } else {
       this.loadChildrenTreeModels(treeModels, parentId, user);
       // SysTreeQuery query = new SysTreeQuery();
       // query.treeIdLike(root.getTreeId() + "%");
       // query.setActorId(actorId);
       // List<SysTree> trees =
       // sysTreeMapper.getTreeListByUsers(query);
       // if (trees != null && !trees.isEmpty()) {
       // for (SysTree tree : trees) {
       // treeModels.add(tree);
       // }
       // }
     }
     treeModels.remove(root);
   }
   return treeModels;
 }
 /**
  * 显示角色权限映射页面
  *
  * @param mapping
  * @param actionForm
  * @param request
  * @param response
  * @return
  * @throws Exception
  */
 public ActionForward showPrivilege(
     ActionMapping mapping,
     ActionForm actionForm,
     HttpServletRequest request,
     HttpServletResponse response)
     throws Exception {
   long deptId = ParamUtil.getLongParameter(request, "deptId", 0);
   long roleId = ParamUtil.getLongParameter(request, "roleId", 0);
   SysDeptRole deptRole = sysDeptRoleService.find(deptId, roleId);
   if (deptRole == null) { // 如果没有找到则创建一个
     deptRole = new SysDeptRole();
     deptRole.setDept(sysDepartmentService.findById(deptId));
     deptRole.setRole(sysRoleService.findById(roleId));
     sysDeptRoleService.create(deptRole);
   }
   request.setAttribute("role", deptRole);
   SysTree parent = sysTreeService.getSysTreeByCode(Constants.TREE_APP);
   List list = new ArrayList();
   sysTreeService.getSysTree(list, (int) parent.getId(), 0);
   request.setAttribute("list", list);
   return mapping.findForward("show_privilege");
 }
 /**
  * 获取某个分类下的全部分类节点
  *
  * @param parentId 父节点编号
  * @return
  */
 public List<TreeModel> getTreeModels(long parentId) {
   List<TreeModel> treeModels = new java.util.ArrayList<TreeModel>();
   TreeModel root = sysTreeService.findById(parentId);
   SysTreeQuery query = new SysTreeQuery();
   query.treeIdLike(root.getTreeId() + "%");
   List<SysTree> trees = sysTreeMapper.getSysTrees(query);
   if (trees != null && !trees.isEmpty()) {
     for (SysTree tree : trees) {
       treeModels.add(tree);
     }
   }
   treeModels.remove(root);
   return treeModels;
 }
 public TreeModel getTreeModelByAppId(long appId) {
   SysApplication bean = this.findById(appId);
   if (bean != null) {
     TreeModel treeModel = sysTreeService.findById(bean.getNodeId());
     treeModel.setCode(bean.getCode());
     treeModel.setName(bean.getName());
     treeModel.setLocked(bean.getLocked());
     treeModel.setDescription(bean.getDesc());
     treeModel.setUrl(bean.getUrl());
     treeModel.setSortNo(bean.getSort());
     return treeModel;
   }
   return null;
 }
  /**
   * 根据节点编号查找对象
   *
   * @param nodeId
   * @return
   */
  public SysApplication findByNodeId(long nodeId) {
    SysApplicationQuery query = new SysApplicationQuery();
    query.nodeId(nodeId);

    List<SysApplication> list = this.list(query);
    if (list != null && !list.isEmpty()) {
      SysApplication sysApplication = list.get(0);
      SysTree node = sysTreeService.findById(sysApplication.getNodeId());
      sysApplication.setNode(node);
      return sysApplication;
    }

    return null;
  }
 @Transactional
 public boolean update(SysApplication bean) {
   String cacheKey = "sys_app_" + bean.getId();
   bean.setUpdateDate(new Date());
   if (StringUtils.isEmpty(bean.getCode())) {
     bean.setCode("app_" + bean.getId());
   }
   this.sysApplicationMapper.updateSysApplication(bean);
   CacheFactory.remove(cacheKey);
   if (bean.getNode() != null) {
     bean.getNode().setCode(bean.getCode());
     bean.getNode().setLocked(bean.getLocked());
     bean.getNode().setUpdateBy(bean.getUpdateBy());
     sysTreeService.update(bean.getNode());
     cacheKey = "sys_tree_" + bean.getNode().getId();
     CacheFactory.remove(cacheKey);
   }
   return true;
 }
 @Transactional
 public boolean create(SysApplication bean) {
   boolean ret = false;
   if (bean.getId() == 0) {
     bean.setId(idGenerator.nextId());
   }
   if (StringUtils.isEmpty(bean.getCode())) {
     bean.setCode("app_" + bean.getId());
   }
   if (bean.getNode() != null) {
     bean.getNode().setCode(bean.getCode());
     bean.getNode().setDiscriminator("A");
     bean.getNode().setCreateBy(bean.getCreateBy());
     sysTreeService.create(bean.getNode());
     bean.setNodeId(bean.getNode().getId());
   }
   bean.setSort((int) bean.getId()); // 设置排序号为刚插入的id值
   bean.setCreateDate(new Date());
   sysApplicationMapper.insertSysApplication(bean);
   ret = true;
   return ret;
 }