今天老大给了我一个需求,我项目里有个机构树,点击左侧的机构,必须显示它下面所有机构的用户信息,如下图所示:
我的思路是这样的
1.当用户点击左侧某一个节点时,传到后台的是一个当前节点id,然后用当前节点id去机构表查它下面的所有子机构(用递归方式),最后结果拼接成一个机构的字符串,
用逗号分隔,格式为a0001,10002,.....
2.查询用户表,把第一步获得的结果作为一个条件,进行select查询,sql格式为select * from 用户表 where .... and 用户的机构id in ('a0001','a0002',......);
具体代码如下:
1.
/**
* 获取机构列表
* @param param
* @param info
* @param Id
* @return
*/
public JsonData getOrgListdept(PaginationBeanParam param, SrapOrganInfo info,String Id) throws Exception{
List<Map<String,Object>> dataList = new ArrayList<Map<String, Object>>();
Integer count = 0;
//获取当前节点以及它所有的子节点数据
JsonData data = getOrdeptList(Id,dataList, count);
String organIdStr = "";
//解析JsonData,将所有的organ_id拼接成一个字符串
List<Map<String,Object>> organIdList = data.getResults();
if(organIdList.size() == 0){
return data;
}
for(Map<String,Object> map : organIdList){
String organid = (String)map.get("organid");
organIdStr+= "'"+organid+"'"+",";
}
organIdStr = organIdStr.substring(0,organIdStr.length()-1);
String sql = " select organ_id as organid, " +
" organ_cn_name as organcnname," +
" organ_en_name as organenname," +
" organ_contact as organcontact," +
" organ_tell as organtell," +
" adderss," +
" organ_scn_name as organscnname,"+
" area_code as areacode,"+
" parent_organ_id as parentorganid," +
"(select concat(parent_area_text,area_text) from srap_sys_area_code as area where area.area_code=srap_organ_info.area_code) as areacodetext,"+
" case when enable_flag=0 then '无效' else '有效' end as enableflag" +
" from srap_organ_info where 1=1";
if(info.getOrganId()!=null && !"".equals(info.getOrganId())){
sql += " and organ_id like '%:f_organ_id%' ";
param.setOtherParam("f_organ_id", info.getOrganId());
}
if(info.getOrganCnName()!=null && !"".equals(info.getOrganCnName())){
sql += " and organ_cn_name like '%:organ_cn_name%' ";
param.setOtherParam("organ_cn_name", info.getOrganCnName());
}
if(organIdStr.length() > 0){
sql += " and organ_id in ("+organIdStr+")";
}
param.setSql(sql);
JsonData jsonData = paginationTemplate.getJsonData(param);
return jsonData;
}
2.
/**
* 递归调用所有机构子节点
* @param Id
* @param dataList
* @param count
* @return
*/
public JsonData getOrdeptList(String Id, List<Map<String,Object>> dataList, Integer count) throws Exception{
PaginationBeanParam param = new PaginationBeanParam();
JsonData resultData = new JsonData();
String sql = " select organ_id as organid, " +
" organ_cn_name as organcnname," +
" parent_organ_id as parentorganid" +
" from srap_organ_info ";
String whereSql = "";
if(count == 0){
whereSql = " where organ_id <> '0' and organ_id= ':organ_id'";
param.setOtherParam("organ_id", Id);
}else{
whereSql = " where organ_id <> '0' and parent_organ_id= ':parent_organ_id'";
param.setOtherParam("parent_organ_id", Id);
}
sql+=whereSql+" order by organid";
param.setSql(sql);
JsonData data = paginationTemplate.getJsonData(param);
List<Map<String,Object>> resultList = data.getResults();
if(resultList.size()>0){
for(Map<String,Object> map : resultList){
dataList.add(map);
String organid = (String)map.get("organid");
count++;
getOrdeptList(organid, dataList, count);
}
}
resultData.setCount(count.longValue());
resultData.setMetaData(data.getMetaData());
resultData.setResults(dataList);
resultData.setSuccess(true);
return resultData;
}