<?php
/**
 * @copyright (c) Kufer Software GmbH, Loisachstrasse 4, D-84453 Mühldorf
 */

class DBWorkTnListe3gKostenTraeger extends DBWorkGenerated
{
  public function __construct($dbnr, $new = false)
  {
    parent::__construct($dbnr, $new);

    $this->_dataModulFields = array('id', 'ksttraeger', 'erfasstam', 'erfasstvon', 'bearbdatum', 'bearbeiter', 'abfrage_typ');
    $this->select = implode(',', array_map(\FunctionOperations::prefixString('tn3gkt'), $this->_dataModulFields));
  }

  public function getNewObject()
  {
    return new \TNListe3gKostenTraeger();
  }

  public function getTableName(): string
  {
    return 'tnliste_3g_ksttraeger';
  }

  public function getAllQuery(): \SelectQueryBuilder
  {
    return $this->getSelectQueryBuilder()
      ->select($this->select)
      ->from($this->getTableName(), 'tn3gkt')
      ->paramRaw('table', 'tn3gkt');
  }

  public function findByFilterQuery(\Kufer\Web\Classes\Dbfilter\TNListe3gKostenTraegerSQLFilter $filter): \SelectQueryBuilder
  {
    return $this->getAllQuery()
      ->and($filter->createSql($this->dboperation));
  }

  public function findByFilter(\Kufer\Web\Classes\Dbfilter\TNListe3gKostenTraegerSQLFilter $filter): array
  {
    $builder = $this->findByFilterQuery($filter);

    $this->dboperation->clear();
    $this->dboperation->query($builder->getSQL());

    return $this->getAllRows();
  }

  public function findKostenTraegerByFilter(\Kufer\Web\Classes\Dbfilter\TNListe3gKostenTraegerSQLFilter $filter): array
  {
    $builder = $this->findByFilterQuery($filter)
      ->select(':table:.ksttraeger');

    $this->dboperation->clear();
    $this->dboperation->query($builder->getSQL());

    return $this->getAllRowsSingleColumn('ksttraeger');
  }

  public function countByFilter(\Kufer\Web\Classes\Dbfilter\TNListe3gKostenTraegerSQLFilter $filter): int
  {
    $builder = $this->findByFilterQuery($filter)
      ->select('COUNT(*) as anzahl');

    $this->dboperation->clear();
    $this->dboperation->query($builder->getSQLForInnerQuery());

    $this->dboperation->selectNextRow();

    return $this->getValue('anzahl', \DatamodulVarType::TYPE_INT);
  }

  public function existsByFilter(\Kufer\Web\Classes\Dbfilter\TNListe3gKostenTraegerSQLFilter $filter): bool
  {
    return $this->countByFilter($filter) > 0;
  }
}