
package com.tmerclub.cloud.group.listener;

import com.tmerclub.cloud.group.service.GroupTeamService;
import org.apache.rocketmq.spring.annotation.RocketMQTransactionListener;
import org.apache.rocketmq.spring.core.RocketMQLocalTransactionListener;
import org.apache.rocketmq.spring.core.RocketMQLocalTransactionState;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.stereotype.Component;

/**
 * 订单退款事务监听
 *
 * @author tmerclub
 * @date 2021/1/7
 */
@Component
@RocketMQTransactionListener(rocketMQTemplateBeanName = "groupOrderUnSuccessRefundTemplate", maximumPoolSize = 20)
public class GroupOrderUnSuccessRefundTransactionListener implements RocketMQLocalTransactionListener {

    @Autowired
    private GroupTeamService groupTeamService;

    @Override
    public RocketMQLocalTransactionState executeLocalTransaction(Message msg, Object groupTeamId) {
        groupTeamService.updateToUnSuccess((Long) groupTeamId);
        return RocketMQLocalTransactionState.COMMIT;
    }

    @Override
    public RocketMQLocalTransactionState checkLocalTransaction(Message msg) {
        // 直接回滚，防止异步出现aba的问题
        // 第二个原因是因为上面那个事务消息会重复发送多次，因为GROUP_ORDER_UN_SUCCESS_TOPIC 这个会回调多次
        return RocketMQLocalTransactionState.ROLLBACK;
    }

}
