Много чего
This commit is contained in:
146
app/Services/ArchiveSearchService.php
Normal file
146
app/Services/ArchiveSearchService.php
Normal file
@@ -0,0 +1,146 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use App\Models\ArchiveInfo;
|
||||
use App\Models\Mis\SttMedicalHistory as MisMedicalHistory;
|
||||
use App\Models\SI\SttMedicalHistory as SiMedicalHistory;
|
||||
|
||||
class ArchiveSearchService
|
||||
{
|
||||
/**
|
||||
* Поиск карты по номеру
|
||||
*/
|
||||
public function searchByNumber(string $searchText): ?ArchiveInfo
|
||||
{
|
||||
if (!is_numeric($searchText)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// 1. Проверяем, есть ли запись в archive_infos
|
||||
$archiveInfo = $this->searchInArchiveInfos($searchText);
|
||||
if ($archiveInfo) {
|
||||
return $archiveInfo;
|
||||
}
|
||||
|
||||
// 2. Если нет в archive_infos, ищем в MIS
|
||||
$misHistory = $this->searchInMis($searchText);
|
||||
if ($misHistory) {
|
||||
// Создаем запись в archive_infos для MIS карты
|
||||
return $this->createArchiveInfoForMis($misHistory);
|
||||
}
|
||||
|
||||
// 3. Если нет в MIS, ищем в FoxPro
|
||||
$foxproHistory = $this->searchInFoxPro($searchText);
|
||||
if ($foxproHistory) {
|
||||
// Создаем запись в archive_infos для FoxPro карты
|
||||
return $this->createArchiveInfoForFoxPro($foxproHistory);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Поиск по ФИО или другим критериям
|
||||
*/
|
||||
public function searchByName(string $searchText): array
|
||||
{
|
||||
// Поиск в MIS
|
||||
$misResults = MisMedicalHistory::query()
|
||||
->where(function($query) use ($searchText) {
|
||||
$query->whereRaw("CONCAT(\"FAMILY\", ' ', \"Name\", ' ', COALESCE(\"OT\", '')) ILIKE ?", ["$searchText%"])
|
||||
->orWhere('FAMILY', 'ILIKE', "$searchText%")
|
||||
->orWhere('Name', 'ILIKE', "$searchText%")
|
||||
->orWhere('OT', 'ILIKE', "$searchText%");
|
||||
})
|
||||
->get();
|
||||
|
||||
return $misResults;
|
||||
}
|
||||
|
||||
/**
|
||||
* Поиск в archive_infos
|
||||
*/
|
||||
private function searchInArchiveInfos(string $cardNumber): ?ArchiveInfo
|
||||
{
|
||||
return ArchiveInfo::query()
|
||||
->where('mis_num', $cardNumber)
|
||||
->orWhere('foxpro_num', $cardNumber)
|
||||
->with(['misHistory', 'foxproHistory'])
|
||||
->first();
|
||||
}
|
||||
|
||||
/**
|
||||
* Поиск в MIS
|
||||
*/
|
||||
private function searchInMis(string $cardNumber): ?MisMedicalHistory
|
||||
{
|
||||
return MisMedicalHistory::where('MedCardNum', $cardNumber)->first();
|
||||
}
|
||||
|
||||
/**
|
||||
* Поиск в FoxPro
|
||||
*/
|
||||
private function searchInFoxPro(string $cardNumber): ?SiMedicalHistory
|
||||
{
|
||||
return SiMedicalHistory::where('nkarta', $cardNumber)->first();
|
||||
}
|
||||
|
||||
/**
|
||||
* Создание записи в archive_infos для MIS карты
|
||||
*/
|
||||
private function createArchiveInfoForMis(MisMedicalHistory $misHistory): ArchiveInfo
|
||||
{
|
||||
// Проверяем, нет ли уже записи
|
||||
$existingArchiveInfo = ArchiveInfo::where('mis_history_id', $misHistory->MedicalHistoryID)->first();
|
||||
if ($existingArchiveInfo) {
|
||||
return $existingArchiveInfo;
|
||||
}
|
||||
|
||||
// Ищем связанную запись в FoxPro
|
||||
$foxproHistory = SiMedicalHistory::where('snils', $misHistory->SS)
|
||||
->where('menddate', $misHistory->DateExtract)
|
||||
->where('dr', '!=', $misHistory->BD)
|
||||
->first();
|
||||
|
||||
// Создаем запись
|
||||
return ArchiveInfo::create([
|
||||
'mis_history_id' => $misHistory->MedicalHistoryID,
|
||||
'mis_num' => $misHistory->MedCardNum,
|
||||
'foxpro_history_id' => $foxproHistory?->keykarta,
|
||||
'foxpro_num' => $foxproHistory?->nkarta,
|
||||
'archive_num' => $foxproHistory?->narhiv,
|
||||
'post_in' => $foxproHistory?->datearhiv,
|
||||
'status_id' => 2, // Статус по умолчанию
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Создание записи в archive_infos для FoxPro карты
|
||||
*/
|
||||
private function createArchiveInfoForFoxPro(SiMedicalHistory $foxproHistory): ArchiveInfo
|
||||
{
|
||||
// Проверяем, нет ли уже записи
|
||||
$existingArchiveInfo = ArchiveInfo::where('foxpro_history_id', $foxproHistory->keykarta)->first();
|
||||
if ($existingArchiveInfo) {
|
||||
return $existingArchiveInfo;
|
||||
}
|
||||
|
||||
// Ищем связанную запись в MIS
|
||||
$misHistory = MisMedicalHistory::where('SS', $foxproHistory->snils)
|
||||
->where('DateExtract', $foxproHistory->menddate)
|
||||
->where('BD', '!=', $foxproHistory->dr)
|
||||
->first();
|
||||
|
||||
// Создаем запись
|
||||
return ArchiveInfo::create([
|
||||
'foxpro_history_id' => $foxproHistory->keykarta,
|
||||
'foxpro_num' => $foxproHistory->nkarta,
|
||||
'archive_num' => $foxproHistory->narhiv,
|
||||
'post_in' => $foxproHistory->datearhiv,
|
||||
'mis_history_id' => $misHistory?->MedicalHistoryID,
|
||||
'mis_num' => $misHistory?->MedCardNum,
|
||||
'status_id' => 2, // Статус по умолчанию
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user