namespace Admin.NET.Core.Service; /// /// 系统数据库管理服务 /// [ApiDescriptionSettings(Order = 250)] public class SysDatabaseService : IDynamicApiController, ITransient { private readonly ISqlSugarClient _db; private readonly IViewEngine _viewEngine; private readonly CodeGenOptions _codeGenOptions; public SysDatabaseService(ISqlSugarClient db, IViewEngine viewEngine, IOptions codeGenOptions) { _db = db; _viewEngine = viewEngine; _codeGenOptions = codeGenOptions.Value; } /// /// 获取库列表 /// /// [DisplayName("获取库列表")] public List GetList() { return App.GetOptions().ConnectionConfigs.Select(u => u.ConfigId).ToList(); } /// /// 获取字段列表 /// /// 表名 /// ConfigId /// [AllowAnonymous] [DisplayName("获取字段列表")] public List GetColumnList(string tableName, string configId = SqlSugarConst.ConfigId) { var db = _db.AsTenant().GetConnectionScope(configId); if (string.IsNullOrWhiteSpace(tableName)) return new List(); return db.DbMaintenance.GetColumnInfosByTableName(tableName, false).Adapt>(); } /// /// 增加列 /// /// [ApiDescriptionSettings(Name = "AddColumn"), HttpPost] [DisplayName("增加列")] public void AddColumn(DbColumnInput input) { var column = new DbColumnInfo { ColumnDescription = input.ColumnDescription, DbColumnName = input.DbColumnName, IsIdentity = input.IsIdentity == 1, IsNullable = input.IsNullable == 1, IsPrimarykey = input.IsPrimarykey == 1, Length = input.Length, DecimalDigits = input.DecimalDigits, DataType = input.DataType }; var db = _db.AsTenant().GetConnectionScope(input.ConfigId); db.DbMaintenance.AddColumn(input.TableName, column); db.DbMaintenance.AddColumnRemark(input.DbColumnName, input.TableName, input.ColumnDescription); if (column.IsPrimarykey) db.DbMaintenance.AddPrimaryKey(input.TableName, input.DbColumnName); } /// /// 删除列 /// /// [ApiDescriptionSettings(Name = "DeleteColumn"), HttpPost] [DisplayName("删除列")] public void DeleteColumn(DeleteDbColumnInput input) { var db = _db.AsTenant().GetConnectionScope(input.ConfigId); db.DbMaintenance.DropColumn(input.TableName, input.DbColumnName); } /// /// 编辑列 /// /// [ApiDescriptionSettings(Name = "UpdateColumn"), HttpPost] [DisplayName("编辑列")] public void UpdateColumn(UpdateDbColumnInput input) { var db = _db.AsTenant().GetConnectionScope(input.ConfigId); db.DbMaintenance.RenameColumn(input.TableName, input.OldColumnName, input.ColumnName); if (db.DbMaintenance.IsAnyColumnRemark(input.ColumnName, input.TableName)) db.DbMaintenance.DeleteColumnRemark(input.ColumnName, input.TableName); db.DbMaintenance.AddColumnRemark(input.ColumnName, input.TableName, string.IsNullOrWhiteSpace(input.Description) ? input.ColumnName : input.Description); } /// /// 获取表列表 /// /// ConfigId /// [DisplayName("获取表列表")] public List GetTableList(string configId = SqlSugarConst.ConfigId) { var db = _db.AsTenant().GetConnectionScope(configId); return db.DbMaintenance.GetTableInfoList(false); } /// /// 增加表 /// /// [ApiDescriptionSettings(Name = "AddTable"), HttpPost] [DisplayName("增加表")] public void AddTable(DbTableInput input) { var columns = new List(); if (input.DbColumnInfoList == null || !input.DbColumnInfoList.Any()) throw Oops.Oh(ErrorCodeEnum.db1000); if (input.DbColumnInfoList.GroupBy(q => q.DbColumnName).Any(q => q.Count() > 1)) throw Oops.Oh(ErrorCodeEnum.db1002); var config = App.GetOptions().ConnectionConfigs.FirstOrDefault(u => u.ConfigId == input.ConfigId); input.DbColumnInfoList.ForEach(m => { columns.Add(new DbColumnInfo { DbColumnName = config.EnableUnderLine ? UtilMethods.ToUnderLine(m.DbColumnName.Trim()) : m.DbColumnName.Trim(), DataType = m.DataType, Length = m.Length, ColumnDescription = m.ColumnDescription, IsNullable = m.IsNullable == 1, IsIdentity = m.IsIdentity == 1, IsPrimarykey = m.IsPrimarykey == 1, DecimalDigits = m.DecimalDigits }); }); var db = _db.AsTenant().GetConnectionScope(input.ConfigId); db.DbMaintenance.CreateTable(input.TableName, columns, false); if (db.CurrentConnectionConfig.DbType == SqlSugar.DbType.Sqlite || db.CurrentConnectionConfig.DbType == SqlSugar.DbType.MySql) return; if (columns.Any(m => m.IsPrimarykey)) db.DbMaintenance.AddPrimaryKey(input.TableName, columns.FirstOrDefault(m => m.IsPrimarykey).DbColumnName); db.DbMaintenance.AddTableRemark(input.TableName, input.Description); input.DbColumnInfoList.ForEach(m => { m.DbColumnName = config.EnableUnderLine ? UtilMethods.ToUnderLine(m.DbColumnName) : m.DbColumnName; db.DbMaintenance.AddColumnRemark(m.DbColumnName, input.TableName, string.IsNullOrWhiteSpace(m.ColumnDescription) ? m.DbColumnName : m.ColumnDescription); }); } /// /// 删除表 /// /// [ApiDescriptionSettings(Name = "DeleteTable"), HttpPost] [DisplayName("删除表")] public void DeleteTable(DeleteDbTableInput input) { var db = _db.AsTenant().GetConnectionScope(input.ConfigId); db.DbMaintenance.DropTable(input.TableName); } /// /// 编辑表 /// /// [ApiDescriptionSettings(Name = "UpdateTable"), HttpPost] [DisplayName("编辑表")] public void UpdateTable(UpdateDbTableInput input) { var db = _db.AsTenant().GetConnectionScope(input.ConfigId); db.DbMaintenance.RenameTable(input.OldTableName, input.TableName); if (db.DbMaintenance.IsAnyTableRemark(input.TableName)) db.DbMaintenance.DeleteTableRemark(input.TableName); db.DbMaintenance.AddTableRemark(input.TableName, input.Description); } /// /// 创建实体 /// /// [ApiDescriptionSettings(Name = "CreateEntity"), HttpPost] [DisplayName("创建实体")] public void CreateEntity(CreateEntityInput input) { var config = App.GetOptions().ConnectionConfigs.FirstOrDefault(u => u.ConfigId == input.ConfigId); input.Position = string.IsNullOrWhiteSpace(input.Position) ? "Admin.NET.Application" : input.Position; input.EntityName = string.IsNullOrWhiteSpace(input.EntityName) ? (config.EnableUnderLine ? CodeGenUtil.CamelColumnName(input.TableName, null) : input.TableName) : input.EntityName; string[] dbColumnNames;//= _codeGenOptions.EntityBaseColumn[input.BaseClassName]; _codeGenOptions.EntityBaseColumn.TryGetValue(input.BaseClassName, out dbColumnNames); if (dbColumnNames is null || dbColumnNames is { Length: 0 }) throw Oops.Oh("基类配置文件不存在此类型"); var templatePath = GetEntityTemplatePath(); var targetPath = GetEntityTargetPath(input); var db = _db.AsTenant().GetConnectionScope(input.ConfigId); DbTableInfo dbTableInfo = db.DbMaintenance.GetTableInfoList(false).FirstOrDefault(m => m.Name == input.TableName || m.Name == input.TableName.ToLower()); if (dbTableInfo == null) throw Oops.Oh(ErrorCodeEnum.db1001); List dbColumnInfos = db.DbMaintenance.GetColumnInfosByTableName(input.TableName, false); dbColumnInfos.ForEach(m => { m.DbColumnName = config.EnableUnderLine ? CodeGenUtil.CamelColumnName(m.DbColumnName, dbColumnNames) : m.DbColumnName;//转下划线后的列名 需要转回来 m.DataType = CodeGenUtil.ConvertDataType(m); }); if (_codeGenOptions.BaseEntityNames.Contains(input.BaseClassName, StringComparer.OrdinalIgnoreCase)) dbColumnInfos = dbColumnInfos.Where(c => !dbColumnNames.Contains(c.DbColumnName, StringComparer.OrdinalIgnoreCase)).ToList(); var tContent = File.ReadAllText(templatePath); var tResult = _viewEngine.RunCompileFromCached(tContent, new { NameSpace = $"{input.Position}.Entity", input.TableName, input.EntityName, BaseClassName = string.IsNullOrWhiteSpace(input.BaseClassName) ? "" : $" : {input.BaseClassName}", input.ConfigId, dbTableInfo.Description, TableField = dbColumnInfos }); File.WriteAllText(targetPath, tResult, Encoding.UTF8); } /// /// 获取实体模板文件路径 /// /// private static string GetEntityTemplatePath() { var templatePath = Path.Combine(App.WebHostEnvironment.WebRootPath, "Template"); return Path.Combine(templatePath, "Entity.cs.vm"); } /// /// 设置生成实体文件路径 /// /// /// private static string GetEntityTargetPath(CreateEntityInput input) { var backendPath = Path.Combine(new DirectoryInfo(App.WebHostEnvironment.ContentRootPath).Parent.FullName, input.Position, "Entity"); if (!Directory.Exists(backendPath)) Directory.CreateDirectory(backendPath); return Path.Combine(backendPath, input.EntityName + ".cs"); } }