Files
citrus-cms/app/Console/Commands/NotificationCheckRepairLog.php
T
2026-04-28 21:14:25 +03:00

159 lines
5.5 KiB
PHP

<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
class NotificationCheckRepairLog extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'notifications:check-repair-log-new-joint';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Check repair logs for missing new joint numbers and send notifications';
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$startTime = microtime(true);
$commandName = $this->signature;
// Log command start
logNotificationStart($commandName);
$this->info('Checking repair logs for missing new joint numbers...');
$notificationCode = 'notification_repair_log_missing_new_joint';
try {
// Skip if checked recently (within 5 minutes)
if (!shouldCheckNotification($notificationCode)) {
$this->info('Skipping: Last notification sent less than 5 minutes ago.');
logNotificationSkip($commandName, 'Last notification sent less than 5 minutes ago');
return 0;
}
// Get last check time (null = first run, check all records)
$lastCheck = getLastCheckTimestamp($notificationCode);
// Build query
$query = DB::table('repair_logs')
->where(function($q) {
$q->whereNull('new_joint_no')
->orWhere('new_joint_no', '');
});
// If not first run, only check new records
if ($lastCheck !== null) {
$query->where('created_at', '>', $lastCheck);
// Get already notified IDs to prevent duplicates
$alreadyNotifiedIds = getAlreadyNotifiedIds($notificationCode);
if (!empty($alreadyNotifiedIds)) {
$query->whereNotIn('id', $alreadyNotifiedIds);
}
}
// Get IDs of records that need notification
$ids = $query->pluck('id')->toArray();
if (empty($ids)) {
$this->info('No new repair logs with missing new joint numbers found.');
$duration = microtime(true) - $startTime;
logNotificationComplete($commandName, 0, 0, 0, $duration);
return 0;
}
// Prepare filter parameters
$filterParams = [
'table' => 'repair_logs',
'conditions' => [
'or' => [
['new_joint_no' => null],
['new_joint_no' => '']
]
]
];
// If not first run, merge with existing IDs (additive approach)
if ($lastCheck !== null) {
$existingIds = getAlreadyNotifiedIds($notificationCode);
if (!empty($existingIds)) {
$allIds = array_values(array_unique(array_merge($existingIds, $ids)));
$newIds = array_diff($ids, $existingIds);
if (empty($newIds)) {
$this->info('Skipping: No new records since last notification.');
$duration = microtime(true) - $startTime;
logNotificationSkip($commandName, 'No new records since last notification');
return 0;
}
$filterParams['conditions']['id'] = $allIds;
} else {
$filterParams['conditions']['id'] = $ids;
}
} else {
$filterParams['conditions']['id'] = $ids;
}
// Check for duplicate
if ($lastCheck !== null && isNotificationDuplicate($notificationCode, $filterParams)) {
$this->info('Skipping: Same issue already notified and no new records.');
$duration = microtime(true) - $startTime;
logNotificationSkip($commandName, 'Same issue already notified and no new records');
return 0;
}
$title = 'Repair Logs Missing New Joint Number';
$newCount = $lastCheck !== null ? count(array_diff($ids, getAlreadyNotifiedIds($notificationCode))) : count($ids);
$message = sprintf(
'%d repair log(s) have no new joint number assigned. Please update each record with the new joint number.',
count($filterParams['conditions']['id'] ?? $ids)
);
$sentCount = sendNotification(
$notificationCode,
$message,
null,
$title,
$filterParams,
count($filterParams['conditions']['id'] ?? $ids)
);
$duration = microtime(true) - $startTime;
$totalRecords = count($filterParams['conditions']['id'] ?? $ids);
// Log completion
logNotificationComplete($commandName, $sentCount, $totalRecords, $newCount, $duration);
$this->info("Sent {$sentCount} notification(s) covering {$totalRecords} repair logs ({$newCount} new).");
return 0;
} catch (\Exception $e) {
$duration = microtime(true) - $startTime;
logNotificationError($commandName, $e->getMessage(), [
'file' => $e->getFile(),
'line' => $e->getLine(),
'duration' => $duration,
]);
$this->error("Error: " . $e->getMessage());
return 1;
}
}
}