private static void getChildrenNodes(List<Tree> parentnodes, Tree node) {
    // 循环遍历所有父节点和node进行匹配,确定父子关系
    for (int i = parentnodes.size() - 1; i >= 0; i--) {

      Tree pnode = parentnodes.get(i);
      // 如果是父子关系,为父节点增加子节点,退出for循环
      if (pnode.getId().equals(node.getPid())) {
        pnode.getChildren().add(node);
        return;
      } else {
        // 如果不是父子关系,删除父节点栈里当前的节点,
        // 继续此次循环,直到确定父子关系或不存在退出for循环
        parentnodes.remove(i);
      }
    }
  }
  @RequestMapping(value = "/getdistricttree")
  @ResponseBody
  @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
  public List<Tree> getDistrictTree(@RequestParam("id") String id) {
    Session session =
        ApplicationContextUtils.getHibernateTemplate().getSessionFactory().getCurrentSession();
    //		DistrictVO vo = (DistrictVO) session.get(DistrictVO.class, id);
    String hql = "from DistrictVO where 1=1 ";
    if (id != null && !"".equals(id)) {
      hql =
          hql
              + " and id ='"
              + id
              + "' or ParentID = '"
              + id
              + "' or ParentID in (select id from DistrictVO where ParentID = '"
              + id
              + "') or ParentID in (select id from DistrictVO where ParentID in (select id from DistrictVO where ParentID = '"
              + id
              + "'))";
    }

    hql = hql + "order by id";

    List list2 = session.createQuery(hql).list();
    List<Tree> list = new ArrayList<Tree>();
    if (list2 != null && list2.size() > 0) {
      for (int i = 0; i < list2.size(); i++) {
        Tree tree = new Tree();
        DistrictVO vo = (DistrictVO) list2.get(i);
        tree.setId(vo.getID());
        tree.setText(vo.getName());
        tree.setPid(vo.getParentID());
        //				try {
        //					BeanUtils.copyProperties(tree, list2.get(i));
        //				} catch (IllegalAccessException e) {
        //					// TODO Auto-generated catch block
        //					e.printStackTrace();
        //				} catch (InvocationTargetException e) {
        //					// TODO Auto-generated catch block
        //					e.printStackTrace();
        //				}
        list.add(tree);
      }
    } else {
      return null;
    }
    Tree root = new Tree();
    Tree node = new Tree();
    List<Tree> treelist = new ArrayList<Tree>(); // 拼凑好的json格式的数据
    List<Tree> parentnodes = new ArrayList<Tree>(); // parentnodes存放所有的父节点
    if (list != null && list.size() > 0) {
      root = (Tree) list.get(0);
      for (int i = 1; i < list.size(); i++) {
        node = (Tree) list.get(i);
        if (node.getPid().equals(root.getId())) {
          // 为tree root 增加子节点
          parentnodes.add(node);
          root.getChildren().add(node);
        } else { // 获取root子节点的孩子节点
          getChildrenNodes(parentnodes, node);
          parentnodes.add(node);
        }
      }
    }
    treelist.add(root);
    return treelist;
  }
  @RequestMapping(value = "/gettaxorgtree")
  @ResponseBody
  @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
  public List<Tree> getTaxorgTree() {
    String taxorgcode = SystemUserAccessor.getInstance().getTaxorgcode();
    String authflag = SystemUserAccessor.getInstance().getAuthflag();
    String hql = "from CodTaxorgcodeVO where 1=1 ";
    if ("01".equals(authflag)) {
      List ls = this.getChildorglist(taxorgcode);
      String taxorgcodes = "";
      for (Iterator iterator = ls.iterator(); iterator.hasNext(); ) {
        CodTaxorgcodeVO orgvo = (CodTaxorgcodeVO) iterator.next();
        taxorgcodes = taxorgcodes + "'" + orgvo.getTaxorgcode() + "',";
      }
      taxorgcodes = taxorgcodes.substring(0, taxorgcodes.length() - 1);
      System.out.println("taxorgcodes=" + taxorgcodes);
      hql = hql + " and taxorgcode in (" + taxorgcodes + ")";
    } else {
      hql = hql + " and taxorgcode ='" + taxorgcode + "'";
    }
    //		JSONObject result = new JSONObject();

    //		if(taxorgcode.length()>=10 && !"00".equals(taxorgcode.substring(8, 10))){//村、街道
    //			hql = hql + " and  taxorgcode ='"+taxorgcode+"'";
    //		}else if(taxorgcode.length()>=10 && !"00".equals(taxorgcode.substring(6, 8))){//乡镇机关
    //			hql = hql + " and (taxorgcode  in (select taxorgcode from CodTaxorgcodeVO where
    // parentId='"+taxorgcode+"') or taxorgcode ='"+taxorgcode+"')";
    //		}else if(taxorgcode.length()>=10 && !"00".equals(taxorgcode.substring(4, 6))){//县级机关
    //			hql = hql + " and (taxorgcode  in (select taxorgcode from CodTaxorgcodeVO where
    // parentId='"+taxorgcode+"') or parentId in (select taxorgcode from CodTaxorgcodeVO where
    // parentId='"+taxorgcode+"')  or taxorgcode ='"+taxorgcode+"')";
    //		}else if(taxorgcode.length()>=10 && !"00".equals(taxorgcode.substring(2, 4))){//州市级机关
    //			hql = hql + " and (taxorgcode  in (select taxorgcode from CodTaxorgcodeVO where
    // parentId='"+taxorgcode+"') or parentId in (select taxorgcode from CodTaxorgcodeVO where
    // parentId='"+taxorgcode+"') or parentId in (select taxorgcode from CodTaxorgcodeVO where
    // parentId in (select taxorgcode from CodTaxorgcodeVO where parentId='"+taxorgcode+"')) or
    // taxorgcode ='"+taxorgcode+"')";
    //		}else if(taxorgcode.length()>=10 && !"00".equals(taxorgcode.substring(0, 2))){//省
    //
    //		}
    hql = hql + "order by taxorgcode";

    Session session =
        ApplicationContextUtils.getHibernateTemplate().getSessionFactory().getCurrentSession();
    List list2 = session.createQuery(hql).list();
    List<Tree> list = new ArrayList<Tree>();
    if (list2 != null && list2.size() > 0) {
      for (int i = 0; i < list2.size(); i++) {
        Tree tree = new Tree();
        CodTaxorgcodeVO vo = (CodTaxorgcodeVO) list2.get(i);
        tree.setId(vo.getTaxorgcode());
        tree.setText(vo.getTaxorgname());
        tree.setPid(vo.getParentId());
        //				try {
        //					BeanUtils.copyProperties(tree, list2.get(i));
        //				} catch (IllegalAccessException e) {
        //					// TODO Auto-generated catch block
        //					e.printStackTrace();
        //				} catch (InvocationTargetException e) {
        //					// TODO Auto-generated catch block
        //					e.printStackTrace();
        //				}
        list.add(tree);
      }
    } else {
      return null;
    }
    Tree root = new Tree();
    Tree node = new Tree();
    List<Tree> treelist = new ArrayList<Tree>(); // 拼凑好的json格式的数据
    List<Tree> parentnodes = new ArrayList<Tree>(); // parentnodes存放所有的父节点
    if (list != null && list.size() > 0) {
      root = (Tree) list.get(0);
      for (int i = 1; i < list.size(); i++) {
        node = (Tree) list.get(i);
        if (node.getPid().equals(root.getId())) {
          // 为tree root 增加子节点
          parentnodes.add(node);
          root.getChildren().add(node);
        } else { // 获取root子节点的孩子节点
          getChildrenNodes(parentnodes, node);
          parentnodes.add(node);
        }
      }
    }
    treelist.add(root);
    return treelist;
  }