123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
- using Newtonsoft.Json;
- using Flyinsono.DBCopy.Tool.Log;
- using System.Text.RegularExpressions;
- using System.IO;
- namespace Flyinsono.DBCopy.Tool.Utilities
- {
- public class ProbeAppHepler
- {
- public List<ApplicationCategory> ApplicationCategoryList { get; set; }
- public List<LocaleItem> ChineseList { get; set; }
- public List<LocaleItem> EnglishList { get; set; }
- public ProbeAppHepler()
- {
- var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Config", "Language", "chinese.json");
- ChineseList = LoadJson(path);
- var path1 = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Config", "Language", "english.json");
- EnglishList = LoadJson(path1);
- LoadFile();
- }
- public UsProbeAppSetting LoadFile()
- {
- var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Config", "UsProbeAppSetting.json");
- var usProbeAppSettingStr = File.ReadAllText(path);
- var usProbeAppSetting = JsonConvert.DeserializeObject<UsProbeAppSetting>(usProbeAppSettingStr);
- ApplicationCategoryList = new List<ApplicationCategory>();
- foreach (var item in usProbeAppSetting.ApplicationCategoryInfos)
- {
- ApplicationCategoryList.AddRange(item.Applications);
- }
- return usProbeAppSetting;
- }
- public List<LocaleItem> LoadJson(string path)
- {
- var jsonString = File.ReadAllText(path);
- var cc = JsonConvert.DeserializeObject<ArrayOfLocaleData>(jsonString);
- return cc.ArrayOfLocaleItem.LocaleItem;
- }
- static bool ContainsChinese(string input)
- {
- return Regex.IsMatch(input, @"\p{IsCJKUnifiedIdeographs}");
- }
- public Tuple<string, string> GetApplication(string applicationStr,string terminalDataId)
- {
- var category = "";
- var application = applicationStr;
- try
- {
- var array = applicationStr.Split('-');
- if (array.Length == 2)
- {
- category = array[0];
- application = array[1];
- var categorykeyList = new List<string>();
- if (ContainsChinese(application))//调用中文表格翻译成英文
- {
- var key = ChineseList.FirstOrDefault(v => v.value == application);
- if (key != null)
- {
- application = key.key;
- }
- }
- if (ContainsChinese(category))
- {
- categorykeyList = ChineseList.Where(v => v.value == category).Select(c => c.key).ToList();
- }
- else
- {
- categorykeyList = EnglishList.Where(v => v.value == category).Select(c => c.key).ToList();
- }
- var applicationCategorys = ApplicationCategoryList.Where(v => v.name == application || v.id == application).ToList();
- if(applicationCategorys.Count<=0)
- {
- if (categorykeyList.Count > 0)
- {
- category = categorykeyList.FirstOrDefault();
- }
- }
- else
- {
- var applicationCategory = applicationCategorys.FirstOrDefault(v => categorykeyList.Contains(v.category));
- if (applicationCategory != null)
- {
- category = applicationCategory.category;
- application = applicationCategory.name;
- }
- }
- }
- else
- {
- if (applicationStr.Contains("From agent"))//魔盒 FromSonopost
- {
- category = "FromSonopost";
- application="";
- }
- if (array.Length > 2)
- {
- category = array[0];
- var categorykeyList = new List<string>();
- if (ContainsChinese(category))
- {
- categorykeyList = ChineseList.Where(v => v.value == category).Select(c => c.key).ToList();
- }
- else
- {
- categorykeyList = EnglishList.Where(v => v.value == category).Select(c => c.key).ToList();
- }
- var applicationCategorys = ApplicationCategoryList.Where(v => v.name == application || v.id == application).ToList();
- if (applicationCategorys.Count <= 0)
- {
- if(categorykeyList.Count>0)
- {
- category = categorykeyList.FirstOrDefault();
- }
-
- }
- int firstDashIndex = applicationStr.IndexOf("-");
- string firstPart = applicationStr.Substring(0, firstDashIndex);
- string secondPart = applicationStr.Substring(firstDashIndex + 1);
- application = secondPart;
- }
- }
- }
- catch (System.Exception ex)
- {
- Logger.WriteLineError($"GetApplication applicationStr:{applicationStr} ex:{ex}");
- }
- // Logger.WriteLineInfo($"GetApplication terminalDataId:{terminalDataId} applicationStr:{applicationStr} new:{category} {application}");
- return new Tuple<string, string>(category, application);
- }
- }
- public class UsProbeAppSetting
- {
- public List<ApplicationCategoryInfo> ApplicationCategoryInfos { get; set; }
- }
- public class ApplicationCategoryInfo
- {
- public string id { get; set; }
- public string name { get; set; }
- public string default1 { get; set; }
- public List<ApplicationCategory> Applications { get; set; }
- }
- public class ApplicationCategory
- {
- public string category { get; set; }
- public string id { get; set; }
- public string name { get; set; }
- }
- public class ArrayOfLocaleData
- {
- public ArrayOfLocaleItem ArrayOfLocaleItem { get; set; }
- }
- public class ArrayOfLocaleItem
- {
- public List<LocaleItem> LocaleItem { get; set; }
- }
- public class LocaleItem
- {
- public string key { get; set; }
- public string value { get; set; }
- }
- }
|