package com.zhiwei.util;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TimeZone;

import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;

import weixin.popular.bean.menu.selfmenu.News;

/**
 * es工具类
 * 
 * @author admin
 */
public class ESUtil {
	private static final Logger log = LoggerFactory.getLogger(ESUtil.class);
	private static String indexName;
	private static String type;
	private static String endWithTime = "T01:00:00.000Z";

	static {
		indexName = Config.getVal("indexName");
		type = Config.getVal("type");
	}

	/**
	 * 获取匹配时起始时间
	 * 
	 * @param hour
	 * @return
	 */
	public static String getTimeMin(int hour) {
		SimpleDateFormat smf = new SimpleDateFormat("yyyy-MM-dd");
		Date date1 = new Date();
		String time = "";
		if (hour == 9) {
			date1.setDate(date1.getDate() - 1);
			time = smf.format(date1) + "T08:00:00.000Z";
		}
		if (hour == 12) {
			time = smf.format(date1) + "T01:00:00.000Z";
		}
		if (hour == 16) {
			time = smf.format(date1) + "T04:00:00.000Z";
		}
		return time;
	}

	public static String getWedTimeByCj(int hour) {
		SimpleDateFormat smf = new SimpleDateFormat("yyyy-MM-dd");
		Date date1 = new Date();
		String time = "";
		date1.setDate(date1.getDate() - 1);
		time = smf.format(date1) + "T08:00:00.000Z";
		return time;
	}

	/**
	 * 获取匹配时终止时间
	 * 
	 * @param hour
	 * @return
	 */
	public static String getTimeMax(int hour) {
		SimpleDateFormat smf = new SimpleDateFormat("yyyy-MM-dd");
		Date date = new Date();
		String time = "";
		time = smf.format(date) + "T0" + (hour - 8) + ":00:00.000Z";
		return time;
	}

	
//	public List<NewsResult> getNewsForWechatWithMechanism(Date start,Date end){
//		SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd日HH:mm:ss");
//		Calendar startTime=Calendar.getInstance();
//		startTime.setTime(start);
//		startTime.add(Calendar.HOUR, -8);
//		Calendar endTime=Calendar.getInstance();
//		endTime.setTime(end);
//		endTime.add(Calendar.HOUR, -8);
//		List<Mechanism> list = new ReadExcel()
//				.getInstitution(SendEmail.class.getClassLoader().getResource("files/jrjg.xls").getPath());
//		List<NewsResult> mechanism = new ArrayList<NewsResult>();
//		try {
//			log.info("金融检索开始");
//			Client esClient = ESClient.getInstance();
//			List<String> snByNeedFull = excelUtil
//					.getOrgName(ESUtil.class.getClassLoader().getResource("files/matchOrgName.xls").getPath());
//			Map<String, String> maps = new HashMap<String, String>();
//			for (String sn : snByNeedFull) {// 需要全称匹配的
//				maps.put(sn, "yes");
//			}
//			for (Mechanism scan : list) {
//				// 搜索数据
//				SearchRequestBuilder srb = esClient.prepareSearch(indexName).setTypes(type);
//				BoolQueryBuilder bb = new BoolQueryBuilder();
//				if (maps.get(scan.getShortName()) != null) {
//					if (scan.getFullName() == null) {
//						continue;
//					}
//				} else {
//					QueryBuilder matchQuery = QueryBuilders.matchPhraseQuery("title", scan.getShortName());
//					QueryBuilder matchQuery1 = QueryBuilders.matchPhraseQuery("content", scan.getShortName());
//					bb.should(matchQuery);
//					bb.should(matchQuery1);
//				}
//				if (scan.getFullName() != null) {
//					QueryBuilder matchQuery2 = QueryBuilders.matchPhraseQuery("content", scan.getFullName());
//					bb.should(matchQuery2);
//				}
//				srb.setQuery(bb);
//				srb.setPostFilter(QueryBuilders.rangeQuery("time").gte(sdf.format(startTime.getTime()).replace("日", "T")+".000Z")
//						.lte(sdf.format(endTime.getTime()).replace("日", "T")+".000Z"));
//				srb.setSize(500);
//				SearchResponse response = srb.execute().actionGet();
//				SearchHits searchHits = response.getHits();
//				for (SearchHit hit : searchHits.getHits()) {
//					Map<String, Object> map = hit.getSource();
//					NewsResult n = new NewsResult();
//					n.setCode(scan.getCodeAndP());
//					n.setShortName(scan.getShortName());
//					String url = hit.getSource().get("url") != null ? String.valueOf(hit.getSource().get("url"))
//							: String.valueOf(hit.getId());
//					n.setUrl(url);
//					n.setRsid(String.valueOf(map.get("rsid")));
//					n.setTitle(String.valueOf(map.get("title")));
//					n.setContent(String.valueOf(map.get("content")));
//					SimpleDateFormat myFmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//					try {
//						Calendar cal = getStringToCal(map.get("time").toString());
//						Date date = new Date(cal.getTimeInMillis());
//						n.setTime(myFmt.format(date));
//					} catch (Exception e) {
//						n.setTime("");
//					}
//					n.setPt(String.valueOf(map.get("pt")));
//					n.setSource(String.valueOf(map.get("source")));
//					n.setOrgChiName(scan.getFullName() == null ? "" : scan.getFullName());
//					n.setType(scan.getType());
//					mechanism.add(n);
//				}
//			}
//			mechanism = getFmNews(mechanism);
//			log.info("金融结束");
//		} catch (Exception e) {
//			log.error("es检索出错，错误信息【{}】", e);
//		}
//		return mechanism;
//	}
}
