WorkpieceProductionPage.razor 6.55 KB
@page "/DataAnalysis/WorkpieceProduction"
@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="equipmentCode">
            <AddOnBefore>设备编号</AddOnBefore>
        </AntDesign.Input>
        <Button Type="@ButtonType.Primary" Icon="@IconType.Outline.Search" @onclick="SearchEvent">搜索</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.Delete" Danger @onclick="BatchDeleteEvent">批量删除</Button>
    </Flex>
</Flex>

<Table @ref="table"
       TItem="WorkpieceProduction"
       DataSource="@dataItems"
       Total="_total"
       @bind-PageIndex="_pageIndex"
       @bind-PageSize="_pageSize"
       @bind-SelectedRows="selectedRows"
       OnChange="OnChange"
       Size="TableSize.Small"
       RowKey="x=>x.Id">
    <Selection Key="@(context.Id.ToString())" />
    <PropertyColumn Property="c=>c.Id" Hidden />
    <PropertyColumn Title="设备编号" Property="c=>c.EquipmentCode" />
    <PropertyColumn Title="设备名称" Property="c=>c.EquipmentName" />
    <PropertyColumn Title="工件型号" Property="c=>c.WorkpieceCode" />
    <PropertyColumn Title="程序号" Property="c=>c.ProgramNo" />
    <PropertyColumn Title="完成状态" Property="c=>c.IsEnd">
        <Switch @bind-Value="@context.IsEnd" Disabled="true"></Switch>
    </PropertyColumn>
    <PropertyColumn Title="开始时间" Property="c=>c.CreateTime" />
    <PropertyColumn Title="结束时间" Property="c=>c.UpdateTime" />
    <ActionColumn Title="操作">
        <Space>
            <SpaceItem>
                <Button Icon="@IconType.Outline.InfoCircle" Size="@ButtonSize.Small" OnClick="()=>OpenDetail(context)">详细</Button>
                <Button Type="@ButtonType.Default" Icon="@IconType.Outline.Delete" Size="@ButtonSize.Small" Danger @onclick="()=>DeleteEvent(context.Id)">删除</Button>
            </SpaceItem>
        </Space>
    </ActionColumn>
</Table>

<Modal Title="@modalTitle"
       @bind-Visible="@_visible"
       Maximizable="true"
       Centered="true"
       Resizable="true"
       Draggable="true"
       Width="1200"
       MaxBodyHeight="80vh"
       Footer="null"
       DestroyOnClose="true"
       DefaultMaximized="false">
    <WeldProcessRecordPage ProductionId="@productionId" />
</Modal>

@code {

    List<WorkpieceProduction> dataItems = new List<WorkpieceProduction>();

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

    string equipmentCode = string.Empty;

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

    bool _visible = false;
    string modalTitle = string.Empty;
    Guid productionId = Guid.Empty;

    public void OnChange(QueryModel<WorkpieceProduction> queryModel)
    {
        LoadData(queryModel.PageIndex, queryModel.PageSize, queryModel.SortModel);
    }

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

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

    private void LoadData(int index, int size, IList<ITableSortModel>? sortModels = null)
    {
        try
        {
            using var dbContext = dbContextFactory.CreateDbContext();
            var query = dbContext.WorkpieceProductions.Where(QueryExpression());
            dataItems = query.OrderBy(x => x.IsEnd).ThenByDescending(x => x.CreateTime).Skip(((index - 1) * size)).Take(size).AsNoTracking().ToList();
            _total = query.Count();
        }
        catch (Exception ex)
        {
            _message.Error($"加载数据失败:{ex.Message}");
        }
    }

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


    public void OpenDetail(WorkpieceProduction production)
    {
        productionId = production.Id;
        modalTitle = $"{production.EquipmentName} - 工件[{production.Id}]";
        _visible = true;
    }

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

    public void DeleteEvent(Guid id)
    {
        _modalService.Confirm(new ConfirmOptions
        {
            Title = "确认删除选中的数据?",
            Icon = icon,
            OnOk = (e) =>
            {
                try
                {
                    using var context = dbContextFactory.CreateDbContext();
                    context.WorkpieceProductions.Where(x => x.Id == id).Include(x => x.WeldProcessRecords).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.WorkpieceProductions.Where(x => selectedRows.Select(e => e.Id).Contains(x.Id)).Include(x => x.WeldProcessRecords).ExecuteDelete();
                    _message.Success($"操作成功");
                    LoadData(_pageIndex, _pageSize);
                    StateHasChanged();
                }
                catch (Exception ex)
                {
                    _message.Error($"操作出现异常:{ex.Message}");
                }
                return Task.CompletedTask;
            }
        });
    }
}