一、serviceimpl的实现方法
public List<DataSource> list(DataSource dataSource) {
// 封装父集list
List<DataSource> mapSources = new ArrayList<>();
// 封装子集list
List<DataSource> mapSourceList = new ArrayList<>();
List<DataSource> mapSourceByName = dataSourceMapper.findDataSourceByName(dataSource);
// 1、遍历查询出的父结构,并添加进new的list当中
for (DataSource mapSource1:mapSourceByName){
if ("0".equals(mapSource1.getPid())){
mapSources.add(mapSource1);
}
else{
// 根据自集对象获取父集对象
mapSourceList.add(mapSource1);
}
}
mapSourceByName.clear();
if (null == mapSources || mapSources.size() <= 0){
// 子集构建父集
List<DataSource> mapSources1 = new ArrayList<>();
for (DataSource mapSourc:mapSourceList){
DataSource mapSource1 = new DataSource();
mapSource1.setOrgcode(mapSourc.getOrgcode());
mapSource1.setId(mapSourc.getPid());
DataSource mapSource2 = dataSourceMapper.findDataSourceByName(mapSource1).get(0);
mapSources1.add(mapSource2);
}
mapSources = mapSources1.stream().distinct().collect(Collectors.toList());
mapSourceByName = mapSourceList;
}
// 父集非空并且子集为空的情况
if (null != mapSources && mapSourceByName.size() == 0){
mapSourceList.clear();
for (DataSource mapSource1:mapSources){
DataSource mapSource2 = new DataSource();
mapSource2.setPid(mapSource1.getId());
mapSource2.setOrgcode(mapSource1.getOrgcode());
dataSourceMapper.findDataSourceByName(mapSource2).forEach(mapSource3 -> {
mapSourceList.add(mapSource3);
});
}
mapSourceByName = mapSourceList;
}
// mapSourceByName子集
// mapSources父集
for (DataSource mapSource:mapSources){
ArrayList<DataSource> dataSources = new ArrayList<>();
for (DataSource dataSource1:mapSourceByName){
if (mapSource.getId().equals(dataSource1.getPid())){
dataSources.add(dataSource1);
}
}
mapSource.setDataSourceList(dataSources);
}
return mapSources;
}
二、mapper接口的sql实现
<select id="findDataSourceByName" parameterType="java.lang.Object" resultMap="DataSource">
select
a.ID as id,
a.PID as pid,
a.NAME as name,
a.CONTROLLER_U as controllerU,
a.CONTROLLER_R as controllerR,
a.CONTROLLER_D as controllerD,
a.UPDATETIME as updatetime,
a.CREATETIME as createtime,
a.UPDATEBYID as updatebyid,
a.URL as url,
a.ORDERBY as orderby
from
DATA_SOURCE a inner join ORGCODE_DATA_REL b
on a.ID = b.DATASOURCEID
and b.STATUS = '0'
<where>
<if test="orgcode != null and orgcode != ''">
AND b.ORGCODE = #{orgcode}
</if>
<if test="id != null and id != ''">
and a.ID = #{id}
</if>
<if test="pid != null and pid != ''">
and a.PID = #{pid}
</if>
<if test="name != null and name != ''">
and a.NAME like concat('%',concat(#{name},'%'))
</if>
</where>
order by a.ORDERBY asc
</select>