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.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;

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
@Deprecated
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
	@Deprecated
	public void insertMQMessage(MQMessage message) {
		// TODO Auto-generated method stub
		// 首先进入队列， 如果存储队列装满，同步数据库
		mQDao.insertOneMQMessage(message);
	}

	@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);
			} else {
				// invoke Method.
				System.out.println(object.getClass());
				Object resultObject = null;
				DataSourceTransactionManager transactionManager = (DataSourceTransactionManager) SpringContextHolder.getBean("transactionManager");
				DefaultTransactionDefinition def = new DefaultTransactionDefinition();
				// 事物隔离级别，开启新事务
				def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
				// 获得事务状态
				TransactionStatus status = transactionManager.getTransaction(def);
				try {
					//逻辑代码
					resultObject = method.invoke(object, mqMessage);
					transactionManager.commit(status);
				} catch (Exception e) {
					transactionManager.rollback(status);
				}
				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;
	}

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