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

import com.its.api.its.global.CodeManager;
import com.its.api.its.model.dto.cctv.TbCctvSttsHsDto;
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;

/**
 * CCTV 상태 이력 Entity Class
 */
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Builder
@AllArgsConstructor
@ApiModel("CCTV 상태 이력")
@Entity
@Table(name = "TB_CCTV_STTS_HS")
@IdClass(TbCctvSttsHsKey.class)
public class TbCctvSttsHs implements Serializable {
    private static final long serialVersionUID = 1L;

    @ApiModelProperty("CCTV 관리 번호")  // N NUMBER(10)
    @Id
    @Column(name = "CCTV_MNGM_NMBR", nullable = false, columnDefinition = "NUMBER", length = 10)
    private Long cctvMngmNmbr;

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

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

    @ApiModelProperty("함체 문 상태 코드")  // Y VARCHAR(7)
    @Column(name = "CBOX_DOOR_STTS_CD", length = 7)
    private String cboxDoorSttsCd;

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

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

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

    @ApiModelProperty("함체 습도")  // Y NUMBER(3)
    @Column(name = "CBOX_HMDT", columnDefinition = "NUMBER", length = 3)
    private Integer cboxHmdt;

    @OneToOne
    @JoinColumn(insertable=false, updatable=false, name="CCTV_MNGM_NMBR", referencedColumnName = "CCTV_MNGM_NMBR")
    @NotFound(action = NotFoundAction.IGNORE)
    private TbCctvCtlr cctv = new TbCctvCtlr();

    public TbCctvSttsHsDto toDto() {
        TbCctvSttsHsDto dto = TbCctvSttsHsDto.builder()
                .cctvMngmNmbr(this.cctvMngmNmbr)
                .crtnDt(this.crtnDt)
                .cmncSttsCd(this.cmncSttsCd)
                .cboxDoorSttsCd(this.cboxDoorSttsCd)
                .fanSttsCd(this.fanSttsCd)
                .hetrSttsCd(this.hetrSttsCd)
                .cboxTmpr(this.cboxTmpr)
                .cboxHmdt(this.cboxHmdt)
                .build();

        if (this.cctv != null) {
            dto.setCctvCtlrId(this.cctv.getCctvCtlrId());
            dto.setIstlLctnNm(this.cctv.getIstlLctnNm());
        }
        else {
            dto.setCctvCtlrId("-");
            dto.setIstlLctnNm("?");
        }

        dto.setCmncSttsDesc(CodeManager.getCodeDesc(CodeManager.CMNC_STTS_CD, this.cmncSttsCd));

        return dto;
    }

}
