package com.philisense.www.pojos.entity;

import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.philisense.www.core.text.Convert;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import java.util.*;

/**
 * 登录用户身份权限
 *
 * @author
 */
public class JwtUser implements UserDetails {

    private static final long serialVersionUID = 1L;

    public JwtUser() {

    }

    public JwtUser(Map<String, Object> user) {
        this.user = user;
    }

    public JwtUser(String userId, String usernameCn, String deptCode, String deptName, Map<String, Object> user) {
        this.userId = userId;
        this.usernameCn = usernameCn;
        this.deptCode = deptCode;
        this.deptName = deptName;
        this.user = user;
    }

    /**
     * 用户ID
     */
    private  String userId;

    /**
     * 用户名称
     */
    private  String usernameCn;

    /**
     * 部门Code
     */
    private  String deptCode;

    /**
     * 部门名称
     */
    private  String deptName;

    /**
     * 用户唯一标识
     */
    private String token;

    /**
     * 登录时间
     */
    private Long loginTime;

    /**
     * 过期时间
     */
    private Long expireTime;

    /**
     * 登录IP地址
     */
    private String ipaddr;

    /**
     * 登录地点
     */
    private String loginLocation;

    /**
     * 浏览器类型
     */
    private String browser;

    /**
     * 操作系统
     */
    private String os;

    /**
     * 客户端
     */
    private Integer client;

    /**
     * 用户信息
     */
    private Map<String, Object> user;

    /**
     * 角色列表
     */
    @JsonIgnore
    private Collection<GrantedAuthority> authorities;

    public String getUserId() {
        return userId;
    }

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

    public String getUsernameCn() {
        return usernameCn;
    }

    public void setUsernameCn(String usernameCn) {
        this.usernameCn = usernameCn;
    }

    public String getDeptCode() {
        return deptCode;
    }

    public void setDeptCode(String deptCode) {
        this.deptCode = deptCode;
    }

    public String getDeptName() {
        return deptName;
    }

    public void setDeptName(String deptName) {
        this.deptName = deptName;
    }

    public String getToken() {
        return token;
    }

    public void setToken(String token) {
        this.token = token;
    }

    public Long getLoginTime() {
        return loginTime;
    }

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

    public Long getExpireTime() {
        return expireTime;
    }

    public void setExpireTime(Long expireTime) {
        this.expireTime = expireTime;
    }

    public String getIpaddr() {
        return ipaddr;
    }

    public void setIpaddr(String ipaddr) {
        this.ipaddr = ipaddr;
    }

    public String getLoginLocation() {
        return loginLocation;
    }

    public void setLoginLocation(String loginLocation) {
        this.loginLocation = loginLocation;
    }

    public String getBrowser() {
        return browser;
    }

    public void setBrowser(String browser) {
        this.browser = browser;
    }

    public String getOs() {
        return os;
    }

    public void setOs(String os) {
        this.os = os;
    }

    public Integer getClient() {
        return client;
    }

    public void setClient(Integer client) {
        this.client = client;
    }

    public Map<String, Object> getUser() {
        return user;
    }

    public void setUser(Map<String, Object> user) {
        this.user = user;
    }

    public void setAuthorities(Collection<GrantedAuthority> authorities) {
        this.authorities = authorities;
    }

    /**
     * 获取用户角色
     * @return
     */
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return authorities;
    }

    /**
     * 获取用户密码
     * @return
     */
    @JSONField(serialize = false)
    @Override
    @JsonIgnore
    public String getPassword() {
        return Convert.toStr(user.get("password"), "");
    }

    /**
     * 获取用户名
     * @return
     */
    @Override
    public String getUsername() {
        return Convert.toStr(user.get("user_name"), "");
    }

    /**
     * 账户是否未过期,过期无法验证
     */
    @JSONField(serialize = false)
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    /**
     * 指定用户是否解锁,锁定的用户无法进行身份验证
     *
     * @return
     */
    @JSONField(serialize = false)
    @Override
    public boolean isAccountNonLocked() {
        return "3".equals(Convert.toStr(user.get("status")));
    }

    /**
     * 指示是否已过期的用户的凭据(密码),过期的凭据防止认证
     *
     * @return
     */
    @JSONField(serialize = false)
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    /**
     * 是否可用 ,禁用的用户不能身份验证
     *
     * @return
     */
    @JSONField(serialize = false)
    @Override
    public boolean isEnabled() {
        return "0".equals(Convert.toStr(user.get("status"))) || "2".equals(Convert.toStr(user.get("status")));
    }

}
