地址树遍历
程序员文章站
2022-05-20 10:34:38
...
@RequestMapping(value = "/selectPatrolAddressTree", method = {RequestMethod.GET, RequestMethod.POST})
@ResponseBody
public String selectPatrolAddressTree(HttpServletRequest request, HttpServletResponse response) {
JSONObject json = new JSONObject();
JSONObject allJsonObject = new JSONObject();
JSONArray allArray = new JSONArray();
Long compId = RequestTool.getLong(request, "compId");
Long businessTypeId = RequestTool.getLong(request, "businessTypeId");
List<SysAddress> selectPatrolAddressList = null;
try {
SysABusinessTypeAddress aBusinessTypeAddress = this.getSysABusinessLineService().getSysABusinessTypeAddress(compId, businessTypeId);
if (aBusinessTypeAddress != null && aBusinessTypeAddress.getAddressId() != null) {
SysAddress paramAddress = new SysAddress();
paramAddress.setCompId(compId);
paramAddress.setrId(aBusinessTypeAddress.getAddressId());
selectPatrolAddressList = this.getSysAddressService().getSysAddressRidTreeList(paramAddress);
}
if (selectPatrolAddressList != null && selectPatrolAddressList.size() > 0) {
for (int i = 0; i < selectPatrolAddressList.size(); i++) {
if (selectPatrolAddressList.get(i).getpId() == null) {
JSONObject pJson = new JSONObject();
pJson.put("id", selectPatrolAddressList.get(i).getAddressId());
pJson.put("text", selectPatrolAddressList.get(i).getAddressName());
pJson.put("pId", selectPatrolAddressList.get(i).getpId());
JSONArray pJsonArray = new JSONArray();
this.getPatrolAddressTree(pJsonArray, pJson, selectPatrolAddressList, selectPatrolAddressList.get(i).getAddressId());
allArray.add(pJson);
}
}
}
} catch (Exception e) {
this.getLogErrorService().insertLogError(request,
Thread.currentThread().getStackTrace()[1].getClassName(),
Thread.currentThread().getStackTrace()[1].getMethodName(),
"巡检计划查询关联地址失败", e);
e.printStackTrace();
}
allJsonObject.put("address", allArray);
JsonUtils.err(ErrCodeEnum.SUCCESS, json);
json.put("data", allJsonObject);
return json.toString();
}
//pJson是父亲,pJsonArray是父亲的所有孩子
public void getPatrolAddressTree(JSONArray pJsonArray, JSONObject pJson, List<SysAddress> selectPatrolAddressList, Long pId) {
for (int i = 0; i < selectPatrolAddressList.size(); i++) {
if (selectPatrolAddressList.get(i).getpId() != null) {
if (pId.toString().equals(selectPatrolAddressList.get(i).getpId().toString())) {
JSONObject json = new JSONObject();
json.put("id", selectPatrolAddressList.get(i).getAddressId());
json.put("text", selectPatrolAddressList.get(i).getAddressName());
json.put("pId", selectPatrolAddressList.get(i).getpId());
JSONArray jsonArray = new JSONArray();
this.getPatrolAddressTree(jsonArray, json, selectPatrolAddressList, selectPatrolAddressList.get(i).getAddressId());
pJsonArray.add(json);
}
}
}
if (pJsonArray.size() > 0) {
pJson.put("children", pJsonArray);
}
}