package com.ningdatech.pmapi.user.security.auth.model;

import cn.hutool.core.collection.CollectionUtil;
import com.ningdatech.basic.auth.AbstractLoginUser;
import com.ningdatech.pmapi.sys.model.entity.Role;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @author LiuXinXin
 * @date 2022/8/1 下午3:32
 */
@Data
@EqualsAndHashCode(callSuper = true)
public class UserInfoDetails extends AbstractLoginUser implements UserDetails {

    private String realName;

    private String password;

    /**
     * 所属区域编码
     */
    private String regionCode;

    /**
     * 浙政钉扫码 员工code
     */
    private String employeeCode;

    /**
     * 浙政钉组织code
     */
    private String organizationCode;

    /**
     * 浙政钉组织名称
     */
    private String organizationName;

    /**
     * 用户角色
     */
    private List<Role> userRoleList;


    /**
     * 获取用户权限
     *
     * @return
     */
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        if (CollectionUtil.isNotEmpty(this.userRoleList)) {
            List<SimpleGrantedAuthority> authorities = new ArrayList<>();
            for (Role role : this.userRoleList) {
                authorities.add(new SimpleGrantedAuthority(role.getCode()));
            }
            return authorities;
        }
        return new ArrayList<>();
    }

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

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

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

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

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

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

}
