package com.enterprise.manage.modules.system.domain.po;

import com.enterprise.manage.common.base.BaseDomain;
import com.enterprise.manage.common.constant.Constants;
import com.enterprise.manage.modules.asset.domain.Enterprise;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

import java.util.Collection;
import java.util.Date;
import java.util.List;

/**
 * system user table
 * 系统核心用户类，实现了springboot security的{@link UserDetails}类，自定义实现逻辑.
 *
 * @author PeiZhi Guo
 * 2021-2-18 10:52
 */
public class User extends BaseDomain implements UserDetails {

    private Long id;

    private Org org;
    /**
     * 企业表ID
     */
    private Enterprise enterprise;

    private String  username;

    private String  password;

    private String  realName; // 用户昵称

    private String  avatar; // 用户头像

    private Integer sex;

    private String  phone;

    private String  email;

    private String  token;

    private Long loginTime;

    private Long expireTime;

    private boolean enable; // 启用标识 0禁用 1正常

    private Integer lock; //锁定 0锁定 1正常

    private Integer loginFailureNum; //登录失败次数

    private Date lastLoginTime; //最后登录时间

    private boolean delFlag; // 删除标识 0删除 1正常

    private List<Menu> menuList;

    private List<Role> roleList;

    @Override
    public void insertConfigure() {
        this.delFlag = true;
        this.enable = true;
        this.setPassword(new BCryptPasswordEncoder().encode(Constants.DEFAULT_PASSWORD));
        super.insertConfigure();
    }

    @Override
    public void updateConfigure() {
        this.delFlag = true;
        this.enable = true;
        super.updateConfigure();
    }

    public Long getId() {
        return id;
    }

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

    public void setUsername(String username) {
        this.username = username;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getRealName() {
        return realName;
    }

    public void setRealName(String realName) {
        this.realName = realName;
    }

    public String getAvatar() {
        return avatar;
    }

    public void setAvatar(String avatar) {
        this.avatar = avatar;
    }

    public Integer getSex() {
        return sex;
    }

    public void setSex(Integer sex) {
        this.sex = sex;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    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 List<Menu> getMenuList() {
        return menuList;
    }

    public void setMenuList(List<Menu> menuList) {
        this.menuList = menuList;
    }

    public List<Role> getRoleList() {
        return roleList;
    }

    public void setRoleList(List<Role> roleList) {
        this.roleList = roleList;
    }

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

    public boolean isEnable() {return enable; }

    public Integer getLock() {
        return lock;
    }

    public void setLock(Integer lock) {
        this.lock = lock;
    }

    public Date getLastLoginTime() {
        return lastLoginTime;
    }

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

    public Integer getLoginFailureNum() {
        return loginFailureNum;
    }

    public void setLoginFailureNum(Integer loginFailureNum) {
        this.loginFailureNum = loginFailureNum;
    }

    public void setDelFlag(boolean delFlag) {
        this.delFlag = delFlag;
    }

    public boolean isDelFlag() { return delFlag; }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        // 此处作用于注解 @PreAuthorize("hasAuthority('')")内授权验证，
        // 已重写自定义注解，此处不做逻辑处理
        return null;
    }

    @Override
    public String getUsername() {
        return username;
    }

    @Override
    public String getPassword() {
        return password;
    }

    @Override
    public boolean isAccountNonExpired() {
        // 账号是否过期，可自定义
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        // 账号是否锁定，此处使用逻辑删除，则视为锁定状态
        if (lock != null && lock > 0) {
            return true;
        }
        return false;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        // 凭证是否过期，暂时用不到
        return true;
    }

    @Override
    public boolean isEnabled() {
        // 账号是否启用
        return enable;
    }

    public Org getOrg() {
        return org;
    }

    public void setOrg(Org org) {
        this.org = org;
    }

    public Enterprise getEnterprise() {
        return enterprise;
    }

    public void setEnterprise(Enterprise enterprise) {
        this.enterprise = enterprise;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", org=" + org +
                ", enterprise=" + enterprise +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", realName='" + realName + '\'' +
                ", avatar='" + avatar + '\'' +
                ", sex=" + sex +
                ", phone='" + phone + '\'' +
                ", email='" + email + '\'' +
                ", token='" + token + '\'' +
                ", loginTime=" + loginTime +
                ", expireTime=" + expireTime +
                ", enable=" + enable +
                ", lock=" + lock +
                ", loginFailureNum=" + loginFailureNum +
                ", lastLoginTime=" + lastLoginTime +
                ", delFlag=" + delFlag +
                ", menuList=" + menuList +
                ", roleList=" + roleList +
                '}';
    }
}
