using Newtonsoft.Json.Linq; using Newtonsoft.Json; using NPOI.SS.Formula.Functions; using OASystem.Domain.ViewModels.Financial; using OASystem.Domain.ViewModels.Groups; using System.Collections; using System.Globalization; using System.Reflection; using System.Reflection.Metadata; using System.Security.Cryptography; using Pinyin4net.Format; using Pinyin4net; namespace OASystem.Infrastructure.Tools; /// /// 工具类 /// public static class CommonFun { public static string GUID => Guid.NewGuid().ToString("N"); public static bool IsNull(this string s) { return string.IsNullOrWhiteSpace(s); } public static bool NotNull(this string s) { return !string.IsNullOrWhiteSpace(s); } public static int GetRandom(int minNum, int maxNum) { var seed = BitConverter.ToInt32(Guid.NewGuid().ToByteArray(), 0); return new Random(seed).Next(minNum, maxNum); } public static string GetSerialNumber(string prefix = "") { return prefix + DateTime.Now.ToString("yyyyMMddHHmmssfff") + GetRandom(1000, 9999).ToString(); } public static string ToJson(this object obj) { return System.Text.Json.JsonSerializer.Serialize(obj); } public static T ToObject(this string json) { return System.Text.Json.JsonSerializer.Deserialize(json); } public static object GetDefaultVal(string typename) { return typename switch { "Boolean" => false, "DateTime" => default(DateTime), "Date" => default(DateTime), "Double" => 0.0, "Single" => 0f, "Int32" => 0, "String" => string.Empty, "Decimal" => 0m, _ => null, }; } public static void CoverNull(T model) where T : class { if (model == null) { return; } var typeFromHandle = typeof(T); var properties = typeFromHandle.GetProperties(); var array = properties; for (var i = 0; i < array.Length; i++) { var propertyInfo = array[i]; if (propertyInfo.GetValue(model, null) == null) { propertyInfo.SetValue(model, GetDefaultVal(propertyInfo.PropertyType.Name), null); } } } public static void CoverNull(List models) where T : class { if (models.Count == 0) { return; } foreach (var model in models) { CoverNull(model); } } public static bool ToBool(this object thisValue, bool errorvalue = false) { if (thisValue != null && thisValue != DBNull.Value && bool.TryParse(thisValue.ToString(), out bool reval)) { return reval; } return errorvalue; } #region 文件操作 public static FileInfo[] GetFiles(string directoryPath) { if (!IsExistDirectory(directoryPath)) { throw new DirectoryNotFoundException(); } var root = new DirectoryInfo(directoryPath); return root.GetFiles(); } public static bool IsExistDirectory(string directoryPath) { return Directory.Exists(directoryPath); } public static string ReadFile(string Path) { string s; if (!File.Exists(Path)) s = "不存在相应的目录"; else { var f2 = new StreamReader(Path, Encoding.Default); s = f2.ReadToEnd(); f2.Close(); f2.Dispose(); } return s; } public static void FileMove(string OrignFile, string NewFile) { File.Move(OrignFile, NewFile); } public static void CreateDir(string dir) { if (dir.Length == 0) return; if (!Directory.Exists(dir)) Directory.CreateDirectory(dir); } /// /// 验证文件名称 /// /// /// public static string ValidFileName(string fileName) { if (string.IsNullOrEmpty(fileName)) return Guid.NewGuid().ToString(); // 获取非法文件名字符 char[] invalidChars = Path.GetInvalidFileNameChars(); return new string(fileName.Where(c => !invalidChars.Contains(c)).ToArray()); } #endregion #region IP /// /// 是否为ip /// /// /// public static bool IsIP(string ip) { return Regex.IsMatch(ip, @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$"); } public static string GetIP(HttpRequest request) { if (request == null) { return ""; } var ip = request.Headers["X-Real-IP"].FirstOrDefault(); if (ip.IsNull()) { ip = request.Headers["X-Forwarded-For"].FirstOrDefault(); } if (ip.IsNull()) { ip = request.HttpContext?.Connection?.RemoteIpAddress?.ToString(); } if (ip.IsNull() || !IsIP(ip)) { ip = "127.0.0.1"; } return ip; } #endregion #region 随机数 /// /// 根据自定义随机包含的字符获取指定长度的随机字符 /// /// 随机字符长度 /// 随机字符 public static string GetRandomStr(int length) { string a = "ABCDEFGHJKLMNPQRSTUVWXYZ012356789"; StringBuilder sb = new StringBuilder(); for (int i = 0; i < length; i++) { sb.Append(a[new Random(Guid.NewGuid().GetHashCode()).Next(0, a.Length - 1)]); } return sb.ToString(); } /// /// 根据自定义随机包含的字符获取指定长度的随机字符 /// /// 随机字符长度 /// 随机字符 public static string GetRandomAllStr(int length) { string a = "ABCDEFGHJKLMNPQRSTUVWXYZabcdefghjklmnpqrstuvwxyz012356789"; StringBuilder sb = new StringBuilder(); for (int i = 0; i < length; i++) { sb.Append(a[new Random(Guid.NewGuid().GetHashCode()).Next(0, a.Length - 1)]); } return sb.ToString(); } /// /// 根据自定义随机包含的字符获取指定长度的随机字母(含大小写) /// /// 随机字符长度 /// 随机字符 public static string GetRandomLetter(int length) { string a = "ABCDEFGHJKLMNPQRSTUVWXYZabcdefghjklmnpqrstuvwxyz"; StringBuilder sb = new StringBuilder(); for (int i = 0; i < length; i++) { sb.Append(a[new Random(Guid.NewGuid().GetHashCode()).Next(0, a.Length - 1)]); } return sb.ToString(); } /// /// 生成不重复随机数字 /// 操作者:037 /// 2021-07-26 15:39 /// /// 输入字符串长度 /// 字符串 public static string GetRandomNumber(int len) { string allChar = "0,1,2,3,4,5,6,7,8,9"; string[] allCharArray = allChar.Split(','); string RandomCode = ""; int temp = -1; Random rand = new Random(); for (int i = 0; i < len; i++) { if (temp != -1) { rand = new Random(temp * i * ((int)DateTime.Now.Ticks)); } int t = rand.Next(allCharArray.Length - 1); while (temp == t) { t = rand.Next(allCharArray.Length - 1); } temp = t; RandomCode += allCharArray[t]; } return RandomCode; } #endregion #region decimal 截取 /// /// /// /// /// /// public static decimal CutDecimalWithN(decimal d, int n) { string strDecimal = d.ToString(); int index = strDecimal.IndexOf("."); if (index == -1 || strDecimal.Length < index + n + 1) { strDecimal = string.Format("{0:F" + n + "}", d); } else { int length = index; if (n != 0) { length = index + n + 1; } strDecimal = strDecimal.Substring(0, length); } return Decimal.Parse(strDecimal); } public static decimal CutDecimalWithN(decimal? d, int n) { if (d == null) { return Decimal.MinValue; } return CutDecimalWithN(Convert.ToDecimal(d), n); } #endregion #region decimal 保留两位小数 /// /// decimal 保留两位小数 不四舍五入 /// /// /// public static decimal DecimalsKeepTwo(this decimal myDecimal) { var subDecimal = Math.Floor(myDecimal * 100) / 100;//保留两位小数,直接截取 return subDecimal; } #endregion #region 团组模块 - 汇率相关存储解析 /// /// 团组模块 - 汇率相关 To List /// /// /// public static List GetCurrencyChinaToList(string? rateStr) { List currencyInfos = new List(); if (string.IsNullOrEmpty(rateStr)) return currencyInfos; if (rateStr.Contains("|")) { string[] currencyArr = rateStr.Split("|"); foreach (string currency in currencyArr) { string[] currency1 = currency.Split(":"); string[] currency2 = currency1[0].Split("("); CurrencyInfo rateInfo = new CurrencyInfo() { CurrencyCode = currency2[1].Replace(")", "").TrimEnd(), CurrencyName = currency2[0], Rate = decimal.Parse(currency1[1]), }; currencyInfos.Add(rateInfo); } } return currencyInfos; } /// /// 团组模块 - 汇率相关存储解析 To String /// /// /// public static string GetCurrencyChinaToString(List rates) { string rateStr = string.Empty; if (rates.Count <= 0) return rateStr; if (rates.Count == 1 ) { var rate = rates[0]; return string.Format("{0}({1}):{2}|", rate.CurrencyName, rate.CurrencyCode, rate.Rate); } foreach (CurrencyInfo rate in rates) { //存储方式: 美元(USD):6.2350|.......|墨西哥比索(MXN):1.0000 rateStr += string.Format("{0}({1}):{2}|", rate.CurrencyName, rate.CurrencyCode, rate.Rate); } if (rateStr.Length > 0) { rateStr = rateStr.Substring(0, rateStr.Length - 1); } return rateStr; } #endregion #region 验证身份证号码 /// /// 正则表达式 /// 验证身份证号码 /// /// /// public static bool IsValidChineseId(this string idNumber) { string pattern = @"^[1-9]\d{5}(18|19|20|21|22)?\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])\d{3}(\d|[Xx])$"; Regex regex = new Regex(pattern); return regex.IsMatch(idNumber); } /// /// 通过身份证提取生日 /// /// /// public static DateTime? GetBirthDateFromIdentityCard(string identityCard) { // 身份证号码正则表达式验证,支持18位身份证号码 if (!Regex.IsMatch(identityCard, @"^\d{17}(\d|X|x)$")) { return null; } // 获取出生日期(8位数字) string birthDateString = identityCard.Substring(6, 8); // 尝试将出生日期字符串转换为DateTime类型 if (DateTime.TryParse(birthDateString, out DateTime birthDate)) { return birthDate; } return null; } /// /// 通过身份证判断性别 /// /// /// 0 男1 女 -1 未设置 /// public static int GetGenderFromIdentityCard(string idNumber) { if (string.IsNullOrEmpty(idNumber) || idNumber.Length != 18) return -1; char genderChar = idNumber[16]; return int.Parse(genderChar.ToString()) % 2 == 0 ? 1 :0; } #endregion #region string格式日期格式化 /// /// string格式日期格式化 /// /// /// /// 格式化标准 /// yyyy-MM-dd yyyy/MM/dd /// /// public static string DateFormat(this string dateStr, string format) { if (!string.IsNullOrEmpty(dateStr)) { if (!string.IsNullOrEmpty(format)) { DateTime result; if (DateTime.TryParse(dateStr, out result)) { return result.ToString(format); } } } return ""; } #endregion /// /// List to DataTable /// 集合转换成datatable /// /// /// /// public static DataTable GetDataTableFromIList(List aIList) { DataTable _returnTable = new DataTable(); if (aIList != null && aIList.Count > 0) { object _baseObj = aIList[0]; Type objectType = _baseObj.GetType(); PropertyInfo[] properties = objectType.GetProperties(); DataColumn _col; foreach (PropertyInfo property in properties) { _col = new DataColumn(); _col.ColumnName = (string)property.Name; _col.DataType = property.PropertyType; _returnTable.Columns.Add(_col); } //Adds the rows to the table DataRow _row; foreach (object objItem in aIList) { _row = _returnTable.NewRow(); foreach (PropertyInfo property in properties) { _row[property.Name] = property.GetValue(objItem, null); } _returnTable.Rows.Add(_row); } } return _returnTable; } /// /// List to DataTable /// 集合转换成datatable /// public static DataTable ToDataTableArray(IList list) { DataTable result = new DataTable(); if (list.Count > 0) { PropertyInfo[] propertys = list[0].GetType().GetProperties(); foreach (PropertyInfo pi in propertys) { try { result.Columns.Add(pi.Name, pi.PropertyType); } catch (Exception ex) { Console.WriteLine($"{pi.Name}:{ex.Message}"); } } for (int i = 0; i < list.Count; i++) { ArrayList tempList = new ArrayList(); foreach (PropertyInfo pi in propertys) { object obj = pi.GetValue(list[i], null); tempList.Add(obj); } object[] array = tempList.ToArray(); result.LoadDataRow(array, true); } } return result; } /// ///获取指定月份起止日期 /// /// /// /// public static (DateTime StartDate, DateTime EndDate) GetMonthStartAndEndDates(int year, int month) { var calendar = new GregorianCalendar(); var daysInMonth = calendar.GetDaysInMonth(year, month); var startDate = new DateTime(year, month, 1); var endDate = new DateTime(year, month, daysInMonth); return (startDate, endDate); } /// /// 验证json字符串是否合法 /// /// /// public static bool IsValidJson(string jsonString) { try { JToken.Parse(jsonString); return true; } catch (JsonReaderException) { return false; } } /// /// 常见的双字姓氏列表 /// private static readonly HashSet commonDoubleSurnames = new HashSet { "欧阳", "司马", "上官", "夏侯", "诸葛", "东方", "赫连", "皇甫", "尉迟", "公羊", "澹台", "公冶", "宗政", "濮阳", "淳于", "单于", "太叔", "申屠", "公孙", "仲孙", "轩辕", "令狐", "钟离", "宇文", "长孙", "慕容", "鲜于", "闾丘", "司徒", "司空", "亓官", "司寇", "仉督", "子车", "颛孙", "端木", "巫马", "公西", "漆雕", "乐正", "壤驷", "公良", "拓跋", "夹谷", "宰父", "谷梁", "晋楚", "闫法", "汝鄢", "涂钦", "段干", "百里", "东郭", "南门", "呼延", "归海", "羊舌", "微生", "岳帅", "缑亢", "况后", "有琴", "梁丘", "左丘", "东门", "西门", "商牟", "佘佴", "伯赏", "南宫" }; /// /// 中文名字取姓氏和名字 /// /// /// 姓:lastName 名:firstName public static (string lastName, string firstName) GetLastNameAndFirstName(string fullName) { if (string.IsNullOrEmpty(fullName) || fullName.Length < 2) return ("", ""); // 尝试匹配双字姓氏 if (fullName.Length > 2) { string potentialDoubleSurname = fullName.Substring(0, 2); if (commonDoubleSurnames.Contains(potentialDoubleSurname)) { string lastName = potentialDoubleSurname; string firstName = fullName.Substring(2); return (lastName, firstName); } } // 默认单字姓氏 string singleSurname = fullName.Substring(0, 1); string remainingName = fullName.Substring(1); return (singleSurname, remainingName); } /// /// 中文名字转拼音 /// /// /// /// public static string ConvertToPinyin(string chineseName) { if (string.IsNullOrEmpty(chineseName)) return ""; HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat { ToneType = HanyuPinyinToneType.WITHOUT_TONE, VCharType = HanyuPinyinVCharType.WITH_V, CaseType = HanyuPinyinCaseType.UPPERCASE }; string pinyin = string.Empty; foreach (char ch in chineseName) { if (PinyinHelper.ToHanyuPinyinStringArray(ch) != null) { pinyin += PinyinHelper.ToHanyuPinyinStringArray(ch, format)[0]; } else { pinyin += ch; } } return pinyin; } }