package com.its.op.entity.its.cctv;

import com.its.op.dto.its.cctv.TbCctvCtrlHsDto;
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;

/**
 * CCTV 제어이력 Entity Class
 */
@Getter
@NoArgsConstructor//(access = AccessLevel.PROTECTED)
@Builder
@AllArgsConstructor
@ApiModel("CCTV 제어이력")
@Entity
@Table(name = "TB_CCTV_CTRL_HS")
@IdClass(TbCctvCtrlHsKey.class)
public class TbCctvCtrlHs implements Serializable {
    private static final long serialVersionUID = 1L;

    @ApiModelProperty("제어 일시")  // N VARCHAR2(14)
    @Id
    @Column(name = "CTRL_DT", nullable = false, length = 14)
    private String ctrlDt;

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

    @ApiModelProperty("제어유형")  // N VARCHAR2(3)
    @Id
    @Column(name = "CTRL_TYPE", nullable = false, length = 3)
    private String ctrlType;

    @ApiModelProperty("제어값")  // Y VARCHAR2(30)
    @Column(name = "CTRL_VAL", length = 30)
    private String ctrlVal;

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

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

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

    public TbCctvCtrlHsDto toDto() {
        TbCctvCtrlHsDto dto = TbCctvCtrlHsDto.builder()
                .ctrlDt(this.ctrlDt)
                .cctvCtlrNmbr(this.cctvCtlrNmbr)
                .ctrlType(this.ctrlType)
                .ctrlVal(this.ctrlVal)
                .ctrlResult(this.ctrlResult)
                .userId(this.userId)
                .build();

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

        // 제어유형(0: SW 리셋, 1: HW 리셋, 2: 가변문자삭제, 3: 가변문자설정, 4: 파리미터설정, 5: 프리셋제어)
        if ("0".equals(this.ctrlType)) {
            dto.setCtrlTypeDesc("SW 리셋");
        }
        else if ("1".equals(this.ctrlType)) {
            dto.setCtrlTypeDesc("HW 리셋");
        }
        else if ("2".equals(this.ctrlType)) {
            dto.setCtrlTypeDesc("가변문자삭제");
        }
        else if ("3".equals(this.ctrlType)) {
            dto.setCtrlTypeDesc("가변문자설정");
        }
        else if ("4".equals(this.ctrlType)) {
            dto.setCtrlTypeDesc("파리미터설정");
        }
        else if ("5".equals(this.ctrlType)) {
            dto.setCtrlTypeDesc("프리셋제어");
        }
        else {
            dto.setCtrlTypeDesc(this.ctrlType);
        }

        switch(this.ctrlResult) {
            case 0: dto.setCtrlResultDesc("성공"); break;
            case 1: dto.setCtrlResultDesc("실패-알수없는 CCTV 제어기"); break;
            case 2: dto.setCtrlResultDesc("실패-CCTV 통신 OffLine"); break;
            case 3: dto.setCtrlResultDesc("실패-명령값 오류"); break;
            case 4: dto.setCtrlResultDesc("실패-명령 전송 통신 오류"); break;
            case 5: dto.setCtrlResultDesc("실패-통신 타임아웃"); break;
            case 6: dto.setCtrlResultDesc("실패-명령 전송 기타 오류"); break;
            case 9: dto.setCtrlResultDesc("실패-명령결과 수신 오류"); break;
            case 10: dto.setCtrlResultDesc("실패-명령 전송 요청 오류"); break;
            case 11: dto.setCtrlResultDesc("실패-명령결과 파싱 오류"); break;
            default:dto.setCtrlResultDesc(String.valueOf(this.ctrlResult)); break;
        }

        return dto;
    }

}
