package com.zd.system.api.laboratory.domain;

import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.zd.common.core.enums.SenseType;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Optional;

/**
 * 传感器功能
 * @Author: zhoupan
 * @Date: 2021/09/27/15:36
 * @Description:
 */
@ApiModel("传感器功能")
public class SensorFunctionStatus extends BasicFunctionStatus {
    public static DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    private static final String format_val ="{}{}";
    /**
     * 传感器id,用于区分多个一样的设备。
     */
    @ApiModelProperty("传感器id")
    private Long id;

    /**
     * 传感器功能值
     */
    @ApiModelProperty("传感器功能值")
    private String val;
    /**
     * 传感器功能值
     */
    @ApiModelProperty("格式化值")
    private String formatVal;


    private String defaultVal="--";

    /**
     * 保留小数点位数
     */
    @JsonIgnore
    private Integer numberDecimal;

    /**
     * 传感器功能值
     */
    @ApiModelProperty("功能值单位")
    private String unit;

    @ApiModelProperty("路由位数")
    @JsonIgnore
    private Integer routeNum;


    @ApiModelProperty("是否存在预警")
    private Boolean hasWarn = false;

    private String sendDate;

    private SenseType type;

    private String  configName;

    public SensorFunctionStatus() {
        //获取当前时间
        LocalDateTime newTime = LocalDateTime.now();
        String format = formatter.format(newTime);
        this.sendDate = format;
    }

    public Boolean getHasWarn() {
        return hasWarn;
    }

    public void setHasWarn(Boolean hasWarn) {
        this.hasWarn = hasWarn;
    }

    public Integer getRouteNum() {
        return routeNum;
    }

    public void setRouteNum(Integer routeNum) {
        this.routeNum = routeNum;
    }

    public String getRoundVlue() {
        if(val==null) return defaultVal;

        if(numberDecimal!=null&&!val.equals(defaultVal))
        {
            String s = NumberUtil.roundStr(val, numberDecimal);
            return s;
        }
        return val;
    }

    public Integer getNumberDecimal() {
        return numberDecimal;
    }

    public void setNumberDecimal(Integer numberDecimal) {
        this.numberDecimal = numberDecimal;
    }


    /**
     * 命令
     */
    @ApiModelProperty("命令")
    private String cmd;

    public String getFormatVal() {

        return StrUtil.format(format_val,getRoundVlue(),Optional.ofNullable(unit).orElse(StrUtil.EMPTY));
    }


    public String getUnit() {
        return unit;
    }

    public void setUnit(String unit) {
        this.unit = unit;
    }

    public String getCmd() {
        return cmd;
    }

    public void setCmd(String cmd) {
        this.cmd = cmd;
    }


    public String getVal() {
        if(val==null) return defaultVal;
        return val;
    }

    public void setVal(String val) {
        this.val = val;
    }

    public String getSendDate() {
        return sendDate;
    }

    public void setSendDate(String sendDate) {
        this.sendDate = sendDate;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public SenseType getType() {
        return type;
    }

    public void setType(SenseType type) {
        this.type = type;
    }

    public String getConfigName() {
        return configName;
    }

    public void setConfigName(String configName) {
        this.configName = configName;
    }
}
