package org.ts.ddcs.DataSaveProcesser;


import com.alibaba.fastjson.JSONObject;
import org.ts.ddcs.DataTemplate.DataTemplateInterface;
import org.ts.ddcs.Log.LogHelper;
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 org.springframework.data.redis.core.RedisTemplate;

import java.util.LinkedList;
import java.util.List;


@SuppressWarnings("all")
public class DataSaveProcesserCF1 extends DataSaveProcesser {
    @Override
    public void Save(DataTemplateInterface dataTemplateInterface) {
        super.Save(dataTemplateInterface);
        LogHelper.info("SAVE:" + dataTemplateInterface.GetFeture());

        String rtuCode = BytesHelp.byte2HexStr(dataTemplateInterface.RtuCode());

        byte[] content = dataTemplateInterface.GetContent();
        byte[] packageIndexBuff = BytesHelp.subBytes(content, 8, 2);
        int packageIndex = BytesHelp.bytes2Int(packageIndexBuff, 0, 2);

        //LogHelper.info("F1 " + packageIndex);
        if (DataProcess.config().isRelayToTaishan()) {
            RedisTemplate redisTemplate = DataProcessHolder.newInstance().dataProcess.redisTemplate;
            HashOperations<String, String, String> ops = redisTemplate.opsForHash();
            String toRtuFileInfo = ops.get(rtuCode, "toRtuFileInfo");
            JSONObject downFileBody = JSONObject.parseObject(toRtuFileInfo);
            long fileBlockSize = downFileBody.getLong("fileBlockSize");
            if (packageIndex == fileBlockSize) {
                JSONObject dataInfo = new JSONObject();
                dataInfo.put("func", "F1");
                List<JSONObject> items = new LinkedList<>();
                JSONObject item = new JSONObject();
                item.put("name", "文件上传");
                item.put("value", "第 " + packageIndex + " 包");
                items.add(item);

                 item = new JSONObject();
                item.put("name", "文件上传");
                item.put("value", "已上传完成");
                items.add(item);
                dataInfo.put("data", items);
                DataProcessHolder.newInstance().dataProcess.SubmitRelayManageTask(rtuCode, dataInfo);
            }
        }

    }
}
