2024-07-22 07:50:10 +00:00
|
|
|
|
using NLog;
|
|
|
|
|
using SqlSugar;
|
2024-05-28 14:36:38 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2024-05-30 15:52:57 +00:00
|
|
|
|
using System.Data.SqlClient;
|
2024-05-28 14:36:38 +00:00
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
|
|
|
|
namespace DeviceRepair.DataAccess
|
|
|
|
|
{
|
|
|
|
|
public class DbContext<T> where T : class, new()
|
|
|
|
|
{
|
2024-07-27 01:44:19 +00:00
|
|
|
|
private static readonly string connectionString = DeviceRepair.Utils.Config.Configurations.Properties.Conn;
|
2024-05-28 14:36:38 +00:00
|
|
|
|
|
2024-07-27 01:44:19 +00:00
|
|
|
|
private static readonly string logConn = DeviceRepair.Utils.Config.Configurations.Properties.logConn;
|
2024-05-28 14:36:38 +00:00
|
|
|
|
|
2024-07-27 01:44:19 +00:00
|
|
|
|
private static readonly string sfc = DeviceRepair.Utils.Config.Configurations.Properties.SfcConn;
|
2024-05-30 15:52:57 +00:00
|
|
|
|
|
2024-05-28 14:36:38 +00:00
|
|
|
|
public SqlSugarClient db;
|
|
|
|
|
//{
|
|
|
|
|
// get
|
|
|
|
|
// {
|
|
|
|
|
// var _db = new SqlSugarClient(new List<ConnectionConfig> {
|
|
|
|
|
// new ConnectionConfig()
|
|
|
|
|
// {
|
|
|
|
|
// ConnectionString = connectionString,
|
|
|
|
|
// DbType = DbType.SqlServer,
|
2024-05-30 15:52:57 +00:00
|
|
|
|
// InitKeyType = InitKeyType.Attribute,
|
|
|
|
|
// IsAutoCloseConnection = true,
|
2024-05-28 14:36:38 +00:00
|
|
|
|
// ConfigId = "main"
|
|
|
|
|
// },
|
|
|
|
|
// new ConnectionConfig()
|
|
|
|
|
// {
|
|
|
|
|
// ConnectionString = logConn,
|
|
|
|
|
// DbType = DbType.SqlServer,
|
2024-05-30 15:52:57 +00:00
|
|
|
|
// InitKeyType = InitKeyType.Attribute,
|
|
|
|
|
// IsAutoCloseConnection = true,
|
2024-05-28 14:36:38 +00:00
|
|
|
|
// ConfigId = "log"
|
|
|
|
|
// }
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
// _db.Aop.OnLogExecuting = (sql, pars) =>
|
|
|
|
|
// {
|
|
|
|
|
// Debug.WriteLine(sql + "\r\n" +
|
|
|
|
|
// db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)));
|
|
|
|
|
// };
|
|
|
|
|
|
|
|
|
|
// return _db;
|
|
|
|
|
// }
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
public SimpleClient<T> CurrentDb { get { return new SimpleClient<T>(db); } }
|
|
|
|
|
|
|
|
|
|
public DbContext()
|
|
|
|
|
{
|
2024-05-30 15:52:57 +00:00
|
|
|
|
SqlConnectionStringBuilder TsSFCdb = new SqlConnectionStringBuilder(sfc);
|
|
|
|
|
TsSFCdb.InitialCatalog = "SFCData";
|
|
|
|
|
string SFCData = TsSFCdb.ConnectionString;
|
|
|
|
|
TsSFCdb.InitialCatalog = "SFCAddOn";
|
|
|
|
|
string SFCAddOn = TsSFCdb.ConnectionString;
|
|
|
|
|
|
2024-05-28 14:36:38 +00:00
|
|
|
|
db = new SqlSugarClient(new List<ConnectionConfig> {
|
|
|
|
|
new ConnectionConfig()
|
|
|
|
|
{
|
|
|
|
|
ConnectionString = connectionString,
|
|
|
|
|
DbType = DbType.SqlServer,
|
2024-05-30 15:52:57 +00:00
|
|
|
|
InitKeyType = InitKeyType.Attribute,
|
|
|
|
|
IsAutoCloseConnection = true,
|
2024-05-28 14:36:38 +00:00
|
|
|
|
ConfigId = "main"
|
|
|
|
|
},
|
|
|
|
|
new ConnectionConfig()
|
|
|
|
|
{
|
|
|
|
|
ConnectionString = logConn,
|
|
|
|
|
DbType = DbType.SqlServer,
|
2024-05-30 15:52:57 +00:00
|
|
|
|
InitKeyType = InitKeyType.Attribute,
|
|
|
|
|
IsAutoCloseConnection = true,
|
2024-05-28 14:36:38 +00:00
|
|
|
|
ConfigId = "log"
|
2024-05-30 15:52:57 +00:00
|
|
|
|
},
|
|
|
|
|
new ConnectionConfig()
|
|
|
|
|
{
|
|
|
|
|
ConnectionString = SFCData,
|
|
|
|
|
DbType = DbType.SqlServer,
|
|
|
|
|
InitKeyType = InitKeyType.Attribute,
|
|
|
|
|
IsAutoCloseConnection = true,
|
|
|
|
|
ConfigId = "data"
|
|
|
|
|
},
|
|
|
|
|
new ConnectionConfig()
|
|
|
|
|
{
|
|
|
|
|
ConnectionString = SFCAddOn,
|
|
|
|
|
DbType = DbType.SqlServer,
|
|
|
|
|
InitKeyType = InitKeyType.Attribute,
|
|
|
|
|
IsAutoCloseConnection = true,
|
|
|
|
|
ConfigId = "addon"
|
2024-05-28 14:36:38 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
//调式代码 用来打印SQL
|
|
|
|
|
db.Aop.OnLogExecuting = (sql, pars) =>
|
|
|
|
|
{
|
|
|
|
|
Debug.WriteLine(sql + "\r\n" +
|
|
|
|
|
db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)));
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 获取所有
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public virtual List<T> GetList()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
return CurrentDb.GetList();
|
|
|
|
|
}
|
|
|
|
|
catch (SqlSugarException)
|
|
|
|
|
{
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception)
|
|
|
|
|
{
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 根据主键获取单条数据
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="Pk"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public virtual T GetSingle(int Pk)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (Pk <= 0)
|
|
|
|
|
return null;
|
|
|
|
|
return CurrentDb.GetById(Pk);
|
|
|
|
|
}
|
|
|
|
|
catch (SqlSugarException)
|
|
|
|
|
{
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception)
|
|
|
|
|
{
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 根据主键删除
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="id"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public virtual bool Delete(dynamic id)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
return CurrentDb.Delete(id);
|
|
|
|
|
}
|
|
|
|
|
catch (SqlSugarException)
|
|
|
|
|
{
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception)
|
|
|
|
|
{
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 更新
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="id"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public virtual bool Update(T obj)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
return CurrentDb.Update(obj);
|
|
|
|
|
}
|
|
|
|
|
catch (SqlSugarException)
|
|
|
|
|
{
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception)
|
|
|
|
|
{
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|