<?php
/**
 * @copyright (c) Kufer Software GmbH, Oderstraße 7, D-84453 Mühldorf
 */

namespace Kufer\Web\Classes\Db;

use Kufer\Web\Classes\Datamodul\AktuellesFreigabe;


class DBWorkAktuellesFreigabe extends \DBWorkGenerated
{
  public function __construct($pDBnr = 1, $pNewInstance = false)
  {
    parent::__construct($pDBnr, $pNewInstance);

    $this->_dataModulFields = array('id', 'wa_id', 'person_typ', 'person_id', 'person_guid', 'schreiben', 'erstelltam');
    $this->select = implode(', ', $this->_dataModulFields);
  }

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

  public function canupdate($varname)
  {
    switch ($varname) {
      default:
        return parent::canupdate($varname);
    }
  }

  
  public function getNewObject()
  {
    return new AktuellesFreigabe();
  }

  public function insert(AktuellesFreigabe $new): int
  {
    $inserter = new \DBInserterOhneGenTable(
      $this,
      $this->getTableName(),
      array_filter($this->_dataModulFields, \FunctionOperations::not(static function(string $str) use ($new): bool {
        if (is_null($new->person_guid) && $str === 'person_guid') {
          return false;
        }

        return $str === 'id';
      }))
    );

    return $inserter->insert($new);
  }

  
  public function update(AktuellesFreigabe $org, AktuellesFreigabe $new): void
  {
    $this->updateObject($new, $org, $this->getTableName(), 'id', 'U', true);
    $this->setActionIDs();
  }

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

  public function findByFilterQuery(\Kufer\Web\Classes\Dbfilter\AktuellesFreigabeSQLFilter $filter): \SelectQueryBuilder
  {
    $condition = $filter->createSql($this->dboperation);

    return $this->getAllQuery()
      ->and($condition)
      ->orderBySrcCode($this->getCompleteOrderByDefault('erstelldatum', $this->getTableName()));
  }

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

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

    return $this->getAllRows();
  }

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

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

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

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

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

    $this->dboperation->selectrow(0);

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

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