package com.iamberry.wechat.service.mq;

import java.lang.reflect.Method;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.iamberry.wechat.core.entity.mq.MQMessage;
import com.iamberry.wechat.face.mq.MQSerivce;
import com.iamberry.wechat.service.mq.dao.MQDao;
import com.iamberry.zk.SpringContextHolder;
/**
 * @author:何秀刚
 * @description: 具体实现
 * @createDate:2016年5月25日
 */
@Service
public class MQSerivceImpl implements MQSerivce {
	
	@Autowired
	private MQDao mQDao;
	
	@Override
	public List<MQMessage> selectWaitHandlerMessage() {
		// TODO Auto-generated method stub
		return mQDao.selectWaitHandlerMessage();
	}

	public boolean test(MQMessage message) {
		System.out.println(message);
		message.getServiceToMessage();// 订单号
		//
		// send 
		return true;
	}

	@Override
	public Integer updateBatchMessage(List<MQMessage> messages) {
		// TODO Auto-generated method stub
		return mQDao.updateBatchMessage(messages);
	}

	@Override
	public boolean insertMQMessage(MQMessage message) {
		// TODO Auto-generated method stub
		
		// 首先进入队列， 如果存储队列装满，同步数据库
		boolean flag = false;
		if (!flag) {
			// 如果serviceToMessage、serivceOtherMessage、serviceStatus相同时，不允许发送第二次请求
			/*if (mQDao.judgeMessageIsExist(message) != null) {
				return true;
			}*/
			return mQDao.insertOneMQMessage(message) >= 1 ? true : false;
		}
		return flag;
	}

	@Override
	public boolean updateMessageOne(MQMessage mqMessage) {
		try {
			// get Spring Context to Bean
			Object object = SpringContextHolder.getBean(mqMessage.getServiceHandlerObjectName());
			@SuppressWarnings("rawtypes")
			Class classes = object.getClass();
			@SuppressWarnings("unchecked")
			
			// get Bean to Method.
			Method method = classes.getMethod(mqMessage.getServiceHandlerMethodName(), MQMessage.class);
			if (method == null) {
				mqMessage.setServiceErrorMessage("Method is not defined:" + mqMessage.getServiceHandlerMethodName());
				mqMessage.setServiceStatus(2);
			}
			
			// invoke Method.
			Object resultObject = method.invoke(object, mqMessage);
			if (!(resultObject instanceof Boolean)) {
				mqMessage.setServiceStatus(2);
				mqMessage.setServiceErrorMessage(mqMessage.getServiceHandlerObjectName() + "." + mqMessage.getServiceHandlerMethodName() + "()Return value is not type Boolean");
			}
			if (!(boolean) resultObject) {
				mqMessage.setServiceErrorMessage("invoke Method Error");
				mqMessage.setServiceStatus(2);
			}
			mqMessage.setServiceStatus(1);
		} catch (Exception e) {
			// TODO: handle exception
			mqMessage.setServiceErrorMessage(e.getMessage());
			mqMessage.setServiceStatus(2);
		} finally {
			mqMessage.setServiceIsSend(1);
		}
		// step 3, update Message status and serviceIsSend.
		return mQDao.insertOneMQMessageContainError(mqMessage) >= 1 ? true : false;
	}

	@Override
	public Integer insertOneMQMessageContainError(MQMessage message) {
		// TODO Auto-generated method stub
		return mQDao.insertOneMQMessageContainError(message);
	}
}
