package com.elab.mq.msg.adptor;

import com.aliyun.openservices.ons.api.Message;
import com.aliyun.openservices.ons.api.transaction.LocalTransactionExecuter;
import com.aliyun.openservices.ons.api.transaction.TransactionStatus;
import com.elab.mq.api.transaction.TransactionStatusEnum;
import com.elab.mq.model.MessageModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 事务消息接口适配
 *
 * @author Liukx
 * @create 2018-04-09 19:19
 * @email liukx@elab-plus.com
 **/
public abstract class LocalTransactionExecuterAdaptor implements LocalTransactionExecuter {

    private Logger logger = LoggerFactory.getLogger(LocalTransactionExecuterAdaptor.class);

    /**
     * 需要交给指定的业务自己去处理
     *
     * @param message
     * @param o
     * @return
     */
    public abstract TransactionStatusEnum executeCallback(MessageModel message, Object o);

    @Override
    public final TransactionStatus execute(Message message, Object o) {
        MessageModel messageModel = new MessageModel(message.getTopic(), message.getTag(), o);
        logger.debug(" 事务消息适配层 : " + message.toString());
        TransactionStatusEnum transactionStatusEnum = executeCallback(messageModel, o);
        logger.debug(" 事务消息执行结果 : " + transactionStatusEnum.name());
        return TransactionStatusEnum.getTransactionStatus(transactionStatusEnum);
    }
}
