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>