<?php

namespace Entity\Model;

use Helpers\QueryBuilder\QueryBuilder;

/**
 * Rupture de Requete d'EtatStat
 */
class ReqRuptures extends QueryBuilder
{
    protected string $table = 'REQ_RUPTURES with (nolock)';
    protected array $primKey = ['REQ_ETAT', 'REQ_ID', 'NUM_ORD'];
    protected string $libelle = 'TEXTE_RUP';

    /**
     * @param array<mixed> $entity
     * @return void
     */
    protected function setAutoincrementExpr(array &$entity) :void
    {
        if (! empty($entity['NUM_ORD'])) {
            return;
        }
        $entity['NUM_ORD'] = [
            'expr' =>
                "SELECT COALESCE( MAX(NUM_ORD) + 1, 1)
                FROM REQ_RUPTURES
                WHERE REQ_ETAT = :etat AND REQ_ID = :req",
            'param' => [
                ':etat' => $entity['REQ_ETAT'],
                ':req' => $entity['REQ_ID'],
            ],
        ];
    }
}
