package com.wechat.callback.handlers.event;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Component;

import com.wechat.callback.handlers.ITencentRequestHandler;
import com.wechat.callback.passiveresponses.BasePassiveResponse;
import com.wechat.callback.passiveresponses.TextResponse;
import com.wechat.callback.tencentrequests.events.TemplateSendFinishEventRequest;
import com.wechat.common.utils.XmlUtils;

@Component("templateSendFinishEvent")
public class TemplateSendFinishEventHandler implements ITencentRequestHandler {
    
    protected static final Log log = LogFactory.getLog(TemplateSendFinishEventHandler.class);
    
    @Override
    public BasePassiveResponse handle(String xml, Object obj) {
        try {
            TemplateSendFinishEventRequest req = XmlUtils.xml2obj(xml, TemplateSendFinishEventRequest.class);
            if (req == null) {
                throw new Exception("Xml parse error");
            }
            
            if(!req.getStatus().equals(TemplateSendFinishEventRequest.StatusEnum.SUCCESS))
            {
                log.error("模板消息推送失败:[ " + req.getStatus() + "]");
                log.error("OpenID:[ " + req.getFromUserName() + "]");
                log.error("CreateTime:[ " + req.getCreateTime() + "]");
                log.error("MsgID:[ " + req.getMsgID() + "]");
            }
        }
        catch (Exception ex) {
            log.error("message:",ex);
        }
        
        return TextResponse.NULL_RESPONSE;
    }
}
