package com.cm.common.pojo.bos;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.User;

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

/**
 * @ClassName: UserBO
 * @Description: 用户
 * @Author: WangGeng
 * @Date: 2019/2/20 2:22 PM
 * @Version: 1.0
 **/
public class UserBO extends User {

    private String userId;
    private String userName;
    private String userPhone;
    private String userAvatar;
    private String userEmail;
    private String userUKey;
    private Integer loginType;
    private String dataAuthority;
    private List<String> baseDepartmentIds;
    private List<String> dataAuthorityUserIds;
    private List<RoleBO> roles;
    private List<GroupBO> groups;
    private List<DepartmentBO> departments;
    private List<PositionBO> positions;
    private String roleIdAndNamesValue;
    private String groupIdAndNamesValue;
    private String departmentIdAndNamesValue;
    private String positionIdAndNamesValue;

    public UserBO() {
        super("", "", null);
    }

    public UserBO(String username, String password, Collection<? extends GrantedAuthority> authorities) {
        super(username, password, authorities);
    }

    public UserBO(String username, String password, boolean enabled, boolean accountNonExpired, boolean credentialsNonExpired, boolean accountNonLocked, Collection<? extends GrantedAuthority> authorities) {
        super(username, password, enabled, accountNonExpired, credentialsNonExpired, accountNonLocked, authorities);
    }

    public String getUserId() {
        return userId == null ? "" : userId.trim();
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String getUserName() {
        return userName == null ? "" : userName.trim();
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getUserPhone() {
        return userPhone == null ? "" : userPhone.trim();
    }

    public void setUserPhone(String userPhone) {
        this.userPhone = userPhone;
    }

    public String getUserAvatar() {
        return userAvatar == null ? "" : userAvatar;
    }

    public void setUserAvatar(String userAvatar) {
        this.userAvatar = userAvatar;
    }

    public String getUserEmail() {
        return userEmail == null ? "" : userEmail;
    }

    public void setUserEmail(String userEmail) {
        this.userEmail = userEmail;
    }

    public String getUserUKey() {
        return userUKey == null ? "" : userUKey.trim();
    }

    public void setUserUKey(String userUKey) {
        this.userUKey = userUKey;
    }

    public Integer getLoginType() {
        return loginType;
    }

    public void setLoginType(Integer loginType) {
        this.loginType = loginType;
    }

    public List<String> getBaseDepartmentIds() {
        return baseDepartmentIds == null ? new ArrayList<>(0) : baseDepartmentIds;
    }

    public void setBaseDepartmentIds(List<String> baseDepartmentIds) {
        this.baseDepartmentIds = baseDepartmentIds;
    }

    public String getDataAuthority() {
        return dataAuthority == null ? "" : dataAuthority.trim();
    }

    public void setDataAuthority(String dataAuthority) {
        this.dataAuthority = dataAuthority;
    }

    public List<String> getDataAuthorityUserIds() {
        return dataAuthorityUserIds == null ? new ArrayList<>(0) : dataAuthorityUserIds;
    }

    public void setDataAuthorityUserIds(List<String> dataAuthorityUserIds) {
        this.dataAuthorityUserIds = dataAuthorityUserIds;
    }

    public List<RoleBO> getRoles() {
        return roles == null ? new ArrayList<>(0) : roles;
    }

    public void setRoles(List<RoleBO> roles) {
        this.roles = roles;
        if (roles == null || roles.isEmpty()) {
            return;
        }
        StringBuilder idSB = new StringBuilder();
        StringBuilder nameSB = new StringBuilder();
        roles.forEach(roleBO -> {
            if (idSB.length() > 0) {
                idSB.append(",");
                nameSB.append(",");
            }
            idSB.append(roleBO.getRoleId());
            nameSB.append(roleBO.getRoleName());
        });
        this.roleIdAndNamesValue = idSB.append("|").append(nameSB).toString();
    }

    public List<GroupBO> getGroups() {
        return groups == null ? new ArrayList<>(0) : groups;
    }

    public void setGroups(List<GroupBO> groups) {
        this.groups = groups;
        if (groups == null || groups.isEmpty()) {
            return;
        }
        StringBuilder idSB = new StringBuilder();
        StringBuilder nameSB = new StringBuilder();
        groups.forEach(groupBO -> {
            if (idSB.length() > 0) {
                idSB.append(",");
                nameSB.append(",");
            }
            idSB.append(groupBO.getGroupId());
            nameSB.append(groupBO.getGroupName());
        });
        this.groupIdAndNamesValue = idSB.append("|").append(nameSB).toString();
    }

    public List<DepartmentBO> getDepartments() {
        return departments == null ? new ArrayList<>(0) : departments;
    }

    public void setDepartments(List<DepartmentBO> departments) {
        this.departments = departments;
        if (departments == null || departments.isEmpty()) {
            return;
        }
        StringBuilder idSB = new StringBuilder();
        StringBuilder nameSB = new StringBuilder();
        departments.forEach(departmentBO -> {
            if (idSB.length() > 0) {
                idSB.append(",");
                nameSB.append(",");
            }
            idSB.append(departmentBO.getDepartmentId());
            nameSB.append(departmentBO.getDepartmentName());
        });
        this.departmentIdAndNamesValue = idSB.append("|").append(nameSB).toString();
    }

    public List<PositionBO> getPositions() {
        return positions == null ? new ArrayList<>(0) : positions;
    }

    public void setPositions(List<PositionBO> positions) {
        this.positions = positions;
        if (positions == null || positions.isEmpty()) {
            return;
        }
        StringBuilder idSB = new StringBuilder();
        StringBuilder nameSB = new StringBuilder();
        positions.forEach(positionBO -> {
            if (idSB.length() > 0) {
                idSB.append(",");
                nameSB.append(",");
            }
            idSB.append(positionBO.getPositionId());
            nameSB.append(positionBO.getPositionName());
        });
        this.positionIdAndNamesValue = idSB.append("|").append(nameSB).toString();
    }

    public String getRoleIdAndNamesValue() {
        return roleIdAndNamesValue == null ? "" : roleIdAndNamesValue.trim();
    }

    public String getGroupIdAndNamesValue() {
        return groupIdAndNamesValue == null ? "" : groupIdAndNamesValue.trim();
    }

    public String getDepartmentIdAndNamesValue() {
        return departmentIdAndNamesValue == null ? "" : departmentIdAndNamesValue.trim();
    }

    public String getPositionIdAndNamesValue() {
        return positionIdAndNamesValue == null ? "" : positionIdAndNamesValue.trim();
    }

    @Override
    public String toString() {
        return "UserBO{" +
                "userId='" + userId + '\'' +
                ", userName='" + userName + '\'' +
                ", userPhone='" + userPhone + '\'' +
                ", userAvatar='" + userAvatar + '\'' +
                ", userEmail='" + userEmail + '\'' +
                ", userUKey='" + userUKey + '\'' +
                ", loginType=" + loginType +
                ", dataAuthority='" + dataAuthority + '\'' +
                ", baseDepartmentIds=" + baseDepartmentIds +
                ", dataAuthorityUserIds=" + dataAuthorityUserIds +
                ", roles=" + roles +
                ", groups=" + groups +
                ", departments=" + departments +
                ", positions=" + positions +
                ", roleIdAndNamesValue='" + roleIdAndNamesValue + '\'' +
                ", groupIdAndNamesValue='" + groupIdAndNamesValue + '\'' +
                ", departmentIdAndNamesValue='" + departmentIdAndNamesValue + '\'' +
                ", positionIdAndNamesValue='" + positionIdAndNamesValue + '\'' +
                '}';
    }
}
