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

import com.its.api.its.global.CodeManager;
import com.its.api.its.model.dto.common.TrafDto;
import com.its.api.utils.ItsUtils;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.*;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import java.io.Serializable;

/**
 * 링크 소통상황 Entity Class
 */
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Builder
@AllArgsConstructor
@ApiModel("링크 소통상황")
@Entity
@Table(name = "TB_LINK_TRAF")
public class TbLinkTrafSimple 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("가공 일시")  // Y VARCHAR(14)
    @Column(name = "PRCN_DT", 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 TrafDto toDto() {
        TrafDto dto = TrafDto.builder()
                .id(this.linkId)
                .sped(this.sped)
                .trvlHh(this.trvlHh)
                .cmtrGradCd(this.cmtrGradCd)
                .missYn(this.missYn)
                .prcnDt(this.prcnDt)
                .build();

        dto.setMissYn(("N").equals(this.missYn) ? ItsUtils.getMissYn(this.prcnDt, this.cmtrGradCd) : "Y");
        if (dto.getMissYn().equals("Y")) {
            dto.setSped(0);
            dto.setTrvlHh(0);
            dto.setCmtrGradCd(CodeManager.MISS_SPED_GRAD);
        }

        return dto;
    }
}
