using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Reflection; using System.Security.Cryptography; using System.Text; using System.Text.RegularExpressions; using System.Xml; using Newtonsoft.Json; using NPinyin; namespace Common.Utils { public static class ConvertExtension { /// /// /// /// /// public static int ObjToInt(this object thisValue) { int reval = 0; if (thisValue == null) return 0; if (thisValue != null && thisValue != DBNull.Value && int.TryParse(thisValue.ToString(), out reval)) { return reval; } return reval; } public static bool ContainsAll(this IEnumerable source, IEnumerable items) { if (source == null || items == null) return false; var sourceCounts = source.GroupBy(x => x).ToDictionary(g => g.Key, g => g.Count()); var itemsCounts = items.GroupBy(x => x).ToDictionary(g => g.Key, g => g.Count()); foreach (var item in itemsCounts) { if (!sourceCounts.TryGetValue(item.Key, out int count) || count < item.Value) { return false; } } return true; } public static IEnumerable RemoveRange(this IEnumerable source, IEnumerable items) { if (source == null) return Enumerable.Empty(); if (items == null || !items.Any()) return source; var list = new List(source); foreach (var item in items) { int index = list.IndexOf(item); if (index >= 0) { list.RemoveAt(index); } } return list; } /// /// /// /// /// /// 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 != null && 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 T GetRandomItem(this IEnumerable thisValue) { if (thisValue != null && thisValue.Count() > 0) { int index = Random.Shared.Next(thisValue.Count()); return thisValue.ToList()[index]; } else if (thisValue.Count() == 1) { return thisValue.ToList()[0]; } else return default(T); } /// /// /// /// /// /// 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 IEnumerable thisValue) { return thisValue != null && thisValue.Count() > 0; ; } /// /// /// /// /// /// public static string ObjToString(this object thisValue, string errorValue) { if (thisValue != null) return thisValue.ToString().Trim(); return errorValue; } /// /// /// /// /// 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 double ObjToDouble(this object thisValue) { double reval = 0; if (thisValue != null && thisValue != DBNull.Value && double.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 DateTime ConvertToDateTime(this long timeStamp) { var dto = DateTimeOffset.FromUnixTimeSeconds(timeStamp); return dto.ToLocalTime().DateTime; } public static DateTime ConvertToDateTime(this string timeStamp) { try { var dto = DateTimeOffset.FromUnixTimeSeconds(Convert.ToInt64(timeStamp)); return dto.ToLocalTime().DateTime; } catch (Exception e) { throw new FormatException($"无效的Unix时间戳: {timeStamp}", e); } } public static string DateToTimeStamp(this DateTime thisValue) { DateTimeOffset dto = new DateTimeOffset(thisValue); return dto.ToUnixTimeSeconds().ToString(); } public static decimal ToCeilingFixed2(this decimal value) { try { return Convert.ToInt32(Math.Ceiling(Math.Round(value * 100, 2))) / (decimal)100.0; } catch { return (decimal)0.0; } } public static decimal ToFloorFixed2(this decimal value) { try { return Convert.ToInt32(Math.Floor(Math.Round(value * 100, 2))) / (decimal)100.0; } catch { return (decimal)0.0; } } public static double ToCeilingFixed2(this double value) { try { return Convert.ToInt32(Math.Ceiling(Math.Round(value * 100, 2))) / 100.0; } catch { return 0.0; } } public static double ToFloorFixed2(this double value) { try { return Convert.ToInt32(Math.Floor(Math.Round(value * 100, 2))) / 100.0; } catch { return 0.0; } } public static string ConvertToPinyin(this string chinese) { if (string.IsNullOrWhiteSpace(chinese)) return "*"; return Pinyin.GetPinyin(chinese).Trim().Replace(" ", "").ToUpper(); } public static int ToCeilingInt(this double value) { try { return Convert.ToInt32(Math.Ceiling(Math.Round(value, 1))); } catch { return Convert.ToInt32(value); } } public static int ToCeilingInt(this decimal value) { try { return Convert.ToInt32(Math.Ceiling(Math.Round(value, 1))); } catch { return Convert.ToInt32(value); } } public static string[] Remove(this string[] value, string str) { List valueList = value.ToList(); if (!valueList.Contains(str)) return value; valueList.Remove(str); return valueList.ToArray(); } public static int[] Remove(this int[] value, int str) { List valueList = value.ToList(); if (!valueList.Contains(str)) return value; valueList.Remove(str); return valueList.ToArray(); } // // // time: public static long ToTimeStamp(this DateTime time) { return new DateTimeOffset(time).ToUnixTimeSeconds(); } // // // timeStamp: public static DateTime ToDateTime(this long timeStamp) { return DateTimeOffset.FromUnixTimeSeconds(timeStamp).ToLocalTime().DateTime; } // // // obj: public static string ToJson(this object obj) { return JsonConvert.SerializeObject(obj); } // // // str: public static T ToObject(this string str) { return JsonConvert.DeserializeObject(str); } // // // str: // // IgnoreCapitalized: public static string ToUrlEncode(this string str, bool IgnoreCapitalized = true) { return WebUtility.UrlEncode(IgnoreCapitalized ? str.ToLowerInvariant() : str); } // // // source: // // T: // // T:System.ArgumentException: public static T Clone(T source) { return JsonConvert.DeserializeObject(source.ToJson()); } // // // str: public static string ToUrlDecode(this string str) { return WebUtility.UrlDecode(str); } // // // str: public static string ToBase64String(this string str) { return Convert.ToBase64String(Encoding.Default.GetBytes(str)); } // // // str: public static string ToParseBase64(this string str) { try { byte[] bytes = Convert.FromBase64String(str); return Encoding.Default.GetString(bytes); } catch { return ""; } } // // // value: public static double ToDoubleFixed2(this double value) { try { return (double)Convert.ToInt32(Math.Ceiling(Math.Round(value * 100.0, 2))) / 100.0; } catch { return 0.0; } } // // // value: public static decimal ToDecimalFixed2(this decimal value) { try { return (decimal)Convert.ToInt32(Math.Ceiling(Math.Round(value * 100m, 2))) / 100m; } catch { return 0m; } } // // // value: public static double ToDoubleFloor2(this double value) { try { return (double)Convert.ToInt32(Math.Floor(Math.Round(value * 100.0, 2))) / 100.0; } catch { return 0.0; } } // // // str: // public static string ToReverse(this string str) { return new string(str.ToCharArray().AsEnumerable().Reverse().ToArray()); } // // // value: public static decimal ToDecimalFloor2(this decimal value) { try { return (decimal)Convert.ToInt32(Math.Floor(Math.Round(value * 100m, 2))) / 100m; } catch { return 0m; } } // // // content: public static string ToDecodeBase64(this string content) { byte[] bytes = Convert.FromBase64String(content); return Encoding.UTF8.GetString(bytes); } // // // mobile: public static string ToHidMobile(this string mobile) { if (string.IsNullOrWhiteSpace(mobile)) { return ""; } if (IsHandset(mobile)) { return mobile.Substring(0, 3) + "****" + mobile.Substring(mobile.Length - 4, 4); } return mobile; } // // // str_handset: public static bool IsHandset(string str_handset) { return Regex.IsMatch(str_handset, "^1[3456789]\\d{9}$"); } // // // list: // // T: public static bool IsNull(this List list) { if (list != null) { return !list.Any(); } return true; } // // // source: // // isReplace: // // isToLower: public static string ToSHA1Encrypt(this string source, bool isReplace = true, bool isToLower = false) { string text = BitConverter.ToString(SHA1.Create().ComputeHash(Encoding.UTF8.GetBytes(source))); if (isReplace) { text = text.Replace("-", ""); } if (isToLower) { text = text.ToLower(); } return text; } } }