2025-03-28 09:49:56 +08:00

726 lines
33 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.OracleClient;
using System.Linq;
using System.Reflection;
using System.Text;
namespace ServerPartTransmission.Common
{
public class OperationDataHelper<T>
{
#region ->
#region
/// <summary>
/// 通用表数据添加
/// Mr.Cai 2018-1-2
/// </summary>
/// <param name="oracleHelper">数据库SQL执行帮助类</param>
/// <param name="t">实体类</param>
/// <param name="tableName">表名</param>
/// <param name="primaryKey">主键名</param>
/// <param name="nextval">主键自增SQL函数</param>
/// <returns></returns>
public static void InsertTableData(OracleHelper oracleHelper, T t, string tableName, string primaryKey, string nextval)
{
if (string.IsNullOrEmpty(tableName))
{
throw new Exception("表名不可为空!");
}
if (string.IsNullOrEmpty(primaryKey))
{
throw new Exception("表名主键名不可为空!");
}
if (string.IsNullOrEmpty(nextval))
{
throw new Exception("序列不可为空!");
}
try
{
var pros = typeof(T).GetProperties();
string names = string.Empty;
string values = string.Empty;
foreach (var item in pros)
{
//排除主键字段
if (item.Name.ToUpper() != primaryKey.ToUpper() &&
item.Name.ToUpper() != (primaryKey + "_Encrypt").ToUpper() &&
item.Name.ToUpper() != "KEYID")
{
object value = item.GetValue(t, null);
if (IsType(item.PropertyType, "System.String") ||
item.PropertyType == typeof(System.String))
{
if (value != null && !string.IsNullOrEmpty(value.ToString()))
{
names += (string.IsNullOrEmpty(names) ? "" : ",") + item.Name;
values += (string.IsNullOrEmpty(values) ? "" : ",") + "'" + value + "'";
}
continue;
}
if (IsType(item.PropertyType, "System.Nullable`1[System.Int16]") ||
IsType(item.PropertyType, "System.Nullable`1[System.Int32]") ||
IsType(item.PropertyType, "System.Nullable`1[System.Int64]") ||
IsType(item.PropertyType, "System.Nullable`1[System.Double]") ||
IsType(item.PropertyType, "System.Nullable`1[System.Decimal]") ||
IsType(item.PropertyType, "System.Nullable`1[System.Boolean]") ||
item.PropertyType == typeof(System.Int16) ||
item.PropertyType == typeof(System.Int32) ||
item.PropertyType == typeof(System.Int64) ||
item.PropertyType == typeof(System.Double) ||
item.PropertyType == typeof(System.Decimal) ||
item.PropertyType == typeof(System.Boolean))
{
if (value != null)
{
names += (string.IsNullOrEmpty(names) ? "" : ",") + item.Name;
values += (string.IsNullOrEmpty(values) ? "" : ",") + value;
}
continue;
}
if (IsType(item.PropertyType, "System.Nullable`1[System.DateTime]") ||
item.PropertyType == typeof(System.DateTime))
{
if (value != null)
{
names += (string.IsNullOrEmpty(names) ? "" : ",") + item.Name;
values += (string.IsNullOrEmpty(values) ? "" : ",") +
string.Format("TO_DATE('{0}','YYYY/MM/DD HH24:MI:SS')", value);
}
else
{
names += (string.IsNullOrEmpty(names) ? "" : ",") + item.Name;
values += (string.IsNullOrEmpty(values) ? "" : ",") +
string.Format("TO_DATE('{0}','YYYY/MM/DD HH24:MI:SS')", DateTime.Now.ToString());
}
continue;
}
}
}
string _SqlString = string.Format("INSERT INTO {0}({1},{2}) VALUES({3},{4})", tableName, primaryKey, names, nextval, values);
oracleHelper.ExcuteSql(_SqlString);
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
#region
/// <summary>
/// 通用表数据修改
/// Mr.Cai 2018-3-15
/// </summary>
/// <param name="oracleHelper">数据库SQL执行帮助类</param>
/// <param name="listData">实体类集合</param>
/// <param name="tableName">表名</param>
/// <param name="whereName">条件名数组</param>
/// <param name="updateNull">是否更新空值字段</param>
public static void UpdateTableData(OracleHelper oracleHelper, List<T> listData, string tableName, string[] whereName, bool updateNull = false)
{
if (listData == null)
{
throw new Exception("数据集合不可为空!");
}
if (whereName == null)
{
throw new Exception("修改条件名不可为空!");
}
if (string.IsNullOrEmpty(tableName))
{
throw new Exception("表名不可为空!");
}
try
{
string[] sqlString = new string[listData.Count];
int index = 0;//索引
foreach (T t in listData)
{
var pros = typeof(T).GetProperties();
string modifiedValues = string.Empty;
string whereModified = string.Empty;
foreach (var item in pros)
{
// isPrimaryKey = true 排除主键字段
if (item.Name.ToUpper().Contains(("_Encrypt").ToUpper()) || item.Name.ToUpper() == "KEYID")
{
continue;
}
object value = item.GetValue(t, null);
if (IsType(item.PropertyType, "System.String") ||
item.PropertyType == typeof(System.String))
{
if (value != null && !string.IsNullOrEmpty(value.ToString()))
{
modifiedValues += (string.IsNullOrEmpty(modifiedValues) ? "" : ",") + item.Name + " = " + "'" + value + "'";
for (int i = 0; i < whereName.Length; i++)
{
if (whereName[i].ToUpper().Equals(item.Name.ToUpper()))
{
whereModified += (string.IsNullOrEmpty(whereModified) ? "" : " AND ") +
item.Name + "=" + "'" + value + "'";
}
}
}
else
{
if (updateNull)
{
modifiedValues += (string.IsNullOrEmpty(modifiedValues) ? "" : ",") + item.Name + " = " + "NULL";
}
}
continue;
}
if (IsType(item.PropertyType, "System.Nullable`1[System.Int16]") ||
IsType(item.PropertyType, "System.Nullable`1[System.Int32]") ||
IsType(item.PropertyType, "System.Nullable`1[System.Int64]") ||
IsType(item.PropertyType, "System.Nullable`1[System.Double]") ||
IsType(item.PropertyType, "System.Nullable`1[System.Decimal]") ||
IsType(item.PropertyType, "System.Nullable`1[System.Boolean]") ||
item.PropertyType == typeof(System.Int16) ||
item.PropertyType == typeof(System.Int32) ||
item.PropertyType == typeof(System.Int64) ||
item.PropertyType == typeof(System.Double) ||
item.PropertyType == typeof(System.Decimal) ||
item.PropertyType == typeof(System.Boolean))
{
if (value != null)
{
modifiedValues += (string.IsNullOrEmpty(modifiedValues) ? "" : ",") + item.Name + " = " + value;
for (int i = 0; i < whereName.Length; i++)
{
if (whereName[i].ToUpper().Equals(item.Name.ToUpper()))
{
whereModified += (string.IsNullOrEmpty(whereModified) ? "" : " AND ") + item.Name + " = " + value;
}
}
}
else
{
modifiedValues += (string.IsNullOrEmpty(modifiedValues) ? "" : ",") + item.Name + " = " + "NULL";
}
continue;
}
if (IsType(item.PropertyType, "System.Nullable`1[System.DateTime]") ||
item.PropertyType == typeof(System.DateTime))
{
if (value != null)
{
modifiedValues += (string.IsNullOrEmpty(modifiedValues) ? "" : ",") + item.Name + "=" +
string.Format("TO_DATE('{0}','YYYY/MM/DD HH24:MI:SS')", value);
for (int i = 0; i < whereName.Length; i++)
{
if (whereName[i].ToUpper().Equals(item.Name.ToUpper()))
{
whereModified += (string.IsNullOrEmpty(whereModified) ? "" : " AND ") + item.Name + "=" +
string.Format("TO_DATE('{0}','YYYY/MM/DD HH24:MI:SS')", value);
}
}
}
continue;
}
}
sqlString[index] += string.Format("UPDATE {0} SET {1} WHERE {2}", tableName, modifiedValues, whereModified);
index++;
}
oracleHelper.ExecuteSqlTran(sqlString);
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
#region
/// <summary>
/// 通用表数据添加(集合)
/// Mr.Cai 2018-3-9
/// </summary>
/// <param name="oracleHelper">数据库SQL执行帮助类</param>
/// <param name="listData">实体类集合</param>
/// <param name="tableName">表名</param>
/// <param name="isPrimaryKey">是否使用自增主键(序列)</param>
/// <param name="primaryKey">主键名</param>
/// <param name="nextval">主键自增SQL函数</param>
public static void InsertTableData(OracleHelper oracleHelper, List<T> listData, string tableName,
bool isPrimaryKey = false, string primaryKey = "", string nextval = "")
{
if (listData == null)
{
throw new Exception("数据集合不可为空!");
}
if (string.IsNullOrEmpty(tableName))
{
throw new Exception("表名不可为空!");
}
if (isPrimaryKey)//不插入原来主键,使用自增主键(序列),则判断必要条件
{
if (string.IsNullOrEmpty(primaryKey))
{
throw new Exception("表名主键名不可为空!");
}
if (string.IsNullOrEmpty(nextval))
{
throw new Exception("序列不可为空!");
}
}
try
{
List<string> sqlStringList = new List<string>();
string sqlString = string.Empty;
foreach (T t in listData)
{
var pros = typeof(T).GetProperties();
string names = string.Empty;
string values = string.Empty;
foreach (var item in pros)
{
// isPrimaryKey = true 排除主键字段
if ((isPrimaryKey && item.Name.ToUpper() == primaryKey.ToUpper()) ||
item.Name.ToUpper().Contains((primaryKey + "_Encrypt").ToUpper()) ||
item.Name.ToUpper() == "KEYID")
{
continue;
}
object value = item.GetValue(t, null);
if (IsType(item.PropertyType, "System.String") ||
item.PropertyType == typeof(System.String))
{
if (value != null && !string.IsNullOrEmpty(value.ToString()))
{
names += (string.IsNullOrEmpty(names) ? "" : ",") + item.Name;
values += (string.IsNullOrEmpty(values) ? "" : ",") + "'" + value.ToString().Replace("'", "''") + "'";
}
continue;
}
if (IsType(item.PropertyType, "System.Nullable`1[System.Int16]") ||
IsType(item.PropertyType, "System.Nullable`1[System.Int32]") ||
IsType(item.PropertyType, "System.Nullable`1[System.Int64]") ||
IsType(item.PropertyType, "System.Nullable`1[System.Double]") ||
IsType(item.PropertyType, "System.Nullable`1[System.Decimal]") ||
IsType(item.PropertyType, "System.Nullable`1[System.Boolean]") ||
item.PropertyType == typeof(System.Int16) ||
item.PropertyType == typeof(System.Int32) ||
item.PropertyType == typeof(System.Int64) ||
item.PropertyType == typeof(System.Double) ||
item.PropertyType == typeof(System.Decimal) ||
item.PropertyType == typeof(System.Boolean))
{
if (value != null)
{
names += (string.IsNullOrEmpty(names) ? "" : ",") + item.Name;
values += (string.IsNullOrEmpty(values) ? "" : ",") + value;
}
continue;
}
if (IsType(item.PropertyType, "System.Nullable`1[System.DateTime]") ||
item.PropertyType == typeof(System.DateTime))
{
if (value != null)
{
names += (string.IsNullOrEmpty(names) ? "" : ",") + item.Name;
values += (string.IsNullOrEmpty(values) ? "" : ",") +
string.Format("TO_DATE('{0}','YYYY/MM/DD HH24:MI:SS')", value);
}
continue;
}
}
if (isPrimaryKey)
{
sqlStringList.Add(string.Format("INSERT INTO {0}({1},{2}) VALUES({3},{4})", tableName, primaryKey, names, nextval, values));
}
else
{
sqlStringList.Add(string.Format("INSERT INTO {0}({1}) VALUES({2})", tableName, names, values));
}
}
if (sqlStringList != null && sqlStringList.Count > 0)
{
oracleHelper.ExecuteSqlTran(sqlStringList);
}
else
{
throw new Exception("SQL语句不可为空");
}
}
catch (Exception ex)
{
//LogHelper.WriteSendLog(ex.Message);
throw ex;
}
}
#endregion
#region
/// <summary>
/// 通用表数据添加(分表)
/// </summary>
/// <param name="oracleHelper">数据库SQL执行帮助类</param>
/// <param name="listData">分表实体类集合</param>
/// <param name="tableName">主表名</param>
/// <param name="isPrimaryKey">是否使用自增主键(序列)</param>
/// <param name="primaryKey">主键名</param>
/// <param name="nextval">主键自增SQL函数</param>
public static void InsertSubTableData(OracleHelper oracleHelper, Dictionary<string, List<T>> listData, string tableName,
bool isPrimaryKey = false, string primaryKey = "", string nextval = "")
{
if (listData == null)
{
throw new Exception("数据集合不可为空!");
}
if (string.IsNullOrEmpty(tableName))
{
throw new Exception("表名不可为空!");
}
if (isPrimaryKey)//不插入原来主键,使用自增主键(序列),则判断必要条件
{
if (string.IsNullOrEmpty(primaryKey))
{
throw new Exception("表名主键名不可为空!");
}
if (string.IsNullOrEmpty(nextval))
{
throw new Exception("序列不可为空!");
}
}
try
{
List<string> sqlStringList = new List<string>();
string sqlString = string.Empty;
foreach (var _Key in listData.Keys)
{
foreach (T t in listData[_Key])
{
var pros = typeof(T).GetProperties();
string names = string.Empty;
string values = string.Empty;
foreach (var item in pros)
{
// isPrimaryKey = true 排除主键字段
if ((isPrimaryKey && item.Name.ToUpper() == primaryKey.ToUpper()) ||
item.Name.ToUpper().Contains((primaryKey + "_Encrypt").ToUpper()) ||
item.Name.ToUpper() == "KEYID")
{
continue;
}
object value = item.GetValue(t, null);
if (IsType(item.PropertyType, "System.String") ||
item.PropertyType == typeof(System.String))
{
if (value != null && !string.IsNullOrEmpty(value.ToString()))
{
names += (string.IsNullOrEmpty(names) ? "" : ",") + item.Name;
values += (string.IsNullOrEmpty(values) ? "" : ",") + "'" + value.ToString().Replace("'", "''") + "'";
}
continue;
}
if (IsType(item.PropertyType, "System.Nullable`1[System.Int16]") ||
IsType(item.PropertyType, "System.Nullable`1[System.Int32]") ||
IsType(item.PropertyType, "System.Nullable`1[System.Int64]") ||
IsType(item.PropertyType, "System.Nullable`1[System.Double]") ||
IsType(item.PropertyType, "System.Nullable`1[System.Decimal]") ||
IsType(item.PropertyType, "System.Nullable`1[System.Boolean]") ||
item.PropertyType == typeof(System.Int16) ||
item.PropertyType == typeof(System.Int32) ||
item.PropertyType == typeof(System.Int64) ||
item.PropertyType == typeof(System.Double) ||
item.PropertyType == typeof(System.Decimal) ||
item.PropertyType == typeof(System.Boolean))
{
if (value != null)
{
names += (string.IsNullOrEmpty(names) ? "" : ",") + item.Name;
values += (string.IsNullOrEmpty(values) ? "" : ",") + value;
}
continue;
}
if (IsType(item.PropertyType, "System.Nullable`1[System.DateTime]") ||
item.PropertyType == typeof(System.DateTime))
{
if (value != null)
{
names += (string.IsNullOrEmpty(names) ? "" : ",") + item.Name;
values += (string.IsNullOrEmpty(values) ? "" : ",") +
string.Format("TO_DATE('{0}','YYYY/MM/DD HH24:MI:SS')", value);
}
continue;
}
}
if (isPrimaryKey)
{
sqlStringList.Add(string.Format("INSERT INTO {0}({1},{2}) VALUES({3},{4})", $"{tableName}_{_Key}", primaryKey, names, nextval, values));
}
else
{
sqlStringList.Add(string.Format("INSERT INTO {0}({1}) VALUES({2})", $"{tableName}_{_Key}", names, values));
}
}
}
if (sqlStringList != null && sqlStringList.Count > 0)
{
oracleHelper.ExecuteSqlTran(sqlStringList);
}
else
{
throw new Exception("SQL语句不可为空");
}
}
catch (Exception ex)
{
//LogHelper.WriteSendLog(ex.Message);
throw ex;
}
}
#endregion
#region OracleParameter数据添加
/// <summary>
/// 通用表OracleParameter数据添加集合
/// 注:该方式支持byte []类型数据添加不支持Boolean、SEQ主键方式添加
/// Mr.Cai 2018-7-19
/// </summary>
/// <param name="oracleHelper">数据库SQL执行帮助类</param>
/// <param name="listData">实体类集合</param>
/// <param name="tableName">表名</param>
/// <param name="isPrimaryKey">是否使用自增主键(序列)</param>
/// <param name="primaryKey">主键名</param>
/// <param name="nextval">主键自增SQL函数</param>
public static void InsertTableData_OracleParameter(OracleHelper oracleHelper, List<T> listData, string tableName)
{
if (listData == null)
{
throw new Exception("数据集合不可为空!");
}
if (string.IsNullOrEmpty(tableName))
{
throw new Exception("表名不可为空!");
}
try
{
//需传递插入参数的集合
List<OracleHelper.OracleSQLInfo> OracleSQLInfos = new List<OracleHelper.OracleSQLInfo>();
foreach (T t in listData)
{
//插入语句及所有字段集合
OracleHelper.OracleSQLInfo oracleSQLInfo = new OracleHelper.OracleSQLInfo();
//所有字段集合
List<OracleParameter> oracleParameters = new List<OracleParameter>();
string sqlString = "INSERT INTO " + tableName + " ({0}) VALUES ({1})";
var pros = typeof(T).GetProperties();
string names = string.Empty;
string values = string.Empty;
foreach (var item in pros)
{
object value = item.GetValue(t, null);
if (IsType(item.PropertyType, "System.String") ||
item.PropertyType == typeof(System.String))
{
if (value != null && !string.IsNullOrEmpty(value.ToString()))
{
names += (string.IsNullOrEmpty(names) ? "" : ",") + item.Name;
values += (string.IsNullOrEmpty(values) ? "" : ",") + ":" + item.Name;
oracleParameters.Add(GetOracleParameter(OracleType.VarChar, item.Name, value.ToString().Replace("'", "''")));
}
continue;
}
if (IsType(item.PropertyType, "System.Nullable`1[System.Int16]") ||
IsType(item.PropertyType, "System.Nullable`1[System.Int32]") ||
IsType(item.PropertyType, "System.Nullable`1[System.Int64]") ||
IsType(item.PropertyType, "System.Nullable`1[System.Double]") ||
IsType(item.PropertyType, "System.Nullable`1[System.Decimal]") ||
item.PropertyType == typeof(System.Int16) ||
item.PropertyType == typeof(System.Int32) ||
item.PropertyType == typeof(System.Int64) ||
item.PropertyType == typeof(System.Double) ||
item.PropertyType == typeof(System.Decimal))
{
if (value != null && !string.IsNullOrEmpty(value.ToString()))
{
names += (string.IsNullOrEmpty(names) ? "" : ",") + item.Name;
values += (string.IsNullOrEmpty(values) ? "" : ",") + ":" + item.Name;
oracleParameters.Add(GetOracleParameter(OracleType.Number, item.Name, value));
}
continue;
}
if (IsType(item.PropertyType, "System.Nullable`1[System.Boolean]") ||
item.PropertyType == typeof(System.Boolean))
{
throw new Exception("ORACLE数据 不支持 Boolean请使用Number 类型!");
}
if (IsType(item.PropertyType, "System.Nullable`1[System.DateTime]") ||
item.PropertyType == typeof(System.DateTime))
{
if (value != null && !string.IsNullOrEmpty(value.ToString()))
{
names += (string.IsNullOrEmpty(names) ? "" : ",") + item.Name;
values += (string.IsNullOrEmpty(values) ? "" : ",") + ":" + item.Name;
oracleParameters.Add(GetOracleParameter(OracleType.DateTime, item.Name, value));
}
continue;
}
if (IsType(item.PropertyType, "System.Nullable`1[System..Byte[]]") ||
item.PropertyType == typeof(System.Byte[]))
{
if (value != null && !string.IsNullOrEmpty(value.ToString()))
{
names += (string.IsNullOrEmpty(names) ? "" : ",") + item.Name;
values += (string.IsNullOrEmpty(values) ? "" : ",") + ":" + item.Name;
oracleParameters.Add(GetOracleParameter(OracleType.Blob, item.Name, value));
}
continue;
}
}
if (!string.IsNullOrEmpty(names) && !string.IsNullOrEmpty(values))
{
oracleSQLInfo.sqlString = string.Format(sqlString, names, values);
oracleSQLInfo.sqlStringArray = oracleParameters;
OracleSQLInfos.Add(oracleSQLInfo);
}
else
{
throw new Exception("不可存在所有值为空的SQL数据");
}
}
if (OracleSQLInfos != null && OracleSQLInfos.Count > 0)
{
oracleHelper.ExecuteSqlTran(OracleSQLInfos);
}
else
{
throw new Exception("SQL语句集合不可为空");
}
}
catch (Exception ex)
{
//LogHelper.WriteSendLog(ex.Message);
throw ex;
}
}
#endregion
#endregion
#region ->
/// <summary>
/// 判断授权码是否正确
/// </summary>
/// <param name="_OracleHelper">数据库SQL执行帮助类</param>
/// <param name="code">授权码</param>
/// <returns></returns>
public static string[] IsCorrectCode(OracleHelper _OracleHelper, string code)
{
if (string.IsNullOrEmpty(code))
{
return null;
}
try
{
string[] codes = code.ToDecrypt().Split('|');
string _SERVERPARTCODE = codes[0];
string _SHOPCODE = codes[1];
string _ENDDATE = codes[2];
string _MACADDRESS = codes.Length > 3 ? codes[3] : "";
//LogHelper.WriteReceiveLog(code.ToDecrypt());
if (DateTime.Now <= DateTime.Parse(_ENDDATE).AddDays(1))
{
if (!string.IsNullOrEmpty(_SERVERPARTCODE))
{
string _SqlString = string.Format(
"SELECT * FROM HIGHWAY_EXCHANGE.T_AUTHORIZATION WHERE AUTHORIZATION_CODE = '{0}' {1}{2}", code,
" AND SERVERPARTCODE = '" + _SERVERPARTCODE + "'", string.IsNullOrEmpty(_MACADDRESS) ? "" :
" AND MACHINE_MACADDRESS = '" + _MACADDRESS + "'");
DataSet dt = _OracleHelper.ExcuteSqlGetDataSet(_SqlString);
if (dt.Tables[0].Rows.Count > 0)
{
return codes;
}
}
}
}
catch (Exception ex)
{
LogHelper.WriteSendLog(ex.Message);
}
return null;
}
#endregion
#region ->
/// <summary>
/// 类型匹配
/// </summary>
/// <param name="type">类型</param>
/// <param name="typeName">类型名称</param>
/// <returns></returns>
public static bool IsType(Type type, string typeName)
{
if (type.ToString() == typeName)
return true;
if (type.ToString() == "System.Object")
return false;
return IsType(type.BaseType, typeName);
}
#endregion
#region OracleParameter
public static OracleParameter GetOracleParameter(OracleType type, string name, object value)
{
OracleParameter oracleParameter = new OracleParameter(name, type);
oracleParameter.Value = value;
return oracleParameter;
}
#endregion
}
}