WorkpieceModelPage.razor 6.72 KB
@page "/Basic/WorkpieceModel"
@using System.Text.Json;
@using AntDesign.TableModels
@using System.ComponentModel
@using DataAcquisition.DataAccess
@using DataAcquisition.Models
@using Microsoft.EntityFrameworkCore
@inject IDbContextFactory<DataContext> dbContextFactory;
@inject ModalService _modalService
@inject IMessageService _message

<Flex Justify="flex-end" Align="center">
    <Button Type="@ButtonType.Primary" Icon="@IconType.Outline.Plus" OnClick="@(()=>{ _visible_add = true; })">新增</Button>
    <Button Danger Type="@ButtonType.Primary" Icon="@IconType.Outline.Delete" OnClick="BatchDelete">删除</Button>
</Flex>
<Table @ref="table"
       TItem="WorkpieceModel"
       Loading="loading"
       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.Code" />
    <PropertyColumn Title="名称" Property="c=>c.Name" />
    <PropertyColumn Title="标识" Property="c=>c.Flag" />
    <PropertyColumn Title="备注" Property="c=>c.Remark" />
    <ActionColumn Title="操作">
        <Space>
            <SpaceItem>
                <Button Type="@ButtonType.Primary"
                        Icon="@IconType.Outline.Edit"
                        Size="@ButtonSize.Small"
                        OnClick="()=>OpenEditMode(context.Id)">
                    编辑
                </Button>
            </SpaceItem>
        </Space>
    </ActionColumn>
</Table>

<Modal Title="新增" @bind-Visible="@_visible_add" OkText="@("保存")" OnOk="AddActionSaveChange">
    <Form Model="@model">
        <FormItem Label="编码">
            <Input @bind-Value="@context.Code" />
        </FormItem>

        <FormItem Label="名称">
            <Input @bind-Value="@context.Name" />
        </FormItem>

        <FormItem Label="标识">
            <Input @bind-Value="@context.Flag" />
        </FormItem>

        <FormItem Label="备注">
            <Input @bind-Value="@context.Remark" />
        </FormItem>
    </Form>
</Modal>

<Modal Title="编辑" @bind-Visible="@_visible_edit" OkText="@("保存")" OnOk="EditActionSaveChange">
    <Form Model="@editModel">
        <FormItem Label="编码">
            <Input @bind-Value="@context.Code" />
        </FormItem>

        <FormItem Label="名称">
            <Input @bind-Value="@context.Name" />
        </FormItem>

        <FormItem Label="标识">
            <Input @bind-Value="@context.Flag" />
        </FormItem>

        <FormItem Label="备注">
            <Input @bind-Value="@context.Remark" />
        </FormItem>
    </Form>
</Modal>

@code {

    bool loading = true;

    bool _visible_add = false;
    bool _visible_edit = false;

    private WorkpieceModel model = new WorkpieceModel();

    private WorkpieceModel editModel = new WorkpieceModel();

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

    IEnumerable<WorkpieceModel> selectedRows = new List<WorkpieceModel>();

    ITable table = null!;

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

    public async Task OnChange(QueryModel<WorkpieceModel> queryModel)
    {
        loading = true;
        using var dbContext = dbContextFactory.CreateDbContext();
        var query = dbContext.WorkpieceModels;

        dataItems = await query.OrderBy(x => x.Id).Skip(((queryModel.PageIndex - 1) * queryModel.PageSize)).Take(queryModel.PageSize).AsNoTracking().ToListAsync();

        _total = await query.CountAsync();
        loading = false;
    }

    public void RemoveSelection(int id)
    {
        var selected = selectedRows.Where(x => x.Id != id);
        selectedRows = selected;
    }

    public void AddActionSaveChange()
    {
        try
        {
            using var dbContext = dbContextFactory.CreateDbContext();
            var item = model;
            dbContext.WorkpieceModels.Add(item);
            dbContext.SaveChanges();
            _message.Success($"新增成功!");
            table.ReloadData();
            model = new WorkpieceModel();
        }
        catch (Exception ex)
        {
            _message.Error($"操作失败,{ex.Message}");
        }
    }

    public void OpenEditMode(int id)
    {
        try
        {
            var dbContext = dbContextFactory.CreateDbContext();
            editModel = dbContext.WorkpieceModels.Where(x => x.Id == id).First();
            _visible_edit = true;
        }
        catch (Exception ex)
        {
            _message.Error($"操作失败,{ex.Message}");
            _visible_edit = false;
        }
    }

    public void EditActionSaveChange()
    {
        try
        {
            var dbContext = dbContextFactory.CreateDbContext();
            dbContext.WorkpieceModels.Update(editModel);
            dbContext.SaveChanges();
            _message.Success($"修改数据成功");
            table.ReloadData();
        }
        catch (Exception ex)
        {
            _message.Error($"操作失败,{ex.Message}");
        }
    }

    public void BatchDelete()
    {
        if (!selectedRows.Any())
        {
            _message.Warning($"请勾选需删除的数据后再进行操作!");
            return;
        }
        _modalService.Confirm(new ConfirmOptions
                {
                    Title = "确定删除选中的数据?",
                    Icon = icon,
                    // Content = "Some descriptions",
                    OnOk = (e) =>
                    {
                        try
                        {
                            using var dbContext = dbContextFactory.CreateDbContext();
                            var ids = selectedRows.Select(x => x.Id).ToList();
                            var count = dbContext.WorkpieceModels.Where(x => ids.Contains(x.Id)).ExecuteDelete();
                            _message.Success($"成功删除{count}条数据");
                            table.ReloadData();
                        }
                        catch (Exception ex)
                        {
                            _message.Success($"操作失败,{ex.Message}");
                        }


                        return Task.CompletedTask;
                    },
                    OkType = "danger",
                });
    }

    Func<ModalClosingEventArgs, Task> onOk = (e) =>
    {
        Console.WriteLine("Ok");
        return Task.CompletedTask;
    };
    Func<ModalClosingEventArgs, Task> onCancel = (e) =>
    {
        Console.WriteLine("Cancel");
        return Task.CompletedTask;
    };

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