235 lines
8.1 KiB
C#
235 lines
8.1 KiB
C#
using System;
|
||
using System.Data;
|
||
using System.Linq;
|
||
using System.Reflection;
|
||
using System.Web;
|
||
|
||
namespace GSYWApi
|
||
{
|
||
/// <summary>
|
||
/// 通用方法帮助类
|
||
/// </summary>
|
||
public class CommonHelper
|
||
{
|
||
#region 方法 -> 将DateTime类型转换为long类型
|
||
/// <summary>
|
||
/// 将DateTime类型转换为long类型
|
||
/// </summary>
|
||
/// <param name="dt">时间</param>
|
||
/// <returns></returns>
|
||
public static long ConvertDataTimeLong(DateTime dt)
|
||
{
|
||
DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
|
||
TimeSpan toNow = dt.Subtract(dtStart);
|
||
long timeStamp = toNow.Ticks;
|
||
timeStamp = long.Parse(timeStamp.ToString().Substring(0, timeStamp.ToString().Length - 7));
|
||
return timeStamp;
|
||
}
|
||
#endregion
|
||
|
||
#region 方法 -> 将Long类型转换为DateTime类型
|
||
/// <summary>
|
||
/// 将Long类型转换为DateTime类型
|
||
/// </summary>
|
||
/// <param name="d">long</param>
|
||
/// <returns></returns>s
|
||
public static DateTime ConvertLongDateTime(long d)
|
||
{
|
||
DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
|
||
long lTime = long.Parse(d + "0000000");
|
||
TimeSpan toNow = new TimeSpan(lTime);
|
||
DateTime dtResult = dtStart.Add(toNow);
|
||
return dtResult;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 时间戳(毫秒值)String转换为DateTime类型转换
|
||
/// </summary>
|
||
/// <param name="time">毫秒时间戳</param>
|
||
/// <returns></returns>
|
||
public static DateTime TicksToDate(string time)
|
||
{
|
||
return new DateTime((Convert.ToInt64(time) * 10000) + 621355968000000000);
|
||
}
|
||
#endregion
|
||
|
||
#region 方法 -> 枚举参数过滤处理
|
||
/// <summary>
|
||
/// 枚举参数过滤处理,解决麻烦的SQL拼接
|
||
/// (对照数据表给定添加、更新的参数枚举类型ValueType)
|
||
/// </summary>
|
||
/// <param name="_Type">数据类型</param>
|
||
/// <param name="_String">数据值</param>
|
||
/// <returns></returns>
|
||
public static string IsArrayType(ValueType _Type, object _String = null)
|
||
{
|
||
if (_String != null && _String.ToString() != "")
|
||
{
|
||
switch (_Type)
|
||
{
|
||
case ValueType.String:
|
||
return string.Format("'{0}'", _String.ToString().Replace("'", " "));
|
||
case ValueType.Int:
|
||
case ValueType.Double:
|
||
case ValueType.Boolean:
|
||
return _String.ToString();
|
||
case ValueType.DateTime:
|
||
return string.Format("TO_DATE('{0}','YYYY/MM/DD HH24:MI:SS')", _String);
|
||
}
|
||
}
|
||
return "null";
|
||
}
|
||
|
||
/// <summary>
|
||
/// 枚举参数过滤处理,解决麻烦的SQL拼接
|
||
/// (对照数据表给定添加、更新的参数枚举类型ValueType)
|
||
/// </summary>
|
||
/// <param name="_Type">数据类型</param>
|
||
/// <param name="_DefaultValue">是否使用缺省值</param>
|
||
/// <returns></returns>
|
||
private static string IsArrayType(ValueType _Type, bool _DefaultValue = false)
|
||
{
|
||
if (_DefaultValue)
|
||
{
|
||
switch (_Type)
|
||
{
|
||
case ValueType.String:
|
||
return "''";
|
||
case ValueType.Int:
|
||
case ValueType.Double:
|
||
case ValueType.Boolean:
|
||
return "0";
|
||
case ValueType.DateTime:
|
||
return string.Format("TO_DATE('{0}','YYYY/MM/DD HH24:MI:SS')", DateTime.Now.ToString());
|
||
}
|
||
}
|
||
return "null";
|
||
}
|
||
#endregion
|
||
|
||
#region 方法 -> 定义5个类型的枚举
|
||
/// <summary>
|
||
/// 定义5个类型的枚举
|
||
/// </summary>
|
||
public enum ValueType
|
||
{
|
||
/// <summary>
|
||
/// 字符串
|
||
/// </summary>
|
||
String = 0,
|
||
/// <summary>
|
||
/// 整型
|
||
/// </summary>
|
||
Int = 1,
|
||
/// <summary>
|
||
/// 双精度
|
||
/// </summary>
|
||
Double = 2,
|
||
/// <summary>
|
||
/// true或false
|
||
/// </summary>
|
||
Boolean = 3,
|
||
/// <summary>
|
||
/// 日期
|
||
/// </summary>
|
||
DateTime = 4
|
||
}
|
||
#endregion
|
||
|
||
#region 方法 -> SQL语句分页
|
||
/// <summary>
|
||
/// SQL语句分页
|
||
/// </summary>
|
||
/// <param name="sql">SQL语句</param>
|
||
/// <param name="rn">开始的行号</param>
|
||
/// <param name="rowNum">结束的行号</param>
|
||
/// <returns></returns>
|
||
public static string GetPageSql(string sql, int? rn = null, int? rowNum = null)
|
||
{
|
||
if (rn != null && rowNum != null)
|
||
{
|
||
//分页查询
|
||
sql = string.Format(@"
|
||
SELECT * FROM (
|
||
SELECT ROWNUM RN,N.* FROM ({0}) N WHERE ROWNUM<={1}
|
||
) M WHERE M.RN>={2}", sql, rowNum, rn);
|
||
}
|
||
|
||
return sql;
|
||
}
|
||
#endregion
|
||
|
||
#region 方法 -> 为属性赋值
|
||
/// <summary>
|
||
/// 为属性赋值
|
||
/// </summary>
|
||
/// <typeparam name="T">源单类</typeparam>
|
||
/// <typeparam name="S">需要转换的实体类</typeparam>
|
||
/// <param name="source"></param>
|
||
/// <returns></returns>
|
||
public static S EntityConvert<T, S>(T source)
|
||
{
|
||
S target = Activator.CreateInstance<S>();
|
||
var sType = source.GetType();
|
||
var dType = typeof(S);
|
||
foreach (PropertyInfo now in sType.GetProperties())
|
||
{
|
||
var name = dType.GetProperty(now.Name);
|
||
if (name == null)
|
||
continue;
|
||
dType.GetProperty(now.Name).SetValue(target, now.GetValue(source));
|
||
}
|
||
return target;
|
||
}
|
||
#endregion
|
||
|
||
#region 方法 -> 获取指定页码Datatable数据
|
||
/// <summary>
|
||
/// 获取指定页码Datatable数据
|
||
/// </summary>
|
||
/// <param name="dtOrigin">源数据</param>
|
||
/// <param name="pageSize">返回第几页数据</param>
|
||
/// <param name="pageIndex">每页返回的数量</param>
|
||
/// <returns>Datatable数据格式</returns>
|
||
public static DataTable GetDataTableWithPageSize(DataTable dtOrigin, int pageSize, int pageIndex)
|
||
{
|
||
if (pageSize == 0)
|
||
{
|
||
pageSize = 10;
|
||
}
|
||
if (pageIndex == 0)
|
||
{
|
||
pageIndex = 1;
|
||
}
|
||
//克隆数据源
|
||
DataTable dtClone = dtOrigin.Clone();
|
||
dtClone.Columns.Add("RN", typeof(int));
|
||
//开始插入数据源
|
||
for (int RowCount = 0; RowCount < pageSize; RowCount++)
|
||
{
|
||
//获取当前数据源行数
|
||
int RN = pageSize * (pageIndex - 1) + RowCount;
|
||
//如果行数大于数据源实际数量,则直接跳过
|
||
if (RN >= dtOrigin.Rows.Count)
|
||
{
|
||
break;
|
||
}
|
||
//获取当前数据源内容
|
||
DataRow EndAccountDataRow = dtOrigin.Rows[RN];
|
||
//创建一个新 System.Data.DataRow 具有与表相同的架构
|
||
DataRow _DataRow = dtClone.NewRow();
|
||
//开始插入数据
|
||
for (int ColumnsCount = 0; ColumnsCount < dtOrigin.Columns.Count; ColumnsCount++)
|
||
{
|
||
_DataRow[ColumnsCount] = EndAccountDataRow[ColumnsCount];
|
||
}
|
||
_DataRow["RN"] = RN + 1;
|
||
//将数据加入到克隆的DataTable中
|
||
dtClone.Rows.Add(_DataRow);
|
||
}
|
||
|
||
return dtClone;
|
||
}
|
||
#endregion
|
||
}
|
||
} |