package cn.fateverse.query.handler.reader;


import cn.fateverse.common.core.exception.CustomException;
import cn.fateverse.query.entity.dto.MockParam;
import cn.fateverse.query.entity.bo.PortalBo;
import cn.fateverse.query.handler.adapter.DataAdapterHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 数据适配器的执行器
 *
 * @author Clay
 * @date 2023-10-31  20:55
 */
@Slf4j
@Component
public class DataAdapterHandlerReader {

    private final List<DataAdapterHandler> handlerList;


    public DataAdapterHandlerReader(List<DataAdapterHandler> handlerList) {
        this.handlerList = handlerList;
    }


    public Object mockExecute(PortalBo portal, MockParam mockParam) {
        for (DataAdapterHandler dataAdapterHandler : handlerList) {
            Object result = dataAdapterHandler.mockExecute(portal, mockParam);
            if (result != null) {
                return result;
            }
        }
        throw new CustomException("当前数据源类型不支持!");
    }


    public Object execute(PortalBo portal, HttpServletRequest request) {
        for (DataAdapterHandler dataAdapterHandler : handlerList) {
            Object result = dataAdapterHandler.execute(portal, request);
            if (result != null) {
                return result;
            }
        }
        throw new CustomException("当前数据源类型不支持!");
    }

    public Boolean remove(Long adaptorId) {
        for (DataAdapterHandler dataAdapterHandler : handlerList) {
            boolean result = dataAdapterHandler.remove(adaptorId);
            if (result) {
                return true;
            }
        }
        return false;
    }


}
