package com.zhiweidata.bjfzxWebapp.controllor;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.google.gson.Gson;
import com.zhiweidata.bjfzxWebapp.conf.StaticConfig;
import com.zhiweidata.bjfzxWebapp.service.BjfzxInfoService;
import com.zhiweidata.bjfzxWebapp.service.KeysWordSerivce;

@RestController
@RequestMapping(value = "/servlet")
public class ServletControllor {

	@Resource
	BjfzxInfoService service;
	
	@Resource
	KeysWordSerivce keysWordSerivce;
	
	@RequestMapping(value = "/Index")
	public void index(
			@RequestParam(value = "time", required = false, defaultValue = "") String time,
			@RequestParam(value = "groupName", required = false, defaultValue = "") String groupName,
			HttpServletResponse response, HttpServletRequest request) throws IOException {
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
	    groupName = (groupName==null || "".equals(groupName)) ? "无此组":groupName;
		SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH");
		
		Date date = null;
		try {
			date = sdf2.parse(sdf2.format(sdf1.parse(time)));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
		Gson gson = new Gson();
		ArrayList<Map<String, Object>> top10 = null;
		try
		{
			top10 = service.getTop10(date,groupName);
		}
		catch (ParseException e)
		{
			e.printStackTrace();
		}
		Map<String, Object> result=new HashMap<String,Object>();
		result.put("top10", top10);
//		result.put("groupName", groupName);
		out.print(gson.toJson(result));
		out.flush();
		out.close();
	}
	
	@RequestMapping(value = "/KeyWord")
	public void keyWord(
			@RequestParam(value = "time", required = false, defaultValue = "") String time,
			@RequestParam(value = "word", required = false, defaultValue = "") String word,
			@RequestParam(value = "page", required = false, defaultValue = "") String page,
			@RequestParam(value = "loadPage", required = false, defaultValue = "") String loadPage,
			@RequestParam(value = "groupName", required = false, defaultValue = "") String groupName,
			HttpServletResponse response, HttpServletRequest request) throws IOException {
		PrintWriter out = response.getWriter();
		groupName = (groupName==null || "".equals(groupName)) ? "无此组":groupName;

	    SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = null;
		try {
			date = sdf1.parse(time);
		} catch (ParseException e) {
			e.printStackTrace();
		}
	    
		ArrayList<Map<String, Object>> listsByThemeAndTime = new ArrayList<Map<String, Object>>();
        Gson gson = new Gson();	
//        //获取之前一小时的数据，6条
        if ("1".equals(loadPage)) {
        	//获取之前六小时的数据，每小时6条
            try
			{
				listsByThemeAndTime = service.getInfolist6(word, date, page,groupName);
			}
			catch (Exception e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
        if ("0".equals(loadPage)){
        	//获取之前一小时的数据，6条
			listsByThemeAndTime = service.getInfoList1ByTheme(word, date, page,groupName);
		}
//        Map<String, Object> result = new HashMap<>();
//        result.put("groupName", groupName);
//        result.put("resultData", listsByThemeAndTime);
        out.print(gson.toJson(listsByThemeAndTime));
		out.flush();
		out.close();
	}
	
	@RequestMapping(value = "/Realtime")
	public void realtime(
			@RequestParam(value = "time", required = false, defaultValue = "") String time,
			@RequestParam(value = "theme", required = false, defaultValue = "") String theme,
			@RequestParam(value = "page", required = false, defaultValue = "") String page,
			@RequestParam(value = "loadPage", required = false, defaultValue = "") String loadPage,
			@RequestParam(value = "groupName", required = false, defaultValue = "") String groupName,
			HttpServletResponse response, HttpServletRequest request) throws IOException {
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		groupName = (groupName==null || "".equals(groupName)) ? "无此组":groupName;
		
		SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = null;
		try {
			date = sdf1.parse(time);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
		ArrayList<Map<String, Object>> listsByThemeAndTime = new ArrayList<Map<String, Object>>();
		Gson gson = new Gson();	
		if ("1".equals(loadPage)) {
			//获取之前六小时的数据，每小时6条
			try
			{   
				if (StaticConfig.ZXGroup.equals(groupName)) {
					listsByThemeAndTime = service.getInfolistByHour(theme, date, loadPage, groupName, 24);
				}else {
					listsByThemeAndTime = service.getInfolist6(theme, date, page, groupName);
				}
			}
			catch (Exception e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if ("0".equals(loadPage)){
			//获取之前一小时的数据，6条
			listsByThemeAndTime = service.getInfoList1ByTheme(theme, date, page,groupName);
		}
		
		service.removeError(listsByThemeAndTime);
//	        Map<String, Object> result = new HashMap<>();
//	        result.put("groupName", groupName);
//	        result.put("resultData", listsByThemeAndTime);
		out.print(gson.toJson(listsByThemeAndTime));
		out.flush();
		out.close();
	}
	
	@RequestMapping(value = "/Theme")
	public void theme(
			@RequestParam(value = "groupName", required = false, defaultValue = "") String groupName,
			HttpServletResponse response, HttpServletRequest request) throws IOException {
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		Gson gson = new Gson();
		List<String> keywords=new ArrayList<String>();
		groupName = (groupName==null || "".equals(groupName)) ? "无此组":groupName;
		keywords.add("全部");
		keywords.addAll(keysWordSerivce.findAllKeyByGroup(groupName));
		
		Map<String, Object> result=new HashMap<String, Object>();
		result.put("themes",keywords);
//		result.put("groupName", groupName);
		out.print(gson.toJson(result));
		out.flush();
		out.close();
	}


}
