Equipment_Add.razor
3.53 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
@using DataAcquisition.Common.Enums
@using DataAcquisition.DataAccess
@using DataAcquisition.Models
@using Microsoft.EntityFrameworkCore
@using System.Text.Json
@inject IDbContextFactory<DataContext> dbContextFactory;
@inject IMessageService _message
<Modal Title="@("新增")"
Visible="Visible"
Maximizable="true"
Centered="true"
Resizable="true"
Draggable="true"
MaxBodyHeight="80vh"
DestroyOnClose="true"
OnOk="HandleOk"
OnCancel="HandleCancel">
<Form Model="@model" OnFinish="OnFinish"
OnFinishFailed="OnFinishFailed"
@ref="@_form">
<FormItem Label="编码" Required>
<Input @bind-Value="@context.Code" />
</FormItem>
<FormItem Label="名称" Required>
<Input @bind-Value="@context.Name" />
</FormItem>
<FormItem Label="通讯配置">
<Select DataSource="@CommunicationTypeItems"
@bind-Value="@context.CommunicationId"
ValueProperty="c=>c.Key"
LabelProperty="c=>c.Value">
</Select>
</FormItem>
<FormItem Label="设备类型" Required>
<EnumSelect TEnum="EquipmentTypeConst" @bind-Value="context.EquipmentType" />
</FormItem>
<FormItem Label="是否启用" Required>
<Switch @bind-Value="@context.Enable" />
</FormItem>
<FormItem Label="区域" Required>
<Input @bind-Value="@context.Area" />
</FormItem>
<FormItem Label="设备SN" Required>
<Input @bind-Value="@context.EquipmentSN" />
</FormItem>
<FormItem Label="仓库编号" Required>
<Input @bind-Value="@context.WarehouseCode" />
</FormItem>
<FormItem Label="备注">
<TextArea @bind-Value="@context.Remark" />
</FormItem>
</Form>
</Modal>
@code {
[Parameter]
public EventCallback OnSubmitSuccess { get; set; }
[Parameter]
public bool Visible { get; set; }
[Parameter]
public EventCallback<bool> VisibleChanged { get; set; }
bool _submitting = false;
private Form<Equipment> _form = null!;
Equipment model = new Equipment();
Dictionary<int, string> CommunicationTypeItems = new Dictionary<int, string>();
protected override void OnParametersSet()
{
if (!Visible) return;
try
{
using var context = dbContextFactory.CreateDbContext();
CommunicationTypeItems = context.CommunicationConfigs.Select(x => new { x.Id, x.Name }).ToDictionary(x => x.Id, y => y.Name);
}
catch (Exception ex)
{
_message.Error($"数据加载失败:{ex.Message}");
}
model = new Equipment();
base.OnParametersSet();
}
public void HandleOk()
{
try
{
using var context = dbContextFactory.CreateDbContext();
context.Equipments.Add(model);
context.SaveChanges();
_form.Submit();
}
catch (Exception ex)
{
_message.Error($"保存失败:{ex.Message}");
}
}
public async Task HandleCancel()
{
await VisibleChanged.InvokeAsync(false);
}
private void OnFinish(EditContext editContext)
{
_submitting = false;
VisibleChanged.InvokeAsync(false);
OnSubmitSuccess.InvokeAsync();
}
private void OnFinishFailed(EditContext editContext)
{
_message.Error($"Failed:{JsonSerializer.Serialize(model)}");
}
}