package ink.wgink.pojo.bos;

import ink.wgink.pojo.pos.DepartmentPO;
import ink.wgink.pojo.pos.GroupPO;
import ink.wgink.pojo.pos.PositionPO;
import ink.wgink.pojo.pos.RolePO;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.User;

import java.io.Serializable;
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 LoginUser 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 dataRight;
    private List<String> baseDepartmentIds;
    private List<String> dataRightUserIds;
    private List<RolePO> roles;
    private List<GroupPO> groups;
    private List<DepartmentPO> departments;
    private List<PositionPO> positions;
    private String roleIdAndNamesValue;
    private String groupIdAndNamesValue;
    private String departmentIdAndNamesValue;
    private String positionIdAndNamesValue;
    /**
     * 拓展数据，用于自定义登录用户所拥有的权限加载
     */
    private Object expandData;

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

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

    public LoginUser(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 getDataRight() {
        return dataRight == null ? "" : dataRight.trim();
    }

    public void setDataRight(String dataRight) {
        this.dataRight = dataRight;
    }

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

    public void setDataRightUserIds(List<String> dataRightUserIds) {
        this.dataRightUserIds = dataRightUserIds;
    }

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

    public void setRoles(List<RolePO> 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<GroupPO> getGroups() {
        return groups == null ? new ArrayList<>(0) : groups;
    }

    public void setGroups(List<GroupPO> 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<DepartmentPO> getDepartments() {
        return departments == null ? new ArrayList<>(0) : departments;
    }

    public void setDepartments(List<DepartmentPO> 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<PositionPO> getPositions() {
        return positions == null ? new ArrayList<>(0) : positions;
    }

    public void setPositions(List<PositionPO> 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();
    }

    public Object getExpandData() {
        return expandData;
    }

    public void setExpandData(Object expandData) {
        this.expandData = expandData;
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder("{");
        sb.append("\"userId\":\"")
                .append(userId).append('\"');
        sb.append(",\"userName\":\"")
                .append(userName).append('\"');
        sb.append(",\"userPhone\":\"")
                .append(userPhone).append('\"');
        sb.append(",\"userAvatar\":\"")
                .append(userAvatar).append('\"');
        sb.append(",\"userEmail\":\"")
                .append(userEmail).append('\"');
        sb.append(",\"userUKey\":\"")
                .append(userUKey).append('\"');
        sb.append(",\"loginType\":")
                .append(loginType);
        sb.append(",\"dataRight\":\"")
                .append(dataRight).append('\"');
        sb.append(",\"baseDepartmentIds\":")
                .append(baseDepartmentIds);
        sb.append(",\"dataRightUserIds\":")
                .append(dataRightUserIds);
        sb.append(",\"roles\":")
                .append(roles);
        sb.append(",\"groups\":")
                .append(groups);
        sb.append(",\"departments\":")
                .append(departments);
        sb.append(",\"positions\":")
                .append(positions);
        sb.append(",\"roleIdAndNamesValue\":\"")
                .append(roleIdAndNamesValue).append('\"');
        sb.append(",\"groupIdAndNamesValue\":\"")
                .append(groupIdAndNamesValue).append('\"');
        sb.append(",\"departmentIdAndNamesValue\":\"")
                .append(departmentIdAndNamesValue).append('\"');
        sb.append(",\"positionIdAndNamesValue\":\"")
                .append(positionIdAndNamesValue).append('\"');
        sb.append(",\"expandData\":")
                .append(expandData);
        sb.append('}');
        return sb.toString();
    }
}
