DeviceManager/DeviceRepair.DataAccess/Data/BaseDa.cs
2024-07-27 09:44:19 +08:00

176 lines
5.5 KiB
C#

using SqlSugar;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Reflection;
using DbType = SqlSugar.DbType;
namespace DeviceRepair.DataAccess.Data
{
public class BaseDa
{
private IDictionary<string, string> m_ApiParameters;
/// <summary>
/// API输入参数
/// </summary>
public IDictionary<string, string> ApiParameters
{
get
{
return m_ApiParameters;
}
private set
{
m_ApiParameters = value;
}
}
public BaseDa()
{
}
public BaseDa(IDictionary<string, string> apiParams)
{
m_ApiParameters = apiParams;
}
private SqlSugarClient deviceDB;
private SqlSugarClient deviceLogDB;
private SqlSugarClient sfcDataDB;
private SqlSugarClient sfcAddOnDB;
/// <summary>
/// 设备主表
/// </summary>
public SqlSugarClient devMain
{
get
{
if (deviceDB == null)
{
SqlConnectionStringBuilder TsDevdb = new SqlConnectionStringBuilder(DeviceRepair.Utils.Config.Configurations.Properties.Conn);
TsDevdb.InitialCatalog = "DriveMaintenance";
deviceDB = new SqlSugarClient(new ConnectionConfig()
{
ConnectionString = TsDevdb.ConnectionString,
DbType = DbType.SqlServer,
InitKeyType = InitKeyType.Attribute,
IsAutoCloseConnection = true,
});
}
return deviceDB;
}
}
/// <summary>
/// 设备日志
/// </summary>
public SqlSugarClient devLog
{
get
{
if (deviceLogDB == null)
{
SqlConnectionStringBuilder TsDevdb = new SqlConnectionStringBuilder(DeviceRepair.Utils.Config.Configurations.Properties.Conn);
TsDevdb.InitialCatalog = "DeviceMaintenanceLog";
deviceLogDB = new SqlSugarClient(new ConnectionConfig()
{
ConnectionString = TsDevdb.ConnectionString,
DbType = DbType.SqlServer,
InitKeyType = InitKeyType.Attribute,
IsAutoCloseConnection = true,
});
}
return deviceLogDB;
}
}
/// <summary>
/// SFC 数据表
/// </summary>
public SqlSugarClient sfcData
{
get
{
if (sfcDataDB == null)
{
SqlConnectionStringBuilder TsSFCdb = new SqlConnectionStringBuilder(DeviceRepair.Utils.Config.Configurations.Properties.SfcConn);
TsSFCdb.InitialCatalog = "SFCData";
sfcDataDB = new SqlSugarClient(new ConnectionConfig()
{
ConnectionString = TsSFCdb.ConnectionString,
DbType = DbType.SqlServer,
InitKeyType = InitKeyType.Attribute,
IsAutoCloseConnection = true,
});
}
return sfcDataDB;
}
}
/// <summary>
/// SFC AddOn 表
/// </summary>
public SqlSugarClient sfcAddOn
{
get
{
if (sfcAddOnDB == null)
{
SqlConnectionStringBuilder TsSFCdb = new SqlConnectionStringBuilder(DeviceRepair.Utils.Config.Configurations.Properties.SfcConn);
TsSFCdb.InitialCatalog = "SFCAddOn";
sfcAddOnDB = new SqlSugarClient(new ConnectionConfig()
{
ConnectionString = TsSFCdb.ConnectionString,
DbType = DbType.SqlServer,
InitKeyType = InitKeyType.Attribute,
IsAutoCloseConnection = true,
});
}
return sfcAddOnDB;
}
}
public DataTable ToDataTable<T>(IEnumerable<T> varlist)
{
DataTable dtReturn = new DataTable();
PropertyInfo[] oProps = null;
foreach (T rec in varlist)
{
if (oProps == null)
{
oProps = ((Type)rec.GetType()).GetProperties();
foreach (PropertyInfo pi in oProps)
{
Type colType = pi.PropertyType;
if ((colType.IsGenericType) && (colType.GetGenericTypeDefinition() == typeof(Nullable<>)))
{
colType = colType.GetGenericArguments()[0];
}
dtReturn.Columns.Add(new DataColumn(pi.Name, colType));
}
}
DataRow dr = dtReturn.NewRow();
foreach (PropertyInfo pi in oProps)
{
dr[pi.Name] = pi.GetValue(rec, null) == null ? DBNull.Value : pi.GetValue(rec, null);
}
dtReturn.Rows.Add(dr);
}
return (dtReturn);
}
}
}