package com.ylcm.sys.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

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

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONObject;
import com.ylcm.sys.common.Util;

/**
 * BaseController
 */
public class BaseController {

	private Logger logger = LoggerFactory.getLogger(BaseController.class);

	public void ajaxJson(String outString, HttpServletResponse response) {
		PrintWriter out = null;

		response.setHeader("content-type", "application/json");
		response.setCharacterEncoding("UTF-8");

		try {

			out = response.getWriter();
			out.write(outString);

		} catch (IOException e) {
			logger.error(e.getMessage(), e);
		} finally {
			if (out != null) {
				out.flush();
				out.close();
			}
		}
	}

	@SuppressWarnings("rawtypes")
	public void printResult(HttpServletRequest request,
			HttpServletResponse response, Map map) {
		try {
			response.setCharacterEncoding("UTF-8");
			response.setContentType("text/plain");
			response.setHeader("Pragma", "No-cache");
			response.setHeader("Cache-Control", "no-cache");
			response.setDateHeader("Expires", 0);
			JSONObject resultJSON = new JSONObject(map);
			String jsonpCallback = request.getParameter("callback");// 客户端请求参数
			PrintWriter out = response.getWriter();
			out.println(jsonpCallback + "(" + resultJSON.toJSONString() + ")");// 返回jsonp格式数据
			out.flush();
			out.close();
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
	}

	public void returnResult(HttpServletRequest request,
			HttpServletResponse response, JSONObject resultJSON) {
		PrintWriter out = null;
		try {
			String jsonpCallback = request.getParameter("callback");// 客户端请求参数
			response.setCharacterEncoding("UTF-8");
			if(Util.isNotNull(jsonpCallback)){
				response.setContentType("text/plain");
				response.setHeader("Pragma", "No-cache");
				response.setHeader("Cache-Control", "no-cache");
				response.setDateHeader("Expires", 0);
				out = response.getWriter();
				out.println(jsonpCallback + "(" + resultJSON.toJSONString() + ")");// 返回jsonp格式数据
			}else{
				response.setHeader("content-type", "json/javascript");
				out = response.getWriter();
				out.write(resultJSON.toString());
			}
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		} finally {
			if (out != null) {
				out.flush();
				out.close();
			}
		}
	}
	
	protected void jsonAjax(HttpServletResponse response, String outString) {
		PrintWriter out = null;

		response.setHeader("content-type", "application/json");
		response.setCharacterEncoding("UTF-8");
		try {
			out = response.getWriter();
			out.write(outString);
		} catch (IOException e) {
			logger.error(e.getMessage(), e);
		} finally {
			if (out != null) {
				out.flush();
				out.close();
			}
		}
	}
	
	
	/*
	 * 获取最终jsonp返回数据
	 */
	public String getReturnStr(JSONObject result, String callback){
		/*
		 * 判断是否需要添加callback
		 */
		String str = result.toString();
		if (Util.isNotNull(callback)) {
			str = callback + "(" + result.toString() + ")";
		}
		
		return str;
	}
	
	
	/**
	 * 校验数组并返回结果
	 * @param jsonobj
	 * @param list
	 */
	@SuppressWarnings("rawtypes")
	public void examList(JSONObject jsonobj,List<Map> list){
		if(list.size() > 0){
			jsonobj.put("code", 1);
			jsonobj.put("msg", "");
			jsonobj.put("list", list);
		}else{
			jsonobj.put("code", 0);
			jsonobj.put("msg", "暂无数据");
		}
	}
}
