package ru.micord.ervu.account_applications.dao;

import java.util.List;
import java.util.Set;

import org.jooq.DSLContext;
import org.jooq.Table;
import org.springframework.stereotype.Repository;
import ru.micord.ervu.account_applications.db_beans.public_.Tables;
import ru.micord.ervu.account_applications.db_beans.public_.tables.records.IpDirectoryRecord;

import static ru.micord.ervu.account_applications.db_beans.public_.Tables.IP_DIRECTORY;

/**
 * @author Adel Kalimullin
 */
@Repository
public class IpDirectoryDao extends AbstractDataDao<IpDirectoryRecord> {
  protected IpDirectoryDao(DSLContext dsl) {
    super(dsl);
  }

  public Set<String> getExistingIpAddresses(List<String> ips) {
    return getExistingValuesByField(Tables.IP_DIRECTORY.IP_ADDRESS, ips);
  }

  public void deleteByIpAddresses(List<String> ips) {
    deleteByFieldInValues(Tables.IP_DIRECTORY.IP_ADDRESS, ips);
  }

  public boolean exists(String ip) {
    return dsl.fetchExists(
        dsl.selectOne()
            .from(IP_DIRECTORY)
            .where(IP_DIRECTORY.IP_ADDRESS.eq(ip))
    );
  }
  @Override
  protected Table<IpDirectoryRecord> getTable() {
    return IP_DIRECTORY;
  }
}
