package com.jiayue.ipfcst.common.data.entity;

import com.jiayue.ipfcst.common.data.constant.enums.DataSourceEnum;
import com.jiayue.ipfcst.common.data.constant.enums.EquipmentTypeEnum;
import lombok.Data;
import org.hibernate.annotations.GenericGenerator;

import javax.persistence.*;

/**
 * 协议数据点表
 *
 * @author 修唯xiuwei
 * @version 3.0
 */
@Data
@Entity
public class ProtocolGatherDataPoint {

    /**
     * id主键
     */
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO, generator = "myid")
    @GenericGenerator(name = "myid", strategy = "com.jiayue.ipfcst.common.data.entity.id.CustomIDGenerator")
    private Integer id;


    /**
     * 该点位是否是采集得到还是通过规则生成
     */
    @Column(length = 1)
    private DataSourceEnum dataSource;

    /**
     * 通道号
     */
    @Column(length = 32)
    private String tunnelId;

    /**
     * 通道协议中对应的顺序位
     */
    @Column(length = 7)
    private Integer protocolPointNo;


    /**
     * 通道协议中对应的数据类型
     * modbus 包含所有的数据类型
     * 104 仅包含遥测  和 遥信
     * CDT 仅包含遥测  和 遥信
     */
    @Column(length = 9)
    @Enumerated(EnumType.STRING)
    private ProtocolDataType protocolPointType;


    /**
     * 该点位对应的设备
     */
    @Column(length = 5)
    private Integer equipmentId;


    /**
     * 协议点描述的设备属性
     */
    @OneToOne
    @JoinColumn(name = "equipmentAttributeId", foreignKey = @ForeignKey(value = ConstraintMode.NO_CONSTRAINT))
    private EquipmentAttribute equipmentAttribute;

    /**
     * 倍率
     */
    @Column(precision = 10, scale = 4)
    private Double magnification;


    /**
     * 用于生成需要内部生成的属性的规则
     */
    @Column(length = 2000)
    private String generationRegulation;


    /**
     * 允许的最大变化值
     */
    @Column(precision = 7, scale = 2)
    Double maxChangeValue;


    /**
     * 允许的最大值
     */
    @Column(precision = 7, scale = 2)
    Double maxValue;

    /**
     * 允许的最小值
     */
    @Column(precision = 7, scale = 2)
    Double minValue;


    /**
     * 该数据点位是否需要数据合理性考核
     */
    @Column(columnDefinition="bit(1)  default false")
    Boolean needAudit;

    /**
     * 该数据点位是否在首页展示
     */
    @Column(columnDefinition = "tinyint default 0")
    Integer showOnHomePage;

    /**
     * 允许数值不发生变化的最长时长   单位为 min 分钟
     * 当值为空时 说明不限制时长
     */
    Long maxDuration;


    /**
     * 场站标识
     */
    @Column
    private String stationCode;


    public EquipmentTypeEnum getEquipmentType() {
        if (getEquipmentAttribute() == null) {
            return null;
        } else {
            return this.getEquipmentAttribute().getEquipmentType();
        }
    }

    public void setEquipmentType(EquipmentTypeEnum equipmentType) {
        if (getEquipmentAttribute() == null) {
            EquipmentAttribute att = new EquipmentAttribute();
            att.setEquipmentType(equipmentType);
            setEquipmentAttribute(att);
        } else {
            getEquipmentAttribute().setEquipmentType(equipmentType);
        }
    }


}
