package com.its.op.entity.its.code;

import com.its.op.global.CodeManager;
import com.its.op.dto.its.code.TbCmtrGradClsfDto;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.*;

import javax.persistence.*;
import java.io.Serializable;

/**
 * 소통 등급 분류 Entity Class
 */
@Getter
@NoArgsConstructor//(access = AccessLevel.PROTECTED)
@Builder
@AllArgsConstructor
@ApiModel("소통 등급 분류")
@Entity
@Table(name = "TB_SECT_GRAD_STUP")
@IdClass(TbSectGradStupKey.class)
public class TbSectGradStup implements Serializable {
    private static final long serialVersionUID = 1L;

    @ApiModelProperty("구간 등급 코드")  // N VARCHAR(7)
    @Id
    @Column(name = "SECT_GRAD_CD", nullable = false, length = 7)
    private String sectGradCd;

    @ApiModelProperty("소통 등급 코드")  // N VARCHAR(7)
    @Id
    @Column(name = "CMTR_GRAD_CD", nullable = false, length = 7)
    private String cmtrGradCd;

    @ApiModelProperty("최저 통행 속도")  // Y NUMBER(3)
    @Column(name = "LWST_TRVL_SPED", columnDefinition = "NUMBER", length = 3)
    private Integer lwstTrvlSped;

    @ApiModelProperty("최고 통행 속도")  // Y NUMBER(3)
    @Column(name = "HGHS_TRVL_SPED", columnDefinition = "NUMBER", length = 3)
    private Integer hghsTrvlSped;

    @ApiModelProperty("정체 등급 여부")  // Y CHAR(1)
    @Column(name = "CNGS_GRAD_YN", columnDefinition = "CHAR", length = 1)
    private String cngsGradYn;

    @OneToOne
    @JoinColumn(insertable=false, updatable=false, name="SECT_GRAD_CD", referencedColumnName = "SECT_GRAD_CD")
    private TbSectGrad sect;

    public TbCmtrGradClsfDto toDto() {
        TbCmtrGradClsfDto dto = TbCmtrGradClsfDto.builder()
                .sectGradCd(this.sectGradCd)
                .cmtrGradCd(this.cmtrGradCd)
                .lwstTrvlSped(this.lwstTrvlSped)
                .hghsTrvlSped(this.hghsTrvlSped)
                .cngsGradYn(this.cngsGradYn)
                .build();

        if (this.sect == null) {
            dto.setSectGradDesc("[" + this.sectGradCd + "] ?");
        }
        else {
            dto.setSectGradDesc("[" + this.sectGradCd + "]" + this.sect.getSectGradNm());
        }
        dto.setCmtrGradDesc(CodeManager.getCodeDesc(CodeManager.CMTR_GRAD_CD, this.cmtrGradCd));

        return dto;
    }

    public TbSectGradStup(String sectGradCd, String cmtrGradCd) {
        this.sectGradCd = sectGradCd;
        this.cmtrGradCd = cmtrGradCd;
    }

    public void updateInfo(TbCmtrGradClsfDto.TbCmtrGradClsfUpdReq req) {
        this.lwstTrvlSped = req.getLwstTrvlSped();
        this.hghsTrvlSped = req.getHghsTrvlSped();
        this.cngsGradYn = req.getCngsGradYn();
    }

}
