package com.its.op.entity.its.vds;

import com.its.op.dto.its.vds.TbVdsDtctDto;
import com.its.op.global.CodeManager;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction;

import javax.persistence.*;
import java.io.Serializable;

/**
 * VDS 검지기 Entity Class
 */
@Getter
@NoArgsConstructor//(access = AccessLevel.PROTECTED)
@Builder
@AllArgsConstructor
@ApiModel("VDS 검지기")
@Entity
@Table(name = "TB_VDS_DTCT")
public class TbVdsDtct implements Serializable {
    private static final long serialVersionUID = 1L;

    @ApiModelProperty("VDS 검지기 번호")  // N NUMBER(10)
    @Id
    @Column(name = "VDS_DTCT_NMBR", nullable = false, columnDefinition = "NUMBER", length = 10)
    private Long vdsDtctNmbr;

    @ApiModelProperty("VDS 검지기 ID")  // N VARCHAR2(10)
    @Column(name = "VDS_DTCT_ID", nullable = false, length = 10)
    private String vdsDtctId;

    @ApiModelProperty("VDS 검지기 명칭")  // Y VARCHAR2(40)
    @Column(name = "VDS_DTCT_NM", length = 40)
    private String vdsDtctNm;

    @ApiModelProperty("VDS 제어기 번호")  // N NUMBER(10)
    @Column(name = "VDS_CTLR_NMBR", nullable = false, columnDefinition = "NUMBER", length = 10)
    private Long vdsCtlrNmbr;

    @ApiModelProperty("VDS 카메라 번호")  // N NUMBER(1)
    @Column(name = "VDS_CMRA_NO", nullable = false, columnDefinition = "NUMBER", length = 1)
    private Integer vdsCmraNo;

    @ApiModelProperty("검지기 유형('DTT')")  // Y VARCHAR2(7)
    @Column(name = "DTCT_TYPE", length = 7)
    private String dtctType;

    @ApiModelProperty("검지기 방향('VCD')")  // N VARCHAR2(7)
    @Column(name = "DTCT_DIR", nullable = false, length = 7)
    private String dtctDir;

    @ApiModelProperty("검지차로(제어기설치차로)")  // Y NUMBER(2)
    @Column(name = "DETECT_LANE", columnDefinition = "NUMBER", length = 2)
    private Integer detectLane;

    @ApiModelProperty("설치 차로")  // Y NUMBER(2)
    @Column(name = "ISTL_LANE", columnDefinition = "NUMBER", length = 2)
    private Integer istlLane;

    @ApiModelProperty("속도수집여부")  // Y CHAR(1)
    @Column(name = "SPED_CLCT_YN", columnDefinition = "CHAR", length = 1)
    private String spedClctYn;

    @ApiModelProperty("교통량수집여부")  // Y CHAR(1)
    @Column(name = "TFVL_CLCT_YN", columnDefinition = "CHAR", length = 1)
    private String tfvlClctYn;

    @ApiModelProperty("점유율수집여부")  // Y CHAR(1)
    @Column(name = "OCPY_CLCT_YN", columnDefinition = "CHAR", length = 1)
    private String ocpyClctYn;

    @ApiModelProperty("최대 교통량")  // Y NUMBER(6)
    @Column(name = "MAX_TFVL", columnDefinition = "NUMBER", length = 6)
    private Integer maxTfvl;

    @ApiModelProperty("최소 교통량")  // Y NUMBER(6)
    @Column(name = "MIN_TFVL", columnDefinition = "NUMBER", length = 6)
    private Integer minTfvl;

    @ApiModelProperty("최대 점유 율")  // Y NUMBER(6,2)
    @Column(name = "MAX_OCPY_RATE", columnDefinition = "NUMBER", length = 6, precision = 2)
    private Double maxOcpyRate;

    @ApiModelProperty("최소 점유 율")  // Y NUMBER(6,2)
    @Column(name = "MIN_OCPY_RATE", columnDefinition = "NUMBER", length = 6, precision = 2)
    private Double minOcpyRate;

