clqms-be/app/Models/Rule/RuleDefModel.php

62 lines
1.5 KiB
PHP
Raw Normal View History

<?php
namespace App\Models\Rule;
use App\Models\BaseModel;
class RuleDefModel extends BaseModel
{
protected $table = 'ruledef';
protected $primaryKey = 'RuleID';
protected $allowedFields = [
'Name',
'Description',
'EventCode',
'ScopeType',
'TestSiteID',
'ConditionExpr',
'Priority',
'Active',
'CreateDate',
'StartDate',
'EndDate',
];
protected $useTimestamps = true;
protected $createdField = 'CreateDate';
protected $updatedField = 'StartDate';
protected $useSoftDeletes = true;
protected $deletedField = 'EndDate';
/**
* Fetch active rules for an event, optionally scoped.
*
* Scope behavior:
* - Always returns GLOBAL rules
* - If $testSiteID provided, also returns TESTSITE rules matching TestSiteID
*/
public function getActiveByEvent(string $eventCode, ?int $testSiteID = null): array
{
$builder = $this->where('EventCode', $eventCode)
->where('EndDate IS NULL')
->where('Active', 1)
->groupStart()
->where('ScopeType', 'GLOBAL');
if ($testSiteID !== null) {
$builder->orGroupStart()
->where('ScopeType', 'TESTSITE')
->where('TestSiteID', $testSiteID)
->groupEnd();
}
$builder->groupEnd();
return $builder
->orderBy('Priority', 'ASC')
->orderBy('RuleID', 'ASC')
->findAll();
}
}