package com.zhiwei.manage.servlet;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.support.RequestPartServletServerHttpRequest;

import com.alibaba.fastjson.JSON;
import com.zhiwei.manage.bean.Channel;
import com.zhiwei.manage.bean.Data;
import com.zhiwei.manage.bean.EasyNews;
import com.zhiwei.manage.bean.JsonResult;
import com.zhiwei.manage.service.CrawTemplateService;
import com.zhiwei.manage.service.DataService;
import com.zhiwei.manage.service.ServerService;
import com.zhiwei.manage.util.Change;
import com.zhiwei.manage.util.TimeUtil;
import com.zhiwei.manage.util.Tools;

@Controller
public class DataController {

	private static final Log log = LogFactory.getLog(DataController.class);

	@Autowired
	private DataService dataService;
	@Autowired
	private ServerService serverService;
	@Autowired
	private CrawTemplateService crService;

	@RequestMapping(value = "/getData", produces = "application/json;charset=utf-8")
	@ResponseBody
	public String getData(HttpServletRequest request, HttpServletResponse response) {
		Calendar c = Calendar.getInstance();
		c.add(Calendar.DATE, -75);
		List<Data> list = dataService.findBytempName(c.getTime(), new Date(), "uc");
		return JSON.toJSONString(list);
	}

	@RequestMapping(value = "/getDataBySource", produces = "application/json;charset=utf-8")
	@ResponseBody
	public String getDataBySource(HttpServletRequest request, HttpServletResponse response,@RequestBody Map<String, Object> map) {
		long start=(long) map.get("start");
		long end=(long) map.get("end");
		String source=(String) map.get("source");
		Calendar startd = Calendar.getInstance();
		startd.setTimeInMillis(start);
		Calendar endd = Calendar.getInstance();
		endd.setTimeInMillis(end);
		List<Data> list = dataService.findBySource(startd.getTime(), endd.getTime(), source);
		return JSON.toJSONString(list);
	}

	@RequestMapping(value = "/getDataByTempName", produces = "application/json;charset=utf-8")
	@ResponseBody
	public String getDataByTempName(HttpServletRequest request, HttpServletResponse response,@RequestBody Map<String, Object> map)
			throws UnsupportedEncodingException {
		long start=(long) map.get("start");
		long end=(long) map.get("end");
		String timeType=(String) map.get("timeType");
		String method=(String) map.get("method");
		Calendar startd = Calendar.getInstance();
		Calendar endTime = Calendar.getInstance();
		if (method == null) {
			JsonResult result = new JsonResult(false, 200, "参数不全", "");
			return JSON.toJSONString(result);
		} else if (timeType.equals("hour")) {
			// 小时数据
			if (start == 0) {
				startd.set(Calendar.HOUR, -10);
			} else {
				startd.setTimeInMillis(start);
				endTime.setTimeInMillis(start);
			}
			Calendar ds=Calendar.getInstance();
			ds.setTimeInMillis(end);
			String source = "";
			List<Data> list = new ArrayList<Data>();
			startd.add(Calendar.HOUR, -8);
			endTime.add(Calendar.HOUR, -7);
			ds.add(Calendar.HOUR, -6);
			String spid=crService.getSpIdByConfigName((String) map.get("tempName"));
			while (endTime.getTime().before(ds.getTime())) {
				endTime.add(Calendar.HOUR, 1);
				int count=0;
				if(method.equals("type")){
					source=(String) map.get("type");
					count=dataService.findByAccurateType(startd.getTime(), endTime.getTime(), source);
				}else{
					if (method.equals("source")) {
						source = (String) map.get("source");
						count = dataService.findBySourceFromLibrary(startd.getTime(), endTime.getTime(), source);
					} else {
						source = (String) map.get("tempName");
						count = dataService.findByTempName(startd.getTime(), endTime.getTime(), spid);
					}
				}
				Data data = new Data();
				data.setCount(count);
				data.setSource(source);
				data.setTime(startd.getTime());
				list.add(data);
				startd.add(Calendar.HOUR, 1);
			}
			return JSON.toJSONString(Tools.beResult(list, source));
		} else {
			if (start == 0) {
				start = startd.getTimeInMillis() - 1000 * 3600 * 24 * 7;
				startd.setTimeInMillis(start);
			} else {
				startd.setTimeInMillis(start);
				endTime.setTimeInMillis(end);
			}
			Channel channel = new Channel();
			List<Data> list = new ArrayList<Data>();
			if (method.equals("source")) {
				String source = (String) map.get("source");
				list = dataService.findBySource(startd.getTime(), endTime.getTime(), source);
				channel.setValue(source);
			} else {
				String tempName = (String) map.get("tempName");
				list = dataService.findBytempName(startd.getTime(), endTime.getTime(), tempName);
				channel.setValue(tempName);
			}
			channel.setList(list);
			JsonResult result = new JsonResult(true, 200, "返回成功", channel);
			return JSON.toJSONString(result);
		}
	}

