/**
 * Copyright 2019 DH
 * All right reserved.
 * 项目名称: 大恒泰山系统
 * 创建日期:2023/8/28
 */
package org.ts.ddcs.dataStream.sink;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.ts.ddcs.common.metadata.DatagramMetadataEnum;
import org.ts.ddcs.enums.SzyElementCodeEnum;
import org.ts.ddcs.dataStream.DataStreamAdapter;
import org.ts.ddcs.dataStream.DataStreamCache;
import org.ts.ddcs.dataStream.DataStreamLineManager;
import org.ts.ddcs.metadata.DataStreamCacheMetadata;
import org.ts.ddcs.metadata.szy.*;
import org.ts.ddcs.tools.CloumnHelp;
import org.ts.ddcs.tools.KeyStoreHelp;

import java.util.List;
import java.util.Map;

/***
 * Date:2023/8/28
 * Title: 兴安盟水资源项目
 * Description：输出流量、累计流量和小时流量
 * @author dylan
 * @version 1.0
 * Remark：认为有必要的其他信息
 */
@Slf4j
public class DataStreamSzyXamKafkaSink extends DataStreamSink {
    private DataStreamCache dataStreamCache;

    @Override
    public void init(DataStreamLineManager context) {
        this.setContext(context);
        this.dataStreamCache = context.getCache();
    }

