using AutoMapper; using DMS.Application.DTOs; using DMS.Application.DTOs.Events; using DMS.Core.Models; using DMS.Application.Interfaces; using DMS.Core.Interfaces; using DMS.Core.Enums; using System.Collections.Concurrent; using System.Collections.Generic; using System.Threading.Tasks; using System; namespace DMS.Application.Services; /// /// 日志管理服务,负责日志相关的业务逻辑。 /// public class LogManagementService { private readonly INlogAppService _nlogAppService; private readonly ConcurrentDictionary _nlogs; /// /// 当日志数据发生变化时触发 /// public event EventHandler NlogChanged; public LogManagementService(INlogAppService nlogAppService, ConcurrentDictionary nlogs) { _nlogAppService = nlogAppService; _nlogs = nlogs; } /// /// 异步根据ID获取日志DTO。 /// public async Task GetNlogByIdAsync(int id) { return await _nlogAppService.GetLogByIdAsync(id); } /// /// 异步获取所有日志DTO列表。 /// public async Task> GetAllNlogsAsync() { return await _nlogAppService.GetAllLogsAsync(); } /// /// 异步获取指定数量的最新日志DTO列表。 /// public async Task> GetLatestNlogsAsync(int count) { return await _nlogAppService.GetLatestLogsAsync(count); } /// /// 异步清空所有日志。 /// public async Task ClearAllNlogsAsync() { await _nlogAppService.ClearAllLogsAsync(); } /// /// 在内存中添加日志 /// public void AddNlogToMemory(NlogDto nlogDto) { if (_nlogs.TryAdd(nlogDto.Id, nlogDto)) { OnNlogChanged(new NlogChangedEventArgs(DataChangeType.Added, nlogDto)); } } /// /// 在内存中更新日志 /// public void UpdateNlogInMemory(NlogDto nlogDto) { _nlogs.AddOrUpdate(nlogDto.Id, nlogDto, (key, oldValue) => nlogDto); OnNlogChanged(new NlogChangedEventArgs(DataChangeType.Updated, nlogDto)); } /// /// 在内存中删除日志 /// public void RemoveNlogFromMemory(int nlogId) { if (_nlogs.TryRemove(nlogId, out var nlogDto)) { OnNlogChanged(new NlogChangedEventArgs(DataChangeType.Deleted, nlogDto)); } } /// /// 触发日志变更事件 /// protected virtual void OnNlogChanged(NlogChangedEventArgs e) { NlogChanged?.Invoke(this, e); } }