package org.ts.ddcs.DataSaveProcesser;

import org.ts.ddcs.DataItemTemplate.DataItemTemplateInterface;
import org.ts.ddcs.DataTemplate.DataTemplateInterface;
import org.ts.ddcs.Entity.*;
import org.ts.ddcs.Log.LogHelper;
import org.ts.ddcs.Service.SwService;
import org.ts.ddcs.Tool.BytesHelp;
import org.ts.ddcs.Worker.DataProcess;
import org.ts.ddcs.Worker.DataProcessHolder;
import org.ts.ddcs.utils.GBConstants;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList;
import java.util.Map;
import java.util.concurrent.LinkedBlockingQueue;

@SuppressWarnings("all")
public class DataSaveProcesserC30 extends DataSaveProcesser {
    @Override
    public void Save(DataTemplateInterface dataTemplateInterface) {
        try {
            super.Save(dataTemplateInterface);
            String rtucode = BytesHelp.byte2HexStr(dataTemplateInterface.RtuCode());
            LogHelper.info("SAVE:" + rtucode + " " + dataTemplateInterface.GetFeture());
            SwService swService = DataProcessHolder.newInstance().dataProcess.swService;
            //时段降水
            LinkedList<DataItemTemplateInterface> dataItem = dataTemplateInterface.MacthDataItem(GBConstants.TABLE_PPTN);
            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);
                for (DataItemTemplateInterface item : dataItem) {
                    item.SetEntity(pptnEntity);
                }
                swService.SaveSwPPTNData(pptnEntity);
            }
            //分钟降水
            dataItem = dataTemplateInterface.MacthDataItem(GBConstants.TABLE_PPTN_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));
                pptnEntity.setSTCD(stcd);
                for (DataItemTemplateInterface item : dataItem) {
                    item.SetEntity(pptnEntity);
                }
                swService.SaveSwPPTN_5Min_Data(pptnEntity);
                swService.SaveSwPPTN_5Min_to_hour_Data(pptnEntity);
            }
            //河道
            dataItem = dataTemplateInterface.MacthDataItem(GBConstants.TABLE_RIVER);
            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));
                riverEntity.setSTCD(stcd);
                for (DataItemTemplateInterface item : dataItem) {
                    item.SetEntity(riverEntity);
                }
                swService.SaveSwRIVERData(riverEntity);
            }
            //水库
            dataItem = dataTemplateInterface.MacthDataItem(GBConstants.TABLE_RSVR);
            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));
                rsvrEntity.setSTCD(stcd);
                for (DataItemTemplateInterface item : dataItem) {
                    item.SetEntity(rsvrEntity);
                }
                swService.SaveSwRSVRData(rsvrEntity);
            }


//            dataItem = dataTemplateInterface.MacthDataItem("t_base_rtu_info");
//            if (dataItem.size() > 0) {
//                RtuStatusEntity statusEntity = new RtuStatusEntity();
//                String stcd = BytesHelp.byte2HexStr(dataTemplateInterface.RtuCode());
//                statusEntity.setStcd(stcd);
//                for (DataItemTemplateInterface item : dataItem) {
//                    if (item.GetFeture().equals("45")) {
//                        Map<String, Object> datas = item.GetDatas();
//                        if (datas.containsKey("waterLevelDeviceStatus")) {//水位计
//                            String waterSE = (String) datas.get("waterLevelDeviceStatus");
//                            statusEntity.setWaterSe(waterSE);
//                        }
//                        if (datas.containsKey("alternatingCurrentCharging")) {//交流电
//                            String alternatingCurrentCharging = (String) datas.get("alternatingCurrentCharging");
//                            statusEntity.setAcZt(alternatingCurrentCharging);
//                        }
//                        if (datas.containsKey("batteryVoltage")) {//电池电压
//                            String batteryVoltage = (String) datas.get("batteryVoltage");
//                            statusEntity.setBatteryVoltage(batteryVoltage);
//                        }
//                    } else if (item.GetFeture().equals("38")) {//电源电压
//                        statusEntity.setVoltage(item.GetDecimal());
//                    }
//                }
//                swService.SaveSwRtuSE(statusEntity);
//            }

//            dataItem = dataTemplateInterface.MacthDataItem("t_base_rtu_info");
//            if (dataItem.size() > 0) {
//                for (DataItemTemplateInterface item : dataItem) {
//                    if (item.GetFeture().equals("45")) {
//                        Map<String, Object> datas = item.GetDatas();
//                        if (datas.containsKey("waterSE")) {
//                            String waterSE = (String) datas.get("waterSE");
//                            //RedisTemplate redisTemplate = DataProcessHolder.newInstance().dataProcess.redisTemplate;
//                            //HashOperations<String, String, String> opsHash = redisTemplate.opsForHash();
//                            RtuStatusEntity statusEntity = new RtuStatusEntity();
//                            String stcd = BytesHelp.byte2HexStr(dataTemplateInterface.RtuCode());
//                            statusEntity.setStcd(stcd);
//                            statusEntity.setWaterSe(waterSE);
//                            swService.SaveSwRtuSE(statusEntity);
//
//                        }
//                    }
//                }
//            }
        } catch (Exception e) {
            LogHelper.error(e);
        }
    }
}
