package com.ningdatech.carapi.datascope.provider;

import com.ningdatech.basic.util.SpringUtils;
import com.ningdatech.carapi.datascope.model.DataScopeDto;
import lombok.RequiredArgsConstructor;

import java.util.Map;
import java.util.Optional;

/**
 * <p>
 * 数据权限获取
 * </p>
 *
 * @author WendyYang
 * @since 2022/1/9 23:28
 */
@RequiredArgsConstructor
public class DataScopeContext {

    private static final String WARN_MSG = "请先创建数据权限[%s]的实现类，使其实现 DataScopeProvider";

    private static final Map<String, DataScopeProvider> DSP_MAP;

    static {
        DSP_MAP = SpringUtils.getBeansOfType(DataScopeProvider.class);
    }

    public static Optional<DataScopeDto> getDataScope(int type) {
        String key = "DATA_SCOPE_" + type;
        DataScopeProvider dataScopeProvider = DSP_MAP.get(key);
        if (dataScopeProvider == null) {
            throw new IllegalArgumentException(String.format(WARN_MSG, key));
        }
        return dataScopeProvider.findDataFieldProperty();
    }

}
