96 lines
3.1 KiB
PHP
96 lines
3.1 KiB
PHP
<?php
|
||
|
||
namespace App\Http\Controllers;
|
||
|
||
use App\Services\ApiService;
|
||
use Illuminate\Http\Request;
|
||
|
||
class StatisticController extends Controller
|
||
{
|
||
protected ApiService $api;
|
||
|
||
public function __construct(ApiService $api)
|
||
{
|
||
$this->api = $api;
|
||
}
|
||
|
||
public function index()
|
||
{
|
||
$groups = $this->api->get('/employee/groups')->json();
|
||
$directions = $this->api->get('/employee/directions')->json();
|
||
$teachers = $this->api->get('/employee/teachers')->json();
|
||
|
||
return view('statistics.index', [
|
||
'groups' => $groups,
|
||
'directions' => $directions,
|
||
'teachers' => $teachers,
|
||
'statistics' => request()->has('type') ? $this->getStatistics(request()) : null
|
||
]);
|
||
}
|
||
|
||
public function generate(Request $request)
|
||
{
|
||
$validated = $request->validate([
|
||
'type' => 'required|string|in:group,direction,teacher',
|
||
'id' => 'required|integer',
|
||
'start_date' => 'required|date',
|
||
'end_date' => 'required|date|after_or_equal:start_date',
|
||
]);
|
||
|
||
if ($validated['type'] === 'teacher') {
|
||
$teachersResponse = $this->api->get('/employee/teachers');
|
||
|
||
if (!$teachersResponse->successful()) {
|
||
return back()->withErrors('Не удалось получить список преподавателей');
|
||
}
|
||
|
||
$teachers = $teachersResponse->json()['data'] ?? [];
|
||
|
||
$teacher = collect($teachers)->firstWhere('teacher_id', $validated['id']);
|
||
|
||
if (!$teacher) {
|
||
return back()->withErrors('Преподаватель не найден');
|
||
}
|
||
|
||
$validated['id'] = $teacher['user_id'];
|
||
}
|
||
|
||
$response = $this->api->get('/employee/statements/statistics', $validated);
|
||
|
||
if ($response->successful()) {
|
||
$content = $response->body();
|
||
$contentType = $response->header('Content-Type');
|
||
$contentDisposition = $response->header('Content-Disposition');
|
||
|
||
return response($content)
|
||
->header('Content-Type', $contentType)
|
||
->header('Content-Disposition', $contentDisposition);
|
||
}
|
||
|
||
return back()->withErrors($response->json()['error'] ?? 'Ошибка при генерации статистики');
|
||
}
|
||
|
||
/**
|
||
* Получает статистику через API на основе параметров запроса.
|
||
*/
|
||
protected function getStatistics(Request $request): array
|
||
{
|
||
$validated = $request->validate([
|
||
'type' => 'required|string|in:group,direction,teacher',
|
||
'id' => 'required|integer',
|
||
'start_date' => 'required|date',
|
||
'end_date' => 'required|date|after_or_equal:start_date',
|
||
]);
|
||
|
||
$response = $this->api->get('/employee/statistics', $validated);
|
||
|
||
if ($response->successful()) {
|
||
return $response->json();
|
||
}
|
||
|
||
return [
|
||
'error' => $response->json()['error'] ?? 'Не удалось загрузить статистику'
|
||
];
|
||
}
|
||
}
|