Java开发笔记11 (只展示到树的二级部分)
=====牵涉到树的生成,树的列表展示=====
1、Controller:
@PostMapping("/list-by-parentId")
public Result listByParentId(@RequestBody Map<String,String> map) {
String regionId = map.get("regionId");
//获取用户有权限查看的车站ID集合
Set<String> stationIds = this.getStationIds();
List<String> ids = new ArrayList<>(stationIds);
return Result.ok(stationRegionTreeService.listByParent(regionId, ids));
}
2、Service:
List<StationRegionTree> listByParent(String regionId, List<String> ids);
3、ServiceImpl:
/**
* 设备区域列表展示
*/
@Override
public List<StationRegionTree> listByParent(String regionId, List<String> stationIds) {
if (org.apache.commons.lang3.StringUtils.isEmpty(regionId)) {
List<StationRegionTree> result = stationRegionTreeDao.findStationByIds(stationIds);
for(StationRegionTree stationRegionTree : result) {
for(StationRegionTree child : stationRegionTree.getChildren()) {
if(child.getChildren() != null && !child.getChildren().isEmpty()) {
child.getChildren().clear();
}
}
}
return result;
} else {
String pid = stationRegionTreeDao.findParentId(regionId, stationIds);
List<StationRegionTree> byRegionId;
if(pid.equals("1")) {
byRegionId = stationRegionTreeDao.findByPid(regionId);
for(StationRegionTree stationRegionTree : byRegionId) {
if(stationRegionTree.getChildren() != null && !stationRegionTree.getChildren().isEmpty()) {
stationRegionTree.getChildren().clear();
}
}
}else {
byRegionId = stationRegionTreeDao.findByRegionId(regionId);
for(StationRegionTree stationRegionTree : byRegionId) {
if(stationRegionTree.getChildren() != null && !stationRegionTree.getChildren().isEmpty()) {
stationRegionTree.getChildren().clear();
}
}
}
return byRegionId;
}
}
4、Dao:
@Query(value = "select * from b_stationregiontree s where :stationIds in (select distinct b.s_id from b_stationregiontree a left join b_station_dict b on a.station_code = b.s_station_telecode) and parent_id = '1'",nativeQuery = true)
List<StationRegionTree> findStationByIds(@Param("stationIds") List<String> stationIds);
@Query(value = "select s.parent_id from b_stationregiontree s where :stationIds in (select distinct b.s_id from b_stationregiontree a left join b_station_dict b on a.station_code = b.s_station_telecode) and s.id = :regionId" ,nativeQuery = true)
String findParentId(@Param("regionId") String regionId, @Param("stationIds") List<String> stationIds);
@Query("select s from StationRegionTree s where s.parent = :regionId")
List<StationRegionTree> findByPid(@Param("regionId") String regionId);
@Query("select s from StationRegionTree s where s.id = :regionId")
List<StationRegionTree> findByRegionId(@Param("regionId") String regionId);
5、Entity:
package com.cars.ict.rbpsems.entity.controlpisled;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Data;
import javax.persistence.*;
import java.util.Set;
@Entity
@Table(name = "b_stationregiontree")
@JsonIgnoreProperties(value = {"hibernateLazyInitializer", "handler", "fieldHandler"})
public class StationRegionTree {
// @Id
// @SequenceGenerator(name="seq",sequenceName="oracle_seq")
// @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="seq")
// private long id;
@Id
@Column(length = 20, name = "id")
private String id;
@Column(length = 20, name = "rela_tree_id")
private String relaTreeId;
// @JsonIgnore
// @ManyToOne(fetch = FetchType.LAZY)
@Column(name = "parent_id")
private String parent;
@OneToMany(fetch = FetchType.EAGER, mappedBy = "parent")
//@OrderBy(value = "code asc")
private Set<StationRegionTree> children;
@Column(length = 20, name = "inf_value")
private String infValue;
@Column(length = 500, name = "inf_code")
private String infCode;
@Column(length = 2, name = "inf_type")
private String infType;
@Column(length = 20, name = "inf_name")
private String infName;
@Column(length = 20, name = "station_name")
private String stationName;
@Column(length = 100, name = "inf_mark")
private String infMark;
@Column(length = 1, name = "rela_tree_state")
private String relaTreeState;
@Column(length = 20, name = "station_code")
private String stationCode;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getRelaTreeId() {
return relaTreeId;
}
public void setRelaTreeId(String relaTreeId) {
this.relaTreeId = relaTreeId;
}
public String getParent() {
return parent;
}
public void setParent(String parent) {
this.parent = parent;
}
public Set<StationRegionTree> getChildren() {
return children;
}
public void setChildren(Set<StationRegionTree> children) {
this.children = children;
}
public String getInfValue() {
return