package com.gyee.edge.loader.adapter;


import com.gyee.edge.loader.data.GeneralTsData;
import com.gyee.edge.loader.data.TsPointData;
import com.gyee.edge.loader.mapper.DBPointMapper;

import com.gyee.edge.loader.service.SqlService;

import com.gyee.edge.loader.sqlite.ReadDBPointData;
import com.gyee.protocol.gyfp2.message.GYMessage;
import com.gyee.protocol.gyfp2.message.MessageData;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * @author ：
 * @date ：Created in 2022/8/25 17:34
 * @description：  写数据
 */

@Slf4j
@Component
public class DataWrite {
    @Autowired
    private SqlService sqlService;
    
    @Autowired
    private ReadDBPointData readDBPointData;

    public void DataWriteGloden(GYMessage gyMessage) throws Exception {
        List<TsPointData> tsDataList = new ArrayList<>();
        List<MessageData> messageDataList = gyMessage.getMessageDataList();
        try {
            if(gyMessage.getAttribute()==0){
                for(MessageData messageData:messageDataList){
                    TsPointData tsPointData = new TsPointData();
                    GeneralTsData generalTsData = new GeneralTsData();
                    DBPointMapper dbPointMapper = readDBPointData.selectBykey(messageData.getDataKey());
                    if(dbPointMapper.getPoint_key() != null && dbPointMapper.getPoint_key().length()>0) {
                        tsPointData.setTagName(dbPointMapper.getPoint_key());
                        generalTsData.setTs(messageData.getTs());
                        if (messageData.getDataType() == 0) {
                            generalTsData.setBooleanValue(messageData.getGYFP2_TYPE_BOOLEAN().get());
                        } else if (messageData.getDataType() == 10) {
                            generalTsData.setFloatValue(messageData.getGYFP2_TYPE_FLOAT32().get());
                        } else if (messageData.getDataType() == 8) {
                            generalTsData.setLongValue(messageData.getGYFP2_TYPE_INT64().get());
                        } else if (messageData.getDataType() == 11) {
                            generalTsData.setDoubleValue(messageData.getGYFP2_TYPE_FLOAT64().get());
                        }
                        generalTsData.setStatus((short) 1);
                        tsPointData.setTsData(generalTsData);
                        tsDataList.add(tsPointData);
                    }
                }
            }else if(gyMessage.getAttribute()==1){
                for(int i = 0; i < gyMessage.getMessageDataList().size();i++){
                    TsPointData tsPointData = new TsPointData();
                    GeneralTsData generalTsData = new GeneralTsData();
                    DBPointMapper dbPointMapper = readDBPointData.selectBykey(gyMessage.getMessageMutable().getKey() + i);
                    if(dbPointMapper.getPoint_key() != null && dbPointMapper.getPoint_key().length()>0) {
                        MessageData messageData = gyMessage.getMessageDataList().get(i);
                        tsPointData.setTagName(dbPointMapper.getPoint_key());
                        generalTsData.setTs(messageData.getTs());
                        if (messageData.getDataType() == 0) {
                            generalTsData.setBooleanValue(messageData.getGYFP2_TYPE_BOOLEAN().get());
                        } else if (messageData.getDataType() == 10) {
                            generalTsData.setFloatValue(messageData.getGYFP2_TYPE_FLOAT32().get());
                        } else if (messageData.getDataType() == 8) {
                            generalTsData.setLongValue(messageData.getGYFP2_TYPE_INT64().get());
                        } else if (messageData.getDataType() == 11) {
                            generalTsData.setDoubleValue(messageData.getGYFP2_TYPE_FLOAT64().get());
                        }
                        generalTsData.setStatus((short) 1);
                        tsPointData.setTsData(generalTsData);
                        tsDataList.add(tsPointData);
                    }
                }
            }else if(gyMessage.getAttribute()==2){
                byte messageType = gyMessage.getMessageMutable().getMessageType();
                for(MessageData messageData:messageDataList){
                    TsPointData tsPointData = new TsPointData();
                    GeneralTsData generalTsData = new GeneralTsData();
                    DBPointMapper dbPointMapper = readDBPointData.selectBykey(messageData.getDataKey());
                    if(dbPointMapper.getPoint_key() != null && dbPointMapper.getPoint_key().length()>0) {
                        tsPointData.setTagName(dbPointMapper.getPoint_key());
                        generalTsData.setTs(messageData.getTs());
                        if (messageType == 0) {
                            generalTsData.setBooleanValue(messageData.getGYFP2_TYPE_BOOLEAN().get());
                        } else if (messageType == 10) {
                            generalTsData.setFloatValue(messageData.getGYFP2_TYPE_FLOAT32().get());
                        } else if (messageType == 8) {
                            generalTsData.setLongValue(messageData.getGYFP2_TYPE_INT64().get());
                        } else if (messageType == 11) {
                            generalTsData.setDoubleValue(messageData.getGYFP2_TYPE_FLOAT64().get());
                        }
                        generalTsData.setStatus((short) 1);
                        tsPointData.setTsData(generalTsData);
                        tsDataList.add(tsPointData);
                    }
                }
            }else if(gyMessage.getAttribute()==3){
                byte messageType = gyMessage.getMessageMutable().getMessageType();
                for(int i = 0; i < gyMessage.getMessageDataList().size();i++){
                    TsPointData tsPointData = new TsPointData();
                    GeneralTsData generalTsData = new GeneralTsData();
                    DBPointMapper dbPointMapper = readDBPointData.selectBykey(gyMessage.getMessageMutable().getKey()+ i);
                    if(dbPointMapper.getPoint_key() != null && dbPointMapper.getPoint_key().length()>0) {
                        MessageData messageData = gyMessage.getMessageDataList().get(i);
                        tsPointData.setTagName(dbPointMapper.getPoint_key());
                        generalTsData.setTs(messageData.getTs());
                        if (messageType == 0) {
                            generalTsData.setBooleanValue(messageData.getGYFP2_TYPE_BOOLEAN().get());
                        } else if (messageType == 10) {
                            generalTsData.setFloatValue(messageData.getGYFP2_TYPE_FLOAT32().get());
                        } else if (messageType == 8) {
                            generalTsData.setLongValue(messageData.getGYFP2_TYPE_INT64().get());
                        } else if (messageType == 11) {
                            generalTsData.setDoubleValue(messageData.getGYFP2_TYPE_FLOAT64().get());
                        }
                        generalTsData.setStatus((short) 1);
                        tsPointData.setTsData(generalTsData);
                        tsDataList.add(tsPointData);
                    }
                }
            }else if(gyMessage.getAttribute()==4){
                long ts = gyMessage.getMessageMutable().getTs();
                for(MessageData messageData:messageDataList){
                    TsPointData tsPointData = new TsPointData();
                    GeneralTsData generalTsData = new GeneralTsData();
                    DBPointMapper dbPointMapper = readDBPointData.selectBykey(messageData.getDataKey());
                    if(dbPointMapper.getPoint_key() != null && dbPointMapper.getPoint_key().length()>0) {
                        tsPointData.setTagName(dbPointMapper.getPoint_key());
                        generalTsData.setTs(ts);
                        if (messageData.getDataType() == 0) {
                            generalTsData.setBooleanValue(messageData.getGYFP2_TYPE_BOOLEAN().get());
                        } else if (messageData.getDataType() == 10) {
                            generalTsData.setFloatValue(messageData.getGYFP2_TYPE_FLOAT32().get());
                        } else if (messageData.getDataType() == 8) {
                            generalTsData.setLongValue(messageData.getGYFP2_TYPE_INT64().get());
                        } else if (messageData.getDataType() == 11) {
                            generalTsData.setDoubleValue(messageData.getGYFP2_TYPE_FLOAT64().get());
                        }
                        generalTsData.setStatus((short) 1);
                        tsPointData.setTsData(generalTsData);
                        tsDataList.add(tsPointData);
                    }
                }
            }else if(gyMessage.getAttribute()==5){
                long ts = gyMessage.getMessageMutable().getTs();
                for(int i = 0; i < gyMessage.getMessageDataList().size();i++){
                    TsPointData tsPointData = new TsPointData();
                    GeneralTsData generalTsData = new GeneralTsData();
                    DBPointMapper dbPointMapper = readDBPointData.selectBykey(gyMessage.getMessageMutable().getKey() + i);
                    if(dbPointMapper.getPoint_key() != null && dbPointMapper.getPoint_key().length()>0) {
                        MessageData messageData = gyMessage.getMessageDataList().get(i);
                        tsPointData.setTagName(dbPointMapper.getPoint_key());
                        generalTsData.setTs(ts);
                        if (messageData.getDataType() == 0) {
                            generalTsData.setBooleanValue(messageData.getGYFP2_TYPE_BOOLEAN().get());
                        } else if (messageData.getDataType() == 10) {
                            generalTsData.setFloatValue(messageData.getGYFP2_TYPE_FLOAT32().get());
                        } else if (messageData.getDataType() == 8) {
                            generalTsData.setLongValue(messageData.getGYFP2_TYPE_INT64().get());
                        } else if (messageData.getDataType() == 11) {
                            generalTsData.setDoubleValue(messageData.getGYFP2_TYPE_FLOAT64().get());
                        }
                        generalTsData.setStatus((short) 1);
                        tsPointData.setTsData(generalTsData);
                        tsDataList.add(tsPointData);
                    }
                }
            }else if(gyMessage.getAttribute()==6){
                long ts = gyMessage.getMessageMutable().getTs();
                byte messageType = gyMessage.getMessageMutable().getMessageType();
                for(MessageData messageData:messageDataList){
                    TsPointData tsPointData = new TsPointData();
                    GeneralTsData generalTsData = new GeneralTsData();
                    DBPointMapper dbPointMapper = readDBPointData.selectBykey(messageData.getDataKey());
                    if(dbPointMapper.getPoint_key() != null && dbPointMapper.getPoint_key().length()>0) {
                        tsPointData.setTagName(dbPointMapper.getPoint_key());
                        generalTsData.setTs(ts);
                        if (messageType == 0) {
                            generalTsData.setBooleanValue(messageData.getGYFP2_TYPE_BOOLEAN().get());
                        } else if (messageType == 10) {
                            generalTsData.setFloatValue(messageData.getGYFP2_TYPE_FLOAT32().get());
                        } else if (messageType == 8) {
                            generalTsData.setLongValue(messageData.getGYFP2_TYPE_INT64().get());
                        } else if (messageType == 11) {
                            generalTsData.setDoubleValue(messageData.getGYFP2_TYPE_FLOAT64().get());
                        }
                        generalTsData.setStatus((short) 1);
                        tsPointData.setTsData(generalTsData);
                        tsDataList.add(tsPointData);
                    }
                }
            }else if(gyMessage.getAttribute()==7){
                long ts = gyMessage.getMessageMutable().getTs();
                byte messageType = gyMessage.getMessageMutable().getMessageType();
                for(int i = 0;i < gyMessage.getMessageDataList().size();i++){
                    TsPointData tsPointData = new TsPointData();
                    GeneralTsData generalTsData = new GeneralTsData();
                    MessageData messageData = gyMessage.getMessageDataList().get(i);
                    DBPointMapper dbPointMapper = readDBPointData.selectBykey(gyMessage.getMessageMutable().getKey() + i);
                    if(dbPointMapper.getPoint_key() != null && dbPointMapper.getPoint_key().length()>0){
                        tsPointData.setTagName(dbPointMapper.getPoint_key());
                        generalTsData.setTs(ts);
                        if(messageType == 0){
                            generalTsData.setBooleanValue(messageData.getGYFP2_TYPE_BOOLEAN().get());
                        }else if(messageType == 10){
                            generalTsData.setFloatValue(messageData.getGYFP2_TYPE_FLOAT32().get());
                        }else if(messageType == 8){
                            generalTsData.setLongValue(messageData.getGYFP2_TYPE_INT64().get());
                        }else if(messageType == 11){
                            generalTsData.setDoubleValue(messageData.getGYFP2_TYPE_FLOAT64().get());
                        }
                        generalTsData.setStatus((short) 1);
                        tsPointData.setTsData(generalTsData);
                        tsDataList.add(tsPointData);
                    }
                }
            }
            Date sTdate = new Date();
            sqlService.saveDataSnaps(tsDataList);
            Date enDate = new Date();
            log.info("写入golden时间"+(enDate.getTime()-sTdate.getTime()));
        }catch (Exception e){
            log.error("datawrite发生异常",e);
        }
    }

}
