İlk temizlik tamamlandı bir önceki projeden
This commit is contained in:
@@ -0,0 +1,193 @@
|
||||
<?php
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use Illuminate\Console\Command;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class NotificationCheckLineList extends Command
|
||||
{
|
||||
/**
|
||||
* The name and signature of the console command.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $signature = 'notifications:check-line-list-missing-data';
|
||||
|
||||
/**
|
||||
* The console command description.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Check line list for missing required data (category, NDT ratio, pressure, temperature, test type, test media) where weld logs exist';
|
||||
|
||||
/**
|
||||
* Execute the console command.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
$startTime = microtime(true);
|
||||
$commandName = $this->signature;
|
||||
|
||||
// Log command start
|
||||
logNotificationStart($commandName);
|
||||
|
||||
$this->info('Checking line list for missing required data...');
|
||||
|
||||
$notificationCode = 'notification_line_list_missing_data';
|
||||
|
||||
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);
|
||||
|
||||
// Get unique line numbers from weld_logs that have records
|
||||
$usedLinesQuery = DB::table('weld_logs')
|
||||
->select('line_number')
|
||||
->whereNotNull('line_number')
|
||||
->where('line_number', '!=', '')
|
||||
->distinct();
|
||||
|
||||
// If not first run, only check lines from new weld_logs
|
||||
if ($lastCheck !== null) {
|
||||
$usedLinesQuery->where(function($q) use ($lastCheck) {
|
||||
$q->where('created_at', '>', $lastCheck)
|
||||
->orWhere('updated_at', '>', $lastCheck);
|
||||
});
|
||||
}
|
||||
|
||||
$usedLines = $usedLinesQuery->limit(100)->pluck('line_number');
|
||||
|
||||
if ($usedLines->isEmpty()) {
|
||||
$this->info('No weld log records found with line numbers.');
|
||||
$duration = microtime(true) - $startTime;
|
||||
logNotificationComplete($commandName, 0, 0, 0, $duration);
|
||||
return 0;
|
||||
}
|
||||
|
||||
$this->info("Found {$usedLines->count()} unique lines in weld logs. Checking line list...");
|
||||
|
||||
// Get already notified line numbers
|
||||
$alreadyNotifiedLines = [];
|
||||
if ($lastCheck !== null) {
|
||||
$lastNotification = \App\Models\Notification::where('notification_code', $notificationCode)
|
||||
->whereNotNull('filter_params')
|
||||
->orderBy('created_at', 'DESC')
|
||||
->first();
|
||||
|
||||
if ($lastNotification && $lastNotification->filter_params) {
|
||||
$lastParams = is_array($lastNotification->filter_params)
|
||||
? $lastNotification->filter_params
|
||||
: json_decode($lastNotification->filter_params, true);
|
||||
if (isset($lastParams['conditions']['line_no'])) {
|
||||
$alreadyNotifiedLines = is_array($lastParams['conditions']['line_no'])
|
||||
? $lastParams['conditions']['line_no']
|
||||
: [$lastParams['conditions']['line_no']];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$linesWithMissingData = [];
|
||||
|
||||
foreach ($usedLines as $lineNumber) {
|
||||
// Skip if already notified (unless first run)
|
||||
if ($lastCheck !== null && in_array($lineNumber, $alreadyNotifiedLines)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$linelist = DB::table('line_lists')
|
||||
->where('line_no', $lineNumber)
|
||||
->first();
|
||||
|
||||
if (!$linelist) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$isMissing = empty($linelist->category) ||
|
||||
empty($linelist->ndt) ||
|
||||
(empty($linelist->working_pressure_mpa) && empty($linelist->design_pressure_mpa)) ||
|
||||
(empty($linelist->working_temperature) && empty($linelist->design_temperature)) ||
|
||||
empty($linelist->test_type) ||
|
||||
empty($linelist->test_media);
|
||||
|
||||
if ($isMissing) {
|
||||
$linesWithMissingData[] = $lineNumber;
|
||||
}
|
||||
}
|
||||
|
||||
if (empty($linesWithMissingData)) {
|
||||
$this->info('No new lines with missing data were found.');
|
||||
$duration = microtime(true) - $startTime;
|
||||
logNotificationComplete($commandName, 0, 0, 0, $duration);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Merge with existing lines if not first run
|
||||
if ($lastCheck !== null && !empty($alreadyNotifiedLines)) {
|
||||
$linesWithMissingData = array_values(array_unique(array_merge($alreadyNotifiedLines, $linesWithMissingData)));
|
||||
} else {
|
||||
$linesWithMissingData = array_values(array_unique($linesWithMissingData));
|
||||
}
|
||||
|
||||
$filterParams = [
|
||||
'table' => 'line_lists',
|
||||
'conditions' => [
|
||||
'line_no' => $linesWithMissingData
|
||||
]
|
||||
];
|
||||
|
||||
// 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;
|
||||
}
|
||||
|
||||
$newCount = $lastCheck !== null ? count(array_diff($linesWithMissingData, $alreadyNotifiedLines)) : count($linesWithMissingData);
|
||||
$message = sprintf(
|
||||
'%d line(s) referenced in weld logs have missing required data (category, NDT ratio, pressure, temperature, test type, or test media). Please update the Line List entries.',
|
||||
count($linesWithMissingData)
|
||||
);
|
||||
|
||||
$sentCount = sendNotification(
|
||||
$notificationCode,
|
||||
$message,
|
||||
null,
|
||||
'Line List Missing Required Data',
|
||||
$filterParams,
|
||||
count($linesWithMissingData)
|
||||
);
|
||||
|
||||
$duration = microtime(true) - $startTime;
|
||||
$totalRecords = count($linesWithMissingData);
|
||||
|
||||
// Log completion
|
||||
logNotificationComplete($commandName, $sentCount, $totalRecords, $newCount, $duration);
|
||||
|
||||
$this->info("Sent {$sentCount} notification(s) for {$totalRecords} line(s) with missing data ({$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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user