package cn.com.qmth.stmms.biz.user.model;

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

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;

import cn.com.qmth.stmms.common.annotation.ExcelField;
import cn.com.qmth.stmms.common.enums.Role;
import cn.com.qmth.stmms.common.enums.UserSource;
import cn.com.qmth.stmms.common.utils.AccessControlUtils;

@Entity
@Table(name = "b_user")
public class User implements Serializable {

    private static final long serialVersionUID = -2094897723165518198L;

    public static final int ACCESS_TOKEN_EXPIRE_SECOND = 3600 * 24;

    /**
     * 主键
     */
    @Id
    @GeneratedValue
    private Integer id;

    /**
     * 登录名
     */
    @ExcelField(title = "登录名", align = 2, sort = 10)
    @Column(name = "login_name", length = 64, nullable = false)
    private String loginName;

    /**
     * 名称
     */
    @ExcelField(title = "名称", align = 2, sort = 20)
    @Column(name = "name", length = 64, nullable = false)
    private String name;

    /**
     * 密码
     */
    @Column(name = "password", length = 64, nullable = false)
    private String password;

    /**
     * 角色
     */
    @Column(name = "role", length = 16, nullable = false)
    @Enumerated(EnumType.STRING)
    private Role role;

    /**
     * 来源
     */
    @Column(name = "source", length = 16, nullable = false)
    @Enumerated(EnumType.STRING)
    private UserSource source;

    /**
     * 状态
     */
    @Column(name = "enable", nullable = false)
    private boolean enable;

    /**
     * 所属学校ID
     */
    @Column(name = "school_id", nullable = false)
    private Integer schoolId;

    /**
     * 关联的外部用户标识
     */
    @Column(name = "related_account", length = 64, nullable = true)
    private String relatedAccount;

    /**
     * 最后一次登录时间
     */
    @Column(name = "last_login_time", nullable = true)
    private Date lastLoginTime;

    /**
     * 最后一次登录IP
     */
    @Column(name = "last_login_ip", length = 64, nullable = true)
    private String lastLoginIp;

    @Column(name = "created_time", nullable = false)
    private Date createdTime;

    @Column(name = "updated_time", nullable = false)
    private Date updatedTime;

    /**
     * 访问令牌
     */
    @Column(name = "access_token", length = 64, nullable = true)
    private String accessToken;

    /**
     * 访问令牌的刷新时间
     */
    @Column(name = "access_token_refresh_time", nullable = true)
    private Date accessTokenRefreshTime;

    @Column(name = "scan_token", length = 64, nullable = true)
    private String scanToken;

    @Column(name = "scan_token_invalid_time", nullable = true)
    private Date scanTokenInvalidTime;

    @Column(name = "description", length = 128, nullable = true)
    private String description;

    /**
     * 随机密码
     */
    @Column(name = "random_password", length = 64, nullable = true)
    private String randomPassword;

    /**
     * 工号
     */
    @Column(name = "empno", length = 64, nullable = true)
    private String empno;

    @ExcelField(title = "角色", align = 2, sort = 30)
    @Transient
    private String roleName;

    public Integer getId() {
        return id;
    }

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

    public String getLoginName() {
        return loginName;
    }

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

    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 Role getRole() {
        return role;
    }

    public void setRole(Role role) {
        this.role = role;
    }

    public Integer getSchoolId() {
        return schoolId;
    }

    public void setSchoolId(Integer schoolId) {
        this.schoolId = schoolId;
    }

    public Date getLastLoginTime() {
        return lastLoginTime;
    }

    public void setLastLoginTime(Date lastLoginTime) {
        this.lastLoginTime = lastLoginTime;
    }

    public String getLastLoginIp() {
        return lastLoginIp;
    }

    public void setLastLoginIp(String lastLoginIp) {
        this.lastLoginIp = lastLoginIp;
    }

    public Date getCreatedTime() {
        return createdTime;
    }

    public void setCreatedTime(Date createdTime) {
        this.createdTime = createdTime;
    }

    public Date getUpdatedTime() {
        return updatedTime;
    }

    public void setUpdatedTime(Date updatedTime) {
        this.updatedTime = updatedTime;
    }

    public UserSource getSource() {
        return source;
    }

    public void setSource(UserSource source) {
        this.source = source;
    }

    public String getRelatedAccount() {
        return relatedAccount;
    }

    public void setRelatedAccount(String relatedAccount) {
        this.relatedAccount = relatedAccount;
    }

    public boolean isEnable() {
        return enable;
    }

    public void setEnable(boolean enable) {
        this.enable = enable;
    }

    public String getAccessToken() {
        return accessToken;
    }

    public Date getAccessTokenRefreshTime() {
        return accessTokenRefreshTime;
    }

    public void refreshAccessToken() {
        this.accessToken = AccessControlUtils.randomString();
        this.accessTokenRefreshTime = new Date();
    }

    public String getAccount() {
        return source == UserSource.INTERNAL ? loginName : relatedAccount;
    }

    public String getRoleName() {
        return roleName;
    }

    public void setRoleName(String roleName) {
        this.roleName = roleName;
    }

    public String getScanToken() {
        return scanToken;
    }

    public Date getScanTokenInvalidTime() {
        return scanTokenInvalidTime;
    }

    public void refreshScanToken() {
        this.scanToken = AccessControlUtils.randomString();
        Calendar rightNow = Calendar.getInstance();
        rightNow.setTime(new Date());
        rightNow.add(Calendar.DAY_OF_YEAR, +2);
        this.scanTokenInvalidTime = rightNow.getTime();
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getRandomPassword() {
        return randomPassword;
    }

    public void setRandomPassword(String randomPassword) {
        this.randomPassword = randomPassword;
    }

    public String getEmpno() {
        return empno;
    }

    public void setEmpno(String empno) {
        this.empno = empno;
    }

}
