using AutoMapper; using DMS.Core.Interfaces.Repositories; using DMS.Core.Models; using DMS.Infrastructure.Data; using DMS.Infrastructure.Entities; using Microsoft.Extensions.Logging; namespace DMS.Infrastructure.Repositories; /// /// Nlog日志仓储实现类,负责Nlog日志数据的持久化操作。 /// 继承自 并实现 接口。 /// public class NlogRepository : BaseRepository, INlogRepository { private readonly IMapper _mapper; /// /// 构造函数,注入 AutoMapper 和 SqlSugarDbContext。 /// /// AutoMapper 实例,用于实体模型和数据库模型之间的映射。 /// SqlSugar 数据库上下文,用于数据库操作。 /// 日志记录器实例。 public NlogRepository(IMapper mapper, SqlSugarDbContext dbContext, ILogger logger) : base(dbContext, logger) { _mapper = mapper; } /// /// 异步删除所有Nlog日志。 /// public async Task DeleteAllAsync() { await _dbContext.GetInstance().Deleteable().ExecuteCommandAsync(); } // Nlog 通常是只读或追加的日志,因此像 AddAsync, UpdateAsync, DeleteAsync 这样的修改方法 // 可能不需要在仓储接口中暴露,或者可以省略具体实现或抛出 NotSupportedException。 // 但为了保持与基类一致性并满足接口要求,这里显式实现它们。 /// /// 异步获取所有Nlog日志条目。 /// /// 包含所有Nlog日志实体的列表。 public new async Task> GetAllAsync() { var dbList = await base.GetAllAsync(); return _mapper.Map>(dbList); } /// /// 异步根据ID获取单个Nlog日志条目。 /// /// 日志条目的唯一标识符。 /// 对应的Nlog日志实体,如果不存在则为null。 public new async Task GetByIdAsync(int id) { var dbNlog = await base.GetByIdAsync(id); return _mapper.Map(dbNlog); } /// /// 异步添加一个新Nlog日志条目。 /// 注意:直接写入数据库日志表通常不推荐,NLog本身负责写入。 /// 此方法主要用于满足接口契约,实际使用应谨慎。 /// /// 要添加的Nlog日志实体。 /// 添加后的Nlog日志实体(包含ID等信息)。 public new async Task AddAsync(Core.Models.Nlog entity) { var dbEntity = _mapper.Map(entity); var addedDbEntity = await base.AddAsync(dbEntity); return _mapper.Map(addedDbEntity, entity); } /// /// 异步更新一个Nlog日志条目。 /// 注意:直接更新数据库日志表通常不推荐,日志应视为不可变。 /// 此方法主要用于满足接口契约,实际使用应谨慎。 /// /// 要更新的Nlog日志实体。 /// 受影响的行数。 public new async Task UpdateAsync(Core.Models.Nlog entity) { var dbEntity = _mapper.Map(entity); return await base.UpdateAsync(dbEntity); } /// /// 异步删除一个Nlog日志条目。 /// 注意:直接删除数据库日志表记录通常不推荐,除非是清理过期日志。 /// 此方法主要用于满足接口契约,实际使用应谨慎。 /// /// 要删除的Nlog日志实体。 /// 受影响的行数。 public new async Task DeleteAsync(Core.Models.Nlog entity) { var dbEntity = _mapper.Map(entity); return await base.DeleteAsync(dbEntity); } /// /// 异步批量添加Nlog日志条目。 /// 注意:直接批量写入数据库日志表通常不推荐,NLog本身负责写入。 /// 此方法主要用于满足接口契约,实际使用应谨慎。 /// /// 要添加的Nlog日志实体列表。 /// 添加的Nlog日志实体列表。 public new async Task> AddAsync(List entities) { var dbEntities = _mapper.Map>(entities); var addedEntities = await base.AddAsync(dbEntities); return _mapper.Map>(addedEntities); } /// /// 异步批量更新Nlog日志条目。 /// /// 要更新的Nlog日志实体列表。 /// 受影响的行数。 public new async Task UpdateAsync(List entities) { var dbEntities = _mapper.Map>(entities); return await base.UpdateAsync(dbEntities); } /// /// 异步根据实体列表批量删除Nlog日志条目。 /// /// 要删除的Nlog日志实体列表。 /// 受影响的行数。 public new async Task DeleteAsync(List entities) { var dbEntities = _mapper.Map>(entities); return await base.DeleteAsync(dbEntities); } }