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

import com.its.api.its.global.CodeManager;
import com.its.api.its.model.dto.link.TbLinkTrafHsDto;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.*;

import javax.persistence.*;
import java.io.Serializable;

/**
 * 링크 소통상황 이력 Entity Class
 */
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Builder
@AllArgsConstructor
@ApiModel("링크 소통상황 이력")
@Entity
@Table(name = "TB_LINK_TRAF_HS")
@IdClass(TbLinkTrafHsKey.class)
public class TbLinkTrafHs implements Serializable {
    private static final long serialVersionUID = 1L;

    @ApiModelProperty("링크ID")  // N NUMBER(10)
    @Id
    @Column(name = "LINK_ID", nullable = false, columnDefinition = "NUMBER", length = 10)
    private Long linkId;

    @ApiModelProperty("가공 일시")  // N VARCHAR(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 = "SPED", columnDefinition = "NUMBER", length = 3)
    private Integer sped;

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

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

    @ApiModelProperty("소통 등급 코드")  // Y VARCHAR(7)
    @Column(name = "CMTR_GRAD_CD", length = 7)
    private String cmtrGradCd;

    @ApiModelProperty("결측 여부")  // Y CHAR(1)
    @Column(name = "MISS_YN", columnDefinition = "CHAR", length = 1)
    private String missYn;

    @ApiModelProperty("가공 속도")  // Y NUMBER(3)
    @Column(name = "PRCN_SPED", columnDefinition = "NUMBER", length = 3)
    private Integer prcnSped;

    @ApiModelProperty("평활화 속도")  // Y NUMBER(3)
    @Column(name = "SMTH_SPED", columnDefinition = "NUMBER", length = 3)
    private Integer smthSped;

    @ApiModelProperty("보정 속도")  // Y NUMBER(3)
    @Column(name = "ADJS_SPED", columnDefinition = "NUMBER", length = 3)
    private Integer adjsSped;

    @ApiModelProperty("패턴 속도")  // Y NUMBER(3)
    @Column(name = "PTRN_SPED", columnDefinition = "NUMBER", length = 3)
    private Integer ptrnSped;

    @ApiModelProperty("가공 소스")  // Y VARCHAR(7)
    @Column(name = "PRCN_SORC", length = 7)
    private String prcnSorc;

    @ApiModelProperty("보정 소스")  // Y VARCHAR(7)
    @Column(name = "ADJS_SORC", length = 7)
    private String adjsSorc;

    @ApiModelProperty("데이터 개수")  // Y NUMBER(3)
    @Column(name = "DATA_NUM", columnDefinition = "NUMBER", length = 3)
    private Integer dataNum;

    public TbLinkTrafHsDto toDto() {
        TbLinkTrafHsDto dto = TbLinkTrafHsDto.builder()
                .linkId(this.linkId)
                .prcnDt(this.prcnDt)
                .tfvl(this.tfvl)
                .sped(this.sped)
                .ocpyRate(this.ocpyRate)
                .trvlHh(this.trvlHh)
                .cmtrGradCd(this.cmtrGradCd)
                .missYn(this.missYn)
                .prcnSped(this.prcnSped)
                .smthSped(this.smthSped)
                .adjsSped(this.adjsSped)
                .ptrnSped(this.ptrnSped)
                .prcnSorc(this.prcnSorc)
                .adjsSorc(this.adjsSorc)
                .dataNum(this.dataNum)
                .build();

        if (this.missYn.equals("Y")) {
            dto.setMissYnDesc("결측");
        }
        else if (this.missYn.equals("N")) {
            dto.setMissYnDesc("측정");
        }
        else {
            dto.setMissYnDesc("-");
        }
        dto.setPrcnSorcDesc(CodeManager.getCodeDescShort(CodeManager.PRCN_SORC, this.prcnSorc));
        dto.setAdjsSorcDesc(CodeManager.getCodeDescShort(CodeManager.ADJS_SORC, this.adjsSorc));
        dto.setCmtrGradDesc(CodeManager.getCodeDescShort(CodeManager.CMTR_GRAD_CD, this.cmtrGradCd));

        return dto;
    }

}
