🚀 feat(新增): 新增基础实体类

This commit is contained in:
bunny 2024-09-12 09:59:18 +08:00
parent 1c06b90242
commit faf760029e
27 changed files with 250 additions and 138 deletions

View File

@ -2,19 +2,7 @@
<project version="4">
<component name="GitCommitMessageStorage">
<option name="messageStorage">
<MessageStorage>
<option name="commitTemplate">
<CommitTemplate>
<option name="body" value="" />
<option name="changes" value="" />
<option name="closes" value="" />
<option name="scope" value="" />
<option name="skipCi" value="" />
<option name="subject" value="" />
<option name="type" value="feat" />
</CommitTemplate>
</option>
</MessageStorage>
<MessageStorage />
</option>
</component>
</project>

View File

@ -1,5 +1,7 @@
using Bunny.Dao.Entity.Base;
using Bunny.Dao.Entity;
using Bunny.Dao.Entity.System;
using Bunny.Dao.Entity.System.Menu;
using Bunny.Dao.Entity.System.User;
using log4net;
using Microsoft.EntityFrameworkCore;
@ -20,16 +22,22 @@ public class EfCoreContext : DbContext
{
var dataBaseConnection = AppSettings.GetAppConfig<string>("DataBase:DataBaseConnection");
// const string dataBaseConnection =
// "192.168.3.98;Initial Catalog=BunnyDemo;TrustServerCertificate=True;Persist Security Info=True;User ID=sa;Password=abc1234.";
// "192.168.3.98;Initial Catalog=BunnyDemo;TrustServerCertificate=True;Persist Security Info=True;Users ID=sa;Password=abc1234.";
DbPath = $"Data Source={dataBaseConnection}";
}
// 连接路径
private string DbPath { get; }
public DbSet<Blog> Blogs { get; set; }
public DbSet<Post> Posts { get; set; }
public DbSet<Product> Products { get; set; }
public DbSet<User> Users { get; set; }
public DbSet<Icons> Icons { get; set; }
public DbSet<Dept> Dept { get; set; }
public DbSet<Users> Users { get; set; }
public DbSet<Roles> Roles { get; set; }
public DbSet<Permissions> Permissions { get; set; }
public DbSet<UserRoles> UserRoles { get; set; }
public DbSet<UserPermissions> UserPermissions { get; set; }
public DbSet<Menus> Menus { get; set; }
public DbSet<MenuRoles> MenuRoles { get; set; }
public DbSet<MenuPermissions> MenuPermissions { get; set; }
/// <summary>
/// 配置数据库上下文选项,指定数据库连接字符串
@ -45,6 +53,10 @@ public class EfCoreContext : DbContext
/// </summary>
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
// 为每个表指定 blogging 架构
modelBuilder.HasDefaultSchema("blogging");
// 根据基础类型创建数据库表
foreach (var entityType in modelBuilder.Model.GetEntityTypes())
if (typeof(BaseEntity).IsAssignableFrom(entityType.ClrType))
entityType.AddSoftDeleteQueryFilter();
@ -106,11 +118,12 @@ public class EfCoreContext : DbContext
if (entity.Id == Guid.Empty) entity.Id = Guid.NewGuid();
entity.CreateTime = DateTime.Now;
entity.UpdateTime = DateTime.Now;
entity.Version = DateTime.Now;
// 判断用户Id是否为空
if (userId != null)
if (userId != null && userId != 0)
{
entity.CreateUserId = userId!.Value;
entity.UpdateUserId = userId!.Value;
entity.CreateUserId = userId.Value;
entity.UpdateUserId = userId.Value;
}
break;
@ -118,7 +131,7 @@ public class EfCoreContext : DbContext
// 修改操作
case EntityState.Modified:
entity.UpdateTime = DateTime.Now;
if (userId != null) entity.UpdateUserId = userId!.Value;
if (userId != null) entity.UpdateUserId = userId.Value;
break;
}
}

View File

@ -1,6 +1,6 @@
using System.Linq.Expressions;
using System.Reflection;
using Bunny.Dao.Entity.Base;
using Bunny.Dao.Entity;
using Microsoft.EntityFrameworkCore.Metadata;
namespace Bunny.Common.Context.Database;

View File

