package com.ningdatech.pmapi.datascope.provider;

import com.ningdatech.basic.util.SpringUtils;
import com.ningdatech.pmapi.datascope.model.DataScopeDTO;
import lombok.RequiredArgsConstructor;

import java.io.Serializable;
import java.util.Map;
import java.util.Optional;

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

    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(String roleCode) {
        DataScopeProvider dataScopeProvider = DSP_MAP.get(roleCode);
        if (dataScopeProvider == null) {
            throw new IllegalArgumentException(String.format(WARN_MSG, roleCode));
        }
        return dataScopeProvider.findDataFieldProperty();
    }

}
