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

import com.its.api.its.model.dto.vds.TbVdsDtctDdStatDto;
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_DD_STAT")
@IdClass(TbVdsDtctDdStatKey.class)
public class TbVdsDtctDdStat 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 = "STAT_DT", nullable = false, length = 14)
    private String statDt;

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

    public TbVdsDtctDdStatDto toDto() {
        TbVdsDtctDdStatDto dto = TbVdsDtctDdStatDto.builder()
                .dtctNmbr(this.dtctNmbr)
                .statDt(this.statDt)
                .systKindDvsn(this.systKindDvsn)
                .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;
    }

}
