package com.zhiwei.manage.servlet;

import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
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.ResponseBody;

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

/**
 * 数据控制
 * 
 * @author admin 
 *
 */
@Controller
public class DataController {

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

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

	/**
	 * 获取数据
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@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);
	}

	/**
	 * 根据日来源获取数据
	 * 
	 * @param request
	 * @param response
	 * @param map
	 * @return
	 */
	@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);
	}

	/**
	 * 根据对应字段获取数据
	 * 
	 * @param request
	 * @param response
	 * @param map
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@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);
				startd.add(Calendar.HOUR_OF_DAY, -8);
				endTime.setTimeInMillis(end);
				endTime.add(Calendar.HOUR_OF_DAY, -8);
			}
			String source = (String) map.get(method);
			List<Data> list = new ArrayList<Data>();
			list = dataService.findByMethodAndParam(startd.getTime(), endTime.getTime(), method, source);
			return JSON.toJSONString(Tools.beResult(list, source));
		} else {// day,数据结果从库里拿
			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 if (method.equals("tempName")) {
				String tempName = (String) map.get("tempName");
				list = dataService.findBytempName(startd.getTime(), endTime.getTime(), tempName);
				channel.setValue(tempName);
			} else {

			}
			channel.setList(list);
			JsonResult result = new JsonResult(true, 200, "返回成功", channel);
			return JSON.toJSONString(result);
		}
	}

	/**
	 * 获取所有的数据
	 * 
	 * @param map
	 * @return
	 */
	@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);
				}
				JsonResult result = new JsonResult(true, 200, "请求成功", list);
				return JSON.toJSONString(result);
			}
		} catch (Exception e) {
			log.error("处理错误，错误信息{}", e);
			JsonResult result = new JsonResult(false, 500, e.getMessage(), "");
			return JSON.toJSONString(result);
		}
	}

	/**
	 * 根据来源获取数据
	 * 
	 * @param map
	 * @return
	 */
	@RequestMapping(value = "/getDayDataByChannel", produces = "application/json;charset=utf-8")
	@ResponseBody
	public String getDayDataByChannel(@RequestBody Map<String, Object> map) {
		long start = (long) map.get("start");
		long end = (long) map.get("end");
		String pt = String.valueOf(map.get("pt"));
		try {
			Calendar nows = Calendar.getInstance();
			Calendar startTime = Calendar.getInstance();
			startTime.setTimeInMillis(start);
			Calendar endTime = Calendar.getInstance();
			endTime.setTimeInMillis(end);
			endTime.set(Calendar.HOUR_OF_DAY, nows.get(Calendar.HOUR_OF_DAY));
			System.out.println(new SimpleDateFormat("yyyy-MM-dd").format(endTime.getTime()));
			System.out.println(new SimpleDateFormat("yyyy-MM-dd").format(startTime.getTime()));
			System.out.println(pt);
			List<Data> datas = dataService.findByPtDay(startTime.getTime(), endTime.getTime(), pt);
			JsonResult result = new JsonResult(true, 200, "请求成功", datas);
			return JSON.toJSONString(result);
		} catch (Exception e) {
			log.error("处理错误，错误信息{}", e);
			JsonResult result = new JsonResult(false, 500, e.getMessage(), "");
			return JSON.toJSONString(result);
		}
	}

	/**
	 * 根据来源名获取
	 * 
	 * @param map
	 * @param req
	 * @return
	 */
	@RequestMapping(value = "/getSource", produces = "application/json;charset=utf-8")
	@ResponseBody
	public String getSource(@RequestBody Map<String, Object> map, HttpServletRequest req) {
		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 {
				JsonResult result = new JsonResult(true, 200, "请求成功", new ArrayList<String>());
				return JSON.toJSONString(result);
			}
		}
		JsonResult result = new JsonResult(true, 200, "参数错误");
		return JSON.toJSONString(result);
	}
}
