package com.uas.platform.b2b.model;

import com.alibaba.fastjson.annotation.JSONField;
import com.uas.platform.core.model.Constant;
import org.codehaus.jackson.annotate.JsonIgnore;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

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.Index;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import java.io.Serializable;
import java.util.Set;

@Entity
@Table(name = "sec$roles", indexes = { @Index(name = "sec$roles_enuu_name", columnList = "role_enuu,role_name", unique = true),
		@Index(name = "sec$roles_enuu_desc", columnList = "role_enuu,role_desc", unique = true) })
@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.AUTO)
	@Column(name = "role_id")
	private Long id;

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

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

	/**
	 * 角色是否系统默认创建
	 */
	@Column(name = "isdefault")
	private Short isdefault;

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

	/**
	 * 角色拥有的权限
	 */
	@ManyToMany(cascade = { CascadeType.REFRESH }, fetch = FetchType.EAGER)
	@JoinTable(name = "sec$roleresource", joinColumns = @JoinColumn(name = "role_id", referencedColumnName = "role_id"), inverseJoinColumns = @JoinColumn(name = "res_id", referencedColumnName = "item_id"))
	@OrderBy("id")
	@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
	private Set<ResourceItem> resourceItems;

	/**
	 * 所属企业UU
	 */
	@Column(name = "role_enuu")
	private Long enUU;

	/**
	 * 职责
	 */
	@Column(name = "role_duty")
	private String duty;

	/**
	 * 下标，用于排序
	 */
	@Column(name = "role_index")
	private Long index;

	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<ResourceItem> getResourceItems() {
		return resourceItems;
	}

	public void setResourceItems(Set<ResourceItem> resourceItems) {
		this.resourceItems = resourceItems;
	}

	public Long getEnUU() {
		return enUU;
	}

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

	public Short getIsdefault() {
		return isdefault;
	}

	public void setIsdefault(Short isdefault) {
		this.isdefault = isdefault;
	}

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

	public String getDuty() {
		return duty;
	}

	public void setDuty(String duty) {
		this.duty = duty;
	}

	public Long getIndex() {
		return index;
	}

	public void setIndex(Long index) {
		this.index = index;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
            return true;
        }
		if (obj == null) {
            return false;
        }
		if (getClass() != obj.getClass()) {
            return false;
        }
		Role other = (Role) obj;
		if (id == null) {
			if (other.id != null) {
                return false;
            }
		} else if (!id.equals(other.id)) {
            return false;
        }
		return true;
	}

	@Override
	public String toString() {
		return "Role{" +
				"id=" + id +
				", name='" + name + '\'' +
				", issys=" + issys +
				", isdefault=" + isdefault +
				", desc='" + desc + '\'' +
				", resourceItems=" + resourceItems +
				", enUU=" + enUU +
				", duty='" + duty + '\'' +
				'}';
	}
}
