diff --git a/src/YhCore.Service/Impl/LogService.cs b/src/YhCore.Service/Impl/LogService.cs new file mode 100644 index 0000000..7583afd --- /dev/null +++ b/src/YhCore.Service/Impl/LogService.cs @@ -0,0 +1,53 @@ +using System.Threading.Tasks; +using YhCore.Entity; +using YhCore.Model.Vo; +using YhCore.Repository; +using YhCore.Service.Interfaces; + +namespace YhCore.Service.Impl +{ + /// + /// 日志查询服务(对应 Java 版 LogServiceImpl,替换框架期 LogServiceStub)。 + /// 查询 sys_operation_log:按学校隔离;systemScope=false 排除查询类(GET=LogLevel 3); + /// keyword 模糊匹配用户名/URI/类方法/请求参数/异常/操作类型;按 CreateTime 倒序分页。 + /// + public class LogService : ILogService + { + /// 查询类日志级别(与 SysOperationLogFilter 推导一致:GET → 3/QUERY) + private const int QueryLogLevel = 3; + + /// 每页大小上限(审计日志量大,防一次拉全表) + private const int MaxPageSize = 200; + + private readonly IRepository _log; + + public LogService(IRepository log) + { + _log = log; + } + + public Task> SearchAsync(int schId, string keyword, bool systemScope, int page, int size) + { + if (page < 0) page = 0; + if (size <= 0) size = 20; + if (size > MaxPageSize) size = MaxPageSize; + + var kw = keyword?.Trim(); + + return _log.PageAsync( + a => a.SchId == schId + // 普通操作日志:只看增删改(排除查询类);系统操作日志全量 + && (systemScope || a.LogLevel != QueryLogLevel) + // 关键字模糊匹配(EF 翻译为 SQL LIKE,NULL 列不命中) + && (string.IsNullOrEmpty(kw) + || a.UserName.Contains(kw) + || a.RequestUri.Contains(kw) + || a.ClassMethod.Contains(kw) + || a.RequestParams.Contains(kw) + || a.ErrorMsg.Contains(kw) + || a.OperationType.Contains(kw)), + page, size, + a => a.CreateTime, asc: false); + } + } +}