组织树

    public TreeForSearch getPartAreaAllTree(String matchName) {
        List<ConstructionArea> tempRstChildList = new ArrayList<ConstructionArea>();//临时拓展对象集
        List<ConstructionArea> tempRstParentList = new ArrayList<ConstructionArea>();//临时拓展父对象集
        //临时列表
                List<ConstructionArea> tempList = new ArrayList<ConstructionArea>();
                //临时结果地图
                Map<String, ConstructionArea> tempMap = new HashMap<String, ConstructionArea>();
                //临时结果集
                List<ConstructionArea> tempResult = new ArrayList<ConstructionArea>();
                
                Map<String,ConstructionArea> allArea = this.getAreaMap();
                
                Map<String, String> params = new HashMap<String, String>();

                TreeForSearch treeForSearch = new TreeForSearch();
                List<TreeForSearch> treeForSearchList = new ArrayList<TreeForSearch>();
                
                //如果查询条件为空默认只查选择中国区域项
                if(StringUtil.isEmpty(matchName.trim())){
                    params.put("sapcountrycode", "CN");
                }
                
                //选择名字模糊查询获取所有含子的

                params.put("longDesc", "%" + matchName.trim() + "%");
                tempList = constructionAreaDao.getBySqlKey("getChild", params);    
                
                if (StringUtil.isNotEmpty(matchName.trim())) {
                    if (tempList.size() > 0 && tempList != null){
                        for (int i=0; i < tempList.size(); i++){
                            //获取所有子类集合
                            getChildElements(tempList.get(i), tempRstChildList);
                            //获取所有父类集合
                            getParentElements(tempList.get(i), tempRstParentList);
                        }
                    }
                    //将查询包含的所有符合条件的节点加入到tempMap中
                    if (tempRstChildList.size() > 0 && tempRstChildList != null){
                        for (int i=0; i < tempRstChildList.size(); i++){
                            tempMap.put(tempRstChildList.get(i).getMdmcode(), tempRstChildList.get(i));
                        }
                    }
                    //将对父结果集的查询包含的所有符合条件的节点加入到tempMap中
                    if (tempRstParentList.size() > 0 && tempRstParentList != null){
                        for (int i=0; i < tempRstParentList.size(); i++){
                            tempMap.put(tempRstParentList.get(i).getMdmcode(), tempRstParentList.get(i));
                        }
                    }
                    //遍历map转化为list对象
                    Iterator<String> it = tempMap.keySet().iterator(); 
                    while (it.hasNext()) {
                        String key = it.next().toString();  
                        tempResult.add(tempMap.get(key));  
                    } 
                    treeForSearchList = toVOList(tempResult);
                }else {
                    treeForSearchList = toVOList(tempList);
                }
                //将ConstructionArea转化为TreeForSearch的List
                //构造虚拟父节点
                treeForSearch.setId("0");
                treeForSearch.setParentId("");
                treeForSearch.setName("root");
                treeForSearch.setPath("");
                treeForSearch.setOpen("true");
                treeForSearch.setIsLast(CommonSourceConst.IS_NOT_LAST_ELEMENT);
                treeForSearchList.add(treeForSearch);
                if (treeForSearch != null) {
                    getChildren(treeForSearch, treeForSearchList);
                }
                return treeForSearch;
    }
    
    /** 
      * @Title: getParentElements 
      * @Description: TODO(获取所有父类集合) 
      * @param @param constructionArea
      * @param @param parentList    设定参数 
      * @return void    返回类型 
      * @throws 
    */ 
    private void getParentElements(ConstructionArea constructionArea, List<ConstructionArea> parentList){
        //找到该节点下所有父类集合
        String parentId = constructionArea.getParentid();
        if (StringUtil.isNotEmpty(parentId) && parentId != null && !parentId.equals("0")){
            parentList.add(constructionArea);
            List<ConstructionArea> tempParent = constructionAreaDao.getParentById(parentId);
            for(ConstructionArea vo : tempParent){
                getParentElements(vo, parentList);
            }
        }else{
            parentList.add(constructionArea);
        }
    }
    
    /** 
      * @Title: getChildElements 
      * @Description: TODO
      * @param @param constructionArea
      * @param @param childList    设定参数 
      * @return void    返回类型 
      * @throws 
    */ 
    private void getChildElements(ConstructionArea constructionArea, List<ConstructionArea> childList){
        String isLast = constructionArea.getIslast();
        if (CommonSourceConst.IS_NOT_LAST_ELEMENT.equals(isLast)){
            childList.add(constructionArea);
            List<ConstructionArea> tempChildList = new ArrayList<ConstructionArea>();
            Map<String, String> childParams = new HashMap<String, String>();
            childParams.put("sapcountrycode", "CN");
            childParams.put("parentid", constructionArea.getMdmcode());
            tempChildList = constructionAreaDao.getBySqlKey("getChild", childParams);
            for (ConstructionArea baseInfo : tempChildList){
                getChildElements(baseInfo,childList);
            }
        }else{
            childList.add(constructionArea);
        }
    }

    private void getChildren(TreeForSearch treeForSearch, List<TreeForSearch> treeForSearchList){
        for (TreeForSearch baseInfo : treeForSearchList) {
            if (StringUtil.isNotEmpty(baseInfo.getParentId()) && baseInfo.getParentId().equals(treeForSearch.getId())) {
                getChildren(baseInfo, treeForSearchList);
                treeForSearch.getChildren().add(baseInfo);
            }
        }
    }

 

sql查询方法

<sql id="dynamicWhere">
        <where>
            
            <if test="@Ognl@isNotEmpty(mdmcode)"> AND MdmCode = #{mdmcode} </if>
            <if test="@Ognl@isNotEmpty(shortdesc)"> AND ShortDesc like #{shortdesc} </if>
            <if test="@Ognl@isNotEmpty(sapcountrycode)"> AND path like 'CN%' </if>
            <if test="@Ognl@isNotEmpty(parentid)"> AND ParentId = #{parentid} </if>
            <if test="@Ognl@isNotEmpty(longDesc)"> AND longDesc like #{longDesc} </if>
            <if test="@Ognl@isNotEmpty(path)"> AND path like #{path} </if>
        </where>
    </sql>

<select id="getChild" resultMap="ConstructionArea" parameterType="java.util.List">
      select
      Hierarchy, MdmCode, case when b.IsLast = '0' then 'false' else 'true' end as IsLast, 
      ParentId, ShortDesc, LongDesc, SapCountryCode, SapLanguageCode, NationalCode,
    LEVEL_NAME, UPPER_LEVEL_NAME, Path, COUNTRY, Status
      from ct_construction_area b
      <include refid="dynamicWhere"/>
      order by Hierarchy asc
    </select>

 

<select id="getById" resultMap="ConstructionArea" parameterType="java.lang.String" >
    select
      Hierarchy, MdmCode, case when b.IsLast = '0' then 'false' else 'true' end as IsLast, 
      ParentId, ShortDesc, LongDesc, SapCountryCode, SapLanguageCode, NationalCode,
    LEVEL_NAME, UPPER_LEVEL_NAME, Path, COUNTRY, Status
      from ct_construction_area b
    where MdmCode = #{mdmcode,jdbcType=VARCHAR}
      </select>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值