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.
188 lines
5.8 KiB
188 lines
5.8 KiB
<?php
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
use App\Exports\AdminNoticeExport;
|
|
use App\Models\AdminNotice;
|
|
use App\Services\AdminNoticeService;
|
|
use App\Services\ApiResponseService;
|
|
use Exception;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Validator;
|
|
use Illuminate\Validation\ValidationException;
|
|
use Maatwebsite\Excel\Facades\Excel;
|
|
use Symfony\Component\HttpFoundation\BinaryFileResponse;
|
|
|
|
class NoticeController extends BaseController
|
|
{
|
|
|
|
protected AdminNoticeService $service;
|
|
|
|
/**
|
|
* 构造函数
|
|
* @param ApiResponseService $responseService
|
|
* @param AdminNoticeService $service
|
|
*/
|
|
public function __construct(
|
|
ApiResponseService $responseService,
|
|
AdminNoticeService $service
|
|
) {
|
|
parent::__construct($responseService);
|
|
$this->service = $service;
|
|
}
|
|
|
|
public function index(Request $request): JsonResponse
|
|
{
|
|
try {
|
|
$query = AdminNotice::query();
|
|
|
|
if ($request->has('alarm_type')) {
|
|
$alarm_type = $request->input('alarm_type');
|
|
if ($alarm_type) {
|
|
$query->where('alarm_type', $alarm_type);
|
|
}
|
|
}
|
|
|
|
if ($request->has('msg_type')) {
|
|
$msg_type = $request->input('msg_type');
|
|
if ($msg_type) {
|
|
$query->where('msg_type', $msg_type);
|
|
}
|
|
}
|
|
|
|
if ($request->has('start_time') && $request->has('end_time')) {
|
|
$start_time = $request->input('start_time');
|
|
$end_time = $request->input('end_time');
|
|
if ($start_time && $end_time) {
|
|
$query->whereBetween('alarm_time', [$start_time, $end_time]
|
|
);
|
|
}
|
|
}
|
|
|
|
// 分页
|
|
$page = $request->input('page', 1);
|
|
$perPage = $request->input('per_page', 10);
|
|
|
|
$columns = [
|
|
'id',
|
|
'alarm_time',
|
|
'alarm_type',
|
|
'camera_ip',
|
|
'msg_type',
|
|
'space_id'
|
|
];
|
|
$total = $query->count();
|
|
$items = $query->latest()->forPage($page, $perPage)->select(
|
|
$columns
|
|
)->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 search(): JsonResponse
|
|
{
|
|
try {
|
|
$data = [
|
|
'alarm_type_list' => get_select_data(
|
|
AdminNoticeService::getAlarmType()
|
|
),
|
|
'msg_type_list' => get_select_data(
|
|
AdminNoticeService::getMsgType()
|
|
)
|
|
];
|
|
return $this->responseService->success($data);
|
|
} catch (Exception $e) {
|
|
$m_prefix = __('exception.exception_handler.resource');
|
|
return $this->responseService->systemError(
|
|
$m_prefix . ':' . $e->getMessage()
|
|
);
|
|
}
|
|
}
|
|
|
|
public function setting(Request $request): JsonResponse
|
|
{
|
|
try {
|
|
$data = $request->all();
|
|
$rules = [
|
|
'close_alert' => 'required|in:0,1',
|
|
'close_all_alert' => 'required|in:0,1',
|
|
];
|
|
$messages = [
|
|
'close_alert.required' => __(
|
|
'validation.notice.c_empty'
|
|
),
|
|
'close_alert.numeric' => __(
|
|
'validation.notice.c_in'
|
|
),
|
|
'close_all_alert.required' => __(
|
|
'validation.notice.c_empty'
|
|
),
|
|
'close_all_alert.numeric' => __(
|
|
'validation.notice.c_in'
|
|
),
|
|
];
|
|
$validator = Validator::make($data, $rules, $messages);
|
|
|
|
if ($validator->fails()) {
|
|
throw new ValidationException($validator);
|
|
}
|
|
$this->service->updateConfig($data);
|
|
return $this->responseService->success([],
|
|
__('admin.operation_successful'));
|
|
} catch (Exception $e) {
|
|
$m_prefix = __('exception.exception_handler.resource');
|
|
return $this->responseService->systemError(
|
|
$m_prefix . ':' . $e->getMessage()
|
|
);
|
|
}
|
|
}
|
|
|
|
public function show(string $id): JsonResponse
|
|
{
|
|
try {
|
|
$columns = [
|
|
'id',
|
|
'alarm_time',
|
|
'alarm_type',
|
|
'camera_ip',
|
|
'msg_type',
|
|
'space_id',
|
|
'floor_id',
|
|
'camera_id'
|
|
];
|
|
$data = AdminNotice::query()->find($id, $columns)->toArray();
|
|
$data = $this->service->getItem($data);
|
|
return $this->responseService->success($data);
|
|
} catch (Exception $e) {
|
|
return $this->responseService->systemError(
|
|
__('exception.get_data_failed') . ':' . $e->getMessage()
|
|
);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @return BinaryFileResponse
|
|
*/
|
|
public function export(): BinaryFileResponse
|
|
{
|
|
return Excel::download(
|
|
new AdminNoticeExport(),
|
|
__('exports.notice.list') . time() . '.xlsx'
|
|
);
|
|
}
|
|
}
|
|
|