/** 
 * @Title: ESThreeDataServiceImpl.java 
 * @Package com.zhiweidata.bjfzxWebapp.service.Impl 
 * @Description: TODO(用一句话描述该文件做什么) 
 * @author xuyimeng 
 * @date 2018年2月7日 下午4:58:24 
 * @version V1.0 
 */
/**
* 
*/
package com.zhiweidata.bjfzxWebapp.service.Impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.elasticsearch.search.SearchHit;

import com.zhiweidata.bjfzxWebapp.entity.Keyword;
import com.zhiweidata.bjfzxWebapp.entity.MediaAndWechatEvent;
import com.zhiweidata.bjfzxWebapp.entity.WeiboBusiness;
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.service.ESThreeDataService;
import com.zhiweidata.bjfzxWebapp.util.TimeUtil;

/**
 * @ClassName: ESThreeDataServiceImpl
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author xuyimeng
 * @date 2018年2月7日 下午4:58:24
 */
public class ESThreeDataServiceImpl implements ESThreeDataService {
	WeiboESDAO weiboESDAO = new WeiboESDAOImpl();
	WechatESDAO wechatESDAO = new WechatESDAOImpl();
	MediaESDAO mediaESDAO = new MediaESDAOImpl();

	@Override
	public List<WeiboBusiness> findWeibo(Date start, Date end, Keyword keyword) {
		try {
			String groupName = keyword.getGroupName();
			String key = keyword.getKeyword();
			key = key.replaceAll("\\+", " ");
			key = key.replaceAll("\\-", " ");
			String[] keys = key.split(" ");

			String startTime = TimeUtil.formatTime(start);
			String endTime = TimeUtil.formatTime(end);

//			return ResultHis2Weibo(weiboESDAO.findByTime(startTime, endTime, keys), groupName, key);
			return weiboESDAO.findByTimeBean(startTime, endTime, groupName, key, keys);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return null;
	}

	@Override
	public List<MediaAndWechatEvent> findMediaAndWechat(Date start, Date end, Keyword keyword) {
		List<MediaAndWechatEvent> list = new ArrayList<>();
		try {
			String groupName = keyword.getGroupName();
			String key = keyword.getKeyword();
			key = key.replaceAll("\\+", " ");
			key = key.replaceAll("\\-", " ");
			String[] keys = key.split(" ");
			String startTime = TimeUtil.formatTime(start);
			String endTime = TimeUtil.formatTime(end);

//			List<SearchHit> result = wechatESDAO.findByTime(startTime, endTime, keys);
//			list.addAll(ResultHis2Wechat(result, groupName, key));
//			List<SearchHit> result2 = mediaESDAO.findByTime(startTime, endTime, keys);
//			list.addAll(ResultHis2Media(result2, groupName, key));
			list.addAll(wechatESDAO.findByTimeBean(startTime, endTime, groupName, key, keys));
			list.addAll(mediaESDAO.findByTimeBean(startTime, endTime, groupName, key, keys));
			
			return list;
		} catch (Exception e) {

		}
		return null;
	}

	private List<WeiboBusiness> ResultHis2Weibo(List<SearchHit> hits, String groupName, String key) {
		List<WeiboBusiness> list = new ArrayList<>();
		try {
			if (hits.size() <= 0) {
				return null;
			}
			WeiboBusiness weiboBusiness;
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
			Map<String, Object> news ;
			for (SearchHit hit : hits) {
				news = hit.getSource();
				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());
				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;
	}

	private List<MediaAndWechatEvent> ResultHis2Media(List<SearchHit> hits, String groupName, String key) {
		List<MediaAndWechatEvent> list = new ArrayList<>();
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS Z");

			if (hits.size() <= 0) {
				return null;
			}
			for (SearchHit hit : hits) {
				Map<String, Object> news = hit.getSource();

				MediaAndWechatEvent event = new MediaAndWechatEvent();
				event.set_id(news.get("id") + "_" + key);
				event.setUrl(news.get("id").toString());
				event.setTitle(news.get("title").toString());

				String date = news.get("time").toString().replaceAll("Z", " UTC");
				Date d = sdf.parse(date);
				event.setTime(d);

				event.setContent(news.get("content").toString());
				event.setSource(news.get("source").toString());
				event.setPt("百度新闻");
				event.setType("百度新闻");
				event.setSavetime(new Date().getTime());
				event.setGroupName(groupName);
				event.setKeyword(key);

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

	private List<MediaAndWechatEvent> ResultHis2Wechat(List<SearchHit> hits, String groupName, String key) {
		List<MediaAndWechatEvent> list = new ArrayList<>();

		try {
			String content;
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS Z");
			for (SearchHit hit : hits) {
				Map<String, Object> news = hit.getSource();
				MediaAndWechatEvent event = new MediaAndWechatEvent();
				event.set_id(news.get("id") + "_" + key);
				event.setUrl(news.get("id").toString());
				event.setTitle(news.get("title").toString());

				String date = news.get("time").toString().replaceAll("Z", " UTC");
				Date d = sdf.parse(date);
				event.setTime(d);
				content = news.get("content").toString();
				content = content.length() > 100 ? content.substring(0, 100) : content;
				event.setContent(content);
				event.setSource(news.get("source").toString());
				event.setPt("微信");
				event.setType("微信");
				event.setSavetime(new Date().getTime());
				event.setGroupName(groupName);
				event.setKeyword(key);

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

}
