package com.its.op.entity.its.vms;

import com.its.op.dto.its.vms.TbVmsRltnIfscDto;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.*;
import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction;

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

/**
 * VMS 정보제공구간 관계 Entity Class
 */
@Getter
@NoArgsConstructor//(access = AccessLevel.PROTECTED)
@Builder
@AllArgsConstructor
@ApiModel("VMS 정보제공구간 관계")
@Entity
@Table(name = "TB_VMS_RLTN_IFSC")
@IdClass(TbVmsRltnIfscKey.class)
public class TbVmsRltnIfsc implements Serializable {
    private static final long serialVersionUID = 1L;

    @ApiModelProperty("VMS 제어기 번호")  // N NUMBER(10)
    @Id
    @Column(name = "VMS_CTLR_NMBR", nullable = false, columnDefinition = "NUMBER", length = 10)
    private Long vmsCtlrNmbr;

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

    @ApiModelProperty("표출 우선순위")  // Y NUMBER(2)
    @Column(name = "DSPL_PRRT", columnDefinition = "NUMBER", length = 2)
    private Integer dsplPrrt;

    @OneToOne
    @JoinColumn(insertable=false, updatable=false, name="VMS_CTLR_NMBR", referencedColumnName = "VMS_CTLR_NMBR")
    @NotFound(action = NotFoundAction.IGNORE)
    private TbVmsCtlr vms;

    @OneToOne
    @JoinColumn(insertable=false, updatable=false, name="VMS_IFSC_ID", referencedColumnName = "VMS_IFSC_ID")
    @NotFound(action = NotFoundAction.IGNORE)
    private TbVmsIfsc ifsc;

    public TbVmsRltnIfscDto toDto() {
        TbVmsRltnIfscDto dto = TbVmsRltnIfscDto.builder()
                .vmsCtlrNmbr(this.vmsCtlrNmbr)
                .vmsIfscId(this.vmsIfscId)
                .dsplPrrt(this.dsplPrrt)
                .build();

        if (this.ifsc != null) {
            dto.setVmsIfscNm(this.ifsc.getVmsIfscNm());
            dto.setDsplStrtNodeNm(this.ifsc.getDsplStrtNodeNm());
            dto.setDsplEndNodeNm(this.ifsc.getDsplEndNodeNm());
            dto.setRoadNm(ifsc.getRoadNm());
            dto.setSpotNm(ifsc.getSpotNm());
            dto.setAxisYn(ifsc.getAxisYn());
        }
        if (this.vms != null) {
            dto.setVmsId(this.vms.getVmsCtlrId());
            dto.setVmsCtlrId(this.vms.getVmsCtlrId());
            dto.setVmsNm(this.vms.getVmsNm());
            dto.setVmsTypeCd(this.vms.getVmsTypeCd());
        }
        return dto;
    }

    public TbVmsRltnIfsc(Long vmsCtlrNmbr, Long vmsIfscId) {
        this.vmsCtlrNmbr = vmsCtlrNmbr;
        this.vmsIfscId = vmsIfscId;
    }

}