@ -124,7 +124,7 @@ namespace Bunny.Common.Migrations
b.ToTable("Products");
});
modelBuilder.Entity("Bunny.Dao.Entity.System.User", b =>
modelBuilder.Entity("Bunny.Dao.Entity.System.Users", b =>
{
b.Property<string>("Id")
.HasColumnType("nvarchar(450)");

View File

@ -9,6 +9,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Abstractions" Version="2.2.0"/>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="2.2.5"/>
<PackageReference Include="Microsoft.EntityFrameworkCore.Abstractions" Version="8.0.8"/>
</ItemGroup>
<ItemGroup>

View File

@ -1,20 +1,27 @@
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Microsoft.EntityFrameworkCore;
namespace Bunny.Dao.Entity.Base;
namespace Bunny.Dao.Entity;
public class BaseEntity
{
[Key] public Guid? Id { get; set; }
[Comment("主键")]
[Column(Order = 0)]
[Key]
public Guid? Id { get; set; }
public DateTime CreateTime { get; set; }
[Comment("创建时间")] public DateTime CreateTime { get; set; }
public DateTime UpdateTime { get; set; }
[Comment("更新时间")] public DateTime UpdateTime { get; set; }
public long CreateUserId { get; set; }
[Comment("创建用户id")] public long? CreateUserId { get; set; }
public long UpdateUserId { get; set; }
[Comment("更新用户id")] public long? UpdateUserId { get; set; }
public bool IsDeleted { get; set; }
[Comment("是否删除")] public bool IsDeleted { get; set; }
[Timestamp] public byte[]? Version { get; set; }
[Comment("并发版本")]
[Column(TypeName = "datetime")]
public DateTime Version { get; set; }
}

View File

@ -1,13 +0,0 @@
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Bunny.Dao.Entity.Base;
namespace Bunny.Dao.Entity.System;
[Table("Blog")]
public class Blog : BaseEntity
{
[Required]
[Column("BlogUrl", TypeName = "varchar(600)")]
public string? Url { get; init; }
}

View File

@ -0,0 +1,20 @@
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Microsoft.EntityFrameworkCore;
namespace Bunny.Dao.Entity.System;
[Comment("系统部门表")]
[Table("System_Dept")]
public class Dept : BaseEntity
{
[Comment("部门名称")] [MaxLength(150)] public string? DeptName { get; init; }
[Comment("排序字段")] public int Sort { get; init; }
[Comment("负责人")] [MaxLength(100)] public string? Manager { get; init; }
[Comment("电话")] [MaxLength(100)] public string? Phone { get; init; }
[Comment("邮箱")] [MaxLength(200)] public string? Email { get; init; }
}

View File

@ -0,0 +1,12 @@
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Microsoft.EntityFrameworkCore;
namespace Bunny.Dao.Entity.System;
[Table("System_Icons")]
[Comment("系统图标表")]
public class Icons:BaseEntity
{
[Comment("图标名称")] [MaxLength(200)] public string? IconName { get; init; }
}

View File

@ -0,0 +1,30 @@
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Microsoft.EntityFrameworkCore;
namespace Bunny.Dao.Entity.System.Menu;
[Table("System_Menus")]
[Comment("菜单表")]
public class Menus : BaseEntity
{
[Comment("菜单父级id")] public Guid PatentId { get; init; }
[Comment("菜单路径/路由路径")]
[MaxLength(500)]
public string? Path { get; init; }
[Comment("菜单名称/路由名称")]
[MaxLength(150)]
public string? MenuName { get; init; }
[Comment("菜单标题/路由标题")]
[MaxLength(150)]
public string? Title { get; init; }
[Comment("菜单图标/路由图标")]
[MaxLength(200)]
public string? Icon { get; init; }
[Comment("菜单/路由等级")] public int RouterRank { get; init; }
}

View File

@ -0,0 +1,13 @@
using System.ComponentModel.DataAnnotations.Schema;
using Microsoft.EntityFrameworkCore;
namespace Bunny.Dao.Entity.System.Menu;
[Table("System_MenuPermissions")]
[Comment("系统菜单权限关系表")]
public class MenuPermissions : BaseEntity
{
[Comment("菜单/路由id")] public Guid MenuId { get; init; }
[Comment("权限id")] public Guid PermissionId { get; init; }
}

View File

@ -0,0 +1,13 @@
using System.ComponentModel.DataAnnotations.Schema;
using Microsoft.EntityFrameworkCore;
namespace Bunny.Dao.Entity.System.Menu;
[Table("System_MenuRoles")]
[Comment("系统菜单和角色关系表")]
public class MenuRoles : BaseEntity
{
[Comment("菜单/路由id")] public Guid MenuId { get; init; }
[Comment("角色id")] public Guid RoleId { get; init; }
}

View File

@ -0,0 +1,14 @@
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Microsoft.EntityFrameworkCore;
namespace Bunny.Dao.Entity.System;
[Table("System_Permissions")]
[Comment("系统权限表")]
public class Permissions : BaseEntity
{
[Comment("权限代码")] [MaxLength(50)] public string? PermissionsCode { get; set; }
[Comment("权限名称")] [MaxLength(150)] public string? PermissionsName { get; set; }
}

View File

@ -1,11 +0,0 @@
using System.ComponentModel.DataAnnotations;
using Bunny.Dao.Entity.Base;
namespace Bunny.Dao.Entity.System;
public class Post : BaseEntity
{
[MaxLength(600)] public string? Title { get; init; }
public string? Content { get; init; }
public Blog? Blog { get; init; }
}

View File

@ -1,12 +0,0 @@
using Bunny.Dao.Entity.Base;
namespace Bunny.Dao.Entity.System;
public class Product : BaseEntity
{
public string? CustomerId { get; set; }
public string? CustomerName { get; set; }
public string? PackageType { get; set; }
}

View File

@ -0,0 +1,14 @@
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Microsoft.EntityFrameworkCore;
namespace Bunny.Dao.Entity.System;
[Table("System_Roles")]
[Comment("系统角色表")]
public class Roles : BaseEntity
{
[Comment("角色代码")] [MaxLength(50)] public string? RoleCode { get; init; }
[Comment("角色详情解释")] [MaxLength(150)] public string? RoleName { get; init; }
}

View File

@ -1,20 +0,0 @@
using Bunny.Dao.Entity.Base;
namespace Bunny.Dao.Entity.System;
public class User : BaseEntity
{
public string? UserName { get; set; }
public string? Password { get; set; }
public string? Email { get; set; }
public string? Phone { get; set; }
public string? QrCode { get; set; }
public string? CompanyCode { get; set; }
public string? DeptCode { get; set; }
}

View File

@ -0,0 +1,13 @@
using System.ComponentModel.DataAnnotations.Schema;
using Microsoft.EntityFrameworkCore;
namespace Bunny.Dao.Entity.System.User;
[Table("System_UserPermissions")]
[Comment("系统用户权限关系表")]
public class UserPermissions : BaseEntity
{
[Comment("用户id")] public Guid UserId { get; init; }
[Comment("权限id")] public Guid PermissionId { get; init; }
}

View File

@ -0,0 +1,13 @@
using System.ComponentModel.DataAnnotations.Schema;
using Microsoft.EntityFrameworkCore;
namespace Bunny.Dao.Entity.System.User;
[Table("System_UserRoles")]
[Comment("系统用户和角色关系表")]
public class UserRoles : BaseEntity
{
[Comment("用户id")] public Guid UserId { get; init; }
[Comment("角色id")] public Guid RoleId { get; init; }
}

View File

@ -0,0 +1,18 @@
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Microsoft.EntityFrameworkCore;
namespace Bunny.Dao.Entity.System.User;
[Table("System_Users", Schema = "blogging")]
[Comment("系统用户表")]
public class Users : BaseEntity
{
[Comment("用户名")] [MaxLength(100)] public string? Username { get; init; }
[Comment("用户密码")] [MaxLength(150)] public string? Password { get; init; }
[Comment("用户性别")] [Column] public int Sex { get; init; }
[Comment("部门编码")] [MaxLength(200)] public string? DeptCode { get; init; }
}

View File

@ -1,4 +1,4 @@
using Bunny.Dao.Entity.Base;
using Bunny.Dao.Entity;
namespace Bunny.Dao.Vo.User;

View File

@ -1,22 +1,22 @@
using Bunny.Dao.Common.Result;
using Bunny.Dao.Dto.System;
using Bunny.Dao.Entity.System;
using Bunny.Dao.Entity.System.User;
namespace Bunny.Service.IService;
public interface IBlogService
public interface IUserService
{
/// <summary>
/// 添加Blog
/// </summary>
/// <param name="dto"></param>
void AddBlog(Blog dto);
void AddBlog(Users dto);
/// <summary>
/// 查询BLog
/// </summary>
/// <returns></returns>
List<Blog> QueryBlog();
List<Users> QueryBlog();
/// <summary>
/// 更新Blog内容
@ -51,5 +51,5 @@ public interface IBlogService
/// </summary>
/// <param name="page"></param>
/// <param name="limit"></param>
PageResult<Blog> QueryPage(int page, int limit);
PageResult<Users> QueryPage(int page, int limit);
}

View File

@ -1,5 +1,5 @@
using Bunny.Common.Context.Middleware;
using Bunny.Dao.Entity.System;
using Bunny.Dao.Entity.System.User;
using Newtonsoft.Json;
using StackExchange.Redis;
@ -58,11 +58,11 @@ public class RedisOptionService : IRedisOptionService
/// <returns></returns>
public string AddJson()
{
var post = new Post
var post = new Users
{
Id = Guid.NewGuid(),
Title = "存入JSON内容",
Content = "正在存入JSON内容"
Username = "存入JSON内容",
Password = "正在存入JSON内容"
};
var json = JsonConvert.SerializeObject(post);
_redisDatabase.StringSet("postKey", json);

View File

@ -4,12 +4,12 @@ using Bunny.Common.Exception;
using Bunny.Common.Utils.Net;
using Bunny.Dao.Common.Result;
using Bunny.Dao.Dto.System;
using Bunny.Dao.Entity.System;
using Bunny.Dao.Entity.System.User;
using Microsoft.AspNetCore.Components;
namespace Bunny.Service.IService.Service;
public class BlogService : IBlogService
public class UserService : IUserService
{
[Inject] public required EfCoreContext DbContext { get; set; }
@ -18,7 +18,7 @@ public class BlogService : IBlogService
/// </summary>
/// <param name="dto"></param>
/// <exception cref="NotImplementedException"></exception>
public void AddBlog(Blog dto)
public void AddBlog(Users dto)
{
DbContext.Add(dto);
DbContext.SaveChanges();
@ -28,9 +28,9 @@ public class BlogService : IBlogService
/// 查询BLog
/// </summary>
/// <returns></returns>
public List<Blog> QueryBlog()
public List<Users> QueryBlog()
{
return DbContext.Blogs.ToList();
return DbContext.Users.ToList();
}
/// <summary>
@ -39,10 +39,10 @@ public class BlogService : IBlogService
/// <param name="dto"></param>
public void UpdateBlog(BlogUpdateDto dto)
{
var blog = new Blog();
var blog = new Users();
NetUtil.CopyProperties(dto, blog);
DbContext.Blogs.Update(blog);
DbContext.Users.Update(blog);
DbContext.SaveChanges();
}
@ -52,8 +52,8 @@ public class BlogService : IBlogService
/// <param name="id"></param>
public void DeleteBlog(string id)
{
var blog = new Blog { Id = Guid.NewGuid() };
DbContext.Blogs.Remove(blog);
var blog = new Users { Id = Guid.NewGuid() };
DbContext.Users.Remove(blog);
DbContext.SaveChanges();
}
@ -64,12 +64,12 @@ public class BlogService : IBlogService
[Transaction]
public void AddBatchBlogs(string url)
{
var list = new List<Blog>();
var list = new List<Users>();
for (var i = 0; i <= 100; i++)
list.Add(new Blog { Url = $"Blog{i}" });
list.Add(new Users { Username = $"Blog{i}" });
DbContext.Blogs.AddRange(list);
DbContext.Users.AddRange(list);
DbContext.SaveChanges();
throw new BunnyException("测试事务注解");
}
@ -79,14 +79,14 @@ public class BlogService : IBlogService
/// </summary>
public void DeleteBatchBlogs()
{
var list = new List<Blog>();
var list = new List<Users>();
for (var i = 0; i < 10; i++)
{
var blog = new Blog();
var blog = new Users();
list.Add(blog);
}
DbContext.Blogs.RemoveRange(list);
DbContext.Users.RemoveRange(list);
DbContext.SaveChanges();
}
@ -99,15 +99,15 @@ public class BlogService : IBlogService
var transaction = DbContext.Database.BeginTransaction();
// 执行批量更新操作
var list = new List<Blog>();
var list = new List<Users>();
for (var i = 0; i < 10; i++)
{
var blog = new Blog { Url = "https://learn.microsoft.com/zh-cn/ef/core/saving/transactions" };
var blog = new Users { Username = "transactions" };
list.Add(blog);
}
// 更新内容
DbContext.Blogs.UpdateRange(list);
DbContext.Users.UpdateRange(list);
DbContext.SaveChanges();
// 还可以使用异步方式
@ -119,15 +119,14 @@ public class BlogService : IBlogService
/// </summary>
/// <param name="page"></param>
/// <param name="limit"></param>
public PageResult<Blog> QueryPage(int page, int limit)
public PageResult<Users> QueryPage(int page, int limit)
{
var items = DbContext.Blogs.Take(limit).Skip(page).ToList();
var items = DbContext.Users.Take(limit).Skip(page).ToList();
var total = items.Count;
var pages = (int)Math.Ceiling(total / (double)limit);
var blogs = items.Skip((page - 1) * limit).Take(limit).ToList();
return new PageResult<Blog>
return new PageResult<Users>
{
PageNo = page,
Total = total,

View File

@ -24,7 +24,7 @@ public static class AddAutofacConfig
// 注入Service服务
builder.RegisterType<BaseService>().As<IBaseService>();
builder.RegisterType<BlogService>().As<IBlogService>();
builder.RegisterType<UserService>().As<IUserService>();
builder.RegisterType<RedisOptionService>().As<IRedisOptionService>();
builder.RegisterType<JobService>().As<IJobService>();
builder.RegisterType<MinioService>().As<IMinioService>();

View File

@ -46,7 +46,7 @@
<connectionType
value="System.Data.SqlClient.SqlConnection,System.Data.SqlClient, Version=4.6.1.3, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
<connectionString
value="Data Source=192.168.3.98;Initial Catalog=LogManager;Persist Security Info=True;User ID=sa;Password=abc1234."/>
value="Data Source=192.168.3.98;Initial Catalog=LogManager;Persist Security Info=True;Users ID=sa;Password=abc1234."/>
<commandText
value="INSERT INTO Log4Net ([Date],[Thread],[Level],[Logger],[Message],[Exception]) VALUES (@log_date, @thread, @log_level, @logger, @Message, @exception)"/>
<parameter>

View File

@ -1,6 +1,6 @@
using Bunny.Dao.Common.Result;
using Bunny.Dao.Dto.System;
using Bunny.Dao.Entity.System;
using Bunny.Dao.Entity.System.User;
using Bunny.Service.IService;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Mvc;
@ -13,7 +13,7 @@ namespace Bunny.WebApi.Controllers;
[Microsoft.AspNetCore.Mvc.Route("/api/[controller]/[action]")]
public class BlogController : ControllerBase
{
[Inject] public required IBlogService BlogService { get; set; }
[Inject] public required IUserService UserService { get; set; }
/// <summary>
/// 添加Blog
@ -21,9 +21,9 @@ public class BlogController : ControllerBase
/// <param name="dto"></param>
/// <returns></returns>
[HttpPost]
public Result<object> AddBlog(Blog dto)
public Result<object> AddBlog(Users dto)
{
BlogService.AddBlog(dto);
UserService.AddBlog(dto);
return Result<object>.Success();
}
@ -32,10 +32,10 @@ public class BlogController : ControllerBase
/// </summary>
/// <returns></returns>
[HttpGet]
public Result<List<Blog>> QueryBlog()
public Result<List<Users>> QueryBlog()
{
var vo = BlogService.QueryBlog();
return Result<List<Blog>>.Success(vo);
var vo = UserService.QueryBlog();
return Result<List<Users>>.Success(vo);
}
/// <summary>
@ -46,7 +46,7 @@ public class BlogController : ControllerBase
[HttpPost]
public Result<object> UpdateBlog(BlogUpdateDto dto)
{
BlogService.UpdateBlog(dto);
UserService.UpdateBlog(dto);
return Result<object>.Success();
}
@ -57,7 +57,7 @@ public class BlogController : ControllerBase
[HttpDelete]
public Result<string> DeleteBlog(string id)
{
BlogService.DeleteBlog(id);
UserService.DeleteBlog(id);
return Result<string>.Success();
}
@ -68,7 +68,7 @@ public class BlogController : ControllerBase
[HttpPost]
public Result<string> AddBatchBlogs(string url)
{
BlogService.AddBatchBlogs(url);
UserService.AddBatchBlogs(url);
return Result<string>.Success();
}
@ -79,7 +79,7 @@ public class BlogController : ControllerBase
[HttpGet]
public Result<string> DeleteBatchBlogs()
{
BlogService.DeleteBatchBlogs();
UserService.DeleteBatchBlogs();
return Result<string>.Success();
}
@ -90,7 +90,7 @@ public class BlogController : ControllerBase
[HttpGet]
public Result<string> UseTransaction()
{
BlogService.UseTransaction();
UserService.UseTransaction();
return Result<string>.Success();
}
@ -99,10 +99,10 @@ public class BlogController : ControllerBase
/// </summary>
/// <returns></returns>
[HttpPost]
public Result<PageResult<Blog>> QueryPage(int page = 1, int limit = 10)
public Result<PageResult<Users>> QueryPage(int page = 1, int limit = 10)
{
var vo = BlogService.QueryPage(page, limit);
var vo = UserService.QueryPage(page, limit);
return Result<PageResult<Blog>>.Success(vo);
return Result<PageResult<Users>>.Success(vo);
}
}