package com.xydl.cac.entity;

import com.fasterxml.jackson.annotation.JsonInclude;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@JsonInclude(JsonInclude.Include.NON_NULL)
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name = "n_sensor")
@ApiModel("装置表")
public class NSensor {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Integer id;

    @ApiModelProperty("设备名称")
    @Column(name = "name")
    private String name;

    @ApiModelProperty("设备的相别")
    @Column(name = "phase")
    private String phase;

    @Column(name = "type_id")
    @ApiModelProperty("类型Id")
    private Integer typeId;

    @ApiModelProperty("传感器的唯一标识")
    @Column(name = "sensor_code")
    private String sensorCode;

    @ApiModelProperty("被监测设备的唯一标识")
    @Column(name = "equipment_code")
    private String equipmentId;

    @ApiModelProperty("装置ID")
    @Column(name = "dev_id")
    private Integer devId;

    @ApiModelProperty("设备状态")
    @Column(name = "status")
    private Integer status;

    @Column(name = "zsb_id")
    @ApiModelProperty("主设备Id")
    private Integer zsbId;

    @Column(name = "icd_id")
    @ApiModelProperty("关联icd表中的id")
    private Integer icdId;

    @Column(name = "order_num")
    @ApiModelProperty("排序编号")
    private Integer orderNum;

    @ApiModelProperty("ip")
    @Column(name = "ip")
    private String ip;

    @Column(name = "port")
    @ApiModelProperty("port")
    private Integer port;

    @ApiModelProperty("username")
    @Column(name = "username")
    private String username;

    @ApiModelProperty("passwd")
    @Column(name = "passwd")
    private String passwd;

    @Column(name = "tm_id")
    @ApiModelProperty("tmId")
    private Integer tmId;

    @Column(name = "tm_type")
    @ApiModelProperty("tmType")
    private Integer tmType;

    @ApiModelProperty("主设备名称")
    @Transient
    private String zsbName;

    @ApiModelProperty("区域名称")
    @Transient
    private String jgName;

    @ApiModelProperty("类型名称")
    @Transient
    private String typeName;

    @ApiModelProperty("设备对应的表名")
    @Transient
    private String tableName;

    @Transient
    private List<NPoint> points;

    @Transient
    N103Sensor n103Sensor;

    @Transient
    HashMap<String, String> lastData;
    @Transient
    List<ModevTypePoint> typePoints;

    public String addPoint(NPoint point) {
        if (points == null) {
            points = new ArrayList<>();
        }
        for (NPoint item : points) {
            if (item.getField().equalsIgnoreCase(point.getField())) {
                return "第" + point.getRow() + "行和第" + item.getRow() +
                        "行存在同一标识(" + sensorCode + ")的相同字段(" + point.getField() + ")";
            }
        }
        points.add(point);
        return null;
    }

    public boolean canTempMeasure() {
        if (StringUtils.isNotBlank(ip) && StringUtils.isNotBlank(username) && StringUtils.isNotBlank(passwd)
                && port != null && tmId != null && tmType != null) {
            return true;
        } else {
            return false;
        }
    }
}