Files
DMS/DMS.WPF/Services/LogDataService.cs

72 lines
2.3 KiB
C#
Raw Normal View History

using System.Collections.ObjectModel;
using AutoMapper;
using CommunityToolkit.Mvvm.ComponentModel;
using DMS.Application.DTOs;
2025-09-16 14:42:23 +08:00
using DMS.Application.Events;
using DMS.Application.Interfaces;
2025-09-16 12:29:09 +08:00
using DMS.Core.Enums;
using DMS.WPF.Interfaces;
using DMS.WPF.ItemViewModel;
namespace DMS.WPF.Services;
/// <summary>
/// 日志数据服务类,负责管理日志相关的数据和操作。
/// </summary>
public class LogDataService : ILogDataService
{
private readonly IMapper _mapper;
private readonly IViewDataService _viewDataService;
private readonly IAppDataService _appDataService;
/// <summary>
/// LogDataService类的构造函数。
/// </summary>
/// <param name="mapper">AutoMapper 实例。</param>
/// <param name="appStorageService">数据服务中心实例。</param>
public LogDataService(IMapper mapper,IViewDataService dataStorageService, IAppDataService appStorageService)
{
_mapper = mapper;
_viewDataService = dataStorageService;
_appDataService = appStorageService;
}
public void LoadAllLog()
{
// 加载日志数据
_viewDataService.Nlogs = _mapper.Map<ObservableCollection<NlogItem>>(_appDataService.Nlogs.Values);
}
/// <summary>
/// 处理日志变更事件。
/// </summary>
public void OnNlogChanged(object sender, NlogChangedEventArgs e)
{
// 在UI线程上更新日志
App.Current.Dispatcher.BeginInvoke(new Action(() =>
{
switch (e.ChangeType)
{
case DataChangeType.Added:
_viewDataService.Nlogs.Add(_mapper.Map<NlogItem>(e.Nlog));
break;
case DataChangeType.Updated:
var existingLog = _viewDataService.Nlogs.FirstOrDefault(l => l.Id == e.Nlog.Id);
if (existingLog != null)
{
_mapper.Map(e.Nlog, existingLog);
}
break;
case DataChangeType.Deleted:
var logToRemove = _viewDataService.Nlogs.FirstOrDefault(l => l.Id == e.Nlog.Id);
if (logToRemove != null)
{
_viewDataService.Nlogs.Remove(logToRemove);
}
break;
}
}));
}
}