using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Web;
using WIDESEA_Core.Const;
using WIDESEA_Core.Enums;
namespace WIDESEA_Core.Helper
{
public static class UtilConvert
{
private static DateTime dateStart = new DateTime(1970, 1, 1, 8, 0, 0);
private static long longTime = 621355968000000000;
private static int samllTime = 10000000;
///
/// 时间戳转换成日期
///
///
///
public static DateTime GetTimeSpmpToDate(this object timeStamp)
{
if (timeStamp == null) return dateStart;
DateTime dateTime = new DateTime(longTime + Convert.ToInt64(timeStamp) * samllTime, DateTimeKind.Utc).ToLocalTime();
return dateTime;
}
public static string Serialize(this object obj, JsonSerializerSettings formatDate = null)
{
if (obj == null) return null;
formatDate = formatDate ?? new JsonSerializerSettings
{
DateFormatString = "yyyy-MM-dd HH:mm:ss"
};
return JsonConvert.SerializeObject(obj, formatDate);
}
public static T DeserializeObject(this string json)
{
if (string.IsNullOrEmpty(json))
{
return default(T);
}
if (json == "{}")
{
json = "[]";
}
return JsonConvert.DeserializeObject(json);
}
public static string FirstLetterToLower(this string thisValue)
{
if (string.IsNullOrEmpty(thisValue)) return string.Empty;
string result = thisValue.Substring(0, 1).ToLower() + thisValue.Substring(1, thisValue.Length - 1);
return result;
}
public static string FirstLetterToUpper(this string thisValue)
{
if (string.IsNullOrEmpty(thisValue)) return string.Empty;
string result = thisValue.Substring(0, 1).ToUpper() + thisValue.Substring(1, thisValue.Length - 1);
return result;
}
///
///
///
///
///
public static int ObjToInt(this object thisValue)
{
int reval = 0;
if (thisValue == null) return 0;
if (thisValue != DBNull.Value && int.TryParse(thisValue.ToString(), out reval))
{
return reval;
}
return reval;
}
///
///
///
///
///
public static int DoubleToInt(this double thisValue)
{
int reval = 0;
return Convert.ToInt32(thisValue);
}
///
///
///
///
///
///
public static int ObjToInt(this object thisValue, int errorValue)
{
int reval = 0;
if (thisValue != null && thisValue != DBNull.Value && int.TryParse(thisValue.ToString(), out reval))
{
return reval;
}
return errorValue;
}
public static long ObjToLong(this object thisValue)
{
long reval = 0;
if (thisValue == null) return 0;
if (thisValue != DBNull.Value && long.TryParse(thisValue.ToString(), out reval))
{
return reval;
}
return reval;
}
///
///
///
///
///
public static double ObjToMoney(this object thisValue)
{
double reval = 0;
if (thisValue != null && thisValue != DBNull.Value && double.TryParse(thisValue.ToString(), out reval))
{
return reval;
}
return 0;
}
///
///
///
///
///
///
public static double ObjToMoney(this object thisValue, double errorValue)
{
double reval = 0;
if (thisValue != null && thisValue != DBNull.Value && double.TryParse(thisValue.ToString(), out reval))
{
return reval;
}
return errorValue;
}
///
///
///
///
///
public static string ObjToString(this object thisValue)
{
if (thisValue != null) return thisValue.ToString().Trim();
return "";
}
///
///
///
///
///
public static bool IsNotEmptyOrNull(this object thisValue)
{
return ObjToString(thisValue) != "" && ObjToString(thisValue) != "undefined" && ObjToString(thisValue) != "null";
}
///
///
///
///
///
///
public static string ObjToString(this object thisValue, string errorValue)
{
if (thisValue != null) return thisValue.ToString().Trim();
return errorValue;
}
public static bool IsNullOrEmpty(this object thisValue) => thisValue == null || thisValue == DBNull.Value || string.IsNullOrWhiteSpace(thisValue.ToString());
///
///
///
///
///
public static Decimal ObjToDecimal(this object thisValue)
{
Decimal reval = 0;
if (thisValue != null && thisValue != DBNull.Value && decimal.TryParse(thisValue.ToString(), out reval))
{
return reval;
}
return 0;
}
///
///
///
///
///
///
public static Decimal ObjToDecimal(this object thisValue, decimal errorValue)
{
Decimal reval = 0;
if (thisValue != null && thisValue != DBNull.Value && decimal.TryParse(thisValue.ToString(), out reval))
{
return reval;
}
return errorValue;
}
///
///
///
///
///
public static DateTime ObjToDate(this object thisValue)
{
DateTime reval = DateTime.MinValue;
if (thisValue != null && thisValue != DBNull.Value && DateTime.TryParse(thisValue.ToString(), out reval))
{
reval = Convert.ToDateTime(thisValue);
}
return reval;
}
///
///
///
///
///
///
public static DateTime ObjToDate(this object thisValue, DateTime errorValue)
{
DateTime reval = DateTime.MinValue;
if (thisValue != null && thisValue != DBNull.Value && DateTime.TryParse(thisValue.ToString(), out reval))
{
return reval;
}
return errorValue;
}
///
///
///
///
///
public static bool ObjToBool(this object thisValue)
{
bool reval = false;
if (thisValue != null && thisValue != DBNull.Value && bool.TryParse(thisValue.ToString(), out reval))
{
return reval;
}
return reval;
}
///
/// 获取当前时间的时间戳
///
///
///
public static string DateToTimeStamp(this DateTime thisValue)
{
TimeSpan ts = thisValue - new DateTime(1970, 1, 1, 0, 0, 0, 0);
return Convert.ToInt64(ts.TotalSeconds).ToString();
}
public static object ChangeType(this object value, Type type)
{
if (value == null && type.IsGenericType) return Activator.CreateInstance(type);
if (value == null) return null;
if (type == value.GetType()) return value;
if (type.IsEnum)
{
if (value is string)
return Enum.Parse(type, value as string);
else
return Enum.ToObject(type, value);
}
if (!type.IsInterface && type.IsGenericType)
{
Type innerType = type.GetGenericArguments()[0];
object innerValue = ChangeType(value, innerType);
return Activator.CreateInstance(type, new object[] { innerValue });
}
if (value is string && type == typeof(Guid)) return new Guid(value as string);
if (value is string && type == typeof(Version)) return new Version(value as string);
if (!(value is IConvertible)) return value;
return Convert.ChangeType(value, type);
}
public static object ChangeTypeList(this object value, Type type)
{
if (value == null) return default;
var gt = typeof(List<>).MakeGenericType(type);
dynamic lis = Activator.CreateInstance(gt);
var addMethod = gt.GetMethod("Add");
string values = value.ToString();
if (values != null && values.StartsWith("(") && values.EndsWith(")"))
{
string[] splits;
if (values.Contains("\",\""))
{
splits = values.Remove(values.Length - 2, 2)
.Remove(0, 2)
.Split("\",\"");
}
else
{
splits = values.Remove(0, 1)
.Remove(values.Length - 2, 1)
.Split(",");
}
foreach (var split in splits)
{
var str = split;
if (split.StartsWith("\"") && split.EndsWith("\""))
{
str = split.Remove(0, 1)
.Remove(split.Length - 2, 1);
}
addMethod.Invoke(lis, new object[] { ChangeType(str, type) });
}
}
return lis;
}
public static string ToJson(this object value)
{
return JsonConvert.SerializeObject(value);
}
public static bool IsInt(this object obj)
{
if (obj == null)
return false;
bool reslut = Int32.TryParse(obj.ToString(), out int _number);
return reslut;
}
public static bool IsDate(this object str)
{
return str.IsDate(out _);
}
public static bool IsDate(this object str, out DateTime dateTime)
{
dateTime = DateTime.Now;
if (str == null || str.ToString() == "")
{
return false;
}
return DateTime.TryParse(str.ToString(), out dateTime);
}
///
/// 根据传入格式判断是否为小数
///
///
/// 18,5
///
public static bool IsNumber(this string str, string formatString)
{
if (string.IsNullOrEmpty(str)) return false;
return Regex.IsMatch(str, @"^[+-]?\d*[.]?\d*$");
}
public static bool IsGuid(this string guid)
{
Guid newId;
return guid.GetGuid(out newId);
}
public static bool GetGuid(this string guid, out Guid outId)
{
Guid emptyId = Guid.Empty;
return Guid.TryParse(guid, out outId);
}
///
/// 转换为短整型 qy 2024-7-15
///
/// 数据
public static short ToShort(this object data)
{
if (data == null)
return 0;
short result;
var success = short.TryParse(data.ToString(), out result);
if (success)
return result;
try
{
return Convert.ToInt16(data);
}
catch (Exception)
{
return 0;
}
}
///
/// 转换为高精度浮点数,并按指定的小数位4舍5入 qy 2024-7-15
///
/// 数据
/// 小数位数
public static decimal ToDecimal(this object data, int digits)
{
return Math.Round(ObjToDecimal(data), digits);
}
///
/// 枚举转List qy 2024-7-15
///
///
///
public static List