package com.zhiwei.servlet;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

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

import net.sf.json.JSONObject;

import com.zhiwei.util.HandleJSON;
import com.zhiwei.weixin.service.TemplateService;

/** 
 * @ClassName: TemplatesServlet 
 * @Description: TODO(发送模版消息) 
 * @author hero 
 * @date 2016年1月28日 下午6:21:38  
 */
public class TemplatesServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		InputStream in = request.getInputStream();
		JSONObject dataJSON = HandleJSON.handleJSON(in);
		TemplateService templateService = new TemplateService();
		int k = templateService.sendTemplate(dataJSON);
		Map<String,Integer> resultmap = new HashMap<String,Integer>();
		resultmap.put("message", k);
		JSONObject result = JSONObject.fromObject(resultmap);
		out.write(result.toString());
		out.flush();
		out.close();
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doGet(request, response);
	}
	
}
