DeviceManager/DeviceRepair.DataAccess/Data/BaseDa.cs

470 lines
15 KiB
C#
Raw Normal View History

2024-11-09 04:25:57 +00:00
using DeviceRepair.Models;
using DeviceRepair.Models.History;
using Newtonsoft.Json;
using NLog;
using SqlSugar;
2024-07-27 01:44:19 +00:00
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
2024-11-09 04:25:57 +00:00
using System.Linq;
2024-07-27 01:44:19 +00:00
using System.Reflection;
using DbType = SqlSugar.DbType;
namespace DeviceRepair.DataAccess.Data
{
public class BaseDa
{
2024-11-09 04:25:57 +00:00
private static readonly Logger log = LogManager.GetCurrentClassLogger();
2024-08-02 02:52:45 +00:00
#region &
2024-08-08 08:46:02 +00:00
2024-08-02 02:52:45 +00:00
private SqlSugarClient deviceDB;
private SqlSugarClient deviceLogDB;
private SqlSugarClient sfcDataDB;
private SqlSugarClient sfcAddOnDB;
2024-11-09 04:25:57 +00:00
private SqlSugarClient sfcSystemDB;
2024-08-02 02:52:45 +00:00
internal OperationModel OperationInfo;
2024-08-08 08:46:02 +00:00
2024-07-27 01:44:19 +00:00
private IDictionary<string, string> m_ApiParameters;
2024-08-02 02:52:45 +00:00
2024-07-27 01:44:19 +00:00
/// <summary>
/// API输入参数
/// </summary>
public IDictionary<string, string> ApiParameters
{
2024-08-02 02:52:45 +00:00
get { return m_ApiParameters; }
private set { m_ApiParameters = value; }
2024-07-27 01:44:19 +00:00
}
/// <summary>
/// 设备主表
/// </summary>
public SqlSugarClient devMain
{
get
{
if (deviceDB == null)
{
2024-08-02 02:52:45 +00:00
SqlConnectionStringBuilder TsDevdb =
new SqlConnectionStringBuilder(DeviceRepair.Utils.Config.Configurations.Properties.Conn);
2024-07-27 01:44:19 +00:00
TsDevdb.InitialCatalog = "DriveMaintenance";
deviceDB = new SqlSugarClient(new ConnectionConfig()
{
ConnectionString = TsDevdb.ConnectionString,
DbType = DbType.SqlServer,
InitKeyType = InitKeyType.Attribute,
IsAutoCloseConnection = true,
});
}
2024-08-02 02:52:45 +00:00
2024-07-27 01:44:19 +00:00
return deviceDB;
}
}
/// <summary>
/// 设备日志
/// </summary>
public SqlSugarClient devLog
{
get
{
if (deviceLogDB == null)
{
2024-08-02 02:52:45 +00:00
SqlConnectionStringBuilder TsDevdb =
new SqlConnectionStringBuilder(DeviceRepair.Utils.Config.Configurations.Properties.Conn);
2024-07-27 01:44:19 +00:00
TsDevdb.InitialCatalog = "DeviceMaintenanceLog";
deviceLogDB = new SqlSugarClient(new ConnectionConfig()
{
ConnectionString = TsDevdb.ConnectionString,
DbType = DbType.SqlServer,
InitKeyType = InitKeyType.Attribute,
IsAutoCloseConnection = true,
});
}
2024-08-02 02:52:45 +00:00
2024-07-27 01:44:19 +00:00
return deviceLogDB;
}
}
/// <summary>
/// SFC 数据表
/// </summary>
public SqlSugarClient sfcData
{
get
{
if (sfcDataDB == null)
{
2024-08-02 02:52:45 +00:00
SqlConnectionStringBuilder TsSFCdb =
new SqlConnectionStringBuilder(DeviceRepair.Utils.Config.Configurations.Properties.SfcConn);
2024-07-27 01:44:19 +00:00
TsSFCdb.InitialCatalog = "SFCData";
sfcDataDB = new SqlSugarClient(new ConnectionConfig()
{
ConnectionString = TsSFCdb.ConnectionString,
DbType = DbType.SqlServer,
InitKeyType = InitKeyType.Attribute,
IsAutoCloseConnection = true,
});
}
2024-08-02 02:52:45 +00:00
2024-07-27 01:44:19 +00:00
return sfcDataDB;
}
}
/// <summary>
/// SFC AddOn 表
/// </summary>
public SqlSugarClient sfcAddOn
{
get
{
if (sfcAddOnDB == null)
{
2024-08-02 02:52:45 +00:00
SqlConnectionStringBuilder TsSFCdb =
new SqlConnectionStringBuilder(DeviceRepair.Utils.Config.Configurations.Properties.SfcConn);
2024-07-27 01:44:19 +00:00
TsSFCdb.InitialCatalog = "SFCAddOn";
sfcAddOnDB = new SqlSugarClient(new ConnectionConfig()
{
ConnectionString = TsSFCdb.ConnectionString,
DbType = DbType.SqlServer,
InitKeyType = InitKeyType.Attribute,
IsAutoCloseConnection = true,
});
}
2024-08-02 02:52:45 +00:00
2024-07-27 01:44:19 +00:00
return sfcAddOnDB;
}
}
2024-08-08 08:46:02 +00:00
2024-11-09 04:25:57 +00:00
public SqlSugarClient sfcSystem
{
get
{
if (sfcSystemDB == null)
{
SqlConnectionStringBuilder TsSFCdb =
new SqlConnectionStringBuilder(DeviceRepair.Utils.Config.Configurations.Properties.SfcConn);
TsSFCdb.InitialCatalog = "SFCSystem";
sfcSystemDB = new SqlSugarClient(new ConnectionConfig()
{
ConnectionString = TsSFCdb.ConnectionString,
DbType = DbType.SqlServer,
InitKeyType = InitKeyType.Attribute,
IsAutoCloseConnection = true,
});
}
return sfcSystemDB;
}
}
2024-08-02 02:52:45 +00:00
#endregion
#region
public BaseDa()
{
}
public BaseDa(IDictionary<string, string> apiParams)
{
m_ApiParameters = apiParams;
OperationInfo = new OperationModel().InstanceModel(apiParams);
}
#endregion
#region
2024-11-09 04:25:57 +00:00
/// <summary>
/// 操作历史
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="his">操作历史对象</param>
/// <param name="OperationType">操作描述</param>
/// <param name="TableName">表名</param>
/// <param name="CurrentDate">操作时间</param>
public void OperationHistory<T>(T his, EnumOperationType OperationType, string TableName, DateTime? CurrentDate = null) where T : HistoryBase
{
try
{
if (!CurrentDate.HasValue)
CurrentDate = DateTime.Now;
his.OperationComputer = OperationInfo.ComputerName;
his.OperationIP = OperationInfo.IPAddress;
his.Operator = OperationInfo.TsSFCUserId;
his.OperationType = OperationType.ToDescription();
his.OperationDate = CurrentDate.Value;
var dic = ToDictionary(his);
//var js = JsonConvert.SerializeObject(his);
//Dictionary<string, object> dic = JsonConvert.DeserializeObject<Dictionary<string, object>>(js);
devLog.Insertable(dic).AS(TableName).ExecuteCommandAsync();
}
catch (Exception ex)
{
log.Error(ex);
throw ex;
}
}
Dictionary<string, object> ToDictionary(object obj)
{
if (obj == null)
throw new ArgumentNullException(nameof(obj));
var props = obj.GetType()
.GetProperties(BindingFlags.Public | BindingFlags.Instance);
return props
.ToDictionary(
prop => prop.Name,
prop => prop.GetValue(obj, null)
);
}
2024-08-08 08:46:02 +00:00
2024-08-02 02:52:45 +00:00
/// <summary>
/// 取字符串参数
/// </summary>
/// <param name="Key">键</param>
/// <param name="Caption">字段标题</param>
/// <returns></returns>
/// <exception cref="ArgumentException"></exception>
public string GetParamString(string Key, string Caption)
{
try
{
if (!ApiParameters.ContainsKey(Key))
{
throw new ArgumentException($"参数【{Caption}】不能为空!");
}
return (ApiParameters[Key] + "").Trim();
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// 取整数型参数
/// </summary>
/// <param name="Key">键</param>
/// <param name="Caption">字段标题</param>
/// <returns></returns>
/// <exception cref="ArgumentException"></exception>
public int GetParamInt(string Key, string Caption)
{
try
{
if (!ApiParameters.ContainsKey(Key))
{
throw new ArgumentException($"参数【{Caption}】不能为空!");
}
int value = 0;
if (!int.TryParse(ApiParameters[Key], out value))
{
throw new ArgumentException($"参数【{Caption}】传入值转整数型出错!");
}
return value;
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// 取时间类型参数
/// </summary>
/// <param name="Key">键</param>
/// <param name="Caption">字段标题</param>
/// <returns></returns>
/// <exception cref="ArgumentException"></exception>
public DateTime GetParamDateTime(string Key, string Caption)
{
try
{
if (!ApiParameters.ContainsKey(Key))
{
throw new ArgumentException($"参数【{Caption}】不能为空!");
}
DateTime value;
2024-08-08 08:46:02 +00:00
if (!DateTime.TryParse(ApiParameters[Key] + "", out value))
2024-08-02 02:52:45 +00:00
{
throw new ArgumentException($"参数【{Caption}】传入值转时间类型出错!");
}
return value;
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// 通用唯一识别码类型参数
/// </summary>
/// <param name="Key">键</param>
/// <param name="Caption">字段标题</param>
/// <returns></returns>
/// <exception cref="ArgumentException"></exception>
public Guid GetParamGuid(string Key, string Caption)
{
try
{
if (!ApiParameters.ContainsKey(Key))
{
throw new ArgumentException($"参数【{Caption}】不能为空!");
}
2024-07-27 01:44:19 +00:00
2024-08-02 02:52:45 +00:00
Guid value;
2024-08-08 08:46:02 +00:00
if (!Guid.TryParse(ApiParameters[Key] + "", out value))
2024-08-02 02:52:45 +00:00
{
throw new ArgumentException($"参数【{Caption}】传入值转通用唯一识别码类型出错!");
}
return value;
}
catch (Exception ex)
{
throw ex;
}
}
2024-07-27 01:44:19 +00:00
2024-08-02 02:52:45 +00:00
/// <summary>
/// 集合 转 DataTable
/// </summary>
/// <param name="varlist"></param>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
2024-08-08 08:46:02 +00:00
public DataTable IListToDataTable<T>(IEnumerable<T> varlist)
2024-07-27 01:44:19 +00:00
{
DataTable dtReturn = new DataTable();
PropertyInfo[] oProps = null;
2024-08-02 02:52:45 +00:00
2024-07-27 01:44:19 +00:00
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];
}
2024-08-02 02:52:45 +00:00
2024-07-27 01:44:19 +00:00
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);
}
2024-08-02 02:52:45 +00:00
2024-07-27 01:44:19 +00:00
dtReturn.Rows.Add(dr);
}
2024-08-02 02:52:45 +00:00
2024-07-27 01:44:19 +00:00
return (dtReturn);
}
2024-08-08 08:46:02 +00:00
/// <summary>
/// 集合 转 DataTable
/// </summary>
/// <param name="varlist"></param>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public DataTable ListToDataTable<T>(List<T> varlist)
{
DataTable dtReturn = new DataTable();
PropertyInfo[] oProps = typeof(T).GetProperties();
dtReturn.TableName = typeof(T).Name;
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));
}
foreach (T rec in varlist)
{
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);
}
2024-11-09 16:05:40 +00:00
/// <summary>
/// 对象转换为字典
/// </summary>
/// <param name="obj">待转化的对象</param>
/// <returns></returns>
public Dictionary<string, string> ObjectToMap(object obj)
{
Dictionary<string, string> map = new Dictionary<string, string>(); //
Type t = obj.GetType(); // 获取对象对应的类, 对应的类型string
PropertyInfo[] pi = t.GetProperties(BindingFlags.Public | BindingFlags.Instance); // 获取当前type公共属性io
foreach (PropertyInfo p in pi)
{
MethodInfo m = p.GetGetMethod();
if (m != null && m.IsPublic)
{
// 进行判NULL处理
if (m.Invoke(obj, new object[] { }) != null)
{
map.Add(p.Name, m.Invoke(obj, new object[] { }).ToString()); // 向字典添加元素
}
}
}
return map;
}
public bool CompareDictionaries(Dictionary<string, string> d1, Dictionary<string, string> d2)
{
//比较d2>=d1
if (d1.Count != d2.Count) return false;
foreach (string key in d1.Keys)
{
if (!d2.ContainsKey(key)) return false;
if (d1[key]?.Trim() != d2[key].Trim()) return false;
}
return true;
}
2024-08-02 02:52:45 +00:00
#endregion
2024-07-27 01:44:19 +00:00
}
2024-08-02 02:52:45 +00:00
}