package com.its.op.entity.its.road;

import com.its.op.global.CodeManager;
import com.its.op.dto.its.common.TrafDto;
import com.its.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_ROAD_TRAF")
public class TbRoadTrafSimple implements Serializable {
    private static final long serialVersionUID = 1L;

    @ApiModelProperty("도로 ID")  // N NUMBER(10)
    @Id
    @Column(name = "ROAD_ID", nullable = false, columnDefinition = "NUMBER", length = 10)
    private Long roadId;

    @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(3)
    @Column(name = "PTRN_SPED", columnDefinition = "NUMBER", length = 3)
    private Integer ptrnSped;

    @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 NUMBER(3)
    @Column(name = "DATA_NUM", columnDefinition = "NUMBER", length = 3)
    private Integer dataNum;

    public TrafDto toDto() {
        TrafDto dto = TrafDto.builder()
                .id(this.roadId)
                .sped(this.sped)
                .cmtrGradCd(this.cmtrGradCd)
                .trvlHh(this.trvlHh)
                .prcnDt(this.prcnDt)
                .build();

        dto.setMissYn(ItsUtils.getMissYn(this.prcnDt, this.cmtrGradCd));
        if (this.cmtrGradCd == null) { // || dto.getMissYn().equals("Y")) {
            dto.setSped(0);
            dto.setTrvlHh(0);
            dto.setCmtrGradCd(CodeManager.MISS_SPED_GRAD);
        }
        return dto;
    }

}
