Blame view

HHECS.DAQClient/ViewModel/EquipmentPropVM/EquipmentPropMonitorVM.cs 5.73 KB
唐召明 authored
1
2
3
4
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using HandyControl.Data;
using HHECS.BllModel;
唐召明 authored
5
using HHECS.DAQClient.Model;
唐召明 authored
6
using HHECS.DAQClient.Services;
唐召明 authored
7
using HHECS.DAQClient.ViewModel.EquipmentVM;
唐召明 authored
8
9
10
11
12
using HHECS.EquipmentModel;
using LinqKit;
using System.Collections.ObjectModel;
using System.Linq.Expressions;
using System.Windows;
唐召明 authored
13
using System.Windows.Threading;
唐召明 authored
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
using MessageBox = HandyControl.Controls.MessageBox;

namespace HHECS.DAQClient.ViewModel.EquipmentPropVM
{
    public partial class EquipmentPropMonitorVM : ObservableObject
    {
        [ObservableProperty]
        private string equipmentPropCode;

        [ObservableProperty]
        private string equipmentPropName;

        [ObservableProperty]
        private Dictionary<string, EquipmentPropType?> equipmentPropTypes = new Dictionary<string, EquipmentPropType?>();

        [ObservableProperty]
        private EquipmentPropType? equipmentPropType;

        [ObservableProperty]
唐召明 authored
33
        private ObservableCollection<EquipmentPropExtend> equipmentPropList = new ObservableCollection<EquipmentPropExtend>();
唐召明 authored
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48

        [ObservableProperty]
        private int pageIndex = 1;

        [ObservableProperty]
        private long maxPage;

        [ObservableProperty]
        private int pageSize = 30;

        public Window Owner { get; set; }

        /// <summary>
        /// 设备Id
        /// </summary>
唐召明 authored
49
        private int EquipmentId { get; set; }
唐召明 authored
50
唐召明 authored
51
        private readonly IFreeSql _freeSql;
唐召明 authored
52
        private readonly CenterService _centerService;
唐召明 authored
53
唐召明 authored
54
        public EquipmentPropMonitorVM(IFreeSql freeSql, CenterService centerService)
唐召明 authored
55
        {
唐召明 authored
56
            _freeSql = freeSql;
唐召明 authored
57
            _centerService = centerService;
唐召明 authored
58
59
        }
唐召明 authored
60
        public void InitialData(EquipmentExtend equipment, CancellationTokenSource cancellationTokenSource)
唐召明 authored
61
62
63
64
65
66
67
68
69
70
        {
            var keyValuePairs = new Dictionary<string, EquipmentPropType?>
            {
                { "全部",null }
            };
            foreach (var item in Enum.GetNames<EquipmentPropType>())
            {
                keyValuePairs.Add(item, Enum.Parse<EquipmentPropType>(item));
            }
            EquipmentPropTypes = keyValuePairs;
唐召明 authored
71
72
            Owner.Title = $"{equipment.Name}-属性数据";
            EquipmentId = equipment.Id;
73
            LoadData();
唐召明 authored
74
            RefreshValues(cancellationTokenSource);
唐召明 authored
75
76
77
78
79
80
81
82
        }

        [RelayCommand]
        public void Serach()
        {
            var result = LoadData();
            if (!result.Success)
            {
唐召明 authored
83
                MessageBox.Error($"[{nameof(EquipmentPropMonitorVM)}]加载数据失败.{result.Msg}");
唐召明 authored
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
            }
        }

        /// <summary>
        /// 页码改变
        /// </summary>
        [RelayCommand]
        private void PageUpdated(FunctionEventArgs<int> info)
        {
            PageIndex = info.Info;
            LoadData();
        }

        private BllResult LoadData()
        {
            try
            {
唐召明 authored
101
                var query = _freeSql.Queryable<EquipmentPropExtend>().Where(GetFilter()).Include(x => x.EquipmentTypePropTemplate);
唐召明 authored
102
103
104
                var total = query.Count();
                MaxPage = total / PageSize + Convert.ToInt32(total % PageSize != 0);
                var result = query.Page(PageIndex, PageSize).ToList();
唐召明 authored
105
                EquipmentPropList = new ObservableCollection<EquipmentPropExtend>(result);
唐召明 authored
106
107
108
109
110
111
112
113
                return BllResultFactory.Success();
            }
            catch (Exception ex)
            {
                return BllResultFactory.Error(ex.Message);
            }
        }
唐召明 authored
114
        private Expression<Func<EquipmentPropExtend, bool>> GetFilter()
唐召明 authored
115
        {
唐召明 authored
116
            var filter = PredicateBuilder.New<EquipmentPropExtend>(x => x.EquipmentId.Equals(EquipmentId));
唐召明 authored
117
118
119
120
121
122
123
124
125
126
            if (!string.IsNullOrWhiteSpace(EquipmentPropCode))
            {
                filter = filter.And(x => x.EquipmentTypePropTemplateCode.Contains(EquipmentPropCode));
            }
            if (!string.IsNullOrWhiteSpace(EquipmentPropName))
            {
                filter = filter.And(x => x.Remark.Contains(EquipmentPropName));
            }
            if (EquipmentPropType != null)
            {
唐召明 authored
127
                filter = filter.And(x => x.EquipmentTypePropTemplate.PropType == EquipmentPropType);
唐召明 authored
128
129
130
            }
            return filter;
        }
唐召明 authored
131
132
133
134
135
136
137
138
139
140
141
142
143

        /// <summary>
        /// 定时刷新Value数据
        /// </summary>
        private void RefreshValues(CancellationTokenSource cancellationTokenSource)
        {
            Task.Run(() =>
            {
                Application.Current.Dispatcher.Invoke(async () =>
                {
                    while (!cancellationTokenSource.IsCancellationRequested)
                    {
                        await Task.Delay(1000);
唐召明 authored
144
145
146
147
148
149
150
                        if (!_centerService.Equipments.Where(x => x.Id == EquipmentId).Any())
                        {
                            continue;
                        }

                        var temps = EquipmentPropList.ToList();
                        foreach (var item in temps)
唐召明 authored
151
152
153
154
155
156
157
158
159
                        {
                            var prop = _centerService.Equipments.SelectMany(x => x.EquipmentProps).Where(x => x.Id == item.Id).FirstOrDefault();
                            if (prop == null)
                            {
                                continue;
                            }
                            item.Value = prop.Value;
                            item.Updated = prop.Updated;
                        }
唐召明 authored
160
                        EquipmentPropList = null;
唐召明 authored
161
                        EquipmentPropList = new ObservableCollection<EquipmentPropExtend>(temps);
唐召明 authored
162
163
164
165
                    }
                }, DispatcherPriority.Background);
            }, cancellationTokenSource.Token);
        }
唐召明 authored
166
167
    }
}