Equipment_Add.razor 3.45 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="@("新增")"
       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="区域">
            <Input @bind-Value="@context.Area" />
        </FormItem>

        <FormItem Label="设备SN">
            <Input @bind-Value="@context.EquipmentSN" />
        </FormItem>

        <FormItem Label="仓库编号">
            <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; }

    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)
    {
        VisibleChanged.InvokeAsync(false);
        OnSubmitSuccess.InvokeAsync();
    }

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