Files
2026-04-28 21:14:25 +03:00

136 lines
4.5 KiB
PHP

<?php
namespace App\Services\LineListTriggers\Triggers;
use App\Services\LineListTriggers\Base\BaseLineListTrigger;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use App\Jobs\ExecuteSaveTriggerJob;
/**
* Paint System Sync Trigger
*
* Syncs data to paint_systems and color_systems tables
*/
class PaintSystemSyncTrigger extends BaseLineListTrigger
{
public function getName(): string
{
return 'Paint System Sync';
}
public function getDependentFields(): array
{
return [
'painting_cycle',
'rev',
'fluid_code',
'fluid_ru',
'design_temperature',
'working_temperature',
'unit'
];
}
protected function process($lineListData, $beforeData, array $context): array
{
$count = 0;
if (!empty($lineListData->painting_cycle)) {
// Update or insert into paint_systems
$existingRecord = DB::table('paint_systems')
->where('paint_cycle', $lineListData->painting_cycle)
->first();
if ($existingRecord) {
DB::table('paint_systems')
->where('paint_cycle', $lineListData->painting_cycle)
->update([
'paint_cycle' => $lineListData->painting_cycle,
'revision' => $lineListData->rev ?? '',
]);
ExecuteSaveTriggerJob::dispatch(
'paint_systems',
['key' => ['id' => $existingRecord->id]],
['id' => $existingRecord->id],
['paint_cycle' => $lineListData->painting_cycle, 'revision' => $lineListData->rev ?? ''],
$existingRecord,
'update'
);
} else {
$insertedId = DB::table('paint_systems')->insertGetId([
'paint_cycle' => $lineListData->painting_cycle,
'revision' => $lineListData->rev ?? '',
'created_at' => now(),
'updated_at' => now(),
]);
$newRecord = DB::table('paint_systems')->find($insertedId);
ExecuteSaveTriggerJob::dispatch(
'paint_systems',
['key' => ['id' => $insertedId]],
['id' => $insertedId],
['paint_cycle' => $lineListData->painting_cycle, 'revision' => $lineListData->rev ?? ''],
$newRecord,
'insert'
);
}
// Update or insert into color_systems
$colorSystemRecord = DB::table('color_systems')
->where('fluid_code', $lineListData->fluid_code)
->first();
$colorSystemData = [
'fluid_code_description' => $lineListData->fluid_ru ?? "",
'fluid_code' => $lineListData->fluid_code,
'design_temperature' => $lineListData->design_temperature ?? "",
'working_temperature' => $lineListData->working_temperature ?? "",
'unit' => $lineListData->unit ?? "",
'updated_at' => now(),
];
if ($colorSystemRecord) {
DB::table('color_systems')
->where('id', $colorSystemRecord->id)
->update($colorSystemData);
ExecuteSaveTriggerJob::dispatch(
'color_systems',
['key' => ['id' => $colorSystemRecord->id]],
['id' => $colorSystemRecord->id],
$colorSystemData,
$colorSystemRecord,
'update'
);
} else {
$colorSystemData['created_at'] = now();
$insertedId = DB::table('color_systems')->insertGetId($colorSystemData);
$newColorRecord = DB::table('color_systems')->find($insertedId);
ExecuteSaveTriggerJob::dispatch(
'color_systems',
['key' => ['id' => $insertedId]],
['id' => $insertedId],
$colorSystemData,
$newColorRecord,
'insert'
);
}
$count++;
}
Log::debug("Sync Paint System $count row update or create");
return [
'success' => true,
'processed_records' => $count
];
}
}