package com.hzs.bonus.listener;

import cn.hutool.json.JSONUtil;
import com.hzs.bonus.bonus.service.IBonusSettleService;
import com.hzs.bonus.bonus.service.ICuMemberBonusService;
import com.hzs.bonus.bonus.service.ICuMemberRetailLogService;
import com.hzs.common.core.constant.CountryConstants;
import com.hzs.common.core.constant.MagicNumberConstants;
import com.hzs.common.core.constant.RabbitMqConstants;
import com.hzs.common.core.enums.EYesNo;
import com.hzs.common.domain.member.bonus.CuMemberRetailLog;
import com.hzs.common.domain.sale.ext.SaOrderExt;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 订单秒接
 */
@Slf4j
@Component
public class SaOrderSecondActivateCompensationListener {
    @Autowired
    private ICuMemberBonusService memberBonusService;

    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange(value = RabbitMqConstants.ORDER_SECOND_ACTIVATE_COMPENSATION_EXCHANGE, type = "topic"),
            value = @Queue(value = RabbitMqConstants.ORDER_SECOND_ACTIVATE_COMPENSATION_QUEUE, durable = "true", autoDelete = "false"),
            key = RabbitMqConstants.ORDER_SECOND_ACTIVATE_COMPENSATION_KEY))
    @RabbitHandler
    public void onMessage(Message<List<Long>> message, Channel channel) throws Exception {
        List<Long> activateCompensationIdList = message.getPayload();
        log.info("秒结开始消费，接收到的参数：{}", JSONUtil.toJsonStr(activateCompensationIdList));

        Long deliveryTag = (Long) message.getHeaders().get(AmqpHeaders.DELIVERY_TAG);
        channel.basicAck(deliveryTag, false);

        memberBonusService.recalculateTeamNewBoxNum(activateCompensationIdList);
    }
}
