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

import com.its.op.dto.its.vds.TbVdsDtctClctDto;
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_CLCT")
@IdClass(TbVdsDtctClctKey.class)
public class TbVdsDtctClct implements Serializable {
    private static final long serialVersionUID = 1L;

    @ApiModelProperty("수집 일시")  // N VARCHAR2(14)
    @Id
    @Column(name = "CLCT_DT", nullable = false, length = 14)
    private String clctDt;

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

    @ApiModelProperty("교통정보수집주기")  // Y NUMBER(3)
    @Column(name = "TRAF_CLCT_CYCL", columnDefinition = "NUMBER", length = 3)
    private Integer trafClctCycl;

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

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

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

    @ApiModelProperty("평균 차량 길이(dm : decimal meter, 1 dm = 1/10 m)")  // Y NUMBER(3)
    @Column(name = "AVRG_LNGT", columnDefinition = "NUMBER", length = 3)
    private Integer avrgLngt;

    @ApiModelProperty("평균 차두시간")  // Y NUMBER(3)
    @Column(name = "HDWY", columnDefinition = "NUMBER", length = 3)
    private Integer hdwy;

    @ApiModelProperty("공간 점유율")  // Y NUMBER(5,2)
    @Column(name = "SPCE_OCPY_RATE", columnDefinition = "NUMBER", length = 5, precision = 2)
    private Double spceOcpyRate;

    @ApiModelProperty("공간 평균속도")  // Y NUMBER(3)
    @Column(name = "SPCE_AVRG_SPED", columnDefinition = "NUMBER", length = 3)
    private Integer spceAvrgSped;

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

    public TbVdsDtctClctDto toDto() {
        TbVdsDtctClctDto dto = TbVdsDtctClctDto.builder()
                .clctDt(this.clctDt)
                .vdsDtctNmbr(this.vdsDtctNmbr)
                .trafClctCycl(this.trafClctCycl)
                .tfvl(this.tfvl)
                .sped(this.sped)
                .avrgOcpyRate(this.avrgOcpyRate)
                .avrgLngt(this.avrgLngt)
                .hdwy(this.hdwy)
                .spceOcpyRate(this.spceOcpyRate)
                .spceAvrgSped(this.spceAvrgSped)
                .build();

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

        return dto;
    }

}
