
package com.tmerclub.cloud.group.listener;

import com.tmerclub.cloud.api.group.vo.GroupOrderVO;
import com.tmerclub.cloud.common.rocketmq.config.RocketMqConstant;
import com.tmerclub.cloud.group.constant.GroupOrderStatusEnum;
import com.tmerclub.cloud.group.service.GroupOrderService;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Objects;

/**
 * 取消团购订单 订单操作
 * 用mq来保证最终一致性，同步发送消息更新团队状态
 * @author tmerclub
 */
@Component
@RocketMQMessageListener(topic = RocketMqConstant.GROUP_ORDER_CANCEL_TOPIC, consumerGroup = RocketMqConstant.GROUP_ORDER_CANCEL_ORDER_GROUP)
public class GroupOrderCancelOrderGroupConsumer implements RocketMQListener<List<Long>> {

    @Autowired
    private GroupOrderService groupOrderService;

    /**
     * 取消团购订单
     */
    @Override
    public void onMessage(List<Long> orderIds) {
        GroupOrderVO groupOrder = groupOrderService.getByOrderId(orderIds.get(0));
        if (!Objects.equals(groupOrder.getStatus(), GroupOrderStatusEnum.WAITING_PAY.value())) {
            return;
        }
        groupOrderService.cancelGroupOrder(groupOrder, orderIds);
    }
}
