package com.pty.search.service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.stream.Collectors;
import org.frameworkset.elasticsearch.boot.BBossESStarter;
import org.frameworkset.elasticsearch.client.ClientInterface;
import org.frameworkset.elasticsearch.entity.RestResponse;
import org.frameworkset.elasticsearch.entity.SearchHit;
import org.frameworkset.elasticsearch.entity.SearchHits;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.pty.pub.common.util.StringUtil;
import com.pty.search.api.SearchObjectService;
import com.pty.search.api.SearchService;
import com.pty.search.api.SearchTemplateService;
import com.pty.search.common.SearchConstants;
import com.pty.search.common.util.SearchUtils;
import com.pty.search.dao.SearchObjectDao;
@Service
public class SearchServiceImpl implements SearchService {
@Autowired
private BBossESStarter bbossESStarter;
@Autowired
private SearchTemplateService searchTemplateService;
@Autowired
private SearchObjectDao searchObjectDao;
@Override
public List<Map> searchAll(String content, List roles, String departmentCode, String agyCode) {
ClientInterface clientUtil = bbossESStarter.getConfigRestClient("com/pty/search/dao/wholeSearch.xml");
List<Map> list = new ArrayList<>();
List<Map> listEmployee = new ArrayList<Map>();
Map<String, Object> params = new HashMap<String, Object>();
params.put("content", content);
do {
RestResponse searchResult = clientUtil.search("_search", "searchAll", params, Map.class);
SearchHits searchHits = searchResult.getSearchHits();
List<SearchHit> listHit = searchHits.getHits();
Map<String, List<SearchHit>> mapList = listHit.stream().collect(Collectors.groupingBy(SearchHit::getIndex));
for (Entry<String, List<SearchHit>> entry : mapList.entrySet()) {
//暂时不启用模板设置
if (entry.getKey().equals(SearchConstants.MADEMPLOYEE)) {
List<SearchHit> listSearchHit = entry.getValue();
List<String> listId = new ArrayList<String>();
List<String> listKey = new ArrayList<String>();
if (listSearchHit != null && listSearchHit.size() > 0) {
for (SearchHit searchHit : listSearchHit) {
Map mapObject = (Map) searchHit.getSource();
mapObject.forEach((key, value) -> {
listKey.add(key.toString());
});
String midId = (String) mapObject.get("MAD_ID");
listId.add(midId);
}
}
StringBuffer indexSql = new StringBuffer();
indexSql.append("select 1=1");
for (String key : listKey) {
indexSql.append("," + key + " as \"" + SearchUtils.underline2Camel(key.toLowerCase()) + "\"");
}
indexSql.append(" from mad_employee where 1=1 and is_deleted != 1").append(" and mad_id in (");
for (int i = 0; i < listId.size(); i++) {
if (i == (listId.size() - 1)) {
indexSql.append("'").append(listId.get(i)).append("')");
} else {
indexSql.append("'").append(listId.get(i)).append("',");
}
}
indexSql.append(" ORDER BY field(mad_id,");
for (int i = 0; i < listId.size(); i++) {
if (i == (listId.size() - 1)) {
indexSql.append("'").append(listId.get(i)).append("')");
} else {
indexSql.append("'").append(listId.get(i)).append("',");
}
}
if (StringUtil.isEmpty(indexSql.toString())) {
throw new RuntimeException();
}
listEmployee = searchObjectDao.getQueryResult(indexSql.toString());
List<Map> listUrl = new ArrayList<Map>();
Map<String, String> mapType = new HashMap<String, String>();
mapType.put("人员信息", "/mad/mad-department");
mapType.put("借款情况", "/pex/pex-ops-work-entry");
mapType.put("报销记录", "/pex/pex-ops-work-entry");
listUrl.add(mapType);
listEmployee.forEach(e -> {
e.put("title", e.get("madName"));
e.put("searchType", SearchConstants.EMPLOYEE);
e.put("menuUrl", "/mad/mad-department");
e.put("menuParam", "");
e.put("listUrl", mapType);
});
}
}
break;
} while (true);
return listEmployee;
}
}
设计模式:动态拼接sql之动态查询属性字段
最新推荐文章于 2024-07-07 14:25:27 发布