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

import com.its.api.its.global.CodeManager;
import com.its.api.its.model.dto.ifsc.TbReptCngsSectTrafDto;
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_REPT_CNGS_SECT")
@IdClass(TbReptCngsSectKey.class)
public class TbReptCngsSectTraf implements Serializable {
    private static final long serialVersionUID = 1L;

    @ApiModelProperty("정보제공구간 ID")  // N NUMBER(10)
    @Id
    @Column(name = "IFSC_ID", nullable = false, columnDefinition = "NUMBER", length = 10)
    private Long ifscId;

    @ApiModelProperty("통계 년월")  // N VARCHAR(6)
    @Id
    @Column(name = "STAT_YM", nullable = false, length = 6)
    private String statYm;

    @ApiModelProperty("요일 유형 코드")  // N VARCHAR(7)
    @Id
    @Column(name = "DAY_TYPE_CD", nullable = false, length = 7)
    private String dayTypeCd;

    @ApiModelProperty("혼잡 시작 시분")  // N VARCHAR(4)
    @Id
    @Column(name = "CNFS_STRT_HM", nullable = false, length = 4)
    private String cnfsStrtHm;

    @ApiModelProperty("혼잡 종료 시분")  // Y VARCHAR(4)
    @Column(name = "CNFS_END_HM", length = 4)
    private String cnfsEndHm;

    @ApiModelProperty("평균 속도")  // Y NUMBER(3)
    @Column(name = "AVRG_SPED", columnDefinition = "NUMBER", length = 3)
    private Integer avrgSped;

    @ApiModelProperty("평균 통행 시간")  // Y NUMBER(6)
    @Column(name = "AVRG_TRVL_HH", columnDefinition = "NUMBER", length = 6)
    private Integer avrgTrvlHh;

    @ApiModelProperty("확정 여부")  // Y CHAR(1)
    @Column(name = "DCSN_YN", columnDefinition = "CHAR", length = 1)
    private String dcsnYn;

    @ApiModelProperty("생성 일자")  // Y VARCHAR(8)
    @Column(name = "CRTN_YMD", length = 8)
    private String crtnYmd;

    @ApiModelProperty("반복 정체 구간 원본 코드")  // Y VARCHAR(7)
    @Column(name = "REPT_CNGS_SECT_ORGN_CD", length = 7)
    private String reptCngsSectOrgnCd;

    @OneToOne
    @JoinColumn(insertable=false, updatable=false, name="IFSC_ID", referencedColumnName = "IFSC_ID")
    TbIfscTraf ifscTraf = new TbIfscTraf();

    public TbReptCngsSectTrafDto toDto() {
        TbReptCngsSectTrafDto dto = TbReptCngsSectTrafDto.builder()
                .ifscId(this.ifscId)
                .statYm(this.statYm)
                .dayTypeCd(this.dayTypeCd)
                .cnfsStrtHm(this.cnfsStrtHm)
                .cnfsEndHm(this.cnfsEndHm)
                .avrgSped(this.avrgSped)
                .avrgTrvlHh(this.avrgTrvlHh)
                .dcsnYn(this.dcsnYn)
                .crtnYmd(this.crtnYmd)
                .reptCngsSectOrgnCd(this.reptCngsSectOrgnCd)
                .build();

        if (this.ifscTraf != null) {
            if (this.ifscTraf.getIfsc() != null) {
                dto.setIfscNm(this.ifscTraf.getIfsc().getIfscNm());
                dto.setStrtNm(this.ifscTraf.getIfsc().getStrtNm());
                dto.setEndNm(this.ifscTraf.getIfsc().getEndNm());
                dto.setDrctDesc(CodeManager.getCodeDesc(CodeManager.DRCT_CD, this.ifscTraf.getIfsc().getDrctCd()));
            }
            dto.setPrcnDt(this.ifscTraf.getPrcnDt());
            dto.setTfvl(this.ifscTraf.getTfvl());
            dto.setSped(this.ifscTraf.getSped());
            dto.setPtrnSped(this.ifscTraf.getPtrnSped());
            dto.setTrvlHh(this.ifscTraf.getTrvlHh());
            dto.setCmtrGradCd(this.ifscTraf.getCmtrGradCd());
            dto.setCmtrGradDesc(CodeManager.getCodeDesc(CodeManager.CMTR_GRAD_CD, this.ifscTraf.getCmtrGradCd()));
        }
        dto.setDayTypeDesc(CodeManager.getCodeDesc(CodeManager.DAY_TYPE_CD, dto.getDayTypeCd()));
        dto.setReptCngsSectOrgnDesc(CodeManager.getCodeDesc(CodeManager.REPT_CNGS_CD, dto.getReptCngsSectOrgnCd()));

        return dto;
    }

}
