package com.ytpm.middle.view;

import cn.hutool.core.collection.CollUtil;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@Data
@NoArgsConstructor
@AllArgsConstructor
@ApiModel("当前登录用户信息")
public class MiddleUserInfo {
    @ApiModelProperty("用户ID")
    private String userId;
    @ApiModelProperty("部门ID")
    private Integer deptId;
    @ApiModelProperty("部门名称")
    private String deptName;
    @ApiModelProperty("用户昵称")
    private String nickName;
    @ApiModelProperty("头像")
    private String headImage;
    @ApiModelProperty("登录用户名")
    private String loginName;
    @ApiModelProperty("手机号")
    private String phone;
    @ApiModelProperty("最新登录时间")
    private Date lastLoginTime;
    @ApiModelProperty("账户状态")
    private Integer accountStatus;
    @ApiModelProperty(value = "是否超级管理员", hidden = true)
    private Integer superAdmin;
    @ApiModelProperty("角色列表")
    private List<MiddleRoleVO> roleList;
    @ApiModelProperty("权限列表")
    private List<MiddlePermissionVO> permissionList;

    public Collection<? extends GrantedAuthority> getAuthorities() {
        List<GrantedAuthority> authorities = new ArrayList<>();
        if(CollUtil.isEmpty(roleList)) {
            return authorities;
        }
        for (MiddleRoleVO role : roleList) {
            if(Objects.nonNull(role)){
                authorities.add(new SimpleGrantedAuthority(role.getRoleName()));
            }
        }
        return authorities;
    }
}
