package cn.fateverse.query.service.impl;

import cn.fateverse.common.decrypt.entity.EncryptOption;
import cn.fateverse.query.entity.DataAdapter;
import cn.fateverse.query.entity.dto.DataAdapterDto;
import cn.fateverse.query.entity.dto.SearchInfo;
import cn.fateverse.query.entity.vo.DataAdapterVo;
import cn.fateverse.query.entity.query.DataAdapterQuery;
import cn.fateverse.query.handler.reader.DataAdapterHandlerReader;
import cn.fateverse.query.mapper.DataAdapterMapper;
import cn.fateverse.query.service.DataAdapterService;
import cn.fateverse.common.core.entity.Option;
import cn.fateverse.common.core.result.page.TableDataInfo;
import cn.fateverse.common.mybatis.utils.PageUtils;
import cn.fateverse.common.security.utils.SecurityUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 数据源适配器 Controller
 *
 * @author clay
 * @date 2023-10-29
 */
@Slf4j
@Service
public class DataAdapterServiceImpl implements DataAdapterService {

    private final DataAdapterMapper dataAdapterMapper;

    private final DataAdapterHandlerReader handlerReader;

    public DataAdapterServiceImpl(DataAdapterMapper dataAdapterMapper,
                                  DataAdapterHandlerReader handlerReader) {
        this.dataAdapterMapper = dataAdapterMapper;
        this.handlerReader = handlerReader;
    }

    @Override
    public DataAdapterVo searchById(Long adapterId) {
        DataAdapter dataAdapter = dataAdapterMapper.selectById(adapterId);
        return DataAdapterVo.toDataAdapterVo(dataAdapter);
    }

    @Override
    public TableDataInfo<DataAdapterVo> searchList(DataAdapterQuery query) {
        PageUtils.startPage();
        List<DataAdapter> list = dataAdapterMapper.selectList(query);
        return PageUtils.convertDataTable(list, DataAdapterVo::toDataAdapterVo);
    }

    @Override
    public List<EncryptOption> searchOptionList() {
        DataAdapterQuery query = new DataAdapterQuery();
        List<DataAdapter> list = dataAdapterMapper.selectList(query);
        return list.stream().map(item -> EncryptOption.builder()
                .value(String.valueOf(item.getAdapterId()))
                .label(item.getAdapterName())
                .build()).collect(Collectors.toList());
    }

    @Override
    public List<DataAdapterVo> exportList(DataAdapterQuery query) {
        List<DataAdapter> list = dataAdapterMapper.selectList(query);
        return list.stream().map(DataAdapterVo::toDataAdapterVo)
                .collect(Collectors.toList());
    }

    @Override
    public Object mockExecute(DataAdapterDto dto) {
        DataAdapter dataAdapter = dto.toDataAdapter();
        SearchInfo searchInfo = new SearchInfo();
        //后续可以添加对应的查询条件
        searchInfo.setList(new ArrayList<>());
        DataAdapter dbData = dataAdapterMapper.selectById(dto.getAdapterId());
//        dataAdapter.setDataSourceId(dbData.getDataSourceId());
//        return handlerReader.mockExecute(dataAdapter, searchInfo);
        return null;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int save(DataAdapterDto dataAdapter) {
        DataAdapter info = dataAdapter.toDataAdapter();
        info.setCreateBy(SecurityUtils.getUsername());
        return dataAdapterMapper.insert(info);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int edit(DataAdapterDto dataAdapter) {
        DataAdapter info = dataAdapter.toDataAdapter();
        info.setUpdateBy(SecurityUtils.getUsername());
        return dataAdapterMapper.update(info);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeDataAdapterById(Long adapterId) {
        return dataAdapterMapper.deleteById(adapterId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeBatch(List<Long> adapterIdList) {
        return dataAdapterMapper.deleteBatchByIdList(adapterIdList);
    }

}