package cn.com.liandisys.ycjt.ggcx.admin.model;

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

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;

import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Type;
import org.springframework.format.annotation.DateTimeFormat;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonTypeName;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;

import cn.com.liandisys.ycjt.ggcx.common.util.AvailableBooleanDeSerialize;
import cn.com.liandisys.ycjt.ggcx.common.util.AvailableBooleanSerialize;

/**
 * 用户
 * 
 * @author jiangb
 */
@Entity
@Table(name = "T_SYS_USER")
@JsonIgnoreProperties(value = { "hibernateLazyInitializer", "handler"})
@JsonTypeName(value = "User")
public class User implements Serializable{
	
	private static final long serialVersionUID = 1L;
	/** 记录主键 */
	private String id;
	/** 是否管理员 */
	protected boolean admin;
	/** 记录是否可用 */
	protected boolean avaliable;
	/** 登录名称 */
	protected String loginName;
	/** 密码 */
	protected String password;
	/** 记录排序号 */
	protected Integer orderid;

	/** 用户姓名 */
	protected String name;
	/** 工 号 */
	protected String workno;
	/** 性 别 */
	protected String sex;    
	/** 出生年月 */
	@DateTimeFormat(pattern = "yyyy/MM/dd")
	@JsonFormat(pattern = "yyyy/MM/dd")
	protected Date birthday;
	/** 籍 贯 */
	protected String origo;
	/** 民 族 */
	protected String nation;
	/** 政治面貌 */
	protected String partyMember;
	/** 学 历 */
	protected String education;
	/** 编 制 */
	protected String formation;
	/** 毕业时间 */
	@DateTimeFormat(pattern = "yyyy/MM/dd")
	@JsonFormat(pattern = "yyyy/MM/dd")
	protected Date graduateTime;
	/** 工作时间 */
	@DateTimeFormat(pattern = "yyyy/MM/dd")
	@JsonFormat(pattern = "yyyy/MM/dd")
	protected Date startworkTime;
	/** 职 务 */
	protected String job;
	/** 手机号码 */
	protected String cellphone;
	/** 密 级 */
	protected String secretRate;
	/** 家庭住址 */
	protected String address;
	/** 住宅电话 */
	protected String telephone;
	/** 身份证号码 */
	protected String identityNumber;
	/** 上级名称 */
	protected String upperName;
	/** 上级登录名 */
	protected String upperLoginName;
	
	/** 用户角色 */
	protected List<Role> roles;

	public User(String loginName, String name) {
		this.loginName = loginName;
		this.name = name;
	}

	public User() {

	}

	@Id
    @GeneratedValue(generator = "system-uuid")
    @GenericGenerator(name = "system-uuid", strategy = "uuid")
    @Column(name="id",length=32)
	public String getId() {
		return id;
	}
	
	public void setId(String id){
		this.id = id;
	}

	public String getLoginName() {
		return loginName;
	}

	@Column(nullable = false)
	public String getName() {
		return name;
	}

	public String getPassword() {
		return password;
	}

	@Type(type = "yes_no")
	@JsonSerialize(using = AvailableBooleanSerialize.class)
	public boolean isAdmin() {
		return admin;
	}

	@JsonSerialize(using = AvailableBooleanSerialize.class)
	@Type(type = "yes_no")
	public boolean isAvaliable() {
		return avaliable;
	}


	@Column(nullable = true)
	public Integer getOrderid() {
		return orderid;
	}

	/**
	 * 设置用户是否为管理员
	 * 
	 * @param admin
	 */
	@JsonDeserialize(using = AvailableBooleanDeSerialize.class)
	public void setAdmin(boolean admin) {
		this.admin = admin;
	}

	/**
	 * 设置记录状态是否可用
	 * 
	 * @param avaliable
	 */
	@JsonDeserialize(using = AvailableBooleanDeSerialize.class)
	public void setAvaliable(boolean avaliable) {
		this.avaliable = avaliable;
	}

