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

268 lines
9.5 KiB
C#

1 month ago
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using WorkData;
using WorkData.Entity;
namespace SigmaDrawerElement
{
public static class tableInfoQuery
{
private static readonly string[] _entityClassNames = new string[]
{
"WorkData.Entity.WellReservesLayer",
"WorkData.Entity.WellPerforation",
"WorkData.Entity.InterpretDrillWellLayered",
"WorkData.Entity.WellInnerLayer",
"WorkData.Entity.WellLithology",
"WorkData.Entity.WellSideWallCoring",
"WorkData.Entity.WellCoring",
"WorkData.Entity.WellLithologicalAnalysis"
// 可随时在此添加或删除,无需修改逻辑
};
private const string TARGET_ASSEMBLY_NAME = "WorkData";
//加载外部程序集
private static Assembly LoadTargetAssembly()
{
try
{
// 方法 1通过程序集名称加载推荐
return Assembly.Load(TARGET_ASSEMBLY_NAME);
}
catch (FileNotFoundException)
{
// 方法 2通过文件路径加载如果知道确切路径
// return Assembly.LoadFrom(@"C:\Path\To\YourEntityLibrary.dll");
// 方法 3如果已在当前 AppDomain 中,尝试通过已加载的程序集查找
foreach (Assembly asm in AppDomain.CurrentDomain.GetAssemblies())
{
if (asm.FullName?.StartsWith(TARGET_ASSEMBLY_NAME) == true)
return asm;
}
return null;
}
}
public static Dictionary<string, string> TableDescriptionMap { get => _tableDescriptionMap; }
private static Dictionary<string, string> BuildTableDescriptionMap()
{
var map = new Dictionary<string, string>();
//var assembly = Assembly.GetExecutingAssembly(); // 或指定特定程序集
//加载外部程序集
Assembly targetAssembly = LoadTargetAssembly();
if (targetAssembly == null)
{
// 可记录日志或抛异常
return map;
}
foreach (string fullClassName in _entityClassNames)
{
// 通过类名字符串获取 Type
Type type = targetAssembly.GetType(fullClassName); //assembly.GetType(fullClassName);
if (type == null)
{
// 可选:记录日志或抛出异常
System.Diagnostics.Debug.WriteLine($"Type not found: {fullClassName}");
continue;
}
try
{
// 调用你的 DBHelp 非泛型方法(需提前实现)
string tableName = DBHelp.GetTableName(type);
string tableDesc = DBHelp.GetTableDescription(type);
if (!string.IsNullOrEmpty(tableName) && !string.IsNullOrEmpty(tableDesc))
{
map[tableName] = tableDesc;
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"Error processing {fullClassName}: {ex.Message}");
}
}
return map;
}
private static readonly Dictionary<string, string> _tableDescriptionMap = BuildTableDescriptionMap();
static public string tableEngName(string tableChsName)
{
string strName = "";
if (string.IsNullOrEmpty(tableChsName))
return "";
foreach (var (engName, chsName) in tableInfoQuery.TableDescriptionMap)
{
if(tableChsName == chsName)
{
strName = engName;break;
}
}
return strName;
//return _tableDescriptionMap.TryGetValue(tableEngName, out var desc) ? desc : "";
}
static public string tableChsName(string tableEngName)
{
string strName = "";
if (string.IsNullOrEmpty(tableEngName))
return "";
return _tableDescriptionMap.TryGetValue(tableEngName, out var desc) ? desc : "";
//if (tableEngName == DBHelp.GetTableName<WellReservesLayer>())
//{
// strName = DBHelp.GetTableDescription(typeof(WellReservesLayer));
//}
//else if (tableEngName == DBHelp.GetTableName<WellPerforation>())
//{
// strName = DBHelp.GetTableDescription(typeof(WellPerforation));
//}
//else if (tableEngName == DBHelp.GetTableName<InterpretDrillWellLayered>())
//{
// strName = DBHelp.GetTableDescription(typeof(InterpretDrillWellLayered));
//}
//else if (tableEngName == DBHelp.GetTableName<WellInnerLayer>())
//{
// strName = DBHelp.GetTableDescription(typeof(WellInnerLayer));
//}
//else if (tableEngName == DBHelp.GetTableName<WellLithology>())
//{
// strName = DBHelp.GetTableDescription(typeof(WellLithology));
//}
//return strName;
}
private static readonly Dictionary<string, List<EntityColumnInfo>> _tableColumnMap = BuildTableColumnMap();
private static Dictionary<string, List<EntityColumnInfo>> BuildTableColumnMap()
{
var map = new Dictionary<string, List<EntityColumnInfo>>();
Assembly targetAssembly = LoadTargetAssembly();
if (targetAssembly == null)
{
// 可记录日志或抛异常
return map;
}
foreach (string fullClassName in _entityClassNames)
{
// 通过类名字符串获取 Type
Type type = targetAssembly.GetType(fullClassName); //assembly.GetType(fullClassName);
if (type == null)
{
// 可选:记录日志或抛出异常
System.Diagnostics.Debug.WriteLine($"Type not found: {fullClassName}");
continue;
}
try
{
// 调用你的 DBHelp 非泛型方法(需提前实现)
string tableName = DBHelp.GetTableName(type);
if (!string.IsNullOrEmpty(tableName))
{
map[tableName] = DBHelp.GetEntityColumnInfo(type);
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"Error processing {fullClassName}: {ex.Message}");
}
}
return map;
}
static public List<EntityColumnInfo> getListTableColumn(string tableName)
{
List<EntityColumnInfo> colList = new List<EntityColumnInfo>();
string strName = "";
if (string.IsNullOrEmpty(tableName))
return colList;
return _tableColumnMap.TryGetValue(tableName, out var desc) ? desc : colList;
//if (tableName == DBHelp.GetTableName<WellReservesLayer>())
//{
// colList = DBHelp.GetEntityColumnInfo(typeof(WellReservesLayer));
//}
//else if (tableName == DBHelp.GetTableName<WellPerforation>())
//{
// colList = DBHelp.GetEntityColumnInfo(typeof(WellPerforation));
//}
//else if (tableName == DBHelp.GetTableName<InterpretDrillWellLayered>())
//{
// colList = DBHelp.GetEntityColumnInfo(typeof(InterpretDrillWellLayered));
//}
//else if (tableName == DBHelp.GetTableName<WellInnerLayer>())
//{
// colList = DBHelp.GetEntityColumnInfo(typeof(WellInnerLayer));
//}
//else if (tableName == DBHelp.GetTableName<WellLithology>())
//{
// colList = DBHelp.GetEntityColumnInfo(typeof(WellLithology));
//}
//return colList;
}
static public string getColumnNameFromChsName(string tableName, string chsName)
{
string engName = chsName;
List<EntityColumnInfo> colList = tableInfoQuery.getListTableColumn(tableName);
if (colList != null)
{
for (int i = 0; i < colList.Count; i++)
{
if (colList[i].ColumnDescription == chsName)
{
engName = colList[i].DbColumnName;
break;
}
}
}
return engName;
}
static public string getColumnChsNameFromEngName(string tableName, string engName)
{
string name = engName;
List<EntityColumnInfo> colList = tableInfoQuery.getListTableColumn(tableName);
if (colList != null)
{
for (int i = 0; i < colList.Count; i++)
{
if (colList[i].DbColumnName == engName)
{
name = colList[i].ColumnDescription;
break;
}
}
}
return name;
}
}
}