package com.uas.platform.b2b.model;

import java.io.Serializable;
import java.util.Set;

import javax.persistence.Cacheable;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OrderBy;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

import org.codehaus.jackson.annotate.JsonIgnore;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

import com.uas.platform.core.model.Constant;

@Entity
@Table(name = "sec$roles")
@Cacheable
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE, region = "com.uas.platform.b2b.model.Role")
public class Role implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Id
	@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sec$roles_gen")
	@SequenceGenerator(name = "sec$roles_gen", sequenceName = "sec$roles_seq", allocationSize = 1)
	@Column(name = "role_id")
	private Long id;

	/**
	 * 角色名称
	 */
	@Column(name = "role_name")
	private String name;

	/**
	 * 角色是否属于超级用户组
	 */
	@Column(name = "issys")
	private Short issys;

	/**
	 * 角色详细描述
	 */
	@Column(name = "role_desc")
	private String desc;

	/**
	 * 角色拥有的权限
	 */
	@ManyToMany(cascade = { CascadeType.REFRESH }, fetch = FetchType.EAGER)
	@JoinTable(name = "sec$roleauthority", joinColumns = @JoinColumn(name = "role_id", referencedColumnName = "role_id"), inverseJoinColumns = @JoinColumn(name = "auth_id", referencedColumnName = "auth_id"))
	@OrderBy("id")
	@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
	private Set<Authority> authorities;
	
	/**
	 * 所属企业UU
	 */
	@Column(name = "role_enuu")
	private Long enUU;

	@JsonIgnore
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDesc() {
		return desc;
	}

	public void setDesc(String desc) {
		this.desc = desc;
	}

	public Short getIssys() {
		return issys;
	}

	public void setIssys(Short issys) {
		this.issys = issys;
	}

	public Set<Authority> getAuthorities() {
		return authorities;
	}

	public void setAuthorities(Set<Authority> authorities) {
		this.authorities = authorities;
	}

	public Long getEnUU() {
		return enUU;
	}

	public void setEnUU(Long enUU) {
		this.enUU = enUU;
	}

	@JsonIgnore
	public boolean isSys() {
		return getIssys() != null && getIssys() == Constant.YES;
	}

}
