平安校园
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

207 linhas
6.3 KiB

  1. //
  2. namespace SafeCampus.SqlSugar;
  3. /// <summary>
  4. /// SqlSugar通用功能
  5. /// </summary>
  6. public static class SqlSugarUtils
  7. {
  8. /// <summary>
  9. /// 根据特性获取所有表信息
  10. /// </summary>
  11. /// <typeparam name="T"></typeparam>
  12. /// <returns></returns>
  13. public static List<SqlSugarTableInfo> GetTablesByAttribute<T>()
  14. {
  15. var tables = new List<SqlSugarTableInfo>();//结果集
  16. // 获取实体表
  17. var entityTypes = App.EffectiveTypes
  18. .Where(u => !u.IsInterface && !u.IsAbstract && u.IsClass && u.IsDefined(typeof(SugarTable), false))//有SugarTable特性
  19. .Where(u => u.IsDefined(typeof(T), false));//具有指定特性
  20. foreach (var entityType in entityTypes)
  21. {
  22. var tenantAttr = entityType.GetCustomAttribute<TenantAttribute>();//获取多租户特性
  23. var configId = tenantAttr.configId.ToString();//获取租户Id
  24. var connection = DbContext.DB.GetConnection(tenantAttr.configId.ToString());//根据租户ID获取连接信息
  25. var entityInfo = connection.EntityMaintenance.GetEntityInfo(entityType);//获取实体信息
  26. if (entityInfo != null)
  27. {
  28. var columns = GetTableColumns(configId, entityInfo.DbTableName);//获取字段信息
  29. tables.Add(new SqlSugarTableInfo
  30. {
  31. TableName = entityInfo.DbTableName,
  32. EntityName = entityInfo.EntityName,
  33. TableDescription = entityInfo.TableDescription,
  34. ConfigId = configId,
  35. Columns = columns
  36. });
  37. }
  38. }
  39. return tables;
  40. }
  41. /// <summary>
  42. /// 获取字段信息
  43. /// </summary>
  44. /// <param name="configId"></param>
  45. /// <param name="tableName"></param>
  46. /// <returns></returns>
  47. public static List<SqlSugarColumnInfo> GetTableColumns(string configId, string tableName)
  48. {
  49. var columns = new List<SqlSugarColumnInfo>();//结果集
  50. var connection = DbContext.DB.GetConnection(configId);
  51. var dbColumnInfos = connection.DbMaintenance.GetColumnInfosByTableName(tableName);//根据表名获取表信息
  52. if (dbColumnInfos != null)
  53. {
  54. //遍历字段获取信息
  55. dbColumnInfos.ForEach(it =>
  56. {
  57. if (it.DbColumnName.Contains("_"))//如果有下划线,转换一下
  58. {
  59. var column = "";//新的字段值
  60. var columnList = it.DbColumnName.Split('_');//根据下划线分割
  61. columnList.ForEach(it =>
  62. {
  63. column += StringHelper.FirstCharToUpper(it);//首字母大写
  64. });
  65. it.DbColumnName = column;//赋值给数据库字段
  66. }
  67. else
  68. {
  69. it.DbColumnName = StringHelper.FirstCharToUpper(it.DbColumnName);//首字母大写
  70. }
  71. columns.Add(new SqlSugarColumnInfo
  72. {
  73. ColumnName = it.DbColumnName,
  74. IsPrimaryKey = it.IsPrimarykey,
  75. ColumnDescription = it.ColumnDescription,
  76. DataType = it.DataType
  77. });
  78. });
  79. }
  80. return columns;
  81. }
  82. /// <summary>
  83. /// 数据库字段类型转.NET类型
  84. /// </summary>
  85. /// <param name="dataType">字段类型</param>
  86. /// <returns></returns>
  87. public static string ConvertDataType(string dataType)
  88. {
  89. switch (dataType)
  90. {
  91. case "text":
  92. case "varchar":
  93. case "char":
  94. case "nvarchar":
  95. case "nchar":
  96. case "blob":
  97. case "longtext":
  98. case "nclob":
  99. return "string";
  100. case "int":
  101. case "mediumint":
  102. return "int";
  103. case "smallint":
  104. return "Int16";
  105. case "tinyint":
  106. return "byte";
  107. case "bigint":
  108. case "integer"://sqlite数据库
  109. return "long";
  110. case "bit":
  111. case "boolean":
  112. return "bool";
  113. case "money":
  114. case "smallmoney":
  115. case "numeric":
  116. case "decimal":
  117. return "decimal";
  118. case "real":
  119. return "Single";
  120. case "datetime":
  121. case "smalldatetime":
  122. case "timestamp":
  123. case "date":
  124. case "year":
  125. case "time":
  126. return "DateTime";
  127. case "float":
  128. case "double":
  129. return "double";
  130. case "image":
  131. case "binary":
  132. case "varbinary":
  133. return "byte[]";
  134. case "uniqueidentifier":
  135. return "Guid";
  136. default:
  137. return "string";
  138. }
  139. }
  140. /// <summary>
  141. /// 数据类型转显示类型
  142. /// </summary>
  143. /// <param name="dataType"></param>
  144. /// <returns></returns>
  145. public static string DataTypeToEff(string dataType)
  146. {
  147. return dataType switch
  148. {
  149. "string" => EffTypeConst.INPUT,
  150. "int" => EffTypeConst.INPUT_NUMBER,
  151. "long" => EffTypeConst.INPUT,
  152. "float" => EffTypeConst.INPUT,
  153. "double" => EffTypeConst.INPUT,
  154. "decimal" => EffTypeConst.INPUT,
  155. "bool" => EffTypeConst.SWITCH,
  156. "Guid" => EffTypeConst.INPUT,
  157. "DateTime" => EffTypeConst.DATEPICKER,
  158. _ => EffTypeConst.INPUT
  159. };
  160. }
  161. /// <summary>
  162. /// 是否通用字段
  163. /// </summary>
  164. /// <param name="columnName">字段名</param>
  165. /// <returns></returns>
  166. public static bool IsCommonColumn(string columnName)
  167. {
  168. var columnList = new List<string>
  169. {
  170. nameof(BaseEntity.CreateTime), nameof(BaseEntity.UpdateTime),
  171. nameof(BaseEntity.CreateUserId), nameof(BaseEntity.CreateUser),
  172. nameof(BaseEntity.UpdateUserId), nameof(BaseEntity.UpdateUser),
  173. nameof(BaseEntity.IsDelete), nameof(DataEntityBase.CreateOrgId),
  174. nameof(PrimaryKeyEntity.ExtJson)
  175. };
  176. return columnList.Contains(columnName);
  177. }
  178. }