package com.zhiweidata.bjfzxWebapp.service.Impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.mongodb.DBObject;
import com.zhiweidata.bjfzxWebapp.service.BjfzxInfoService;
import com.zhiweidata.bjfzxWebapp.service.WeiboAndMediaSerivce;
import com.zhiweidata.bjfzxWebapp.util.TreatOrder;

@Service
public class BjfzxInfoServiceImpl implements BjfzxInfoService{
	
	private static SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH");
	
	@Resource
	WeiboAndMediaSerivce weiboAndMediaSerivce;
	
	@Override
	public ArrayList<Map<String, Object>> getInfoList1ByTheme(String theme, Date date, String page,String groupName) {
		Integer pageint = Integer.parseInt(page)*6-6;
		List<Map<String, Object>> listsByThemeAndTime = new ArrayList<Map<String, Object>>();
		listsByThemeAndTime = weiboAndMediaSerivce.getInfoByThemeAndTime_1H(theme, date,groupName);
       
	    ArrayList<Map<String, Object>> lists1 = new ArrayList<Map<String, Object>>();
	    for (int j = 0; j < 6; j++) {
	    	if (j+pageint>=listsByThemeAndTime.size()) {
	    		break;
	    	}
	    	lists1.add(listsByThemeAndTime.get(j+pageint));
	    }
	    return lists1;
//	    return (ArrayList<Map<String, Object>>) TreatOrder.treatOrderByDateAsc(lists1);
		
	}

	@Override
	public ArrayList<Map<String, Object>> getInfolist6(String theme, Date date, String page, String groupName) 
			throws Exception {
		 
		Integer pageint = Integer.parseInt(page)*6-6;
		//转日历类进行处理
		Calendar calendar = Calendar.getInstance();		
		ArrayList<Map<String, Object>> info_6hours = new ArrayList<Map<String, Object>>();
		//6小时，获取每小时数据
		for (int i = 0; i < 6; i++) {
			Map<String, Object> map = new HashMap<String, Object>();
			calendar.setTime(date);
			calendar.add(Calendar.HOUR_OF_DAY, -i);
			Date time = calendar.getTime();
			List<Map<String, Object>> listsByThemeAndTime = new ArrayList<Map<String, Object>>();
			listsByThemeAndTime = weiboAndMediaSerivce.getInfoByThemeAndTime_1H(theme, time,groupName);			
			
		    ArrayList<Map<String, Object>> lists6 = new ArrayList<Map<String, Object>>();
		    //每小时添加6条
		    for (int j = 0; j < 6; j++) {
		    	if (j+pageint>=listsByThemeAndTime.size()) {
		    		break;
		    	}
		    	lists6.add(listsByThemeAndTime.get(j+pageint));
		    }
		    map.put("time",sdf2.format(time));
		    map.put("info",lists6);
		  
		    info_6hours.add(map);
		}
		
		return info_6hours;
//		return (ArrayList<Map<String, Object>>) TreatOrder.treatOrderByDateAsc(info_6hours);
	}

	@SuppressWarnings("unchecked")
	@Override
    public ArrayList<Map<String, Object>> getTop10(Date date,String groupName) throws ParseException {
		//获取数据（已降序排序）
		List<DBObject> list = weiboAndMediaSerivce.findAllFollowTime_XH(date,groupName);
		List<String> keywords = weiboAndMediaSerivce.getKeywordsByGroup(groupName);
		
		//对本地的mongo数据的累积
		Map<String, Integer> map = new HashMap<String, Integer>();
		for(String keyword :keywords)
		{
			map.put(keyword, 0);
		}
		for (DBObject dbObject : list) {
			String keyword = dbObject.get("keyword").toString();
			if (map.containsKey(keyword))
			{
				int count = map.get(keyword).intValue();
				count++;
				map.put(keyword, count);
			}
		}
		//获取关键词声量
		ArrayList<Map<String,Object>> Top = new  ArrayList<Map<String,Object>>();
		for (String key : map.keySet()) {
			Map<String,Object> word = new HashMap<String,Object>();
			word.put("name", key);
			word.put("value", Integer.parseInt(map.get(key).toString()));
			Top.add(word);
		}
		Top =  TreatOrder.treatOrderDESCByMap(Top, "value");
		ArrayList<Map<String,Object>> Top10 = new  ArrayList<Map<String,Object>>();
		
		for (Map<String,Object> map2 : Top) {
			if ((Integer)map2.get("value")>0)
			{
				Top10.add((Map<String,Object>) map2);
			}
			if (Top10.size()>=10) {
				break;
			}
		}		
		return Top10;
	}

