update
This commit is contained in:
@@ -845,6 +845,102 @@ class AstmMessageService
|
||||
return false;
|
||||
}
|
||||
}
|
||||
protected function splitHl7LikeMessages(string $raw): array
|
||||
{
|
||||
$normalized = str_replace(["\r\n", "\r"], "\n", $raw);
|
||||
$normalized = preg_replace('/[\x00-\x08\x0B-\x1F\x7F]/', '', $normalized);
|
||||
$normalized = preg_replace('/(?=MSH\|)/', "\n", $normalized);
|
||||
$parts = preg_split('/\n\s*\n+/', trim($normalized)) ?: [];
|
||||
|
||||
$messages = [];
|
||||
foreach ($parts as $part) {
|
||||
$part = trim($part);
|
||||
if ($part === '' || !str_contains($part, 'MSH|')) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$start = strpos($part, 'MSH|');
|
||||
if ($start !== false) {
|
||||
$part = substr($part, $start);
|
||||
}
|
||||
|
||||
$messages[] = trim($part);
|
||||
}
|
||||
|
||||
return $messages;
|
||||
}
|
||||
protected function saveInstrumentRawResult(string $raw, string $alat): bool
|
||||
{
|
||||
try {
|
||||
$segments = preg_split("/\r\n|\n|\r/", trim($raw)) ?: [];
|
||||
$parsed = [];
|
||||
|
||||
foreach ($segments as $segment) {
|
||||
$segment = trim($segment);
|
||||
if ($segment === '' || !str_contains($segment, '|')) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$fields = explode('|', $segment);
|
||||
$segmentType = strtoupper(trim($fields[0] ?? ''));
|
||||
if ($segmentType !== '') {
|
||||
$parsed[$segmentType][] = $fields;
|
||||
}
|
||||
}
|
||||
|
||||
$msh = $parsed['MSH'][0] ?? [];
|
||||
$pid = $parsed['PID'][0] ?? [];
|
||||
$obr = $parsed['OBR'][0] ?? [];
|
||||
$obx = $parsed['OBX'][0] ?? [];
|
||||
$spm = $parsed['SPM'][0] ?? [];
|
||||
$nte = $parsed['NTE'][0] ?? [];
|
||||
$qpd = $parsed['QPD'][0] ?? [];
|
||||
$qid = $parsed['QID'][0] ?? [];
|
||||
|
||||
$messageDateTime = $this->astmToDateTime($msh[6] ?? null);
|
||||
$accessionNumber = $spm[3] ?? $pid[2] ?? $pid[3] ?? $qpd[3] ?? $qid[1] ?? null;
|
||||
if ($accessionNumber) {
|
||||
$accessionNumber = trim((string) $accessionNumber, "\"@ \t\n\r\0\x0B");
|
||||
}
|
||||
|
||||
$patientName = $pid[3] ?? $pid[5] ?? null;
|
||||
$patientName = $patientName ? trim((string) $patientName, "\" \t\n\r\0\x0B") : null;
|
||||
|
||||
$resultSample = new ResultSample();
|
||||
$resultSample->sender_name = $alat;
|
||||
$resultSample->version_number = $msh[11] ?? null;
|
||||
$resultSample->message_datetime = $messageDateTime;
|
||||
$resultSample->patient_id = $accessionNumber;
|
||||
$resultSample->patient_name_last = $patientName;
|
||||
$resultSample->accession_number = $accessionNumber;
|
||||
$resultSample->test_id = $obr[4] ?? ($qpd[1] ?? null);
|
||||
$resultSample->result_type_code = $obx[3] ?? ($msh[8] ?? null);
|
||||
$resultSample->organism = $obx[5] ?? null;
|
||||
$resultSample->test_status = $obx[11] ?? null;
|
||||
$resultSample->result_status_datetime = $this->astmToDateTime($obx[14] ?? null);
|
||||
$resultSample->instrument_type = $alat;
|
||||
$resultSample->protocol_name = $obr[4] ?? null;
|
||||
$resultSample->result_data = $obx ? json_encode($obx) : null;
|
||||
$resultSample->additional_result = [
|
||||
'source' => 'local_instrument_fallback',
|
||||
'raw_astm' => $raw,
|
||||
'segments' => array_keys($parsed),
|
||||
'nte' => $nte,
|
||||
'qpd' => $qpd,
|
||||
'qid' => $qid,
|
||||
];
|
||||
$resultSample->save();
|
||||
|
||||
return true;
|
||||
} catch (\Throwable $e) {
|
||||
Log::error('Local instrument fallback parse error', [
|
||||
'alat' => $alat,
|
||||
'msg' => $e->getMessage(),
|
||||
]);
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
public function processAstmMessagesLokal($dataListener) {
|
||||
foreach ($dataListener as $data) {
|
||||
try {
|
||||
@@ -872,8 +968,30 @@ class AstmMessageService
|
||||
'created_by' => 'BD'
|
||||
]
|
||||
);
|
||||
} else if ($data->alat == '10.10.120.75' OR $data->alat == '10.10.120.74' OR $data->alat == '10.10.120.73'){
|
||||
|
||||
} else if (
|
||||
$data->alat == 'MYLA-10.10.120.89'
|
||||
OR $data->alat == 'GeneXpert-10.10.120.75'
|
||||
OR $data->alat == 'GeneXpert-10.10.120.74'
|
||||
OR $data->alat == 'GeneXpert-10.10.120.73'
|
||||
OR $data->alat == '10.10.120.75'
|
||||
OR $data->alat == '10.10.120.74'
|
||||
OR $data->alat == '10.10.120.73'
|
||||
){
|
||||
$messages = $this->splitHl7LikeMessages($data->rawdt);
|
||||
$ok = false;
|
||||
|
||||
if (empty($messages)) {
|
||||
$ok = $this->saveInstrumentRawResult($data->rawdt, $data->alat);
|
||||
} else {
|
||||
foreach ($messages as $message) {
|
||||
$saved = $this->saveInstrumentRawResult($message, $data->alat);
|
||||
$ok = $ok || $saved;
|
||||
}
|
||||
}
|
||||
|
||||
DB::table('lis_phoenix')->where('id', $data->id)->update([
|
||||
'processed' => $ok ? 1 : 9
|
||||
]);
|
||||
} else {
|
||||
$result = $this->processAstmResponse($data->rawdt, $data->alat);
|
||||
if ($result) {
|
||||
|
||||
Reference in New Issue
Block a user