package com.hzs.third.mqcall.listener;

import cn.hutool.core.collection.CollectionUtil;
import com.hzs.common.core.constant.RabbitMqConstants;
import com.hzs.common.core.domain.R;
import com.hzs.member.base.IMemberServiceApi;
import com.hzs.member.base.dto.MemberContinueDTO;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.messaging.Message;
import org.springframework.stereotype.Component;

/**
 * 会员续约MQ处理
 */
@Slf4j
@Component
public class MemberContinueListener {

    @DubboReference
    IMemberServiceApi iMemberServiceApi;

//    /**
//     * 会员续约处理，当会员注册、撤单、升级奖衔等进行处理
//     *
//     * @param message
//     * @param channel
//     * @throws Exception
//     */
//    @RabbitListener(bindings = @QueueBinding(
//            exchange = @Exchange(value = RabbitMqConstants.MEMBER_CONTINUE_EXCHANGE, type = "topic"),
//            value = @Queue(value = RabbitMqConstants.MEMBER_CONTINUE_QUEUE, durable = "true", autoDelete = "false"),
//            key = RabbitMqConstants.MEMBER_CONTINUE_KEY))
//    @RabbitHandler
//    public void onMessage(Message<MemberContinueDTO> message, Channel channel) throws Exception {
//        // 手动应答
//        Long deliveryTag = (Long) message.getHeaders().get(AmqpHeaders.DELIVERY_TAG);
//        channel.basicAck(deliveryTag, false);
//
//        MemberContinueDTO memberContinueDTO = message.getPayload();
//        log.info("会员续约MQ处理. 参数：{}", memberContinueDTO);
//
//        // 拿到信息之后，延迟1秒再处理，防止出现MQ消费处理速度比正常业务处理速度快
//        Thread.sleep(1000);
//
//        if (null == memberContinueDTO.getEMemberContinue()
//                || CollectionUtil.isEmpty(memberContinueDTO.getPkMemberList())) {
//            log.error("会员续约MQ处理缺少信息");
//            return;
//        }
//
//        // 调用会员服务处理同姓名、同手机号有效期及休止期逻辑
//        R resultR = iMemberServiceApi.handleMemberContinue(memberContinueDTO);
//        if (!resultR.isSuccess()) {
//            log.error("会员续约处理异常，msg: {}", resultR.getMsg());
//        }
//    }

}
