ProbeAppHepler.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using Newtonsoft.Json;
  6. using Flyinsono.DBCopy.Tool.Log;
  7. using System.Text.RegularExpressions;
  8. using System.IO;
  9. namespace Flyinsono.DBCopy.Tool.Utilities
  10. {
  11. public class ProbeAppHepler
  12. {
  13. public List<ApplicationCategory> ApplicationCategoryList { get; set; }
  14. public List<LocaleItem> ChineseList { get; set; }
  15. public List<LocaleItem> EnglishList { get; set; }
  16. public ProbeAppHepler()
  17. {
  18. var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Config", "Language", "chinese.json");
  19. ChineseList = LoadJson(path);
  20. var path1 = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Config", "Language", "english.json");
  21. EnglishList = LoadJson(path1);
  22. LoadFile();
  23. }
  24. public UsProbeAppSetting LoadFile()
  25. {
  26. var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Config", "UsProbeAppSetting.json");
  27. var usProbeAppSettingStr = File.ReadAllText(path);
  28. var usProbeAppSetting = JsonConvert.DeserializeObject<UsProbeAppSetting>(usProbeAppSettingStr);
  29. ApplicationCategoryList = new List<ApplicationCategory>();
  30. foreach (var item in usProbeAppSetting.ApplicationCategoryInfos)
  31. {
  32. ApplicationCategoryList.AddRange(item.Applications);
  33. }
  34. return usProbeAppSetting;
  35. }
  36. public List<LocaleItem> LoadJson(string path)
  37. {
  38. var jsonString = File.ReadAllText(path);
  39. var cc = JsonConvert.DeserializeObject<ArrayOfLocaleData>(jsonString);
  40. return cc.ArrayOfLocaleItem.LocaleItem;
  41. }
  42. static bool ContainsChinese(string input)
  43. {
  44. return Regex.IsMatch(input, @"\p{IsCJKUnifiedIdeographs}");
  45. }
  46. public Tuple<string, string> GetApplication(string applicationStr,string terminalDataId)
  47. {
  48. var category = "";
  49. var application = applicationStr;
  50. try
  51. {
  52. var array = applicationStr.Split('-');
  53. if (array.Length == 2)
  54. {
  55. category = array[0];
  56. application = array[1];
  57. var categorykeyList = new List<string>();
  58. if (ContainsChinese(application))//调用中文表格翻译成英文
  59. {
  60. var key = ChineseList.FirstOrDefault(v => v.value == application);
  61. if (key != null)
  62. {
  63. application = key.key;
  64. }
  65. }
  66. if (ContainsChinese(category))
  67. {
  68. categorykeyList = ChineseList.Where(v => v.value == category).Select(c => c.key).ToList();
  69. }
  70. else
  71. {
  72. categorykeyList = EnglishList.Where(v => v.value == category).Select(c => c.key).ToList();
  73. }
  74. var applicationCategorys = ApplicationCategoryList.Where(v => v.name == application || v.id == application).ToList();
  75. if(applicationCategorys.Count<=0)
  76. {
  77. if (categorykeyList.Count > 0)
  78. {
  79. category = categorykeyList.FirstOrDefault();
  80. }
  81. }
  82. else
  83. {
  84. var applicationCategory = applicationCategorys.FirstOrDefault(v => categorykeyList.Contains(v.category));
  85. if (applicationCategory != null)
  86. {
  87. category = applicationCategory.category;
  88. application = applicationCategory.name;
  89. }
  90. }
  91. }
  92. else
  93. {
  94. if (applicationStr.Contains("From agent"))//魔盒 FromSonopost
  95. {
  96. category = "FromSonopost";
  97. application="";
  98. }
  99. if (array.Length > 2)
  100. {
  101. category = array[0];
  102. var categorykeyList = new List<string>();
  103. if (ContainsChinese(category))
  104. {
  105. categorykeyList = ChineseList.Where(v => v.value == category).Select(c => c.key).ToList();
  106. }
  107. else
  108. {
  109. categorykeyList = EnglishList.Where(v => v.value == category).Select(c => c.key).ToList();
  110. }
  111. var applicationCategorys = ApplicationCategoryList.Where(v => v.name == application || v.id == application).ToList();
  112. if (applicationCategorys.Count <= 0)
  113. {
  114. if(categorykeyList.Count>0)
  115. {
  116. category = categorykeyList.FirstOrDefault();
  117. }
  118. }
  119. int firstDashIndex = applicationStr.IndexOf("-");
  120. string firstPart = applicationStr.Substring(0, firstDashIndex);
  121. string secondPart = applicationStr.Substring(firstDashIndex + 1);
  122. application = secondPart;
  123. }
  124. }
  125. }
  126. catch (System.Exception ex)
  127. {
  128. Logger.WriteLineError($"GetApplication applicationStr:{applicationStr} ex:{ex}");
  129. }
  130. // Logger.WriteLineInfo($"GetApplication terminalDataId:{terminalDataId} applicationStr:{applicationStr} new:{category} {application}");
  131. return new Tuple<string, string>(category, application);
  132. }
  133. }
  134. public class UsProbeAppSetting
  135. {
  136. public List<ApplicationCategoryInfo> ApplicationCategoryInfos { get; set; }
  137. }
  138. public class ApplicationCategoryInfo
  139. {
  140. public string id { get; set; }
  141. public string name { get; set; }
  142. public string default1 { get; set; }
  143. public List<ApplicationCategory> Applications { get; set; }
  144. }
  145. public class ApplicationCategory
  146. {
  147. public string category { get; set; }
  148. public string id { get; set; }
  149. public string name { get; set; }
  150. }
  151. public class ArrayOfLocaleData
  152. {
  153. public ArrayOfLocaleItem ArrayOfLocaleItem { get; set; }
  154. }
  155. public class ArrayOfLocaleItem
  156. {
  157. public List<LocaleItem> LocaleItem { get; set; }
  158. }
  159. public class LocaleItem
  160. {
  161. public string key { get; set; }
  162. public string value { get; set; }
  163. }
  164. }