package com.zcxk.charge.api.vo;

import com.zcxk.charge.api.enums.MathematicalSymbolEnum;
import com.zcxk.core.utils.CalcUtil;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;

/**
 * @description
 * @author  wangli
 * @data 2020-11-19 9:31
 */
@Data
@Slf4j
public class DeviceCheckAlarmVo {

    @ApiModelProperty(value = "租户ID")
    private String tenantId;

    @ApiModelProperty(value = "一级场景id")
    private Integer parentSceneId;

    @ApiModelProperty(value = "一级场景名称")
    private String parentSceneName;

    @ApiModelProperty(value = "所属场景id")
    private Integer sceneId;

    @ApiModelProperty(value = "所属场景名称")
    private String sceneName;

    @ApiModelProperty(value = "设备id")
    private Integer deviceId;

    @ApiModelProperty(value = "设备名称")
    private String deviceName;

    @ApiModelProperty(value = "公司id")
    private Integer companyOrgId;

    @ApiModelProperty(value = "部门id")
    private Integer deptOrgId;

    @ApiModelProperty(value = "地址")
    private String address;

    @ApiModelProperty(value = "报警类型")
    private String alarmType;

    @ApiModelProperty(value = "设备属性")
    private Integer attributeId;

    @ApiModelProperty(value = "上次上报时间")
    private Date lastUpdateTime;

    @ApiModelProperty(value = "报警设置id")
    private Integer alarmSettingId;

    @ApiModelProperty(value = "离线时间")
    private Double duration;

    @ApiModelProperty(value = "报警规则（符号）")
    private String alarmCondition;

    @ApiModelProperty(value = "报警值")
    private Double alarmValue;

    @ApiModelProperty(value = "设备属性字段")
    private String identifiter;

    @ApiModelProperty(value = "属性名称")
    private String attributeName;

    @ApiModelProperty(value = "属性单位")
    private String unit;

    @ApiModelProperty(value = "报警设置开始时间",hidden = true)
    private String startTime;

    @ApiModelProperty(value = "报警设置结束时间" ,hidden = true)
    private String endTime;

    /**
    * 识别符号 >、≥、<、≤、=、≠
    * @author Andy
    * @date 16:16 2021/8/17
    * @param :
    * @return java.lang.Boolean
    **/
    public Boolean checkdeviceOffLine(){
        if(this.duration != null && this.alarmValue != null
              &&  StringUtils.isNotBlank(this.alarmCondition)){
               return CalcUtil.compareBySign(this.duration,this.alarmValue,this.alarmCondition);
        }
        return false;
    }

    public Boolean checkdeviceAttributeAlarm(Double value){
        if( value != null && alarmValue != null
                &&  StringUtils.isNotBlank(this.alarmCondition)){
                return CalcUtil.compareBySign(value,this.alarmValue,this.alarmCondition);
        }
        return false;
    }

    /**
    * 识别符号 >、≥、<、≤、=、≠
     *  A.报警规则为（≥、＞、≤、＜）：【设备名称】+【报警字段】+【字段数值】+【单位】+【报警规则】+【报警字段】+【设定数值】+【单位】
     *  B.报警规则为（=）：【设备名称】+【报警字段】
     *  C.报警规则为（≠）：【设备名称】+【报警字段】 + 不等于 + 【字段数值】 + 【单位】
     *  D.状态类型报警详情规则：【设备名称】+【状态】+【报警规则】+【数值】+单位
    * @author Andy
    * @date 16:31 2021/8/17
    * @param receivedValue:
    * @return java.lang.String
    **/
    public String getAlarminfo(Double receivedValue){
        if(MathematicalSymbolEnum.EQ.getSymbol().equals(this.alarmCondition)){
            return  new StringBuilder().append(this.deviceName)
                    .append(this.attributeName).toString();
        }else if(MathematicalSymbolEnum.NEQ.getSymbol().equals(this.alarmCondition)){
            return  new StringBuilder().append(this.deviceName)
                    .append(this.attributeName)
                    .append(this.alarmCondition)
                    .append(this.alarmValue)
                    .append(StringUtils.isNotBlank(this.unit)?this.unit:"").toString();
        }
        String str = receivedValue.toString();
        BigDecimal bigDecimalScale = new BigDecimal(str).setScale(2, RoundingMode.UP);
        return  new StringBuilder().append(this.deviceName).append(this.attributeName)
                    .append(bigDecimalScale)
                    .append(StringUtils.isNotBlank(this.unit)?this.unit:"")
                    .append(this.alarmCondition)
                    .append(this.alarmValue)
                    .append(StringUtils.isNotBlank(this.unit)?this.unit:"").toString();
    }
}
