Форматирование
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
<?php
|
||||
|
||||
// app/Services/StatisticsService.php
|
||||
|
||||
namespace App\Services;
|
||||
@@ -7,19 +8,15 @@ use App\Models\Department;
|
||||
use App\Models\MetrikaResult;
|
||||
use App\Models\Report;
|
||||
use App\Models\User;
|
||||
use App\Factories\MetricCalculatorFactory;
|
||||
use App\Models\UserDepartment;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Collection;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class StatisticsService
|
||||
{
|
||||
public function __construct(
|
||||
protected BedDayService $bedDayService
|
||||
)
|
||||
{
|
||||
}
|
||||
) {}
|
||||
|
||||
public function getStatisticsData(User $user, string $startDate, string $endDate, bool $isRangeOneDay): array
|
||||
{
|
||||
@@ -32,10 +29,10 @@ class StatisticsService
|
||||
// 1. Получаем отделения
|
||||
$departments = Department::select('department_id', 'name_short', 'rf_department_type', 'user_name', 'order')
|
||||
->with('departmentType')
|
||||
->join((new UserDepartment)->getTable(), (new Department)->getTable() . '.department_id', (new UserDepartment)->getTable() . '.rf_department_id')
|
||||
->where((new UserDepartment)->getTable() . '.rf_user_id', $user->id)
|
||||
->join((new UserDepartment)->getTable(), (new Department)->getTable().'.department_id', (new UserDepartment)->getTable().'.rf_department_id')
|
||||
->where((new UserDepartment)->getTable().'.rf_user_id', $user->id)
|
||||
->orderBy('rf_department_type')
|
||||
->orderBy((new UserDepartment)->getTable() . '.order', 'asc')
|
||||
->orderBy((new UserDepartment)->getTable().'.order', 'asc')
|
||||
->get()
|
||||
->groupBy('departmentType.name_full');
|
||||
|
||||
@@ -46,17 +43,8 @@ class StatisticsService
|
||||
// Рассчитываем коэффициент периода (дни периода / 365)
|
||||
$start = Carbon::parse($startDate);
|
||||
$end = Carbon::parse($endDate);
|
||||
$monthsInPeriod = $start->diffInMonths($end); // +1 чтобы включить оба дня
|
||||
$periodCoefficient = $monthsInPeriod / 12;
|
||||
$monthsInPeriod = ceil($start->diffInMonths($end));
|
||||
|
||||
// foreach ($departments as $departmentType) {
|
||||
// foreach ($departmentType as $department) {
|
||||
// if ($department->recipientPlanOfYear() === null) continue;
|
||||
// $recipientPlanOfYear += (int)$department->recipientPlanOfYear()->value;
|
||||
// }
|
||||
// }
|
||||
|
||||
$allDeptIds = $departments->flatten()->pluck('department_id')->toArray();
|
||||
|
||||
// 2. Получаем ВСЕ метрики за период ОДНИМ запросом
|
||||
@@ -72,7 +60,6 @@ class StatisticsService
|
||||
DB::raw('SUM(CAST(mr.value AS DECIMAL)) as total'),
|
||||
DB::raw('COUNT(*) as records_count')
|
||||
)
|
||||
->whereIn('mr.rf_metrika_item_id', [1, 4, 12, 11, 10, 13, 7, 9, 17, 14, 16, 18, 19, 22])
|
||||
->groupBy('r.rf_department_id', 'mr.rf_metrika_item_id')
|
||||
->get()
|
||||
->groupBy('rf_department_id');
|
||||
@@ -123,18 +110,16 @@ class StatisticsService
|
||||
->first();
|
||||
|
||||
// Базовые показатели
|
||||
$bedsCount = (int)($beds[$deptId]->value ?? 0);
|
||||
$currentCount = (int)($currentPatients[$deptId]->value ?? 0);
|
||||
$bedsCount = (int) ($beds[$deptId]->value ?? 0);
|
||||
$currentCount = (int) ($currentPatients[$deptId]->value ?? 0);
|
||||
|
||||
// Получаем годовой план
|
||||
$annualPlanModel = $dept->recipientPlanOfYear();
|
||||
// $annualPlan = $annualPlanModel ? (int)$annualPlanModel->value : 0;
|
||||
$annualPlan = $annualPlanModel ? (int)$annualPlanModel->value : 0;
|
||||
$annualPlan = $annualPlanModel ? (int) $annualPlanModel->value : 0;
|
||||
$oneMonthPlan = ceil($annualPlan / 12);
|
||||
|
||||
// Рассчитываем план на период
|
||||
$periodPlan = round($oneMonthPlan * $monthsInPeriod);
|
||||
// $periodPlan = round($annualPlan * $periodCoefficient);
|
||||
|
||||
// Счетчики
|
||||
$plan = 0;
|
||||
@@ -149,25 +134,27 @@ class StatisticsService
|
||||
$unwanted = 0;
|
||||
$bedDaysSum = 0;
|
||||
$avgBedDays = 0;
|
||||
$preoperativeSum = 0;
|
||||
|
||||
if (isset($metrics[$deptId])) {
|
||||
foreach ($metrics[$deptId] as $item) {
|
||||
$value = (float)$item->total;
|
||||
$value = (float) $item->total;
|
||||
|
||||
match ($item->rf_metrika_item_id) {
|
||||
4 => $plan = (int)$value,
|
||||
12 => $emergency = (int)$value,
|
||||
11 => $planSurgical = (int)$value,
|
||||
10 => $emergencySurgical = (int)$value,
|
||||
13 => $transferred = (int)$value,
|
||||
7 => $outcome = (int)$value,
|
||||
9 => $deceased = (int)$value,
|
||||
17 => $staff = (int)$value,
|
||||
14 => $observable = (int)$value,
|
||||
16 => $unwanted = (int)$value,
|
||||
18 => $bedDaysSum += $value,
|
||||
4 => $plan = (int) $value,
|
||||
12 => $emergency = (int) $value,
|
||||
11 => $planSurgical = (int) $value,
|
||||
10 => $emergencySurgical = (int) $value,
|
||||
13 => $transferred = (int) $value,
|
||||
7 => $outcome = (int) $value,
|
||||
9 => $deceased = (int) $value,
|
||||
17 => $staff = (int) $value,
|
||||
14 => $observable = (int) $value,
|
||||
16 => $unwanted = (int) $value,
|
||||
25 => $bedDaysSum += $value,
|
||||
19 => $lethalitySum = $value,
|
||||
// 24 => $completePlanProgress = (int)$value,
|
||||
26 => $preoperativeSum += $value,
|
||||
// 24 => $completePlanProgress = (int)$value,
|
||||
default => null
|
||||
};
|
||||
}
|
||||
@@ -186,11 +173,7 @@ class StatisticsService
|
||||
$avgBedDays = $outcome > 0 ? round($bedDaysSum / $outcome, 2) : 0;
|
||||
|
||||
// Предоперационный койко-день
|
||||
$preoperativeValue = $lastReport
|
||||
? (float)MetrikaResult::where('rf_report_id', $lastReport->report_id)
|
||||
->where('rf_metrika_item_id', 21)
|
||||
->value('value')
|
||||
: 0;
|
||||
$preoperativeValue =
|
||||
|
||||
// Летальность
|
||||
$lethality = $outcome > 0 ? round(($deceased / $outcome) * 100, 2) : 0;
|
||||
@@ -214,7 +197,7 @@ class StatisticsService
|
||||
'percentLoadedBeds' => $percentLoaded,
|
||||
'surgical' => [
|
||||
'plan' => $planSurgical,
|
||||
'emergency' => $emergencySurgical
|
||||
'emergency' => $emergencySurgical,
|
||||
],
|
||||
'deceased' => $deceased,
|
||||
'countStaff' => $staff,
|
||||
@@ -242,7 +225,7 @@ class StatisticsService
|
||||
'recipientPlanOfYear' => [
|
||||
'plan' => $grandRecipientPlan, // Сумма планов по периоду
|
||||
'progress' => $grandProgressPlan, // Сумма фактов по периоду
|
||||
]
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
@@ -307,6 +290,7 @@ class StatisticsService
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $grand;
|
||||
}
|
||||
|
||||
@@ -321,14 +305,14 @@ class StatisticsService
|
||||
$final[] = [
|
||||
'isGroupHeader' => true,
|
||||
'groupName' => $type,
|
||||
'colspan' => 16
|
||||
'colspan' => 16,
|
||||
];
|
||||
|
||||
foreach ($items as $item) {
|
||||
$final[] = $item;
|
||||
}
|
||||
|
||||
if (!empty($items) && isset($totalsByType[$type])) {
|
||||
if (! empty($items) && isset($totalsByType[$type])) {
|
||||
$final[] = $this->createTotalRow($type, $totalsByType[$type], false);
|
||||
}
|
||||
}
|
||||
@@ -342,7 +326,7 @@ class StatisticsService
|
||||
private function createTotalRow(string $type, array $total, bool $isGrandTotal): array
|
||||
{
|
||||
return [
|
||||
'isTotalRow' => !$isGrandTotal,
|
||||
'isTotalRow' => ! $isGrandTotal,
|
||||
'isGrandTotal' => $isGrandTotal,
|
||||
'department' => $isGrandTotal ? 'ОБЩИЕ ИТОГИ:' : 'ИТОГО:',
|
||||
'beds' => '—',
|
||||
@@ -357,7 +341,7 @@ class StatisticsService
|
||||
'percentLoadedBeds' => '—',
|
||||
'surgical' => [
|
||||
'plan' => $total['plan_surgical_sum'],
|
||||
'emergency' => $total['emergency_surgical_sum']
|
||||
'emergency' => $total['emergency_surgical_sum'],
|
||||
],
|
||||
'deceased' => $total['deceased_sum'],
|
||||
'averageBedDays' => '—',
|
||||
@@ -368,7 +352,7 @@ class StatisticsService
|
||||
'countStaff' => $total['staff_sum'],
|
||||
'countObservable' => $total['observable_sum'],
|
||||
'countUnwanted' => $total['unwanted_sum'],
|
||||
'isBold' => true
|
||||
'isBold' => true,
|
||||
];
|
||||
}
|
||||
|
||||
@@ -380,7 +364,7 @@ class StatisticsService
|
||||
return [
|
||||
'data' => [],
|
||||
'totalsByType' => [],
|
||||
'grandTotals' => $this->initTypeTotals()
|
||||
'grandTotals' => $this->initTypeTotals(),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user