package com.its.api.its.model.entity.vds;

import com.its.api.its.global.CodeManager;
import com.its.api.its.model.dto.vds.TbVdsDtctDto;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.*;
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_MSTR")
public class TbVdsDtct implements Serializable {
    private static final long serialVersionUID = 1L;

    @ApiModelProperty("검지기 번호")  // N VARCHAR(8)
    @Id
    @Column(name = "DTCT_NMBR", nullable = false, length = 8)
    private String dtctNmbr;

    @ApiModelProperty("검지기 유형 코드")  // Y VARCHAR(7)
    @Column(name = "DTCT_TYPE_CD", length = 7)
    private String dtctTypeCd;

    @ApiModelProperty("정지선 거리")  // Y NUMBER(7)
    @Column(name = "STLN_DSTC", columnDefinition = "NUMBER", length = 7)
    private Integer stlnDstc;

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

    @ApiModelProperty("DET 번호")  // Y VARCHAR(1)
    @Column(name = "DET_NMBR", length = 1)
    private String detNmbr;

    @ApiModelProperty("감도")  // Y NUMBER(3)
    @Column(name = "SNST", columnDefinition = "NUMBER", length = 3)
    private Integer snst;

    @ApiModelProperty("검지기 유효 거리")  // Y NUMBER(7)
    @Column(name = "DTCT_VALD_DSTC", columnDefinition = "NUMBER", length = 7)
    private Integer dtctValdDstc;

    @ApiModelProperty("중형차 상한 길이")  // Y NUMBER(7)
    @Column(name = "MDVH_UPLM_LNGT", columnDefinition = "NUMBER", length = 7)
    private Integer mdvhUplmLngt;

    @ApiModelProperty("중형차 하한 길이")  // Y NUMBER(7)
    @Column(name = "MDVH_LWLM_LNGT", columnDefinition = "NUMBER", length = 7)
    private Integer mdvhLwlmLngt;

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

    @ApiModelProperty("채널 번호")  // Y VARCHAR(4)
    @Column(name = "CHNL_NMBR", length = 4)
    private String chnlNmbr;

    @ApiModelProperty("삭제자 번호")  // Y VARCHAR(20)
    @Column(name = "DLTR_NMBR", length = 20)
    private String dltrNmbr;

    @ApiModelProperty("삭제 일시")  // Y VARCHAR(14)
    @Column(name = "DEL_DT", length = 14)
    private String delDt;

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

    @ApiModelProperty("최초 등록자 번호")  // Y VARCHAR(20)
    @Column(name = "FRST_REGR_NMBR", length = 20)
    private String frstRegrNmbr;

    @ApiModelProperty("최초 등록 일시")  // Y VARCHAR(14)
    @Column(name = "FRST_RGST_DT", length = 14)
    private String frstRgstDt;

    @ApiModelProperty("최종 수정자 번호")  // Y VARCHAR(20)
    @Column(name = "LAST_CRPR_NMBR", length = 20)
    private String lastCrprNmbr;

    @ApiModelProperty("최종 수정 일시")  // Y VARCHAR(14)
    @Column(name = "LAST_CRCT_DT", length = 14)
    private String lastCrctDt;

    @ApiModelProperty("제어기 관리 번호")  // Y VARCHAR(20)
    @Column(name = "CTLR_MNGM_NMBR", length = 20)
    private String ctlrMngmNmbr;

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

    @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;

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


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

    public TbVdsDtctDto toDto() {
        TbVdsDtctDto dto = TbVdsDtctDto.builder()
                .dtctNmbr(this.dtctNmbr)
                .dtctTypeCd(this.dtctTypeCd)
                .stlnDstc(this.stlnDstc)
                .istlLane(this.istlLane)
                .detNmbr(this.detNmbr)
                .snst(this.snst)
                .dtctValdDstc(this.dtctValdDstc)
                .mdvhUplmLngt(this.mdvhUplmLngt)
                .mdvhLwlmLngt(this.mdvhLwlmLngt)
                .useYn(this.useYn)
                .chnlNmbr(this.chnlNmbr)
                .dltrNmbr(this.dltrNmbr)
                .delDt(this.delDt)
                .delYn(this.delYn)
                .frstRegrNmbr(this.frstRegrNmbr)
                .frstRgstDt(this.frstRgstDt)
                .lastCrprNmbr(this.lastCrprNmbr)
                .lastCrctDt(this.lastCrctDt)
                .ctlrMngmNmbr(this.ctlrMngmNmbr)
                .vdsDtctNm(this.vdsDtctNm)
                .maxTfvl(this.maxTfvl)
                .minTfvl(this.minTfvl)
                .maxOcpyRate(this.maxOcpyRate)
                .minOcpyRate(this.minOcpyRate)
                .maxSped(this.maxSped)
                .minSped(this.minSped)
                .build();

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

        return dto;
    }

    public TbVdsDtct(String dtctNmbr) {
        this.dtctNmbr = dtctNmbr;
    }

    public void updateInfo(TbVdsDtctDto.TbVdsDtctMstrUpdReq req) {
        this.dtctTypeCd = req.getDtctTypeCd();
        this.stlnDstc = req.getStlnDstc();
        this.istlLane = req.getIstlLane();
        this.detNmbr = req.getDetNmbr();
        this.snst = req.getSnst();
        this.dtctValdDstc = req.getDtctValdDstc();
        this.mdvhUplmLngt = req.getMdvhUplmLngt();
        this.mdvhLwlmLngt = req.getMdvhLwlmLngt();
        this.useYn = req.getUseYn();
        this.chnlNmbr = req.getChnlNmbr();
        this.delYn = req.getDelYn();
        this.ctlrMngmNmbr = req.getCtlrMngmNmbr();
        this.vdsDtctNm = req.getVdsDtctNm();
        this.maxTfvl = req.getMaxTfvl();
        this.minTfvl = req.getMinTfvl();
        this.maxOcpyRate = req.getMaxOcpyRate();
        this.minOcpyRate = req.getMinOcpyRate();
        this.maxSped = req.getMaxSped();
        this.minSped = req.getMinSped();
    }

}
