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

import com.its.api.utis.model.dto.RsemstDto;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.*;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction;

import javax.persistence.*;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * RSE마스터 Entity Class
 */
@Slf4j
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Builder
@AllArgsConstructor
@ApiModel("RSE마스터")
@Entity
@Table(name = "RSEMST")
public class Rsemst implements Serializable {
    private static final long serialVersionUID = 1L;

    @ApiModelProperty("RSEID")  // N VARCHAR(16)
    @Id
    @Column(name = "RSEID", nullable = false, length = 16)
    private String rseid;

    @ApiModelProperty("설치일자")  // Y DATE(0)
    @Column(name = "INSTDT")
    private LocalDateTime instdt;

    @ApiModelProperty("UTIS도시코드")  // Y NUMBER(3)
    @Column(name = "CITYCD", columnDefinition = "NUMBER", length = 3)
    private Integer citycd;

    @ApiModelProperty("권역코드")  // Y NUMBER(3)
    @Column(name = "REGIONCD", columnDefinition = "NUMBER", length = 3)
    private Integer regioncd;

    @ApiModelProperty("구역코드")  // Y NUMBER(3)
    @Column(name = "AREACD", columnDefinition = "NUMBER", length = 3)
    private Integer areacd;

    @ApiModelProperty("IP")  // Y VARCHAR(39)
    @Column(name = "RSEIP", length = 39)
    private String rseip;

    @ApiModelProperty("경도")  // Y NUMBER(10)
    @Column(name = "COORDX", columnDefinition = "NUMBER", length = 10)
    private Long coordx;

    @ApiModelProperty("위도")  // Y NUMBER(10)
    @Column(name = "COORDY", columnDefinition = "NUMBER", length = 10)
    private Long coordy;

    @ApiModelProperty("구역반경1")  // Y NUMBER(4)
    @Column(name = "RADIUS1", columnDefinition = "NUMBER", length = 4)
    private Integer radius1;

    @ApiModelProperty("구역반경2")  // Y NUMBER(4)
    @Column(name = "RADIUS2", columnDefinition = "NUMBER", length = 4)
    private Integer radius2;

    @ApiModelProperty("제조사코드")  // Y VARCHAR(5)
    @Column(name = "COMPCD", length = 5)
    private String compcd;

    @ApiModelProperty("RSE 설치위치")  // Y VARCHAR(32)
    @Column(name = "LOCATION", length = 32)
    private String location;

    @ApiModelProperty("RSE환경정보ID")  // Y NUMBER(3)
    @Column(name = "RSECONFIGID", columnDefinition = "NUMBER", length = 3)
    private Integer rseconfigid;

    @ApiModelProperty("OBE파라미터ID")  // Y NUMBER(3)
    @Column(name = "OBEPARAMID", columnDefinition = "NUMBER", length = 3)
    private Integer obeparamid;

    @ApiModelProperty("포트")  // Y NUMBER(5)
    @Column(name = "PORT", columnDefinition = "NUMBER", length = 5)
    private Integer port;

    @ApiModelProperty("브릿지RES여부(0:일반/1:브릿지)")  // Y NUMBER(1)
    @Column(name = "BRIDGE", columnDefinition = "NUMBER", length = 1)
    private Integer bridge;

    @ApiModelProperty("통신여부(0:통신안함/1:통신함)")  // Y NUMBER(1)
    @Column(name = "COMYN", columnDefinition = "NUMBER", length = 1)
    private Integer comyn;

    @ApiModelProperty("통신유형")  // Y NUMBER(1)
    @Column(name = "COMMTYPE", columnDefinition = "NUMBER", length = 1)
    private Integer commtype;

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

    public RsemstDto toDto() {
        RsemstDto dto = RsemstDto.builder()
                .rseid(this.rseid)
                .instdt(this.instdt)
                .citycd(this.citycd)
                .regioncd(this.regioncd)
                .areacd(this.areacd)
                .rseip(this.rseip)
                .coordx(this.coordx/10000000.0)
                .coordy(this.coordy/10000000.0)
                .radius1(this.radius1)
                .radius2(this.radius2)
                .compcd(this.compcd)
                .location(this.location)
                .rseconfigid(this.rseconfigid)
                .obeparamid(this.obeparamid)
                .port(this.port)
                .bridge(this.bridge)
                .comyn(this.comyn)
                .commtype(this.commtype)
                .build();

        if (this.state != null) {
            String collDt = DateTimeFormatter.ofPattern("yyyyMMddHHmmss").format(this.state.getColldt());
            dto.setCollDt(collDt);
            log.error("{}", dto);
        }
        return dto;
    }

}
