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

import com.its.api.its.model.dto.oper.TbGropMenuAthrDto;
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_GROP_MENU_ATHR")
@IdClass(TbGropMenuAthrKey.class)
public class TbGropMenuAthr implements Serializable {
    private static final long serialVersionUID = 1L;

    @ApiModelProperty("그룹ID")  // N VARCHAR(30)
    @Id
    @Column(name = "GROP_ID", nullable = false, length = 30)
    private String gropId;

    @ApiModelProperty("운영시스템메뉴ID")  // N VARCHAR(30)
    @Id
    @Column(name = "OPERSYST_MENU_ID", nullable = false, length = 30)
    private String operSystMenuId;

    @ApiModelProperty("권한 여부")  // Y CHAR(1)
    @Column(name = "ATHR_YN", columnDefinition = "CHAR", length = 1)
    private String athrYn;

    @ApiModelProperty("최종 수정 일시")  // Y VARCHAR(14)
    @Column(name = "LAST_CRCT_DT", length = 14)
    private String lastCrctDt;

    @ApiModelProperty("최종 수정자 번호")  // Y VARCHAR(20)
    @Column(name = "LAST_CRPR_NMBR", length = 20)
    private String lastCrprNmbr;

    @ApiModelProperty("최초 등록자 번호")  // Y VARCHAR(20)
    @Column(name = "FRST_REGR_NMBR", length = 20)
    private String frstRegrNmbr;

    @ApiModelProperty("최초 등록 일시")  // Y VARCHAR(14)
    @Column(name = "FRST_RGST_DT", length = 14)
    private String frstRgstDt;

    @OneToOne
    @JoinColumn(insertable=false, updatable=false, name = "GROP_ID", referencedColumnName = "GROP_ID")
    private TbUserGropInfr userGrop = new TbUserGropInfr();

    @OneToOne
    @JoinColumn(insertable=false, updatable=false, name = "OPERSYST_MENU_ID", referencedColumnName = "OPERSYST_MENU_ID")
    private TbOperSystMenu operMenu = new TbOperSystMenu();

    public TbGropMenuAthrDto toDto() {
        TbGropMenuAthrDto dto = TbGropMenuAthrDto.builder()
                .gropId(this.gropId)
                .operSystMenuId(this.operSystMenuId)
                .athrYn(this.athrYn)
                .build();

        dto.setAthrDesc(this.athrYn.equals("Y") ? "[Y] 권한있음" : "[N] 권한없음");

        return dto;
    }
    // 메뉴권한 조회할 때 사용하는 생성자
    public static TbGropMenuAthrDto toDtoByMenu(TbUserGropInfr userGrop, TbOperSystMenu systMenu) {
        return TbGropMenuAthrDto.builder()
                .gropId(userGrop.getGropId())
                .gropNm(userGrop.getGropNm())
                .operSystId(systMenu.getOperSystId())
                .operSystNm(systMenu.getOperSystNm())
                .operSystMenuId(systMenu.getOperSystMenuId())
                .operSystMenuNm(systMenu.getMenuName())
                .athrYn("Y")
                .athrDesc("[Y] 권한있음")
                .build();
    }

    public TbGropMenuAthr(String gropId, String operSystMenuId) {
        this.gropId = gropId;
        this.operSystMenuId = operSystMenuId;
    }

    public void updateInfo(TbGropMenuAthrDto.TbGropMenuAthrUpdReq req) {
        this.athrYn = req.getAthrYn();
    }

}
