İlk temizlik tamamlandı bir önceki projeden
This commit is contained in:
@@ -0,0 +1,161 @@
|
||||
<?php
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use Illuminate\Console\Command;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class NotificationCheckPaintSystem extends Command
|
||||
{
|
||||
/**
|
||||
* The name and signature of the console command.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $signature = 'notifications:check-paint-system-values';
|
||||
|
||||
/**
|
||||
* The console command description.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Check for Paint Systems with incomplete customer-agreed paint values';
|
||||
|
||||
/**
|
||||
* Execute the console command.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
$startTime = microtime(true);
|
||||
$commandName = $this->signature;
|
||||
|
||||
// Log command start
|
||||
logNotificationStart($commandName);
|
||||
|
||||
$this->info('Checking Paint Systems for incomplete customer-agreed values...');
|
||||
|
||||
$notificationCode = 'notification_paint_system_incomplete';
|
||||
|
||||
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);
|
||||
|
||||
$query = DB::table('paint_systems')
|
||||
->where(function($q) {
|
||||
$q->whereNull('primer_coat_name_1')
|
||||
->orWhere('primer_coat_name_1', '')
|
||||
->orWhereNull('brand_name_1')
|
||||
->orWhere('brand_name_1', '')
|
||||
->orWhereNull('thickness_1')
|
||||
->orWhere('thickness_1', 0);
|
||||
})
|
||||
->whereExists(function($q) {
|
||||
$q->select(DB::raw(1))
|
||||
->from('line_lists')
|
||||
->whereColumn('line_lists.painting_cycle', 'paint_systems.paint_cycle');
|
||||
});
|
||||
|
||||
// If not first run, only check new/updated records
|
||||
if ($lastCheck !== null) {
|
||||
$query->where(function($q) use ($lastCheck) {
|
||||
$q->where('created_at', '>', $lastCheck)
|
||||
->orWhere('updated_at', '>', $lastCheck);
|
||||
});
|
||||
|
||||
// Get already notified IDs
|
||||
$alreadyNotifiedIds = getAlreadyNotifiedIds($notificationCode);
|
||||
if (!empty($alreadyNotifiedIds)) {
|
||||
$query->whereNotIn('id', $alreadyNotifiedIds);
|
||||
}
|
||||
}
|
||||
|
||||
$ids = $query->pluck('id')->toArray();
|
||||
|
||||
if (empty($ids)) {
|
||||
$this->info('No new incomplete Paint Systems detected.');
|
||||
$duration = microtime(true) - $startTime;
|
||||
logNotificationComplete($commandName, 0, 0, 0, $duration);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Prepare filter params
|
||||
$filterParams = [
|
||||
'table' => 'paint_systems',
|
||||
'conditions' => []
|
||||
];
|
||||
|
||||
// If not first run, merge with existing IDs
|
||||
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;
|
||||
}
|
||||
|
||||
$newCount = $lastCheck !== null ? count(array_diff($ids, getAlreadyNotifiedIds($notificationCode))) : count($ids);
|
||||
$title = 'Paint System Missing';
|
||||
$message = sprintf(
|
||||
'%d paint system(s) used by Line Lists have missing customer-agreed values. Please complete the paint system details.',
|
||||
count($filterParams['conditions']['id'])
|
||||
);
|
||||
|
||||
$sentCount = sendBatchNotificationWithFilter(
|
||||
$notificationCode,
|
||||
$title,
|
||||
$message,
|
||||
$filterParams,
|
||||
count($filterParams['conditions']['id'])
|
||||
);
|
||||
|
||||
$duration = microtime(true) - $startTime;
|
||||
$totalRecords = count($filterParams['conditions']['id']);
|
||||
|
||||
// Log completion
|
||||
logNotificationComplete($commandName, $sentCount, $totalRecords, $newCount, $duration);
|
||||
|
||||
$this->info("Sent batch notification for incomplete Paint Systems ({$totalRecords} records, {$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