Blazor + SqlSugar 实现单表增删改功能

效果图

Blazor + SqlSugar 实现单表增删改功能_第1张图片

Blazor + SqlSugar 实现单表增删改功能_第2张图片

 前台代码

@page "/UserAccount/Index"


        
        
        @**@
        
    
        

后台代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Components;
using System.Net.Http;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.AspNetCore.Components.Forms;
using Microsoft.AspNetCore.Components.Routing;
using Microsoft.AspNetCore.Components.Web;
using Microsoft.AspNetCore.Components.Web.Virtualization;
using Microsoft.JSInterop;
using BlazorApp;
using BlazorApp.Shared;
using BootstrapBlazor.Components;
using BlazorService;
using System.Diagnostics.CodeAnalysis;

namespace BlazorApp.Pages.UserAccount
{
    public partial class Index
    {
        //弹窗组件
        [Inject]
        [NotNull]
        private ToastService? ToastService { get; set; }


        //数据库业务处理对象
        UserAccountBll bll = new UserAccountBll();

        //查询条件对象
        private BlazorORM.Entity.UserAccount SearchModel { get; set; } = new BlazorORM.Entity.UserAccount();

        

        /// 
        /// 清空搜索
        /// 
        /// 
        /// 
        private static Task OnResetSearchAsync(BlazorORM.Entity.UserAccount item)
        {
            item.Name = "";
            item.Account = "";
            return Task.CompletedTask;
        }


        /// 
        /// 查询事件
        /// 
        /// 
        /// 
        private Task> OnSearchModelQueryAsync(QueryPageOptions options)
        {
            int total = 0;
            var items = bll.GetPageList(SearchModel.Account, SearchModel.Name, options.PageIndex - 1, options.PageItems, total);
            return Task.FromResult(new QueryData()
            {
                Items = items,
                TotalCount = total,
                IsSorted = true,
                IsFiltered = options.Filters.Any(),
                IsSearch = options.Searches.Any(),
                IsAdvanceSearch = options.AdvanceSearches.Any()
            });
        }

        /// 
        /// 添加
        /// 
        /// 
        private static Task OnAddAsync() => Task.FromResult(new BlazorORM.Entity.UserAccount() { CreateTime = DateTime.Now });

        /// 
        /// 保存
        /// 
        /// 
        /// 
        /// 
        private Task OnSaveAsync(BlazorORM.Entity.UserAccount item, ItemChangedType changedType)
        {
            try
            {
                bool result = false;
                // 增加数据演示代码
                if (changedType == ItemChangedType.Add)
                {
                    item.ID = Guid.NewGuid();

                    if (bll.GetByName(item.Name) != null)
                    {
                        
                        ToastService.Show(new ToastOption()
                        {
                            Category = ToastCategory.Error,
                            Title="消息提醒",
                            Content = "名称重复"
                        });
                        
                        return Task.FromResult(false);
                    }
                    if (bll.GetByAcccount(item.Account) != null)
                    {
                        
                        ToastService.Show(new ToastOption()
                        {
                            Category = ToastCategory.Error,
                            Title = "消息提醒",
                            Content = "账号重复"
                        });
                        
                        return Task.FromResult(false);
                    }
                    result = bll.Add(item);
                }
                else
                {
                    result = bll.Update(item);
                }
                
                return Task.FromResult(true);
            }
            catch (Exception ex)
            {
                
                ToastService.Show(new ToastOption()
                {
                    Category = ToastCategory.Error,
                    Title = "消息提醒",
                    Content = ex.Message
                });
                
                return Task.FromResult(false);
            }
        }
        /// 
        /// 删除
        /// 
        /// 
        /// 
        private Task OnDeleteAsync(IEnumerable items)
        {
            var result = bll.DeleteByIds(items.ToList());
            
            return Task.FromResult(result);
        }
    }
}

业务处理代码

using BlazorORM;
using BlazorORM.Entity;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace BlazorService
{
    public class UserAccountBll : SqlSugarBase
    {
        /// 
        /// 分页数据
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        public List GetPageList(string account, string name, int pageIndex, int pageSize, int totalCount)
        {
            return DB.Queryable()
                .With(SqlWith.NoLock)
                .WhereIF(string.IsNullOrEmpty(account) == false, x => x.Account.Contains(account))
                .WhereIF(string.IsNullOrEmpty(name) == false, x => x.Name.Contains(name))
                .OrderBy(x => x.IDQue)
                .ToPageList(pageIndex, pageSize, ref totalCount);
        }
        /// 
        /// 根据名称获取
        /// 
        /// 
        /// 
        public UserAccount GetByName(string name)
        {
            return DB.Queryable()
                .With(SqlWith.NoLock)
                .Where(x => x.Name == name).ToList().FirstOrDefault();
        }
        /// 
        /// 根据账号获取
        /// 
        /// 
        /// 
        public UserAccount GetByAcccount(string account)
        {
            return DB.Queryable()
                .With(SqlWith.NoLock)
                .Where(x => x.Account == account).ToList().FirstOrDefault();
        }

        /// 
        /// 添加
        /// 
        /// 
        /// 
        public bool Add(UserAccount entity)
        {
            return DB.Insertable(entity).ExecuteCommand() > 0;
        }

        /// 
        /// 修改
        /// 
        /// 
        /// 
        public bool Update(UserAccount entity)
        {
            return DB.Updateable(entity).ExecuteCommand() > 0;
        }

        /// 
        /// 删除
        /// 
        /// 
        /// 
        public bool DeleteByIds(List entitysList)
        {
            return DB.Deleteable(entitysList).ExecuteCommand() > 0;
        }

    }
}

你可能感兴趣的:(C#,Blazor,前端,html,javascript)