父子級結構——遞迴的很好應用
阿新 • • 發佈:2019-01-22
以前上學的時候學遞迴的時候總是會很糊塗,不知道在哪個點重新進入遞迴迴圈……但是看別人的程式碼又會覺得很好並且意猶未盡,借鑑學習吧……
public JSONArray initGrid(Grids parent,User user){ List<Grids> parents=new ArrayList<>(); JSONArray result = new JSONArray(); if (RoleUtil.hasRole(user, "ROLE_SYSTEM") || RoleUtil.hasRole(user, "ROLE_ZX") || RoleUtil.hasRole(user, "ROLE_ZG")) { // ROLE_SYSTEM:超級管理員 ROLE_ZX:省中心管理員 ROLE_ZG:省主管 if (parent == null) { parents = gridsRepository.findByParentIsNullOrderByCreateDateAsc(); } else { parents = gridsRepository.findByParentOrderByCreateDateAsc(parent); } } else if (RoleUtil.hasRole(user, "ROLE_SZX") || RoleUtil.hasRole(user, "ROLE_SZG")){ // 市中心管理員 if (parent == null) { parents = gridsRepository.findByGridManagerIDAndGridTypeIsOrderByCreateDateAsc(user.getId(),"nav"); removeItem(parents); } else { parents = gridsRepository.findByParentOrderByCreateDateAsc(parent); } } for (Grids grid : parents) { Map<String, Object> map = new HashMap<String, Object>(); map.put("id", grid.getId()); map.put("text", grid.getGridName()); map.put("type", grid.getGridType()); map.put("gridManager", grid.getGridManager()); JSONArray children = initGrid(grid,user); map.put("children", children); result.add(map); } return result; }