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.etl.EtlTaskManager;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.support.Acknowledgment;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

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

	@Resource
	private EtlTaskManager etlTaskManager;

	/**
	 * 立即补数，同步某个测站的数据
	 * @param record
	 * @param acknowledgment
	 */
    @KafkaListener(groupId = "${spring.kafka.consumer.group-id}", topics = "${spring.mq-topic.topic-etl-promptly-collect}", containerFactory = "ackContainerFactory")
	public void taskRunMessage(ConsumerRecord record, Acknowledgment acknowledgment) {
		try {
			String message = (String) record.value();
			log.info("{} ,{}","rtu info",message);
			JSONObject json = JSONObject.parseObject(message);
			String rtuCode = json.getString("rtuCode");
			if (null != rtuCode){
				etlTaskManager.submitRtuDataPromptlyCollectTask(rtuCode);
			}
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		} finally {
			// 手动提交 offset
			acknowledgment.acknowledge();
		}
	}
}
