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

import com.its.api.its.global.CodeManager;
import com.its.api.its.model.dto.vds.TbVdsDtctRawClctDto;
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_RAW_CLCT")
@IdClass(TbVdsDtctRawClctKey.class)
public class TbVdsDtctRawClct 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("발생 일시")  // N VARCHAR(14)
    @Id
    @Column(name = "OCRR_DT", nullable = false, length = 14)
    private String ocrrDt;

    @ApiModelProperty("시스템 종류 구분")  // N VARCHAR(1)
    @Id
    @Column(name = "SYST_KIND_DVSN", nullable = false, length = 1)
    private String systKindDvsn;

    @ApiModelProperty("교통량")  // Y NUMBER(6)
    @Column(name = "TFVL", columnDefinition = "NUMBER", length = 6)
    private Integer tfvl;

    @ApiModelProperty("평균 점유 율")  // Y NUMBER(6,2)
    @Column(name = "AVRG_OCPY_RATE", columnDefinition = "NUMBER", length = 6, precision = 2)
    private Double avrgOcpyRate;

    @ApiModelProperty("속도")  // Y NUMBER(3)
    @Column(name = "SPED", columnDefinition = "NUMBER", length = 3)
    private Integer sped;

    @ApiModelProperty("차두시간")  // Y VARCHAR(6)
    @Column(name = "HDWY", length = 6)
    private String hdwy;

    @ApiModelProperty("평균 길이")  // Y NUMBER(5,1)
    @Column(name = "AVRG_LNGT", columnDefinition = "NUMBER", length = 5, precision = 1)
    private Double avrgLngt;

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

    public TbVdsDtctRawClctDto toDto() {
        TbVdsDtctRawClctDto dto = TbVdsDtctRawClctDto.builder()
                .dtctNmbr(this.dtctNmbr)
                .ocrrDt(this.ocrrDt)
                .systKindDvsn(this.systKindDvsn)
                .tfvl(this.tfvl)
                .avrgOcpyRate(this.avrgOcpyRate)
                .sped(this.sped)
                .hdwy(this.hdwy)
                .avrgLngt(this.avrgLngt)
                .build();

        if (this.dtct != null) {
            dto.setDtctTypeCd(this.dtct.getDtctTypeCd());
            dto.setDtctTypeDesc(CodeManager.getCodeDescShort(CodeManager.DTCT_TYPE_CD, this.dtct.getDtctTypeCd()));
            if (this.dtct.getVds() != null) {
                dto.setVdsCtlrId(this.dtct.getVds().getVdsCtlrId());
                dto.setVdsNm(this.dtct.getVds().getLctn());
            }
            else {
                dto.setVdsCtlrId("-");
                dto.setVdsNm("?");
            }
        }
        else {
            dto.setDtctTypeCd("-");
            dto.setDtctTypeDesc("?");
            dto.setVdsCtlrId("-");
            dto.setVdsNm("?");
        }

        return dto;
    }

}
