设计模式:动态拼接sql之动态查询属性字段

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;
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值