package com.hz.pm.api.datascope.utils;

import com.hz.pm.api.datascope.model.DataScopeDTO;
import com.hz.pm.api.datascope.provider.DataScopeContext;
import com.hz.pm.api.user.security.model.UserFullInfoDTO;
import com.hz.pm.api.user.security.model.UserInfoDetails;
import com.hz.pm.api.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> getCurrentUserDataScopeHasUserId(UserFullInfoDTO user) {
        if (Objects.isNull(user) || Objects.isNull(user.getRoleCode())) {
            return Optional.empty();
        }
        return DataScopeContext.getDataScopeHasUserId(user.getRoleCode().name(),user.getUserId());
    }

    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());
    }
}
