EqipmentPage.razor 6.26 KB
@page "/Basic/Equipment"

@using System.Text.Json;
@using AntDesign.TableModels
@using System.ComponentModel
@using DataAcquisitionServer.Common.Utils
@using DataAcquisitionServer.DataAccess
@using DataAcquisitionServer.Models
@using DataAcquisitionServer.Services
@using Microsoft.EntityFrameworkCore
@inject IDbContextFactory<DataContext> dbContextFactory;
@inject DataCacheService dataCacheService;

<Spin Spinning="loading">
    <Table @ref="mainTable"
           TItem="Equipment"
           DataSource="@equipments"
           Total="_total"
           @bind-PageIndex="_pageIndex"
           @bind-PageSize="_pageSize"
           OnChange="EquipmentOnChange"
           Size="TableSize.Small"
           RowKey="x=>x.Id">
        <PropertyColumn Property="c=>c.Id" />
        <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="备注" Property="c=>c.Remark" />
        <PropertyColumn Title="创建时间" Property="c=>c.CreateTime" />
        <ActionColumn Title="操作">
            <Space>
                <SpaceItem>
                    <Button Type="@ButtonType.Primary"
                            Icon="@IconType.Outline.InfoCircle"
                            Size="@ButtonSize.Small"
                            OnClick="()=>OpenDetail(context.Id)">
                        详细
                    </Button>
                </SpaceItem>
            </Space>
        </ActionColumn>
    </Table>
</Spin>

<Modal Title="@modalTitle"
       @bind-Visible="@_visible"
       Maximizable="true"
       Centered="true"
       Resizable="true"
       Draggable="true"
       Width="900"
       Footer="null"
       DestroyOnClose="true"
       DefaultMaximized="false">
    <Table @ref="detailTable"
           TItem="EquipmentProperty"
           DataSource="@equipmentProperties"
           Total="_detailTotal"
           @bind-PageIndex="_detailPageIndex"
           @bind-PageSize="_detailPageSize"
           OnChange="EquipmentPropertyOnChange"
           Size="TableSize.Small"
           RowKey="x=>x.Id">
        <PropertyColumn Property="c=>c.Id" />
        <PropertyColumn Title="编号" Property="c=>c.Code" />
        <PropertyColumn Title="名称" Property="c=>c.Name" />
        <PropertyColumn Title="数据地址" Property="c=>c.DataAddress" />
        <PropertyColumn Title="数据类型" Property="c=>c.DataType" />
        <PropertyColumn Title="值" Property="c=>c.Value" />
        <PropertyColumn Title="是否启用" Property="c=>c.Enable">
            <Switch @bind-Value="@context.Enable" Disabled="true"></Switch>
        </PropertyColumn>
        <PropertyColumn Title="备注" Property="c=>c.Remark" />
        <PropertyColumn Title="创建时间" Property="c=>c.CreateTime" />
        <PropertyColumn Title="更新时间" Property="c=>c.UpdateTime" />
    </Table>
</Modal>

@code {

    bool loading = true;

    bool _visible = false;

    string modalTitle = string.Empty;

    List<Equipment> equipments = new List<Equipment>();

    List<EquipmentProperty> equipmentProperties = new List<EquipmentProperty>();

    SystemLog log = SystemLog.Instance;

    ITable mainTable = null!;
    ITable detailTable = null!;

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

    int equipmentId = 0;
    int _detailPageIndex = 1;
    int _detailPageSize = 10;
    int _detailTotal = 0;

    public async Task EquipmentOnChange(QueryModel<Equipment> queryModel)
    {
        loading = true;
        using var dbContext = dbContextFactory.CreateDbContext();
        var query = dbContext.Equipments;
        equipments = await query.OrderBy(x => x.Id).Skip(((queryModel.PageIndex - 1) * queryModel.PageSize)).Take(queryModel.PageSize).Include(x => x.CommunicationConfig).AsSplitQuery().AsNoTracking().ToListAsync();
        _total = await query.CountAsync();
        loading = false;
    }

    public async Task EquipmentPropertyOnChange(QueryModel<EquipmentProperty> queryModel)
    {
        await LoadEquipmentPropertyData(queryModel.PageIndex, queryModel.PageSize, queryModel.SortModel);
    }

    private void OpenDetail(int id)
    {
        equipmentProperties.Clear();
        _visible = true;
        _detailPageIndex = 1;
        modalTitle = equipments.Find(x => x.Id == id)?.Name ?? string.Empty;
        LoadEquipmentPropertyData(_detailPageIndex, _detailPageSize).Wait();
        _ = InvokeAsync(async () =>
         {
             equipmentId = id;
             try
             {
                 do
                 {
                     var equipmentPropertiesCache = dataCacheService.Equipments.Find(x => x.Id == equipmentId)?.EquipmentProperties.ToList() ?? new List<EquipmentProperty>();
                     foreach (var item in equipmentProperties)
                     {
                         var newItem = equipmentPropertiesCache.Find(x => x.Id == item.Id);
                         if (newItem is null) continue;
                         item.Value = newItem.Value ?? string.Empty;
                         item.UpdateTime = newItem.UpdateTime;
                     }
                     StateHasChanged();
                     await Task.Delay(1000);
                 } while (_visible);
             }
             catch (Exception ex)
             {
                 log.LogError($"设备详细页面数据刷新异常:{ex.Message}");
             }
         });
    }

    private Task LoadEquipmentPropertyData(int index, int size, IList<ITableSortModel>? sortModels = null)
    {
        return Task.Run(async () =>
        {
            using var dbContext = dbContextFactory.CreateDbContext();
            var query = dbContext.EquipmentProperties.Where(x => x.EquipmentId == equipmentId);
            equipmentProperties = await query.OrderBy(x => x.Id).Skip(((index - 1) * size)).Take(size).AsNoTracking().ToListAsync();
            _detailTotal = await query.CountAsync();
        });
    }
}