Equipment_Index.razor
7.95 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
@page "/Basic/Equipment"
@using System.Text.Json;
@using AntDesign.TableModels
@using System.ComponentModel
@using DataAcquisition.Common.Enums
@using DataAcquisition.Common.Utils
@using DataAcquisition.DataAccess
@using DataAcquisition.Models
@using DataAcquisition.Pages.Basic.EquipmentProptery
@using DataAcquisition.Services
@using DataAcquisition.ViewModels.IOT
@using LinqKit
@using Microsoft.EntityFrameworkCore
@using System.Linq.Expressions
@inject IDbContextFactory<DataContext> dbContextFactory;
@inject DataCacheService dataCacheService;
@inject IotService iotService;
@inject ModalService _modalService
@inject IMessageService _message
<Flex Justify="space-between" Align="center">
<Flex Justify="flex-start" Align="center" Gap="small">
<AntDesign.Input DefaultValue="@string.Empty" @bind-Value="code">
<AddOnBefore>编号</AddOnBefore>
</AntDesign.Input>
<Button Type="@ButtonType.Primary" Icon="@IconType.Outline.Search" @onclick="SearchEvent">搜索</Button>
<Button Type="@ButtonType.Default" Icon="@IconType.Outline.Redo" @onclick="ResetEvent">重置</Button>
</Flex>
<Flex Justify="flex-end" Align="center" Gap="small">
<Button Type="@ButtonType.Primary" Icon="@IconType.Outline.Plus" @onclick="AddEvent">新增</Button>
<Button Type="@ButtonType.Primary" Icon="@IconType.Outline.Delete" Danger @onclick="BatchDeleteEvent">批量删除</Button>
</Flex>
</Flex>
<Table @ref="mainTable"
TItem="Equipment"
DataSource="@equipments"
Total="_total"
@bind-PageIndex="_pageIndex"
@bind-PageSize="_pageSize"
@bind-SelectedRows="selectedRows"
Loading="loading"
OnChange="EquipmentOnChange"
Size="TableSize.Small"
RowKey="x=>x.Id">
<Selection Key="@(context.Id.ToString())" />
<PropertyColumn Property="c=>c.Id" Hidden />
<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="设备SN" Property="c=>c.EquipmentSN" />
<PropertyColumn Title="仓库编号" Property="c=>c.WarehouseCode" />
<PropertyColumn Title="备注" Property="c=>c.Remark" />
<PropertyColumn Title="创建时间" Property="c=>c.CreateTime" />
<ActionColumn Title="操作">
<Space>
<SpaceItem>
<Button Icon="@IconType.Outline.Edit" Size="@ButtonSize.Small" @onclick="()=>EditEvent(context.Id)">修改</Button>
<Button Icon="@IconType.Outline.Delete" Size="@ButtonSize.Small" Danger @onclick="()=>DeleteEvent(context.Id)">删除</Button>
<Button Icon="@IconType.Outline.InfoCircle" Size="@ButtonSize.Small" OnClick="()=>OpenDetail(context.Id)">详细</Button>
</SpaceItem>
</Space>
</ActionColumn>
</Table>
<Equipment_Add @bind-Visible="@visible_add" OnSubmitSuccess="AddEventSuccessCallback" />
<Equipment_Edit @bind-Visible="@visible_edit" EquipmentId="@equipmentId" OnSubmitSuccess="EditEventSuccessCallback" />
<Modal Title="@modalTitle"
@bind-Visible="@_visible"
Maximizable="true"
Centered="true"
Resizable="true"
Draggable="true"
Width="1200"
MaxBodyHeight="80vh"
Footer="null"
DestroyOnClose="true"
DefaultMaximized="false">
<EquipmentProptery_Index EquipmentId="@equipmentId" />
</Modal>
@code {
bool loading = true;
bool _visible = false;
string modalTitle = string.Empty;
bool visible_add = false;
bool visible_edit = false;
IEnumerable<Equipment> selectedRows = null!;
List<Equipment> equipments = new List<Equipment>();
SystemLog log = SystemLog.Instance;
ITable mainTable = null!;
#region 查询输入
string code = string.Empty;
#endregion
int _pageIndex = 1;
int _pageSize = 10;
int _total = 0;
int equipmentId = 0;
public void EquipmentOnChange(QueryModel<Equipment> queryModel)
{
LoadData(queryModel.PageIndex, queryModel.PageSize);
}
public void SearchEvent()
{
_pageIndex = 1;
LoadData(_pageIndex, _pageSize);
}
public void ResetEvent()
{
code = string.Empty;
}
public void LoadData(int pageIndex, int pageSize)
{
loading = true;
try
{
using var dbContext = dbContextFactory.CreateDbContext();
var query = dbContext.Equipments.Where(QueryExpression());
equipments = query.OrderBy(x => x.Id).Skip(((pageIndex - 1) * pageSize)).Take(pageSize).Include(x => x.CommunicationConfig).AsSplitQuery().AsNoTracking().ToList();
_total = query.Count();
}
catch (Exception ex)
{
_message.Error($"加载数据失败:{ex.Message}");
}
loading = false;
}
private Expression<Func<Equipment, bool>> QueryExpression()
{
var filter = PredicateBuilder.New<Equipment>(true);
if (!string.IsNullOrWhiteSpace(code))
{
filter = filter.And(x => x.Code.Contains(code));
}
return filter;
}
public void AddEvent()
{
visible_add = true;
}
public void AddEventSuccessCallback()
{
visible_add = false;
LoadData(_pageIndex, _pageSize);
}
public void EditEvent(int id)
{
equipmentId = id;
visible_edit = true;
}
public void EditEventSuccessCallback()
{
visible_edit = false;
LoadData(_pageIndex, _pageSize);
}
RenderFragment icon = @<Icon Type="exclamation-circle" Theme="outline" />;
public void DeleteEvent(int id)
{
_modalService.Confirm(new ConfirmOptions
{
Title = "确认删除选中的数据?",
Icon = icon,
OnOk = (e) =>
{
try
{
using var context = dbContextFactory.CreateDbContext();
context.Equipments.Where(x => x.Id == id).ExecuteDelete();
_message.Success($"操作成功");
LoadData(_pageIndex, _pageSize);
StateHasChanged();
}
catch (Exception ex)
{
_message.Error($"操作出现异常:{ex.Message}");
}
return Task.CompletedTask;
}
});
}
public void BatchDeleteEvent()
{
if (selectedRows?.Any() != true)
{
_message.Warning($"未选中任何数据!");
return;
}
_modalService.Confirm(new ConfirmOptions
{
Title = "确认删除选中的数据?",
Icon = icon,
OnOk = (e) =>
{
try
{
using var context = dbContextFactory.CreateDbContext();
context.Equipments.Where(x => selectedRows.Select(e => e.Id).Contains(x.Id)).ExecuteDelete();
_message.Success($"操作成功");
LoadData(_pageIndex, _pageSize);
StateHasChanged();
}
catch (Exception ex)
{
_message.Error($"操作出现异常:{ex.Message}");
}
return Task.CompletedTask;
}
});
}
private void OpenDetail(int id)
{
_visible = true;
equipmentId = id;
modalTitle = equipments.Find(x => x.Id == id)?.Name ?? string.Empty;
}
}