/**
 * 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.constant.KafkaConstants;
import org.ts.ddcs.common.enums.SwSiteKindEnum;
import org.ts.ddcs.enums.SwElementCodeEnum;
import org.ts.ddcs.metadata.sw.jpz.SwJpzFlowRateMetadataEnum;
import org.ts.ddcs.metadata.sw.jpz.SwJpzWaterLevelMetadataEnum;
import org.ts.ddcs.metadata.sw.jpz.SwJpzWaterQualityMetadataEnum;
import org.ts.ddcs.metadata.sw.jpz.SwJpzWeatherMetadataEnum;
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.tools.CloumnHelp;
import org.ts.ddcs.tools.KeyStoreHelp;


import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
import java.util.Map;

/***
 * Date:2023/8/28
 * Title:柬埔寨监测项目
 * Description：输出气象站、水位站、水质站及流量站数据
 * @author dylan
 * @version 1.0
 * Remark：认为有必要的其他信息
 */
@Slf4j
public class DataStreamSwJpzKafkaSink 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()));
        Date dt = Date.from(this.dataStreamCache.getPickPacketTime().atZone(ZoneId.systemDefault()).toInstant());

        JSONObject jsonBody = new JSONObject();
        String siteKind = this.dataStreamCache.getStringValue(DataStreamCacheMetadata.METADATA_SITE_KIND.getName());
        if (SwSiteKindEnum.RTU_KIND_I.getCode() == Integer.valueOf(siteKind,16)) {
            //流量站
            //for (Map<String, Object> data : elementDatas) {
            jsonBody.put(SwJpzFlowRateMetadataEnum.METADATA_RTU_CODE.getName(), this.dataStreamCache.getRtuCode());
            jsonBody.put(SwJpzFlowRateMetadataEnum.METADATA_ST.getName(), SwSiteKindEnum.RTU_KIND_I.getName());
            jsonBody.put(SwJpzFlowRateMetadataEnum.METADATA_FROM_TIME.getName(), dt.getTime());

            //观测时间
            Map<String, Object> cloumn = KeyStoreHelp.getOne(elementDatas, SwElementCodeEnum.getCode(SwJpzFlowRateMetadataEnum.METADATA_COLLECT_TIME.getCode()));
            if (null != cloumn) {
                String tmText = CloumnHelp.getString(cloumn);
                DateTimeFormatter timeFormat = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
                LocalDateTime tm = LocalDateTime.parse(tmText, timeFormat);
                jsonBody.put(SwJpzFlowRateMetadataEnum.METADATA_COLLECT_TIME.getName(),Date.from(tm.atZone(ZoneId.systemDefault()).toInstant()).getTime());
            }

            //累计流量
            cloumn = KeyStoreHelp.getOne(elementDatas, SwElementCodeEnum.getCode(SwJpzFlowRateMetadataEnum.METADATA_ACCF.getCode()));
            if (null != cloumn) {
                jsonBody.put(SwJpzFlowRateMetadataEnum.METADATA_ACCF.getName(), CloumnHelp.getLong(cloumn));
            }


            //  jsonBody.put(SwJpzFlowRateMetadataEnum.METADATA_COLLECT_TIME.getName(),data.get(JsonPackMetadataEnum.PACK_METADATA_COLLECT_TIME.getName()));


            //  List<JSONObject> keyStore = (List<JSONObject>) data.get(JsonPackMetadataEnum.PACK_METADATA_ELEMENT_DATA.getName());
            //累计流量
//                    JSONObject cloumn = KeyStoreHelp.getOne(keyStore, Sw2014DatagramElementEnum.ELEMENT_ACCF.getName());
//                    if (null != cloumn) {
//                        jsonBody.put(SwJpzFlowRateMetadataEnum.METADATA_ACCF.getName(), CloumnHelp.getLong(cloumn));
//                    }
            //瞬时流量、抽水流量
            cloumn = KeyStoreHelp.getOne(elementDatas, SwElementCodeEnum.getCode(SwJpzFlowRateMetadataEnum.METADATA_Q.getCode()));
            if (null != cloumn) {
                jsonBody.put(SwJpzFlowRateMetadataEnum.METADATA_Q.getName(), CloumnHelp.getDouble(cloumn));
            }


//                    cloumn = KeyStoreHelp.getOne(keyStore, Sw2014DatagramElementEnum.ELEMENT_Q.getName());
//                    if (null != cloumn) {
//                        jsonBody.put(SwJpzFlowRateMetadataEnum.METADATA_Q.getName(), CloumnHelp.getDouble(cloumn));
//                    }
            //当前瞬时流速
            cloumn = KeyStoreHelp.getOne(elementDatas, SwElementCodeEnum.getCode(SwJpzFlowRateMetadataEnum.METADATA_VJ.getCode()));
            if (null != cloumn) {
                jsonBody.put(SwJpzFlowRateMetadataEnum.METADATA_VJ.getName(), CloumnHelp.getDouble(cloumn));
            }

//                    cloumn = KeyStoreHelp.getOne(keyStore, Sw2014DatagramElementEnum.ELEMENT_VJ.getName());
//                    if (null != cloumn) {
//                        jsonBody.put(SwJpzFlowRateMetadataEnum.METADATA_VJ.getName(), CloumnHelp.getDouble(cloumn));
//                    }
            //瞬时水位
            cloumn = KeyStoreHelp.getOne(elementDatas, SwElementCodeEnum.getCode(SwJpzFlowRateMetadataEnum.METADATA_Z.getCode()));
            if (null != cloumn) {
                jsonBody.put(SwJpzFlowRateMetadataEnum.METADATA_Z.getName(), CloumnHelp.getDouble(cloumn));
            }

//                     cloumn = KeyStoreHelp.getOne(keyStore, Sw2014DatagramElementEnum.ELEMENT_Z.getName());
//                    if (null != cloumn) {
//                        jsonBody.put(SwJpzFlowRateMetadataEnum.METADATA_Z.getName(), CloumnHelp.getDouble(cloumn));
//                    }

            // }

        } else if (SwSiteKindEnum.RTU_KIND_H.getCode() == Integer.valueOf(siteKind,16)) {
            //水位站
            jsonBody.put(SwJpzWaterLevelMetadataEnum.METADATA_RTU_CODE.getName(), this.dataStreamCache.getRtuCode());
            jsonBody.put(SwJpzWaterLevelMetadataEnum.METADATA_ST.getName(), SwSiteKindEnum.RTU_KIND_H.getName());
            jsonBody.put(SwJpzWaterLevelMetadataEnum.METADATA_FROM_TIME.getName(), dt.getTime());


            //  for (Map<String, Object> data : elementDatas) {
//                jsonBody.put(SwJpzWaterLevelMetadataEnum.METADATA_RTU_CODE.getName(), data.get(JsonPackMetadataEnum.PACK_METADATA_RTU_CODE.getName()));
//                jsonBody.put(SwJpzWaterLevelMetadataEnum.METADATA_ST.getName(), "H");
//                jsonBody.put(SwJpzWaterLevelMetadataEnum.METADATA_FROM_TIME.getName(), data.get(JsonPackMetadataEnum.PACK_METADATA_FROM_TIME.getName()));
            //jsonBody.put(SwJpzWaterLevelMetadataEnum.METADATA_COLLECT_TIME.getName(), data.get(JsonPackMetadataEnum.PACK_METADATA_COLLECT_TIME.getName()));
            //  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, SwElementCodeEnum.getCode(SwJpzWaterLevelMetadataEnum.METADATA_COLLECT_TIME.getCode()));
            if (null != cloumn) {
               // jsonBody.put(SwJpzWaterLevelMetadataEnum.METADATA_COLLECT_TIME.getName(), CloumnHelp.getString(cloumn));

                String tmText = CloumnHelp.getString(cloumn);
                DateTimeFormatter timeFormat = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
                LocalDateTime tm = LocalDateTime.parse(tmText, timeFormat);
                jsonBody.put(SwJpzWaterLevelMetadataEnum.METADATA_COLLECT_TIME.getName(),Date.from(tm.atZone(ZoneId.systemDefault()).toInstant()).getTime());
            }
            //瞬时水位
            cloumn = KeyStoreHelp.getOne(elementDatas, SwElementCodeEnum.getCode(SwJpzWaterLevelMetadataEnum.METADATA_Z.getCode()));
            if (null != cloumn) {
                jsonBody.put(SwJpzWaterLevelMetadataEnum.METADATA_Z.getName(), CloumnHelp.getDouble(cloumn));
            }
            //}
            //  }
        } else if (SwSiteKindEnum.RTU_KIND_P.getCode() == Integer.valueOf(siteKind,16)) {
            //气象站
            // for (Map<String, Object> data : elementDatas) {
            jsonBody.put(SwJpzWeatherMetadataEnum.METADATA_RTU_CODE.getName(), this.dataStreamCache.getRtuCode());
            jsonBody.put(SwJpzWeatherMetadataEnum.METADATA_ST.getName(), SwSiteKindEnum.RTU_KIND_P.getName());
            jsonBody.put(SwJpzWeatherMetadataEnum.METADATA_FROM_TIME.getName(), dt.getTime());
            //jsonBody.put(SwJpzWeatherMetadataEnum.METADATA_COLLECT_TIME.getName(), data.get(JsonPackMetadataEnum.PACK_METADATA_COLLECT_TIME.getName()));
            //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, SwElementCodeEnum.getCode(SwJpzWeatherMetadataEnum.METADATA_COLLECT_TIME.getCode()));
            if (null != cloumn) {
                String tmText = CloumnHelp.getString(cloumn);
                DateTimeFormatter timeFormat = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
                LocalDateTime tm = LocalDateTime.parse(tmText, timeFormat);
                jsonBody.put(SwJpzWeatherMetadataEnum.METADATA_COLLECT_TIME.getName(),Date.from(tm.atZone(ZoneId.systemDefault()).toInstant()).getTime());

                //jsonBody.put(SwJpzWeatherMetadataEnum.METADATA_COLLECT_TIME.getName(), CloumnHelp.getString(cloumn));
            }
            //5 分钟时段降水量
            cloumn = KeyStoreHelp.getOne(elementDatas, SwElementCodeEnum.getCode(SwJpzWeatherMetadataEnum.METADATA_PN_05.getCode()));
            if (null != cloumn) {
                // log.info("5 分钟时段降水量 {}", cloumn.toJSONString());
                jsonBody.put(SwJpzWeatherMetadataEnum.METADATA_PN_05.getName(), CloumnHelp.getDouble(cloumn));
            }
            //降水量累计值
            cloumn = KeyStoreHelp.getOne(elementDatas, SwElementCodeEnum.getCode(SwJpzWeatherMetadataEnum.METADATA_PT.getCode()));
            if (null != cloumn) {
                jsonBody.put(SwJpzWeatherMetadataEnum.METADATA_PT.getName(), CloumnHelp.getDouble(cloumn));
            }
            //瞬时气温
            cloumn = KeyStoreHelp.getOne(elementDatas, SwElementCodeEnum.getCode(SwJpzWeatherMetadataEnum.METADATA_AI.getCode()));
            if (null != cloumn) {
                jsonBody.put(SwJpzWeatherMetadataEnum.METADATA_AI.getName(), CloumnHelp.getDouble(cloumn));
            }
            //湿度
            cloumn = KeyStoreHelp.getOne(elementDatas, SwElementCodeEnum.getCode(SwJpzWeatherMetadataEnum.METADATA_MST.getCode()));
            if (null != cloumn) {
                jsonBody.put(SwJpzWeatherMetadataEnum.METADATA_MST.getName(), CloumnHelp.getDouble(cloumn));
            }
            //PM2.5
            cloumn = KeyStoreHelp.getOne(elementDatas, SwElementCodeEnum.getCode(SwJpzWeatherMetadataEnum.METADATA_PM_25.getCode()));
            if (null != cloumn) {
                jsonBody.put(SwJpzWeatherMetadataEnum.METADATA_PM_25.getName(), CloumnHelp.getDouble(cloumn));
            }
            //气压
            cloumn = KeyStoreHelp.getOne(elementDatas, SwElementCodeEnum.getCode(SwJpzWeatherMetadataEnum.METADATA_FL.getCode()));
            if (null != cloumn) {
                jsonBody.put(SwJpzWeatherMetadataEnum.METADATA_FL.getName(), CloumnHelp.getDouble(cloumn));
            }
            //风速
            cloumn = KeyStoreHelp.getOne(elementDatas, SwElementCodeEnum.getCode(SwJpzWeatherMetadataEnum.METADATA_US.getCode()));
            if (null != cloumn) {
                jsonBody.put(SwJpzWeatherMetadataEnum.METADATA_US.getName(), CloumnHelp.getDouble(cloumn));
            }
            //风向
            cloumn = KeyStoreHelp.getOne(elementDatas, SwElementCodeEnum.getCode(SwJpzWeatherMetadataEnum.METADATA_UC.getCode()));
            if (null != cloumn) {
                jsonBody.put(SwJpzWeatherMetadataEnum.METADATA_UC.getName(), CloumnHelp.getDouble(cloumn));
            }
            //紫外线
            cloumn = KeyStoreHelp.getOne(elementDatas, SwElementCodeEnum.getCode(SwJpzWeatherMetadataEnum.METADATA_UV.getCode()));
            if (null != cloumn) {
                jsonBody.put(SwJpzWeatherMetadataEnum.METADATA_UV.getName(), CloumnHelp.getDouble(cloumn));
            }
            //太阳总辐射
            cloumn = KeyStoreHelp.getOne(elementDatas, SwElementCodeEnum.getCode(SwJpzWeatherMetadataEnum.METADATA_RAD.getCode()));
            if (null != cloumn) {
                jsonBody.put(SwJpzWeatherMetadataEnum.METADATA_RAD.getName(), CloumnHelp.getDouble(cloumn));
            }
            //二氧化氮
            cloumn = KeyStoreHelp.getOne(elementDatas, SwElementCodeEnum.getCode(SwJpzWeatherMetadataEnum.METADATA_ND.getCode()));
            if (null != cloumn) {
                jsonBody.put(SwJpzWeatherMetadataEnum.METADATA_ND.getName(), CloumnHelp.getDouble(cloumn));
            }
            // }
            // }
        } else if (SwSiteKindEnum.RTU_KIND_Q.getCode() == Integer.valueOf(siteKind,16)) {
            //水质站
            // for (Map<String, Object> data : elementDatas) {
            jsonBody.put(SwJpzWaterQualityMetadataEnum.METADATA_RTU_CODE.getName(), this.dataStreamCache.getRtuCode());
            jsonBody.put(SwJpzWaterQualityMetadataEnum.METADATA_ST.getName(), SwSiteKindEnum.RTU_KIND_Q.getName());

            jsonBody.put(SwJpzWaterQualityMetadataEnum.METADATA_FROM_TIME.getName(), dt.getTime());
            //jsonBody.put(SwJpzWaterQualityMetadataEnum.METADATA_COLLECT_TIME.getName(), data.get(JsonPackMetadataEnum.PACK_METADATA_COLLECT_TIME.getName()));
            // 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, SwElementCodeEnum.getCode(SwJpzWaterQualityMetadataEnum.METADATA_COLLECT_TIME.getCode()));
            if (null != cloumn) {
                String tmText = CloumnHelp.getString(cloumn);
                DateTimeFormatter timeFormat = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
                LocalDateTime tm = LocalDateTime.parse(tmText, timeFormat);
                jsonBody.put(SwJpzWaterQualityMetadataEnum.METADATA_COLLECT_TIME.getName(),Date.from(tm.atZone(ZoneId.systemDefault()).toInstant()).getTime());
               // jsonBody.put(SwJpzWaterQualityMetadataEnum.METADATA_COLLECT_TIME.getName(), CloumnHelp.getString(cloumn));
            }
            //电导率
            cloumn = KeyStoreHelp.getOne(elementDatas, SwElementCodeEnum.getCode(SwJpzWaterQualityMetadataEnum.METADATA_COND.getCode()));
            if (null != cloumn) {
                jsonBody.put(SwJpzWaterQualityMetadataEnum.METADATA_COND.getName(), CloumnHelp.getDouble(cloumn));
            }
            //瞬时水温
            cloumn = KeyStoreHelp.getOne(elementDatas, SwElementCodeEnum.getCode(SwJpzWaterQualityMetadataEnum.METADATA_C.getCode()));
            if (null != cloumn) {
                jsonBody.put(SwJpzWaterQualityMetadataEnum.METADATA_C.getName(), CloumnHelp.getDouble(cloumn));
            }
            //PH值
            cloumn = KeyStoreHelp.getOne(elementDatas, SwElementCodeEnum.getCode(SwJpzWaterQualityMetadataEnum.METADATA_PH.getCode()));
            if (null != cloumn) {
                jsonBody.put(SwJpzWaterQualityMetadataEnum.METADATA_PH.getName(), CloumnHelp.getDouble(cloumn));
            }
            // }
            //  }
        }
        log.info("data item :{}", jsonBody.toJSONString());
        //KafkaClient.messageSubmit(KafkaConstants.TOPIC_TSYWXT_DATAGRAM_MESSAGE, jsonBody.toJSONString());
    }
}
