diff --git a/app/Services/ReportService.php b/app/Services/ReportService.php index 2a519ca..f4a1fd9 100644 --- a/app/Services/ReportService.php +++ b/app/Services/ReportService.php @@ -15,6 +15,7 @@ use App\Models\UnwantedEvent; use App\Models\User; use Carbon\Carbon; use Illuminate\Support\Facades\Auth; +use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\DB; class ReportService @@ -22,7 +23,8 @@ class ReportService public function __construct( protected DateRangeService $dateRangeService, protected PatientService $patientQueryService, - protected SnapshotService $snapshotService + protected SnapshotService $snapshotService, + protected StatisticsService $statisticsService ) {} /** @@ -62,6 +64,10 @@ class ReportService $this->snapshotService->createPatientSnapshots($report, $user, $data['dates'], $fillableAuto); DB::commit(); + + // ОЧИСТКА КЭША ПОСЛЕ УСПЕШНОГО СОЗДАНИЯ ОТЧЕТА + $this->clearCacheAfterReportCreation($user, $report); + return $report; } catch (\Exception $e) { DB::rollBack(); @@ -69,6 +75,42 @@ class ReportService } } + /** + * Очистить кэш после создания отчета + */ + private function clearCacheAfterReportCreation(User $user, Report $report): void + { + // Очищаем кэш статистики для пользователя + $this->statisticsService->clearStatisticsCache($user); + + // Также можно очистить кэш для всех пользователей отдела + $this->statisticsService->clearDepartmentStatisticsCache($user->rf_department_id); + + // Очищаем кэш за сегодня и вчера (так как отчеты влияют на эти даты) + $this->clearDailyCache($user, $report->created_at); + } + + /** + * Очистить дневной кэш + */ + private function clearDailyCache(User $user, $reportDate): void + { + $datesToClear = [ + Carbon::parse($reportDate)->format('Y-m-d'), + Carbon::parse($reportDate)->subDay()->format('Y-m-d'), + ]; + + foreach ($datesToClear as $date) { + $cacheKey = $this->generateDailyCacheKey($user, $date); + Cache::forget($cacheKey); + } + } + + private function generateDailyCacheKey(User $user, string $date): string + { + return 'daily_stats:' . $user->rf_department_id . ':' . $date; + } + /** * Получить пациентов по статусу */ @@ -418,7 +460,7 @@ class ReportService $outcomeCount = $this->patientQueryService->getOutcomePatients( $branchId, $dateRange, - 'all' + 'without-transferred' )->count(); // Умершие за период @@ -642,7 +684,7 @@ class ReportService 'outcome' => $this->patientQueryService->getOutcomePatients( $branchId, $dateRange, - 'all' + 'without-transferred' )->count(), 'outcome-discharged' => $this->patientQueryService->getOutcomePatients( $branchId,