package cn.wise.sc.energy.power.plant.business.domain;

import cn.hutool.core.codec.Base64Encoder;
import com.alibaba.fastjson.JSON;
import com.spring4all.spring.boot.starter.hbase.api.RowMapper;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.util.Bytes;
import java.util.List;

/**
 * @description: rowKey
 * @author: qh
 * @create: 2020-09-02 11:38
 **/
public class RowKeyMapper implements RowMapper<Oscillogram> {

    private static byte[] REORDSFAMILY = "records".getBytes();
    private static byte[] DATA = "Data".getBytes();
    private static byte[] TAGSFAMILY = "tags".getBytes();
    private static byte[] TIMESPAN = "TimeSpan".getBytes();
    private static byte[] DATATYPE = "DataType".getBytes();
    private static byte[] DIRECTION = "Direction".getBytes();
    private static byte[] GENERATIONFREQ = "GenerationFreq".getBytes();
    private static byte[] KKSCODE = "KKSCode".getBytes();
    private static byte[] PARTTYPE = "PartType".getBytes();
    private static byte[] POLENUM = "PoleNum".getBytes();
    private static byte[] RECORDFLAG = "RecordFlag".getBytes();
    private static byte[] SIGNALTYPE = "Signaltype".getBytes();
    private static byte[] STARTTIME = "StartTime".getBytes();
    private static byte[] OFFSET = "KeyPhaseOffset".getBytes();

    @Override
    public Oscillogram mapRow(Result result, int rowNum) {

        String timeSpan = Bytes.toString(result.getValue(TAGSFAMILY, TIMESPAN));
        String dataType = Bytes.toString(result.getValue(TAGSFAMILY, DATATYPE));
        String direction = Bytes.toString(result.getValue(TAGSFAMILY, DIRECTION));
        String generationFreq = Bytes.toString(result.getValue(TAGSFAMILY, GENERATIONFREQ));
        String kKSCode = Bytes.toString(result.getValue(TAGSFAMILY, KKSCODE));
        String partType = Bytes.toString(result.getValue(TAGSFAMILY, PARTTYPE));
        String poleNum = Bytes.toString(result.getValue(TAGSFAMILY, POLENUM));
        String recordFlag = Bytes.toString(result.getValue(TAGSFAMILY, RECORDFLAG));
        String signaltype = Bytes.toString(result.getValue(TAGSFAMILY, SIGNALTYPE));
        String startTime = Bytes.toString(result.getValue(TAGSFAMILY, STARTTIME));
        List<Float> data = JSON.parseArray(Bytes.toString(result.getValue(REORDSFAMILY, DATA)), Float.class);
        Oscillogram oscillogram = new Oscillogram();
        oscillogram.setRowKey(Base64Encoder.encode(result.getRow()));
        oscillogram.setPeriod(0);
        oscillogram.setKKsCode(kKSCode);
        oscillogram.setMapData(data);
        //获取偏移量
        oscillogram.setNoVersionKeyPhaseOffset(JSON.parseArray(Bytes.toString(result.getValue(REORDSFAMILY, OFFSET)), Float.class));
        try {
            oscillogram.setTimeSpan(Long.parseLong(timeSpan));
        } catch (NumberFormatException ex) {
            oscillogram.setTimeSpan(0L);
        }
        try {
            oscillogram.setRecordFlag(Integer.parseInt(recordFlag));
        } catch (NumberFormatException ex) {
            oscillogram.setRecordFlag(1);
        }
        try {
            oscillogram.setGenerationFreq(Integer.parseInt(generationFreq));
        } catch (NumberFormatException ex) {
            oscillogram.setGenerationFreq(1);
        }
        try {
            oscillogram.setPoleNum(Integer.parseInt(poleNum));
        } catch (NumberFormatException ex) {
            oscillogram.setPoleNum(0);
        }
        try {
            oscillogram.setStart(Long.parseLong(startTime));
        } catch (NumberFormatException ex) {
            throw new RuntimeException("HBase这个波形图开始时间不为0");
        }
        return oscillogram;
    }
}
