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

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.ts.ddcs.common.metadata.DatagramMetadataEnum;
import org.ts.ddcs.common.metadata.TaishanV2JsonPackMetadataEnum;
import org.ts.ddcs.dataStream.DataStreamAdapter;
import org.ts.ddcs.dataStream.DataStreamCache;
import org.ts.ddcs.dataStream.DataStreamLineManager;
import org.ts.ddcs.enums.SwElementCodeEnum;
import org.ts.ddcs.main.DataProcessorHolder;
import org.ts.ddcs.metadata.DataStreamCacheMetadata;
import org.ts.ddcs.metadata.sw.jpz.SwJpzFlowRateMetadataEnum;
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：对本文件的详细描述，原则上不能少于30字
 * @author dylan
 * @version 1.0
 * Remark：认为有必要的其他信息
 */
@Slf4j
public class DataStreamSwRestSink 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();
        jsonBody.put(TaishanV2JsonPackMetadataEnum.PACK_METADATA_RTU_CODE.getName(), this.dataStreamCache.getRtuCode());
        String siteKind = this.dataStreamCache.getStringValue(DataStreamCacheMetadata.METADATA_SITE_KIND.getName());
        jsonBody.put(TaishanV2JsonPackMetadataEnum.PACK_METADATA_SITE_TYPE.getName(), siteKind);
        jsonBody.put(TaishanV2JsonPackMetadataEnum.PACK_METADATA_FROM_TIME.getName(), dt.getTime());

        String datagramCode = this.dataStreamCache.getDatagramCode();
        jsonBody.put(TaishanV2JsonPackMetadataEnum.PACK_METADATA_DATAGRAM_TYPE.getName(), datagramCode);

        //观测时间
        Map<String, Object> cloumn = KeyStoreHelp.getOne(elementDatas, SwElementCodeEnum.getCode("TT"));
        if (null != cloumn) {
            String tmText = CloumnHelp.getString(cloumn);
            DateTimeFormatter timeFormat = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
            LocalDateTime tm = LocalDateTime.parse(tmText, timeFormat);
            jsonBody.put(TaishanV2JsonPackMetadataEnum.PACK_METADATA_COLLECT_TIME.getName(), Date.from(tm.atZone(ZoneId.systemDefault()).toInstant()).getTime());
        }
        JSONArray items = new JSONArray();
        for (Map<String, Object> data : elementDatas) {
            JSONObject item = new JSONObject();
            String codeValue = (String) data.get(DatagramMetadataEnum.CACHE_METADATA_ELEMENT_CODE.getName());
            List<Map<String, Object>> keyStore = (List<Map<String, Object>>) data.get(DatagramMetadataEnum.CACHE_METADATA_DATA.getName());
            JSONArray datas = new JSONArray();
            if (null != keyStore && keyStore.size() > 0) {
                for (Map<String, Object> v : keyStore) {
                    JSONObject value = new JSONObject(v);
//                    String keyText = (String) v.get(ElementDataMetadataEnum.ELEMENT_METADATA_CODE.getName());
//                    String nameText = (String) v.get(ElementDataMetadataEnum.ELEMENT_METADATA_NAME.getName());
//                    String subCodeText = (String) v.get(ElementDataMetadataEnum.ELEMENT_METADATA_SUB_CODE.getName());
//                    String valueText = (String) v.get(ElementDataMetadataEnum.ELEMENT_METADATA_VALUE.getName());
//                    String unitText = (String) v.get(ElementDataMetadataEnum.ELEMENT_METADATA_UNIT.getName());
//                    String descText = (String) v.get(ElementDataMetadataEnum.ELEMENT_METADATA_DESC.getName());
//                    String errorText = (String) v.get(ElementDataMetadataEnum.ELEMENT_METADATA_ERROR.getName());
                    datas.add(value);
                }
            }
            item.put(DatagramMetadataEnum.CACHE_METADATA_ELEMENT_CODE.getName(), codeValue);
            item.put(DatagramMetadataEnum.CACHE_METADATA_DATA.getName(), datas);
            items.add(item);
        }
        jsonBody.put(TaishanV2JsonPackMetadataEnum.PACK_METADATA_DATA.getName(), items);

        log.info("data item :{}", jsonBody.toJSONString());
        DataProcessorHolder.newInstance().dataProcess.postSwData(jsonBody);
    }
}
