package cn.qinys.platform.base.security.dto;

import cn.qinys.platform.base.security.constants.Constants;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import lombok.Data;
import lombok.experimental.Accessors;


import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.Collection;
import java.util.List;
import java.util.Objects;

/**
 * Description:
 *
 * @author tanlie
 * Date: 2023/12/22 18:03
 */
@Data
@Accessors(chain = true)
public class AuthUserDetails implements UserDetails, Serializable {

    private static final long serialVersionUID = 1L;

    private Integer id;
    private String username;
    private String password;
    private String mobile;
    private String isExpired;
    private String isLocked;
    private LocalDateTime pwdExpired;
    private String fullName;
    private String tenantId;
    private String tenantName;
    private List<GrantedAuthority> authorities;

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return this.authorities;
    }

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

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

    @Override
    public boolean isAccountNonExpired() {
        return Objects.equals(this.isExpired, Constants.YesOrNoCons.NO.getCode());
    }

    @Override
    public boolean isAccountNonLocked() {
        return Objects.equals(this.isLocked, Constants.YesOrNoCons.NO.getCode());
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return LocalDateTime.now().isBefore(this.pwdExpired);
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}
