package com.skr.mdm.dto;

import cn.hutool.core.collection.CollectionUtil;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.skr.mdm.entity.AuAdministratorDO;
import lombok.Data;
import lombok.ToString;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author nfq
 * @date 2020/7/6 13:57
 */
@Data
@ToString(callSuper = true)
public class LoginAdminDTO extends AuAdministratorDO implements UserDetails {

    private static final long serialVersionUID = 562209312114460895L;

    private Integer roleId;
    /**
     * 登录时间
     */
    private Long loginTime;
    /**
     * token过期时间
     */
    private Long tokenExpireTime;
    /**
     * 刷新token过期时间
     */
    private Long refreshTokenExpireTime;
    /**
     * 权限集合
     */
    private Set<String> permissionList;

    @Override
    @JsonIgnore
    public Collection<? extends GrantedAuthority> getAuthorities() {
        if (CollectionUtil.isEmpty(permissionList)) {
            return null;
        }
        return permissionList.stream().map(SimpleGrantedAuthority::new).collect(Collectors.toSet());
    }

    @Override
    @JsonIgnore
    public String getUsername() {
        return super.getAdminName();
    }

    @Override
    @JsonIgnore
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    @JsonIgnore
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    @JsonIgnore
    public boolean isCredentialsNonExpired() {
        return true;
    }

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