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

import com.its.op.dto.its.facility.TbFcltInfrDto;
import com.its.op.entity.its.oper.TbUserInfr;
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_FCLT_INFR")
public class TbFcltInfr implements Serializable {
    private static final long serialVersionUID = 1L;

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

    @ApiModelProperty("시설물 유형")  // N VARCHAR2(7)
    @Column(name = "FCLT_TYPE", nullable = false, length = 7)
    private String fcltType;

    @ApiModelProperty("시설물 ID")  // N VARCHAR2(30)
    @Column(name = "FCLT_ID", nullable = false, length = 30)
    private String fcltId;

    @ApiModelProperty("시설물 위치")  // Y VARCHAR2(100)
    @Column(name = "FCLT_LCTN", length = 100)
    private String fcltLctn;

    @ApiModelProperty("설치 위치 주소")  // Y VARCHAR2(200)
    @Column(name = "ISTL_LCTN_ADDR", length = 200)
    private String istlLctnAddr;

    @ApiModelProperty("모델 명")  // Y VARCHAR2(50)
    @Column(name = "MDL_NM", length = 50)
    private String mdlNm;

    @ApiModelProperty("사용 년수")  // Y NUMBER(3)
    @Column(name = "USE_YEAR", columnDefinition = "NUMBER", length = 3)
    private Integer useYear;

    @ApiModelProperty("설치일")  // Y VARCHAR2(8)
    @Column(name = "ISTL_DT", length = 8)
    private String istlDt;

    @ApiModelProperty("제조업체 ID")  // Y VARCHAR2(30)
    @Column(name = "MNFC_CMPY_ID", length = 30)
    private String mnfcCmpyId;

    @ApiModelProperty("설치업체 ID")  // Y VARCHAR2(30)
    @Column(name = "ISTL_CMPY_ID", length = 30)
    private String istlCmpyId;

    @ApiModelProperty("유지보수업체 ID")  // Y VARCHAR2(30)
    @Column(name = "MAINT_CMPY_ID", length = 30)
    private String maintCmpyId;

    @ApiModelProperty("RFID ID")  // Y VARCHAR2(30)
    @Column(name = "RFID_ID", length = 30)
    private String rfidId;

    @ApiModelProperty("관리책임자-정 사원번호(ID)")  // Y VARCHAR2(20)
    @Column(name = "MAIN_USER_ID", length = 20)
    private String mainUserId;

    @ApiModelProperty("관리책임자-부 사원번호(ID)")  // Y VARCHAR2(20)
    @Column(name = "SUB_USER_ID", length = 20)
    private String subUserId;

    @ApiModelProperty("유효기간")  // Y VARCHAR2(8)
    @Column(name = "VALID_YMD", length = 8)
    private String validYmd;

    @ApiModelProperty("사업단계")  // Y VARCHAR2(7)
    @Column(name = "WORK_STEP", length = 7)
    private String workStep;

    @ApiModelProperty("폐기 일시")  // Y VARCHAR2(14)
    @Column(name = "ABDN_DT", length = 14)
    private String abdnDt;

    @ApiModelProperty("금액")  // Y NUMBER(15)
    @Column(name = "AMT", columnDefinition = "NUMBER", length = 15)
    private Long amt;

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

    @OneToOne
    @JoinColumn(insertable=false, updatable=false, name="MNFC_CMPY_ID", referencedColumnName = "CMPY_ID")
    @NotFound(action = NotFoundAction.IGNORE)
    private TbFcltCmpy mnfcCmpy;

    @OneToOne
    @JoinColumn(insertable=false, updatable=false, name="ISTL_CMPY_ID", referencedColumnName = "CMPY_ID")
    @NotFound(action = NotFoundAction.IGNORE)
    private TbFcltCmpy istlCmpy;

    @OneToOne
    @JoinColumn(insertable=false, updatable=false, name="MAINT_CMPY_ID", referencedColumnName = "CMPY_ID")
    @NotFound(action = NotFoundAction.IGNORE)
    private TbFcltCmpy maintCmpy;

    @OneToOne
    @JoinColumn(insertable=false, updatable=false, name="MAIN_USER_ID", referencedColumnName = "USER_ID")
    @NotFound(action = NotFoundAction.IGNORE)
    private TbUserInfr mainUser;

    @OneToOne
    @JoinColumn(insertable=false, updatable=false, name="SUB_USER_ID", referencedColumnName = "USER_ID")
    @NotFound(action = NotFoundAction.IGNORE)
    private TbUserInfr subUser;


    public TbFcltInfrDto toDto() {
        TbFcltInfrDto dto = TbFcltInfrDto.builder()
                .fcltNmbr(this.fcltNmbr)
                .fcltType(this.fcltType)
                .fcltId(this.fcltId)
                .fcltLctn(this.fcltLctn)
                .istlLctnAddr(this.istlLctnAddr)
                .mdlNm(this.mdlNm)
                .useYear(this.useYear)
                .istlDt(this.istlDt)
                .mnfcCmpyId(this.mnfcCmpyId)
                .istlCmpyId(this.istlCmpyId)
                .maintCmpyId(this.maintCmpyId)
                .rfidId(this.rfidId)
                .mainUserId(this.mainUserId)
                .subUserId(this.subUserId)
                .validYmd(this.validYmd)
                .workStep(this.workStep)
                .abdnDt(this.abdnDt)
                .amt(this.amt)
                .delYn(this.delYn)
                .build();

        dto.setWorkStepDesc(CodeManager.getCodeDescShort(CodeManager.FMS_WSTEP, dto.getWorkStep()));
        if (this.mnfcCmpy != null) {
            dto.setMnfcCmpyNm(this.mnfcCmpy.getCmpyNm());
        }
        if (this.istlCmpy != null) {
            dto.setIstlCmpyNm(this.istlCmpy.getCmpyNm());
        }
        if (this.maintCmpyId != null) {
            dto.setMaintCmpyNm(this.maintCmpy.getCmpyNm());
        }
        if (this.mainUser != null) {
            dto.setMainUserNm(this.mainUser.getName());
        }
        if (this.subUser != null) {
            dto.setSubUserNm(this.subUser.getName());
        }
        return dto;
    }

    public TbFcltInfr(Long fcltNmbr) {
        this.fcltNmbr = fcltNmbr;
    }

    public void updateInfo(TbFcltInfrDto.TbFcltInfrUpdReq req) {
        this.fcltType = req.getFcltType();
        this.fcltId = req.getFcltId();
        this.fcltLctn = req.getFcltLctn();
        this.istlLctnAddr = req.getIstlLctnAddr();
        this.mdlNm = req.getMdlNm();
        this.useYear = req.getUseYear();
        this.istlDt = req.getIstlDt();
        this.mnfcCmpyId = req.getMnfcCmpyId();
        this.istlCmpyId = req.getIstlCmpyId();
        this.rfidId = req.getRfidId();
        this.mainUserId = req.getMainUserId();
        this.subUserId = req.getSubUserId();
        this.validYmd = req.getValidYmd();
        this.workStep = req.getWorkStep();
        this.abdnDt = req.getAbdnDt();
        this.amt = req.getAmt();
        this.delYn = req.getDelYn();
    }

}
