Initial commit
This commit is contained in:
159
app/Jobs/ApplyLogOverridesJob.php
Normal file
159
app/Jobs/ApplyLogOverridesJob.php
Normal file
@@ -0,0 +1,159 @@
|
||||
<?php
|
||||
|
||||
namespace App\Jobs;
|
||||
|
||||
use App\Models\EvaluationRun;
|
||||
use App\Models\Log;
|
||||
use App\Models\LogOverride;
|
||||
use App\Models\LogResult;
|
||||
use App\Services\Evaluation\EvaluationCoordinator;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Foundation\Queue\Queueable;
|
||||
use Throwable;
|
||||
|
||||
/**
|
||||
* Job: ApplyLogOverridesJob
|
||||
*
|
||||
* Použije ruční override nad log_results po agregaci, aby se do pořadí
|
||||
* promítl rozhodčí stav/kategorie/power, ale agregované skóre zůstalo zachováno.
|
||||
*/
|
||||
class ApplyLogOverridesJob implements ShouldQueue
|
||||
{
|
||||
use Queueable;
|
||||
|
||||
public int $tries = 2;
|
||||
public array $backoff = [60];
|
||||
|
||||
public function __construct(
|
||||
protected int $evaluationRunId
|
||||
) {
|
||||
}
|
||||
|
||||
public function handle(): void
|
||||
{
|
||||
$run = EvaluationRun::find($this->evaluationRunId);
|
||||
if (! $run || $run->isCanceled()) {
|
||||
return;
|
||||
}
|
||||
$coordinator = new EvaluationCoordinator();
|
||||
|
||||
try {
|
||||
$coordinator->eventInfo($run, 'Apply log overrides: krok spuštěn.', [
|
||||
'step' => 'apply_log_overrides',
|
||||
'round_id' => $run->round_id,
|
||||
]);
|
||||
$overrides = LogOverride::where('evaluation_run_id', $this->evaluationRunId)->get();
|
||||
if ($overrides->isEmpty()) {
|
||||
$coordinator->eventInfo($run, 'Apply log overrides: nic ke zpracování.', [
|
||||
'step' => 'apply_log_overrides',
|
||||
'round_id' => $run->round_id,
|
||||
]);
|
||||
$coordinator->eventInfo($run, 'Apply log overrides: krok dokončen.', [
|
||||
'step' => 'apply_log_overrides',
|
||||
'round_id' => $run->round_id,
|
||||
]);
|
||||
return;
|
||||
}
|
||||
|
||||
foreach ($overrides as $override) {
|
||||
if (EvaluationRun::isCanceledRun($run->id)) {
|
||||
return;
|
||||
}
|
||||
$data = [];
|
||||
|
||||
if ($override->forced_log_status && $override->forced_log_status !== 'AUTO') {
|
||||
$data['status'] = $override->forced_log_status;
|
||||
}
|
||||
|
||||
if ($override->forced_band_id !== null) {
|
||||
$data['band_id'] = $override->forced_band_id;
|
||||
}
|
||||
|
||||
if ($override->forced_category_id !== null) {
|
||||
$data['category_id'] = $override->forced_category_id;
|
||||
}
|
||||
|
||||
if ($override->forced_power_category_id !== null) {
|
||||
$data['power_category_id'] = $override->forced_power_category_id;
|
||||
}
|
||||
|
||||
if ($override->forced_sixhr_category !== null) {
|
||||
$data['sixhr_category'] = $override->forced_sixhr_category;
|
||||
}
|
||||
|
||||
if (! $data) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$logResult = LogResult::where('evaluation_run_id', $this->evaluationRunId)
|
||||
->where('log_id', $override->log_id)
|
||||
->first();
|
||||
if (! $logResult) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$bandId = $data['band_id'] ?? $logResult->band_id;
|
||||
$sixhrCategory = $data['sixhr_category'] ?? $logResult->sixhr_category;
|
||||
if ($sixhrCategory && ! $this->isSixHourBand($bandId)) {
|
||||
$this->addSixHourRemark($override->log_id);
|
||||
}
|
||||
|
||||
$logResult->update($data);
|
||||
}
|
||||
|
||||
$coordinator->eventInfo($run, 'Apply log overrides: krok dokončen.', [
|
||||
'step' => 'apply_log_overrides',
|
||||
'round_id' => $run->round_id,
|
||||
]);
|
||||
} catch (Throwable $e) {
|
||||
$coordinator->eventError($run, 'Apply log overrides: krok selhal.', [
|
||||
'step' => 'apply_log_overrides',
|
||||
'round_id' => $run->round_id,
|
||||
'error' => $e->getMessage(),
|
||||
]);
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
|
||||
protected function isSixHourBand(?int $bandId): bool
|
||||
{
|
||||
if (! $bandId) {
|
||||
return false;
|
||||
}
|
||||
return in_array($bandId, [1, 2], true);
|
||||
}
|
||||
|
||||
protected function addSixHourRemark(int $logId): void
|
||||
{
|
||||
$log = Log::find($logId);
|
||||
if (! $log) {
|
||||
return;
|
||||
}
|
||||
$remarksEval = $this->decodeRemarksEval($log->remarks_eval);
|
||||
$message = '6H kategorie je povolena jen pro pásma 145 MHz a 435 MHz.';
|
||||
if (! in_array($message, $remarksEval, true)) {
|
||||
$remarksEval[] = $message;
|
||||
$log->remarks_eval = $this->encodeRemarksEval($remarksEval);
|
||||
$log->save();
|
||||
}
|
||||
}
|
||||
|
||||
protected function decodeRemarksEval(?string $value): array
|
||||
{
|
||||
if (! $value) {
|
||||
return [];
|
||||
}
|
||||
$decoded = json_decode($value, true);
|
||||
return is_array($decoded) ? $decoded : [];
|
||||
}
|
||||
|
||||
protected function encodeRemarksEval(array $value): ?string
|
||||
{
|
||||
$filtered = array_values(array_filter($value, fn ($item) => is_string($item) && trim($item) !== ''));
|
||||
$filtered = array_values(array_unique($filtered));
|
||||
if (count($filtered) === 0) {
|
||||
return null;
|
||||
}
|
||||
return json_encode($filtered, JSON_UNESCAPED_UNICODE);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user