diff --git a/src/Shentun.Utilities/LanguageConverter.cs b/src/Shentun.Utilities/LanguageConverter.cs index 054470c..26f5604 100644 --- a/src/Shentun.Utilities/LanguageConverter.cs +++ b/src/Shentun.Utilities/LanguageConverter.cs @@ -7,83 +7,141 @@ namespace Shentun.Utilities { public class LanguageConverter { - public static string GetPYSimpleCode(string data, int len = 50) - { + //public static string GetPYSimpleCode(string data, int len = 50) + //{ - string rtnValue = ""; + // string rtnValue = ""; - if (string.IsNullOrWhiteSpace(data)) - { - return ""; - } + // if (string.IsNullOrWhiteSpace(data)) + // { + // return ""; + // } - foreach (char obj in data) - { - try - { - ChineseChar chineseChar = new ChineseChar(obj); - string t = chineseChar.Pinyins[0].ToString(); - rtnValue += t.Substring(0, 1); - } - catch - { - rtnValue += obj.ToString(); - } - } - if (len > rtnValue.Length) - { - len = rtnValue.Length; - } - rtnValue = rtnValue.Substring(0, len).ToUpper(); - return rtnValue; - } + // foreach (char obj in data) + // { + // try + // { + // ChineseChar chineseChar = new ChineseChar(obj); + // string t = chineseChar.Pinyins[0].ToString(); + // rtnValue += t.Substring(0, 1); + // } + // catch + // { + // rtnValue += obj.ToString(); + // } + // } + // if (len > rtnValue.Length) + // { + // len = rtnValue.Length; + // } + // rtnValue = rtnValue.Substring(0, len).ToUpper(); + // return rtnValue; + //} - public static string GetPYCode(string data, int len = 50) - { + //public static string GetPYCode(string data, int len = 50) + //{ - string rtnValue = ""; + // string rtnValue = ""; - if (string.IsNullOrWhiteSpace(data)) - { - return ""; - } + // if (string.IsNullOrWhiteSpace(data)) + // { + // return ""; + // } - foreach (char obj in data) + // foreach (char obj in data) + // { + // try + // { + // ChineseChar chineseChar = new ChineseChar(obj); + // string t = chineseChar.Pinyins[0].TrimEnd('1', '2', '3', '4', '5').ToUpper(); + // rtnValue += t; + // } + // catch + // { + // rtnValue += obj.ToString(); + // } + // } + // if (len > rtnValue.Length) + // { + // len = rtnValue.Length; + // } + // rtnValue = rtnValue.Substring(0, len).ToUpper(); + // return rtnValue; + //} + + //public static string GetPYCodeWithSpace(string data) + //{ + // if(string.IsNullOrWhiteSpace(data)) + // { + // return ""; + // } + // string rtnValue = ""; + // for (int i = 0; i < data.Length; i++) + // { + // rtnValue += " " + LanguageConverter.GetPYCode(data.Substring(i, 1)); + // } + // return rtnValue.Trim(); + //} + + + #region 新版 + + + /// + /// 汉字转首字母 + /// + /// + /// + public static string GetPYSimpleCode(string strChinese) + { + + try { - try + if (strChinese.Length != 0) { - ChineseChar chineseChar = new ChineseChar(obj); - string t = chineseChar.Pinyins[0].TrimEnd('1', '2', '3', '4', '5').ToUpper(); - rtnValue += t; - } - catch - { - rtnValue += obj.ToString(); + StringBuilder fullSpell = new StringBuilder(); + for (int i = 0; i < strChinese.Length; i++) + { + var chr = strChinese[i]; + fullSpell.Append(GetSpell(chr)[0]); + } + + return fullSpell.ToString().ToUpper(); } } - if (len > rtnValue.Length) + catch (Exception e) { - len = rtnValue.Length; + + Console.WriteLine("首字母转化出错!" + e.Message); } - rtnValue = rtnValue.Substring(0, len).ToUpper(); - return rtnValue; + + return string.Empty; } - public static string GetPYCodeWithSpace(string data) + private static string GetSpell(char chr) { - if(string.IsNullOrWhiteSpace(data)) - { - return ""; - } - string rtnValue = ""; - for (int i = 0; i < data.Length; i++) + var coverchr = NPinyin.Pinyin.GetPinyin(chr); + + bool isChineses = ChineseChar.IsValidChar(coverchr[0]); + if (isChineses) { - rtnValue += " " + LanguageConverter.GetPYCode(data.Substring(i, 1)); + ChineseChar chineseChar = new ChineseChar(coverchr[0]); + foreach (string value in chineseChar.Pinyins) + { + if (!string.IsNullOrEmpty(value)) + { + return value.Remove(value.Length - 1, 1); + } + } } - return rtnValue.Trim(); + + return coverchr; + } + #endregion + } } diff --git a/src/Shentun.Utilities/Shentun.Utilities.csproj b/src/Shentun.Utilities/Shentun.Utilities.csproj index ae6f8ee..c71321c 100644 --- a/src/Shentun.Utilities/Shentun.Utilities.csproj +++ b/src/Shentun.Utilities/Shentun.Utilities.csproj @@ -13,6 +13,7 @@ + diff --git a/test/Shentun.Peis.Application.Tests/BaseDataHandleTest.cs b/test/Shentun.Peis.Application.Tests/BaseDataHandleTest.cs index e9f0241..ab5bc6d 100644 --- a/test/Shentun.Peis.Application.Tests/BaseDataHandleTest.cs +++ b/test/Shentun.Peis.Application.Tests/BaseDataHandleTest.cs @@ -10,6 +10,8 @@ using Volo.Abp.Uow; using Xunit.Abstractions; using Xunit; using Shentun.Peis.DataMigrations; +using Microsoft.International.Converters.PinYinConverter; +using Shentun.Utilities; namespace Shentun.Peis { @@ -21,11 +23,17 @@ namespace Shentun.Peis private readonly ITestOutputHelper _output; private readonly IUnitOfWorkManager _unitOfWorkManager; private readonly BaseDataHandleAppService _appService; + private readonly IRepository _asbitemRepository; + private readonly IRepository _itemRepository; + private readonly IRepository _diagnosisRepository; public BaseDataHandleTest(ITestOutputHelper testOutputHelper) { _output = testOutputHelper; _unitOfWorkManager = GetRequiredService(); _appService = GetRequiredService(); + _asbitemRepository = GetRequiredService>(); + _itemRepository = GetRequiredService>(); + _diagnosisRepository = GetRequiredService>(); } //[Fact] @@ -48,6 +56,23 @@ namespace Shentun.Peis // } // } // } + [Fact] + public async void GetPYSimpleCode() + { + using (var unitOfWork = _unitOfWorkManager.Begin(isTransactional: true)) + { + var diagnosisList = await _diagnosisRepository.GetListAsync(); + foreach (var diagnosis in diagnosisList) + { + diagnosis.SimpleCode = LanguageConverter.GetPYSimpleCode(diagnosis.DisplayName); + } + + await _diagnosisRepository.UpdateManyAsync(diagnosisList); + + await unitOfWork.CompleteAsync(); + } + } + @@ -74,7 +99,7 @@ namespace Shentun.Peis // } // } // } - + //} //[Fact] @@ -103,4 +128,5 @@ namespace Shentun.Peis //} } + }