zoukankan      html  css  js  c++  java
  • 调用 递归方法

    数据表是这种,具有父子级关系
     
    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 返回结果

     

     
     


  • 相关阅读:
    设计模式-观察者模式(Observer Pattern)
    设计模式-策略模式(Strategy Pattern)
    数据结构-红黑树
    数据结构-二叉搜索树(BST binary search tree)
    算法-插入排序(Insertion sorting)
    算法-桶排序(Bucket sort)
    设计模式-单例模式(Singleton Pattern)
    算法-基数排序(radix sort)
    算法-计数排序及其变体
    Pytest框架的使用
  • 原文地址:https://www.cnblogs.com/YangBinChina/p/12658548.html
Copyright © 2011-2022 走看看