package com.tzy.listener.mq;

import com.rabbitmq.client.Channel;
import com.tzy.common.constant.MqConstans;
import com.tzy.sportcard.api.service.AsyncAppService;
import com.tzy.sportcard.group.domain.CardGroupInfo;
import com.tzy.sportcard.group.service.ICardGroupInfoService;
import com.tzy.system.service.ISysDictDataService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;


@Slf4j
@Component
public class GroupSortConsumer {
    @Resource
    private ICardGroupInfoService cardGroupInfoService;
    @Resource
    private AsyncAppService asyncAppService;
    @Resource
    private ISysDictDataService dictDataService;

    @RabbitHandler
    @RabbitListener(queuesToDeclare = @Queue(MqConstans.QUEUE_GROUP_SORT_UPDATE))
    public void groupSortConsumer(Long groupInfoId, Channel channel, Message message) throws IOException {
        try {
            CardGroupInfo cardGroupInfo = cardGroupInfoService.selectCardGroupInfoById(groupInfoId);
            if (cardGroupInfo != null && cardGroupInfo.getCopies() > cardGroupInfo.getSoldCopies()) {
                asyncAppService.updateInfoSortQueryV3(cardGroupInfo);
            }
        } catch (Exception e) {
            log.info("组团排序修改异常,参数:{}", groupInfoId, e);
        }
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }






}
