Изменения в основном report
This commit is contained in:
@@ -4,6 +4,8 @@ namespace App\Infrastructure\Reports\Services;
|
||||
|
||||
use App\Domain\Reports\ValueObjects\MetrikaConfig;
|
||||
use App\Models\Department;
|
||||
use App\Models\MedicalHistory;
|
||||
use App\Models\MigrationPatient;
|
||||
use App\Models\MisMedicalHistory;
|
||||
use App\Models\MisStationarBranch;
|
||||
use App\Models\User;
|
||||
@@ -11,6 +13,7 @@ use App\Services\DateRange;
|
||||
use App\Services\PatientService;
|
||||
use App\Services\UnifiedPatientService;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
class AutoFillReportPayloadBuilder
|
||||
{
|
||||
@@ -75,19 +78,35 @@ class AutoFillReportPayloadBuilder
|
||||
$deceasedCodes = [5, 6, 15, 16];
|
||||
$transferCodes = [4, 14];
|
||||
|
||||
$planRecipient = (clone $recipientQuery)
|
||||
->where('rf_EmerSignID', 1)
|
||||
->distinct()
|
||||
->count('MedicalHistoryID');
|
||||
if ($this->useMaterializedViews()) {
|
||||
$planRecipient = (clone $recipientQuery)
|
||||
->where('urgency_id', 1)
|
||||
->distinct()
|
||||
->count('original_id');
|
||||
|
||||
$emergencyRecipient = (clone $recipientQuery)
|
||||
->whereIn('rf_EmerSignID', [2, 4])
|
||||
->distinct()
|
||||
->count('MedicalHistoryID');
|
||||
$emergencyRecipient = (clone $recipientQuery)
|
||||
->whereIn('urgency_id', [2, 4])
|
||||
->distinct()
|
||||
->count('original_id');
|
||||
|
||||
$recipientTotal = (clone $recipientQuery)
|
||||
->distinct()
|
||||
->count('MedicalHistoryID');
|
||||
$recipientTotal = (clone $recipientQuery)
|
||||
->distinct()
|
||||
->count('original_id');
|
||||
} else {
|
||||
$planRecipient = (clone $recipientQuery)
|
||||
->where('rf_EmerSignID', 1)
|
||||
->distinct()
|
||||
->count('MedicalHistoryID');
|
||||
|
||||
$emergencyRecipient = (clone $recipientQuery)
|
||||
->whereIn('rf_EmerSignID', [2, 4])
|
||||
->distinct()
|
||||
->count('MedicalHistoryID');
|
||||
|
||||
$recipientTotal = (clone $recipientQuery)
|
||||
->distinct()
|
||||
->count('MedicalHistoryID');
|
||||
}
|
||||
|
||||
$discharged = $this->countOutcomeByVisitResultIds($branchId, $dateRange, $dischargeCodes);
|
||||
$deceased = $this->countOutcomeByVisitResultIds($branchId, $dateRange, $deceasedCodes);
|
||||
@@ -108,6 +127,13 @@ class AutoFillReportPayloadBuilder
|
||||
|
||||
private function buildRecipientMedicalHistoryQuery(int $branchId, DateRange $dateRange)
|
||||
{
|
||||
if ($this->useMaterializedViews()) {
|
||||
return MedicalHistory::query()
|
||||
->whereHas('migrations', fn ($query) => $query
|
||||
->where('stationar_branch_id', $branchId)
|
||||
->admitted($dateRange->startSql(), $dateRange->endSql()));
|
||||
}
|
||||
|
||||
$startAt = $dateRange->start()->copy()->subDay()->format('Y-m-d H:i:s');
|
||||
$endAt = $dateRange->end()->copy()->addDay()->format('Y-m-d H:i:s');
|
||||
|
||||
@@ -130,6 +156,13 @@ class AutoFillReportPayloadBuilder
|
||||
|
||||
private function buildTreatedMedicalHistoryQuery(int $branchId, DateRange $dateRange)
|
||||
{
|
||||
if ($this->useMaterializedViews()) {
|
||||
return MedicalHistory::query()
|
||||
->whereHas('migrations', fn ($query) => $query
|
||||
->where('stationar_branch_id', $branchId)
|
||||
->dateRange($dateRange->startSql(), $dateRange->endSql()));
|
||||
}
|
||||
|
||||
$query = MisMedicalHistory::query()
|
||||
->where('MedicalHistoryID', '<>', 0)
|
||||
->whereExists(function ($query) use ($branchId) {
|
||||
@@ -155,6 +188,24 @@ class AutoFillReportPayloadBuilder
|
||||
|
||||
private function countOutcomeByVisitResultIds(int $branchId, DateRange $dateRange, array $visitResultIds): int
|
||||
{
|
||||
if ($this->useMaterializedViews()) {
|
||||
$query = MigrationPatient::query()
|
||||
->where('stationar_branch_id', $branchId);
|
||||
|
||||
if ($visitResultIds === [1, 11, 2, 12, 7, 18, 48]) {
|
||||
$query->discharged($dateRange->startSql(), $dateRange->endSql());
|
||||
} elseif ($visitResultIds === [5, 6, 15, 16]) {
|
||||
$query->deceased($dateRange->startSql(), $dateRange->endSql());
|
||||
} elseif ($visitResultIds === [4, 14]) {
|
||||
$query->transferred($dateRange->startSql(), $dateRange->endSql());
|
||||
} else {
|
||||
$query->whereIn('visit_result_id', $visitResultIds)
|
||||
->dateRange($dateRange->startSql(), $dateRange->endSql());
|
||||
}
|
||||
|
||||
return $query->distinct('medical_history_id')->count('medical_history_id');
|
||||
}
|
||||
|
||||
return $this->buildTreatedMedicalHistoryQuery($branchId, $dateRange)
|
||||
->whereExists(function ($query) use ($branchId, $visitResultIds) {
|
||||
$query->select(DB::raw(1))
|
||||
@@ -172,4 +223,9 @@ class AutoFillReportPayloadBuilder
|
||||
return MisStationarBranch::where('rf_DepartmentID', $misDepartmentId)
|
||||
->value('StationarBranchID');
|
||||
}
|
||||
|
||||
private function useMaterializedViews(): bool
|
||||
{
|
||||
return Schema::hasTable('mv_medicalhistory_summary') && Schema::hasTable('mv_migrationpatient_details');
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user