    @ApiModelProperty("최대 속도")  // Y NUMBER(3)
    @Column(name = "MAX_SPED", columnDefinition = "NUMBER", length = 3)
    private Integer maxSped;

    @ApiModelProperty("최소 속도")  // Y NUMBER(3)
    @Column(name = "MIN_SPED", columnDefinition = "NUMBER", length = 3)
    private Integer minSped;

    @ApiModelProperty("사용여부")  // Y CHAR(1)
    @Column(name = "USE_YN", columnDefinition = "CHAR", length = 1)
    private String useYn;

    @ApiModelProperty("삭제 여부")  // Y CHAR(1)
    @Column(name = "DEL_YN", columnDefinition = "CHAR", length = 1)
    private String delYn;

    @OneToOne
    @JoinColumn(insertable=false, updatable=false, name="VDS_CTLR_NMBR", referencedColumnName = "VDS_CTLR_NMBR")
    @NotFound(action = NotFoundAction.IGNORE)
    private TbVdsCtlr vds;

    @OneToOne
    @JoinColumn(insertable=false, updatable=false, name="VDS_DTCT_NMBR", referencedColumnName = "VDS_DTCT_NMBR")
    @NotFound(action = NotFoundAction.IGNORE)
    private TbVdsDtctLinkRltn link;

    public void setDelete() {
        this.delYn = "Y";
    }

    public TbVdsDtctDto toDto() {
        TbVdsDtctDto dto = TbVdsDtctDto.builder()
                .vdsDtctNmbr(this.vdsDtctNmbr)
                .vdsDtctId(this.vdsDtctId)
                .vdsDtctNm(this.vdsDtctNm)
                .vdsCtlrNmbr(this.vdsCtlrNmbr)
                .vdsCmraNo(this.vdsCmraNo)
                .dtctType(this.dtctType)
                .dtctDir(this.dtctDir)
                .detectLane(this.detectLane)
                .istlLane(this.istlLane)
                .spedClctYn(this.spedClctYn)
                .tfvlClctYn(this.tfvlClctYn)
                .ocpyClctYn(this.ocpyClctYn)
                .maxTfvl(this.maxTfvl)
                .minTfvl(this.minTfvl)
                .maxOcpyRate(this.maxOcpyRate)
                .minOcpyRate(this.minOcpyRate)
                .maxSped(this.maxSped)
                .minSped(this.minSped)
                .useYn(this.useYn)
                .delYn(this.delYn)
                .build();

        dto.setDtctTypeDesc(CodeManager.getCodeDescShort(CodeManager.DTCT_TYPE_CD, this.getDtctType()));
        if (this.link != null) {
            dto.setLinkId(this.link.getLinkId());
        }

        return dto;
    }

    public TbVdsDtct(Long vdsDtctNmbr) {
        this.vdsDtctNmbr = vdsDtctNmbr;
    }

    public void updateInfo(TbVdsDtctDto.TbVdsDtctUpdReq req) {
        this.vdsDtctId = req.getVdsDtctId();
        this.vdsDtctNm = req.getVdsDtctNm();
        this.vdsCtlrNmbr = req.getVdsCtlrNmbr();
        this.vdsCmraNo = req.getVdsCmraNo();
        this.dtctType = req.getDtctType();
        this.dtctDir = req.getDtctDir();
        this.detectLane = req.getDetectLane();
        this.istlLane = req.getIstlLane();
        this.spedClctYn = req.getSpedClctYn();
        this.tfvlClctYn = req.getTfvlClctYn();
        this.ocpyClctYn = req.getOcpyClctYn();
        this.maxTfvl = req.getMaxTfvl();
        this.minTfvl = req.getMinTfvl();
        this.maxOcpyRate = req.getMaxOcpyRate();
        this.minOcpyRate = req.getMinOcpyRate();
        this.maxSped = req.getMaxSped();
        this.minSped = req.getMinSped();
        this.useYn = req.getUseYn();
        this.delYn = req.getDelYn();
    }

}
