Java开发笔记12 (树的简单的增删改 & 树的生成 & 树的列表展示)

(1)增

@PostMapping("/add")
public Result save(@RequestBody StationRegionTree tree){
try{
tree.setId(UUID.randomUUID().toString().trim().replaceAll("_",""));
stationRegionTreeService.add(tree);
return Result.ok();
}catch (Exception e) {
e.printStackTrace();
return Result.error(e);
}
}
void add(StationRegionTree tree);
@Override
public void add(StationRegionTree tree) {
stationRegionTreeDao.save(tree);
}

(2)删

@PostMapping("/deleteTree/{id}")
public Result delate(@PathVariable("id") long id){
try{
stationRegionTreeService.delateTree(id);
return Result.ok();
}catch (Exception e) {
e.printStackTrace();
return Result.error(e);
}
}


void delateTree(long id);

@Override
public void delateTree(long id) {
stationRegionTreeDao.deleteTreeById(id);
}

@Transactional
@Modifying
@Query(value = "delete from StationRegionTree s where s.id = :id")
void deleteTreeById(@Param("id") long id);
 

(3)改

@PostMapping("/updateTree")
public Result update(@RequestBody StationRegionTree tree){
try{
stationRegionTreeService.updateTree(tree);
return Result.ok();
}catch (Exception e) {
e.printStackTrace();
return Result.error(e);
}
}

void updateTree(StationRegionTree tree);


@Override
public void updateTree(StationRegionTree tree) {
stationRegionTreeMapper.updateTreeById(tree);
}

void updateTreeById(StationRegionTree tree);




(4)树的生成(只生成到二级)

@PostMapping("/regionTree")
public Result list(String stationId) {
Set<String> stationIds;
if (org.apache.commons.lang3.StringUtils.isBlank(stationId)) {
stationIds = this.getStationIds();
} else {
stationIds = Sets.newHashSet();
stationIds.add(stationId);
}
return stationRegionTreeService.regionTreeList(stationIds);
}

Result regionTreeList(Set<String> stationIds);

    @Override
public Result regionTreeList(Set<String> stationIds) {
Set<StationRegionTree> allList = stationRegionTreeDao.findByStationIds(stationIds); //StationRegionTree
//创建一个list集合,用于存放根节点
List<NewSRTreeVo> parent = new ArrayList<>();
NewSRTreeVo newSRTreeVo = new NewSRTreeVo();
try {
for (StationRegionTree person : allList) {
//如果parentId'1',则代表是根节点,存入到集合中
// if (person.getParent().equals("1")) {
if (person.getParent().equals("1")) {
// //虚拟字段spreadtrue时表示默认展开
// person.setSpread("true");
// //虚拟字段checkedtrue时表示默认选中
// person.setChecked("true");
newSRTreeVo.setId(person.getId());
newSRTreeVo.setText(person.getInfName());
Map<String, String> userdata = new HashMap<>();
userdata.put("relaTreeId",person.getRelaTreeId());
userdata.put("parent",person.getParent());
userdata.put("infValue",person.getInfValue());
userdata.put("rela",person.getInfCode());
userdata.put("infType",person.getInfType());
userdata.put("infName",person.getInfName());
userdata.put("infMark",person.getInfMark());
userdata.put("relaTreeState",person.getRelaTreeState());
userdata.put("stationCode",person.getStationCode());
//----------------------------
Set<StationRegionTree> children = person.getChildren();
// HashSet<NewSRTreeVo> newSRTreeVos = new HashSet<>();
Set<NewSRTreeVo> collect = children.stream().map(s -> {
s.setChildren(null);
NewSRTreeVo tempVO = new NewSRTreeVo();
tempVO.setId(s.getId());
tempVO.setText(s.getInfName());
Map<String, String> userdata2 = new HashMap<String, String>();
userdata2.put("relaTreeId", s.getRelaTreeId());
userdata2.put("parent", s.getParent());
userdata2.put("infValue", s.getInfValue());
userdata2.put("rela", s.getInfCode());
userdata2.put("infType", s.getInfType());
userdata2.put("infName", s.getInfName());
userdata2.put("infMark", s.getInfMark());
userdata2.put("relaTreeState", s.getRelaTreeState());
userdata2.put("stationCode", s.getStationCode());
tempVO.setUserdata(userdata2);
return tempVO;
}).collect(Collectors.toSet());
//----------------------------

newSRTreeVo.setItems(collect);

parent.add(newSRTreeVo);
}
}
return Result.ok(parent);
}catch(Exception e) {
e.printStackTrace();
System.out.println("stationRegionTree表中的parentId值不能为空");
}
//查找根节点下的子节点(只查到二级子节点),因为根节点的id就是子节点的parentId;
// for (NewSRTreeVo person : parent) {
// Set<NewSRTreeVo> children = getRegionChild(String.valueOf(person.getId()), allList);
// person.setChildren(children);
// }
// Set<NewSRTreeVo> items = parent.get(0).getItems();
// List arrayList = new ArrayList<String>();
// for (NewSRTreeVo item : items) {
// String infValue = item.getUserdata().get("infValue");
// arrayList.add(infValue);
// }
return Result.error();
}

//获取所有二级区域的方法
// public List<String> getSRegionlist(Set<String> stationIds) {
public Result getSRegionlist(Set<String> stationIds) {
Result result = regionTreeList(stationIds);
List<NewSRTreeVo> parent = (List<NewSRTreeVo>) result.getData();
Set<NewSRTreeVo> items = parent.get(0).getItems();
List arrayList = new ArrayList<String>();
for (NewSRTreeVo item : items) {
String infValue = item.getUserdata().get("infName");
arrayList.add(infValue);
}
return Result.ok(arrayList);
}



//查找子节点的方法
// private Set<NewSRTreeVo> getRegionChild(String id, Set<NewSRTreeVo> allList) {
// //存放子节点的集合
// Set<NewSRTreeVo> children = new HashSet<>();
// for (NewSRTreeVo person : allList) {
// //如果根节点的id等于集合内parentId,说明是根节点的子节点
// if (person.getUserdata().get("parentId").equals(id)) {
// //虚拟字段checkedtrue时表示默认选中
//// person.setChecked("true");
//
// children.add(person);
// }
// }
// return children;
// }

@Query(value = "select r.* from b_stationregiontree r left join b_station_dict b on r.station_name = b.s_station_name where b.s_id\n" +
"in (:stationIds)", nativeQuery = true)
Set<StationRegionTree> findByStationIds(@Param("stationIds") Set<String> stationIds);
 
 

(5)树的列表展示

    @GetMapping("/list-by-parentId")
public Result listByParentId(String regionId) {
// String regionId = map.get("regionId");
//获取用户有权限查看的车站ID集合
Set<String> stationIds = this.getStationIds();
List<String> ids = new ArrayList<>(stationIds);
List<StationRegionTree> stationRegionTrees = stationRegionTreeService.listByParent(regionId, ids);
HashMap<Object, List<StationRegionTree>> map = new HashMap<>();
map.put("content",stationRegionTrees);
return Result.ok(map);
}
List<StationRegionTree> listByParent(String regionId, List<String> ids);
@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;
}
}
@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);

 

posted @   sensen~||^_^|||&  阅读(145)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 记一次.NET内存居高不下排查解决与启示
点击右上角即可分享
微信分享提示