EquipmentPropteryIndex.razor 4.64 KB
@using AntDesign.TableModels
@using DataAcquisition.Common.Utils
@using DataAcquisition.DataAccess
@using DataAcquisition.Models
@using DataAcquisition.Services
@using Microsoft.EntityFrameworkCore
@inject IDbContextFactory<DataContext> dbContextFactory;
@inject DataCacheService dataCacheService;
@inject IMessageService _message

<Flex Justify="space-between" Align="center">
    <Flex Justify="flex-start" Align="center" Gap="small">
        <AntDesign.Input DefaultValue="@("mysite")">
            <AddOnBefore>编号</AddOnBefore>
        </AntDesign.Input>
        <Button Type="@ButtonType.Primary" Icon="@IconType.Outline.Search">搜索</Button>
        <Button Type="@ButtonType.Default" Icon="@IconType.Outline.Redo">重置</Button>
    </Flex>
    <Flex Justify="flex-end" Align="center" Gap="small">
        <Button Type="@ButtonType.Primary" Icon="@IconType.Outline.CloudUpload">新增</Button>
        <Button Type="@ButtonType.Primary" Icon="@IconType.Outline.Delete" Danger>批量删除</Button>
    </Flex>
</Flex>

<Table @ref="detailTable"
       TItem="EquipmentProperty"
       DataSource="@equipmentProperties"
       Total="_detailTotal"
       @bind-PageIndex="_detailPageIndex"
       @bind-PageSize="_detailPageSize"
       OnChange="EquipmentPropertyOnChange"
       Size="TableSize.Small"
       RowKey="x=>x.Id">
    <Selection Key="@(context.Id.ToString())" />
    <PropertyColumn Property="c=>c.Id" Hidden />
    <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" />
    <ActionColumn Title="操作">
        <Space>
            <SpaceItem>
                <Button Type="@ButtonType.Primary" Icon="@IconType.Outline.Edit" Size="@ButtonSize.Small">修改</Button>
                <Button Type="@ButtonType.Primary" Icon="@IconType.Outline.Delete" Size="@ButtonSize.Small" Danger>删除</Button>
            </SpaceItem>
        </Space>
    </ActionColumn>
</Table>

@code {
    [Parameter]
    public int EquipmentId { get; set; } = 0;

    ITable detailTable = null!;
    List<EquipmentProperty> equipmentProperties = new List<EquipmentProperty>();

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

    SystemLog log = SystemLog.Instance;

    bool load = false;

    protected override Task OnParametersSetAsync()
    {
        load = false;
        _detailPageIndex = 1;
        LoadEquipmentPropertyData(_detailPageIndex, _detailPageSize).Wait();
        _ = InvokeAsync(async () =>
     {
         try
         {
             load = true;
             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 (load);
         }
         catch (Exception ex)
         {
             log.LogError($"设备详细页面数据刷新异常:{ex.Message}");
         }
     });
        return base.OnParametersSetAsync();
    }

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

    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();
        });
    }
}