
package com.tmerclub.cloud.group.listener;

import com.tmerclub.cloud.group.bo.mongo.MongoGroupTeamBO;
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 TRAKC
 */
@Component
@RocketMQTransactionListener(rocketMQTemplateBeanName = "groupOrderSuccessTemplate", maximumPoolSize = 20)
public class GroupOrderSuccessTransactionListener implements RocketMQLocalTransactionListener {

    @Autowired
    private GroupTeamService groupTeamService;

    @Override
    public RocketMQLocalTransactionState executeLocalTransaction(Message msg, Object groupTeam) {
        // 确保团队更新成功
        groupTeamService.updateToSuccess((MongoGroupTeamBO) groupTeam);
        return RocketMQLocalTransactionState.COMMIT;
    }

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

}
