diff --git a/IRaCIS.Core.Test/IRaCIS.Core.Test.csproj b/IRaCIS.Core.Test/IRaCIS.Core.Test.csproj index 5d8890377..29c18a0ea 100644 --- a/IRaCIS.Core.Test/IRaCIS.Core.Test.csproj +++ b/IRaCIS.Core.Test/IRaCIS.Core.Test.csproj @@ -17,13 +17,13 @@ - + Always Always - + Always diff --git a/IRaCIS.Core.Test/Template/EntityService.liquid b/IRaCIS.Core.Test/OK/EntityService.liquid similarity index 100% rename from IRaCIS.Core.Test/Template/EntityService.liquid rename to IRaCIS.Core.Test/OK/EntityService.liquid diff --git a/IRaCIS.Core.Test/Template/IEntityService.liquid b/IRaCIS.Core.Test/OK/IEntityService.liquid similarity index 100% rename from IRaCIS.Core.Test/Template/IEntityService.liquid rename to IRaCIS.Core.Test/OK/IEntityService.liquid diff --git a/IRaCIS.Core.Test/Program.cs b/IRaCIS.Core.Test/Program.cs index 688860674..6dd7114f1 100644 --- a/IRaCIS.Core.Test/Program.cs +++ b/IRaCIS.Core.Test/Program.cs @@ -6,6 +6,7 @@ using Microsoft.Extensions.Options; using System; using System.Collections.Generic; using System.IO; +using System.Linq; using System.Text; partial class Program @@ -48,34 +49,9 @@ partial class Program - #region 直接通过上下文拿到实体信息 - - - var contextOptions = new DbContextOptionsBuilder().UseSqlServer(@"Server=106.14.89.110,1435;Database=Test_IRC;User ID=sa;Password=xc@123456;TrustServerCertificate=true").Options; - - using var dbContext = new IRaCISDBContext(contextOptions); - - var dbModel = dbContext.Model; - - foreach (var entityType in dbModel.GetEntityTypes()) - { - string tableName = entityType.GetTableName(); - Console.WriteLine($"Table: {tableName}"); - - foreach (var property in entityType.GetProperties()) - { - string columnName = property.GetColumnName(); - string dataType = property.ClrType.Name; - bool isNullable = property.IsNullable; - - - } - } - - #endregion // 要生成的表名数组 - var tableNames = new List { "Product"/*, "Order"*/ }; + var tableNames = new List { "Subject"/*, "Order"*/ }; try { @@ -94,9 +70,13 @@ partial class Program { TableName = tableName, - IsPaged = true + IsPaged = true, + + TableFieLdList = GetTableFiledList(tableName) }; + var options = new TemplateOptions(); + options.MemberAccessStrategy.Register(); var parser = new FluidParser(); @@ -104,12 +84,13 @@ partial class Program if (parser.TryParse(source, out var template, out var error)) { - var context = new TemplateContext(model); + var context = new TemplateContext(model, options, true); //Console.WriteLine(template.Render(context)); string outputFilePath = Path.Combine(outPutTemplateFolderPath, /*folder,*/ $"{fileName.Replace("Entity", tableName)}.cs"); + File.WriteAllText(outputFilePath, template.Render(context)); } else @@ -139,6 +120,23 @@ partial class Program //生成的列表是是分页 还是不分页 public bool IsPaged { get; set; } + public DateTime DateTimeNow = DateTime.Now; + + + public List TableFieLdList { get; set; } + + + + + public List AddOrUpdateFieldList => TableFieLdList.Where(t => !AddOrUpdateExcludeNameList.Contains(t.FieldName)).ToList(); + public List ViewListFieldList => TableFieLdList.Where(t => ListInCludeNameList.Contains(t.FieldName)).ToList(); + public List QueryListFieldList => TableFieLdList.Where(t => !AddOrUpdateExcludeNameList.Contains(t.FieldName) && t.IsPrimarykey==false).ToList(); + + //添加和更新模型排除的字段名 + public List AddOrUpdateExcludeNameList = new List() { "CreateUserId", "UpdateUserId", "CreateTime", "UpdateTime", "DeleteUserId", "IsDeleted", "DeletedTime" }; + + public List ListInCludeNameList = new List() { "CreateTime", "UpdateTime" }; + // 列表视图模型名称 public string TableNameView => TableName + "View"; //列表查询模型名称 @@ -152,9 +150,84 @@ partial class Program public string LowercaseQueryableName => $"{char.ToLower(TableName[0]) + TableName.Substring(1)}Queryable"; - public DateTime DateTimeNow = DateTime.Now; } + public class TableProperty + { + public string ColumnName { get; set; } + public string FieldName { get; set; } + + public string CSharpType { get; set; } + + public bool IsNullable { get; set; } + + // 自动赋值 String.Empty 避免 数据库存储Null + public bool IsCSharpString => CSharpType.ToLower() == "string"; + + public int? MaxLength { get; set; } + + public bool IsPrimarykey { get; set; } + + } + + + private static List GetTableFiledList(string tableName) + { + + #region 直接通过上下文拿到实体信息 + + + var contextOptions = new DbContextOptionsBuilder().UseSqlServer(@"Server=106.14.89.110,1435;Database=Test_IRC;User ID=sa;Password=xc@123456;TrustServerCertificate=true").Options; + + using var dbContext = new IRaCISDBContext(contextOptions); + + var dbModel = dbContext.Model; + + var entityType = dbModel.GetEntityTypes().Where(t => t.GetTableName() == tableName).FirstOrDefault(); + + if (entityType != null) + { + var list = entityType.GetProperties().Select(property => new TableProperty() + { + FieldName = property.Name, + ColumnName = property.GetColumnName(), + IsNullable = property.IsNullable, + IsPrimarykey = property.IsKey(), + CSharpType = GetCSharpType(property.ClrType), + }).ToList(); + + return list; + } + + return new List(); + + #endregion + } + + static string GetCSharpType(Type type) + { + if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>)) + { + var underlyingType = Nullable.GetUnderlyingType(type); + return $"{GetCSharpType(underlyingType)}?"; + } + + return type switch + { + _ when type == typeof(int) => "int", + _ when type == typeof(long) => "long", + _ when type == typeof(short) => "short", + _ when type == typeof(byte) => "byte", + _ when type == typeof(bool) => "bool", + _ when type == typeof(decimal) => "decimal", + _ when type == typeof(double) => "double", + _ when type == typeof(float) => "float", + _ when type == typeof(Guid) => "Guid", + _ when type == typeof(DateTime) => "DateTime", + _ when type == typeof(string) => "string", + _ => type.Name // 非常见类型,返回 .NET 类型名称 + }; + } #region 核对code first 暂时屏蔽 diff --git a/IRaCIS.Core.Test/Template/EntityViewModel.liquid b/IRaCIS.Core.Test/Template/EntityViewModel.liquid index 3afa57c50..fa6acf051 100644 --- a/IRaCIS.Core.Test/Template/EntityViewModel.liquid +++ b/IRaCIS.Core.Test/Template/EntityViewModel.liquid @@ -10,11 +10,36 @@ using System.Collections.Generic; namespace IRaCIS.Core.Application.ViewModel { - public class {{ entityName }}ViewModel + public class {{ TableNameView }} : {{ TableNameAddOrEdit }} { - {% for property in properties %} - public {{ property.type }} {{ property.name }} { get; set; } + {% for field in ViewListFieldList %} + public {{ field.CSharpType }} {{ field.FieldName }} { get; set; } {% endfor %} } + + + public class {{ TableNameAddOrEdit }} + { + {%- for field in AddOrUpdateFieldList -%} + {% if field.IsPrimarykey %} + public {{ field.CSharpType }}? {{ field.FieldName }} { get; set; } + {% else %} + public {{ field.CSharpType }} {{ field.FieldName }} { get; set; } + {% endif %} + {%- endfor -%} + } + + public class {{ TableNameQuery }} + { + {%- for field in QueryListFieldList -%} + {% if field.IsCSharpString %} + public {{ field.CSharpType }}? {{ field.FieldName }} { get; set; } + {% elsif field.IsNullable %} + public {{ field.CSharpType }} {{ field.FieldName }} { get; set; } + {% else %} + public {{ field.CSharpType }}? {{ field.FieldName }} { get; set; } + {% endif %} + {%- endfor -%} + } }