package org.ts.ddcs.DataSaveProcesser;

import com.alibaba.fastjson.JSONObject;
import org.ts.ddcs.DataTemplate.DataTemplateInterface;
import org.ts.ddcs.Entity.RtuBaseInfo;
import org.ts.ddcs.log.LogHelper;
import org.ts.ddcs.Tool.BytesHelp;
import org.ts.ddcs.Worker.DataProcessHolder;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.List;

/**
 * 水文规约查询版本号
 */
public class DataSaveProcesserC45 extends DataSaveProcesser {
    @Override
    public void Save(DataTemplateInterface dataTemplateInterface) {
        try {
            super.Save(dataTemplateInterface);
            RedisTemplate redisTemplate = DataProcessHolder.newInstance().dataProcess.redisTemplate;
            HashOperations<String, String, String> opsHash = redisTemplate.opsForHash();
            String rtucode = BytesHelp.byte2HexStr(dataTemplateInterface.RtuCode());

            String dataInfo = dataTemplateInterface.getDataInfo();
            if (null != dataInfo && dataInfo.length() > 0) {
                JSONObject data = JSONObject.parseObject(dataInfo);
                List<JSONObject> items = (List<JSONObject>) data.get("data");
                String ver = items.get(0).getString("value");
                if (null != ver) {
                    RtuBaseInfo rtuBaseInfo = new RtuBaseInfo();
                    rtuBaseInfo.setRtuCode(rtucode);
                    rtuBaseInfo.setRtuVersion(ver);
                    DataProcessHolder.newInstance().dataProcess.rtuService.updateRtuVersion(rtuBaseInfo);
                    opsHash.put(rtucode, "version", ver);

                    LogHelper.info("45 SAVE:" + rtucode + " " + ver);
                }
            }
        } catch (Exception e) {
            LogHelper.error(e);
        }
    }
}
