Перевод на доменную архитектуру
This commit is contained in:
@@ -2,6 +2,8 @@
|
||||
|
||||
namespace App\Http\Controllers\Api;
|
||||
|
||||
use App\Application\Reports\ReportSavePathService;
|
||||
use App\Domain\Reports\ValueObjects\MetrikaConfig;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Resources\Api\DepartmentPatientOperationResource;
|
||||
use App\Http\Resources\Mis\FormattedPatientResource;
|
||||
@@ -33,7 +35,9 @@ class ReportController extends Controller
|
||||
public function __construct(
|
||||
protected MisPatientService $misPatientService,
|
||||
protected ReportService $reportService,
|
||||
protected DateRangeService $dateRangeService) {}
|
||||
protected DateRangeService $dateRangeService,
|
||||
protected ReportSavePathService $reportSavePathService,
|
||||
) {}
|
||||
|
||||
public function index(Request $request)
|
||||
{
|
||||
@@ -64,94 +68,9 @@ class ReportController extends Controller
|
||||
]);
|
||||
}
|
||||
|
||||
private function getSurgicalPatientsFromSnapshot(string $type, array $reportIds)
|
||||
{
|
||||
$count = 0;
|
||||
switch ($type) {
|
||||
case 'emergency':
|
||||
$count = $this->getMetrikaResult(10, $reportIds);
|
||||
break;
|
||||
case 'plan':
|
||||
$count = $this->getMetrikaResult(11, $reportIds);
|
||||
break;
|
||||
case 'recipient':
|
||||
$count = $this->getMetrikaResult(3, $reportIds);
|
||||
break;
|
||||
}
|
||||
|
||||
return $count;
|
||||
}
|
||||
|
||||
private function getPatientsCountFromSnapshot(string $type, array $reportIds)
|
||||
{
|
||||
$count = 0;
|
||||
switch ($type) {
|
||||
case 'emergency':
|
||||
$count = $this->getMetrikaResult(10, $reportIds);
|
||||
break;
|
||||
case 'plan':
|
||||
$count = $this->getMetrikaResult(11, $reportIds);
|
||||
break;
|
||||
case 'recipient':
|
||||
$count = $this->getMetrikaResult(3, $reportIds);
|
||||
break;
|
||||
case 'outcome':
|
||||
$count = $this->getMetrikaResult(7, $reportIds);
|
||||
break;
|
||||
case 'deceased':
|
||||
$count = $this->getMetrikaResult(9, $reportIds);
|
||||
break;
|
||||
case 'current':
|
||||
$count = $this->getMetrikaResult(8, $reportIds);
|
||||
break;
|
||||
}
|
||||
|
||||
return $count;
|
||||
}
|
||||
|
||||
private function getMetrikaResult(int $metrikaItemId, array $reportIds)
|
||||
{
|
||||
$reports = Report::whereIn('report_id', $reportIds)
|
||||
->with('metrikaResults')
|
||||
->get();
|
||||
|
||||
$count = 0;
|
||||
foreach ($reports as $report) {
|
||||
foreach ($report->metrikaResults as $metrikaResult) {
|
||||
if ($metrikaResult->rf_metrika_item_id === $metrikaItemId) {
|
||||
$count += intval($metrikaResult->value) ?? 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $count;
|
||||
}
|
||||
|
||||
/**
|
||||
* Получить ID поступивших пациентов из снапшотов
|
||||
*/
|
||||
private function getRecipientIdsFromSnapshots(array $reportIds)
|
||||
{
|
||||
$recipientIds = MedicalHistorySnapshot::whereIn('rf_report_id', $reportIds)
|
||||
->where('patient_type', 'recipient')
|
||||
->pluck('rf_medicalhistory_id')
|
||||
->unique()
|
||||
->toArray();
|
||||
|
||||
return $recipientIds;
|
||||
}
|
||||
|
||||
public function store(Request $request)
|
||||
{
|
||||
$user = Auth::user();
|
||||
$misDepartmentId = $user->department->rf_mis_department_id;
|
||||
|
||||
$branchId = MisStationarBranch::where('rf_DepartmentID', $misDepartmentId)
|
||||
->value('StationarBranchID');
|
||||
|
||||
// Определяем, является ли пользователь заведующим/администратором
|
||||
$isHeadOrAdmin = $user->isHeadOfDepartment() || $user->isAdmin();
|
||||
|
||||
$data = $request->validate([
|
||||
'metrics' => 'required',
|
||||
'observationPatients' => 'nullable',
|
||||
@@ -162,6 +81,27 @@ class ReportController extends Controller
|
||||
'userId' => 'required|integer',
|
||||
'reportId' => 'nullable',
|
||||
]);
|
||||
$dateRange = $this->dateRangeService->getDateRangeFromRequest($request, $user);
|
||||
|
||||
if ($this->reportSavePathService->usesNewArchitecture()) {
|
||||
$this->reportSavePathService->saveManual($user, [
|
||||
...$data,
|
||||
'dates' => [(int) $data['startAt'], (int) $data['endAt']],
|
||||
]);
|
||||
return response()->json([
|
||||
'status' => 'ok',
|
||||
'path' => 'new',
|
||||
]);
|
||||
}
|
||||
|
||||
$misDepartmentId = $user->department->rf_mis_department_id;
|
||||
|
||||
$branchId = MisStationarBranch::where('rf_DepartmentID', $misDepartmentId)
|
||||
->value('StationarBranchID');
|
||||
|
||||
// Определяем, является ли пользователь заведующим/администратором
|
||||
$isHeadOrAdmin = $user->isHeadOfDepartment() || $user->isAdmin();
|
||||
|
||||
$metrics = $data['metrics'];
|
||||
$observationPatients = $data['observationPatients'];
|
||||
$unwantedEvents = $data['unwantedEvents'];
|
||||
@@ -291,11 +231,11 @@ class ReportController extends Controller
|
||||
MetrikaResult::updateOrCreate(
|
||||
[
|
||||
'rf_report_id' => $report->report_id,
|
||||
'rf_metrika_item_id' => 16,
|
||||
'rf_metrika_item_id' => MetrikaConfig::UNWANTED_EVENTS,
|
||||
],
|
||||
[
|
||||
'rf_report_id' => $report->report_id,
|
||||
'rf_metrika_item_id' => 16,
|
||||
'rf_metrika_item_id' => MetrikaConfig::UNWANTED_EVENTS,
|
||||
'value' => count($unwantedEvents),
|
||||
]
|
||||
);
|
||||
@@ -338,11 +278,11 @@ class ReportController extends Controller
|
||||
MetrikaResult::updateOrCreate(
|
||||
[
|
||||
'rf_report_id' => $report->report_id,
|
||||
'rf_metrika_item_id' => 14,
|
||||
'rf_metrika_item_id' => MetrikaConfig::OBSERVATION,
|
||||
],
|
||||
[
|
||||
'rf_report_id' => $report->report_id,
|
||||
'rf_metrika_item_id' => 14,
|
||||
'rf_metrika_item_id' => MetrikaConfig::OBSERVATION,
|
||||
'value' => count($observationPatients),
|
||||
]
|
||||
);
|
||||
@@ -360,11 +300,11 @@ class ReportController extends Controller
|
||||
MetrikaResult::updateOrCreate(
|
||||
[
|
||||
'rf_report_id' => $report->report_id,
|
||||
'rf_metrika_item_id' => 4,
|
||||
'rf_metrika_item_id' => MetrikaConfig::PLAN,
|
||||
],
|
||||
[
|
||||
'rf_report_id' => $report->report_id,
|
||||
'rf_metrika_item_id' => 4,
|
||||
'rf_metrika_item_id' => MetrikaConfig::PLAN,
|
||||
'value' => $planCount,
|
||||
]
|
||||
);
|
||||
@@ -381,11 +321,11 @@ class ReportController extends Controller
|
||||
MetrikaResult::updateOrCreate(
|
||||
[
|
||||
'rf_report_id' => $report->report_id,
|
||||
'rf_metrika_item_id' => 12,
|
||||
'rf_metrika_item_id' => MetrikaConfig::EMERGENCY,
|
||||
],
|
||||
[
|
||||
'rf_report_id' => $report->report_id,
|
||||
'rf_metrika_item_id' => 12,
|
||||
'rf_metrika_item_id' => MetrikaConfig::EMERGENCY,
|
||||
'value' => $emergencyCount,
|
||||
]
|
||||
);
|
||||
@@ -401,11 +341,11 @@ class ReportController extends Controller
|
||||
MetrikaResult::updateOrCreate(
|
||||
[
|
||||
'rf_report_id' => $report->report_id,
|
||||
'rf_metrika_item_id' => 15,
|
||||
'rf_metrika_item_id' => MetrikaConfig::DISCHARGED,
|
||||
],
|
||||
[
|
||||
'rf_report_id' => $report->report_id,
|
||||
'rf_metrika_item_id' => 15,
|
||||
'rf_metrika_item_id' => MetrikaConfig::DISCHARGED,
|
||||
'value' => count($dischargedIds),
|
||||
]
|
||||
);
|
||||
@@ -421,11 +361,11 @@ class ReportController extends Controller
|
||||
MetrikaResult::updateOrCreate(
|
||||
[
|
||||
'rf_report_id' => $report->report_id,
|
||||
'rf_metrika_item_id' => 13,
|
||||
'rf_metrika_item_id' => MetrikaConfig::TRANSFERRED,
|
||||
],
|
||||
[
|
||||
'rf_report_id' => $report->report_id,
|
||||
'rf_metrika_item_id' => 13,
|
||||
'rf_metrika_item_id' => MetrikaConfig::TRANSFERRED,
|
||||
'value' => count($transferredIds),
|
||||
]
|
||||
);
|
||||
@@ -441,11 +381,11 @@ class ReportController extends Controller
|
||||
MetrikaResult::updateOrCreate(
|
||||
[
|
||||
'rf_report_id' => $report->report_id,
|
||||
'rf_metrika_item_id' => 9,
|
||||
'rf_metrika_item_id' => MetrikaConfig::DECEASED,
|
||||
],
|
||||
[
|
||||
'rf_report_id' => $report->report_id,
|
||||
'rf_metrika_item_id' => 9,
|
||||
'rf_metrika_item_id' => MetrikaConfig::DECEASED,
|
||||
'value' => count($deceasedIds),
|
||||
]
|
||||
);
|
||||
@@ -471,11 +411,11 @@ class ReportController extends Controller
|
||||
MetrikaResult::updateOrCreate(
|
||||
[
|
||||
'rf_report_id' => $report->report_id,
|
||||
'rf_metrika_item_id' => 3,
|
||||
'rf_metrika_item_id' => MetrikaConfig::RECIPIENT,
|
||||
],
|
||||
[
|
||||
'rf_report_id' => $report->report_id,
|
||||
'rf_metrika_item_id' => 3,
|
||||
'rf_metrika_item_id' => MetrikaConfig::RECIPIENT,
|
||||
'value' => count($recipientIds),
|
||||
]
|
||||
);
|
||||
@@ -675,258 +615,6 @@ class ReportController extends Controller
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Получить пациентов (плановых или экстренных)
|
||||
*/
|
||||
private function getPlanOrEmergencyPatients(
|
||||
?string $status,
|
||||
bool $isHeadOrAdmin,
|
||||
$branchId,
|
||||
$startDate, $endDate,
|
||||
bool $returnedCount = false,
|
||||
bool $all = false,
|
||||
bool $onlyIds = false,
|
||||
bool $today = false
|
||||
) {
|
||||
// Определяем, является ли статус outcome
|
||||
$isOutcomeStatus = in_array($status, ['outcome-transferred', 'outcome-discharged', 'outcome-deceased']);
|
||||
|
||||
if ($isOutcomeStatus) {
|
||||
$visitResultIds = match ($status) {
|
||||
'outcome-transferred' => [4, 14],
|
||||
'outcome-discharged' => [1, 11, 2, 12, 7, 18, 48],
|
||||
'outcome-deceased' => [5, 6, 15, 16],
|
||||
default => [],
|
||||
};
|
||||
|
||||
$historyQuery = $this->buildOutcomeMedicalHistoryQuery(
|
||||
$branchId,
|
||||
$startDate,
|
||||
$endDate,
|
||||
$visitResultIds
|
||||
);
|
||||
|
||||
if ($onlyIds) {
|
||||
return $historyQuery->pluck('MedicalHistoryID')->values();
|
||||
}
|
||||
|
||||
if ($returnedCount) {
|
||||
return $historyQuery->count();
|
||||
}
|
||||
|
||||
return $historyQuery
|
||||
->with(['surgicalOperations' => function ($query) use ($startDate, $endDate) {
|
||||
$query->where('Date', '>=', $startDate)
|
||||
->where('Date', '<=', $endDate);
|
||||
}])
|
||||
->orderBy('DateRecipient', 'DESC')
|
||||
->get();
|
||||
} else {
|
||||
// Разная логика для заведующего и врача
|
||||
if ($isHeadOrAdmin) {
|
||||
// Заведующий: используем whereInDepartment
|
||||
$query = MisMigrationPatient::whereInDepartment($branchId)
|
||||
// ->whereBetween('DateIngoing', [$startDate, $endDate]);
|
||||
->where('DateIngoing', '>=', $startDate)
|
||||
->where('DateIngoing', '<=', $endDate);
|
||||
} else {
|
||||
// Врач: используем currentlyInTreatment + фильтр по дате
|
||||
$query = MisMigrationPatient::currentlyInTreatment($branchId)
|
||||
->when($today, function ($query) use ($startDate, $endDate) {
|
||||
// return $query->whereBetween('DateIngoing', [$startDate, $endDate]);
|
||||
return $query->where('DateIngoing', '>=', $startDate)
|
||||
->where('DateIngoing', '<=', $endDate);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
$medicalHistoryIds = $query->pluck('rf_MedicalHistoryID')->toArray();
|
||||
|
||||
if (empty($medicalHistoryIds)) {
|
||||
if ($returnedCount) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return collect();
|
||||
}
|
||||
|
||||
// Получаем истории
|
||||
$query = MisMedicalHistory::whereIn('MedicalHistoryID', $medicalHistoryIds)
|
||||
->with(['surgicalOperations' => function ($query) use ($startDate, $endDate) {
|
||||
// $query->whereBetween('Date', [$startDate, $endDate]);
|
||||
$query->where('Date', '>=', $startDate)
|
||||
->where('Date', '<=', $endDate);
|
||||
}])
|
||||
->orderBy('DateRecipient', 'DESC');
|
||||
|
||||
// Выбираем план или экстренность
|
||||
if (! $all && ! $isOutcomeStatus) {
|
||||
if ($status === 'plan') {
|
||||
$query->plan();
|
||||
} elseif ($status === 'emergency') {
|
||||
$query->emergency();
|
||||
}
|
||||
}
|
||||
|
||||
// Для врача добавляем условие "в отделении"
|
||||
if (! $isHeadOrAdmin && ! $isOutcomeStatus) {
|
||||
$query->currentlyHospitalized();
|
||||
}
|
||||
|
||||
if ($onlyIds) {
|
||||
return $query->select('MedicalHistoryID')
|
||||
->pluck('MedicalHistoryID')->values();
|
||||
} else {
|
||||
if ($returnedCount) {
|
||||
return $query->count();
|
||||
} else {
|
||||
return $query->get();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Получить всех выбывших пациентов
|
||||
*/
|
||||
private function getAllOutcomePatients($branchId, $startDate, $endDate, bool $returnedCount = false)
|
||||
{
|
||||
$query = $this->buildOutcomeMedicalHistoryQuery($branchId, $startDate, $endDate);
|
||||
|
||||
if ($returnedCount) {
|
||||
return $query->count();
|
||||
}
|
||||
|
||||
return $query
|
||||
->with(['surgicalOperations'])
|
||||
->orderBy('DateRecipient', 'DESC')
|
||||
->get();
|
||||
}
|
||||
|
||||
/**
|
||||
* Получить понятное название типа выбытия
|
||||
*/
|
||||
private function getOutcomeTypeName($visitResultId): string
|
||||
{
|
||||
return match ($visitResultId) {
|
||||
1, 7, 8, 9, 10, 11, 48, 49, 124 => 'Выписка',
|
||||
2, 3, 4, 12, 13, 14 => 'Перевод',
|
||||
5, 6, 15, 16 => 'Умер',
|
||||
// Добавьте другие коды по мере необходимости
|
||||
default => 'Другое ('.$visitResultId.')'
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Получить умерших пациентов (исход)
|
||||
*/
|
||||
private function getDeceasedOutcomePatients($branchId, $startDate, $endDate, bool $returnedCount = false, bool $onlyIds = false)
|
||||
{
|
||||
$query = $this->buildOutcomeMedicalHistoryQuery(
|
||||
$branchId,
|
||||
$startDate,
|
||||
$endDate,
|
||||
[5, 6, 15, 16]
|
||||
);
|
||||
|
||||
if ($onlyIds) {
|
||||
return $query->pluck('MedicalHistoryID')->values();
|
||||
}
|
||||
|
||||
if ($returnedCount) {
|
||||
return $query->count();
|
||||
} else {
|
||||
return $query
|
||||
->with(['surgicalOperations'])
|
||||
->orderBy('DateRecipient', 'DESC')
|
||||
->get();
|
||||
}
|
||||
}
|
||||
|
||||
private function buildOutcomeMedicalHistoryQuery(
|
||||
int $branchId,
|
||||
string $startDate,
|
||||
string $endDate,
|
||||
?array $visitResultIds = null
|
||||
) {
|
||||
$startDateOnly = Carbon::parse($startDate)->toDateString();
|
||||
$endDateOnly = Carbon::parse($endDate)->toDateString();
|
||||
|
||||
return MisMedicalHistory::query()
|
||||
->where('MedicalHistoryID', '<>', 0)
|
||||
->whereDate('DateExtract', '>', $startDateOnly)
|
||||
->whereDate('DateExtract', '<=', $endDateOnly)
|
||||
->whereHas('migrations', function ($migrationQuery) use ($branchId, $visitResultIds) {
|
||||
$migrationQuery->where('rf_StationarBranchID', $branchId);
|
||||
|
||||
if ($visitResultIds !== null && ! empty($visitResultIds)) {
|
||||
$migrationQuery->whereIn('rf_kl_VisitResultID', $visitResultIds);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Получить пациентов с операциями
|
||||
*/
|
||||
private function getSurgicalPatients(string $status, bool $isHeadOrAdmin, $branchId, $startDate, $endDate, bool $returnedCount = false)
|
||||
{
|
||||
$query = MisSurgicalOperation::where('rf_StationarBranchID', $branchId)
|
||||
->completed()
|
||||
// ->whereBetween('Date', [$startDate, $endDate])
|
||||
->where('Date', '>=', $startDate)
|
||||
->where('Date', '<=', $endDate)
|
||||
->orderBy('Date', 'DESC');
|
||||
|
||||
if ($status === 'plan') {
|
||||
$query->where('rf_TypeSurgOperationInTimeID', 6);
|
||||
} else {
|
||||
$query->whereIn('rf_TypeSurgOperationInTimeID', [4, 5]);
|
||||
}
|
||||
|
||||
if ($returnedCount) {
|
||||
return $query->count();
|
||||
} else {
|
||||
return $query->get();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Находятся на лечении
|
||||
*/
|
||||
private function getCurrentPatients($branchId, bool $returnedCount = false, bool $onlyIds = false)
|
||||
{
|
||||
// $currentCount = MisMedicalHistory::whereIn('MedicalHistoryID', $medicalHistoryIds)
|
||||
// ->currentlyHospitalized()
|
||||
// ->orderBy('DateRecipient', 'DESC')
|
||||
// ->count();
|
||||
$medicalHistoryIds = MisMigrationPatient::currentlyInTreatment($branchId)
|
||||
->pluck('rf_MedicalHistoryID')
|
||||
->unique()
|
||||
->toArray();
|
||||
|
||||
if (empty($medicalHistoryIds)) {
|
||||
if ($returnedCount) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return collect();
|
||||
}
|
||||
|
||||
if ($onlyIds) {
|
||||
return $medicalHistoryIds;
|
||||
}
|
||||
|
||||
$patients = MisMedicalHistory::whereIn('MedicalHistoryID', $medicalHistoryIds)
|
||||
->currentlyHospitalized()
|
||||
->with(['surgicalOperations'])
|
||||
->orderBy('DateRecipient', 'DESC');
|
||||
|
||||
if ($returnedCount) {
|
||||
return $patients->count();
|
||||
} else {
|
||||
return $patients->get();
|
||||
}
|
||||
}
|
||||
|
||||
public function removeObservation(
|
||||
Request $request,
|
||||
) {
|
||||
|
||||
Reference in New Issue
Block a user