package cn.fateverse.query.handler.adapter.impl;

import cn.fateverse.common.core.exception.CustomException;
import cn.fateverse.common.core.result.page.TableDataInfo;
import cn.fateverse.query.entity.DataAdapter;
import cn.fateverse.query.entity.UniQuery;
import cn.fateverse.query.entity.dto.SearchInfo;
import cn.fateverse.query.enums.DataAdapterSource;
import cn.fateverse.query.handler.adapter.DataAdapterHandler;
import cn.fateverse.query.handler.reader.EngineExecuteHandlerReader;
import cn.fateverse.query.mapper.UniQueryMapper;
import cn.fateverse.query.service.DynamicDataSearchService;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.TypeReference;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author Clay
 * @date 2023-10-31  21:04
 */
@Slf4j
@Service
public class CustomQueryDataAdapterHandler implements DataAdapterHandler {


    private final UniQueryMapper uniQueryMapper;

    private final EngineExecuteHandlerReader handlerReader;

    private final DynamicDataSearchService dynamicDataSearchService;

    public CustomQueryDataAdapterHandler(UniQueryMapper uniQueryMapper,
                                         EngineExecuteHandlerReader handlerReader,
                                         DynamicDataSearchService dynamicDataSearchService) {
        this.uniQueryMapper = uniQueryMapper;
        this.handlerReader = handlerReader;
        this.dynamicDataSearchService = dynamicDataSearchService;
    }


    @Override
    public Object mockExecute(DataAdapter dataAdapter, Object param) {
        if (!(dataAdapter.getSource() == DataAdapterSource.TOPO_QUERY || dataAdapter.getSource() == DataAdapterSource.SQL_QUERY)) {
            return null;
        }
        handlerReader.preconditioning(dataAdapter);
        if (null == param) {
            throw new CustomException("参数对象不能为空");
        }
        if (!(param instanceof SearchInfo)) {
            throw new CustomException("数据类型不匹配");
        }
        SearchInfo info = (SearchInfo) param;
        UniQuery query = uniQueryMapper.selectSampleById(dataAdapter.getDataSourceId());
        if (null == query) {
            throw new CustomException("数据源为空!");
        }
        //根据设置的参数动态调整当前是否需要分页操作
        TableDataInfo<Map<String, Object>> tableDataInfo = dynamicDataSearchService.searchData(info.getList(), query, null, Boolean.TRUE);
        return handlerReader.execute(dataAdapter, tableDataInfo.getRows(), true);
    }


    @Override
    public Object execute(DataAdapter dataAdapter, Object param) {
        if (!(dataAdapter.getSource() == DataAdapterSource.TOPO_QUERY || dataAdapter.getSource() == DataAdapterSource.SQL_QUERY)) {
            return null;
        }
        handlerReader.preconditioning(dataAdapter);
        List<Map<String, Object>> data = JSON.parseObject(dataAdapter.getMockData(), new TypeReference<List<Map<String, Object>>>() {
        });
        return handlerReader.execute(dataAdapter, data, false);
    }
}
