package com.zhiweidata.bjfzxWebapp.esTest;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.RangeQueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.sort.SortBuilder;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
import org.junit.Test;

import com.zhiweidata.bjfzxWebapp.ObjectTest.ObjectTest;
import com.zhiweidata.bjfzxWebapp.conf.EsConfig;
import com.zhiweidata.bjfzxWebapp.entity.WeiboBusiness;
import com.zhiweidata.bjfzxWebapp.esDao.ESClient;
import com.zhiweidata.bjfzxWebapp.esDao.MediaESDAO;
import com.zhiweidata.bjfzxWebapp.esDao.WechatESDAO;
import com.zhiweidata.bjfzxWebapp.esDao.WeiboESDAO;
import com.zhiweidata.bjfzxWebapp.esDao.impl.MediaESDAOImpl;
import com.zhiweidata.bjfzxWebapp.esDao.impl.WechatESDAOImpl;
import com.zhiweidata.bjfzxWebapp.esDao.impl.WeiboESDAOImpl;
import com.zhiweidata.bjfzxWebapp.util.ESQueryUtil;
import com.zhiweidata.bjfzxWebapp.util.TimeUtil;

public class EsTest extends ObjectTest {

	WeiboESDAO weiboESDAO = new WeiboESDAOImpl();
	WechatESDAO wechatESDAO = new WechatESDAOImpl();
	MediaESDAO mediaESDAO = new MediaESDAOImpl();
	TransportClient transportClient = new ESClient().getInstance();
	@Test
	public void esTest() {
		
		String start ="2018-04-01 00:00";
		String end ="2018-04-12 00:00";
		String groupName = "区块链";
		String key ="区块链";
		key = key.replaceAll("\\+", " ");
		key = key.replaceAll("\\-", " ");
		String[] keys = key.split(" ");
		Date startTime = TimeUtil.parseTime(start, "yyyy-MM-dd HH:mm");
        Date endTime = TimeUtil.parseTime(end, "yyyy-MM-dd HH:mm");
        List<String> indexs = ESClient.eSIndexes.getIndexes(
                EsConfig.es_weibo_indexName, startTime, endTime);
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        boolQueryBuilder.must(new RangeQueryBuilder("time").gt(start).lte(end));
        boolQueryBuilder.should(ESQueryUtil.matchPhraseQueryWords(
            QueryBuilders.boolQuery(), "must", "text", keys));
        boolQueryBuilder.should(ESQueryUtil.matchPhraseQueryWords(
            QueryBuilders.boolQuery(), "must", "roottext", keys));
		SortBuilder sortBuilder = SortBuilders.fieldSort("time")
              .order(SortOrder.DESC);
		
		 SearchRequestBuilder searchRequestBuilder = transportClient
	                .prepareSearch(indexs.toArray(new String[indexs.size()]));// 指定查询库
		 searchRequestBuilder.setTypes(EsConfig.es_weibo_type);
		 searchRequestBuilder.setQuery(boolQueryBuilder);

		 //		 searchRequestBuilder.addSort(sortBuilder);
//		 searchRequestBuilder.setPostFilter(QueryBuilders.rangeQuery("time")
//	                .from(startTime).to(endTime));
		 
		 System.out.println(searchRequestBuilder);
		 
		 
		 List<WeiboBusiness> weibos = findSearchWeibo(searchRequestBuilder, groupName, key);
		System.out.println(weibos.size());
	}
	
	@Test
	public void findTest() {
		String start ="2018-04-01 00:00";
		String end ="2018-04-11 00:00";
		String groupName = "区块链";
		String key ="阿里";
		key = key.replaceAll("\\+", " ");
		key = key.replaceAll("\\-", " ");
		String[] keys = key.split(" ");
		
		try {
			
			List<WeiboBusiness> weibos = weiboESDAO.findByTimeBean(start, end, groupName, key, keys);
			for (WeiboBusiness weiboBusiness : weibos) {
				System.out.println(weiboBusiness);
			}
			System.out.println(weibos.size());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	
	}

	
	public List<WeiboBusiness> findSearchWeibo(SearchRequestBuilder searchRequestBuilder,String groupName,String key) {
		List<WeiboBusiness> hits = new ArrayList<WeiboBusiness>();
		SearchResponse response = searchRequestBuilder.setSize(1).execute().actionGet();
		SearchHits searchHits = response.getHits();// 获取结果，初次只拿总数
		long count = searchHits.getTotalHits();
		int size = 500;// 500不怎么会漏数据
		response = searchRequestBuilder.setSize(size).setScroll(TimeValue.timeValueMillis(10000)).execute().actionGet();
		System.out.println(response);
		searchHits = response.getHits();// 获取结果
		if (searchHits.getHits() != null && searchHits.getHits().length > 0) {
			hits.addAll(ResultHis2Weibo(Arrays.asList(searchHits.getHits()), groupName, key));
		}

		long sum = searchHits.hits().length;
		long addNum;
		String scrollId = "";
		for (int i = 0; sum < count; i++) {
			scrollId = response.getScrollId();// 获取查询记录点，相当于游标
			response = transportClient.prepareSearchScroll(scrollId)
					// 直接由断点继续获取
					.setScroll(TimeValue.timeValueMillis(80000)).execute().actionGet();
			searchHits = response.getHits();
			addNum = searchHits.hits().length;
			if (searchHits.getHits() != null && searchHits.getHits().length > 0) {
				hits.addAll(ResultHis2Weibo(Arrays.asList(searchHits.getHits()), groupName, key));
			}
			sum += addNum;
			if (addNum <= 0) {
				break;
			}
		}
		return hits;
	}
	
	private List<WeiboBusiness> ResultHis2Weibo(List<SearchHit> hits, String groupName, String key) {
		List<WeiboBusiness> list = new ArrayList<>();
		try {
			if (hits.size() <= 0) {
				return null;
			}
			for (SearchHit hit : hits) {
				Map<String, Object> news = hit.getSource();

				WeiboBusiness weiboBusiness = new WeiboBusiness();
				weiboBusiness.setId(key + news.get("id"));
				weiboBusiness.setKeyword(key);
				weiboBusiness.setUid(news.get("user_id").toString());
				weiboBusiness.setUsername(news.get("username").toString());
				weiboBusiness.setVtype(0);
				weiboBusiness.setFensi(0);
				weiboBusiness.setGuanzhu(0);
				weiboBusiness.setWeibo(0);
				weiboBusiness.setSex(news.get("sex").toString());
				weiboBusiness.setLocation(news.get("location").toString());
				weiboBusiness.setDescription(news.get("description").toString());
				weiboBusiness.setReason("");
				weiboBusiness.setImg_url(news.get("weibo_img").toString());
				weiboBusiness.setMid(news.get("id").toString());
				weiboBusiness.setReposts_count(0);
				weiboBusiness.setComments_count(0);
				weiboBusiness.setHdl_count(0);
				weiboBusiness.setUrl(news.get("url").toString());
				weiboBusiness.setText(news.get("text").toString());

				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
				weiboBusiness.setCreatedAt(sdf.parse(news.get("time").toString()));
				weiboBusiness.setSource(news.get("source").toString());
				weiboBusiness.setIsForword((Integer) news.get("isForword"));
				weiboBusiness.setSaveTime(new Date().getTime());
				weiboBusiness.setGroupName(groupName);

				list.add(weiboBusiness);
			}
		} catch (Exception e) {
			return list;
		}
		return list;
	}
}
