package com.its.op.entity.its.vds;

import com.its.op.dto.its.vds.TbVdsCtlrSttsHsDto;
import com.its.op.global.CodeManager;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction;

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

/**
 *  Entity Class
 */
@Getter
@NoArgsConstructor//(access = AccessLevel.PROTECTED)
@Builder
@AllArgsConstructor
@ApiModel("")
@Entity
@Table(name = "TB_VDS_CTLR_STTS_HS")
@IdClass(TbVdsCtlrSttsHsKey.class)
public class TbVdsCtlrSttsHs implements Serializable {
    private static final long serialVersionUID = 1L;

    @ApiModelProperty("생성 시각")  // N VARCHAR2(14)
    @Id
    @Column(name = "CRTN_DT", nullable = false, length = 14)
    private String crtnDt;

    @ApiModelProperty("VDS 제어기 관리 번호")  // N NUMBER(10)
    @Id
    @Column(name = "VDS_CTLR_NMBR", nullable = false, columnDefinition = "NUMBER", length = 10)
    private Long vdsCtlrNmbr;

    @ApiModelProperty("통신 상태 코드('CMS')")  // Y VARCHAR2(7)
    @Column(name = "CMNC_STTS_CD", length = 7)
    private String cmncSttsCd;

    @ApiModelProperty("함체 문 상태 코드('CDS')-앞문/뒷문상태가 모두닫힌경우 닫힘")  // Y VARCHAR2(7)
    @Column(name = "CBOX_DOOR_STTS_CD", length = 7)
    private String cboxDoorSttsCd;

    @ApiModelProperty("앞문 상태 코드('CDS')")  // Y VARCHAR2(7)
    @Column(name = "FRONT_DOOR_STTS_CD", length = 7)
    private String frontDoorSttsCd;

    @ApiModelProperty("뒷문 상태 코드('CDS')")  // Y VARCHAR2(7)
    @Column(name = "BACK_DOOR_STTS_CD", length = 7)
    private String backDoorSttsCd;

    @ApiModelProperty("팬 상태 코드('PAS')")  // Y VARCHAR2(7)
    @Column(name = "FAN_STTS_CD", length = 7)
    private String fanSttsCd;

    @ApiModelProperty("히터 상태 코드('HTS')")  // Y VARCHAR2(7)
    @Column(name = "HETR_STTS_CD", length = 7)
    private String hetrSttsCd;

    @ApiModelProperty("함체 온도")  // Y NUMBER(5)
    @Column(name = "CBOX_TMPR", columnDefinition = "NUMBER", length = 5)
    private Integer cboxTmpr;

    @ApiModelProperty("입력 전압")  // Y NUMBER(3)
    @Column(name = "INPT_VOLT", columnDefinition = "NUMBER", length = 3)
    private Integer inptVolt;

    @ApiModelProperty("출력 전압")  // Y NUMBER(3)
    @Column(name = "PRNT_VOLT", columnDefinition = "NUMBER", length = 3)
    private Integer prntVolt;

    @ApiModelProperty("출력 전원 상태 코드")  // Y VARCHAR2(7)
    @Column(name = "PRNT_PWER_STTS_CD", length = 7)
    private String prntPwerSttsCd;

    @ApiModelProperty("Video Input 여부('VDI')")  // Y VARCHAR2(7)
    @Column(name = "VIDEO_INPUT", length = 7)
    private String videoInput;

    @OneToOne
    @JoinColumn(insertable=false, updatable=false, name="VDS_CTLR_NMBR", referencedColumnName = "VDS_CTLR_NMBR")
    @NotFound(action = NotFoundAction.IGNORE)
    private TbVdsCtlr vds;


    public TbVdsCtlrSttsHsDto toDto() {
        TbVdsCtlrSttsHsDto dto = TbVdsCtlrSttsHsDto.builder()
                .crtnDt(this.crtnDt)
                .vdsCtlrNmbr(this.vdsCtlrNmbr)
                .cmncSttsCd(this.cmncSttsCd)
                .cboxDoorSttsCd(this.cboxDoorSttsCd)
                .frontDoorSttsCd(this.frontDoorSttsCd)
                .backDoorSttsCd(this.backDoorSttsCd)
                .fanSttsCd(this.fanSttsCd)
                .hetrSttsCd(this.hetrSttsCd)
                .cboxTmpr(this.cboxTmpr)
                .inptVolt(this.inptVolt)
                .prntVolt(this.prntVolt)
                .prntPwerSttsCd(this.prntPwerSttsCd)
                .videoInput(this.videoInput)
                .build();

        dto.setCmncSttsDesc(CodeManager.getCodeDescShort(CodeManager.CMNC_STTS_CD, this.cmncSttsCd));           // 통신상태
        if (CodeManager.CMNC_STTS_NORMAL.equals(dto.getCmncSttsCd())) {
            dto.setPrntPwerSttsDesc(CodeManager.getCodeDescShort(CodeManager.PWER_STTS_CD, this.prntPwerSttsCd));   // 전원상태
            dto.setCboxDoorSttsDesc(CodeManager.getCodeDescShort(CodeManager.DOOR_STTS_CD, this.cboxDoorSttsCd));   // 도어
            dto.setFanSttsDesc(CodeManager.getCodeDescShort(CodeManager.FAN_STTS_CD, this.fanSttsCd));              // 팬상태
            dto.setHetrSttsDesc(CodeManager.getCodeDescShort(CodeManager.HETR_STTS_CD, this.hetrSttsCd));           // 히터상태
        }
        else {
            dto.setPrntPwerSttsDesc(CodeManager.STTS_UNKNOWN);      // 전원상태
            dto.setCboxDoorSttsDesc(CodeManager.STTS_UNKNOWN);      // 도어
            dto.setFanSttsDesc(CodeManager.STTS_UNKNOWN);           // 팬상태
            dto.setHetrSttsDesc(CodeManager.STTS_UNKNOWN);          // 히터상태
            dto.setCboxTmpr(255);
        }

        if (this.vds != null) {
            dto.setVdsCtlrId(this.vds.getVdsCtlrId());
            dto.setVdsNm(this.vds.getVdsNm());
        }
        else {
            dto.setVdsCtlrId("-");
            dto.setVdsNm("-");
        }

        return dto;
    }

}
