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

import com.its.api.its.model.dto.vms.TbVmsCtrlHsDto;
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_CTL_HS")
@IdClass(TbVmsCtrlHsKey.class)
public class TbVmsCtrlHs implements Serializable {
    private static final long serialVersionUID = 1L;

    @ApiModelProperty("제어 일시")  // N VARCHAR(14)
    @Id
    @Column(name = "CTL_DT", nullable = false, length = 14)
    private String ctlDt;

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

    @ApiModelProperty("제어유형(V01:전광판ON,V02:전광판OFF,V03:제어기리셋,V04:함체환경설정,V05:전광판ON/OFF시각설정)")  // N VARCHAR(3)
    @Id
    @Column(name = "CTL_TYPE", nullable = false, length = 3)
    private String ctlType;

    @ApiModelProperty("제어값")  // Y VARCHAR(30)
    @Column(name = "CTL_VAL", length = 30)
    private String ctlVal;

    @ApiModelProperty("제어결과(0:실패,1:성공)")  // Y NUMBER(1)
    @Column(name = "CTL_RESULT", columnDefinition = "NUMBER", length = 1)
    private Integer ctlResult;

    @ApiModelProperty("제어한사용자ID")  // Y VARCHAR(20)
    @Column(name = "USER_ID", length = 20)
    private String userId;

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

    public TbVmsCtrlHsDto toDto() {
        TbVmsCtrlHsDto dto = TbVmsCtrlHsDto.builder()
                .ctlDt(this.ctlDt)
                .vmsCtlrNmbr(this.vmsCtlrNmbr)
                .ctlType(this.ctlType)
                .ctlVal(this.ctlVal)
                .ctlResult(this.ctlResult)
                .userId(this.userId)
                .build();

        if (this.vms != null) {
            dto.setVmsCtlrId(this.vms.getVmsCtlrId());
            dto.setVmsNm(this.vms.getVmsNm());
        }
        else {
            dto.setVmsCtlrId("-");
            dto.setVmsNm("?");
        }

        return dto;
    }

}
