using WxOrder.Framework.Commons;
using _29WxOrderWpf.Services;
using AllBase.Model.MvvmBaseClasses;
using AllBase.Util;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Linq;
using System.Runtime.InteropServices.ComTypes;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using Work.KJDServer.API.Models;
using WPFCommon.Models;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.TaskbarClock;
namespace _29WxOrderWpf.Ctrls.OrderCtrls;
///
/// OrderTaskDialogView.xaml 的交互逻辑
///
public partial class OrderTaskDialogView : UserControl
{
private readonly OrderTaskDialogViewModel _viewModel;
public OrderTaskDialogView()
{
InitializeComponent();
this.Loaded += OrderTaskDialogView_Loaded;
_viewModel = new OrderTaskDialogViewModel();
DataContext = _viewModel;
}
private async void OrderTaskDialogView_Loaded(object sender, RoutedEventArgs e)
{
_viewModel.LoadTasks();
}
}
///
/// 任务列表 - ViewModel
///
public class OrderTaskDialogViewModel : BindableBaseEx, IDisposable
{
private DispatcherTimer _timer;
private readonly TaskService _taskService;
public OrderTaskDialogViewModel()
: this(TaskService.Instance)
{
_timer = new DispatcherTimer();
_timer.Interval = TimeSpan.FromSeconds(1);
_timer.Tick += _timer_Tick;
_timer.Start();
}
public OrderTaskDialogViewModel(TaskService taskService)
{
_taskService = taskService;
StartDate = _taskService.StartDate;
EndDate = _taskService.EndDate;
Tasks = new ObservableCollection();
TasksView = CollectionViewSource.GetDefaultView(Tasks);
TasksView.Filter = FilterTaskStatus;
_taskService.TaskChanged += TaskChanged;
}
private async void _timer_Tick(object sender, EventArgs e)
{
RefreshTime = _taskService.ResetCountdown;
}
public TaskType SelectedTaskType
{
get => GetValue();
set
{
SetValue(value);
TasksView.Refresh();
}
}
private void TaskChanged(object sender, IEnumerable data)
{
LoadTasks();
}
public void LoadTasks()
{
System.Windows.Application.Current.Dispatcher.Invoke(() =>
{
Tasks.Clear();
foreach (var item in TaskService.Instance.Tasks)
{
if (!string.IsNullOrEmpty(item.TaskFailReason))
{
if (item.TaskFailReason.Contains("任务执行异常"))
{
item.Status = 4;
}
}
Tasks.Add(item);
}
});
}
///
/// 过滤
///
///
///
private bool FilterTaskStatus(object obj)
{
if (obj is not TaskItem task)
return false;
// Status 维度筛选
bool statusMatch = IsShowAll || task.Status != 2;
// 如果 IsShowAll=true,表示显示所有状态;否则只显示特定状态
// TaskType 维度筛选
bool typeMatch = SelectedTaskType == TaskType.ALL ? true : SelectedTaskType == task.TaskType;
bool timeMatch = StartDate <= task.CreateTime && task.CreateTime <= EndDate;
// 如果 SelectedTaskType==null,表示显示所有类型;否则只显示特定类型
return statusMatch && typeMatch && timeMatch;
}
/// 任务列表
public ObservableCollection Tasks { get; }
public ICollectionView TasksView { get; }
/// 汇总文字(总数/执行中/成功/失败)
public string SummaryText
{
get => GetValue();
set => SetValue(value);
}
public int RefreshTime
{
get => GetValue();
set => SetValue(value);
}
public DateTime StartDate
{
get => GetValue();
set => SetValue(value);
}
public DateTime EndDate
{
get => GetValue();
set => SetValue(value);
}
public bool IsShowAll
{
get => GetValue();
set
{
SetValue(value);
TasksView.Refresh();
}
}
public void Dispose()
{
_timer.Stop();
_timer = null;
}
}