Communication_Index.razor 6.76 KB
@page "/Basic/Communication"
@using System.Text.Json;
@using AntDesign.TableModels
@using System.ComponentModel
@using DataAcquisition.DataAccess
@using DataAcquisition.Models
@using LinqKit
@using Microsoft.EntityFrameworkCore
@using System.Linq.Expressions
@inject IDbContextFactory<DataContext> dbContextFactory;
@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="QueryEvent">搜索</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="table"
       TItem="CommunicationConfig"
       DataSource="@communicationConfigs"
       Total="_total"
       @bind-PageIndex="_pageIndex"
       @bind-PageSize="_pageSize"
       @bind-SelectedRows="selectedRows"
       OnChange="OnChange"
       Loading="loading"
       Size="TableSize.Small"
       RowKey="x=>x.Id">
    <Selection Key="@(context.Id.ToString())" />
    <PropertyColumn Property="c=>c.Id" Hidden Sortable />
    <PropertyColumn Title="编号" Property="c=>c.Code" />
    <PropertyColumn Title="名称" Property="c=>c.Name" />
    <PropertyColumn Title="通信方式" Property="c=>c.CommunicationType" />
    <PropertyColumn Title="IP地址" Property="c=>c.IpAddress" />
    <PropertyColumn Title="端口" Property="c=>c.Port" />
    <PropertyColumn Title="是否启用" Property="c=>c.Enable">
        <Switch @bind-Value="@context.Enable" Disabled="true"></Switch>
    </PropertyColumn>
    <PropertyColumn Title="备注" Property="c=>c.Remark" />
    <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>
            </SpaceItem>
        </Space>
    </ActionColumn>
</Table>

<Communication_Add @bind-Visible="@visible_add" OnSubmitSuccess="AddEventSuccessCallback" />


<Communication_Edit @bind-Visible="@visible_edit" CommunicationId="@communicationId" OnSubmitSuccess="EditEventSuccessCallback" />

@code {
    bool loading = true;

    List<CommunicationConfig> communicationConfigs = new List<CommunicationConfig>();

    IEnumerable<CommunicationConfig> selectedRows = null!;
    ITable table = null!;

    #region 搜索输入条件

    string code = string.Empty;

    #endregion

    bool visible_add = false;

    int communicationId = 0;
    bool visible_edit = false;

    int _pageIndex = 1;
    int _pageSize = 10;
    int _total = 0;

    public void OnChange(QueryModel<CommunicationConfig> queryModel)
    {
        LoadData(_pageIndex, _pageSize);
    }

    private Expression<Func<CommunicationConfig, bool>> QueryExpression()
    {
        var filter = PredicateBuilder.New<CommunicationConfig>(true);
        if (!string.IsNullOrWhiteSpace(code))
        {
            filter = filter.And(x => x.Code.Contains(code));
        }
        return filter;
    }

    public void QueryEvent()
    {
        LoadData(_pageIndex, _pageSize);
    }

    private void LoadData(int pageIndex, int pageSize)
    {
        loading = true;
        try
        {
            using var dbContext = dbContextFactory.CreateDbContext();
            var query = dbContext.CommunicationConfigs.Where(QueryExpression());
            communicationConfigs = query.OrderBy(x => x.Id).Skip(((pageIndex - 1) * pageSize)).Take(pageSize).AsNoTracking().ToList();
            _total = query.Count();
        }
        catch (Exception ex)
        {
            _message.Error($"加载数据出现异常:{ex.Message}");
        }
        loading = false;
    }

    public void ResetEvent()
    {
        code = string.Empty;
    }

    public void AddEvent()
    {
        visible_add = true;
    }

    public void AddEventSuccessCallback()
    {
        visible_add = false;
        LoadData(_pageIndex, _pageSize);
    }

    public void EditEvent(int id)
    {
        communicationId = id;
        visible_edit = true;
    }

    public void EditEventSuccessCallback()
    {
        visible_edit = false;
        LoadData(_pageIndex, _pageSize);
    }

    RenderFragment icon = @<Icon Type="exclamation-circle" Theme="outline"></Icon>;

    public void DeleteEvent(int id)
    {
        _modalService.Confirm(new ConfirmOptions
            {
                Title = "确认删除选中的数据?",
                Icon = icon,
                OnOk = (e) =>
                {
                    try
                    {
                        using var context = dbContextFactory.CreateDbContext();
                        context.CommunicationConfigs.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.CommunicationConfigs.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;
                }
            });
    }
}