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

import com.its.api.its.global.CodeManager;
import com.its.api.its.model.dto.unit.TbUnitSystDto;
import com.its.api.its.model.dto.unit.TbUnitSystSttsDto;
import com.its.api.utils.ItsUtils;
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;

/**
 * 단위 시스템 Entity Class
 */
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Builder
@AllArgsConstructor
@ApiModel("단위 시스템")
@Entity
@Table(name = "TB_UNIT_SYST")
public class TbUnitSyst implements Serializable {
    private static final long serialVersionUID = 1L;

    @ApiModelProperty("시스템 ID")  // N VARCHAR(30)
    @Id
    @Column(name = "SYST_ID", nullable = false, length = 30)
    private String systId;

    @ApiModelProperty("시스템 유형")  // Y VARCHAR(7)
    @Column(name = "SYST_TYPE", length = 7)
    private String systType;

    @ApiModelProperty("시스템 번호")  // Y NUMBER(3)
    @Column(name = "SYST_NMBR", columnDefinition = "NUMBER", length = 3)
    private Integer systNmbr;

    @ApiModelProperty("시스템 명")  // Y VARCHAR(100)
    @Column(name = "SYST_NM", length = 100)
    private String systNm;

    @ApiModelProperty("시스템 IP_1")  // Y VARCHAR(20)
    @Column(name = "SYST_IP_1", length = 20)
    private String systIp1;

    @ApiModelProperty("시스템 IP_2")  // Y VARCHAR(20)
    @Column(name = "SYST_IP_2", length = 20)
    private String systIp2;

    @ApiModelProperty("프로그램 포트")  // Y VARCHAR(5)
    @Column(name = "PRGM_PORT", length = 5)
    private String prgmPort;

    @ApiModelProperty("호스트 명")  // Y VARCHAR(100)
    @Column(name = "HOST_NM", length = 100)
    private String hostNm;

    @ApiModelProperty("운영체제 명")  // Y VARCHAR(100)
    @Column(name = "OS_NM", length = 100)
    private String osNm;

    @ApiModelProperty("삭제 여부")  // Y CHAR(1)
    @Column(name = "DEL_YN", columnDefinition = "CHAR", length = 1)
    private String delYn;

    @ApiModelProperty("상태 표출 여부")  // Y CHAR(1)
    @Column(name = "STTS_DSPL_YN", columnDefinition = "CHAR", length = 1)
    private String sttsDsplYn;

    @OneToOne
    @JoinColumn(insertable=false, updatable=false, name="SYST_ID", referencedColumnName = "SYST_ID")
    @NotFound(action = NotFoundAction.IGNORE)
    private TbUnitSystStts state = new TbUnitSystStts();

    public TbUnitSystDto toDto() {
        TbUnitSystDto dto = TbUnitSystDto.builder()
                .systId(this.systId)
                .systType(this.systType)
                .systNmbr(this.systNmbr)
                .systNm(this.systNm)
                .systIp1(this.systIp1)
                .systIp2(this.systIp2)
                .prgmPort(this.prgmPort)
                .hostNm(this.hostNm)
                .osNm(this.osNm)
                .delYn(this.delYn)
                .sttsDsplYn(this.sttsDsplYn)
                .build();

        dto.setSystDesc("[" + this.systId + "] " +  this.systNm);
        return dto;
    }

    public TbUnitSystSttsDto toSttsDto() {
        TbUnitSystSttsDto dto = TbUnitSystSttsDto.builder()
                .systId(this.systId)
                .systNm(this.systNm)
                .systSttsCd("1")    // 기본값을 이상 으로 설정
                .updtDt(CodeManager.CMNC_STTS_ERROR_DT)
                .build();

        if (this.state != null) {
            dto.setUpdtDt(this.state.getUpdtDt());
            String missYn = ItsUtils.getMissSttsYn(dto.getUpdtDt());
            if (!("Y").equals(missYn)) {
                // 업데이트 시각이 기준시각 이내 일 경우 정상 데이터 처리
                String sttsCd = this.state.getSystSttsCd();
                if (("SPS0").equals(sttsCd)) {
                    dto.setSystSttsCd("2"); // 알수없음
                }
                else if (("SPS1").equals(sttsCd) || ("SPS2").equals(sttsCd) || ("SPS4").equals(sttsCd)) {
                    dto.setSystSttsCd("0"); // 정상
                    dto.setRunSts("0");
                }
                else {
                    dto.setSystSttsCd("1"); // 이상
                }

                if (("SPS1").equals(sttsCd) || ("SPS2").equals(sttsCd)) {
                    dto.setComSts("0");
                    dto.setDbSts("0");
                }
                else {
                    dto.setComSts("1");
                    dto.setDbSts("1");
                }
            }
        }

        if (("1").equals(dto.getSystSttsCd())) {
            // 상태테이블에 데이터가 존재하지 않을 경우 또는 상태정보가 이상인 경우
            dto.setSystSttsCd("1");
            dto.setRunSts("1");
            dto.setComSts("1");
            dto.setDbSts("1");
        }

        if (("0").equals(dto.getSystSttsCd())) {
            dto.setSystSttsDesc("정상");
        }
        else if (("1").equals(dto.getSystSttsCd())) {
            dto.setSystSttsDesc("이상");
        }
        else {
            dto.setSystSttsDesc("알수없음");
        }

        return dto;
    }

    public TbUnitSyst(String systId) {
        this.systId = systId;
    }

    public void updateInfo(TbUnitSystDto.TbUnitSystUpdReq req) {
        this.systType = req.getSystType();
        this.systNmbr = req.getSystNmbr();
        this.systNm = req.getSystNm();
        this.systIp1 = req.getSystIp1();
        this.systIp2 = req.getSystIp2();
        this.prgmPort = req.getPrgmPort();
        this.hostNm = req.getHostNm();
        this.osNm = req.getOsNm();
        this.delYn = req.getDelYn();
        this.sttsDsplYn = req.getSttsDsplYn();
    }

}
