package com.uas.platform.b2b.model;

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

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.NotNull;

import org.codehaus.jackson.annotate.JsonIgnore;
import org.springframework.mobile.device.site.SitePreference;

import com.alibaba.fastjson.annotation.JSONField;
import com.uas.platform.core.model.Constant;

/**
 * 登录日志
 * 
 * @author yingp
 * 
 */
@Entity
@Table(name = "log$signin")
public class SigninLog implements Serializable {

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

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	@Column(name = "log_id")
	private Long id;

	@Column(name = "log_enuu")
	private Long enUU;

	@NotNull
	@Column(name = "log_useruu")
	private Long userUU;

	/**
	 * 登录时间
	 */
	@Temporal(value = TemporalType.TIMESTAMP)
	@Column(name = "log_time")
	private Date time;

	/**
	 * ip
	 */
	@Column(name = "log_ip")
	private String ip;

	/**
	 * @see SitePreference
	 */
	@Column(name = "log_agent")
	private String agent;

	/**
	 * 记住用户登录方式：通过浏览器cookie和数据库persistent_logins
	 */
	@Column(name = "log_remember")
	private Short isRemember;

	public SigninLog() {
		this.time = new Date();
	}

	public SigninLog(User user) {
		this();
		Enterprise enterprise = user.getEnterprise();
		if (enterprise != null) {
            this.enUU = enterprise.getUu();
        }
		this.userUU = user.getUserUU();
		this.ip = user.getIp();
	}

	public SigninLog(User user, SitePreference preference, boolean remember) {
		this(user);
		setAgent(preference);
		setIsRemember(remember ? Constant.YES : Constant.NO);
	}

	@JsonIgnore
	@JSONField(serialize = false)
	public Long getId() {
		return id;
	}

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

	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 Date getTime() {
		return time;
	}

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

	public String getIp() {
		return ip;
	}

	public void setIp(String ip) {
		this.ip = ip;
	}

	public String getAgent() {
		return agent;
	}

	public void setAgent(String agent) {
		this.agent = agent;
	}

	public void setAgent(SitePreference preference) {
		this.agent = preference.name();
	}

	public Short getIsRemember() {
		return isRemember;
	}

	public void setIsRemember(Short isRemember) {
		this.isRemember = isRemember;
	}

}
