Organization of all departmental trees, and conditional Query Department tree:
/*** Organizational Department tree *@return */@Override PublicList<sxybranchvo>Findallbranchtree (String branchname) {SXYBRANCHVO Sxybranchvoparam=NewSXYBRANCHVO (); Sxybranchvoparam.setbranchcode (""); Sxybranchvoparam.setdeleteflag ("1"); //querying all root nodes rootList<sxybranchvo> sxybranchvolist =SXYBRANCHMAPPER.FINDPARENTBRANCHVO (Sxybranchvoparam); if(Stringutils.isnotblank (Branchname)) {//Query by department name, organize the eligible nodes into a treeSet<sxybranchvo> Branchvoset =NewHashset<>(); Set<SxyBranchVO> branchvosetsub =NewHashset<>(); //querying all child nodes that meet the criteriaList<sxybranchvo> Sxybranchvos =Sxybranchmapper.getbranchsbyname (branchname); for(Sxybranchvo sxybranchvosub:sxybranchvos) {Sxybranchvosub.setdeleteflag ("1"); Branchvoset.add (sxybranchvosub); //recursive upward, placing the eligible node and all its parent nodes in the Set collectionBranchvosetsub =Buildtreeup (sxybranchvosub,branchvosetsub); for(Sxybranchvo sbv:branchvosetsub) {branchvoset.add (SBV); } } //organization Tree, which organizes all eligible nodes into the treeList<sxybranchvo> sxybranchvolistnew =NewArraylist<>(); for(Sxybranchvo sxybranchvo:sxybranchvolist) {//Organizational tree, all departments under the root nodeSxybranchvo.setdeleteflag ("1"); Buildtree (Sxybranchvo,branchvoset); if(Sxybranchvo.getchildren ()! =NULL&& Sxybranchvo.getchildren (). Size () > 0) {sxybranchvolistnew.add (SXYBRANCHVO); } } returnsxybranchvolistnew; }Else{//querying all, organizing all nodes into a tree for(Sxybranchvo sxybranchvo:sxybranchvolist) {//Organizational tree, all departments under the root nodeSxybranchvo.setdeleteflag ("1"); Buildtreeall (SXYBRANCHVO); } returnsxybranchvolist; } } /*** Recursively queries all parent nodes (only one parent node per layer) *@paramSxybranchvosub *@paramBranchvosetsub *@return */ PrivateSet<sxybranchvo> Buildtreeup (Sxybranchvo sxybranchvosub, set<sxybranchvo>branchvosetsub) { //query all parent nodes recursivelySXYBRANCHVO SXYBRANCHVO =Sxybranchmapper.findparentbranchvobyparentcode (sxybranchvosub); if(SXYBRANCHVO! =NULL){//if not the root nodeBranchvosetsub.add (SXYBRANCHVO); Sxybranchvo.setdeleteflag ("1"); Buildtreeup (sxybranchvo,branchvosetsub); } returnbranchvosetsub; } /*** Recursive Query sub-node (recursive downward, organization-eligible tree) *@paramSXYBRANCHVO*/ Private voidBuildtree (SXYBRANCHVO sxybranchvo,set<sxybranchvo>Branchvoset) {List<SxyBranchVO> sxybranchvolistnew =NewArraylist<>(); //querying immediate child nodesList<sxybranchvo> sxybranchvolist =SXYBRANCHMAPPER.FINDPARENTBRANCHVO (SXYBRANCHVO); for(Sxybranchvo sxybranch:sxybranchvolist) { for(Sxybranchvo sbr:branchvoset) {if(Sxybranch.getbranchcode (). Equals (Sbr.getbranchcode ())) {Sxybranchvolistnew.add (sxybranch); }}} Sxybranchvo.setchildren (Sxybranchvolistnew); for(Sxybranchvo sxybranchitem:sxybranchvolist) {Sxybranchitem.setdeleteflag ("1"); Buildtree (Sxybranchitem,branchvoset); } } /*** Recursive query child node (recursive down, organization of all trees) *@paramSXYBRANCHVO*/ Private voidBuildtreeall (Sxybranchvo sxybranchvo) {//querying immediate child nodesList<sxybranchvo> sxybranchvolist =SXYBRANCHMAPPER.FINDPARENTBRANCHVO (SXYBRANCHVO); Sxybranchvo.setchildren (sxybranchvolist); for(Sxybranchvo sxybranchitem:sxybranchvolist) {Sxybranchitem.setdeleteflag ("1"); Buildtreeall (Sxybranchitem); } }
Comment out the code for reference:
/* Private Boolean Buildtreematch (SXYBRANCHVO sxybranchvo,list<sxybranchvo> sxybranchvos) { Boolean Flag = false; Query immediate child node list<sxybranchvo> sxybranchvolist = SXYBRANCHMAPPER.FINDPARENTBRANCHVO (SXYBRANCHVO); for (Sxybranchvo Sb:sxybranchvos) {for (Sxybranchvo sbv:sxybranchvolist) { if (Sb.getbranchcode (). Equals ( Sbv.getbranchcode ())) { return true; } } } if (!flag) { sxybranchvo.setchildren (sxybranchvolist); for (Sxybranchvo sxybranchitem:sxybranchvolist) { Sxybranchitem.setdeleteflag ("1"); Buildtreematch (Sxybranchitem,sxybranchvos); } } return flag;} */
Java code recursive departmental structure tree