package com.ningdatech.pmapi.user.security.auth.model;

import cn.hutool.core.collection.CollUtil;
import com.ningdatech.pmapi.sys.model.entity.Role;
import com.ningdatech.pmapi.user.constant.UserAvailableEnum;
import com.ningdatech.pmapi.user.entity.enumeration.RoleEnum;
import lombok.Data;

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

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

@Data
public class UserFullInfoDTO {

    private Long userId;

    private String realName;

    private String username;

    private String identifier;

    private String credential;

    /**
     * 所属区域编码
     */
    private String regionCode;
    /**
     * 所属区域编码
     */
    private Integer regionLevel;

    /**
     * 浙政钉扫码 员工code
     */
    private String employeeCode;

    /**
     * 浙政钉组织code
     */
    private String organizationCode;

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

    /**
     * 浙政钉组织名称
     */
    private String organizationName;

    /**
     * 任职所在单位code
     */
    private String empPosUnitCode;

    /**
     * 任职所在单位
     */
    private String empPosUnitName;

    /**
     * 用户角色
     */
    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() {
        if (CollUtil.isNotEmpty(this.userRoleList)) {
            for (Role role : this.userRoleList) {
                RoleEnum roleEnum = RoleEnum.mathByName(role.getCode());
                if (Objects.nonNull(roleEnum) && roleEnum.eq(RoleEnum.COMPANY_MANAGER.name())) {
                    return Boolean.TRUE;
                }
            }
        }
        return Boolean.FALSE;
    }
}