	/**
	 * 设置用户的系统登录名
	 * 
	 * @param loginName
	 */
	public void setLoginName(String loginName) {
		this.loginName = loginName;
	}

	/**
	 * 设置用户姓名
	 * 
	 * @param name
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * 设置记录排序号
	 * 
	 * @param orderid
	 */
	public void setOrderid(Integer orderid) {
		this.orderid = orderid;
	}

	/**
	 * 设置用户密码
	 * 
	 * @param password
	 */
	public void setPassword(String password) {
		this.password = password;
	}

	@Override
	public String toString() {
		return "User [loginName=" + loginName + ", name=" + name + "]";
	}

	@Column(name = "workno")
	public String getWorkno() {
		return workno;
	}

	public void setWorkno(String workno) {
		this.workno = workno;
	}

	@Column(name = "sex")
	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	@Column(name = "birthday")
	public Date getBirthday() {
		return birthday;
	}

	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}

	@Column(name = "origo")
	public String getOrigo() {
		return origo;
	}

	public void setOrigo(String origo) {
		this.origo = origo;
	}

	@Column(name = "nation")
	public String getNation() {
		return nation;
	}

	public void setNation(String nation) {
		this.nation = nation;
	}

	@Column(name = "partymember")
	public String getPartyMember() {
		return partyMember;
	}

	public void setPartyMember(String partyMember) {
		this.partyMember = partyMember;
	}

	@Column(name = "education")
	public String getEducation() {
		return education;
	}

	public void setEducation(String education) {
		this.education = education;
	}

	@Column(name = "formation")
	public String getFormation() {
		return formation;
	}

	public void setFormation(String formation) {
		this.formation = formation;
	}

	@Column(name = "graduatetime")
	public Date getGraduateTime() {
		return graduateTime;
	}

	public void setGraduateTime(Date graduateTime) {
		this.graduateTime = graduateTime;
	}

	@Column(name = "startworktime")
	public Date getStartworkTime() {
		return startworkTime;
	}

	public void setStartworkTime(Date startworkTime) {
		this.startworkTime = startworkTime;
	}

	@Column(name = "job")
	public String getJob() {
		return job;
	}

	public void setJob(String job) {
		this.job = job;
	}

	@Column(name = "cellphone")
	public String getCellphone() {
		return cellphone;
	}

	public void setCellphone(String cellphone) {
		this.cellphone = cellphone;
	}

	@Column(name = "secretrate")
	public String getSecretRate() {
		return secretRate;
	}

	public void setSecretRate(String secretRate) {
		this.secretRate = secretRate;
	}

	@Column(name = "address")
	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	@Column(name = "telephone")
	public String getTelephone() {
		return telephone;
	}

	public void setTelephone(String telephone) {
		this.telephone = telephone;
	}

	@Column(name = "identitynumber")
	public String getIdentityNumber() {
		return identityNumber;
	}

	public void setIdentityNumber(String identityNumber) {
		this.identityNumber = identityNumber;
	}

	@Column(name = "upper_name")
	public String getUpperName() {
		return upperName;
	}

	public void setUpperName(String upperName) {
		this.upperName = upperName;
	}

	@Column(name = "upper_login_name")
	public String getUpperLoginName() {
		return upperLoginName;
	}

	public void setUpperLoginName(String upperLoginName) {
		this.upperLoginName = upperLoginName;
	}
	
	@ManyToMany(fetch = FetchType.EAGER, targetEntity = Role.class)
	@JoinTable(name = "t_sys_user_role", joinColumns = { @JoinColumn(name = "user_id") }, inverseJoinColumns = { @JoinColumn(name = "role_id") })
	@OrderBy("id")
	public List<Role> getRoles() {
		return roles;
	}
	
	/**
	 * 设置用户的角色
	 * 
	 * @param roles
	 */
	public void setRoles(List<Role> roles) {
		this.roles = roles;
	}

}