    @Override
    public void sink(DataStreamAdapter dataStreamAdapter, List<Map<String, Object>> elementDatas) {
        log.info("sink {} 接收时间 {}", dataStreamAdapter.getRtuCode(), this.dataStreamCache.getStringValue(DataStreamCacheMetadata.METADATA_PICK_TIME.getName()));
        for (Map<String, Object> data : elementDatas) {
            JSONObject jsonBody = new JSONObject();
            //  if (data.containsKey(JsonPackMetadataEnum.PACK_METADATA_ELEMENT_TYPE.getName())) {
            String elementCode = (String) data.get(DatagramMetadataEnum.CACHE_METADATA_ELEMENT_CODE.getName());
            if (elementCode.equals(SzyElementCodeEnum.CODE_FLOW_RATE.getCode())) {
                jsonBody.put(SzyFlowRateMetadataEnum.METADATA_FROM_TIME.getName(), this.dataStreamCache.getStringValue(DataStreamCacheMetadata.METADATA_PICK_TIME.getName()));
                jsonBody.put(SzyFlowRateMetadataEnum.METADATA_UP_TIME.getName(), this.dataStreamCache.getStringValue(DataStreamCacheMetadata.METADATA_UP_TIME.getName()));
                jsonBody.put(SzyFlowRateMetadataEnum.METADATA_COLLECT_TIME.getName(), this.dataStreamCache.getStringValue(DataStreamCacheMetadata.METADATA_COLLECT_TIME.getName()));
                jsonBody.put(SzyFlowRateMetadataEnum.METADATA_RTU_CODE.getName(), this.dataStreamCache.getRtuCode());
                //   if (data.containsKey(JsonPackMetadataEnum.PACK_METADATA_ELEMENT_DATA.getName())) {
                // List<JSONObject> keyStore = (List<JSONObject>) data.get(JsonPackMetadataEnum.PACK_METADATA_ELEMENT_DATA.getName());
                Map<String, Object> cloumn = KeyStoreHelp.getOne(elementDatas, SzyElementCodeEnum.CODE_FLOW_RATE.getCode(), SzyFlowRateMetadataEnum.METADATA_CODE_Q.getName());
                if (null != cloumn) {
                    jsonBody.put(SzyFlowRateMetadataEnum.METADATA_CODE_Q.getName(), CloumnHelp.getDouble(cloumn));
                }
                cloumn = KeyStoreHelp.getOne(elementDatas, SzyElementCodeEnum.CODE_FLOW_RATE.getCode(), SzyFlowRateMetadataEnum.METADATA_CODE_ACC_Q.getName());
                if (null != cloumn) {
                    jsonBody.put(SzyFlowRateMetadataEnum.METADATA_CODE_ACC_Q.getName(), CloumnHelp.getLong(cloumn));
                }
                cloumn = KeyStoreHelp.getOne(elementDatas, SzyElementCodeEnum.CODE_FLOW_RATE.getCode(), SzyFlowRateMetadataEnum.METADATA_CODE_HOUR_Q.getName());
                if (null != cloumn) {
                    jsonBody.put(SzyFlowRateMetadataEnum.METADATA_CODE_HOUR_Q.getName(), CloumnHelp.getLong(cloumn));
                }
                //  }
                log.info("data item :{}", jsonBody.toJSONString());
               // KafkaClient.messageSubmit("topic-szy-water-flow-rate", jsonBody.toJSONString());
            } else if (elementCode.equals(SzyElementCodeEnum.CODE_EXT_RTU_STATUS.getCode())) {
                jsonBody.put(SzyFlowRateMetadataEnum.METADATA_FROM_TIME.getName(), this.dataStreamCache.getStringValue(DataStreamCacheMetadata.METADATA_PICK_TIME.getName()));
                jsonBody.put(SzyFlowRateMetadataEnum.METADATA_UP_TIME.getName(), this.dataStreamCache.getStringValue(DataStreamCacheMetadata.METADATA_UP_TIME.getName()));
                jsonBody.put(SzyFlowRateMetadataEnum.METADATA_COLLECT_TIME.getName(), this.dataStreamCache.getStringValue(DataStreamCacheMetadata.METADATA_COLLECT_TIME.getName()));
                jsonBody.put(SzyStatusMetadataEnum.METADATA_RTU_CODE.getName(), this.dataStreamCache.getRtuCode());
                // if (data.containsKey(JsonPackMetadataEnum.PACK_METADATA_ELEMENT_DATA.getName())) {
                //List<JSONObject> keyStore = (List<JSONObject>) data.get(JsonPackMetadataEnum.PACK_METADATA_ELEMENT_DATA.getName());
                Map<String, Object> cloumn = KeyStoreHelp.getOne(elementDatas, SzyElementCodeEnum.CODE_EXT_RTU_STATUS.getCode(), SzyStatusMetadataEnum.METADATA_WORK_STATUS.getName());
                if (null != cloumn) {
                    jsonBody.put(SzyStatusMetadataEnum.METADATA_WORK_STATUS.getName(), CloumnHelp.getLong(cloumn));
                }
                cloumn = KeyStoreHelp.getOne(elementDatas, SzyElementCodeEnum.CODE_EXT_RTU_STATUS.getCode(), SzyStatusMetadataEnum.METADATA_IC_STATUS.getName());
                if (null != cloumn) {
                    jsonBody.put(SzyStatusMetadataEnum.METADATA_IC_STATUS.getName(), CloumnHelp.getLong(cloumn));
                }
                cloumn = KeyStoreHelp.getOne(elementDatas, SzyElementCodeEnum.CODE_EXT_RTU_STATUS.getCode(), SzyStatusMetadataEnum.METADATA_VALUE_STATUS.getName());
                if (null != cloumn) {
                    jsonBody.put(SzyStatusMetadataEnum.METADATA_VALUE_STATUS.getName(), CloumnHelp.getLong(cloumn));
                }
                cloumn = KeyStoreHelp.getOne(elementDatas, SzyElementCodeEnum.CODE_EXT_RTU_STATUS.getCode(), SzyStatusMetadataEnum.METADATA_WATER_PUMP_STATUS.getName());
                if (null != cloumn) {
                    jsonBody.put(SzyStatusMetadataEnum.METADATA_WATER_PUMP_STATUS.getName(), CloumnHelp.getLong(cloumn));
                }
                cloumn = KeyStoreHelp.getOne(elementDatas, SzyElementCodeEnum.CODE_EXT_RTU_STATUS.getCode(), SzyStatusMetadataEnum.METADATA_DOOR_STATUS.getName());
                if (null != cloumn) {
                    jsonBody.put(SzyStatusMetadataEnum.METADATA_DOOR_STATUS.getName(), CloumnHelp.getLong(cloumn));
                }
                cloumn = KeyStoreHelp.getOne(elementDatas, SzyElementCodeEnum.CODE_EXT_RTU_STATUS.getCode(), SzyStatusMetadataEnum.METADATA_POWER_STATUS.getName());
                if (null != cloumn) {
                    jsonBody.put(SzyStatusMetadataEnum.METADATA_POWER_STATUS.getName(), CloumnHelp.getLong(cloumn));
                }
                //  }
                log.info("data item :{}", jsonBody.toJSONString());
                //KafkaClient.messageSubmit("topic-szy-rtu-status", jsonBody.toJSONString());
            } else if (elementCode.equals(SzyElementCodeEnum.CODE_EXT_RTU_WARN.getCode())) {
                jsonBody.put(SzyFlowRateMetadataEnum.METADATA_FROM_TIME.getName(), this.dataStreamCache.getStringValue(DataStreamCacheMetadata.METADATA_PICK_TIME.getName()));
                jsonBody.put(SzyFlowRateMetadataEnum.METADATA_UP_TIME.getName(), this.dataStreamCache.getStringValue(DataStreamCacheMetadata.METADATA_UP_TIME.getName()));
                jsonBody.put(SzyFlowRateMetadataEnum.METADATA_COLLECT_TIME.getName(), this.dataStreamCache.getStringValue(DataStreamCacheMetadata.METADATA_COLLECT_TIME.getName()));
                jsonBody.put(SzyWarnMetadataEnum.METADATA_RTU_CODE.getName(), this.dataStreamCache.getRtuCode());
                //  if (data.containsKey(JsonPackMetadataEnum.PACK_METADATA_ELEMENT_DATA.getName())) {
                //  List<JSONObject> keyStore = (List<JSONObject>) data.get(JsonPackMetadataEnum.PACK_METADATA_ELEMENT_DATA.getName());
                Map<String, Object> cloumn = KeyStoreHelp.getOne(elementDatas, SzyElementCodeEnum.CODE_EXT_RTU_WARN.getCode(), SzyWarnMetadataEnum.METADATA_AC_WARN.getName());
                if (null != cloumn) {
                    jsonBody.put(SzyWarnMetadataEnum.METADATA_AC_WARN.getName(), CloumnHelp.getLong(cloumn));
                }
                cloumn = KeyStoreHelp.getOne(elementDatas, SzyElementCodeEnum.CODE_EXT_RTU_WARN.getCode(), SzyWarnMetadataEnum.METADATA_BT_V_WARN.getName());
                if (null != cloumn) {
                    jsonBody.put(SzyWarnMetadataEnum.METADATA_BT_V_WARN.getName(), CloumnHelp.getLong(cloumn));
                }
                cloumn = KeyStoreHelp.getOne(elementDatas, SzyElementCodeEnum.CODE_EXT_RTU_WARN.getCode(), SzyWarnMetadataEnum.METADATA_WATER_LEVEL_OUT_WARN.getName());
                if (null != cloumn) {
                    jsonBody.put(SzyWarnMetadataEnum.METADATA_WATER_LEVEL_OUT_WARN.getName(), CloumnHelp.getLong(cloumn));
                }
                cloumn = KeyStoreHelp.getOne(elementDatas, SzyElementCodeEnum.CODE_EXT_RTU_WARN.getCode(), SzyWarnMetadataEnum.METADATA_FLOW_OUT_WARN.getName());
                if (null != cloumn) {
                    jsonBody.put(SzyWarnMetadataEnum.METADATA_FLOW_OUT_WARN.getName(), CloumnHelp.getLong(cloumn));
                }
                cloumn = KeyStoreHelp.getOne(elementDatas, SzyElementCodeEnum.CODE_EXT_RTU_WARN.getCode(), SzyWarnMetadataEnum.METADATA_WATER_QUALITY_WARN.getName());
                if (null != cloumn) {
                    jsonBody.put(SzyWarnMetadataEnum.METADATA_WATER_QUALITY_WARN.getName(), CloumnHelp.getLong(cloumn));
                }
                cloumn = KeyStoreHelp.getOne(elementDatas, SzyElementCodeEnum.CODE_EXT_RTU_WARN.getCode(), SzyWarnMetadataEnum.METADATA_FLOW_SENSOR_WARN.getName());
                if (null != cloumn) {
                    jsonBody.put(SzyWarnMetadataEnum.METADATA_FLOW_SENSOR_WARN.getName(), CloumnHelp.getLong(cloumn));
                }
                cloumn = KeyStoreHelp.getOne(elementDatas, SzyElementCodeEnum.CODE_EXT_RTU_WARN.getCode(), SzyWarnMetadataEnum.METADATA_WATER_PUMP_STATUS.getName());
                if (null != cloumn) {
                    jsonBody.put(SzyWarnMetadataEnum.METADATA_WATER_PUMP_STATUS.getName(), CloumnHelp.getLong(cloumn));
                }
                cloumn = KeyStoreHelp.getOne(elementDatas, SzyElementCodeEnum.CODE_EXT_RTU_WARN.getCode(), SzyWarnMetadataEnum.METADATA_WATER_LEVEL_SENSOR_WARN.getName());
                if (null != cloumn) {
                    jsonBody.put(SzyWarnMetadataEnum.METADATA_WATER_LEVEL_SENSOR_WARN.getName(), CloumnHelp.getLong(cloumn));
                }
                cloumn = KeyStoreHelp.getOne(elementDatas, SzyElementCodeEnum.CODE_EXT_RTU_WARN.getCode(), SzyWarnMetadataEnum.METADATA_WATER_PRESURE.getName());
                if (null != cloumn) {
                    jsonBody.put(SzyWarnMetadataEnum.METADATA_WATER_PRESURE.getName(), CloumnHelp.getLong(cloumn));
                }
                cloumn = KeyStoreHelp.getOne(elementDatas, SzyElementCodeEnum.CODE_EXT_RTU_WARN.getCode(), SzyWarnMetadataEnum.METADATA_RESERVE.getName());
                if (null != cloumn) {
                    jsonBody.put(SzyWarnMetadataEnum.METADATA_RESERVE.getName(), CloumnHelp.getLong(cloumn));
                }
                cloumn = KeyStoreHelp.getOne(elementDatas, SzyElementCodeEnum.CODE_EXT_RTU_WARN.getCode(), SzyWarnMetadataEnum.METADATA_IC_WARN.getName());
                if (null != cloumn) {
                    jsonBody.put(SzyWarnMetadataEnum.METADATA_IC_WARN.getName(), CloumnHelp.getLong(cloumn));
                }
                cloumn = KeyStoreHelp.getOne(elementDatas, SzyElementCodeEnum.CODE_EXT_RTU_WARN.getCode(), SzyWarnMetadataEnum.METADATA_CONTROL_WARN.getName());
                if (null != cloumn) {
                    jsonBody.put(SzyWarnMetadataEnum.METADATA_CONTROL_WARN.getName(), CloumnHelp.getLong(cloumn));
                }
                cloumn = KeyStoreHelp.getOne(elementDatas, SzyElementCodeEnum.CODE_EXT_RTU_WARN.getCode(), SzyWarnMetadataEnum.METADATA_SURPLUS_WATER.getName());
                if (null != cloumn) {
                    jsonBody.put(SzyWarnMetadataEnum.METADATA_SURPLUS_WATER.getName(), CloumnHelp.getLong(cloumn));
                }
                cloumn = KeyStoreHelp.getOne(elementDatas, SzyElementCodeEnum.CODE_EXT_RTU_WARN.getCode(), SzyWarnMetadataEnum.METADATA_DOOR_WARN.getName());
                if (null != cloumn) {
                    jsonBody.put(SzyWarnMetadataEnum.METADATA_DOOR_WARN.getName(), CloumnHelp.getLong(cloumn));
                }

                //  }
                log.info("data item :{}", jsonBody.toJSONString());
              //  KafkaClient.messageSubmit("topic-szy-rtu-warn", jsonBody.toJSONString());

            } else if (elementCode.equals(SzyElementCodeEnum.CODE_WATER_LEVEL.getCode())) {
                jsonBody.put(SzyFlowRateMetadataEnum.METADATA_FROM_TIME.getName(), this.dataStreamCache.getStringValue(DataStreamCacheMetadata.METADATA_PICK_TIME.getName()));
                jsonBody.put(SzyFlowRateMetadataEnum.METADATA_UP_TIME.getName(), this.dataStreamCache.getStringValue(DataStreamCacheMetadata.METADATA_UP_TIME.getName()));
                jsonBody.put(SzyFlowRateMetadataEnum.METADATA_COLLECT_TIME.getName(), this.dataStreamCache.getStringValue(DataStreamCacheMetadata.METADATA_COLLECT_TIME.getName()));
                jsonBody.put(SzyWaterLevelMetadataEnum.METADATA_RTU_CODE.getName(), this.dataStreamCache.getRtuCode());
                //   if (data.containsKey(JsonPackMetadataEnum.PACK_METADATA_ELEMENT_DATA.getName())) {
                //  List<JSONObject> keyStore = (List<JSONObject>) data.get(JsonPackMetadataEnum.PACK_METADATA_ELEMENT_DATA.getName());
                Map<String, Object> cloumn = KeyStoreHelp.getOne(elementDatas, SzyElementCodeEnum.CODE_WATER_LEVEL.getCode(), SzyWaterLevelMetadataEnum.METADATA_CODE_LEVEL.getName());
                if (null != cloumn) {
                    jsonBody.put(SzyWaterLevelMetadataEnum.METADATA_CODE_LEVEL.getName(), CloumnHelp.getDouble(cloumn));
                }
                //  }
                log.info("data item :{}", jsonBody.toJSONString());
               // KafkaClient.messageSubmit("topic-szy-water-level", jsonBody.toJSONString());
            } else if (elementCode.equals(SzyElementCodeEnum.CODE_FLOW_SPEED.getCode())) {
                jsonBody.put(SzyFlowRateMetadataEnum.METADATA_FROM_TIME.getName(), this.dataStreamCache.getStringValue(DataStreamCacheMetadata.METADATA_PICK_TIME.getName()));
                jsonBody.put(SzyFlowRateMetadataEnum.METADATA_UP_TIME.getName(), this.dataStreamCache.getStringValue(DataStreamCacheMetadata.METADATA_UP_TIME.getName()));
                jsonBody.put(SzyFlowRateMetadataEnum.METADATA_COLLECT_TIME.getName(), this.dataStreamCache.getStringValue(DataStreamCacheMetadata.METADATA_COLLECT_TIME.getName()));
                jsonBody.put(SzyFlowSpeedMetadataEnum.METADATA_RTU_CODE.getName(), this.dataStreamCache.getRtuCode());
                //if (data.containsKey(JsonPackMetadataEnum.PACK_METADATA_ELEMENT_DATA.getName())) {
                //  List<JSONObject> keyStore = (List<JSONObject>) data.get(JsonPackMetadataEnum.PACK_METADATA_ELEMENT_DATA.getName());
                Map<String, Object> cloumn = KeyStoreHelp.getOne(elementDatas, SzyElementCodeEnum.CODE_FLOW_SPEED.getCode(), SzyFlowSpeedMetadataEnum.METADATA_CODE_FLOWSPEED.getName());
                if (null != cloumn) {
                    jsonBody.put(SzyFlowSpeedMetadataEnum.METADATA_CODE_FLOWSPEED.getName(), CloumnHelp.getDouble(cloumn));
                }
                //  }
                log.info("data item :{}", jsonBody.toJSONString());
               // KafkaClient.messageSubmit("topic-szy-flow-speed", jsonBody.toJSONString());
            }
            // }
        }
    }
}
