package cn.hmsoft.weixin.processor;

import cn.hmsoft.frame.constants.FrameStatus;
import cn.hmsoft.helper.StringHelper;
import cn.hmsoft.log.LogHelper;
import cn.hmsoft.weixin.data.model.WxMsgProcessor;
import cn.hmsoft.weixin.entity.message.EventMessage;
import cn.hmsoft.weixin.entity.reply.WeiXinReply;
import cn.hmsoft.weixin.processor.event.EvenBasicProcessor;
import cn.hmsoft.weixin.util.WeiXinUtil;

/**********************************
 * @note 事件处理器
 * @author revisit@126.com
 * @Company: www.hmsoft.cn
 * @create 2017-05-09 02:25:18
 * @version 4.0.0
 */
public class EventProcessor extends AbstractProcessor<EventMessage> {

	@Override
	protected WeiXinReply handle() throws Exception {
		EvenBasicProcessor processor = null;
		WxMsgProcessor mPro = null;
		try {
			mPro = WeiXinUtil.getProcessorClass(this.wxDefine,
					"Event" + StringHelper.upperFirstChar(this.jsonEntity.getEvent().toLowerCase()));
			
			if (mPro == null)
				return null;
			processor = (EvenBasicProcessor) Class.forName(mPro.getProcess_class()).newInstance();
		} catch (Exception e) {
			LogHelper.error(e);
		}
		if (processor == null)
			return null;
		processor.setEntity(wxDefine, msg, jsonEntity, user, null);
		if (FrameStatus.Active.toString().equals(mPro.getRun_thread())) {
			// 线程模式
			new Thread(processor).run();
			return null;
		} else
			return processor.handleEvent();
	}
}
