package com.iailab.module.data.point.collection.utils;

import com.iailab.module.data.common.enums.DataTypeEnum;
import com.iailab.module.data.point.dto.DaPointDTO;
import com.iailab.module.data.influxdb.pojo.*;

/**
 * @author PanZhibao
 * @Description
 * @createTime 2023年05月08日 13:34:00
 */
public class GenInfluxPointValueUtils {

    public static InfluxPointValuePOJO getByPoint(DaPointDTO dto){
        if (DataTypeEnum.FLOAT.getCode().equals(dto.getDataType())) {
            InfluxPointValuePOJO pojo = new InfluxPointValueSimPOJO();
            pojo.setPoint(dto.getPointNo());
            if (dto.getDefaultValue() != null) {
                ((InfluxPointValueSimPOJO) pojo).setValue(dto.getDefaultValue().doubleValue());
            }
            return pojo;
        } else if (DataTypeEnum.INT.getCode().equals(dto.getDataType())) {
            InfluxPointValuePOJO pojo = new InfluxPointValueDigPOJO();
            pojo.setPoint(dto.getPointNo());
            if (dto.getDefaultValue() != null) {
                ((InfluxPointValueDigPOJO) pojo).setValue(dto.getDefaultValue().intValue());
            }
            return pojo;
        } else if (DataTypeEnum.BOOLEAN.getCode().equals(dto.getDataType())) {
            return new InfluxPointValueDigPOJO();
        } else {
            return new InfluxPointValueStrPOJO();
        }
    }

    public static InfluxPointValuePOJO getByPoint(DaPointDTO dto, Object value){
        if (DataTypeEnum.FLOAT.getCode().equals(dto.getDataType())) {
            InfluxPointValuePOJO pojo = new InfluxPointValueSimPOJO();
            pojo.setPoint(dto.getPointNo());
            if (dto.getDefaultValue() != null) {
                ((InfluxPointValueSimPOJO) pojo).setValue(dto.getDefaultValue().doubleValue());
            }
            if (value != null) {
                ((InfluxPointValueSimPOJO) pojo).setValue(Double.parseDouble(value.toString()));
            }
            return pojo;
        } else if (DataTypeEnum.INT.getCode().equals(dto.getDataType())) {
            InfluxPointValuePOJO pojo = new InfluxPointValueDigPOJO();
            pojo.setPoint(dto.getPointNo());
            if (dto.getDefaultValue() != null) {
                ((InfluxPointValueDigPOJO) pojo).setValue(dto.getDefaultValue().intValue());
            }
            if (value != null) {
                ((InfluxPointValueDigPOJO) pojo).setValue(Integer.parseInt(value.toString()));
            }
            return pojo;
        } else if (DataTypeEnum.BOOLEAN.getCode().equals(dto.getDataType().trim())) {
            InfluxPointValuePOJO pojo = new InfluxPointValueBoolPOJO();
            pojo.setPoint(dto.getPointNo());
            if (value != null) {
                ((InfluxPointValueBoolPOJO) pojo).setValue(Boolean.parseBoolean(value.toString()));
            }
            return pojo;
        } else {
            InfluxPointValuePOJO pojo = new InfluxPointValueStrPOJO();
            if (value != null) {
                ((InfluxPointValueStrPOJO) pojo).setValue(value.toString());
            }
            return new InfluxPointValueStrPOJO();
        }
    }
}