60 lines
2.2 KiB
PHP
60 lines
2.2 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Web;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\Department;
|
|
use App\Models\MetrikaForm;
|
|
use App\Models\MetrikaGroup;
|
|
use App\Models\MetrikaItem;
|
|
use App\Models\MetrikaResult;
|
|
use App\Models\Report;
|
|
use App\Services\DateRangeService;
|
|
use App\Services\StatisticsService;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\Cache;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Validator;
|
|
use Inertia\Inertia;
|
|
|
|
class StatisticController extends Controller
|
|
{
|
|
public function __construct(
|
|
protected DateRangeService $dateService,
|
|
protected StatisticsService $statisticsService
|
|
) { }
|
|
|
|
public function index(Request $request)
|
|
{
|
|
$user = $request->user();
|
|
|
|
$queryStartDate = $request->query('startAt');
|
|
$queryEndDate = $request->query('endAt');
|
|
[$startDate, $endDate] = $this->dateService->getStatisticsDateRange($user, $queryStartDate, $queryEndDate);
|
|
$isRangeOneDay = $this->dateService->isRangeOneDay($startDate, $endDate);
|
|
|
|
// Генерируем ключ кэша на основе параметров запроса
|
|
// $cacheKey = $this->generateCacheKey($user, $startDate, $endDate, $isRangeOneDay);
|
|
|
|
// Получаем данные из кэша или вычисляем
|
|
$finalData = $this->statisticsService->getStatisticsData($user, $startDate, $endDate, $isRangeOneDay);
|
|
|
|
$isHeadOrAdmin = $user->isAdmin() || $user->isHeadOfDepartment();
|
|
$date = $isHeadOrAdmin ? [
|
|
$this->dateService->parseDate($isRangeOneDay ? $endDate : $startDate)->getTimestampMs(),
|
|
$this->dateService->parseDate($endDate)->getTimestampMs()
|
|
] : $this->dateService->parseDate($endDate)->getTimestampMs();
|
|
|
|
return Inertia::render('Statistic/Index', [
|
|
'data' => $finalData['data'],
|
|
'totalsByType' => $finalData['totalsByType'],
|
|
'grandTotals' => $finalData['grandTotals'],
|
|
'isHeadOrAdmin' => $isHeadOrAdmin,
|
|
'date' => $date,
|
|
'isOneDay' => $isRangeOneDay,
|
|
]);
|
|
}
|
|
}
|