package com.liquidnet.service.consumer.kylin.service.processor;

import com.liquidnet.common.mq.constant.MQConst;
import com.liquidnet.common.sms.processor.SmsProcessor;
import com.liquidnet.commons.lang.util.JsonUtils;
import com.liquidnet.service.base.SmsMessage;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

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

/**
 * ConsumerAdamSmsProcessor.class
 *
 * @author zhanggb
 * Created by IntelliJ IDEA at 2021/7/13
 */
@Slf4j
@Component
public class ConsumerKylinSmsProcessor {
    @Resource
    SmsProcessor smsProcessor;

    private void consumerSmsSendHandler(Message msg, Channel channel) {
        MessageProperties properties = msg.getMessageProperties();
        String consumerQueue = properties.getConsumerQueue();
        long deliveryTag = properties.getDeliveryTag();
        log.info("CONSUMER SMS ==> [consumerQueue:{},deliveryTag:{}]", consumerQueue, deliveryTag);
        String msgBody = new String(msg.getBody());
        log.debug("CONSUMER SMS ==> Preparing:{}", msgBody);
        try {
            SmsMessage smsMessage = JsonUtils.fromJson(msgBody, SmsMessage.class);
            boolean result = smsProcessor.send(smsMessage.getPhone(), smsMessage.getSignName(), smsMessage.getTemplateCode(), smsMessage.getTemplateParam().toString());
            log.debug("CONSUMER SMS result of execution:{}", result);
            if (result) {
                channel.basicAck(deliveryTag, false);
            } else {
                log.warn("###CONSUMER SMS[consumerQueue:{},deliveryTag={},sqlMessage:{}]", consumerQueue, deliveryTag, msgBody);
                channel.basicAck(deliveryTag, false);
            }
        } catch (IOException e) {
            log.error("CONSUMER SMS[consumerQueue:{},deliveryTag:{},sqlMessage:{}]", consumerQueue, deliveryTag, msgBody, e);
        }
    }

    /* ================================================================== ｜ 短信验证码 */

//    @RabbitListener(
//            bindings = @QueueBinding(
//                    exchange = @Exchange(MQConst.EX_LNS_SMS_SENDER),
//                    key = MQConst.RK_SMS_CODE,
//                    value = @Queue(MQConst.QUEUES_SMS_CODE)
//            ),
//            concurrency = "25"
//    )
//    public void consumerSqlForSmsCode(Message msg, Channel channel) {
//        this.consumerSmsSendHandler(msg, channel);
//    }

    /* ================================================================== ｜ 短信通知 */

    @RabbitListener(
            bindings = @QueueBinding(
                    exchange = @Exchange(MQConst.EX_LNS_SMS_SENDER),
                    key = MQConst.RK_SMS_NOTICE,
                    value = @Queue(MQConst.QUEUES_SMS_NOTICE)
            ),
            concurrency = "10"
    )
    public void consumerSqlForSmsNotice(Message msg, Channel channel) {
        this.consumerSmsSendHandler(msg, channel);
    }


    /* ================================================================== ｜  */
}
