package com.wechat.service;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.wechat.activerequests.responses.MessageIdResponse;
import com.wechat.common.Constants;
import com.wechat.common.utils.JacksonUtils;
import com.wechat.common.utils.MessageUtils;
import com.wechat.common.utils.NetworkUtils;
import com.wechat.common.utils.StringsUtils;
import com.wechat.model.requestDto.MemberExpireReq;
import com.wechat.model.requestDto.PositionInvitedReq;
import com.wechat.model.requestDto.TMsgSendBaseReq;

/**
 * FAQ服务提供类
 * 
 */
@Service
@Transactional(readOnly = true)
public class TemplateMsgService {
	protected static final Log log = LogFactory.getLog(TemplateMsgService.class);
	// 模板消息发送用url
	@Value("#{configProperties['weixin.tmsg.send']}")
	private String tmsgSendUrl;

	/**
	 * 模板消息推送  （会员到期）
	 * 
	 * @return
	 */
	public String sendMemberExpire(MemberExpireReq req) {
		String url = MessageUtils.getText(tmsgSendUrl, WeiXinService.getInstance().getAccessToken());
		MessageIdResponse response = NetworkUtils.getRemoteResponseAsJson(url, req, MessageIdResponse.class);
		log.debug("Responded: " + JacksonUtils.beautifulSerialize(response));
		// 模板消息推送出错
		if (!StringsUtils.isEqual(Constants.STR_OK, Long.toString(response.getErrorCode()))) {
			log.error("模板消息推送异常：ErrorMsg=" + response.getErrorMsg() + "; ErrorCode=" + response.getErrorCode()
					+ "; MessageId=" + response.getMessageId());
			// 40001 的情况下强制重取token
			if (StringsUtils.isEqual(Constants.STR_TOKEN_ERO, Long.toString(response.getErrorCode()))) {
				WeiXinService.getInstance().updAccessTokenInfo();
				url = MessageUtils.getText(tmsgSendUrl, WeiXinService.getInstance().getAccessToken());
			}
			// 重新发送
			response = NetworkUtils.getRemoteResponseAsJson(url, req, MessageIdResponse.class);
			log.error("模板消息重发：ErrorMsg=" + response.getErrorMsg() + "; ErrorCode=" + response.getErrorCode()
					+ "; MessageId=" + response.getMessageId());
		}
		return response.getErrorCode().toString();
	}
	
	
	/**
	 * 模板消息推送
	 * @return
	 */
	public String sendTMsg(TMsgSendBaseReq req) {
		String url = MessageUtils.getText(tmsgSendUrl, WeiXinService.getInstance().getAccessToken());
		MessageIdResponse response = NetworkUtils.getRemoteResponseAsJson(url, req, MessageIdResponse.class);
		log.debug("Responded: " + JacksonUtils.beautifulSerialize(response));
		// 模板消息推送出错
		if (!StringsUtils.isEqual(Constants.STR_OK, Long.toString(response.getErrorCode()))) {
			log.error("模板消息推送异常：ErrorMsg=" + response.getErrorMsg() + "; ErrorCode=" + response.getErrorCode()
					+ "; MessageId=" + response.getMessageId());
			// 40001 的情况下强制重取token
			if (StringsUtils.isEqual(Constants.STR_TOKEN_ERO, Long.toString(response.getErrorCode()))) {
				WeiXinService.getInstance().updAccessTokenInfo();
				url = MessageUtils.getText(tmsgSendUrl, WeiXinService.getInstance().getAccessToken());
			}
			// 重新发送
			response = NetworkUtils.getRemoteResponseAsJson(url, req, MessageIdResponse.class);
			log.error("模板消息重发：ErrorMsg=" + response.getErrorMsg() + "; ErrorCode=" + response.getErrorCode()
					+ "; MessageId=" + response.getMessageId());
		}
		return response.getErrorCode().toString();
	}
	
}
