调用 递归方法
数据表是这种,具有父子级关系
1、最顶层调用处
List<ReMap> deviceRMLt = new ArrayList<>();
deviceRMLt = DeviceModel.getRecursionDeviceLt(1,null,pmap);
dataMap.put(Ckey.Pc.LIST, deviceRMLt);
2、注意,调用递归方法的时候,自身的id,作为父id;tier+1。
/** * 递归获取设备 * * @param Integer tier 层级,例如1,2,3 * @param Long fid 父级id * @param ReMap pmap 其他请求参数 * @return List<ReMap> */ public static List<ReMap> getRecursionDeviceLt(Integer tier,Long fid,ReMap pmap) { List<ReMap> resultLt = new ArrayList<>(); DbCache dbCache = DbCache.instance(); pmap.put(Ckey.Pc.TIER, tier); pmap.put(Ckey.Pc.FDID, fid); List<Device> list = dbCache.selectList("deviceList", pmap); if (list != null) { ReMap resultRM = new ReMap(); for (Device device : list) { ReMap mapItem; try { mapItem = MapUtil.objectToReMap(device); if (mapItem == null) { continue; } SystemDict systemDict = SysModel.getSubSystem(device.getSysid()); if (systemDict != null && systemDict.getFid() != null) { mapItem.put(Ckey.Sc.FSYSID, systemDict.getFid()); mapItem.put(Ckey.Sc.SYSNAME, systemDict.getName()); } Area area = AreaModel.getArea(device.getAid()); if (area != null) { mapItem.put(Ckey.Sc.ANAME, area.getName()); } Cabinet cabinet = CabinetManager.getCabinet(device.getCabinetid()); if (cabinet != null) { mapItem.put(Ckey.Sc.CNAME, cabinet.getCname()); } DeviceType deviceType = DeviceTypeModel.getDeviceType(device.getDevtypeid()); if (deviceType != null) { mapItem.put(Ckey.Dv.DEVTYPENAME, deviceType.getDtname()); } //递归调用 List<ReMap> tmpList = getRecursionDeviceLt(tier+1,device.getId(),pmap); if (tmpList != null && tmpList.size() > 0) { mapItem.put(Ckey.Pc.CHILDREN, tmpList); } resultLt.add(mapItem); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } } return resultLt; }
3 返回结果