codeFirst实体建表

This commit is contained in:
不做码农
2023-06-07 22:28:06 +08:00
parent 628df80df7
commit b96edfdac7
47 changed files with 688 additions and 194 deletions

View File

@@ -58,16 +58,16 @@ namespace ZR.Admin.WebApi.Controllers.System
/// <summary>
/// 添加角色
/// </summary>
/// <param name="sysRoleDto"></param>
/// <param name="dto"></param>
/// <returns></returns>
[HttpPost]
[ActionPermissionFilter(Permission = "system:role:add")]
[Log(Title = "角色管理", BusinessType = BusinessType.INSERT)]
[Route("edit")]
public IActionResult RoleAdd([FromBody] SysRole sysRoleDto)
public IActionResult RoleAdd([FromBody] SysRoleDto dto)
{
if (sysRoleDto == null) return ToResponse(ApiResult.Error(101, "请求参数错误"));
if (dto == null) return ToResponse(ApiResult.Error(101, "请求参数错误"));
SysRole sysRoleDto = dto.Adapt<SysRole>();
if (UserConstants.NOT_UNIQUE.Equals(sysRoleService.CheckRoleKeyUnique(sysRoleDto)))
{
return ToResponse(ApiResult.Error((int)ResultCode.CUSTOM_ERROR, $"新增角色'{sysRoleDto.RoleName}'失败,角色权限已存在"));
@@ -82,18 +82,19 @@ namespace ZR.Admin.WebApi.Controllers.System
/// <summary>
/// 修改角色
/// </summary>
/// <param name="sysRoleDto"></param>
/// <param name="dto"></param>
/// <returns></returns>
[HttpPut]
[ActionPermissionFilter(Permission = "system:role:edit")]
[Log(Title = "角色管理", BusinessType = BusinessType.UPDATE)]
[Route("edit")]
public IActionResult RoleEdit([FromBody] SysRole sysRoleDto)
public IActionResult RoleEdit([FromBody] SysRoleDto dto)
{
if (sysRoleDto == null || sysRoleDto.RoleId <= 0 || string.IsNullOrEmpty(sysRoleDto.RoleKey))
if (dto == null || dto.RoleId <= 0 || string.IsNullOrEmpty(dto.RoleKey))
{
return ToResponse(ApiResult.Error(101, "请求参数错误"));
}
SysRole sysRoleDto = dto.Adapt<SysRole>();
sysRoleService.CheckRoleAllowed(sysRoleDto);
var info = sysRoleService.SelectRoleById(sysRoleDto.RoleId);
if (info != null && info.RoleKey != sysRoleDto.RoleKey)

View File

@@ -9,6 +9,7 @@ using ZR.Admin.WebApi.Extensions;
using ZR.Admin.WebApi.Filters;
using ZR.Model;
using ZR.Model.System;
using ZR.Model.System.Dto;
using ZR.Service.System.IService;
namespace ZR.Admin.WebApi.Controllers.System
@@ -44,7 +45,7 @@ namespace ZR.Admin.WebApi.Controllers.System
/// <returns></returns>
[ActionPermissionFilter(Permission = "system:user:list")]
[HttpGet("list")]
public IActionResult List([FromQuery] SysUser user, PagerInfo pager)
public IActionResult List([FromQuery] SysUserQueryDto user, PagerInfo pager)
{
var list = UserService.SelectUserList(user, pager);
@@ -208,7 +209,7 @@ namespace ZR.Admin.WebApi.Controllers.System
[HttpGet("export")]
[Log(Title = "用户导出", BusinessType = BusinessType.EXPORT)]
[ActionPermissionFilter(Permission = "system:user:export")]
public IActionResult UserExport([FromQuery] SysUser user)
public IActionResult UserExport([FromQuery] SysUserQueryDto user)
{
var list = UserService.SelectUserList(user, new PagerInfo(1, 10000));

View File

@@ -1,9 +1,11 @@
using Infrastructure;
using Infrastructure.Extensions;
using Infrastructure.Helper;
using SqlSugar;
using SqlSugar.IOC;
using System.Reflection;
using ZR.Admin.WebApi.Framework;
using ZR.Model;
using ZR.Model.System;
namespace ZR.Admin.WebApi.Extensions
@@ -22,12 +24,16 @@ namespace ZR.Admin.WebApi.Extensions
//仅本人数据权限
public static long DATA_SCOPE_SELF = 5;
private static XmlCommentHelper commentHelper = new XmlCommentHelper();
/// <summary>
/// 初始化db
/// </summary>
/// <param name="services"></param>
/// <param name="Configuration"></param>
public static void AddDb(this IServiceCollection services, IConfiguration Configuration)
{
commentHelper.LoadAll();
List<DbConfigs> dbConfigs = Configuration.GetSection("DbConfigs").Get<List<DbConfigs>>();
var iocList = new List<IocConfig>();
@@ -110,12 +116,44 @@ namespace ZR.Admin.WebApi.Extensions
DataInfoCacheService = cache,
EntityService = (c, p) =>
{
if (db.GetConnectionScope(configId).CurrentConnectionConfig.DbType == DbType.PostgreSQL && p.DataType != null && p.DataType.Contains("nvarchar"))
p.DbTableName = p.DbTableName.FirstLowerCase();
p.DbColumnName = p.DbColumnName.FirstLowerCase();
var des = commentHelper.GetFieldOrPropertyComment(c);
if (des.IsNotEmpty())
{
p.DataType = "text";
p.ColumnDescription = des;
}
if (db.GetConnectionScope(configId).CurrentConnectionConfig.DbType == DbType.PostgreSQL)
{
if (p.DataType != null && p.DataType.Contains("nvarchar"))
{
p.DataType = "text";
}
if (c.Name == nameof(SysMenu.IsCache) || c.Name == nameof(SysMenu.IsFrame))
{
p.DataType = "char(1)";
}
}
if (p.IsPrimarykey == true)//主键不能为null
{
p.IsNullable = false;
}
else if (p.ExtendedAttribute?.ToString() == ProteryConstant.NOTNULL.ToString())
{
p.IsNullable = false;
}
else//则否默认为null
{
p.IsNullable = true;
}
}
};
db.GetConnectionScope(configId).Aop.DataExecuting = (oldValue, entiyInfo) =>
{
};
}
/// <summary>
@@ -130,7 +168,9 @@ namespace ZR.Admin.WebApi.Extensions
var baseType = typeof(SysBase);
var entityes = AssemblyUtils.GetAllTypes().Where(p => !p.IsAbstract && p != baseType && /*p.IsAssignableTo(baseType) && */p.GetCustomAttribute<SugarTable>() != null).ToArray();
db.CodeFirst.SetStringDefaultLength(200).InitTables(entityes);
//23个表
db.CodeFirst.InitTables(entityes);
}
private static object GetParsValue(SugarParameter x)

View File

@@ -38,7 +38,7 @@ namespace ZR.Admin.WebApi.Extensions
public static void AddSwaggerConfig(this IServiceCollection services)
{
if (services == null) throw new ArgumentNullException(nameof(services));
//IWebHostEnvironment hostEnvironment = App.GetRequiredService<IWebHostEnvironment>();
//IWebHostEnvironment hostEnvironment = services.BuildServiceProvider().GetRequiredService<IWebHostEnvironment>();
services.AddSwaggerGen(c =>
{
@@ -50,10 +50,10 @@ namespace ZR.Admin.WebApi.Extensions
});
try
{
var tempPath = "";// hostEnvironment.ContentRootPath;
//var tempPath = hostEnvironment.ContentRootPath;
//添加文档注释
c.IncludeXmlComments(Path.Combine(tempPath, "ZRAdmin.xml"), true);
c.IncludeXmlComments(Path.Combine(tempPath, "ZRModel.xml"), true);
c.IncludeXmlComments(Path.Combine("ZRAdmin.xml"), true);
c.IncludeXmlComments(Path.Combine("ZRModel.xml"), true);
//c.IncludeXmlComments(Path.Combine(Directory.GetParent(tempPath).FullName, "ZR.Model", "ZRModel.xml"), true);
}
catch (Exception ex)

View File

@@ -2,7 +2,7 @@
using Infrastructure.Model;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using ZR.Model.System;
using ZR.Model.System.Dto;
namespace ZR.Admin.WebApi.Filters
{

View File

@@ -9,7 +9,7 @@ using System;
using System.Linq;
using ZR.Admin.WebApi.Extensions;
using ZR.Admin.WebApi.Framework;
using ZR.Model.System;
using ZR.Model.System.Dto;
namespace ZR.Admin.WebApi.Filters
{

View File

@@ -6,7 +6,7 @@ using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using System.Text;
using ZR.Admin.WebApi.Extensions;
using ZR.Model.System;
using ZR.Model.System.Dto;
using ZR.Service.System;
namespace ZR.Admin.WebApi.Framework