package com.its.op.entity.its.oper;

import com.its.op.dto.its.oper.TbUserGropMenuAthrDto;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

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

/**
 * 그룹-메뉴권한 Entity Class
 */
@Getter
@NoArgsConstructor//(access = AccessLevel.PROTECTED)
@Builder
@AllArgsConstructor
@ApiModel("그룹-메뉴권한")
@Entity
@Table(name = "TB_USER_GROP_MENU_ATHR")
@IdClass(TbUserGropMenuAthrKey.class)
public class TbUserGropMenuAthr implements Serializable {
    private static final long serialVersionUID = 1L;

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

    @ApiModelProperty("운영시스템ID")  // N VARCHAR2(30)
    @Id
    @Column(name = "OPER_SYST_ID", nullable = false, length = 7)
    private String operSystId;

    @ApiModelProperty("운영시스템메뉴ID")  // N VARCHAR2(100)
    @Id
    @Column(name = "MENU_ID", nullable = false, length = 100)
    private String menuId;

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

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

    @OneToOne
    @JoinColumns({
        @JoinColumn(insertable=false, updatable=false, name = "OPER_SYST_ID", referencedColumnName = "OPER_SYST_ID"),
        @JoinColumn(insertable=false, updatable=false, name = "MENU_ID", referencedColumnName = "MENU_ID")
    })
    private TbOperSystMenu operMenu;

    public TbUserGropMenuAthrDto toDto() {
        TbUserGropMenuAthrDto dto = TbUserGropMenuAthrDto.builder()
                .gropId(this.gropId)
                .operSystId(this.operSystId)
                .menuId(this.menuId)
                .athrYn(this.athrYn)
                .build();

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

        return dto;
    }

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

    public TbUserGropMenuAthr(String gropId, String operSystId, String menuId) {
        this.gropId = gropId;
        this.operSystId = operSystId;
        this.menuId = menuId;
    }

    public void updateInfo(TbUserGropMenuAthrDto.TbUserGropMenuAthrUpdReq req) {
        this.athrYn = req.getAthrYn();
    }

}
