直接上代码:
/**
* 根据创建人部门id分组,使得一个共享部门一个树形结构
*
* @param bmShares
* @return {@link JSONArray}
*/
private JSONArray getShareBybmGroup(List<DghyYjhcHcxShare> bmShares) {
JSONArray array = new JSONArray();
//通过部门id分组
bmShares.stream().collect(Collectors.groupingBy(DghyYjhcHcxShare::getCreatedUnitId, Collectors.toList()))
.forEach((id, items) -> {
JSONObject result = new JSONObject();
result.put("bmid", id);
result.put("bmName", items.get(0).getCreatedUnitName());
result.put("datas", items);
array.add(result);
});
return array;
}