package com.eactive.eai.rms.data.entity.man.menu;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.Cacheable;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.PostLoad;
import javax.persistence.Table;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.Comment;

import com.eactive.eai.data.entity.AbstractEntity;

import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NonNull;
import lombok.ToString;

@Data
@EqualsAndHashCode(onlyExplicitlyIncluded = true, callSuper = true)

@Entity
@Table(name = "tseairm01")
@org.hibernate.annotations.Table(appliesTo = "tseairm01", comment = "메뉴 관리")
@Cacheable
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class Menu extends AbstractEntity<String> implements Serializable {
	private static final long serialVersionUID = 1L;

	@Id
	@Column(unique = true, nullable = false, length = 40)
	@Comment("메뉴 ID")
	private String menuId;

	@Column(nullable = false, length = 40)
	@Comment("상위 메뉴 ID")
	private String parentMenuId;

	@Column(nullable = false)
	@Comment("정렬 순서")
	private Integer sortOrder;

	@Column(nullable = false, length = 200)
	@Comment("메뉴명")
	private String menuName;

	@Column(nullable = false, length = 4000)
	@Comment("메뉴URL")
	private String menuUrl;

	@Column(length = 50)
	@Comment("메뉴 IMG")
	private String menuImage;

	@Column(nullable = false, length = 1)
	@Comment("출력 여부")
	private String displayYn;

	@Column(nullable = false, length = 1)
	@Comment("사용여부")
	private String useYn;

	@Column(length = 100)
	@Comment("데이터 경로")
	private String appPath;

	@Column(nullable = false, length = 3)
	@Comment("메뉴코드")
	private String appCode;

	@Override
	public @NonNull String getId() {
		return menuId;
	}

	@ManyToOne
	@JoinColumn(name = "parentMenuId", insertable = false, updatable = false)
	@ToString.Exclude
	private Menu parentMenu;

	@OneToMany(mappedBy = "parentMenu", cascade = CascadeType.ALL, orphanRemoval = true)
	@OrderBy("menuId")
	@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
	@ToString.Exclude
	private List<Menu> childMenus;

	@PostLoad
	private void postLoad() {
		if (this.getChildMenus() == null) {
			this.childMenus = new ArrayList<>();
		}
	}

}
