mirror of
https://gitee.com/dotnetchina/OpenAuth.Net.git
synced 2025-07-17 19:37:45 +08:00
feat: 强化多表代码生成
This commit is contained in:
parent
de12924fac
commit
b23be0571f
@ -273,6 +273,11 @@ namespace OpenAuth.App
|
||||
CheckExistsModule(sysTableInfo.ModuleCode);
|
||||
|
||||
string domainContent = string.Empty;
|
||||
//查找是否存在子表的情况
|
||||
var subTable = Repository.FirstOrDefault(u => u.ParentTableId == sysTableInfo.Id);
|
||||
|
||||
if (subTable == null) //如果子表不存在,则用单模版生成
|
||||
{
|
||||
if (sysTableInfo.IsDynamicHeader) //使用动态头部的模版
|
||||
{
|
||||
domainContent = FileHelper.ReadFile(@"Template\\SingleTable\\BuildAppWithDynamicHeader.html");
|
||||
@ -281,6 +286,7 @@ namespace OpenAuth.App
|
||||
{
|
||||
domainContent = FileHelper.ReadFile(@"Template\\SingleTable\\BuildApp.html");
|
||||
}
|
||||
|
||||
domainContent = domainContent
|
||||
.Replace("{TableName}", sysTableInfo.TableName)
|
||||
.Replace("{ModuleCode}", sysTableInfo.ModuleCode)
|
||||
@ -288,6 +294,7 @@ namespace OpenAuth.App
|
||||
.Replace("{ClassName}", sysTableInfo.ClassName)
|
||||
.Replace("{StartName}", StratName);
|
||||
|
||||
//如果有外键(是一个子表)
|
||||
if (!string.IsNullOrEmpty(sysTableInfo.ForeignKey))
|
||||
{ //替换外键模版
|
||||
var foreignTemplate = $"objs = objs.Where(u => u.{sysTableInfo.ForeignKey} == request.{sysTableInfo.ForeignKey});";
|
||||
@ -299,6 +306,29 @@ namespace OpenAuth.App
|
||||
domainContent = domainContent
|
||||
.Replace("{ForeignKeyTemplate}", "");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (sysTableInfo.IsDynamicHeader) //使用动态头部的模版
|
||||
{
|
||||
domainContent = FileHelper.ReadFile(@"Template\\MultiTable\\BuildAppWithDynamicHeader.html");
|
||||
}
|
||||
else
|
||||
{
|
||||
domainContent = FileHelper.ReadFile(@"Template\\MultiTable\\BuildApp.html");
|
||||
}
|
||||
|
||||
domainContent = domainContent
|
||||
.Replace("{TableName}", sysTableInfo.TableName)
|
||||
.Replace("{ModuleCode}", sysTableInfo.ModuleCode)
|
||||
.Replace("{ModuleName}", sysTableInfo.ModuleName)
|
||||
.Replace("{ClassName}", sysTableInfo.ClassName)
|
||||
.Replace("{SubForeignKey}", subTable.ForeignKey)
|
||||
.Replace("{SubClassName}", subTable.ClassName)
|
||||
.Replace("{SubModuleCode}", subTable.ModuleCode)
|
||||
.Replace("{StartName}", StratName);
|
||||
|
||||
}
|
||||
|
||||
var primarykey = sysColumns.FirstOrDefault(u => u.IsKey);
|
||||
if (primarykey == null)
|
||||
@ -537,9 +567,6 @@ namespace OpenAuth.App
|
||||
mapPath +
|
||||
$"\\OpenAuth.Repository\\Domain\\", tableInfo.ClassName + ".cs",
|
||||
domainContent);
|
||||
|
||||
string openAuthDBContextPath = mapPath + "\\OpenAuth.Repository\\OpenAuthDBContext.cs";
|
||||
FileHelper.RegxAddContentByParenthesis(openAuthDBContextPath, "public virtual DbSet<" + tableInfo.ClassName + "> " + tableInfo.TableName + "s { get; set; }");
|
||||
}
|
||||
|
||||
Dictionary<string, Type> PrimitiveTypes = new Dictionary<string, Type>()
|
||||
@ -569,7 +596,8 @@ namespace OpenAuth.App
|
||||
{
|
||||
return "DateTime.Now";
|
||||
}
|
||||
else if (type == "bool"){
|
||||
else if (type == "bool")
|
||||
{
|
||||
return "false";
|
||||
}
|
||||
return Activator.CreateInstance(t).ToString();
|
||||
|
108
OpenAuth.WebApi/Template/MultiTable/BuildApp.html
Normal file
108
OpenAuth.WebApi/Template/MultiTable/BuildApp.html
Normal file
@ -0,0 +1,108 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Infrastructure;
|
||||
using OpenAuth.App.Interface;
|
||||
using OpenAuth.App.Request;
|
||||
using OpenAuth.App.Response;
|
||||
using OpenAuth.Repository.Domain;
|
||||
using SqlSugar;
|
||||
namespace OpenAuth.App
|
||||
{
|
||||
public class {ModuleCode} : SqlSugarBaseApp<{ClassName}>
|
||||
{
|
||||
private {SubModuleCode} _{SubClassName}App;
|
||||
/// <summary>
|
||||
/// 加载列表
|
||||
/// </summary>
|
||||
public async Task<TableData> Load(Query{ClassName}ListReq request)
|
||||
{
|
||||
var loginContext = _auth.GetCurrentUser();
|
||||
if (loginContext == null)
|
||||
{
|
||||
throw new CommonException("登录已过期", Define.INVALID_TOKEN);
|
||||
}
|
||||
|
||||
var result = new TableData();
|
||||
var objs = GetDataPrivilege("u");
|
||||
if (!string.IsNullOrEmpty(request.key))
|
||||
{
|
||||
objs = objs.Where(u => u.Id.Contains(request.key));
|
||||
}
|
||||
if (!string.IsNullOrEmpty(request.sqlWhere))
|
||||
{
|
||||
objs = objs.Where(request.sqlWhere);
|
||||
}
|
||||
|
||||
result.data = objs.OrderBy(u => u.Id)
|
||||
.Skip((request.page - 1) * request.limit)
|
||||
.Take(request.limit).ToList();
|
||||
result.count = await objs.CountAsync();
|
||||
return result;
|
||||
}
|
||||
public void Add(AddOrUpdate{ClassName}Req request)
|
||||
{
|
||||
var obj = request.MapTo<{ClassName}>();
|
||||
//todo:补充或调整自己需要的字段
|
||||
obj.CreateTime = DateTime.Now;
|
||||
var user = _auth.GetCurrentUser().User;
|
||||
obj.CreateUserId = user.Id;
|
||||
obj.CreateUserName = user.Name;
|
||||
if(obj.KeyIsNull()) //如果主键为空,则生成默认值
|
||||
{
|
||||
obj.GenerateDefaultKeyVal();
|
||||
}
|
||||
|
||||
SugarClient.Ado.BeginTran();
|
||||
SugarClient.Insertable(obj).ExecuteCommand();
|
||||
if (request.{SubClassName}Reqs != null && request.{SubClassName}Reqs.Any())
|
||||
{
|
||||
foreach (var detail in request.{SubClassName}Reqs)
|
||||
{
|
||||
detail.{SubForeignKey} = obj.Id;
|
||||
_{SubClassName}App.Add(detail);
|
||||
}
|
||||
}
|
||||
SugarClient.Ado.CommitTran();
|
||||
}
|
||||
public void Update(AddOrUpdate{ClassName}Req request)
|
||||
{
|
||||
var user = _auth.GetCurrentUser().User;
|
||||
SugarClient.Ado.BeginTran();
|
||||
if (request.{SubClassName}Reqs != null && request.{SubClassName}Reqs.Any())
|
||||
{
|
||||
//请求的id不在数据库的记录,需要删除
|
||||
var containids = request.{SubClassName}Reqs.Select(u => u.Id)
|
||||
.Where(u => !string.IsNullOrEmpty(u)).ToList();
|
||||
if (containids.Any())
|
||||
{
|
||||
SugarClient.Deleteable<{SubClassName}>(u => (!containids.Contains(u.Id)) && u.{SubForeignKey} == request.Id).ExecuteCommand();
|
||||
}
|
||||
//id为空的添加
|
||||
foreach (var detail in request.{SubClassName}Reqs.Where(u => string.IsNullOrEmpty(u.Id)))
|
||||
{
|
||||
detail.{SubForeignKey} = request.Id;
|
||||
_{SubClassName}App.Add(detail);
|
||||
}
|
||||
//更新id相同的
|
||||
foreach (var detail in request.{SubClassName}Reqs.Where(u => !string.IsNullOrEmpty(u.Id)))
|
||||
{
|
||||
_{SubClassName}App.Update(detail);
|
||||
}
|
||||
}
|
||||
Repository.Update(u => new {ClassName}
|
||||
{
|
||||
UpdateTime = DateTime.Now,
|
||||
UpdateUserId = user.Id,
|
||||
UpdateUserName = user.Name
|
||||
//todo:补充或调整自己需要的字段
|
||||
}, u => u.Id == request.Id);
|
||||
SugarClient.Ado.CommitTran();
|
||||
}
|
||||
public {ModuleCode}(ISqlSugarClient client, IAuth auth,
|
||||
{SubModuleCode} {SubClassName}App) : base(client, auth)
|
||||
{
|
||||
_{SubClassName}App = {SubClassName}App;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,113 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Infrastructure;
|
||||
using OpenAuth.App.Interface;
|
||||
using OpenAuth.App.Request;
|
||||
using OpenAuth.App.Response;
|
||||
using OpenAuth.Repository.Domain;
|
||||
using SqlSugar;
|
||||
namespace OpenAuth.App
|
||||
{
|
||||
public class {ModuleCode} : SqlSugarBaseApp<{ClassName}>
|
||||
{
|
||||
private {SubModuleCode} _{SubClassName}App;
|
||||
/// <summary>
|
||||
/// 加载列表
|
||||
/// </summary>
|
||||
public async Task<TableData> Load(Query{ClassName}ListReq request)
|
||||
{
|
||||
var loginContext = _auth.GetCurrentUser();
|
||||
if (loginContext == null)
|
||||
{
|
||||
throw new CommonException("登录已过期", Define.INVALID_TOKEN);
|
||||
}
|
||||
var columns = loginContext.GetTableColumns("{ClassName}");
|
||||
if (columns == null || columns.Count == 0)
|
||||
{
|
||||
throw new Exception("请在代码生成界面配置{ClassName}表的字段属性");
|
||||
}
|
||||
var result = new TableData();
|
||||
result.columnFields = columns;
|
||||
var objs = GetDataPrivilege("u");
|
||||
if (!string.IsNullOrEmpty(request.key))
|
||||
{
|
||||
objs = objs.Where(u => u.Id.Contains(request.key));
|
||||
}
|
||||
if (!string.IsNullOrEmpty(request.sqlWhere))
|
||||
{
|
||||
objs = objs.Where(request.sqlWhere);
|
||||
}
|
||||
var propertyStr = string.Join(',', columns.Select(u => u.ColumnName));
|
||||
result.data = objs.OrderBy(u => u.Id)
|
||||
.Skip((request.page - 1) * request.limit)
|
||||
.Take(request.limit).Select($"{propertyStr}").ToList();
|
||||
result.count = await objs.CountAsync();
|
||||
return result;
|
||||
}
|
||||
public void Add(AddOrUpdate{ClassName}Req request)
|
||||
{
|
||||
var obj = request.MapTo<{ClassName}>();
|
||||
//todo:补充或调整自己需要的字段
|
||||
obj.CreateTime = DateTime.Now;
|
||||
var user = _auth.GetCurrentUser().User;
|
||||
obj.CreateUserId = user.Id;
|
||||
obj.CreateUserName = user.Name;
|
||||
if(obj.KeyIsNull()) //如果主键为空,则生成默认值
|
||||
{
|
||||
obj.GenerateDefaultKeyVal();
|
||||
}
|
||||
|
||||
SugarClient.Ado.BeginTran();
|
||||
SugarClient.Insertable(obj).ExecuteCommand();
|
||||
if (request.{SubClassName}Reqs != null && request.{SubClassName}Reqs.Any())
|
||||
{
|
||||
foreach (var detail in request.{SubClassName}Reqs)
|
||||
{
|
||||
detail.{SubForeignKey} = obj.Id;
|
||||
_{SubClassName}App.Add(detail);
|
||||
}
|
||||
}
|
||||
SugarClient.Ado.CommitTran();
|
||||
}
|
||||
public void Update(AddOrUpdate{ClassName}Req request)
|
||||
{
|
||||
var user = _auth.GetCurrentUser().User;
|
||||
SugarClient.Ado.BeginTran();
|
||||
if (request.{SubClassName}Reqs != null && request.{SubClassName}Reqs.Any())
|
||||
{
|
||||
//请求的id不在数据库的记录,需要删除
|
||||
var containids = request.{SubClassName}Reqs.Select(u => u.Id)
|
||||
.Where(u => !string.IsNullOrEmpty(u)).ToList();
|
||||
if (containids.Any())
|
||||
{
|
||||
SugarClient.Deleteable<{SubClassName}>(u => (!containids.Contains(u.Id)) && u.{SubForeignKey} == request.Id).ExecuteCommand();
|
||||
}
|
||||
//id为空的添加
|
||||
foreach (var detail in request.{SubClassName}Reqs.Where(u => string.IsNullOrEmpty(u.Id)))
|
||||
{
|
||||
detail.{SubForeignKey} = request.Id;
|
||||
_{SubClassName}App.Add(detail);
|
||||
}
|
||||
//更新id相同的
|
||||
foreach (var detail in request.{SubClassName}Reqs.Where(u => !string.IsNullOrEmpty(u.Id)))
|
||||
{
|
||||
_{SubClassName}App.Update(detail);
|
||||
}
|
||||
}
|
||||
Repository.Update(u => new {ClassName}
|
||||
{
|
||||
UpdateTime = DateTime.Now,
|
||||
UpdateUserId = user.Id,
|
||||
UpdateUserName = user.Name
|
||||
//todo:补充或调整自己需要的字段
|
||||
}, u => u.Id == request.Id);
|
||||
SugarClient.Ado.CommitTran();
|
||||
}
|
||||
public {ModuleCode}(ISqlSugarClient client, IAuth auth,
|
||||
{SubModuleCode} {SubClassName}App) : base(client, auth)
|
||||
{
|
||||
_{SubClassName}App = {SubClassName}App;
|
||||
}
|
||||
}
|
||||
}
|
@ -129,7 +129,7 @@ create table stock
|
||||
|
||||
选中刚刚添加的`Stock`表,依次点击【生成实体】【生成业务代码】【生成vue页面】;
|
||||
|
||||
如果存在子表,也进行相同的操作。即选中刚刚添加的`StockDetail`表,依次点击【生成实体】【生成业务代码】【生成vue页面】;
|
||||
如果存在子表,也进行相同的操作。即选中刚刚添加的`StockDetail`表,依次点击【生成实体】【生成业务代码】,子表不需要生成vue页面;
|
||||
|
||||
成功后生成的后端.Net代码位置如下:
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user