package org.ts.ddcs.kafka;

import org.ts.ddcs.log.LogHelper;
import org.ts.ddcs.worker.DataProcessHolder;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author dylan
 */
@Component
public class KafkaMessageListener {

    @KafkaListener(topics = "${run-config.mq.checkStateQueue}")
    public void process(List<ConsumerRecord<String, String>> records) {
        try {
            for (ConsumerRecord<String, String> record : records) {
                LogHelper.info("kafka " + record.topic() + " " + record.partition() + " " + record.value());
                DataProcessHolder.newInstance().dataProcess.submitRtuStatusCheckTask(record.value());
            }
        } catch (Exception e) {
            LogHelper.error(e);
        }
    }

    @KafkaListener(topics = "${run-config.mq.warningMessage}")
    public void warningProcess(List<ConsumerRecord<String, String>> records) {
        try {
            for (ConsumerRecord<String, String> record : records) {
                LogHelper.info("kafka " + record.topic() + " " + record.partition() + " " + record.value());
                DataProcessHolder.newInstance().dataProcess.submitWarningProcessTask(record.value());
            }
        } catch (Exception e) {
            LogHelper.error(e);
        }
    }
}
