package com.hz.pm.api.user.security.model;

import cn.hutool.core.collection.CollUtil;
import com.hz.pm.api.sys.model.entity.Role;
import com.hz.pm.api.user.model.enumeration.UserAvailableEnum;
import com.hz.pm.api.user.model.enumeration.RoleEnum;
import lombok.Data;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author liuxinxin
 * @since 2023/1/4 下午5:07
 */

@Data
public class UserFullInfoDTO {

    private Long userId;

    private String mhUnitName;

    private Long mhUnitId;

    private String mhUserId;

    private String realName;

    private String username;

    private String identifier;

    private String credential;

    /**
     * 手机号码
     */
    private String mobile;

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

    private Long accountId;

    /**
     * 用户是否可用
     */
    private UserAvailableEnum available;


    /**
     * 取最高的权限
     *
     * @return \
     */
    public RoleEnum getRoleCode() {
        if (CollUtil.isNotEmpty(this.userRoleList)) {
            return RoleEnum.checkHigherRole(this.userRoleList.stream()
                    .map(Role::getCode).collect(Collectors.toList()));
        }
        return null;
    }

    public boolean getIsOrgAdmin() {
        return containsRole(RoleEnum.COMPANY_MANAGER);
    }

    public boolean getSuperAdmin() {
        return containsRole(RoleEnum.SUPER_ADMIN);
    }

    public boolean getRegionAdmin() {
        return containsRole(RoleEnum.REGION_MANAGER);
    }

    public Boolean notAdmin() {
        return !getSuperAdmin() && !getRegionAdmin() && !getIsOrgAdmin();
    }

    public String getMhUnitIdStr() {
        return String.valueOf(getMhUnitId());
    }

    public String getUserIdStr() {
        return String.valueOf(getUserId());
    }

    private boolean containsRole(RoleEnum targetRole) {
        if (CollUtil.isNotEmpty(this.userRoleList)) {
            for (Role role : this.userRoleList) {
                RoleEnum roleEnum = RoleEnum.mathByName(role.getCode());
                if (Objects.nonNull(roleEnum) && roleEnum.eq(targetRole.name())) {
                    return Boolean.TRUE;
                }
            }
        }
        return Boolean.FALSE;
    }

}
