Files
citrus-cms/app/Services/LineListTriggers/Triggers/PaintSystemToMatrixSyncTrigger.php
2026-04-28 21:14:25 +03:00

160 lines
6.0 KiB
PHP

<?php
namespace App\Services\LineListTriggers\Triggers;
use App\Services\LineListTriggers\Base\BaseLineListTrigger;
use App\Helpers\TransactionHelper;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
/**
* Paint System To Matrix Sync Trigger
*
* Syncs paint_systems + color_systems data to paint_matrices
* Includes RAL code to Russian color description mapping
*/
class PaintSystemToMatrixSyncTrigger extends BaseLineListTrigger
{
public function getName(): string
{
return 'Paint System To Matrix Sync';
}
public function getDependentFields(): array
{
return [
];
}
protected function process($lineListData, $beforeData, array $context): array
{
Log::info("PaintSystemToMatrixSyncTrigger started", ['painting_cycle' => $lineListData->painting_cycle]);
// Skip if painting_cycle is empty
if (empty($lineListData->painting_cycle)) {
Log::debug("PaintSystemToMatrixSyncTrigger skipped - painting_cycle is empty");
return ['skipped' => true, 'reason' => 'painting_cycle_empty'];
}
// Get paint system
$paintSystem = DB::table('paint_systems')
->where('paint_cycle', $lineListData->painting_cycle)
->first();
if (!$paintSystem) {
Log::debug("No paint system found for paint cycle: {$lineListData->painting_cycle}");
return ['skipped' => true, 'reason' => 'paint_system_not_found'];
}
// Get color system
$colorSystem = DB::table('color_systems')
->where('fluid_code', $lineListData->fluid_code)
->first();
if (!$colorSystem) {
Log::debug("No color system found for fluid code: {$lineListData->fluid_code}");
return ['skipped' => true, 'reason' => 'color_system_not_found'];
}
// Get RAL values
$ral1 = $colorSystem->ral_1;
$ral2 = $colorSystem->ral_2;
$ral3 = $colorSystem->ral_3;
// Get Russian color descriptions
$ralCodes = j(setting("ral-codes"));
$colorRu1 = $this->findRussianColorDescription($ralCodes, $ral1);
$colorRu2 = $this->findRussianColorDescription($ralCodes, $ral2);
$colorRu3 = $this->findRussianColorDescription($ralCodes, $ral3);
// Find paint matrices with matching paint_cycle
$paintMatrices = DB::table('paint_matrices')
->where('area', $lineListData->unit)
->where('fluid_code', $lineListData->fluid_code)
->where('paint_cycle', $lineListData->painting_cycle)
->where('line', $lineListData->line_no)
->orderBy('id', 'ASC') // Deadlock prevention
->get();
Log::info("Paint Matrices found", ['count' => $paintMatrices->count()]);
$updatedCount = 0;
// Process in chunks with TransactionHelper
TransactionHelper::chunkTransaction(
$paintMatrices,
function ($pmChunk) use ($paintSystem, $colorSystem, $ral1, $ral2, $ral3, $colorRu1, $colorRu2, $colorRu3, &$updatedCount) {
foreach ($pmChunk as $paintMatrix) {
$updateData = [
// Paint System data
'surface_preparation' => $paintSystem->surface_preparation,
'touch_up_of_damaged_parts' => $paintSystem->surface_roughness,
'primer_coat' => $paintSystem->primer_coat_name_1,
'brend_name_1' => $paintSystem->brand_name_1,
'thickness_1' => $paintSystem->thickness_1,
'intermediate_coat' => $paintSystem->primer_coat_name_2,
'brend_name_2' => $paintSystem->brand_name_2,
'thickness_2' => $paintSystem->thickness_2,
'final_coat' => $paintSystem->primer_coat_name_3,
'brend_name_3' => $paintSystem->brand_name_3,
'thickness_3' => $paintSystem->thickness_3,
'total_thickness' => ($paintSystem->thickness_1 ?? 0) + ($paintSystem->thickness_2 ?? 0) + ($paintSystem->thickness_3 ?? 0),
// Color System data
'fluid_code_description' => $colorSystem->fluid_code_description,
'design_temperature' => $colorSystem->design_temperature,
'operation_temperature' => $colorSystem->working_temperature,
'ral_code_1' => $ral1,
'ral_code_2' => $ral2,
'ral_code_3' => $ral3,
'colour_1' => $colorRu1,
'colour_2' => $colorRu2,
'colour_3' => $colorRu3,
'updated_at' => now()
];
$result = DB::table('paint_matrices')
->where('id', $paintMatrix->id)
->update($updateData);
if ($result) {
$updatedCount++;
}
}
return $pmChunk->count();
},
1, // Chunk size
10000 // 10ms delay
);
Log::debug("Paint Systems to Paint Matrices synchronization completed. Updated $updatedCount records.");
return [
'success' => true,
'updated_records' => $updatedCount
];
}
/**
* Find Russian color description for a RAL code
*/
protected function findRussianColorDescription($ralCodes, $ralCode): string
{
if (empty($ralCode) || empty($ralCodes)) {
return '';
}
foreach ($ralCodes as $entry) {
if (isset($entry['ral_code']) && $entry['ral_code'] == $ralCode && isset($entry['ru'])) {
return $entry['ru'];
}
}
return '';
}
}