package com.hzs.third.mqcall.listener;

import cn.hutool.json.JSONUtil;
import com.hzs.activity.base.IActivityServiceApi;
import com.hzs.common.core.constant.RabbitMqConstants;
import com.hzs.common.core.domain.R;
import com.hzs.common.core.enums.EOrderType;
import com.hzs.common.domain.sale.ext.SaOrderExt;
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 ActivityRecoilGiveListener {

    @DubboReference
    IActivityServiceApi iActivityServiceApi;

    /**
     * 复消币赠送活动MQ处理
     *
     * @param message
     * @param channel
     */
    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange(value = RabbitMqConstants.ACTIVITY_EXCHANGE, type = "topic"),
            value = @Queue(value = RabbitMqConstants.ACTIVITY_RECOIL_GIVE_QUEUE, durable = "true", autoDelete = "false"),
            key = RabbitMqConstants.ACTIVITY_KEY))
    @RabbitHandler
    public void onMessage(Message<SaOrderExt> message, Channel channel) {
        try {
            SaOrderExt saOrderExt = message.getPayload();

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

            log.info("复消币赠送活动MQ接收参数：{}", JSONUtil.toJsonStr(saOrderExt));

            if (EOrderType.REGISTER_ORDER.getValue() == saOrderExt.getOrderType()
                    || EOrderType.UPGRADE_ORDER.getValue() == saOrderExt.getOrderType()) {
                // 注册、升级订单才进行处理

                // 延迟处理一下，以防止主业务没有最终提交事务
                Thread.sleep(500);

                R<Boolean> resultR = iActivityServiceApi.recoilGiveHandle(saOrderExt);
                if (!resultR.isSuccess()) {
                    log.error("复消币赠送活动处理异常：{}", resultR.getMsg());
                }
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }

    /**
     * 复消币赠送活动撤单MQ处理
     *
     * @param message
     * @param channel
     */
    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange(value = RabbitMqConstants.ACTIVITY_BACK_EXCHANGE, type = "topic"),
            value = @Queue(value = RabbitMqConstants.ACTIVITY_BACK_RECOIL_GIVE_QUEUE, durable = "true", autoDelete = "false"),
            key = RabbitMqConstants.ACTIVITY_BACK_KEY))
    @RabbitHandler
    public void onCancelMessage(Message<SaOrderExt> message, Channel channel) {
        try {
            SaOrderExt saOrderExt = message.getPayload();

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

            log.info("复消币赠送活动撤单MQ接收参数：{}", saOrderExt);

            if (EOrderType.REGISTER_ORDER.getValue() == saOrderExt.getOrderType()
                    || EOrderType.UPGRADE_ORDER.getValue() == saOrderExt.getOrderType()) {
                // 注册、升级订单才进行处理

                // 延迟处理一下，以防止主业务没有最终提交事务
                Thread.sleep(500);

                R<Boolean> resultR = iActivityServiceApi.recoilGiveCancelHandle(saOrderExt);
                if (!resultR.isSuccess()) {
                    log.error("复消币赠送活动撤单处理异常：{}", resultR.getMsg());
                }
            }
        } catch (Exception e) {
            log.error(e.getMessage());
            e.printStackTrace();
        }
    }

}
