package ru.nbch.indicsearchservice.loader.subtable;

import java.io.IOException;
import java.nio.file.Path;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import org.apache.ibatis.cursor.Cursor;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import ru.nbch.indicsearchservice.util.IndicSearchUtils;
import ru.nbch.indicsearchservice.util.MapperHolder;

@Component
public class RuPaymtTableLoader implements RuUidSubTableLoader {
    private final MapperHolder mapperHolder;
    private final IndicSearchUtils indicSearchUtils;

    public RuPaymtTableLoader(MapperHolder mapperHolder,
                              IndicSearchUtils indicSearchUtils) {
        this.mapperHolder = mapperHolder;
        this.indicSearchUtils = indicSearchUtils;
    }

    @Override
    public String getTableName() {
        return "RU_PAYMT";
    }

    @Override
    @Transactional(readOnly = true)
    public void searchByRuUidFid(UUID taskId, Set<Integer> fids, Map<String, Path> tableFileMap) {
        try (Cursor<LinkedHashMap<String, Object>> cursor = mapperHolder.getIndicMapper().findAllByFidsFromRuPaymt(fids)) {
            tableFileMap.put(getTableName(), indicSearchUtils.writeRowsToFile(taskId, getTableName(), cursor));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
