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 javax.persistence.Transient;

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

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

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

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

	public User() {
	}

	public User(Long userUU) {
		this.userUU = userUU;
	}

	@Id
	@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "users_gen")
	@SequenceGenerator(name = "users_gen", sequenceName = "users_seq", allocationSize = 1)
	@Column(name = "user_uu")
	private Long userUU;

	@Column(name = "user_name", length = 50)
	private String userName;

	@Column(name = "user_pwd", length = 32)
	private String userPwd;

	@Column(name = "user_email", length = 50)
	private String userEmail;

	@Column(name = "user_tel", length = 30)
	private String userTel;

	@Column(name = "user_sex", length = 10)
	private String userSex;

	@Column(name = "user_idcode", length = 20)
	private String userIdcode;

	private Short enable;

	private Short issys;

	/**
	 * 用户类型
	 */
	@Column(name = "user_type", length = 20)
	private String userType;

	/**
	 * 所属公司
	 */
	@ManyToMany(cascade = { CascadeType.REFRESH }, fetch = FetchType.EAGER)
	@JoinTable(name = "sec$userenterprise", joinColumns = @JoinColumn(name = "user_uu", referencedColumnName = "user_uu"), inverseJoinColumns = @JoinColumn(name = "en_uu", referencedColumnName = "en_uu"))
	@OrderBy("uu")
	@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
	private Set<Enterprise> enterprises;

	/**
	 * 所属公司
	 */
	@Transient
	@NotFound(action = NotFoundAction.IGNORE)
	private Enterprise enterprise;

	/**
	 * 角色
	 */
	@ManyToMany(cascade = { CascadeType.PERSIST, CascadeType.MERGE }, fetch = FetchType.EAGER)
	@JoinTable(name = "sec$userrole", joinColumns = @JoinColumn(name = "user_uu", referencedColumnName = "user_uu"), inverseJoinColumns = @JoinColumn(name = "role_id", referencedColumnName = "role_id"))
	@OrderBy("id")
	@Cache(usage = CacheConcurrencyStrategy.READ_ONLY)
	private Set<Role> roles;

	public Long getUserUU() {
		return userUU;
	}

	public void setUserUU(Long userUU) {
		this.userUU = userUU;
	}

	public String getUserEmail() {
		return userEmail;
	}

	public void setUserEmail(String userEmail) {
		this.userEmail = userEmail;
	}

	public String getUserTel() {
		return userTel;
	}

	public void setUserTel(String userTel) {
		this.userTel = userTel;
	}

	public String getUserIdcode() {
		return userIdcode;
	}

	public void setUserIccode(String userIdcode) {
		this.userIdcode = userIdcode;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	@JsonIgnore
	public String getUserPwd() {
		return userPwd;
	}

	public void setUserPwd(String userPwd) {
		this.userPwd = userPwd;
	}

	public Short getEnable() {
		return enable;
	}

	public void setEnable(Short enable) {
		this.enable = enable;
	}

	public Short getIssys() {
		return issys;
	}

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

	public String getUserSex() {
		return userSex;
	}

	public void setUserSex(String userSex) {
		this.userSex = userSex;
	}

	public Enterprise getEnterprise() {
		return enterprise;
	}

	public void setEnterprise(Enterprise enterprise) {
		this.enterprise = enterprise;
	}

	public String getUserType() {
		return userType;
	}

	public void setUserType(String userType) {
		this.userType = userType;
	}

	public void setUserIdcode(String userIdcode) {
		this.userIdcode = userIdcode;
	}

	@JsonIgnore
	public Set<Enterprise> getEnterprises() {
		return enterprises;
	}

	public void setEnterprises(Set<Enterprise> enterprises) {
		this.enterprises = enterprises;
	}

	@JsonIgnore
	public boolean enable() {
		return this.getEnable() != null && this.getEnable() == Constant.YES;
	}

	public void setCurrentEnterprise() {
		if (this.enterprises != null && this.enterprises.size() > 0)
			this.enterprise = this.enterprises.iterator().next();
	}

	public Set<Role> getRoles() {
		return roles;
	}

	public void setRoles(Set<Role> roles) {
		this.roles = roles;
	}

	public void addEnterprise(Enterprise enterprise) {
		if (!this.enterprises.contains(enterprise)) {
			this.enterprises.add(enterprise);
		}
	}

	public void removeEnterprise(Enterprise enterprise) {
		if (this.enterprises.contains(enterprise)) {
			this.enterprises.remove(enterprise);
		}
	}

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

}
