* исправил фантомный сдвиг даты * переделал получение ФИО врачей из отделений * добавил возможность поиска врача * переписал сохранение отчета
158 lines
4.9 KiB
PHP
158 lines
4.9 KiB
PHP
<?php
|
||
|
||
namespace App\Http\Controllers\Web;
|
||
|
||
use App\Http\Controllers\Controller;
|
||
use App\Http\Resources\Mis\FormattedPatientResource;
|
||
use App\Models\MetrikaGroup;
|
||
use App\Models\MisLpuDoctor;
|
||
use App\Models\Report;
|
||
use App\Models\UnwantedEvent;
|
||
use App\Services\DateRangeService;
|
||
use App\Services\ReportService;
|
||
use Illuminate\Http\Request;
|
||
use Illuminate\Support\Carbon;
|
||
use Illuminate\Support\Facades\Auth;
|
||
use Inertia\Inertia;
|
||
|
||
class ReportController extends Controller
|
||
{
|
||
public function __construct(
|
||
protected ReportService $reportService,
|
||
protected DateRangeService $dateRangeService
|
||
) {}
|
||
|
||
public function index(Request $request)
|
||
{
|
||
$user = Auth::user();
|
||
$department = $user->department;
|
||
|
||
$dateRange = $this->dateRangeService->getDateRangeFromRequest($request, $user);
|
||
|
||
// Получаем статистику
|
||
$statistics = $this->reportService->getReportStatistics($user, $dateRange);
|
||
|
||
// Получаем метрики
|
||
$metrikaGroup = MetrikaGroup::whereMetrikaGroupId(2)->first();
|
||
$metrikaItems = $metrikaGroup->metrikaItems;
|
||
|
||
// Получаем информацию о текущем отчете
|
||
$reportInfo = $this->reportService->getCurrentReportInfo($user, $dateRange);
|
||
|
||
return Inertia::render('Report/Index', [
|
||
'department' => [
|
||
'beds' => $department->beds,
|
||
'percentLoadedBeds' => $this->calculateBedOccupancy($department, $user),
|
||
...$statistics,
|
||
],
|
||
'dates' => [
|
||
'startAt' => $dateRange->startTimestamp(),
|
||
'endAt' => $dateRange->endTimestamp()
|
||
],
|
||
'report' => $reportInfo,
|
||
'metrikaItems' => $metrikaItems,
|
||
'userId' => $reportInfo['userId'],
|
||
'userName' => $reportInfo['userName']
|
||
]);
|
||
}
|
||
|
||
public function store(Request $request)
|
||
{
|
||
$validated = $request->validate([
|
||
'metrics' => 'required|array',
|
||
'observationPatients' => 'nullable|array',
|
||
'departmentId' => 'required|integer',
|
||
'unwantedEvents' => 'nullable|array',
|
||
'dates' => 'required|array',
|
||
'userId' => 'required|integer',
|
||
'reportId' => 'nullable|integer'
|
||
]);
|
||
|
||
$report = $this->reportService->storeReport($validated, Auth::user(), false);
|
||
|
||
return redirect()->route('start');
|
||
}
|
||
|
||
public function getPatients(Request $request)
|
||
{
|
||
$user = Auth::user();
|
||
|
||
$validated = $request->validate([
|
||
'status' => 'required|string',
|
||
'startAt' => 'nullable',
|
||
'endAt' => 'nullable',
|
||
]);
|
||
|
||
$dateRange = $this->dateRangeService->getDateRangeFromRequest($request, $user);
|
||
|
||
$patients = $this->reportService->getPatientsByStatus(
|
||
Auth::user(),
|
||
$validated['status'],
|
||
$dateRange
|
||
);
|
||
|
||
return response()->json(FormattedPatientResource::collection($patients));
|
||
}
|
||
|
||
public function getPatientsCount(Request $request)
|
||
{
|
||
$user = Auth::user();
|
||
|
||
$validated = $request->validate([
|
||
'status' => 'required|string',
|
||
'startAt' => 'nullable',
|
||
'endAt' => 'nullable',
|
||
]);
|
||
|
||
$dateRange = $this->dateRangeService->getDateRangeFromRequest($request, $user);
|
||
|
||
$count = $this->reportService->getPatientsCountByStatus(
|
||
Auth::user(),
|
||
$validated['status'],
|
||
$dateRange,
|
||
);
|
||
|
||
return response()->json($count);
|
||
}
|
||
|
||
public function removeObservation(Request $request)
|
||
{
|
||
$validated = $request->validate(['id' => 'required|integer']);
|
||
|
||
$this->reportService->removeObservationPatient($validated['id']);
|
||
|
||
return response()->json(['message' => 'Удалено'], 200);
|
||
}
|
||
|
||
public function removeUnwantedEvent(UnwantedEvent $unwantedEvent)
|
||
{
|
||
$unwantedEvent->delete();
|
||
return response()->json(['message' => 'Удалено'], 200);
|
||
}
|
||
|
||
public function getDepartmentUsers()
|
||
{
|
||
$users = MisLpuDoctor::select(['LPUDoctorID', 'FAM_V', 'IM_V', 'OT_V'])
|
||
->active()
|
||
->inMyDepartment()
|
||
->get();
|
||
|
||
return response()->json($users, 200);
|
||
}
|
||
|
||
/**
|
||
* Рассчитать загруженность коек
|
||
*/
|
||
private function calculateBedOccupancy($department, $user): int
|
||
{
|
||
$beds = (int)$department->metrikaDefault()->where('rf_metrika_item_id', 1)->first()->value;
|
||
$occupiedBeds = optional(Report::where('rf_department_id', $user->rf_department_id)
|
||
->join('metrika_results', 'reports.report_id', '=', 'metrika_results.rf_report_id')
|
||
->where('metrika_results.rf_metrika_item_id', 8)
|
||
->orderBy('sent_at', 'desc')
|
||
->first())->value ?? 0;
|
||
|
||
return $beds > 0 ? round(intval($occupiedBeds) * 100 / $beds) : 0;
|
||
}
|
||
}
|