package org.ts.ddcs.DataSaveProcesser;

import org.ts.ddcs.DataItemTemplate.DataItemTemplateInterface;
import org.ts.ddcs.DataTemplate.DataTemplateInterface;
import org.ts.ddcs.Entity.CommandEntity;
import org.ts.ddcs.Entity.PptnEntity;
import org.ts.ddcs.Entity.RtuStatusEntity;
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.springframework.data.redis.core.HashOperations;

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

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

            SwService swService = DataProcessHolder.newInstance().dataProcess.swService;
            LinkedList<DataItemTemplateInterface> dataItem = dataTemplateInterface.MacthDataItem("t_base_rtu_info");
            if (dataItem.size() > 0) {

                RtuStatusEntity entity = new RtuStatusEntity();
                entity.setStcd(rtucode);
                StringBuffer dataStr = new StringBuffer(BytesHelp.byte2HexStr(dataTemplateInterface.SendPacketTime()));
                String tm = "20" + dataStr.toString();
                tm = tm.substring(0,tm.length()-2)+"00";
                SimpleDateFormat timeFormat = new SimpleDateFormat("yyyyMMddHHmmss");
                entity.setCollectTime(new Timestamp(timeFormat.parse(tm).getTime()));
                for(DataItemTemplateInterface item:dataItem){
                    item.SetEntity(entity);
                }
                swService.SaveSwRtuStatus(entity);
                String ccid = entity.getCcid();
                if (null != ccid){
                    HashOperations hashOperations = DataProcessHolder.newInstance().dataProcess.redisTemplate.opsForHash();
                    hashOperations.put(rtucode,"ccid",ccid);
                }
            }
        }catch (Exception e){
            LogHelper.error(e);
        }

       // DataProcess.DownloadNotify(rtucode);
    }
}
