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.springframework.util.CollectionUtils;

import javax.persistence.*;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@JsonInclude(JsonInclude.Include.NON_NULL)
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name = "jg")
@ApiModel("区域间隔表")
public class Jg {

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

    @NotBlank(message = "名称不能为空")
    @Size(max = 50, message = "名称长度不能超过50个字符")
    @ApiModelProperty("名称")
    @Column(name = "mc")
    private String mc;

    @NotNull(message = "变电站id不能为空")
    @ApiModelProperty("变电站id")
    @Column(name = "bdzid")
    private Integer bdzid;

    @Column(name = "ls")
    private BigDecimal ls;

    @Column(name = "areaid")
    private String areaid;

    @Column(name = "xh")
    private BigDecimal xh;

    @ApiModelProperty("变电站名称")
    @Transient
    private String bdzName;

    @Transient
    private long sensorCount;

    @Transient
    private List<Zsb> children;

    public void addChild(Zsb child, boolean ignoreEmpty) {
        if (children == null) {
            children = new ArrayList<>();
        }
        if (ignoreEmpty && CollectionUtils.isEmpty(child.getChildren())) {
        } else {
            children.add(child);
        }
    }
}