package com.sunxung.factoring.entity.sys;

import java.util.Date;

import com.alibaba.fastjson.annotation.JSONField;
import com.sunxung.factoring.component.util.SystemVariable;
import com.sunxung.factoring.entity.BaseEntity;

public class Session extends BaseEntity {

    private static final long serialVersionUID = 1L;
    /** 用户登录名 */
    private String loginName;
    /** 用户名 */
    private String userName;
    /** 对应用户id */
    private Long userId;
    /** 最后访问路径 */
    private String url;
    /** 登录时间 */
    @JSONField(format = "yyyy-MM-dd HH:mm:ss")
    private Date loginTime;
    /** 最后访问时间 */
    @JSONField(format = "yyyy-MM-dd HH:mm:ss")
    private Date laterTime;
    /** 最后访问ip */
    private String accessIp;
    /** 是否记住密码 */
    private boolean remember;
    /** 是否登录 **/
    private boolean isLogin = false;

    public Session(){
    }

    public Session(User user, boolean remember, String accessIp){
        Date date = new Date();
        this.setUserId(user.getId());
        this.setLoginName(user.getLoginName());
        this.setUserName(user.getName());
        this.setLaterTime(date);
        this.setLoginTime(date);
        this.setAccessIp(accessIp);
        this.setRemember(remember);
    }

    /** 判断session超时 */
    public boolean isSessionTimeOut() {
        if ((System.currentTimeMillis() - getLaterTime().getTime()) > SystemVariable.SESSION_OUT_TIME * 1000) {
            return true;
        }
        return false;
    }

    /** 判断session超时 */
    public boolean isCookieSessionTimeOut() {
        if ((System.currentTimeMillis() - getLaterTime().getTime()) > SystemVariable.cookieExpiry * 1000) {
            return true;
        }
        return false;
    }

    public String getLoginName() {
        return loginName;
    }

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

    public String getUserName() {
        return userName;
    }

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

    public Long getUserId() {
        return userId;
    }

    public void setUserId(Long userId) {
        this.userId = userId;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public Date getLoginTime() {
        return loginTime;
    }

    public void setLoginTime(Date loginTime) {
        this.loginTime = loginTime;
    }

    public Date getLaterTime() {
        return laterTime;
    }

    public void setLaterTime(Date laterTime) {
        this.laterTime = laterTime;
    }

    public String getAccessIp() {
        return accessIp;
    }

    public void setAccessIp(String accessIp) {
        this.accessIp = accessIp;
    }

    public boolean isRemember() {
        return remember;
    }

    public void setRemember(boolean remember) {
        this.remember = remember;
    }

    public boolean isLogin() {
        return isLogin;
    }

    public void setLogin(boolean isLogin) {
        this.isLogin = isLogin;
    }
}
