平安校园
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.

SqlSugarUtils.cs 6.3 KiB

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