using DocumentFormat.OpenXml.Spreadsheet; using ReZero.Common; using ReZero.Excel; using ReZero.TextTemplate; using SqlSugar; using System; using System.Collections.Generic; using System.Data; using System.Data.SqlTypes; using System.IO; using System.Linq; using System.Text; using System.Text.RegularExpressions; namespace ReZero.SuperAPI { public partial class MethodApi { #region ExecTemplateByTableIds /// /// Execute template generation for multiple tables. /// /// The ID of the database. /// The IDs of the tables. /// The ID of the template. /// The URL of the output file. /// The directory path of the generated files. public string ExecTemplateByTableIds(long databaseId, long[] tableIds, long templateId, string url,string viewName) { if (IsView(viewName)) { return ExecTemplateByView(databaseId, viewName, templateId, url); } List datatables = new List(); var db = App.Db; List datas = GetZeroEntities(databaseId, tableIds, db); var template = App.Db.Queryable().First(it => it.Id == templateId); var outUrl = string.Empty; foreach (var item in datas) { outUrl = CreateFile(databaseId, template, item, url); } var result = Directory.GetParent(outUrl).FullName; return result; } private string ExecTemplateByView(long databaseId, string viewName, long templateId, string url) { var db = App.Db; var template = App.Db.Queryable().First(it => it.Id == templateId); var item = new ZeroEntityInfo(); var viewDb = App.GetDbById(databaseId); var dt=viewDb!.Queryable().AS(viewName).Take(1).Select("*").ToDataTable(); item.ClassName = viewName; item.DbTableName = viewName; item.Description = string.Empty; item.ZeroEntityColumnInfos = new List(); foreach (DataColumn dataColumn in dt.Columns) { item.ZeroEntityColumnInfos.Add(new ZeroEntityColumnInfo() { PropertyName=dataColumn.ColumnName, DbColumnName=dataColumn.ColumnName, DataType=dataColumn.DataType.Name, PropertyType=EntityGeneratorManager.GetNativeTypeByType(dataColumn.DataType) }); } var outUrl = CreateFile(databaseId, template, item, url); var result = Directory.GetParent(outUrl).FullName; return result; } private static bool IsView(string viewName) { return !string.IsNullOrEmpty(viewName); } /// /// Create a file based on the template and entity information. /// /// The ID of the database. /// The template to use. /// The entity information. /// The URL of the output file. /// The URL of the created file. private string CreateFile(long databaseId, ZeroTemplate template, ZeroEntityInfo item, string url) { var classString = GetClassString(databaseId, template, item, out TemplateEntitiesGen templateEntitiesGen); url = GetUrl(url, templateEntitiesGen); if (url.Contains(PubConst.Common_Project)) { var baseDir = AppContext.BaseDirectory; var findDir = DirectoryHelper.FindParentDirectoryWithSlnFile(baseDir); if (!string.IsNullOrEmpty(findDir)) { url = Regex.Replace(url, PubConst.Common_ProjectRegex, string.Empty, RegexOptions.IgnoreCase).TrimStart('/').TrimStart('\\'); url = Path.Combine(findDir, url); } else { throw new Exception(TextHandler.GetCommonText("没有找到 项目sln文件,可以使完整物理路径", "No project sln file found that can make the full physical path")); } } FileSugar.CreateFileReplace(url, classString, Encoding.UTF8); return url; } /// /// Get the class string based on the template and entity information. /// /// The ID of the database. /// The template to use. /// The entity information. /// The generated template entities. /// The class string. private string GetClassString(long databaseId, ZeroTemplate template, ZeroEntityInfo item, out TemplateEntitiesGen templateEntitiesGen) { var propertyGens = new List(); templateEntitiesGen = new TemplateEntitiesGen() { ClassName = item.ClassName, Description = item.Description, TableName = item.DbTableName, PropertyGens = propertyGens }; var columnInfos = App.GetDbById(databaseId)!.DbMaintenance.GetColumnInfosByTableName(item.DbTableName, false); foreach (var zeroEntityColumn in item.ZeroEntityColumnInfos!) { AddProperty(propertyGens, columnInfos, zeroEntityColumn); } var classString = ExecTemplate(TemplateType.Entity, new SerializeService().SerializeObject(templateEntitiesGen), template.TemplateContent!); return classString; } /// /// Add a property to the property list based on the entity column information. /// /// The list of property generators. /// The list of column information. /// The entity column information. private static void AddProperty(List propertyGens, List columnInfos, ZeroEntityColumnInfo zeroEntityColumn) { var dbColumn = columnInfos.FirstOrDefault(it => it.DbColumnName!.EqualsCase(zeroEntityColumn.DbColumnName!)); TemplatePropertyGen templatePropertyGen = new TemplatePropertyGen() { DbColumnName = zeroEntityColumn.DbColumnName, DbType = zeroEntityColumn.DataType, DecimalDigits = zeroEntityColumn.DecimalDigits, DefaultValue = string.Empty, Description = zeroEntityColumn.Description?.Replace(Environment.NewLine,PubConst.Common_BlankSpace)?.Replace(PubConst.Common_N, PubConst.Common_BlankSpace)?.Replace(PubConst.Common_R, PubConst.Common_BlankSpace), IsIdentity = zeroEntityColumn.IsIdentity, IsNullable = zeroEntityColumn.IsNullable, IsPrimaryKey = zeroEntityColumn.IsPrimarykey, PropertyName = zeroEntityColumn.PropertyName, PropertyType = EntityGeneratorManager.GetTypeByNativeTypes(zeroEntityColumn.PropertyType).Name, IsJson = zeroEntityColumn.IsJson, IsIgnore = zeroEntityColumn.PropertyType == NativeType.IsIgnore }; ProcessingPropertyDefault(zeroEntityColumn, templatePropertyGen); ProcessingPropertyByDbColumn(dbColumn, templatePropertyGen); propertyGens.Add(templatePropertyGen); } /// /// Process the property based on the database column information. /// /// The database column information. /// The template property generator. private static void ProcessingPropertyByDbColumn(SqlSugar.DbColumnInfo dbColumn, TemplatePropertyGen templatePropertyGen) { if (dbColumn != null) { templatePropertyGen.DbType = string.IsNullOrEmpty(dbColumn.OracleDataType) ? dbColumn.DataType : dbColumn.OracleDataType; templatePropertyGen.DecimalDigits = dbColumn.DecimalDigits; templatePropertyGen.Length = dbColumn.Length; templatePropertyGen.IsNullable = dbColumn.IsNullable; } } /// /// Process the default value of the property. /// /// The entity column information. /// The template property generator. private static void ProcessingPropertyDefault(ZeroEntityColumnInfo zeroEntityColumn, TemplatePropertyGen templatePropertyGen) { templatePropertyGen.PropertyType = EntityGeneratorManager.GetNativeTypeName(templatePropertyGen.PropertyType!); templatePropertyGen.PropertyType = templatePropertyGen.PropertyType + (zeroEntityColumn.IsNullable ? PubConst.Common_Q : string.Empty); } /// /// Get the URL based on the template entities information. /// /// The URL template. /// The generated template entities. /// The URL with replaced placeholders. private static string GetUrl(string url, TemplateEntitiesGen templateEntitiesGen) { url = url.Replace(PubConst.Common_Format0, templateEntitiesGen.ClassName).Replace(PubConst.Common_Format1, templateEntitiesGen.TableName); return url; } /// /// Get the entity information for the specified database and table IDs. /// /// The ID of the database. /// The IDs of the tables. /// The SQL Sugar client. /// The list of entity information. private static List GetZeroEntities(long databaseId, long[] tableIds, ISqlSugarClient db) { return db.Queryable() .OrderBy(it => it.DbTableName) .Where(it => it.DataBaseId == databaseId) .WhereIF(tableIds.Any(), it => tableIds.Contains(it.Id)) .Includes(it => it.ZeroEntityColumnInfos).ToList(); } #endregion #region ExecTemplate /// /// Execute template generation based on the specified type, data, and template. /// /// The type of the template. /// The data to use for template generation. /// The template to use. /// The generated template result. public string ExecTemplate(TemplateType type, string data, string template) { string result = string.Empty; switch (type) { case TemplateType.Entity: result = ExecTemplateByEntity(data, template); break; default: throw new ArgumentException("Invalid template type."); } return result; } /// /// Execute template generation based on the entity type. /// /// The data to use for template generation. /// The template to use. /// The generated template result. private string ExecTemplateByEntity(string data, string template) { var model = new SerializeService().DeserializeObject(data); var templateModel = new TemplateModel { Model = model }; var temp = new TextTemplateManager().RenderTemplate(template, templateModel); return temp.ToString(); } #endregion #region Helper internal string ExecTemplateByViewWithoutCreatingFiles(long databaseId, string viewName,bool isView, long templateId) { var db = App.Db; var template = App.Db.Queryable().First(it => it.Id == templateId); var item = new ZeroEntityInfo(); var viewDb = App.GetDbById(databaseId); string className = string.Empty; if (isView == false) { var data = db.Queryable()!.InSingle(viewName); viewName = data!.DbTableName!; className = data!.ClassName!; } var dt = viewDb!.Queryable().AS(viewName).Take(1).Select("*").ToDataTable(); item.ClassName = className==string.Empty? viewName: className; item.DbTableName = viewName; item.Description = string.Empty; item.ZeroEntityColumnInfos = new List(); foreach (DataColumn dataColumn in dt.Columns) { item.ZeroEntityColumnInfos.Add(new ZeroEntityColumnInfo() { PropertyName = dataColumn.ColumnName, DbColumnName = dataColumn.ColumnName, DataType = dataColumn.DataType.Name, PropertyType = EntityGeneratorManager.GetNativeTypeByType(dataColumn.DataType) }); } var classString = GetClassString(databaseId, template, item, out _); return classString; } #endregion } }