EqipmentPage.razor
6.26 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
@page "/Basic/Equipment"
@using System.Text.Json;
@using AntDesign.TableModels
@using System.ComponentModel
@using DataAcquisitionServer.Common.Utils
@using DataAcquisitionServer.DataAccess
@using DataAcquisitionServer.Models
@using DataAcquisitionServer.Services
@using Microsoft.EntityFrameworkCore
@inject IDbContextFactory<DataContext> dbContextFactory;
@inject DataCacheService dataCacheService;
<Spin Spinning="loading">
<Table @ref="mainTable"
TItem="Equipment"
DataSource="@equipments"
Total="_total"
@bind-PageIndex="_pageIndex"
@bind-PageSize="_pageSize"
OnChange="EquipmentOnChange"
Size="TableSize.Small"
RowKey="x=>x.Id">
<PropertyColumn Property="c=>c.Id" />
<PropertyColumn Title="编号" Property="c=>c.Code" />
<PropertyColumn Title="设备名称" Property="c=>c.Name" />
<PropertyColumn Title="设备类型" Property="c=>c.EquipmentType" />
<PropertyColumn Title="通信配置" Property="c=>c.CommunicationConfig!.Code" />
<PropertyColumn Title="是否启用" Property="c=>c.Enable">
<Switch @bind-Value="@context.Enable" Disabled="true"></Switch>
</PropertyColumn>
<PropertyColumn Title="区域" Property="c=>c.Area" />
<PropertyColumn Title="备注" Property="c=>c.Remark" />
<PropertyColumn Title="创建时间" Property="c=>c.CreateTime" />
<ActionColumn Title="操作">
<Space>
<SpaceItem>
<Button Type="@ButtonType.Primary"
Icon="@IconType.Outline.InfoCircle"
Size="@ButtonSize.Small"
OnClick="()=>OpenDetail(context.Id)">
详细
</Button>
</SpaceItem>
</Space>
</ActionColumn>
</Table>
</Spin>
<Modal Title="@modalTitle"
@bind-Visible="@_visible"
Maximizable="true"
Centered="true"
Resizable="true"
Draggable="true"
Width="900"
Footer="null"
DestroyOnClose="true"
DefaultMaximized="false">
<Table @ref="detailTable"
TItem="EquipmentProperty"
DataSource="@equipmentProperties"
Total="_detailTotal"
@bind-PageIndex="_detailPageIndex"
@bind-PageSize="_detailPageSize"
OnChange="EquipmentPropertyOnChange"
Size="TableSize.Small"
RowKey="x=>x.Id">
<PropertyColumn Property="c=>c.Id" />
<PropertyColumn Title="编号" Property="c=>c.Code" />
<PropertyColumn Title="名称" Property="c=>c.Name" />
<PropertyColumn Title="数据地址" Property="c=>c.DataAddress" />
<PropertyColumn Title="数据类型" Property="c=>c.DataType" />
<PropertyColumn Title="值" Property="c=>c.Value" />
<PropertyColumn Title="是否启用" Property="c=>c.Enable">
<Switch @bind-Value="@context.Enable" Disabled="true"></Switch>
</PropertyColumn>
<PropertyColumn Title="备注" Property="c=>c.Remark" />
<PropertyColumn Title="创建时间" Property="c=>c.CreateTime" />
<PropertyColumn Title="更新时间" Property="c=>c.UpdateTime" />
</Table>
</Modal>
@code {
bool loading = true;
bool _visible = false;
string modalTitle = string.Empty;
List<Equipment> equipments = new List<Equipment>();
List<EquipmentProperty> equipmentProperties = new List<EquipmentProperty>();
SystemLog log = SystemLog.Instance;
ITable mainTable = null!;
ITable detailTable = null!;
int _pageIndex = 1;
int _pageSize = 10;
int _total = 0;
int equipmentId = 0;
int _detailPageIndex = 1;
int _detailPageSize = 10;
int _detailTotal = 0;
public async Task EquipmentOnChange(QueryModel<Equipment> queryModel)
{
loading = true;
using var dbContext = dbContextFactory.CreateDbContext();
var query = dbContext.Equipments;
equipments = await query.OrderBy(x => x.Id).Skip(((queryModel.PageIndex - 1) * queryModel.PageSize)).Take(queryModel.PageSize).Include(x => x.CommunicationConfig).AsSplitQuery().AsNoTracking().ToListAsync();
_total = await query.CountAsync();
loading = false;
}
public async Task EquipmentPropertyOnChange(QueryModel<EquipmentProperty> queryModel)
{
await LoadEquipmentPropertyData(queryModel.PageIndex, queryModel.PageSize, queryModel.SortModel);
}
private void OpenDetail(int id)
{
equipmentProperties.Clear();
_visible = true;
_detailPageIndex = 1;
modalTitle = equipments.Find(x => x.Id == id)?.Name ?? string.Empty;
LoadEquipmentPropertyData(_detailPageIndex, _detailPageSize).Wait();
_ = InvokeAsync(async () =>
{
equipmentId = id;
try
{
do
{
var equipmentPropertiesCache = dataCacheService.Equipments.Find(x => x.Id == equipmentId)?.EquipmentProperties.ToList() ?? new List<EquipmentProperty>();
foreach (var item in equipmentProperties)
{
var newItem = equipmentPropertiesCache.Find(x => x.Id == item.Id);
if (newItem is null) continue;
item.Value = newItem.Value ?? string.Empty;
item.UpdateTime = newItem.UpdateTime;
}
StateHasChanged();
await Task.Delay(1000);
} while (_visible);
}
catch (Exception ex)
{
log.LogError($"设备详细页面数据刷新异常:{ex.Message}");
}
});
}
private Task LoadEquipmentPropertyData(int index, int size, IList<ITableSortModel>? sortModels = null)
{
return Task.Run(async () =>
{
using var dbContext = dbContextFactory.CreateDbContext();
var query = dbContext.EquipmentProperties.Where(x => x.EquipmentId == equipmentId);
equipmentProperties = await query.OrderBy(x => x.Id).Skip(((index - 1) * size)).Take(size).AsNoTracking().ToListAsync();
_detailTotal = await query.CountAsync();
});
}
}