需求一、查詢當前群的所有子級群組

遞歸 遍歷N叉樹,獲取數據
/** * 獲取子級群列表信息 * @param roomId * @param inside * @return */ @Override public TreeRoom getSubGroup(ObjectId roomId, int inside) { Room room = roomCoreDao.get(roomId); if(null == room){ return null; } TreeRoom treeRoom = new TreeRoom(room.getId(),room.getLogo(),room.getJid(),room.getGroupId(),room.getName(),room.getType(),room.getInside(),room.getParentId(),null); return recursion(treeRoom,inside); } /** * 遍歷多叉樹節點(向下遍歷) * @param treeRoom 根節點 * @param inside -1不區分內外部群 0內部群 1外部群 * @return */ public TreeRoom recursion(TreeRoom treeRoom,int inside){ List<TreeRoom> list = null; TreeRoom subRoom = null; List<Room> rooms = roomCoreDao.getRoomByParentId(treeRoom.getId()); if (null != rooms && rooms.size()>0){ list = new ArrayList<>(rooms.size()); for (int i = 0; i < rooms.size(); i++) { if(rooms.get(i).getInside() == inside || inside == -1){ subRoom = new TreeRoom(rooms.get(i).getId(),rooms.get(i).getLogo(),rooms.get(i).getJid(),rooms.get(i).getGroupId(),rooms.get(i).getName(),rooms.get(i).getType(),rooms.get(i).getInside(),rooms.get(i).getParentId(),null); } if(null == subRoom){ break; } if(treeRoom.getSubRoom() == null){ list.add(subRoom); treeRoom.setSubRoom(list); }else { treeRoom.getSubRoom().add(subRoom); } // 遞歸處理子級群 recursion(subRoom,inside); } } return treeRoom; }
返回數據
{ "currentTime": 1637218489567, "data": { "groupId": "100000005371", "id": "618b747e55bae6217cca507b", "inside": 0, "jid": "e349b7bc809c46e1b8614687a04e5201", "name": "太極馬包谷官方群", "subRoom": [ { "groupId": "100000002330", "id": "618ce53889180154e0733bd0", "inside": 1, "jid": "c686735712924e47b09fd2f57262227c", "name": "馬太極主群", "parentId": "618b747e55bae6217cca507b", "subRoom": [ { "groupId": "100000005301", "id": "6195f86b06eccf39ceb04ad2", "inside": 1, "jid": "42a71b84483b422d9e641d254bfdec3d", "name": "馬太極分群1", "parentId": "618ce53889180154e0733bd0", "type": 12 }, { "groupId": "100000007758", "id": "6195f8a806eccf39ceb04ad6", "inside": 1, "jid": "366fba389cac4370941fde3885d94ecf", "name": "馬太極分群3", "parentId": "618ce53889180154e0733bd0", "type": 12 } ], "type": 11 }, { "groupId": "100000005717", "id": "618cecb43c48063a72efde44", "inside": 0, "jid": "01e9ff3f159c4134a9327d536cf9611b", "name": "馬太極主群2", "parentId": "618b747e55bae6217cca507b", "subRoom": [ { "groupId": "100000006522", "id": "618ced113c48063a72efde49", "inside": 0, "jid": "9a13f53c573e4c418704052ca8c4e8aa", "name": "馬太極分群2", "parentId": "618cecb43c48063a72efde44", "subRoom": [ { "groupId": "100000006107", "id": "618ced5b3c48063a72efde4e", "inside": 0, "jid": "5f3decd59f5446f5a7934fa36b957cbe", "name": "馬太極支群2", "parentId": "618ced113c48063a72efde49", "subRoom": [ { "groupId": "100000009504", "id": "618ced803c48063a72efde52", "inside": 0, "jid": "c7ef71e5912444839a9e783b827c6349", "name": "馬太極子群2", "parentId": "618ced5b3c48063a72efde4e", "type": 14 } ], "type": 13 } ], "type": 12 } ], "type": 11 } ], "type": 2 }, "resultCode": 1 }
需求二、模糊搜索展示群組信息,並附帶它的父級群組信息

先模糊查詢群組信息,再向上遍歷補全父級節點數據
/** * 向上遍歷補全父級數據 * @param * @return */ public List<TreeRoom> upTraversal(List<TreeRoom> treeRooms){ TreeRoom parentRoom = null; Room parent = null; List<TreeRoom> list = null; if(null != treeRooms && treeRooms.size() > 0){ for (int i = 0; i < treeRooms.size(); i++) { if(treeRooms.get(i).getType() != 2){ //判斷不是祖級 parent = roomCoreDao.getRoomById(treeRooms.get(i).getParentId()); parentRoom = new TreeRoom(parent.getId(),parent.getLogo(),parent.getJid(),parent.getGroupId(),parent.getName(),parent.getType(),parent.getInside(),parent.getParentId(),null); list = new ArrayList<>(1); list.add(new TreeRoom(treeRooms.get(i).getId(),treeRooms.get(i).getLogo(),treeRooms.get(i).getJid(),treeRooms.get(i).getGroupId(),treeRooms.get(i).getName(),treeRooms.get(i).getType(),treeRooms.get(i).getInside(),treeRooms.get(i).getParentId(),treeRooms.get(i).getSubRoom())); parentRoom.setSubRoom(list); treeRooms.remove(i); treeRooms.add(i,parentRoom); if(parent.getType() != 2){ upTraversal(treeRooms); } } } } return treeRooms; }
對於同一個祖級節點下的同名群組,存在數據重復的問題,因此要去重
{ "groupId": "100000005371", "id": "618b747e55bae6217cca507b", "inside": 0, "jid": "e349b7bc809c46e1b8614687a04e5201", "name": "太極馬包谷官方群", "type": 2 }, { "groupId": "100000005371", "id": "618b747e55bae6217cca507b", "inside": 0, "jid": "e349b7bc809c46e1b8614687a04e5201", "name": "太極馬包谷官方群", "subRoom": [ { "groupId": "100000002330", "id": "618ce53889180154e0733bd0", "inside": 1, "jid": "c686735712924e47b09fd2f57262227c", "name": "馬太極主群", "parentId": "618b747e55bae6217cca507b", "type": 11 } ], "type": 2 }, { "groupId": "100000005371", "id": "618b747e55bae6217cca507b", "inside": 0, "jid": "e349b7bc809c46e1b8614687a04e5201", "name": "太極馬包谷官方群", "subRoom": [ { "groupId": "100000005717", "id": "618cecb43c48063a72efde44", "inside": 0, "jid": "01e9ff3f159c4134a9327d536cf9611b", "name": "馬太極主群2", "parentId": "618b747e55bae6217cca507b", "type": 11 } ], "type": 2 }
可以看到,當搜索“太極”時,這三條數據其實都是一個祖級節點。需要合並成一條數據。
就此打住,這個實現復雜不說,后台處理大量數據也影響性能。因此增加群層級的類,保存到數據庫,直接查詢比較簡單快捷。
