package com.its.op.entity.its.link;

import com.its.op.dto.its.link.TbLinkParaClctSystDto;
import com.its.op.entity.its.code.TbClctSystStup;
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_LINK_PARA_CLCT_SYST")
@IdClass(TbLinkParaClctSystKey.class)
public class TbLinkParaClctSyst implements Serializable {
    private static final long serialVersionUID = 1L;

    @ApiModelProperty("파라미터ID")  // N NUMBER(10)
    @Id
    @Column(name = "PARA_ID", nullable = false, columnDefinition = "NUMBER", length = 10)
    private Long paraId;

    @ApiModelProperty("수집 시스템 코드")  // N VARCHAR2(7)
    @Id
    @Column(name = "CLCT_SYST_CD", nullable = false, length = 7)
    private String clctSystCd;

    @ApiModelProperty("퓨전 우선순위")  // Y NUMBER(2)
    @Column(name = "FUSN_PRRT", columnDefinition = "NUMBER", length = 2)
    private Integer fusnPrrt;

    @ApiModelProperty("퓨전 가중치")  // Y NUMBER(5,2)
    @Column(name = "FUSN_WGHT", columnDefinition = "NUMBER", length = 5, precision = 2)
    private Double fusnWght;

    @OneToOne
    @JoinColumn(insertable=false, updatable=false, name="CLCT_SYST_CD", referencedColumnName = "CLCT_SYST_CD")
    @NotFound(action = NotFoundAction.IGNORE)
    private TbClctSystStup syst;

    @Builder.Default
    @Transient
    private boolean isNew = true;

    @PrePersist
    @PostLoad
    public void markNew() {
        this.isNew = false;
    }

    public TbLinkParaClctSystDto toDto() {
        TbLinkParaClctSystDto dto = TbLinkParaClctSystDto.builder()
                .paraId(this.paraId)
                .clctSystCd(this.clctSystCd)
                .fusnPrrt(this.fusnPrrt)
                .fusnWght(this.fusnWght)
                .build();

        if (this.syst != null) {
            dto.setClctSystDesc(this.syst.getClctSystNm());
        }
        return dto;
    }

    public TbLinkParaClctSyst(Long paraId, String clctSystCd) {
        this.paraId = paraId;
        this.clctSystCd = clctSystCd;
    }

    public void updateInfo(TbLinkParaClctSystDto.TbLinkParaClctSystUpdReq req) {
        this.fusnPrrt = req.getFusnPrrt();
        this.fusnWght = req.getFusnWght();
    }

}
