
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 com.tmerclub.cloud.group.service.GroupTeamService;
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;

/**
 * 取消团购订单 团队操作
 * @author tmerclub
 */
@Component
@RocketMQMessageListener(topic = RocketMqConstant.GROUP_ORDER_CANCEL_TOPIC, consumerGroup = RocketMqConstant.GROUP_ORDER_CANCEL_TEAM_GROUP)
public class GroupOrderCancelTeamGroupConsumer implements RocketMQListener<List<Long>> {

    @Autowired
    private GroupOrderService groupOrderService;
    @Autowired
    private GroupTeamService groupTeamService;

    /**
     * 取消团购订单
     */
    @Override
    public void onMessage(List<Long> orderIds) {
        GroupOrderVO groupOrder = groupOrderService.getByOrderId(orderIds.get(0));
        if (!Objects.equals(groupOrder.getStatus(), GroupOrderStatusEnum.WAITING_PAY.value())) {
            return;
        }
        // 如果是待成团的状态就取消拼团活动
        groupTeamService.cancelGroupTeam(groupOrder.getGroupTeamId());
    }
}
