package com.palmnest.webapp.controller.wechat;

import com.palmnest.service.estateserve.SmsAuthCodeManager;
import com.palmnest.service.wechat.WeChatApiManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import lombok.extern.slf4j.Slf4j;

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

@Slf4j
@Controller
@RequestMapping(value = "/wechat/pushmsgout")
public class PushMsgOutController {
	@Autowired
	private SmsAuthCodeManager smsAuthCodeManager;
	@Autowired
	private WeChatApiManager weChatApiManager;
	//private Logger log=Logger.getLogger(PushMsgOutController.class);
	/**
	 * 发送手机短信
	 *
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/phonemsg", method = {RequestMethod.GET, RequestMethod.POST})
	public @ResponseBody
	String Phonemsg(HttpServletRequest request, HttpServletResponse response) {
		String orgId   = request.getParameter("orgId");
		String content = request.getParameter("content");
		String phones  = request.getParameter("phones");
		String result  = "Error";
		result = smsAuthCodeManager.Phonemsg(orgId, content, phones);

		return result;
	}

	/**
	 * 发送微信消息
	 *
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/wxmsg", method = {RequestMethod.GET, RequestMethod.POST})
	public @ResponseBody
	String Wxmsg(HttpServletRequest request, HttpServletResponse response) {
		String userId  = request.getParameter("userId");
		String content = request.getParameter("content");
		String phones  = request.getParameter("phones");
		String result  = "Error";
		//result=smsAuthCodeManager.Phonemsg(orgId,content,phones);

		return result;
	}

	/**
	 * 对外提供模板消息发送， 由调用方组织好json
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/wxTemplateMsg4Office", method = { RequestMethod.POST})
	public @ResponseBody
	String wxTemplateMsg4Office(HttpServletRequest request, HttpServletResponse response) {
		String developId  = request.getParameter("developId"); //公众号ID
		String content = request.getParameter("content");      //模板消息json串
		String orgCode  = request.getParameter("orgCode");      //小邮局代码
		String openid  = request.getParameter("openid");        //openid

		String result  = "Error";
		//result=smsAuthCodeManager.Phonemsg(orgId,content,phones);
		log.info("in wxTemplateMsg4Office --developId:" + developId + "  orgCode:" + orgCode + " openid:" + openid + " json: " + content);
		try {
			weChatApiManager.sendTemplateMsg(developId, content);
		}catch(Exception ex)
		{
			log.error("in wxTemplateMsg4Office --developId:" + developId + "  orgCode:" + orgCode + " openid:" + openid + " json: " + content + " exception:" + ex.toString());
		}
		return result;
	}
}
