package org.springblade.mq.kafka.handler;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerRecord;

import org.springblade.constant.RtuDataInfoConstant;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.support.Acknowledgment;
import org.springframework.stereotype.Component;

/**
 * KAFKA消息处理器
 *
 * @author
 */
@Slf4j
@Component
public class MessageHandler {


    @Value("${spring.mq-topic.ywxt-cloud-rtu-data-rain}")
    private String topicRtuDataRain;

    @Value("${spring.mq-topic.ywxt-cloud-rtu-data-river}")
    private String topicRtuDataRiver;

    @Value("${spring.mq-topic.ywxt-cloud-rtu-data-rsvr}")
    private String topicRtuDataRsvr;


    /**
     * RTU雨情数据
     *
     * @param record
     * @param acknowledgment
     */
    @KafkaListener(groupId = "${spring.kafka.consumer.group-id}", topics = "${spring.mq-topic.ywxt-cloud-rtu-data-rain}", containerFactory = "ackContainerFactory")
    public void rtuDataRainInfoMessage(ConsumerRecord record, Acknowledgment acknowledgment) {
        try {
            String message = (String) record.value();
            log.info("{} ,{}", "rtu data  rain info", message);
            JSONObject data = JSONObject.parseObject(message, JSONObject.class);
            if (data.containsKey(RtuDataInfoConstant.RTU_DATA_RTUCODE_KEY)) {
                log.info("rtu code {}", data.getString(RtuDataInfoConstant.RTU_DATA_RTUCODE_KEY));
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        } finally {
            // 手动提交 offset
            acknowledgment.acknowledge();
        }
    }

    /**
     * RTU河道水情数据
     *
     * @param record
     * @param acknowledgment
     */
    @KafkaListener(groupId = "${spring.kafka.consumer.group-id}", topics = "${spring.mq-topic.ywxt-cloud-rtu-data-river}", containerFactory = "ackContainerFactory")
    public void rtuDataRiverInfoMessage(ConsumerRecord record, Acknowledgment acknowledgment) {
        try {
            String message = (String) record.value();
            log.info("{} ,{}", "rtu river info", message);
            JSONObject data = JSONObject.parseObject(message, JSONObject.class);
            if (data.containsKey(RtuDataInfoConstant.RTU_DATA_RTUCODE_KEY)) {
                log.info("rtu code {}", data.getString(RtuDataInfoConstant.RTU_DATA_RTUCODE_KEY));
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        } finally {
            // 手动提交 offset
            acknowledgment.acknowledge();
        }
    }

    /**
     * RTU水库水情数据
     *
     * @param record
     * @param acknowledgment
     */
    @KafkaListener(groupId = "${spring.kafka.consumer.group-id}", topics = "${spring.mq-topic.ywxt-cloud-rtu-data-rsvr}", containerFactory = "ackContainerFactory")
    public void rtuDataRsvrInfoMessage(ConsumerRecord record, Acknowledgment acknowledgment) {
        try {
            String message = (String) record.value();
            log.info("{} ,{}", "rtu rsvr info", message);
            JSONObject data = JSONObject.parseObject(message, JSONObject.class);
            if (data.containsKey(RtuDataInfoConstant.RTU_DATA_RTUCODE_KEY)) {
                log.info("rtu code {}", data.getString(RtuDataInfoConstant.RTU_DATA_RTUCODE_KEY));
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        } finally {
            // 手动提交 offset
            acknowledgment.acknowledge();
        }
    }

}
