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

681 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 OperatingData.SDK
{
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(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly);
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.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);
}
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>
public static void UpdateTableData(OracleHelper oracleHelper, List<T> listData, string tableName, string[] whereName)
{
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(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly);
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.ToString().Replace("'", "''") + "'";
for (int i = 0; i < whereName.Length; i++)
{
if (whereName[i].ToUpper().Equals(item.Name.ToUpper()))
{
whereModified += (string.IsNullOrEmpty(whereModified) ? "" : " AND ") +
item.Name + "=" + "'" + 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)
{
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;
}
}
}
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>
/// 通用表数据添加(集合)
/// 朱梓毅 2021-8-3 重新整理
/// </summary>
/// <param name="oracleHelper">数据库SQL执行帮助类</param>
/// <param name="listData">实体类集合</param>
/// <param name="tableName">表名</param>
public static void InsertTableData(OracleHelper oracleHelper, List<T> listData, string tableName)
{
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)
{
//获取字段对应的值
object value = item.GetValue(t, null);
//如果是字符串类型的字段获取拼接SQL语句需要的字段名和值
if (IsType(item.PropertyType, "System.String") ||
item.PropertyType == typeof(System.String) || item.Name.ToLower() == "item")
{
if (value != null && !string.IsNullOrEmpty(value.ToString()))
{
names += (string.IsNullOrEmpty(names) ? "" : ",") + item.Name;
//此处要注意如果值里面包含了单引号,需要将其转化成两个单引号
values += (string.IsNullOrEmpty(values) ? "" : ",") + "'" + value.ToString().Replace("'", "''") + "'";
}
continue;
}
//如果是数值或者bool类型的字段获取拼接SQL语句需要的字段名和值
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;
}
//如果是数值或者bool类型的字段获取拼接SQL语句需要的字段名和值
//值的格式为TO_DATE('{0}','YYYY/MM/DD HH24:MI:SS'),{0}用value填充
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;
}
}
//如果主键以数据包中的键值为准,则执行如下代码
sqlStringList.Add(string.Format("INSERT INTO {0}({1}) VALUES({2})", tableName, names, values));
}
if (sqlStringList != null && sqlStringList.Count > 0)
{
//批量执行SQL语句
oracleHelper.ExecuteSqlTran(sqlStringList);
}
else
{
throw new Exception("SQL语句不可为空");
}
}
catch (Exception ex)
{
//LogHelper.WriteSendLog(ex.Message);
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, 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 排除主键字段primaryKey为主键值加上_Encrypt为加密字段KEYID也是主键值字段
if ((isPrimaryKey && item.Name.ToUpper() == primaryKey.ToUpper()) ||
item.Name.ToUpper().Contains((primaryKey + "_Encrypt").ToUpper()) ||
item.Name.ToUpper() == "KEYID" || item.Name.ToLower() == "item")
{
continue;
}
//获取字段对应的值
object value = item.GetValue(t, null);
//如果是字符串类型的字段获取拼接SQL语句需要的字段名和值
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;
}
//如果是数值或者bool类型的字段获取拼接SQL语句需要的字段名和值
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;
}
//如果是数值或者bool类型的字段获取拼接SQL语句需要的字段名和值
//值的格式为TO_DATE('{0}','YYYY/MM/DD HH24:MI:SS'),{0}用value填充
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)
{
//批量执行SQL语句
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) || item.Name.ToLower() == "item")
{
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.Length > 2 ? codes[2] : "";
string _MACADDRESS = codes.Length > 3 ? codes[3] : "";
//LogHelper.WriteReceiveLog(code.ToDecrypt());
if (_SERVERPARTCODE == "888888" || string.IsNullOrEmpty(_ENDDATE) || 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)
{
WebService.SDK.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
}
}