package cn.reghao.oss.console.account.model.po;

import cn.reghao.oss.console.util.db.BaseEntity;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.*;
import org.hibernate.annotations.Proxy;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import javax.persistence.*;
import javax.validation.constraints.NotBlank;
import java.time.LocalDateTime;
import java.util.Collection;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 用户
 *
 * @author reghao
 * @date 2019/03/14 19:12:48
 */
@NoArgsConstructor
@Getter
@Setter
@Proxy(lazy = false)
@Table(name = "sys_user")
@Entity
public class User extends BaseEntity implements UserDetails {
    // 用户名和密码
    @Column(nullable = false, unique = true)
    @NotBlank(message = "用户名不能为空白字符串")
    private String username;
    @Column(nullable = false)
    @JsonIgnore
    @NotBlank(message = "密码不能为空白字符串")
    private String encodedPassword;
    @Column(nullable = false)
    private String salt;
    private LocalDateTime createAt;

    @ElementCollection(fetch = FetchType.EAGER)
    @CollectionTable(name = "sys_user_role")
    private Set<String> role;
    private Boolean enabled = true;
    private Boolean locked = false;

    @NotBlank(message = "用户名不能为空白字符串")
    private String screenName;
    private String avatarUrl;
    private String mobile;
    private String email;
    private Integer gender;

    public User(String username, String encodedPassword, String salt, Set<String> roles) {
        this.username = username;
        this.encodedPassword = encodedPassword;
        this.salt = salt;
        this.createAt = LocalDateTime.now();
        this.role = roles;
        this.enabled = true;
        this.locked = false;
        this.screenName = username;
        this.gender = 3;
        this.avatarUrl = "/imgs/avatar.jpg";
    }

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

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

    /**
     * 帐号是否未过期
     *
     * @param
     * @return
     * @date 2019-05-29 下午2:05
     */
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    /**
     * 密码是否未过期
     *
     * @param
     * @return
     * @date 2019-05-29 下午2:05
     */
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    /**
     * 帐号是否可用
     *
     * @param
     * @return
     * @date 2019-05-29 下午2:05
     */
    @Override
    public boolean isEnabled() {
        return enabled;
    }

    /**
     * 帐号是否锁定
     *
     * @param
     * @return
     * @date 2019-05-29 下午2:05
     */
    @Override
    public boolean isAccountNonLocked() {
        return locked;
    }

    /**
     * 权限列表
     *
     * @param
     * @return
     * @date 2019-05-29 下午2:22
     */
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return role.stream().map(UserAuthority::new).collect(Collectors.toSet());
    }
}
