mirror of
https://gitee.com/dotnetchina/OpenAuth.Net.git
synced 2025-08-25 10:08:10 +08:00
代码生成加上外部数据源支持
This commit is contained in:
parent
44d5b286aa
commit
093be9500e
@ -25,7 +25,7 @@ using OpenAuth.Repository.Interface;
|
||||
|
||||
namespace OpenAuth.App
|
||||
{
|
||||
public class BuilderTableApp : BaseStringApp<BuilderTable,OpenAuthDBContext>
|
||||
public class BuilderTableApp : BaseStringApp<BuilderTable, OpenAuthDBContext>
|
||||
{
|
||||
private BuilderTableColumnApp _builderTableColumnApp;
|
||||
private CategoryApp _categoryApp;
|
||||
@ -34,7 +34,7 @@ namespace OpenAuth.App
|
||||
private string _startName = "";
|
||||
private IOptions<AppSetting> _appConfiguration;
|
||||
|
||||
public BuilderTableApp(IUnitWork<OpenAuthDBContext> unitWork, IRepository<BuilderTable,OpenAuthDBContext> repository,
|
||||
public BuilderTableApp(IUnitWork<OpenAuthDBContext> unitWork, IRepository<BuilderTable, OpenAuthDBContext> repository,
|
||||
RevelanceManagerApp app, IAuth auth, DbExtension dbExtension, BuilderTableColumnApp builderTableColumnApp,
|
||||
IOptions<AppSetting> appConfiguration, CategoryApp categoryApp) : base(unitWork, repository, auth)
|
||||
{
|
||||
@ -93,10 +93,10 @@ namespace OpenAuth.App
|
||||
objs = objs.Where(u => u.Id.Contains(request.key));
|
||||
}
|
||||
|
||||
result.data =await objs.OrderBy(u => u.Id)
|
||||
result.data = await objs.OrderBy(u => u.Id)
|
||||
.Skip((request.page - 1) * request.limit)
|
||||
.Take(request.limit).ToListAsync();
|
||||
result.count =await objs.CountAsync();
|
||||
result.count = await objs.CountAsync();
|
||||
return result;
|
||||
}
|
||||
|
||||
@ -192,7 +192,8 @@ namespace OpenAuth.App
|
||||
ParentTableId = obj.ParentTableId,
|
||||
UpdateTime = DateTime.Now,
|
||||
UpdateUserId = user.Id,
|
||||
UpdateUserName = user.Name
|
||||
UpdateUserName = user.Name,
|
||||
ExternalDataSourceId = obj.ExternalDataSourceId //外部数据源ID
|
||||
});
|
||||
}
|
||||
|
||||
@ -304,7 +305,7 @@ namespace OpenAuth.App
|
||||
}
|
||||
if (primarykey.ColumnType == "decimal" || primarykey.ColumnType == "numeric") //是否为数字
|
||||
{
|
||||
if(primarykey.IsIncrement) //是否自增
|
||||
if (primarykey.IsIncrement) //是否自增
|
||||
{
|
||||
domainContent = domainContent.Replace("{BaseAppName}", "BaseIntAutoGenApp");
|
||||
}
|
||||
@ -428,7 +429,7 @@ namespace OpenAuth.App
|
||||
}
|
||||
if (primarykey.ColumnType == "decimal" || primarykey.ColumnType == "numeric") //是否为数字
|
||||
{
|
||||
if(primarykey.IsIncrement) //是否自增
|
||||
if (primarykey.IsIncrement) //是否自增
|
||||
{
|
||||
domainContent = domainContent.Replace("{KeyTypeName}", "int");
|
||||
}
|
||||
@ -468,7 +469,7 @@ namespace OpenAuth.App
|
||||
attributeBuilder.Append(" /// </summary>");
|
||||
attributeBuilder.Append("\r\n");
|
||||
|
||||
attributeBuilder.Append(" [Description(\""+ column.Remark +"\")]");
|
||||
attributeBuilder.Append(" [Description(\"" + column.Remark + "\")]");
|
||||
attributeBuilder.Append("\r\n");
|
||||
|
||||
string entityType = column.EntityType;
|
||||
@ -481,7 +482,7 @@ namespace OpenAuth.App
|
||||
attributeBuilder.Append("\r\n\r\n ");
|
||||
|
||||
constructionBuilder.Append(" this." + column.EntityName
|
||||
+ "=" + (GetDefault(column.EntityType)??"\"\"")
|
||||
+ "=" + (GetDefault(column.EntityType) ?? "\"\"")
|
||||
+ ";\r\n");
|
||||
}
|
||||
|
||||
@ -500,7 +501,7 @@ namespace OpenAuth.App
|
||||
}
|
||||
if (primarykey.ColumnType == "decimal" || primarykey.ColumnType == "numeric") //是否为数字
|
||||
{
|
||||
if(primarykey.IsIncrement) //是否自增
|
||||
if (primarykey.IsIncrement) //是否自增
|
||||
{
|
||||
domainContent = domainContent.Replace("{BaseEntityName}", "IntAutoGenEntity");
|
||||
}
|
||||
@ -594,7 +595,7 @@ namespace OpenAuth.App
|
||||
&& x.BaseType == typeof(StringEntity));
|
||||
foreach (var entity in types)
|
||||
{
|
||||
if (entity.Name == moduleCode )
|
||||
if (entity.Name == moduleCode)
|
||||
throw new Exception($"实际表名【{moduleCode}】已创建实体,不能创建实体");
|
||||
|
||||
if (entity.Name != moduleCode)
|
||||
@ -764,7 +765,7 @@ namespace OpenAuth.App
|
||||
|
||||
domainContent = domainContent.Replace("{TableName}", sysTableInfo.ClassName.ToCamelCase());
|
||||
|
||||
FileHelper.WriteFile(Path.Combine(req.VueProjRootPath, $"src/api/"),$"{sysTableInfo.ClassName.ToCamelCase()}s.js",
|
||||
FileHelper.WriteFile(Path.Combine(req.VueProjRootPath, $"src/api/"), $"{sysTableInfo.ClassName.ToCamelCase()}s.js",
|
||||
domainContent);
|
||||
}
|
||||
|
||||
@ -775,14 +776,14 @@ namespace OpenAuth.App
|
||||
public async Task<TableData> AllMain()
|
||||
{
|
||||
var result = new TableData();
|
||||
var objs = UnitWork.Find<BuilderTable>(u =>string.IsNullOrEmpty(u.ParentTableId)).Select(u=>new
|
||||
var objs = UnitWork.Find<BuilderTable>(u => string.IsNullOrEmpty(u.ParentTableId)).Select(u => new
|
||||
{
|
||||
Id= u.Id,
|
||||
Id = u.Id,
|
||||
Name = u.TableName
|
||||
});
|
||||
|
||||
result.data =await objs.OrderBy(u => u.Id).ToListAsync();
|
||||
result.count =await objs.CountAsync();
|
||||
result.data = await objs.OrderBy(u => u.Id).ToListAsync();
|
||||
result.count = await objs.CountAsync();
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
@ -7,23 +7,17 @@ using Microsoft.EntityFrameworkCore;
|
||||
using OpenAuth.App.Interface;
|
||||
using OpenAuth.App.Request;
|
||||
using OpenAuth.App.Response;
|
||||
using OpenAuth.Repository;
|
||||
using OpenAuth.Repository.Domain;
|
||||
using OpenAuth.Repository.Interface;
|
||||
|
||||
|
||||
using SqlSugar;
|
||||
namespace OpenAuth.App
|
||||
{
|
||||
public class BuilderTableColumnApp : BaseStringApp<BuilderTableColumn,OpenAuthDBContext>
|
||||
public class BuilderTableColumnApp : SqlSugarBaseApp<BuilderTableColumn>
|
||||
{
|
||||
|
||||
private DbExtension _dbExtension;
|
||||
public BuilderTableColumnApp(IUnitWork<OpenAuthDBContext> unitWork, IRepository<BuilderTableColumn,OpenAuthDBContext> repository,
|
||||
IAuth auth, DbExtension dbExtension) : base(unitWork, repository,auth)
|
||||
public BuilderTableColumnApp(ISqlSugarClient client, IAuth auth, DbExtension dbExtension) : base(client, auth)
|
||||
{
|
||||
_dbExtension = dbExtension;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 加载列表
|
||||
/// </summary>
|
||||
@ -34,25 +28,23 @@ namespace OpenAuth.App
|
||||
throw new Exception($"缺少必要的参数BuilderTableId");
|
||||
}
|
||||
var result = new TableResp<BuilderTableColumn>();
|
||||
var objs = UnitWork.Find<BuilderTableColumn>(u =>u.TableId == request.BuilderTableId);
|
||||
var objs = Repository.AsQueryable().Where(u => u.TableId == request.BuilderTableId);
|
||||
if (!string.IsNullOrEmpty(request.key))
|
||||
{
|
||||
objs = objs.Where(u => u.ColumnName.Contains(request.key));
|
||||
}
|
||||
|
||||
result.data =await objs.OrderByDescending(u => u.IsList)
|
||||
.ThenBy(u=>u.Sort)
|
||||
.ThenBy(u =>u.ColumnName)
|
||||
result.data = await objs.OrderByDescending(u => u.IsList)
|
||||
.OrderBy(u => u.Sort)
|
||||
.OrderBy(u => u.ColumnName)
|
||||
.Skip((request.page - 1) * request.limit)
|
||||
.Take(request.limit).ToListAsync();
|
||||
result.count =await objs.CountAsync();
|
||||
result.count = await objs.CountAsync();
|
||||
return result;
|
||||
}
|
||||
|
||||
public void Update(AddOrUpdateBuilderTableColumnReq obj)
|
||||
{
|
||||
var user = _auth.GetCurrentUser().User;
|
||||
UnitWork.Update<BuilderTableColumn>(u => u.Id == obj.Id, u => new BuilderTableColumn
|
||||
Repository.Update(u => new BuilderTableColumn
|
||||
{
|
||||
TableId = obj.TableId,
|
||||
TableName = obj.TableName,
|
||||
@ -82,28 +74,34 @@ namespace OpenAuth.App
|
||||
UpdateTime = DateTime.Now,
|
||||
UpdateUserId = user.Id,
|
||||
UpdateUserName = user.Name
|
||||
//todo:补充或调整自己需要的字段
|
||||
});
|
||||
|
||||
}, u => u.Id == obj.Id);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 同步数据结构
|
||||
/// <para>读取数据库结构与当前结构的差异,如果数据库有新增的字段,则自动加入</para>
|
||||
/// </summary>
|
||||
public void Sync(SyncStructureReq req)
|
||||
{
|
||||
var builderTable = SugarClient.Queryable<BuilderTable>().First(u => u.Id == req.BuilderTableId);
|
||||
if (builderTable != null)
|
||||
{
|
||||
//如果代码生成器配置了外部数据库连接,则使用外部数据库连接
|
||||
var connection = SugarClient.Queryable<ExternalDataSource>().First(u => u.Id == builderTable.ExternalDataSourceId);
|
||||
if (connection != null)
|
||||
{
|
||||
var dbType = connection.Dbtype;
|
||||
_dbExtension.SetConnection(connection.Connectionstring, dbType);
|
||||
}
|
||||
}
|
||||
var columns = _dbExtension.GetDbTableStructure(req.TableName);
|
||||
if (!columns.Any())
|
||||
{
|
||||
throw new Exception($"未能找到{req.TableName}表结构定义");
|
||||
}
|
||||
|
||||
var exists = Find(req.Id).Select(u=>u.ColumnName);
|
||||
|
||||
var exists = Find(req.Id).Select(u => u.ColumnName.ToLower());
|
||||
foreach (var column in columns)
|
||||
{
|
||||
if(exists.Contains(column.ColumnName)) continue;
|
||||
if (exists.Contains(column.ColumnName.ToLower())) continue; //列名转化为小写对比
|
||||
var builderColumn = new BuilderTableColumn
|
||||
{
|
||||
ColumnName = column.ColumnName,
|
||||
@ -111,7 +109,6 @@ namespace OpenAuth.App
|
||||
ColumnType = column.ColumnType,
|
||||
EntityType = column.EntityType,
|
||||
EntityName = column.ColumnName,
|
||||
|
||||
IsKey = column.IsKey == 1,
|
||||
IsRequired = column.IsNull != 1,
|
||||
IsEdit = true,
|
||||
@ -120,21 +117,14 @@ namespace OpenAuth.App
|
||||
MaxLength = column.MaxLength,
|
||||
TableName = req.TableName,
|
||||
TableId = req.Id,
|
||||
|
||||
CreateTime = DateTime.Now
|
||||
};
|
||||
UnitWork.Add(builderColumn);
|
||||
Repository.Insert(builderColumn);
|
||||
}
|
||||
UnitWork.Save();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public List<BuilderTableColumn> Find(string tableId)
|
||||
{
|
||||
return Repository.Find(u => u.TableId == tableId).ToList();
|
||||
return Repository.AsQueryable().Where(u => u.TableId == tableId).ToList();
|
||||
}
|
||||
}
|
||||
}
|
@ -83,5 +83,10 @@ namespace OpenAuth.App.Request
|
||||
/// 子表外键
|
||||
/// </summary>
|
||||
public string ForeignKey { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 外部数据源ID
|
||||
/// </summary>
|
||||
public string ExternalDataSourceId { get; set; }
|
||||
}
|
||||
}
|
@ -16,10 +16,17 @@ namespace OpenAuth.App.Request
|
||||
public class SyncStructureReq
|
||||
{
|
||||
/// <summary>
|
||||
/// Id为空则为添加
|
||||
/// 列ID
|
||||
/// <para>Id为空则为添加</para>
|
||||
/// </summary>
|
||||
public string Id { get; set; }
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 代码生成器主表ID
|
||||
/// </summary>
|
||||
public string BuilderTableId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 表英文全称
|
||||
/// </summary>
|
||||
|
@ -408,5 +408,23 @@ namespace OpenAuth.App
|
||||
}
|
||||
return new List<SysTableColumn>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置数据库连接
|
||||
/// </summary>
|
||||
/// <param name="connectionString">数据库连接字符串</param>
|
||||
/// <param name="dbType">数据库类型</param>
|
||||
public void SetConnection(string connectionString, int? dbType)
|
||||
{
|
||||
var config = new ConnectionConfig
|
||||
{
|
||||
ConnectionString = connectionString,
|
||||
IsAutoCloseConnection = true,
|
||||
DbType = (SqlSugar.DbType)dbType
|
||||
};
|
||||
// 创建新的SqlSugarClient实例
|
||||
SugarClient = new SqlSugarClient(config);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
@ -157,5 +157,11 @@ namespace OpenAuth.Repository.Domain
|
||||
/// </summary>
|
||||
[Description("主表ID,如果为空表示为主表")]
|
||||
public string ParentTableId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 外部数据源ID
|
||||
/// </summary>
|
||||
[Description("外部数据源ID")]
|
||||
public string ExternalDataSourceId { get; set; }
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user