package com.ningdatech.pmapi.datascope.utils;

import com.ningdatech.pmapi.datascope.model.DataScopeDTO;
import com.ningdatech.pmapi.datascope.provider.DataScopeContext;
import com.ningdatech.pmapi.user.security.auth.model.UserFullInfoDTO;
import com.ningdatech.pmapi.user.security.auth.model.UserInfoDetails;
import com.ningdatech.pmapi.user.util.LoginUserUtil;

import java.util.Objects;
import java.util.Optional;

/**
 * @Classname DataScopeUtil
 * @Description
 * @Date 2023/3/28 16:00
 * @Author PoffyZhang
 */
public class DataScopeUtil {

    public static Optional<DataScopeDTO> getCurrentUserDataScope(UserFullInfoDTO user) {
        if (Objects.isNull(user) || Objects.isNull(user.getRoleCode())) {
            return Optional.empty();
        }
        return DataScopeContext.getDataScope(user.getRoleCode().name());
    }

    public static Optional<DataScopeDTO> getCurrentUserDataScope() {
        UserInfoDetails loginUser = LoginUserUtil.loginUserDetail();
        if (Objects.isNull(loginUser) || Objects.isNull(loginUser.getRoleCode())) {
            return Optional.empty();
        }
        return DataScopeContext.getDataScope(loginUser.getRoleCode().name());
    }
}
