package com.qmth.qrzk.repository.user.model;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

import com.qmth.qrzk.security.LoginUser;
import com.qmth.qrzk.supplypaper.model.PrintProvTask;

/**
 * 用户表
 * @author 钟辉
 *
 */
@Entity
@Table(name="s_user")
public class User implements LoginUser{

	@Id
	@GeneratedValue
	private int id; 					//主键id
	
	@Column(name="is_enabled")
	private int isEnabled = 1;				//是否启用
	
	@Column(name="last_login_date")
	@Temporal(TemporalType.TIMESTAMP)
	private Date lastLoginDate;			//最后登录时间
	
	private String name;				//用户名
	
	private String password;			//密码
	
	private String remark;				//备注
	
	@Column(name="update_date")
	@Temporal(TemporalType.TIMESTAMP)
	private Date updateDate;			//更新时间
	
	@Column(name="login_name")
	private String loginName;			//登录名
	
	private String certino;				
	
	private String phone;				//电话
	
	private String email;				//eamil
	
	private String position;			//所在位置
	
	private String department;			//所属什么部门
	
	@Transient
	private Integer instituteId;			
	
	@Column(name="user_type")
	private Integer userType;				//用户类型
	
	@ManyToMany
	@JoinTable(
		       name="s_user_role",
		       joinColumns = @JoinColumn( name="user_id"),
		       inverseJoinColumns = @JoinColumn( name="role_id")
		    )
	private List<Role> roles = new ArrayList<Role>();
	
	@Transient
	private List<PrintProvTask> tasks = new ArrayList<PrintProvTask>();

	public int getIsEnabled() {
		return isEnabled;
	}

	public void setIsEnabled(int isEnabled) {
		this.isEnabled = isEnabled;
	}

	public Date getLastLoginDate() {
		return lastLoginDate;
	}

	public void setLastLoginDate(Date lastLoginDate) {
		this.lastLoginDate = lastLoginDate;
	}

	public String getName() {
		return name;
	}

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

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getRemark() {
		return remark;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}

	public Date getUpdateDate() {
		return updateDate;
	}

	public void setUpdateDate(Date updateDate) {
		this.updateDate = updateDate;
	}

	public String getLoginName() {
		return loginName;
	}

	public void setLoginName(String loginName) {
		this.loginName = loginName;
	}

	public String getCertino() {
		return certino;
	}

	public void setCertino(String certino) {
		this.certino = certino;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getPosition() {
		return position;
	}

	public void setPosition(String position) {
		this.position = position;
	}

	public String getDepartment() {
		return department;
	}

	public void setDepartment(String department) {
		this.department = department;
	}

	public Integer getUserType() {
		return userType;
	}

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

	public int getId() {
		return id;
	}

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

	public Integer getInstituteId() {
		return instituteId;
	}

	public void setInstituteId(Integer instituteId) {
		this.instituteId = instituteId;
	}

	public boolean isEnabled() {
		return isEnabled == 1;
	}

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

	public void setRoles(List<Role> roles) {
		this.roles = roles;
	}
	
	public void addTask(PrintProvTask task){
		this.tasks.add(task);
	}
	
	public List<PrintProvTask> getTasks() {
		return tasks;
	}

	public void setTasks(List<PrintProvTask> tasks) {
		this.tasks = tasks;
	}

	public String getRoleString(){
		StringBuffer sb = new StringBuffer();
		for(Role role : roles){
			sb.append(role.getName()).append(" ; ");
		}
		return sb.toString();
	}
	

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

	public User() {
		super();
		// TODO Auto-generated constructor stub
	}

	public User(int isEnabled, String name, String password, String loginName,
			int userType) {
		super();
		this.isEnabled = isEnabled;
		this.name = name;
		this.password = password;
		this.loginName = loginName;
		this.userType = userType;
	}

	public User(int id, String password) {
		super();
		this.id = id;
		this.password = password;
	}

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