package org.ts.ddcs.DataRelayDatabaseProcesser;

import org.ts.ddcs.DataItemTemplate.DataItemTemplateInterface;
import org.ts.ddcs.DataSaveProcesser.DataSaveProcesser;
import org.ts.ddcs.DataTemplate.DataTemplateInterface;
import org.ts.ddcs.Entity.PptnEntity;
import org.ts.ddcs.Entity.RiverEntity;
import org.ts.ddcs.Entity.RsvrEntity;
import org.ts.ddcs.log.LogHelper;
import org.ts.ddcs.Service.RelayService;
import org.ts.ddcs.Service.SwService;
import org.ts.ddcs.Tool.BytesHelp;
import org.ts.ddcs.Worker.DataProcessHolder;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList;

@SuppressWarnings("all")
public class DataRelayDatabaseProcesserC32 extends DataRelayDatabaseProcesser{
    @Override
    public void Save(DataTemplateInterface dataTemplateInterface) {
        super.Save(dataTemplateInterface);
        try {
            LogHelper.info("SAVE:" + dataTemplateInterface.GetFeture() + " " + BytesHelp.byte2HexStr(dataTemplateInterface.RtuCode()));

            RelayService relayService = DataProcessHolder.newInstance().dataProcess.relayService;


            LinkedList<DataItemTemplateInterface> dataItem = dataTemplateInterface.MacthDataItem("ST_PPTN_R");
            if (dataItem.size() > 0) {
                PptnEntity pptnEntity = new PptnEntity();
                StringBuffer dataStr = new StringBuffer(BytesHelp.byte2HexStr(dataTemplateInterface.CollectTime()));
                String tm = dataStr.toString();
                tm = "20" + tm.substring(0,tm.length()-2) + "0000";
                dataStr = new StringBuffer(BytesHelp.byte2HexStr(dataTemplateInterface.SendPacketTime()));
                String dt = "20" + dataStr.toString();
                SimpleDateFormat timeFormat = new SimpleDateFormat("yyyyMMddHHmmss");
                pptnEntity.setTM(new Timestamp(timeFormat.parse(tm).getTime()));
                pptnEntity.setDT(new Timestamp(timeFormat.parse(dt).getTime()));
                pptnEntity.setTS(new Timestamp(new Date().getTime()));
                String stcd = BytesHelp.byte2HexStr(dataTemplateInterface.RtuCode());
                pptnEntity.setSTCD( stcd.substring(2));
                for(DataItemTemplateInterface item:dataItem){
                    item.SetEntity(pptnEntity);
                }
                relayService.RelaySwPPTNData(pptnEntity);
            }

            dataItem = dataTemplateInterface.MacthDataItem("st_pptn_r_5_min");
            if (dataItem.size() > 0) {
                PptnEntity pptnEntity = new PptnEntity();
                StringBuffer dataStr = new StringBuffer(BytesHelp.byte2HexStr(dataTemplateInterface.CollectTime()));
                String tm = "20" + dataStr.toString() + "00";
                dataStr = new StringBuffer(BytesHelp.byte2HexStr(dataTemplateInterface.SendPacketTime()));
                String dt = "20" + dataStr.toString();
                SimpleDateFormat timeFormat = new SimpleDateFormat("yyyyMMddHHmmss");
                pptnEntity.setTM(new Timestamp(timeFormat.parse(tm).getTime()));
                pptnEntity.setDT(new Timestamp(timeFormat.parse(dt).getTime()));
                pptnEntity.setTS(new Timestamp(new Date().getTime()));
                String stcd = BytesHelp.byte2HexStr(dataTemplateInterface.RtuCode());
                pptnEntity.setSTCD( stcd.substring(2));
                for(DataItemTemplateInterface item:dataItem){
                    item.SetEntity(pptnEntity);
                }
                relayService.RelaySwPPTN_5MinData(pptnEntity);
                relayService.RelaySwPPTN_5Min_to_hour_Data(pptnEntity);
            }

            dataItem = dataTemplateInterface.MacthDataItem("st_river_r");
            if (dataItem.size() > 0) {
                RiverEntity riverEntity = new RiverEntity();
                StringBuffer dataStr = new StringBuffer(BytesHelp.byte2HexStr(dataTemplateInterface.CollectTime()));
                String tm = "20" + dataStr.toString() + "00";
                dataStr = new StringBuffer(BytesHelp.byte2HexStr(dataTemplateInterface.SendPacketTime()));
                String dt = "20" + dataStr.toString();
                SimpleDateFormat timeFormat = new SimpleDateFormat("yyyyMMddHHmmss");
                riverEntity.setTM(new Timestamp(timeFormat.parse(tm).getTime()));
                riverEntity.setDT(new Timestamp(timeFormat.parse(dt).getTime()));
                riverEntity.setTS(new Timestamp(new Date().getTime()));
                String stcd = BytesHelp.byte2HexStr(dataTemplateInterface.RtuCode());
                riverEntity.setSTCD( stcd.substring(2));
                for(DataItemTemplateInterface item:dataItem){
                    item.SetEntity(riverEntity);
                }
                relayService.RelaySwRIVERData(riverEntity);
            }

            dataItem = dataTemplateInterface.MacthDataItem("st_rsvr_r");
            if (dataItem.size() > 0) {
                RsvrEntity rsvrEntity = new RsvrEntity();
                StringBuffer dataStr = new StringBuffer(BytesHelp.byte2HexStr(dataTemplateInterface.CollectTime()));
                String tm = "20" + dataStr.toString() + "00";
                dataStr = new StringBuffer(BytesHelp.byte2HexStr(dataTemplateInterface.SendPacketTime()));
                String dt = "20" + dataStr.toString();
                SimpleDateFormat timeFormat = new SimpleDateFormat("yyyyMMddHHmmss");
                rsvrEntity.setTM(new Timestamp(timeFormat.parse(tm).getTime()));
                rsvrEntity.setDT(new Timestamp(timeFormat.parse(dt).getTime()));
                rsvrEntity.setTS(new Timestamp(new Date().getTime()));
                String stcd = BytesHelp.byte2HexStr(dataTemplateInterface.RtuCode());
                rsvrEntity.setSTCD( stcd.substring(2));
                for(DataItemTemplateInterface item:dataItem){
                    item.SetEntity(rsvrEntity);
                }
                relayService.RelaySwRSVRData(rsvrEntity);
            }
        }catch (Exception e){
            LogHelper.error(e);
        }
    }
}
