package org.ts.ddcs.DataSaveProcesser;

import org.ts.ddcs.DataItemTemplate.DataItemTemplateInterface;
import org.ts.ddcs.DataTemplate.DataTemplateInterface;
import org.ts.ddcs.Entity.FlowRateEntity;
import org.ts.ddcs.Entity.WqRiverEntity;
import org.ts.ddcs.log.LogHelper;
import org.ts.ddcs.Service.SzyService;
import org.ts.ddcs.Service.WqService;
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;
import java.util.UUID;

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

            LinkedList<DataItemTemplateInterface> dataItem = dataTemplateInterface.MacthDataItem("wq_river");
            if (dataItem.size() > 0) {
                WqService wqService = DataProcessHolder.newInstance().dataProcess.wqService;
                WqRiverEntity wqRiverEntity = new WqRiverEntity();
                StringBuffer dataStr = new StringBuffer(BytesHelp.byte2HexStr(dataTemplateInterface.CollectTime()));
                String tm = BytesHelp.byte2HexStr(dataTemplateInterface.CollectTime());
                String dt = BytesHelp.byte2HexStr(dataTemplateInterface.SendPacketTime());
                String ts = BytesHelp.byte2HexStr(dataTemplateInterface.ReceiveTime());

                SimpleDateFormat timeFormat = new SimpleDateFormat("yyyyMMddHHmmss");
                String ct = timeFormat.format(new Date());
                wqRiverEntity.setTM(new Timestamp(timeFormat.parse(ct.substring(0, 2) + tm).getTime()));
                wqRiverEntity.setDT(new Timestamp(timeFormat.parse(ct.substring(0, 2) + dt).getTime()));
                wqRiverEntity.setTS(new Timestamp(timeFormat.parse(ct.substring(0, 2) + ts).getTime()));

                wqRiverEntity.setSTCD(rtuCode);
                for (DataItemTemplateInterface item : dataItem) {
                    item.SetEntity(wqRiverEntity);
                    if (item.GetColumnName().equals("FLOW_SPEED")) {
                        wqService.SaveWqSpeedData(wqRiverEntity);
                    } else if (item.GetColumnName().equals("WATER_HIGH")) {
                        wqService.SaveWqHighData(wqRiverEntity);
                    }
                }
            }
            dataItem = dataTemplateInterface.MacthDataItem("t_szy_flow_rate");
            if (dataItem.size() > 0) {
                String batchId = UUID.randomUUID().toString().replace("-", "").toLowerCase();
                SzyService szyService = DataProcessHolder.newInstance().dataProcess.szyService;
                FlowRateEntity flowRateEntity = new FlowRateEntity();
                flowRateEntity.setId(batchId);
                flowRateEntity.setMpCode(rtuCode);
                String datagramPickTime = dataTemplateInterface.SupplementInfo();
                String tm = BytesHelp.byte2HexStr(dataTemplateInterface.CollectTime());
                String ts = BytesHelp.byte2HexStr(dataTemplateInterface.ReceiveTime());
                if (null != datagramPickTime) {
                    SimpleDateFormat timeFormat = new SimpleDateFormat("yyyyMMddHHmmss");
                    tm = datagramPickTime.substring(0,6) + tm.substring(4);
                    ts = datagramPickTime.substring(0,6)  + ts.substring(4);
                    flowRateEntity.setTm(new Timestamp(timeFormat.parse(tm).getTime()));
                    flowRateEntity.setTs(new Timestamp(timeFormat.parse(ts).getTime()));
                } else {
                    SimpleDateFormat timeFormat = new SimpleDateFormat("yyyyMMddHHmmss");
                    String ct = timeFormat.format(new Date());
                    flowRateEntity.setTm(new Timestamp(timeFormat.parse(ct.substring(0, 2) + tm).getTime()));
                    flowRateEntity.setTs(new Timestamp(timeFormat.parse(ct.substring(0, 2) + ts).getTime()));
                }
                for (DataItemTemplateInterface item : dataItem) {
                    if (item.GetColumnName().equals("FLOW_RATE_R_01")) {
                        flowRateEntity.setInstantFlowrateFirst(item.GetVarchar());
                    } else if (item.GetColumnName().equals("FLOW_RATE_S_01")) {
                        flowRateEntity.setAccFlowrateFirst(item.GetVarchar());
                    } else if (item.GetColumnName().equals("FLOW_RATE_R_02")) {
                        flowRateEntity.setInstantFlowrateSecond(item.GetVarchar());
                    } else if (item.GetColumnName().equals("FLOW_RATE_S_02")) {
                        flowRateEntity.setAccFlowrateSecond(item.GetVarchar());
                    }
                }
                szyService.SaveFlowRateData(flowRateEntity);
            }

        } catch (Exception e) {
            LogHelper.error(e);
        }
    }
}
