CommonFun.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733
  1. using MathNet.Numerics.Statistics;
  2. using Newtonsoft.Json;
  3. using Newtonsoft.Json.Linq;
  4. using OASystem.Domain.ViewModels.Groups;
  5. using System.Collections;
  6. using System.Globalization;
  7. using System.Reflection;
  8. using TinyPinyin;
  9. namespace OASystem.Infrastructure.Tools;
  10. /// <summary>
  11. /// 工具类
  12. /// </summary>
  13. public static class CommonFun
  14. {
  15. public static string GUID => Guid.NewGuid().ToString("N");
  16. public static bool IsNull(this string s)
  17. {
  18. return string.IsNullOrWhiteSpace(s);
  19. }
  20. public static bool NotNull(this string s)
  21. {
  22. return !string.IsNullOrWhiteSpace(s);
  23. }
  24. public static int GetRandom(int minNum, int maxNum)
  25. {
  26. var seed = BitConverter.ToInt32(Guid.NewGuid().ToByteArray(), 0);
  27. return new Random(seed).Next(minNum, maxNum);
  28. }
  29. public static string GetSerialNumber(string prefix = "")
  30. {
  31. return prefix + DateTime.Now.ToString("yyyyMMddHHmmssfff") + GetRandom(1000, 9999).ToString();
  32. }
  33. public static string ToJson(this object obj)
  34. {
  35. return System.Text.Json.JsonSerializer.Serialize(obj);
  36. }
  37. public static T ToObject<T>(this string json)
  38. {
  39. return System.Text.Json.JsonSerializer.Deserialize<T>(json);
  40. }
  41. public static object GetDefaultVal(string typename)
  42. {
  43. return typename switch
  44. {
  45. "Boolean" => false,
  46. "DateTime" => default(DateTime),
  47. "Date" => default(DateTime),
  48. "Double" => 0.0,
  49. "Single" => 0f,
  50. "Int32" => 0,
  51. "String" => string.Empty,
  52. "Decimal" => 0m,
  53. _ => null,
  54. };
  55. }
  56. public static void CoverNull<T>(T model) where T : class
  57. {
  58. if (model == null)
  59. {
  60. return;
  61. }
  62. var typeFromHandle = typeof(T);
  63. var properties = typeFromHandle.GetProperties();
  64. var array = properties;
  65. for (var i = 0; i < array.Length; i++)
  66. {
  67. var propertyInfo = array[i];
  68. if (propertyInfo.GetValue(model, null) == null)
  69. {
  70. propertyInfo.SetValue(model, GetDefaultVal(propertyInfo.PropertyType.Name), null);
  71. }
  72. }
  73. }
  74. public static void CoverNull<T>(List<T> models) where T : class
  75. {
  76. if (models.Count == 0)
  77. {
  78. return;
  79. }
  80. foreach (var model in models)
  81. {
  82. CoverNull(model);
  83. }
  84. }
  85. public static bool ToBool(this object thisValue, bool errorvalue = false)
  86. {
  87. if (thisValue != null && thisValue != DBNull.Value && bool.TryParse(thisValue.ToString(), out bool reval))
  88. {
  89. return reval;
  90. }
  91. return errorvalue;
  92. }
  93. #region 文件操作
  94. public static FileInfo[] GetFiles(string directoryPath)
  95. {
  96. if (!IsExistDirectory(directoryPath))
  97. {
  98. throw new DirectoryNotFoundException();
  99. }
  100. var root = new DirectoryInfo(directoryPath);
  101. return root.GetFiles();
  102. }
  103. public static bool IsExistDirectory(string directoryPath)
  104. {
  105. return Directory.Exists(directoryPath);
  106. }
  107. public static string ReadFile(string Path)
  108. {
  109. string s;
  110. if (!File.Exists(Path))
  111. s = "不存在相应的目录";
  112. else
  113. {
  114. var f2 = new StreamReader(Path, Encoding.Default);
  115. s = f2.ReadToEnd();
  116. f2.Close();
  117. f2.Dispose();
  118. }
  119. return s;
  120. }
  121. public static void FileMove(string OrignFile, string NewFile)
  122. {
  123. File.Move(OrignFile, NewFile);
  124. }
  125. public static void CreateDir(string dir)
  126. {
  127. if (dir.Length == 0) return;
  128. if (!Directory.Exists(dir))
  129. Directory.CreateDirectory(dir);
  130. }
  131. /// <summary>
  132. /// 验证文件名称
  133. /// </summary>
  134. /// <param name="fileName"></param>
  135. /// <returns></returns>
  136. public static string ValidFileName(string fileName)
  137. {
  138. if (string.IsNullOrEmpty(fileName)) return Guid.NewGuid().ToString();
  139. // 获取非法文件名字符
  140. char[] invalidChars = Path.GetInvalidFileNameChars();
  141. return new string(fileName.Where(c => !invalidChars.Contains(c)).ToArray());
  142. }
  143. /// <summary>
  144. /// 文件后缀名签名
  145. /// </summary>
  146. public static Dictionary<string, List<byte[]>> FileSignature => new Dictionary<string, List<byte[]>>
  147. {
  148. { ".jpeg", new List<byte[]>
  149. {
  150. new byte[] { 0xFF, 0xD8, 0xFF, 0xE0 },
  151. new byte[] { 0xFF, 0xD8, 0xFF, 0xE2 },
  152. new byte[] { 0xFF, 0xD8, 0xFF, 0xE3 },
  153. }
  154. },
  155. { ".png", new List<byte[]>
  156. {
  157. new byte[] { 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A }
  158. }
  159. },
  160. { ".pdf", new List<byte[]>
  161. {
  162. new byte[] { 0x25, 0x50, 0x44, 0x46 }
  163. }
  164. },
  165. { ".xls", new List<byte[]>
  166. {
  167. new byte[] { 0xD0, 0xCF, 0x11, 0xE0, 0xA1, 0xB1, 0x1A, 0xE1 }
  168. }
  169. },
  170. { ".xlsx", new List<byte[]>
  171. {
  172. new byte[] { 0x50, 0x4B, 0x03, 0x04 }
  173. }
  174. }
  175. };
  176. #endregion
  177. #region IP
  178. /// <summary>
  179. /// 是否为ip
  180. /// </summary>
  181. /// <param name="ip"></param>
  182. /// <returns></returns>
  183. public static bool IsIP(string ip)
  184. {
  185. 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?)$");
  186. }
  187. public static string GetIP(HttpRequest request)
  188. {
  189. if (request == null) return "";
  190. var ip = request.Headers["X-Real-IP"].FirstOrDefault();
  191. if (ip.IsNull())
  192. {
  193. ip = request.Headers["X-Forwarded-For"].FirstOrDefault();
  194. }
  195. if (ip.IsNull())
  196. {
  197. ip = request.HttpContext?.Connection?.RemoteIpAddress?.ToString();
  198. }
  199. if (ip.IsNull() || !IsIP(ip))
  200. {
  201. ip = "127.0.0.1";
  202. }
  203. return ip;
  204. }
  205. #endregion
  206. #region 随机数
  207. /// <summary>
  208. /// 根据自定义随机包含的字符获取指定长度的随机字符
  209. /// </summary>
  210. /// <param name="length">随机字符长度</param>
  211. /// <returns>随机字符</returns>
  212. public static string GetRandomStr(int length)
  213. {
  214. string a = "ABCDEFGHJKLMNPQRSTUVWXYZ012356789";
  215. StringBuilder sb = new StringBuilder();
  216. for (int i = 0; i < length; i++)
  217. {
  218. sb.Append(a[new Random(Guid.NewGuid().GetHashCode()).Next(0, a.Length - 1)]);
  219. }
  220. return sb.ToString();
  221. }
  222. /// <summary>
  223. /// 根据自定义随机包含的字符获取指定长度的随机字符
  224. /// </summary>
  225. /// <param name="length">随机字符长度</param>
  226. /// <returns>随机字符</returns>
  227. public static string GetRandomAllStr(int length)
  228. {
  229. string a = "ABCDEFGHJKLMNPQRSTUVWXYZabcdefghjklmnpqrstuvwxyz012356789";
  230. StringBuilder sb = new StringBuilder();
  231. for (int i = 0; i < length; i++)
  232. {
  233. sb.Append(a[new Random(Guid.NewGuid().GetHashCode()).Next(0, a.Length - 1)]);
  234. }
  235. return sb.ToString();
  236. }
  237. /// <summary>
  238. /// 根据自定义随机包含的字符获取指定长度的随机字母(含大小写)
  239. /// </summary>
  240. /// <param name="length">随机字符长度</param>
  241. /// <returns>随机字符</returns>
  242. public static string GetRandomLetter(int length)
  243. {
  244. string a = "ABCDEFGHJKLMNPQRSTUVWXYZabcdefghjklmnpqrstuvwxyz";
  245. StringBuilder sb = new StringBuilder();
  246. for (int i = 0; i < length; i++)
  247. {
  248. sb.Append(a[new Random(Guid.NewGuid().GetHashCode()).Next(0, a.Length - 1)]);
  249. }
  250. return sb.ToString();
  251. }
  252. /// <summary>
  253. /// 生成不重复随机数字
  254. /// 操作者:037
  255. /// 2021-07-26 15:39
  256. /// </summary>
  257. /// <param name="CodeCount">输入字符串长度</param>
  258. /// <returns>字符串</returns>
  259. public static string GetRandomNumber(int len)
  260. {
  261. string allChar = "0,1,2,3,4,5,6,7,8,9";
  262. string[] allCharArray = allChar.Split(',');
  263. string RandomCode = "";
  264. int temp = -1;
  265. Random rand = new Random();
  266. for (int i = 0; i < len; i++)
  267. {
  268. if (temp != -1)
  269. {
  270. rand = new Random(temp * i * ((int)DateTime.Now.Ticks));
  271. }
  272. int t = rand.Next(allCharArray.Length - 1);
  273. while (temp == t)
  274. {
  275. t = rand.Next(allCharArray.Length - 1);
  276. }
  277. temp = t;
  278. RandomCode += allCharArray[t];
  279. }
  280. return RandomCode;
  281. }
  282. #endregion
  283. #region decimal 截取
  284. /// <summary>
  285. ///
  286. /// </summary>
  287. /// <param name="d"></param>
  288. /// <param name="n"></param>
  289. /// <returns></returns>
  290. public static decimal CutDecimalWithN(decimal d, int n)
  291. {
  292. string strDecimal = d.ToString();
  293. int index = strDecimal.IndexOf(".");
  294. if (index == -1 || strDecimal.Length < index + n + 1)
  295. {
  296. strDecimal = string.Format("{0:F" + n + "}", d);
  297. }
  298. else
  299. {
  300. int length = index;
  301. if (n != 0)
  302. {
  303. length = index + n + 1;
  304. }
  305. strDecimal = strDecimal.Substring(0, length);
  306. }
  307. return Decimal.Parse(strDecimal);
  308. }
  309. /// <summary>
  310. /// decimal 保留小数,不四舍五入
  311. /// </summary>
  312. /// <param name="value"></param>
  313. /// <param name="decimalPlaces"></param>
  314. /// <returns></returns>
  315. public static decimal TruncDecimals(this decimal value, int decimalPlaces)
  316. {
  317. decimal scaleFactor = (decimal)Math.Pow(10, decimalPlaces);
  318. var truncated = Math.Truncate(scaleFactor * value) / scaleFactor;
  319. // 检查是否需要补零
  320. if (GetDecimalDigits(value) < decimalPlaces)
  321. {
  322. string format = "0." + new string('0', decimalPlaces);
  323. truncated = decimal.Parse(truncated.ToString(format));
  324. }
  325. return truncated;
  326. }
  327. private static int GetDecimalDigits(decimal number)
  328. {
  329. string[] parts = number.ToString().Split('.');
  330. return parts.Length > 1 ? parts[1].Length : 0;
  331. }
  332. #endregion
  333. #region decimal 保留两位小数
  334. /// <summary>
  335. /// decimal 保留两位小数 不四舍五入
  336. /// </summary>
  337. /// <param name="number"></param>
  338. /// <returns></returns>
  339. public static decimal DecimalsKeepTwo(this decimal myDecimal)
  340. {
  341. var subDecimal = Math.Floor(myDecimal * 100) / 100;//保留两位小数,直接截取
  342. return subDecimal;
  343. }
  344. #endregion
  345. #region 团组模块 - 汇率相关存储解析
  346. /// <summary>
  347. /// 团组模块 - 汇率相关 To List
  348. /// </summary>
  349. /// <param name="rateStr"></param>
  350. /// <returns></returns>
  351. public static List<CurrencyInfo> GetCurrencyChinaToList(string? rateStr)
  352. {
  353. List<CurrencyInfo> currencyInfos = new List<CurrencyInfo>();
  354. if (string.IsNullOrEmpty(rateStr)) return currencyInfos;
  355. if (rateStr.Contains("|"))
  356. {
  357. string[] currencyArr = rateStr.Split("|");
  358. foreach (string currency in currencyArr)
  359. {
  360. string[] currency1 = currency.Split(":");
  361. string[] currency2 = currency1[0].Split("(");
  362. CurrencyInfo rateInfo = new CurrencyInfo()
  363. {
  364. CurrencyCode = currency2[1].Replace(")", "").TrimEnd(),
  365. CurrencyName = currency2[0],
  366. Rate = decimal.Parse(currency1[1]),
  367. };
  368. currencyInfos.Add(rateInfo);
  369. }
  370. }
  371. return currencyInfos;
  372. }
  373. /// <summary>
  374. /// 团组模块 - 汇率相关存储解析 To String
  375. /// </summary>
  376. /// <param name="rates"></param>
  377. /// <returns></returns>
  378. public static string GetCurrencyChinaToString(List<CurrencyInfo>? rates)
  379. {
  380. string rateStr = string.Empty;
  381. if (rates == null) return rateStr;
  382. if (rates.Count <= 0) return rateStr;
  383. if (rates.Count == 1)
  384. {
  385. var rate = rates[0];
  386. return string.Format("{0}({1}):{2}|", rate.CurrencyName, rate.CurrencyCode, rate.Rate);
  387. }
  388. foreach (CurrencyInfo rate in rates)
  389. {
  390. //存储方式: 美元(USD):6.2350|.......|墨西哥比索(MXN):1.0000
  391. rateStr += string.Format("{0}({1}):{2}|", rate.CurrencyName, rate.CurrencyCode, rate.Rate);
  392. }
  393. if (rateStr.Length > 0)
  394. {
  395. rateStr = rateStr.Substring(0, rateStr.Length - 1);
  396. }
  397. return rateStr;
  398. }
  399. #endregion
  400. #region 验证身份证号码
  401. /// <summary>
  402. /// 正则表达式
  403. /// 验证身份证号码
  404. /// </summary>
  405. /// <param name="idNumber"></param>
  406. /// <returns></returns>
  407. public static bool IsValidChineseId(this string idNumber)
  408. {
  409. 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])$";
  410. Regex regex = new Regex(pattern);
  411. return regex.IsMatch(idNumber);
  412. }
  413. /// <summary>
  414. /// 通过身份证提取生日
  415. /// </summary>
  416. /// <param name="identityCard"></param>
  417. /// <returns></returns>
  418. public static DateTime? GetBirthDateFromIdentityCard(string identityCard)
  419. {
  420. // 身份证号码正则表达式验证,支持18位身份证号码
  421. if (!Regex.IsMatch(identityCard, @"^\d{17}(\d|X|x)$"))
  422. {
  423. return null;
  424. }
  425. // 获取出生日期(8位数字)
  426. string birthDateString = identityCard.Substring(6, 8);
  427. // 尝试将出生日期字符串转换为DateTime类型
  428. if (DateTime.TryParse(birthDateString, out DateTime birthDate))
  429. {
  430. return birthDate;
  431. }
  432. return null;
  433. }
  434. /// <summary>
  435. /// 通过身份证判断性别
  436. /// </summary>
  437. /// <param name="idNumber"></param>
  438. /// <returns>0 男1 女 -1 未设置</returns>
  439. /// <exception cref="ArgumentException"></exception>
  440. public static int GetGenderFromIdentityCard(string idNumber)
  441. {
  442. if (string.IsNullOrEmpty(idNumber) || idNumber.Length != 18)
  443. return -1;
  444. char genderChar = idNumber[16];
  445. return int.Parse(genderChar.ToString()) % 2 == 0 ? 1 : 0;
  446. }
  447. #endregion
  448. #region string格式日期格式化
  449. /// <summary>
  450. /// string格式日期格式化
  451. /// </summary>
  452. /// <param name="dateStr"></param>
  453. /// <param name="format">
  454. /// 格式化标准
  455. /// yyyy-MM-dd yyyy/MM/dd
  456. /// </param>
  457. /// <returns></returns>
  458. public static string DateFormat(this string dateStr, string format)
  459. {
  460. if (!string.IsNullOrEmpty(dateStr))
  461. {
  462. if (!string.IsNullOrEmpty(format))
  463. {
  464. DateTime result;
  465. if (DateTime.TryParse(dateStr, out result))
  466. {
  467. return result.ToString(format);
  468. }
  469. }
  470. }
  471. return "";
  472. }
  473. #endregion
  474. /// <summary>
  475. /// List to DataTable
  476. /// 集合转换成datatable
  477. /// </summary>
  478. /// <typeparam name="T"></typeparam>
  479. /// <param name="aIList"></param>
  480. /// <returns></returns>
  481. public static DataTable GetDataTableFromIList<T>(List<T> aIList)
  482. {
  483. DataTable _returnTable = new DataTable();
  484. if (aIList != null && aIList.Count > 0)
  485. {
  486. object _baseObj = aIList[0];
  487. Type objectType = _baseObj.GetType();
  488. PropertyInfo[] properties = objectType.GetProperties();
  489. DataColumn _col;
  490. foreach (PropertyInfo property in properties)
  491. {
  492. _col = new DataColumn();
  493. _col.ColumnName = (string)property.Name;
  494. _col.DataType = property.PropertyType;
  495. _returnTable.Columns.Add(_col);
  496. }
  497. //Adds the rows to the table
  498. DataRow _row;
  499. foreach (object objItem in aIList)
  500. {
  501. _row = _returnTable.NewRow();
  502. foreach (PropertyInfo property in properties)
  503. {
  504. _row[property.Name] = property.GetValue(objItem, null);
  505. }
  506. _returnTable.Rows.Add(_row);
  507. }
  508. }
  509. return _returnTable;
  510. }
  511. /// <summary>
  512. /// List to DataTable
  513. /// 集合转换成datatable
  514. /// </summary>
  515. public static DataTable ToDataTableArray(IList list)
  516. {
  517. DataTable result = new DataTable();
  518. if (list.Count > 0)
  519. {
  520. PropertyInfo[] propertys = list[0].GetType().GetProperties();
  521. foreach (PropertyInfo pi in propertys)
  522. {
  523. try
  524. {
  525. result.Columns.Add(pi.Name, pi.PropertyType);
  526. }
  527. catch (Exception ex)
  528. {
  529. Console.WriteLine($"{pi.Name}:{ex.Message}");
  530. }
  531. }
  532. for (int i = 0; i < list.Count; i++)
  533. {
  534. ArrayList tempList = new ArrayList();
  535. foreach (PropertyInfo pi in propertys)
  536. {
  537. object obj = pi.GetValue(list[i], null);
  538. tempList.Add(obj);
  539. }
  540. object?[] array = tempList.ToArray();
  541. result.LoadDataRow(array, true);
  542. }
  543. }
  544. return result;
  545. }
  546. /// <summary>
  547. ///获取指定月份起止日期
  548. /// </summary>
  549. /// <param name="year"></param>
  550. /// <param name="month"></param>
  551. /// <returns></returns>
  552. public static (DateTime StartDate, DateTime EndDate) GetMonthStartAndEndDates(int year, int month)
  553. {
  554. var calendar = new GregorianCalendar();
  555. var daysInMonth = calendar.GetDaysInMonth(year, month);
  556. var startDate = new DateTime(year, month, 1);
  557. var endDate = new DateTime(year, month, daysInMonth);
  558. return (startDate, endDate);
  559. }
  560. /// <summary>
  561. /// 验证json字符串是否合法
  562. /// </summary>
  563. /// <param name="jsonString"></param>
  564. /// <returns></returns>
  565. public static bool IsValidJson(string jsonString)
  566. {
  567. try
  568. {
  569. JToken.Parse(jsonString);
  570. return true;
  571. }
  572. catch (JsonReaderException)
  573. {
  574. return false;
  575. }
  576. }
  577. /// <summary>
  578. /// 常见的双字姓氏列表
  579. /// </summary>
  580. private static readonly HashSet<string> commonDoubleSurnames = new HashSet<string>
  581. {
  582. "欧阳", "司马", "上官", "夏侯", "诸葛", "东方", "赫连", "皇甫", "尉迟", "公羊",
  583. "澹台", "公冶", "宗政", "濮阳", "淳于", "单于", "太叔", "申屠", "公孙", "仲孙",
  584. "轩辕", "令狐", "钟离", "宇文", "长孙", "慕容", "鲜于", "闾丘", "司徒", "司空",
  585. "亓官", "司寇", "仉督", "子车", "颛孙", "端木", "巫马", "公西", "漆雕", "乐正",
  586. "壤驷", "公良", "拓跋", "夹谷", "宰父", "谷梁", "晋楚", "闫法", "汝鄢", "涂钦",
  587. "段干", "百里", "东郭", "南门", "呼延", "归海", "羊舌", "微生", "岳帅", "缑亢",
  588. "况后", "有琴", "梁丘", "左丘", "东门", "西门", "商牟", "佘佴", "伯赏", "南宫"
  589. };
  590. /// <summary>
  591. /// 中文名字取姓氏和名字
  592. /// </summary>
  593. /// <param name="fullName"></param>
  594. /// <returns>姓:lastName 名:firstName</returns>
  595. public static (string lastName, string firstName) GetLastNameAndFirstName(string fullName)
  596. {
  597. if (string.IsNullOrEmpty(fullName) || fullName.Length < 2) return ("", "");
  598. // 尝试匹配双字姓氏
  599. if (fullName.Length > 2)
  600. {
  601. string potentialDoubleSurname = fullName.Substring(0, 2);
  602. if (commonDoubleSurnames.Contains(potentialDoubleSurname))
  603. {
  604. string lastName = potentialDoubleSurname;
  605. string firstName = fullName.Substring(2);
  606. return (lastName, firstName);
  607. }
  608. }
  609. // 默认单字姓氏
  610. string singleSurname = fullName.Substring(0, 1);
  611. string remainingName = fullName.Substring(1);
  612. return (singleSurname, remainingName);
  613. }
  614. /// <summary>
  615. /// 中文名字转拼音
  616. /// </summary>
  617. /// <param name="chineseName"></param>
  618. /// <returns></returns>
  619. /// <exception cref="ArgumentException"></exception>
  620. public static string ConvertToPinyin(string chineseName)
  621. {
  622. if (string.IsNullOrEmpty(chineseName)) return "";
  623. return PinyinHelper.GetPinyin(chineseName, "").ToUpper();
  624. }
  625. #region stringBuilder 扩展
  626. /// <summary>
  627. /// StringBuilder 验证是否有值
  628. /// </summary>
  629. /// <param name="sb"></param>
  630. /// <returns></returns>
  631. public static bool HasValue(this StringBuilder sb)
  632. {
  633. return sb != null && sb.Length > 0;
  634. }
  635. #endregion
  636. }