You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
185 lines
7.8 KiB
185 lines
7.8 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data;
|
|
using System.Data.SqlClient;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace ICSSoft.Common
|
|
{
|
|
public class HTTPHelper
|
|
{
|
|
private static log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
|
|
|
|
//public static string HttpPost(string apiName, string url, string body)
|
|
//{
|
|
// try
|
|
// {
|
|
// //log.Debug(url + Environment.NewLine + body);
|
|
// Encoding encoding = Encoding.UTF8;
|
|
// System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
|
|
// request.Method = "POST";
|
|
// request.Accept = "application/json, text/javascript, */*"; //"text/html, application/xhtml+xml, */*";
|
|
// request.ContentType = "application/json; charset=utf-8";
|
|
// // request.ContentType = "text/html, application/xhtml+xml";
|
|
// byte[] buffer = encoding.GetBytes(body);
|
|
// request.ContentLength = buffer.Length;
|
|
// request.GetRequestStream().Write(buffer, 0, buffer.Length);
|
|
// System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
|
|
// using (System.IO.StreamReader reader = new System.IO.StreamReader(response.GetResponseStream(), encoding))
|
|
// {
|
|
// return reader.ReadToEnd();
|
|
// }
|
|
// }
|
|
// catch (System.Net.WebException ex)
|
|
// {
|
|
// log.Error(ex.ToString() + Environment.NewLine + url + Environment.NewLine + body);
|
|
// throw new Exception(apiName + "调用失败," + ex.Message);
|
|
// }
|
|
//}
|
|
|
|
public static string HttpPost(string apiName, string url, string body)
|
|
{
|
|
try
|
|
{
|
|
Encoding encoding = Encoding.UTF8;
|
|
System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
|
|
request.Method = "POST";
|
|
request.Accept = "application/json, text/javascript, */*";
|
|
request.ContentType = "application/json; charset=utf-8";
|
|
request.Timeout = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["TimeOut"]);//超时时间
|
|
|
|
byte[] buffer = encoding.GetBytes(body);
|
|
request.ContentLength = buffer.Length;
|
|
request.GetRequestStream().Write(buffer, 0, buffer.Length);
|
|
System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
|
|
using (System.IO.StreamReader reader = new System.IO.StreamReader(response.GetResponseStream(), encoding))
|
|
{
|
|
return reader.ReadToEnd();
|
|
}
|
|
}
|
|
catch (System.Net.WebException ex)
|
|
{
|
|
log.Error(ex.ToString() + Environment.NewLine + url + Environment.NewLine + body);
|
|
throw new Exception(apiName + "调用失败," + ex.Message);
|
|
}
|
|
}
|
|
|
|
public static void BatchUpdate(SqlConnection conn, SqlTransaction transaction, string crateTemplateSql, string tmpTable, string updateCommandSql, DataSet dset, Dictionary<string, string[]> tableDecimalColumns = null)
|
|
{
|
|
try
|
|
{
|
|
if (crateTemplateSql != "")
|
|
{
|
|
using (SqlCommand command = new SqlCommand(crateTemplateSql, conn, transaction))
|
|
{
|
|
command.ExecuteNonQuery();
|
|
}
|
|
}
|
|
|
|
foreach (DataTable dt in dset.Tables)
|
|
{
|
|
if (dt == null) continue;
|
|
log.Debug("表名: " + dt.TableName);
|
|
// 根据表名获取对应的 decimal 列配置
|
|
string[] decimalColumns = null;
|
|
if (tableDecimalColumns != null && tableDecimalColumns.ContainsKey(dt.TableName))
|
|
{
|
|
decimalColumns = tableDecimalColumns[dt.TableName];
|
|
}
|
|
|
|
if (decimalColumns != null && decimalColumns.Length > 0)
|
|
{
|
|
ValidateDecimalColumns(dt, decimalColumns);
|
|
}
|
|
|
|
using (SqlBulkCopy bulkCopy = new SqlBulkCopy(conn, SqlBulkCopyOptions.Default, transaction))
|
|
{
|
|
bulkCopy.DestinationTableName = dt.TableName;
|
|
|
|
foreach (DataColumn column in dt.Columns)
|
|
{
|
|
log.Debug("column.ColumnName: " + column.ColumnName);
|
|
bulkCopy.ColumnMappings.Add(column.ColumnName, column.ColumnName);
|
|
}
|
|
|
|
bulkCopy.WriteToServer(dt);
|
|
bulkCopy.Close();
|
|
}
|
|
}
|
|
|
|
if (!string.IsNullOrEmpty(updateCommandSql))
|
|
{
|
|
using (SqlCommand command = new SqlCommand(updateCommandSql, conn, transaction))
|
|
{
|
|
int i = command.ExecuteNonQuery();
|
|
log.Debug("updateCommandSql影响行数: " + i);
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
log.Error("BatchUpdate 出错: " + ex.Message);
|
|
throw;
|
|
}
|
|
}
|
|
|
|
// 验证 decimal 列的数据类型
|
|
private static void ValidateDecimalColumns(DataTable dataTable, string[] decimalColumns)
|
|
{
|
|
// 添加 null 检查
|
|
if (dataTable == null || decimalColumns == null) return;
|
|
|
|
foreach (string columnName in decimalColumns)
|
|
{
|
|
// 检查 columnName 是否为 null 或空
|
|
if (string.IsNullOrEmpty(columnName)) continue;
|
|
|
|
if (dataTable.Columns.Contains(columnName))
|
|
{
|
|
// 检查列的数据类型
|
|
if (dataTable.Columns[columnName].DataType != typeof(decimal))
|
|
{
|
|
// 如果数据类型不是 decimal,创建一个新的 decimal 列
|
|
DataColumn newColumn = new DataColumn(columnName + "_New", typeof(decimal));
|
|
dataTable.Columns.Add(newColumn);
|
|
|
|
// 转换数据
|
|
foreach (DataRow row in dataTable.Rows)
|
|
{
|
|
// 检查 row 是否为 null
|
|
if (row == null) continue;
|
|
|
|
if (row[columnName] != DBNull.Value && row[columnName] != null)
|
|
{
|
|
decimal value=0;
|
|
if (decimal.TryParse(row[columnName].ToString(), out value))
|
|
{
|
|
row[newColumn] = value;
|
|
}
|
|
else
|
|
{
|
|
row[newColumn] = 0m;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
row[newColumn] = 0m;
|
|
}
|
|
}
|
|
|
|
// 删除原列并重命名新列
|
|
dataTable.Columns.Remove(columnName);
|
|
newColumn.ColumnName = columnName;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
continue;
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|