You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
138 lines
4.3 KiB
138 lines
4.3 KiB
<?php
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
use App\Exports\UtilizationRateExport;
|
|
use App\Models\AdminFloor;
|
|
use App\Models\ParkingSpace;
|
|
use App\Models\ParkingSpaceType;
|
|
use App\Models\ParkingUtilizationRate;
|
|
use App\Services\ApiResponseService;
|
|
use App\Services\UtilizationRateService;
|
|
use Exception;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
use Maatwebsite\Excel\Facades\Excel;
|
|
use Symfony\Component\HttpFoundation\BinaryFileResponse;
|
|
|
|
class UtilizationRateController extends BaseController
|
|
{
|
|
protected string $menuUri = 'utilizationRate';
|
|
|
|
/**
|
|
* @var UtilizationRateService
|
|
*/
|
|
protected UtilizationRateService $service;
|
|
|
|
/**
|
|
* 构造函数
|
|
* @param ApiResponseService $responseService
|
|
* @param UtilizationRateService $service
|
|
*/
|
|
public function __construct(
|
|
ApiResponseService $responseService,
|
|
UtilizationRateService $service
|
|
) {
|
|
parent::__construct($responseService);
|
|
$this->service = $service;
|
|
}
|
|
|
|
public function index(Request $request): JsonResponse
|
|
{
|
|
try {
|
|
$query = $this->service->searchQuery($request->all());
|
|
|
|
// 分页
|
|
$page = $request->input('page', 1);
|
|
$perPage = $request->input('per_page', 10);
|
|
|
|
$total = $query->count();
|
|
$items = $query->latest()->forPage($page, $perPage)->select()->get()
|
|
->each(function ($item) {
|
|
return $this->service->getItem($item);
|
|
});
|
|
return $this->responseService->success([
|
|
'items' => $items,
|
|
'total' => $total,
|
|
'page' => $page,
|
|
'per_page' => $perPage,
|
|
'last_page' => ceil($total / $perPage)
|
|
]);
|
|
} catch (Exception $e) {
|
|
$m_prefix = __('exception.exception_handler.resource');
|
|
return $this->responseService->systemError(
|
|
$m_prefix . ':' . $e->getMessage()
|
|
);
|
|
}
|
|
}
|
|
|
|
public function illustration(Request $request): JsonResponse
|
|
{
|
|
try {
|
|
$data = [];
|
|
$query = $this->service->searchQuery($request->all());
|
|
$list = $query->select()->get()->toArray();
|
|
$tempData = [];
|
|
foreach ($list as $item) {
|
|
$date_type = $item['date_type'];
|
|
$format = 'H:i';
|
|
if ($date_type == 2) {
|
|
$format = 'm/d';
|
|
}
|
|
$key = date($format, strtotime($item['end_time']));
|
|
$tempData[$key][] = $item['rate'];
|
|
}
|
|
foreach ($tempData as $date => $value) {
|
|
$count = count($value);
|
|
$sum = 0;
|
|
foreach ($value as $val) {
|
|
$sum += $val;
|
|
}
|
|
$rate = 0;
|
|
if ($sum > 0) {
|
|
$rate = round($sum / $count, 2);
|
|
}
|
|
$data[] = [
|
|
'time' => $date,
|
|
'rate' => $rate
|
|
];
|
|
}
|
|
return $this->responseService->success($data);
|
|
} catch (Exception $e) {
|
|
$m_prefix = __('exception.exception_handler.resource');
|
|
return $this->responseService->systemError(
|
|
$m_prefix . ':' . $e->getMessage()
|
|
);
|
|
}
|
|
}
|
|
|
|
public function search(): JsonResponse
|
|
{
|
|
try {
|
|
$data = [
|
|
'floor_list' => AdminFloor::getData(),
|
|
'space_type_list' => ParkingSpaceType::getData()
|
|
];
|
|
return $this->responseService->success($data);
|
|
} catch (Exception $e) {
|
|
$m_prefix = __('exception.exception_handler.resource');
|
|
return $this->responseService->systemError(
|
|
$m_prefix . ':' . $e->getMessage()
|
|
);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param Request $request
|
|
* @return BinaryFileResponse
|
|
*/
|
|
public function export(Request $request): BinaryFileResponse
|
|
{
|
|
return Excel::download(
|
|
new UtilizationRateExport($request->all()),
|
|
set_space_underline(__('exports.access_record.list'))
|
|
. get_datetime('date_')
|
|
. '.xlsx'
|
|
);
|
|
}
|
|
}
|
|
|