EquipmentProperty_Edit.razor 2.96 KB
@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="@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="数据地址" Required>
            <Input @bind-Value="@context.DataAddress" />
        </FormItem>

        <FormItem Label="数据类型" Required>
            <EnumSelect TEnum="DataTypeConst" @bind-Value="context.DataType" />
        </FormItem>

        <FormItem Label="是否启用" Required>
            <Switch @bind-Value="@context.Enable" />
        </FormItem>

        <FormItem Label="备注">
            <TextArea @bind-Value="@context.Remark" />
        </FormItem>
    </Form>
</Modal>

@code {
    [Parameter]
    public int EquipmentPropertyId { get; set; }

    [Parameter]
    public EventCallback OnSubmitSuccess { get; set; }

    [Parameter]
    public bool Visible { get; set; }

    [Parameter]
    public EventCallback<bool> VisibleChanged { get; set; }

    string modal_title = string.Empty;

    private Form<EquipmentProperty> _form = null!;

    EquipmentProperty model = new EquipmentProperty();

    protected override void OnParametersSet()
    {
        if (!Visible) return;
        try
        {
            using var context = dbContextFactory.CreateDbContext();
            model = context.EquipmentProperties.Where(x => x.Id == EquipmentPropertyId).First();
            modal_title = model.Name;
            base.OnParametersSet();
        }
        catch (Exception ex)
        {
            _message.Error($"数据加载异常:{ex.Message}");
        }
    }

    public void HandleOk()
    {
        try
        {
            using var context = dbContextFactory.CreateDbContext();
            context.EquipmentProperties.Update(model);
            context.SaveChanges();
            _form.Submit();
        }
        catch (Exception ex)
        {
            _message.Error($"保存失败:{ex.Message}");
        }
    }

    public void HandleCancel()
    {
        VisibleChanged.InvokeAsync(false);
    }

    private void OnFinish(EditContext editContext)
    {
        VisibleChanged.InvokeAsync(false);
        OnSubmitSuccess.InvokeAsync();
    }

    private void OnFinishFailed(EditContext editContext)
    {
        _message.Error($"Failed:{JsonSerializer.Serialize(model)}");
    }
}