package org.ts.ddcs.kafka;


import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerRecord;

import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.support.Acknowledgment;
import org.springframework.stereotype.Component;
import org.ts.ddcs.common.metadata.TaishanV1JsonPackMetadataEnum;
import org.ts.ddcs.constant.BusinessConstant;
import org.ts.ddcs.hbase.HBaseService;
import org.ts.ddcs.main.MainProcessorHolder;
import org.ts.ddcs.task.DownloadDatagramTask;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.FutureTask;


/**
 * 消息处理器
 *
 * @author
 */
@Slf4j
@Component
public class MessageHandler {
    @Resource
    private HBaseService hBaseService;

    /**
     * 下发报文到测站
     *
     * @param record
     * @param acknowledgment
     */
    @KafkaListener(groupId = "${spring.kafka.consumer.group-id}", topics = "${mq-topic.topic-queue-to-rtu-datagram}", containerFactory = "ackContainerFactory")
    public void toRtu(ConsumerRecord record, Acknowledgment acknowledgment) {
        try {
            String message = (String) record.value();
            log.info("kafka handler {}", message);
            if (null != message) {
                JSONObject data = JSONObject.parseObject(message);
                String rtuCode = data.getString(BusinessConstant.BASE_KEY_RTU);
                if (null != rtuCode) {
                    DownloadDatagramTask task = new DownloadDatagramTask(rtuCode);
                    FutureTask<Integer> futureTask = new FutureTask<>(task);
                    MainProcessorHolder.newInstance().dataProcess.submitTask(futureTask);
                }
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        } finally {
            // 手动提交 offset
            acknowledgment.acknowledge();
        }
    }


    @KafkaListener(groupId = "${spring.kafka.consumer.group-id}", topics = "${mq-topic.topic-tsywxt-hbase}", containerFactory = "ackContainerFactory")
    public void toHbase(ConsumerRecord record, Acknowledgment acknowledgment) {
        try {
            String message = (String) record.value();
            log.info("kafka handler {}", message);
            if (null != message) {
                JSONObject data = JSONObject.parseObject(message);
                List<String> columnFamily = new ArrayList<>(1);
                columnFamily.add("datagram");
                String tableName = data.getString(TaishanV1JsonPackMetadataEnum.PACK_METADATA_AGREEMENT_TYPE.getName()) + "_" + "datagram";
                hBaseService.createTable(tableName.toUpperCase(), columnFamily);

                String rtuCode = data.getString(TaishanV1JsonPackMetadataEnum.PACK_METADATA_RTU_CODE.getName());
                String rowKey = rtuCode + "-" + IdUtil.fastSimpleUUID() + "-" + String.valueOf(Long.MAX_VALUE - System.currentTimeMillis());

                List<String> columns = new ArrayList<>();
                List<String> values = new ArrayList<>();

                columns.add(TaishanV1JsonPackMetadataEnum.PACK_METADATA_RTU_CODE.getName());
                values.add(rtuCode);

                columns.add(TaishanV1JsonPackMetadataEnum.PACK_METADATA_FROM_TIME.getName());
                values.add(data.getString(TaishanV1JsonPackMetadataEnum.PACK_METADATA_FROM_TIME.getName()));

                columns.add(TaishanV1JsonPackMetadataEnum.PACK_METADATA_DATAGRAM_TYPE.getName());
                values.add(data.getString(TaishanV1JsonPackMetadataEnum.PACK_METADATA_DATAGRAM_TYPE.getName()));

                columns.add(TaishanV1JsonPackMetadataEnum.PACK_METADATA_AGREEMENT_TYPE.getName());
                values.add(data.getString(TaishanV1JsonPackMetadataEnum.PACK_METADATA_AGREEMENT_TYPE.getName()));

                columns.add(TaishanV1JsonPackMetadataEnum.PACK_METADATA_DATAGRAM.getName());
                values.add(data.getString(TaishanV1JsonPackMetadataEnum.PACK_METADATA_DATAGRAM.getName()));

                columns.add("collectTime");
                values.add(data.getString("collectTime"));

                hBaseService.putData(tableName.toUpperCase(), rowKey, "datagram", columns, values);

            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        } finally {
            // 手动提交 offset
            acknowledgment.acknowledge();
        }
    }

}
