142 lines
5.5 KiB
PHP
142 lines
5.5 KiB
PHP
<?php use Carbon\Carbon;
|
||
|
||
// İşlem başlangıç zamanı
|
||
$startTime = microtime(true);
|
||
|
||
$lastIdPrefix = "logs-to-weldlog-sync";
|
||
$lastId = 0;
|
||
$take = setting('cron_job_batch_process_count');
|
||
|
||
if(Cache::has($lastIdPrefix)) {
|
||
$lastId = Cache::get($lastIdPrefix);
|
||
}
|
||
|
||
$testTypes = log_test_types();
|
||
$updateDatas = [];
|
||
$acceptedColumns = weldlog_accepted_columns();
|
||
|
||
$weldLogColumns = table_columns("weld_logs");
|
||
|
||
$diff = array_diff($acceptedColumns,$weldLogColumns);
|
||
|
||
Log::debug("$lastIdPrefix start " . simdi());
|
||
|
||
foreach($testTypes AS $testType) {
|
||
$query = db($testType)
|
||
->whereNotNull("welding_date")
|
||
->get();
|
||
|
||
foreach($query AS $q) {
|
||
$updateData = [];
|
||
|
||
foreach($q AS $columnName => $value) {
|
||
if(in_array($columnName, $acceptedColumns)) {
|
||
if($value == "") $value = null;
|
||
$updateData[$columnName] = $value;
|
||
}
|
||
}
|
||
// result_ferrite -> ferrite_result dönüşümü
|
||
/*
|
||
if (isset($updateData['result_ferrite'])) {
|
||
$updateData['ferrite_result'] = $updateData['result_ferrite'];
|
||
unset($updateData['result_ferrite']);
|
||
}
|
||
*/
|
||
|
||
if (isset($updateDatas[$q->iso_number][$q->no_of_the_joint_as_per_as_built_survey][$q->welding_date])) {
|
||
$existingData = $updateDatas[$q->iso_number][$q->no_of_the_joint_as_per_as_built_survey][$q->welding_date];
|
||
$mergedData = array_merge_recursive($existingData, $updateData);
|
||
} else {
|
||
$mergedData = $updateData;
|
||
}
|
||
|
||
$updateDatas[$q->iso_number][$q->no_of_the_joint_as_per_as_built_survey][$q->welding_date] = $mergedData;
|
||
Cache::put($lastIdPrefix, $q->id);
|
||
Log::debug("updateDatas eklendi: iso_number=$q->iso_number, joint=$q->no_of_the_joint_as_per_as_built_survey, welding_date=$q->welding_date, data=" . json_encode($mergedData));
|
||
}
|
||
}
|
||
|
||
// result_ferrite anahtarını tüm dizide temizle
|
||
function recursiveUnsetResultFerrite(&$array) {
|
||
foreach ($array as &$value) {
|
||
if (is_array($value)) {
|
||
recursiveUnsetResultFerrite($value);
|
||
}
|
||
}
|
||
/*
|
||
if (isset($array['result_ferrite'])) {
|
||
$array['ferrite_result'] = $array['result_ferrite'];
|
||
unset($array['result_ferrite']);
|
||
}
|
||
*/
|
||
}
|
||
|
||
DB::beginTransaction();
|
||
Log::debug("Transaction başladı. updateDatas toplam iso_number: " . count($updateDatas));
|
||
$k = 0;
|
||
try {
|
||
// Tüm updateDatas dizisinde result_ferrite temizliği
|
||
recursiveUnsetResultFerrite($updateDatas);
|
||
foreach($updateDatas AS $isoNumber => $data) {
|
||
foreach($data AS $jointName => $weldingDateDatas) {
|
||
foreach($weldingDateDatas AS $weldingDate => $subData) {
|
||
// result_ferrite dönüşümü
|
||
/*
|
||
if (isset($subData['result_ferrite'])) {
|
||
$subData['ferrite_result'] = $subData['result_ferrite'];
|
||
unset($subData['result_ferrite']);
|
||
}
|
||
*/
|
||
// Sadece weld_logs tablosunda olan sütunları bırak
|
||
$weldLogColumns = table_columns('weld_logs');
|
||
$subData = array_intersect_key($subData, array_flip($weldLogColumns));
|
||
|
||
// Alanları normalize et
|
||
$normalizedIsoNumber = trim((string)$isoNumber);
|
||
$normalizedJointName = trim((string)$jointName);
|
||
$normalizedWeldingDate = trim((string)$weldingDate);
|
||
|
||
// Güncellenecek kayıtları bul
|
||
$updatedRows = db("weld_logs")
|
||
->where([
|
||
'iso_number' => $normalizedIsoNumber,
|
||
'no_of_the_joint_as_per_as_built_survey' => $normalizedJointName,
|
||
'welding_date' => $normalizedWeldingDate
|
||
])
|
||
->get(['id']);
|
||
|
||
Log::debug("Güncelleme denemesi: iso_number=$normalizedIsoNumber, joint=$normalizedJointName, welding_date=$normalizedWeldingDate, subData=" . json_encode($subData) . ", bulunan id'ler=" . json_encode($updatedRows->pluck('id')->toArray()));
|
||
|
||
if (count($updatedRows) > 0) {
|
||
$ids = $updatedRows->pluck('id')->toArray();
|
||
Log::debug("$normalizedIsoNumber + $normalizedJointName + $normalizedWeldingDate = Güncellenen weld_logs id'leri: ", $ids);
|
||
Log::debug("$normalizedIsoNumber + $normalizedJointName + $normalizedWeldingDate = Güncellenen weld_logs veri: ", $subData);
|
||
$k += db("weld_logs")
|
||
->where([
|
||
'iso_number' => $normalizedIsoNumber,
|
||
'no_of_the_joint_as_per_as_built_survey' => $normalizedJointName,
|
||
'welding_date' => $normalizedWeldingDate
|
||
])
|
||
->update($subData);
|
||
Log::debug("Güncelleme sonrası: iso_number=$normalizedIsoNumber, joint=$normalizedJointName, welding_date=$normalizedWeldingDate, güncellenen satır sayısı=$k");
|
||
}
|
||
}
|
||
}
|
||
}
|
||
DB::commit();
|
||
Log::debug("Transaction başarılı şekilde tamamlandı. Toplam güncellenen satır: $k");
|
||
} catch (\Throwable $th) {
|
||
Log::debug("Hata oluştu: " . $th->getMessage());
|
||
DB::rollback();
|
||
Log::debug("Transaction geri alındı.");
|
||
}
|
||
|
||
$endTime = microtime(true);
|
||
$executionTime = round($endTime - $startTime, 2);
|
||
|
||
dump("Total $k row processed");
|
||
dump("Execution time: {$executionTime} seconds");
|
||
|
||
Log::debug("$lastIdPrefix finish " . simdi());
|
||
?>
|