You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

104 lines
4.3 KiB

2 years ago
  1. using SqlSugar;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Text;
  5. namespace EntityBuilder
  6. {
  7. public class CreateFileHelper
  8. {
  9. /// <summary>
  10. /// 创建文件目录和文件
  11. /// </summary>
  12. /// <param name="tables">所有表</param>
  13. /// <param name="fileDir">文件目录</param>
  14. public static void CreateEntityHelper(List<DbTableInfo> tables, string fileDir)
  15. {
  16. CreateDirectory(fileDir);
  17. //實體類文件名
  18. var filePath = fileDir + "EntityHelper.cs";
  19. //文件是否存在
  20. var exists = File.Exists(filePath);
  21. if (exists)
  22. {
  23. File.Delete(filePath);
  24. }
  25. //創建文件
  26. var fs = new FileStream(filePath, FileMode.Create, FileAccess.Write);
  27. using (var sw = new StreamWriter(fs))
  28. {
  29. //生成代码
  30. var code = CreateFileHelper.BuilderEntityHelperCode(tables);
  31. //写入代码到文件
  32. sw.WriteLine(code);
  33. sw.Close();
  34. fs.Close();
  35. }
  36. }
  37. /// <summary>
  38. /// 创建文件目录
  39. /// </summary>
  40. /// <param name="targetDir"></param>
  41. private static void CreateDirectory(string targetDir)
  42. {
  43. var dir = new DirectoryInfo(targetDir);
  44. if (!dir.Exists)
  45. dir.Create();
  46. }
  47. /// <summary>
  48. /// 根据表名,生成代码
  49. /// </summary>
  50. /// <param name="tables">todo: describe tables parameter on BuilderEntityHelperCode</param>
  51. /// <returns></returns>
  52. public static string BuilderEntityHelperCode(List<DbTableInfo> tables)
  53. {
  54. var sb = new StringBuilder();
  55. var EntityCase = new StringBuilder();
  56. sb.Append("using Entity.Sugar;").Append("\n\n");
  57. sb.Append("namespace Entity ").Append("\n");
  58. sb.Append("{ ").Append("\n");
  59. sb.Append("\t public class EntityHelper").Append("\n");
  60. sb.Append("\t { ").Append("\n");
  61. foreach (DbTableInfo table in tables)
  62. {
  63. //實體類名稱
  64. var entityName = table.Name;//GenVarName(table.Name);
  65. var keyPre = "";
  66. var entitys = entityName.Split('_');
  67. if (entityName.StartsWith("OVW_"))
  68. {
  69. keyPre = "ovw_";
  70. }
  71. sb.Append("\t\t").Append("public const string ").Append((keyPre + entitys[entitys.Length - 1]).ToUpper()).Append(" = ").Append("\"" + keyPre + (entitys[entitys.Length - 1]).ToLower() + "\";\n\n");
  72. EntityCase.Append("\t\t\t\t").Append("case " + (keyPre + entitys[entitys.Length - 1]).ToUpper() + ":\n\n");
  73. EntityCase.Append("\t\t\t\t\t\t").Append("entity = new " + entityName + "();\n\n");
  74. EntityCase.Append("\t\t\t\t\t\t").Append("break;\n");
  75. }
  76. sb.Append("\t\t").Append("/// <summary>").Append("\n");
  77. sb.Append("\t\t").Append("/// get the entity object").Append("\n");
  78. sb.Append("\t\t").Append("/// </summary>").Append("\n");
  79. sb.Append("\t\t").Append("/// <param name=\"type\"/>type{String}</param>").Append("\n");
  80. sb.Append("\t\t").Append("/// <returns>entity{Object}entity</returns>").Append("\n");
  81. sb.Append("\t\t").Append("public static object GetEntity(string type)").Append("\n");
  82. sb.Append("\t\t").Append("{").Append("\n");
  83. sb.Append("\t\t\t\t").Append("var entity = new object();").Append("\n");
  84. sb.Append("\t\t\t\t").Append("entity = \"\";").Append("\n");
  85. sb.Append("\t\t\t\t").Append("switch (type.ToLower())").Append("\n");
  86. sb.Append("\t\t\t\t").Append("{").Append("\n");
  87. sb.Append("\t\t").Append(EntityCase.ToString()).Append("\n");
  88. sb.Append("\t\t\t\t").Append("}").Append("\n");
  89. sb.Append("\t\t\t\t").Append("return entity;").Append("\n");
  90. sb.Append("\t\t").Append("}").Append("\n");
  91. sb.Append("\t").Append("}").Append("\n");
  92. sb.Append("}").Append("\n");
  93. return sb.ToString();
  94. }
  95. }
  96. }