package ru.micord.ervu.account_applications.component.dao;

import java.sql.Timestamp;
import java.util.List;

import org.jooq.DSLContext;
import org.springframework.stereotype.Repository;

import static ru.micord.ervu.account_applications.db_beans.public_.tables.UserApplicationList.USER_APPLICATION_LIST;
import static ru.micord.ervu.account_applications.db_beans.public_.tables.UserApplicationListAudit.USER_APPLICATION_LIST_AUDIT;

/**
 * @author Eduard Tihomirov
 */
@Repository
public class AuditDao {

  private final DSLContext dslContext;

  public AuditDao(DSLContext dslContext) {
    this.dslContext = dslContext;
  }

  public void insert(Long listId, String fio, String login, String status, Timestamp timestamp) {
    dslContext.insertInto(USER_APPLICATION_LIST_AUDIT)
        .set(USER_APPLICATION_LIST_AUDIT.USER_APPLICATION_LIST_ID, listId)
        .set(USER_APPLICATION_LIST_AUDIT.FIO, fio)
        .set(USER_APPLICATION_LIST_AUDIT.STATUS, status)
        .set(USER_APPLICATION_LIST_AUDIT.DATE, timestamp)
        .set(USER_APPLICATION_LIST_AUDIT.LOGIN, login)
        .execute();
  }

  public List<Long> selectAppListIdsByAppNumber(long appNumber) {
    return dslContext.select(USER_APPLICATION_LIST.USER_APPLICATION_LIST_ID)
        .from(USER_APPLICATION_LIST)
        .where(USER_APPLICATION_LIST.NUMBER_APP.eq(appNumber))
        .fetch(USER_APPLICATION_LIST.USER_APPLICATION_LIST_ID);
  }
}
