宿舍树结构
作者:互联网
// service 代码
public RestResponse<JSONObject> getDormTreeByWorkshop( @RequestParam("workshopId") String workshopId,@RequestParam("sex") String sex) { List<Map> dormitoryAllList = iDormManageDao.getDormitoryBydept(workshopId,sex); //1. 所有区域 JSONArray areas = new JSONArray(); //1.1 所有区域id List<String> areaIdList = new LinkedList<>(); //2. 所有楼 JSONArray floors = new JSONArray(); //2.1 所有楼id List<String> floorIdList = new LinkedList<>(); //1. 所有房间 JSONArray rooms = new JSONArray(); //1.1 所有房间id List<String> roomIdList = new LinkedList<>(); //1. 所有床位 JSONArray beds = new JSONArray(); //1.1 所有床位id List<String> bedIdList = new LinkedList<>(); //1.2 循环把id放进区域id中 for (Map map1 : dormitoryAllList) { Optional<String> optional = areaIdList.stream().filter(e -> e.equals( map1.get("areaId"))).findFirst(); if (!optional.isPresent()){ areaIdList.add((String) map1.get("areaId")); JSONObject area = new JSONObject(); area.put("value",map1.get("areaId")); area.put("label",map1.get("area")); //1.3 相对应的区域信息也放进所有区域中 areas.add(area); } Optional<String> optional1 = floorIdList.stream().filter(e -> e.equals( map1.get("lId"))).findFirst(); if (!optional1.isPresent()){ floorIdList.add((String) map1.get("lId")); JSONObject floor = new JSONObject(); floor.put("value",map1.get("lId")); floor.put("parentId",map1.get("areaId")); floor.put("label",map1.get("lmc")); //1.3 相对应的区域信息也放进所有区域中 floors.add(floor); }Optional<String> optional2 = roomIdList.stream().filter(e -> e.equals( map1.get("fjId"))).findFirst(); if (!optional2.isPresent()){ roomIdList.add((String) map1.get("fjId")); JSONObject room = new JSONObject(); room.put("value",map1.get("fjId")); room.put("parentId",map1.get("lId")); room.put("label",map1.get("fjmc")); //1.3 相对应的区域信息也放进所有区域中 rooms.add(room); }Optional<String> optional3 = bedIdList.stream().filter(e -> e.equals( map1.get("bedId"))).findFirst(); if (!optional3.isPresent()){ bedIdList.add((String) map1.get("bedId")); JSONObject bed = new JSONObject(); bed.put("value",map1.get("bedId")); bed.put("parentId",map1.get("fjId")); bed.put("label",map1.get("bedName")); //1.3 相对应的区域信息也放进所有区域中 beds.add(bed); } } List<JSONObject> areaList = JSONObject.parseArray(areas.toJSONString(), JSONObject.class); List<JSONObject> floorList = JSONObject.parseArray(floors.toJSONString(), JSONObject.class); List<JSONObject> roomsList = JSONObject.parseArray(rooms.toJSONString(), JSONObject.class); List<JSONObject> bedsList = JSONObject.parseArray(beds.toJSONString(), JSONObject.class); try { //循环房间 和 床位的关系
//先循环大的再循环小的 for (JSONObject room : roomsList) { ArrayList<JSONObject> bedjson = new ArrayList<>(); for (JSONObject bed : bedsList) { if (bed.getString("parentId").equals(room.getString("value"))){ bedjson.add(bed); } } room.put("children",bedjson); } //楼 和 房间的关系 for (JSONObject floor : floorList) { ArrayList<JSONObject> romjson = new ArrayList<>(); for (JSONObject rom : roomsList) { if (rom.getString("parentId").equals(floor.getString("value"))){ romjson.add(rom); } } floor.put("children",romjson); } //区域 和 楼的关系 for (JSONObject area : areaList) { ArrayList<JSONObject> floorjson = new ArrayList<>(); for (JSONObject floor : floorList) { if (floor.getString("parentId").equals(area.getString("value"))){ floorjson.add(floor); } } area.put("children",floorjson); } }catch (Exception e){ e.printStackTrace(); } return new RestResponse(areaList); }
数据库sql
SELECT q.mc area, q.id areaId, l.id lId, l.mc lmc, f.id fjId, f.mc fjmc, c.bh, c.id, f.sslb, f.cjid FROM t_sys_sscwxx c INNER JOIN t_sys_ssfjxx f ON c.bh LIKE concat ( f.bh, '%' ) INNER JOIN t_sys_sslxx l ON f.bh LIKE concat ( l.bh, '%' ) INNER JOIN t_sys_ssxx q ON l.bh LIKE concat ( q.bh, '%' ) WHERE c.zt = 0 AND f.sslb= '女' AND f.cjid= '12' /*部门ID*/
数据库结果集
标签:get,JSONObject,树结构,add,宿舍,map1,put,new 来源: https://www.cnblogs.com/lilulin/p/15785663.html