126 lines
3.1 KiB
PHP
126 lines
3.1 KiB
PHP
<?php
|
|
|
|
namespace App\Services\System;
|
|
|
|
use App\Models\System\Log;
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
class LogService
|
|
{
|
|
public function create(array $data): Log
|
|
{
|
|
return Log::create($data);
|
|
}
|
|
|
|
public function getList(array $params): array
|
|
{
|
|
$query = $this->buildQuery($params);
|
|
|
|
$query->orderBy('created_at', 'desc');
|
|
|
|
$pageSize = $params['page_size'] ?? 20;
|
|
$list = $query->paginate($pageSize);
|
|
|
|
return [
|
|
'list' => $list->items(),
|
|
'total' => $list->total(),
|
|
'page' => $list->currentPage(),
|
|
'page_size' => $list->perPage(),
|
|
];
|
|
}
|
|
|
|
/**
|
|
* 构建查询(用于导出等场景)
|
|
*
|
|
* @param array $params
|
|
* @return \Illuminate\Database\Eloquent\Builder
|
|
*/
|
|
public function getListQuery(array $params)
|
|
{
|
|
return $this->buildQuery($params);
|
|
}
|
|
|
|
/**
|
|
* 构建基础查询
|
|
*
|
|
* @param array $params
|
|
* @return \Illuminate\Database\Eloquent\Builder
|
|
*/
|
|
protected function buildQuery(array $params)
|
|
{
|
|
$query = Log::query()->with('user:id,name,username');
|
|
|
|
if (!empty($params['user_id'])) {
|
|
$query->where('user_id', $params['user_id']);
|
|
}
|
|
|
|
if (!empty($params['username'])) {
|
|
$query->where('username', 'like', '%' . $params['username'] . '%');
|
|
}
|
|
|
|
if (!empty($params['module'])) {
|
|
$query->where('module', $params['module']);
|
|
}
|
|
|
|
if (!empty($params['action'])) {
|
|
$query->where('action', $params['action']);
|
|
}
|
|
|
|
if (!empty($params['status'])) {
|
|
$query->where('status', $params['status']);
|
|
}
|
|
|
|
if (!empty($params['start_date']) && !empty($params['end_date'])) {
|
|
$query->whereBetween('created_at', [$params['start_date'], $params['end_date']]);
|
|
}
|
|
|
|
if (!empty($params['ip'])) {
|
|
$query->where('ip', 'like', '%' . $params['ip'] . '%');
|
|
}
|
|
|
|
return $query;
|
|
}
|
|
|
|
public function getById(int $id): ?Log
|
|
{
|
|
return Log::with('user')->find($id);
|
|
}
|
|
|
|
public function delete(int $id): bool
|
|
{
|
|
$log = Log::findOrFail($id);
|
|
return $log->delete();
|
|
}
|
|
|
|
public function clearLogs(string $days = '30'): bool
|
|
{
|
|
Log::where('created_at', '<', now()->subDays($days))->delete();
|
|
return true;
|
|
}
|
|
|
|
public function batchDelete(array $ids): bool
|
|
{
|
|
Log::whereIn('id', $ids)->delete();
|
|
return true;
|
|
}
|
|
|
|
public function getStatistics(array $params = []): array
|
|
{
|
|
$query = Log::query();
|
|
|
|
if (!empty($params['start_date']) && !empty($params['end_date'])) {
|
|
$query->whereBetween('created_at', [$params['start_date'], $params['end_date']]);
|
|
}
|
|
|
|
$total = $query->count();
|
|
$successCount = (clone $query)->where('status', 'success')->count();
|
|
$errorCount = (clone $query)->where('status', 'error')->count();
|
|
|
|
return [
|
|
'total' => $total,
|
|
'success' => $successCount,
|
|
'error' => $errorCount,
|
|
];
|
|
}
|
|
}
|