package com.its.op.entity.its.rse;

import com.its.op.dto.its.rse.TbRseSectTrafHsDto;
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;

/**
 * RSE 구간 소통상황 Entity Class
 */
@Getter
@NoArgsConstructor//(access = AccessLevel.PROTECTED)
@Builder
@AllArgsConstructor
@ApiModel("RSE 구간 소통상황")
@Entity
@Table(name = "TB_RSE_SECT_TRAF_HS")
@IdClass(TbRseSectTrafHsKey.class)
public class TbRseSectTrafHs implements Serializable {
    private static final long serialVersionUID = 1L;

    @ApiModelProperty("RSE 구간 아이디")  // N NUMBER(10)
    @Id
    @Column(name = "RSE_SECT_ID", nullable = false, columnDefinition = "NUMBER", length = 10)
    private Long rseSectId;

    @ApiModelProperty("가공 일시")  // N VARCHAR2(14)
    @Id
    @Column(name = "PRCN_DT", nullable = false, length = 14)
    private String prcnDt;

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

    @ApiModelProperty("통행 속도")  // Y NUMBER(3)
    @Column(name = "TRVL_SPED", columnDefinition = "NUMBER", length = 3)
    private Integer trvlSped;

    @ApiModelProperty("통행 시간")  // Y NUMBER(6)
    @Column(name = "TRVL_HH", columnDefinition = "NUMBER", length = 6)
    private Integer trvlHh;

    @OneToOne
    @JoinColumn(insertable=false, updatable=false, name="RSE_SECT_ID", referencedColumnName = "RSE_SECT_ID")
    @NotFound(action = NotFoundAction.IGNORE)
    private TbRseSect rse;

    public TbRseSectTrafHsDto toDto() {
        TbRseSectTrafHsDto dto = TbRseSectTrafHsDto.builder()
                .rseSectId(this.rseSectId)
                .prcnDt(this.prcnDt)
                .tfvl(this.tfvl)
                .trvlSped(this.trvlSped)
                .trvlHh(this.trvlHh)
                .build();
        if (this.rse != null) {
            dto.setRseSectId(this.rse.getRseSectId());
            dto.setStrtSpotNm(this.rse.getStrtSpotNm());
            dto.setEndSpotNm(this.rse.getEndSpotNm());
            dto.setStrtRseCtlrNmbr(this.rse.getStrtRseCtlrNmbr());
            dto.setEndRseCtlrNmbr(this.rse.getEndRseCtlrNmbr());
        }

        return dto;
    }

    public TbRseSectTrafHs(Long rseSectId, String prcnDt) {
        this.rseSectId = rseSectId;
        this.prcnDt = prcnDt;
    }

}
