package org.ts.ddcs.DataSaveProcesser;

import com.alibaba.fastjson.JSONObject;
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.math.BigDecimal;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

@SuppressWarnings("all")
public class DataSaveProcesserC32 extends DataSaveProcesser {
    @Override
    public void Save(DataTemplateInterface dataTemplateInterface) {
        super.Save(dataTemplateInterface);
        try {
            String rtuCode = BytesHelp.byte2HexStr(dataTemplateInterface.RtuCode());
            LogHelper.info("SAVE:" + dataTemplateInterface.GetFeture() + " " + BytesHelp.byte2HexStr(dataTemplateInterface.RtuCode()));
            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 = "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);
                for (DataItemTemplateInterface item : dataItem) {
                    item.SetEntity(pptnEntity);
                }

                if (DataProcess.config().isDataShare()) {
                    Object obj = JSONObject.toJSON(pptnEntity);
                    String json = obj.toString();
                    JSONObject data = new JSONObject();
                    data.put("kind", "50");
                    data.put("data", json);
                    String queue = DataProcess.queueConfig().getShareMessage();
                    DataProcessHolder.newInstance().dataProcess.kafkaTemplate.send(queue, data.toJSONString());
                }

                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);
                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);
                for (DataItemTemplateInterface item : dataItem) {
                    item.SetEntity(riverEntity);
                }
                if (DataProcess.config().isDataShare()) {
                    Object obj = JSONObject.toJSON(riverEntity);
                    String json = obj.toString();
                    JSONObject data = new JSONObject();
                    data.put("kind", "38");
                    data.put("data", json);
                    String queue = DataProcess.queueConfig().getShareMessage();
                    DataProcessHolder.newInstance().dataProcess.kafkaTemplate.send(queue, data.toJSONString());
                }
                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);
                for (DataItemTemplateInterface item : dataItem) {
                    item.SetEntity(rsvrEntity);
                }
                if (DataProcess.config().isDataShare()) {
                    Object obj = JSONObject.toJSON(rsvrEntity);
                    String json = obj.toString();
                    JSONObject data = new JSONObject();
                    data.put("kind", "4B");
                    data.put("data", json);
                    String queue = DataProcess.queueConfig().getShareMessage();
                    DataProcessHolder.newInstance().dataProcess.kafkaTemplate.send(queue, data.toJSONString());
                }
                swService.SaveSwRSVRData(rsvrEntity);

            }

            dataItem = dataTemplateInterface.MacthDataItem(GBConstants.TABLE_WATERGATE);
            if (null != dataItem && dataItem.size() > 0) {
                WaterGateInfo waterGateInfo = new WaterGateInfo();
                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");
                waterGateInfo.setTM(new Timestamp(timeFormat.parse(tm).getTime()));
                waterGateInfo.setDT(new Timestamp(timeFormat.parse(dt).getTime()));
                waterGateInfo.setTS(new Timestamp(new Date().getTime()));
                String stcd = BytesHelp.byte2HexStr(dataTemplateInterface.RtuCode());
                waterGateInfo.setSTCD(stcd);
                List<WaterGateDetailInfo> detailInfoList = new LinkedList<>();
                for (DataItemTemplateInterface item : dataItem) {
                    if (item.GetFeture().equals("FF10")) {//闸门开度
                        Map<String, Object> datas = item.GetDatas();
                        if (null != datas) {
                            for (int i = 0; i < datas.size(); i++) {
                                WaterGateDetailInfo detailInfo = new WaterGateDetailInfo();
                                String key = String.format("%d", i + 1);
                                detailInfo.setGateCode(String.format("%d", i + 1));
                                String value = (String) datas.get(key);
                                if (value == null || value.equals("FFFFFF")) {

                                } else {
                                    detailInfo.setOpenHeight(new BigDecimal(value));
                                }

                                detailInfo.setSTCD(stcd);
                                detailInfo.setTM(new Timestamp(timeFormat.parse(tm).getTime()));
                                detailInfoList.add(detailInfo);
                            }
                        }
                    }
                }
                waterGateInfo.setDetailInfoList(detailInfoList);
                waterGateInfo.setGateSum(detailInfoList.size());

                if (DataProcess.config().isDataShare()) {
                    Object obj = JSONObject.toJSON(waterGateInfo);
                    String json = obj.toString();
                    JSONObject data = new JSONObject();
                    data.put("kind", "5A");
                    data.put("data", json);
                    String queue = DataProcess.queueConfig().getShareMessage();
                    DataProcessHolder.newInstance().dataProcess.kafkaTemplate.send(queue, data.toJSONString());
                }

                swService.SaveSwWaterGateData(waterGateInfo);
            }

            //记录状态
            dataItem = dataTemplateInterface.MacthDataItem("t_base_rtu_info");
            if (dataItem.size() > 0) {
                RtuStatusEntity statusEntity = new RtuStatusEntity();
                String stcd = BytesHelp.byte2HexStr(dataTemplateInterface.RtuCode());
                StringBuffer dataStr = new StringBuffer(BytesHelp.byte2HexStr(dataTemplateInterface.CollectTime()));
                String tm = "20" + dataStr.toString() + "00";
                SimpleDateFormat timeFormat = new SimpleDateFormat("yyyyMMddHHmmss");
                statusEntity.setCollectTime(new Timestamp(timeFormat.parse(tm).getTime()));
                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);
            }
        } catch (Exception e) {
            LogHelper.error(e);
        }
    }
}
