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.dto.MockParam;
import cn.fateverse.query.entity.PortalMapping;
import cn.fateverse.query.entity.UniQuery;
import cn.fateverse.query.entity.bo.PortalBo;
import cn.fateverse.query.entity.dto.UniConDto;
import cn.fateverse.query.enums.PortalEnum;
import cn.fateverse.query.handler.adapter.AbstractDataAdapterHandler;
import cn.fateverse.query.handler.reader.EngineExecuteHandlerReader;
import cn.fateverse.query.mapper.DataAdapterMapper;
import cn.fateverse.query.mapper.UniQueryMapper;
import cn.fateverse.query.service.DynamicDataSearchService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

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

/**
 * @author Clay
 * @date 2023-10-31  21:04
 */
@Slf4j
@Service
public class LocalDataAdapterHandler extends AbstractDataAdapterHandler {


    private final UniQueryMapper uniQueryMapper;
    private final DynamicDataSearchService dynamicDataSearchService;

    public LocalDataAdapterHandler(UniQueryMapper uniQueryMapper,
                                   DataAdapterMapper dataAdapterMapper,
                                   EngineExecuteHandlerReader handlerReader,
                                   DynamicDataSearchService dynamicDataSearchService) {
        super(dataAdapterMapper, handlerReader);
        this.uniQueryMapper = uniQueryMapper;
        this.dynamicDataSearchService = dynamicDataSearchService;
    }


    @Override
    public Object mockExecute(PortalBo portal, MockParam mockParam) {
        if (portal.getType() != PortalEnum.LOCAL) {
            return null;
        }
        //自定义查询编辑查询对象
        List<UniConDto> uniConList = new ArrayList<>();
        if (!ObjectUtils.isEmpty(mockParam.getParams())) {
            for (MockParam.Param param : mockParam.getParams()) {
                if (!ObjectUtils.isEmpty(param.getKey()) && !ObjectUtils.isEmpty(param.getValue())) {
                    UniConDto uniCon = new UniConDto();
                    uniCon.setQuery(param.getValue());
                    uniCon.setUcId(Long.parseLong(param.getKey()));
                }
            }
        }
        UniQuery query = uniQueryMapper.selectSampleById(portal.getQueryId());
        if (null == query) {
            throw new CustomException("数据源为空!");
        }
        //根据设置的参数动态调整当前是否需要分页操作
        TableDataInfo<Map<String, Object>> tableDataInfo = dynamicDataSearchService.searchData(uniConList, query, null, Boolean.TRUE);
        if (portal.getCreateDataAdapter()) {
            return super.mockExecute(portal.getAdapterId(), mockParam.getCode(), tableDataInfo.getRows(), true);
        } else {
            return tableDataInfo.getRows();
        }
    }


    @Override
    public Object execute(PortalBo portal, HttpServletRequest request) {
        if (portal.getType() != PortalEnum.LOCAL) {
            return null;
        }
        //自定义查询编辑查询对象
        List<UniConDto> uniConList = new ArrayList<>();
        //根据映射关系从request中获取请求参数
        for (PortalMapping portalMapping : portal.getMappings()) {
            UniConDto uniCon = new UniConDto();
            String mappingValue = portalMapping.getMappingValue();
            String mappingKey = portalMapping.getMappingKey();
            uniCon.setUcId(Long.parseLong(mappingValue));
            if (portalMapping.getMappingType() == 0) {
                uniCon.setQuery(request.getParameter(mappingKey));
            } else if (portalMapping.getMappingType() == 1) {
                uniCon.setQuery(request.getHeaders(mappingKey));
            } else {
                uniCon.setQuery(request.getParameter(mappingKey));
            }
            uniConList.add(uniCon);
        }
        UniQuery query = uniQueryMapper.selectSampleById(portal.getQueryId());
        if (null == query) {
            throw new CustomException("数据源为空!");
        }
        //根据设置的参数动态调整当前是否需要分页操作
        TableDataInfo<Map<String, Object>> tableDataInfo = dynamicDataSearchService.searchData(uniConList, query, null, Boolean.TRUE);
        if (portal.getCreateDataAdapter()) {
            return super.execute(portal.getAdapterId(), tableDataInfo.getRows(), false);
        } else {
            return tableDataInfo.getRows();
        }
    }


}
