EquipmentDataQueueVM.cs
3.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using HandyControl.Data;
using HHECS.BllModel;
using HHECS.DAQClient.Model;
using LinqKit;
using System.Collections.ObjectModel;
using System.Linq.Expressions;
using MessageBox = HandyControl.Controls.MessageBox;
namespace HHECS.DAQClient.ViewModel.EquipmentVM
{
public partial class EquipmentDataQueueVM : ObservableObject
{
[ObservableProperty]
private string equipmentCode;
[ObservableProperty]
private Dictionary<string, string> equipmentTypes = new Dictionary<string, string>();
[ObservableProperty]
private string equipmentTypeCode;
[ObservableProperty]
private int pageIndex = 1;
[ObservableProperty]
private long maxPage;
[ObservableProperty]
private int pageSize = 30;
[ObservableProperty]
private ObservableCollection<EquipmentDataQueue> dataQueues = new ObservableCollection<EquipmentDataQueue>();
private readonly IFreeSql _freeSql;
public EquipmentDataQueueVM(IFreeSql freeSql)
{
_freeSql = freeSql;
InitialData();
LoadData();
}
private void InitialData()
{
try
{
var result = _freeSql.Queryable<EquipmentTypeExtend>().ToList();
var keyValuePairs = new Dictionary<string, string>
{
{ string.Empty,"全部" }
};
foreach (var item in result)
{
keyValuePairs.Add(item.Code, item.Name);
}
EquipmentTypes = keyValuePairs;
}
catch (Exception ex)
{
MessageBox.Error($"[{nameof(EquipmentDataQueueVM)}]加载数据失败.{ex.Message}");
}
}
[RelayCommand]
public void Serach()
{
var result = LoadData();
if (!result.Success)
{
MessageBox.Error($"[{nameof(EquipmentDataQueueVM)}]加载数据失败.{result.Msg}");
}
}
/// <summary>
/// 页码改变
/// </summary>
[RelayCommand]
private void PageUpdated(FunctionEventArgs<int> info)
{
PageIndex = info.Info;
LoadData();
}
private BllResult LoadData()
{
try
{
var query = _freeSql.Queryable<EquipmentDataQueue>().Where(GetFilter());
var total = query.Count();
MaxPage = total / PageSize + Convert.ToInt32(total % PageSize != 0);
var result = query.OrderByDescending(x => x.Created).Page(PageIndex, PageSize).ToList();
DataQueues = new ObservableCollection<EquipmentDataQueue>(result);
return BllResultFactory.Success();
}
catch (Exception ex)
{
return BllResultFactory.Error(ex.Message);
}
}
private Expression<Func<EquipmentDataQueue, bool>> GetFilter()
{
var filter = PredicateBuilder.New<EquipmentDataQueue>(true);
if (!string.IsNullOrWhiteSpace(EquipmentCode))
{
filter = filter.And(x => x.EquipmentCode.Contains(EquipmentCode));
}
if (!string.IsNullOrWhiteSpace(EquipmentTypeCode))
{
filter = filter.And(x => x.EquipmentTypeCode.Contains(EquipmentTypeCode));
}
return filter;
}
}
}