//
namespace SafeCampus.SqlSugar;
///
/// SqlSugar通用功能
///
public static class SqlSugarUtils
{
///
/// 根据特性获取所有表信息
///
///
///
public static List GetTablesByAttribute()
{
var tables = new List();//结果集
// 获取实体表
var entityTypes = App.EffectiveTypes
.Where(u => !u.IsInterface && !u.IsAbstract && u.IsClass && u.IsDefined(typeof(SugarTable), false))//有SugarTable特性
.Where(u => u.IsDefined(typeof(T), false));//具有指定特性
foreach (var entityType in entityTypes)
{
var tenantAttr = entityType.GetCustomAttribute();//获取多租户特性
var configId = tenantAttr.configId.ToString();//获取租户Id
var connection = DbContext.DB.GetConnection(tenantAttr.configId.ToString());//根据租户ID获取连接信息
var entityInfo = connection.EntityMaintenance.GetEntityInfo(entityType);//获取实体信息
if (entityInfo != null)
{
var columns = GetTableColumns(configId, entityInfo.DbTableName);//获取字段信息
tables.Add(new SqlSugarTableInfo
{
TableName = entityInfo.DbTableName,
EntityName = entityInfo.EntityName,
TableDescription = entityInfo.TableDescription,
ConfigId = configId,
Columns = columns
});
}
}
return tables;
}
///
/// 获取字段信息
///
///
///
///
public static List GetTableColumns(string configId, string tableName)
{
var columns = new List();//结果集
var connection = DbContext.DB.GetConnection(configId);
var dbColumnInfos = connection.DbMaintenance.GetColumnInfosByTableName(tableName);//根据表名获取表信息
if (dbColumnInfos != null)
{
//遍历字段获取信息
dbColumnInfos.ForEach(it =>
{
if (it.DbColumnName.Contains("_"))//如果有下划线,转换一下
{
var column = "";//新的字段值
var columnList = it.DbColumnName.Split('_');//根据下划线分割
columnList.ForEach(it =>
{
column += StringHelper.FirstCharToUpper(it);//首字母大写
});
it.DbColumnName = column;//赋值给数据库字段
}
else
{
it.DbColumnName = StringHelper.FirstCharToUpper(it.DbColumnName);//首字母大写
}
columns.Add(new SqlSugarColumnInfo
{
ColumnName = it.DbColumnName,
IsPrimaryKey = it.IsPrimarykey,
ColumnDescription = it.ColumnDescription,
DataType = it.DataType
});
});
}
return columns;
}
///
/// 数据库字段类型转.NET类型
///
/// 字段类型
///
public static string ConvertDataType(string dataType)
{
switch (dataType)
{
case "text":
case "varchar":
case "char":
case "nvarchar":
case "nchar":
case "blob":
case "longtext":
case "nclob":
return "string";
case "int":
case "mediumint":
return "int";
case "smallint":
return "Int16";
case "tinyint":
return "byte";
case "bigint":
case "integer"://sqlite数据库
return "long";
case "bit":
case "boolean":
return "bool";
case "money":
case "smallmoney":
case "numeric":
case "decimal":
return "decimal";
case "real":
return "Single";
case "datetime":
case "smalldatetime":
case "timestamp":
case "date":
case "year":
case "time":
return "DateTime";
case "float":
case "double":
return "double";
case "image":
case "binary":
case "varbinary":
return "byte[]";
case "uniqueidentifier":
return "Guid";
default:
return "string";
}
}
///
/// 数据类型转显示类型
///
///
///
public static string DataTypeToEff(string dataType)
{
return dataType switch
{
"string" => EffTypeConst.INPUT,
"int" => EffTypeConst.INPUT_NUMBER,
"long" => EffTypeConst.INPUT,
"float" => EffTypeConst.INPUT,
"double" => EffTypeConst.INPUT,
"decimal" => EffTypeConst.INPUT,
"bool" => EffTypeConst.SWITCH,
"Guid" => EffTypeConst.INPUT,
"DateTime" => EffTypeConst.DATEPICKER,
_ => EffTypeConst.INPUT
};
}
///
/// 是否通用字段
///
/// 字段名
///
public static bool IsCommonColumn(string columnName)
{
var columnList = new List
{
nameof(BaseEntity.CreateTime), nameof(BaseEntity.UpdateTime),
nameof(BaseEntity.CreateUserId), nameof(BaseEntity.CreateUser),
nameof(BaseEntity.UpdateUserId), nameof(BaseEntity.UpdateUser),
nameof(BaseEntity.IsDelete), nameof(DataEntityBase.CreateOrgId),
nameof(PrimaryKeyEntity.ExtJson)
};
return columnList.Contains(columnName);
}
}