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

import com.its.api.its.model.dto.link.TbLinkSyopTrafDto;
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_SYOP_TRAF")
@IdClass(TbLinkSyopTrafKey.class)
public class TbLinkSyopTraf 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 = "CRTN_DT", nullable = false, length = 14)
    private String crtnDt;

    @ApiModelProperty("적용 시작 일시")  // Y VARCHAR(14)
    @Column(name = "APLY_STRT_DT", length = 14)
    private String aplyStrtDt;

    @ApiModelProperty("적용 종료 일시")  // Y VARCHAR(14)
    @Column(name = "APLY_END_DT", length = 14)
    private String aplyEndDt;

    @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;

    @OneToOne//(fetch = FetchType.EAGER)
    @JoinColumn(insertable=false, updatable=false, name="LINK_ID", referencedColumnName = "LINK_ID")
    TbLink link = new TbLink();

    public TbLinkSyopTrafDto toDto() {
        TbLinkSyopTrafDto dto = TbLinkSyopTrafDto.builder()
                .linkId(this.linkId)
                .crtnDt(this.crtnDt)
                .aplyStrtDt(this.aplyStrtDt)
                .aplyEndDt(this.aplyEndDt)
                .tfvl(this.tfvl)
                .sped(this.sped)
                .ocpyRate(this.ocpyRate)
                .trvlHh(this.trvlHh)
                .build();

        if (this.linkId != null) {
            dto.setRoadName(this.link.getRoadName());
            String fromNodeName = this.link.getFromNode() != null ? this.link.getFromNode().getNodeName() : "";
            String toNodeName = this.link.getToNode() != null ? this.link.getToNode().getNodeName() : "";
            dto.setFromNodeInfo("[" + this.link.getFNodeId() + "] " + fromNodeName);
            dto.setToNodeInfo("[" + this.link.getTNodeId() + "] " + toNodeName);
        }
        else {
            dto.setRoadName("");
            dto.setFromNodeInfo("");
            dto.setToNodeInfo("");
        }
        return dto;
    }

    public TbLinkSyopTraf(Long linkId, String crtnDt) {
        this.linkId = linkId;
        this.crtnDt = crtnDt;
    }

    public void updateInfo(TbLinkSyopTrafDto.TbLinkSyopTrafUpdReq req) {
        this.aplyStrtDt = req.getAplyStrtDt();
        this.aplyEndDt = req.getAplyEndDt();
        this.tfvl = req.getTfvl();
        this.sped = req.getSped();
        this.ocpyRate = req.getOcpyRate();
        this.trvlHh = req.getTrvlHh();
    }

}
