首页 > 其他 > 详细

调用 递归方法

时间:2020-04-08 11:21:11      阅读:72      评论:0      收藏:0      [点我收藏+]
数据表是这种,具有父子级关系
技术分享图片

 

 

 
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 返回结果
技术分享图片

 

 技术分享图片

 

 

 
 


调用 递归方法

原文:https://www.cnblogs.com/YangBinChina/p/12658548.html

(1)
(1)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!