package com.its.op.entity.its.facility;

import com.its.op.dto.its.facility.TbFcltFailDto;
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_FCLT_FAIL")
public class TbFcltFail implements Serializable {
    private static final long serialVersionUID = 1L;

    @ApiModelProperty("장애발생번호(YYYYMMNNNN)")  // N VARCHAR2(10)
    @Id
    @Column(name = "FAIL_NO", nullable = false, length = 10)
    private String failNo;

    @ApiModelProperty("등록일시")  // N VARCHAR2(14)
    @Column(name = "REG_DT", nullable = false, length = 14)
    private String regDt;

    @ApiModelProperty("시설물 관리 번호")  // N NUMBER(10)
    @Column(name = "FCLT_NMBR", nullable = false, columnDefinition = "NUMBER", length = 10)
    private Long fcltNmbr;

    @ApiModelProperty("장애 발생 구분코드")  // Y VARCHAR2(7)
    @Column(name = "FAIL_OCRR_CD", length = 7)
    private String failOcrrCd;

    @ApiModelProperty("장애품목 관리번호")  // Y NUMBER(10)
    @Column(name = "SUBJ_NMBR", columnDefinition = "NUMBER", length = 10)
    private Long subjNmbr;

    @ApiModelProperty("장애발견자명")  // Y VARCHAR2(30)
    @Column(name = "FIND_MAN", length = 30)
    private String findMan;

    @ApiModelProperty("장애발생시각")  // Y VARCHAR2(14)
    @Column(name = "STR_DT", length = 14)
    private String strDt;

    @ApiModelProperty("장애종료시각")  // Y VARCHAR2(14)
    @Column(name = "END_DT", length = 14)
    private String endDt;

    @ApiModelProperty("진행상태-장애조치('FSTEP')")  // Y VARCHAR2(7)
    @Column(name = "STEP_CD", length = 7)
    private String stepCd;

    @ApiModelProperty("장애명")  // Y VARCHAR2(100)
    @Column(name = "FAIL_NM", length = 100)
    private String failNm;

    @ApiModelProperty("장애등록구분('FREG')")  // Y VARCHAR2(7)
    @Column(name = "FAIL_REG_CD", length = 7)
    private String failRegCd;

    @ApiModelProperty("장애등급구분('FLVL')")  // Y VARCHAR2(7)
    @Column(name = "FAIL_LEVL_CD", length = 7)
    private String failLevlCd;

    @ApiModelProperty("날씨('FWTHR')")  // Y VARCHAR2(7)
    @Column(name = "WTHR_CD", length = 7)
    private String wthrCd;

    @ApiModelProperty("장애내용")  // Y VARCHAR2(256)
    @Column(name = "FAIL_DESC", length = 256)
    private String failDesc;

    @ApiModelProperty("선조치내용")  // Y VARCHAR2(256)
    @Column(name = "BEF_ACT_DESC", length = 256)
    private String befActDesc;

    @ApiModelProperty("기타비고")  // Y VARCHAR2(256)
    @Column(name = "ETC_RMK", length = 256)
    private String etcRmk;

    @ApiModelProperty("사용여부")  // Y CHAR(1)
    @Column(name = "USE_YN", columnDefinition = "CHAR", length = 1)
    private String useYn;

    @OneToOne
    @JoinColumn(insertable=false, updatable=false, name="FCLT_NMBR", referencedColumnName = "FCLT_NMBR")
    @NotFound(action = NotFoundAction.IGNORE)
    private TbFcltInfr fclt;

    public TbFcltFailDto toDto() {
        TbFcltFailDto dto = TbFcltFailDto.builder()
                .failNo(this.failNo)
                .regDt(this.regDt)
                .fcltNmbr(this.fcltNmbr)
                .failOcrrCd(this.failOcrrCd)
                .subjNmbr(this.subjNmbr)
                .findMan(this.findMan)
                .strDt(this.strDt)
                .endDt(this.endDt)
                .stepCd(this.stepCd)
                .failNm(this.failNm)
                .failRegCd(this.failRegCd)
                .failLevlCd(this.failLevlCd)
                .wthrCd(this.wthrCd)
                .failDesc(this.failDesc)
                .befActDesc(this.befActDesc)
                .etcRmk(this.etcRmk)
                .useYn(this.useYn)
                .build();

        if (this.fclt != null) {
            // TODO
            // 시설물 대분류, 시설물 군분류
        }
        //        @ApiModelProperty("장애 발생 구분코드 설명")
//        @JsonProperty("fail_ocrr_desc")    // FAIL_OCRR_CD
//        private String failOcrrDesc;
//
//        @ApiModelProperty("진행상태-장애조치('FSTEP') 설명")
//        @JsonProperty("step_desc")    // STEP_CD
//        private String stepDesc;
//
//        @ApiModelProperty("장애등록구분('FREG') 설명")
//        @JsonProperty("fail_reg_desc")    // FAIL_REG_CD
//        private String failRegDesc;
//
//        @ApiModelProperty("장애등급구분('FLVL') 설명")
//        @JsonProperty("fail_levl_desc")    // FAIL_LEVL_CD
//        private String failLevlDesc;
//
//        @ApiModelProperty("날씨('FWTHR') 설명")
//        @JsonProperty("wthr_desc")    // WTHR_CD
//        private String wthrDesc;
//
//        @ApiModelProperty("사용여부 설명")
//        @JsonProperty("use_desc")    // USE_YN
//        private String useDesc;

        return dto;
    }

    public TbFcltFail(String failNo) {
        this.failNo = failNo;
    }

    public void updateInfo(TbFcltFailDto.TbFcltFailUpdReq req) {
        this.regDt = req.getRegDt();
        this.fcltNmbr = req.getFcltNmbr();
        this.failOcrrCd = req.getFailOcrrCd();
        this.subjNmbr = req.getSubjNmbr();
        this.findMan = req.getFindMan();
        this.strDt = req.getStrDt();
        this.endDt = req.getEndDt();
        this.stepCd = req.getStepCd();
        this.failNm = req.getFailNm();
        this.failRegCd = req.getFailRegCd();
        this.failLevlCd = req.getFailLevlCd();
        this.wthrCd = req.getWthrCd();
        this.failDesc = req.getFailDesc();
        this.befActDesc = req.getBefActDesc();
        this.etcRmk = req.getEtcRmk();
        this.useYn = req.getUseYn();
    }

}
