CreaterHelper.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. using Microsoft.Extensions.Caching.Distributed;
  2. using System;
  3. using XYY.Core.Standard.Data.Infrastructure;
  4. using Dapper;
  5. using XYY.Core.Standard;
  6. using System.Threading.Tasks;
  7. namespace XYY.DataMSHelper.Standard
  8. {
  9. public interface ICreateHelper
  10. {
  11. }
  12. public class CreaterHelper : ICreateHelper
  13. {
  14. public IUnitOfWork _unitOfWork;
  15. public CreaterHelper(IUnitOfWork unitOfWork)
  16. {
  17. _unitOfWork = unitOfWork;
  18. }
  19. private string baseText = null;
  20. public string GetBaseText(bool isLog)
  21. {
  22. if (string.IsNullOrEmpty(baseText))
  23. {
  24. string fileName = isLog ? "LogBase.txt" : "EntityBase.txt";
  25. string filePath = System.IO.Path.Combine(AppContext.BaseDirectory, "Sql", fileName);
  26. baseText = System.IO.File.ReadAllText(filePath);
  27. }
  28. return baseText;
  29. }
  30. public async Task CreateTables(string Namespace, string dll)
  31. {
  32. var assemeble = System.Reflection.Assembly.Load(dll);
  33. var types = assemeble.GetTypes();
  34. foreach (var t in types)
  35. {
  36. if (t.IsClass)
  37. {
  38. if (t.Namespace == Namespace)
  39. {
  40. if (t.BaseType == typeof(BaseEntity))
  41. {
  42. await CreateTable(t, false);
  43. }
  44. else if (t.BaseType == typeof(BaseLogEntity))
  45. {
  46. await CreateTable(t, true);
  47. }
  48. }
  49. }
  50. }
  51. }
  52. public async Task CreateTable(Type type, bool isLog)
  53. {
  54. if (type.Name.Contains("View"))
  55. {
  56. return;
  57. }
  58. var pros = type.GetProperties();
  59. System.Text.StringBuilder columns = new System.Text.StringBuilder();
  60. foreach (var pro in pros)
  61. {
  62. string columnType = "";
  63. if (string.Equals(pro.Name, "CreateUserName", StringComparison.OrdinalIgnoreCase)
  64. ||
  65. (string.Equals(pro.Name, "UpdateUserName", StringComparison.OrdinalIgnoreCase)
  66. ))
  67. {
  68. columnType = "nvarchar(16)";
  69. }
  70. else if (pro.Name.Equals("Id", StringComparison.OrdinalIgnoreCase))
  71. {
  72. if (isLog)
  73. {
  74. columnType = "[int] IDENTITY(1,1) NOT NULL";
  75. }
  76. else
  77. {
  78. columnType = "[int] NOT NULL";
  79. }
  80. }
  81. else if (pro.PropertyType.IsEnum)
  82. {
  83. columnType = "int";
  84. }
  85. else
  86. {
  87. var pt = pro.PropertyType;
  88. if (pt == typeof(int))
  89. {
  90. columnType = "int Not Null";
  91. }
  92. else if (pt == typeof(string))
  93. {
  94. columnType = "nvarchar(64) Null";
  95. }
  96. else if (pt == typeof(System.DateTime?))
  97. {
  98. columnType = "dateTime Null";
  99. }
  100. else if (pt == typeof(System.DateTime))
  101. {
  102. columnType = "dateTime Not Null";
  103. }
  104. else if (pt == typeof(decimal))
  105. {
  106. columnType = "decimal(18,2) Not Null";
  107. }
  108. }
  109. if (!string.IsNullOrEmpty(columnType))
  110. {
  111. columns.AppendLine($"[{pro.Name}] {columnType},");
  112. }
  113. }
  114. string baseText = GetBaseText(isLog);
  115. string sql = string.Format(baseText, type.Name, columns.ToString());
  116. Console.WriteLine(sql);
  117. await _unitOfWork.ExecuteAsync(sql);
  118. }
  119. }
  120. }