	@Override
	public ArrayList<Map<String, Object>> getInfoList1ByWord(String keyWord, Date date, String page, String groupName) 
			throws Exception, ParseException {
		//每页6条，pageint为跳过的数据条数
		Integer pageint = Integer.parseInt(page)*6-6;
		List<Map<String, Object>> listsByThemeAndTime = new ArrayList<Map<String, Object>>();
		//获取一小时内有关的所有条数
		listsByThemeAndTime = weiboAndMediaSerivce.getInfoByKeywordAndTime(keyWord, date, groupName);
	    ArrayList<Map<String, Object>> lists1 = new ArrayList<Map<String, Object>>();
	    for (int j = 0; j < 6; j++) {
	    	if (j+pageint>=listsByThemeAndTime.size()) {
	    		break;
	    	}
	    	lists1.add(listsByThemeAndTime.get(j+pageint));
	    }
	    
		return lists1;
//	    return (ArrayList<Map<String, Object>>) TreatOrder.treatOrderByDateAsc(lists1);
	}

	@Override
	public ArrayList getInfolistByHour(String theme, Date date, String page, String groupName, int hourNum)
			throws Exception {
		 
		Integer pageint = Integer.parseInt(page)*6-6;
		//转日历类进行处理
		Calendar calendar = Calendar.getInstance();		
		ArrayList<Map<String, Object>> info_6hours = new ArrayList<Map<String, Object>>();
		//6小时，获取每小时数据
		for (int i = 0; i < hourNum; i++) {
			Map<String, Object> map = new HashMap<String, Object>();
			calendar.setTime(date);
			calendar.add(Calendar.HOUR_OF_DAY, -i);
			Date time = calendar.getTime();
			List<Map<String, Object>> listsByThemeAndTime = new ArrayList<Map<String, Object>>();
			listsByThemeAndTime = weiboAndMediaSerivce.getInfoByThemeAndTime_1H(theme, time,groupName);			
			
		    ArrayList<Map<String, Object>> lists6 = new ArrayList<Map<String, Object>>();
		    //每小时添加6条
		    for (int j = 0; j < 6; j++) {
		    	if (j+pageint>=listsByThemeAndTime.size()) {
		    		break;
		    	}
		    	lists6.add(listsByThemeAndTime.get(j+pageint));
		    }
		    map.put("time",sdf2.format(time));
		    map.put("info",lists6);
		  
		    info_6hours.add(map);
		}
		
		return info_6hours;
//		return (ArrayList<Map<String, Object>>) TreatOrder.treatOrderByDateAsc(info_6hours);
	}

	
	@Override
	public void removeError(List<Map<String, Object>> list) {
		for (int i=0;i<list.size();i++)
		{
			List<Map<String,Object>> obj = (List<Map<String, Object>>) list.get(i).get("info");
			
			if (obj == null)
			{
				obj = list;
			}
			
			Iterator<Map<String,Object>> it = obj.iterator();
			while(it.hasNext())
			{
				String context = "";
				Map<String,Object> map = it.next();
				String keyword = map.get("keywords").toString();
				if (map.containsKey("content"))
				{
					context += map.get("content").toString();
				}
				if (map.containsKey("title"))
				{
					context += map.get("title").toString();
				}
				
				if (keyword.contains("+"))
				{
					keyword = keyword.replaceAll("\\+", " ");
				}
				if (keyword.contains("-"))
				{
					keyword = keyword.replaceAll("\\-", " ");
				}
				
				String[] keywords = keyword.split("\\s+");
				
				String str = ".*";
				for (String key : keywords)
				{
					str += key + ".*";
				}
				Pattern pattern = Pattern.compile(str);
				
				if (!pattern.matcher(context).find())
				{
					it.remove();
				}
			}
			
//			if (keyword.equals("币新")) {
//				str += keyword + ".*";
//			}
			
		}
	}

}
