242 lines
9.5 KiB
PHP
242 lines
9.5 KiB
PHP
<?php
|
||
|
||
namespace App\Services\LineListTriggers\Triggers;
|
||
|
||
use App\Services\LineListTriggers\Base\BaseLineListTrigger;
|
||
use App\Models\NdeMatrix;
|
||
use App\Helpers\TransactionHelper;
|
||
use Illuminate\Support\Facades\DB;
|
||
use Illuminate\Support\Facades\Log;
|
||
|
||
/**
|
||
* NDE Matrix Sync Trigger
|
||
*
|
||
* Handles NDE Matrix synchronization from WeldLogs and LineList:
|
||
* - Collects spec joint types from weld_logs
|
||
* - Creates/updates nde_matrices for each fluid_code + line_no + type_of_welds combination
|
||
* - Handles PWHT, HT, NDT calculations
|
||
*/
|
||
class NdeMatrixSyncTrigger extends BaseLineListTrigger
|
||
{
|
||
public function getName(): string
|
||
{
|
||
return 'NDE Matrix Sync';
|
||
}
|
||
|
||
public function getDependentFields(): array
|
||
{
|
||
return [
|
||
|
||
];
|
||
}
|
||
|
||
protected function process($lineListData, $beforeData, array $context): array
|
||
{
|
||
// Get fresh weld logs data
|
||
$weldlogs = db("weld_logs")
|
||
->where("line_number", $lineListData->line_no)
|
||
->get();
|
||
|
||
// Collect spec joint types from weld logs
|
||
$specJointTypes = $this->collectSpecJointTypes($weldlogs, $lineListData);
|
||
|
||
if(empty($specJointTypes)) {
|
||
return [
|
||
'skipped' => true,
|
||
'reason' => 'no_spec_joint_types_found'
|
||
];
|
||
}
|
||
|
||
Log::debug('specJointTypes içeriği:', ['specJointTypes' => $specJointTypes]);
|
||
|
||
$update = 0;
|
||
$create = 0;
|
||
|
||
// Check if specJointTypes exist for this line list
|
||
if(!isset($specJointTypes[$lineListData->fluid_code][$lineListData->line_no])) {
|
||
Log::warning("NDE Matrix işlemi atlandı - specJointTypes bulunamadı", [
|
||
'line_no' => $lineListData->line_no,
|
||
'fluid_code' => $lineListData->fluid_code
|
||
]);
|
||
return [
|
||
'skipped' => true,
|
||
'reason' => 'spec_joint_types_not_found_for_line'
|
||
];
|
||
}
|
||
|
||
$typeOfJoint = $specJointTypes[$lineListData->fluid_code][$lineListData->line_no];
|
||
|
||
// Convert array to collection for chunk processing
|
||
$jointCollection = collect($typeOfJoint);
|
||
|
||
Log::debug("NDE Matrix sync starting with chunk transaction", [
|
||
'total_joint_types' => $jointCollection->count(),
|
||
'line_no' => $lineListData->line_no,
|
||
'fluid_code' => $lineListData->fluid_code
|
||
]);
|
||
|
||
// Process joints in chunks with TransactionHelper
|
||
TransactionHelper::chunkTransaction(
|
||
$jointCollection,
|
||
function ($jointChunk) use ($lineListData, &$update, &$create) {
|
||
foreach($jointChunk as $jointItem) {
|
||
$ht = 0;
|
||
if($lineListData->pwht == "YES") $ht = 100;
|
||
|
||
$data = [
|
||
'design_area' => $lineListData->unit,
|
||
'fluid' => $lineListData->fluid_code,
|
||
'line' => $lineListData->line_no,
|
||
'line_spec' => $lineListData->line_specification,
|
||
'operation_temp' => $lineListData->working_temperature,
|
||
'operations_pressure_kg' => $lineListData->working_pressure_mpa,
|
||
'pipe_material_class' => $lineListData->pipe_material_class,
|
||
'type_of_joint' => $jointItem,
|
||
'pwht' => $lineListData->pwht,
|
||
'pwht_field' => $lineListData->pwht,
|
||
'ht' => $ht,
|
||
'ndt' => $lineListData->ndt,
|
||
'piping_class_according_to_gost' => $lineListData->category,
|
||
'piping_group' => $lineListData->fluid_group,
|
||
'rev' => $lineListData->rev,
|
||
'tracing' => $lineListData->tracing,
|
||
'tracing_type' => $lineListData->tracing_type,
|
||
'naks_technology' => $lineListData->naks_technology,
|
||
];
|
||
|
||
// UNIQUE CONSTRAINT: line + type_of_joint + fluid
|
||
$uniqueKey = [
|
||
'line' => $lineListData->line_no,
|
||
'type_of_joint' => $jointItem,
|
||
'fluid' => $lineListData->fluid_code,
|
||
];
|
||
|
||
// Check if record already exists with deadlock prevention
|
||
$already = NdeMatrix::where($uniqueKey)
|
||
->orderBy('id', 'ASC') // Deadlock prevention
|
||
->first();
|
||
|
||
if($already) {
|
||
unset($data['pwht_field']);
|
||
|
||
try {
|
||
$updateResult = NdeMatrix::where($uniqueKey)
|
||
->orderBy('id', 'ASC') // Deadlock prevention
|
||
->update($data);
|
||
|
||
Log::debug("NDE Matrix record updated successfully", [
|
||
'affected_rows' => $updateResult,
|
||
'line_no' => $lineListData->line_no,
|
||
'joint_type' => $jointItem,
|
||
'fluid_code' => $lineListData->fluid_code
|
||
]);
|
||
|
||
$update++;
|
||
} catch (\Exception $e) {
|
||
Log::error("NDE Matrix update error", [
|
||
'line_no' => $lineListData->line_no,
|
||
'joint_type' => $jointItem,
|
||
'fluid_code' => $lineListData->fluid_code,
|
||
'error_message' => $e->getMessage()
|
||
]);
|
||
throw $e;
|
||
}
|
||
} else {
|
||
try {
|
||
$ndeRecord = NdeMatrix::updateOrCreate($uniqueKey, $data);
|
||
|
||
$action = $ndeRecord->wasRecentlyCreated ? 'created' : 'updated';
|
||
|
||
Log::debug("NDE Matrix record processed successfully", [
|
||
'record_id' => $ndeRecord->id,
|
||
'action' => $action,
|
||
'line_no' => $lineListData->line_no,
|
||
'joint_type' => $jointItem,
|
||
'fluid_code' => $lineListData->fluid_code
|
||
]);
|
||
|
||
if($action === 'created') {
|
||
$create++;
|
||
} else {
|
||
$update++;
|
||
}
|
||
} catch (\Exception $e) {
|
||
Log::error("NDE Matrix create/update error", [
|
||
'line_no' => $lineListData->line_no,
|
||
'joint_type' => $jointItem,
|
||
'fluid_code' => $lineListData->fluid_code,
|
||
'error_message' => $e->getMessage()
|
||
]);
|
||
throw $e;
|
||
}
|
||
}
|
||
}
|
||
return $jointChunk->count();
|
||
},
|
||
1, // Chunk size
|
||
10000 // 10ms delay - sufficient for deadlock prevention
|
||
);
|
||
|
||
Log::info("NDE Matrix sync completed", [
|
||
'updated' => $update,
|
||
'created' => $create,
|
||
'total_affected' => $update + $create
|
||
]);
|
||
|
||
return [
|
||
'success' => true,
|
||
'created' => $create,
|
||
'updated' => $update
|
||
];
|
||
}
|
||
|
||
/**
|
||
* Collect spec joint types from weld logs
|
||
*/
|
||
protected function collectSpecJointTypes($weldlogs, $lineListData): array
|
||
{
|
||
$specJointTypes = [];
|
||
|
||
foreach($weldlogs AS $weldlog) {
|
||
// Update fluid_code if empty
|
||
if(empty($weldlog->fluid_code) && !empty($lineListData->fluid_code)) {
|
||
db("weld_logs")
|
||
->where("id", $weldlog->id)
|
||
->update([
|
||
'fluid_code' => $lineListData->fluid_code,
|
||
'service_category' => $lineListData->category,
|
||
'fluid_group' => $lineListData->fluid_group,
|
||
]);
|
||
|
||
$weldlog->fluid_code = $lineListData->fluid_code;
|
||
$weldlog->service_category = $lineListData->category;
|
||
$weldlog->fluid_group = $lineListData->fluid_group;
|
||
}
|
||
|
||
if(empty($weldlog->fluid_code)) {
|
||
continue;
|
||
}
|
||
|
||
if(empty($weldlog->line_number)) {
|
||
continue;
|
||
}
|
||
|
||
if(empty($weldlog->type_of_welds)) {
|
||
continue;
|
||
}
|
||
|
||
if(!isset($specJointTypes[$weldlog->fluid_code][$weldlog->line_number])) {
|
||
$specJointTypes[$weldlog->fluid_code][$weldlog->line_number] = [];
|
||
}
|
||
|
||
if(!in_array($weldlog->type_of_welds, $specJointTypes[$weldlog->fluid_code][$weldlog->line_number])) {
|
||
$specJointTypes[$weldlog->fluid_code][$weldlog->line_number][] = $weldlog->type_of_welds;
|
||
}
|
||
}
|
||
|
||
return $specJointTypes;
|
||
}
|
||
}
|
||
|
||
|