package com.uas.platform.b2b.model;

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

import com.uas.platform.core.util.StringUtil;

/**
 * token登录，用于存放token信息
 * 
 * @author yingp
 *
 */
public class AccessToken implements Serializable {

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

	private String id;

	/**
	 * 用户名
	 */
	private String user;

	/**
	 * 企业
	 */
	private Long enUU;

	/**
	 * 用户
	 */
	private Long userUU;

	/**
	 * 可以在token绑定任意数据
	 */
	private String bind;

	private Date time;

	private int expires_in;

	/**
	 * 默认60秒过期
	 */
	public final static int default_expires_in = 60;

	public AccessToken() {
	}

	public AccessToken(User user, Object bindObject) {
		this(user, bindObject, default_expires_in);
	}

	public AccessToken(User user, Object bindObject, int expires_in) {
		this.id = StringUtil.uuid();
		this.user = user.getUserName();
		this.enUU = user.getEnterprise().getUu();
		this.userUU = user.getUserUU();
		this.bind = String.valueOf(bindObject);
		this.time = new Date();
		this.expires_in = expires_in;
	}

	public AccessToken(UserBaseInfo user, long enUU, Object bindObject, int expires_in) {
		this.id = StringUtil.uuid();
		this.user = user.getUserName();
		this.enUU = enUU;
		this.userUU = user.getUserUU();
		this.bind = String.valueOf(bindObject);
		this.time = new Date();
		this.expires_in = expires_in;
	}

	public String getId() {
		return id;
	}

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

	public String getUser() {
		return user;
	}

	public void setUser(String user) {
		this.user = user;
	}

	public Long getEnUU() {
		return enUU;
	}

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

	public Long getUserUU() {
		return userUU;
	}

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

	public Object getBind() {
		return bind;
	}

	public void setBind(String bind) {
		this.bind = bind;
	}

	public Date getTime() {
		return time;
	}

	public void setTime(Date time) {
		this.time = time;
	}

	public int getExpires_in() {
		return expires_in;
	}

	public void setExpires_in(int expires_in) {
		this.expires_in = expires_in;
	}

	/**
	 * 是否过期
	 * 
	 * @return
	 */
	public boolean isExpired() {
		return System.currentTimeMillis() - this.time.getTime() > expires_in * 1000;
	}

}