	@RequestMapping(value = "/getDataAll", produces = "application/json;charset=utf-8")
	@ResponseBody
	public String getAllData(@RequestBody Map<String, Object> map) {
		long start=(long) map.get("start");
		long end=(long) map.get("end");
		try {
			if (start == 0) {
				List<Channel> channel = serverService.findDisChannel();
				List<Channel> list = new ArrayList<>();
				Calendar startTime = Calendar.getInstance();
				start = startTime.getTimeInMillis() - 1000 * 3600 * 24 * 7;
				startTime.setTimeInMillis(start);
				Calendar endTime = Calendar.getInstance();
				for(Channel chan:channel){
					List<Data> weixin = dataService.findByPt(startTime.getTime(), endTime.getTime(), chan.getPt());
					chan.setList(weixin);
					chan.setPt(chan.getPt());
					chan.setValue(chan.getValue());
					list.add(chan);
				}
				System.out.println(list);
				JsonResult result = new JsonResult(true, 200, "请求成功", list);
				return JSON.toJSONString(result);
			} else {
				Calendar startTime = Calendar.getInstance();
				startTime.setTimeInMillis(start);
				Calendar endTime = Calendar.getInstance();
				endTime.setTimeInMillis(end);
				List<Channel> channel = serverService.findDisChannel();
				List<Channel> list = new ArrayList<>();
				for(Channel chan:channel){
					List<Data> weixin = dataService.findByPt(startTime.getTime(), endTime.getTime(), chan.getPt());
					chan.setList(weixin);
					list.add(chan);
				}
				System.out.println("kil2:"+list);
				JsonResult result = new JsonResult(true, 200, "请求成功", list);
				return JSON.toJSONString(result);
			}
		} catch (Exception e) {
			JsonResult result = new JsonResult(false, 500, e.getMessage(), "");
			return JSON.toJSONString(result);
		}
	}

	@RequestMapping(value = "/getSource", produces = "application/json;charset=utf-8")
	@ResponseBody
	public String getAllDataCountByTime(@RequestBody Map<String, Object> map) {
		String value=(String) map.get("value");
		String method=(String) map.get("method");
		if (method != null) {
			if (method.equals("tempName")) {
				List<String> list = dataService.findTmpNameByLikeParam(value);
				JsonResult result = new JsonResult(true, 200, "请求成功", list);
				return JSON.toJSONString(result);
			} else if(method.equals("type")){
				List<String> list = dataService.findTypeByLikeParam(value);
				JsonResult result = new JsonResult(true, 200, "请求成功", list);
				return JSON.toJSONString(result);
			}else{
				JsonResult result = new JsonResult(true, 200, "请求成功", new ArrayList<String>());
				return JSON.toJSONString(result);
			}
		}
		JsonResult result = new JsonResult(true, 200, "参数错误");
		return JSON.toJSONString(result);
	}
}
