package cn.fateverse.query.service.impl;

import cn.fateverse.common.core.entity.Option;
import cn.fateverse.query.entity.UniCon;
import cn.fateverse.query.entity.UniQuery;
import cn.fateverse.query.entity.query.UniQueryQuery;
import cn.fateverse.query.mapper.UniConMapper;
import cn.fateverse.query.mapper.UniQueryMapper;
import cn.fateverse.query.service.QueryToolService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

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

/**
 * @author Clay
 * @date 2024/4/16 16:18
 */
@Slf4j
@Service
public class QueryToolServiceImpl implements QueryToolService {

    private final UniConMapper conMapper;

    private final UniQueryMapper queryMapper;


    public QueryToolServiceImpl(UniConMapper conMapper,
                                UniQueryMapper queryMapper) {
        this.conMapper = conMapper;
        this.queryMapper = queryMapper;
    }


    @Override
    public List<Option> searchQueryOption(Integer type) {
        UniQueryQuery query = new UniQueryQuery();
        query.setType(type);
        query.setPreview(Boolean.TRUE);
        List<UniQuery> queryList = queryMapper.selectList(query);
        if (ObjectUtils.isEmpty(queryList)) {
            return new ArrayList<>();
        }
        return queryList.stream().map(uniQuery -> Option.builder()
                .value(uniQuery.getId())
                .label(uniQuery.getUqName())
                .build()).collect(Collectors.toList());
    }


    @Override
    public List<Option> searchQueryCon(Long queryId) {
        List<UniCon> uniCons = conMapper.selectByQueryId(queryId);
        if (ObjectUtils.isEmpty(uniCons)) {
            return new ArrayList<>();
        }
        return uniCons.stream()
                .map(uniCon -> Option.builder()
                        .value(uniCon.getUcId())
                        .label(uniCon.getUcName())
                        .build()).collect(Collectors.toList());
    }
}
