package org.ts.ddcs.DataSaveProcesser;


import com.alibaba.fastjson.JSONArray;
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;


/**
 * 水资源查询测站编码
 * 0x50
 */
@SuppressWarnings("all")
public class DataSaveProcesserSzyC50 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);
                JSONArray items = (JSONArray) data.get("data");
                String ver = (String) items.get(0);
                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("50 SAVE:" + rtucode + " " + ver);
                }
            }
        } catch (Exception e) {
            LogHelper.error(e);
        }
    }
}
