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

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

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

    @ApiModelProperty("통계 일시")  // N VARCHAR(14)
    @Id
    @Column(name = "STAT_DT", nullable = false, length = 14)
    private String statDt;

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

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

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

    @ApiModelProperty("평균 차두시간")  // Y VARCHAR(6)
    @Column(name = "AVRG_HDWY", columnDefinition = "NUMBER", length = 6)
    private Integer avrgHdwy;

    @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="VDS_DTCT_NMBR", referencedColumnName = "VDS_DTCT_NMBR")
    @NotFound(action = NotFoundAction.IGNORE)
    private TbVdsDtct dtct;

    public TbVdsDtctStatDdDto toDto() {
        TbVdsDtctStatDdDto dto = TbVdsDtctStatDdDto.builder()
                .vdsDtctNmbr(this.vdsDtctNmbr)
                .statDt(this.statDt)
                .tfvl(this.tfvl)
                .avrgSped(this.avrgSped)
                .avrgOcpyRate(this.avrgOcpyRate)
                .avrgHdwy(this.avrgHdwy)
                .avrgLngt(this.avrgLngt)
                .build();

        if (this.dtct != null) {
            dto.setIstlLane(this.dtct.getIstlLane());
            dto.setVdsDtctNm(this.dtct.getVdsDtctNm());
        }

        return dto;
    }

}
