SchoolService.cs 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009
  1. using Azure.Cosmos;
  2. using HTEXLib.COMM.Helpers;
  3. using Microsoft.AspNetCore.Hosting;
  4. using Microsoft.International.Converters.PinYinConverter;
  5. using Microsoft.International.Converters.TraditionalChineseToSimplifiedConverter;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Text.Json;
  12. using System.Text.RegularExpressions;
  13. using System.Threading.Tasks;
  14. using TEAMModelOS.Models;
  15. using TEAMModelOS.SDK.DI;
  16. using TEAMModelOS.SDK.Extension;
  17. using TEAMModelOS.SDK.Models;
  18. namespace TEAMModelOS.SDK
  19. {
  20. public class SchoolService
  21. {
  22. /// <summary>
  23. /// 根据年份获取年级
  24. /// </summary>
  25. /// <param name="semesterList"></param>
  26. /// <returns></returns>
  27. public static List<KeyValuePair<int, string>> GetGrades(School school,string periodId ,List<int> years,long time =0) {
  28. var date = DateTimeOffset.UtcNow;
  29. //2001-09-09 09:46:40
  30. if (time > 1000000000000) {
  31. date = DateTimeOffset.FromUnixTimeMilliseconds(time);
  32. }
  33. //年级算法
  34. var period = school.period.Find(x => x.id.Equals(periodId));
  35. int? Count = period?.grades?.Count;
  36. List<KeyValuePair<int, string>> yearGrades = new List<KeyValuePair<int, string>>();
  37. if (Count.HasValue)
  38. {
  39. int Day = date.Day;
  40. int Month = date.Month;
  41. int Year = date.Year;
  42. int start = int.Parse($"{Year}0901");
  43. var se = period.semesters.Find(x => x.start == 1);
  44. if (se == null)
  45. {
  46. se = period.semesters.First();
  47. }
  48. string sm = "09";
  49. string sd = "01";
  50. if (se != null)
  51. {
  52. sm = se.month >= 10 ? $"{se.month}" : $"0{se.month}";
  53. sd = se.day >= 10 ? $"{se.day}" : $"0{se.day}";
  54. start = int.Parse($"{Year}{sm}{sd}");
  55. }
  56. int curr = int.Parse(date.ToString("yyyyMMdd"));
  57. //新学年开学时间大于当前时间,计算年级需要减1 20220901-20220408 > 0 则当前20220408是2021年入学的,
  58. //当前时间大于新学年开学时间,计算年级则不需要 20220901-20221203 < 1 则当前20221203是2022年入学的,
  59. //20230901-20230101 > 0 则当前20230101是2022年入学的,
  60. int dis = start - curr;
  61. foreach (int year in years)
  62. {
  63. if (int.Parse($"{year}{sm}{sd}") < curr) {
  64. int grade;
  65. if (dis > 0)
  66. {
  67. grade = Math.Abs((Year - year - 1)) % Count.Value;
  68. }
  69. else
  70. {
  71. grade = Math.Abs((Year - year)) % Count.Value;
  72. }
  73. yearGrades.Add(new KeyValuePair<int, string>(year, $"{grade}"));
  74. }
  75. }
  76. }
  77. return yearGrades;
  78. }
  79. /// <summary>
  80. /// 处理学期排序
  81. /// </summary>
  82. /// <param name="semesterList"></param>
  83. /// <returns></returns>
  84. public static List<Semester> SortSemester(List<Semester> semesterList)
  85. {
  86. int Year = DateTimeOffset.UtcNow.Year;
  87. List<KeyValuePair<int, Semester>> pairs = new List<KeyValuePair<int, Semester>>();
  88. semesterList.ForEach(se => {
  89. string sm = se.month >= 10 ? $"{se.month}" : $"0{se.month}";
  90. string sd = se.day >= 10 ? $"{se.day}" : $"0{se.day}";
  91. int order = int.Parse($"{Year}{sm}{sd}");
  92. pairs.Add(new KeyValuePair<int, Semester>(order, se));
  93. });
  94. var orderPairs = pairs.OrderBy(z => z.Key);
  95. semesterList = orderPairs.Select(z => z.Value).ToList();
  96. int startIndex = semesterList.FindIndex(z => z.start == 1);
  97. if (startIndex == -1)
  98. {
  99. //未设置学期的情况默认9月份开始的。
  100. startIndex = semesterList.FindIndex(z => z.month == 9);
  101. //如果还未找到,就以排序结果为准
  102. if (startIndex == -1)
  103. {
  104. startIndex = 0;
  105. }
  106. }
  107. if (startIndex > 0)
  108. {
  109. List<Semester> before = semesterList.Take(startIndex).ToList();
  110. List<Semester> after = semesterList.Skip(startIndex).ToList();
  111. semesterList = new List<Semester>();
  112. semesterList.AddRange(after);
  113. semesterList.AddRange(before);
  114. return semesterList;
  115. }
  116. else
  117. {
  118. return semesterList;
  119. }
  120. }
  121. public static async Task<(List<Class> school_classes, List<Class> graduate_classes)> DoGraduateClasses(HttpTrigger _httpTrigger, AzureCosmosFactory _azureCosmos, List<string> periodIds, School school_base, Option _option,DingDing _dingDing, int waite = 0) {
  122. List<Class> school_classes = new List<Class>();
  123. List<Class> graduate_classes = new List<Class>();
  124. try
  125. {
  126. var client = _azureCosmos.GetCosmosClient();
  127. //取得班级
  128. int nowYear = DateTimeOffset.UtcNow.Year;
  129. int nowMonth = DateTimeOffset.UtcNow.Month;
  130. int nowDay = DateTimeOffset.UtcNow.Day;
  131. //因为修改年级,而导致取消的
  132. List<Class> cancel_graduate_classes = new List<Class>();
  133. string sql = $"SELECT value c FROM c where (c.graduate = 0 or IS_DEFINED(c.graduate) = false) ";
  134. if (periodIds.IsNotEmpty())
  135. {
  136. sql = $"SELECT value c FROM c where c.periodId in ({string.Join(",", periodIds.Select(x => $"'{x}'"))}) ";
  137. }
  138. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<Class>
  139. //(queryText: $"SELECT value c FROM c where (c.graduate = 0 or IS_DEFINED(c.graduate) = false)",
  140. (queryText: sql,
  141. requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Class-{school_base.id}") }))
  142. {
  143. bool isgraduate = false;
  144. if (!string.IsNullOrWhiteSpace(item.periodId))
  145. {
  146. var period = school_base.period.Find(x => x.id.Equals(item.periodId));
  147. if (period != null)
  148. {
  149. var gradeCount = period.grades.Count();
  150. //2022-2016=6(待判断月份日期是否是毕业) 2022-2017=5(未毕业)
  151. if (nowYear - item.year > gradeCount)
  152. {
  153. isgraduate = true;
  154. }
  155. else if (nowYear - item.year == gradeCount)
  156. {
  157. var semester = period.semesters.Find(x => x.start == 1);
  158. if (semester != null)
  159. {
  160. if (nowMonth > semester.month)
  161. {
  162. isgraduate = true;
  163. }
  164. else if (nowMonth == semester.month)
  165. {
  166. if (nowDay >= semester.day)
  167. {
  168. isgraduate = true;
  169. }
  170. else
  171. {
  172. }
  173. }
  174. else { isgraduate = false; }
  175. }
  176. }
  177. else
  178. {
  179. isgraduate = false;
  180. }
  181. }
  182. }
  183. if (isgraduate)
  184. {
  185. graduate_classes.Add(item);
  186. }
  187. else
  188. {
  189. if (item.graduate == 1)
  190. {
  191. item.graduate = 0;
  192. cancel_graduate_classes.Add(item);
  193. }
  194. school_classes.Add(item);
  195. }
  196. }
  197. if (graduate_classes.Any() || cancel_graduate_classes.Any())
  198. {
  199. if (waite == 0)
  200. {
  201. _ = _httpTrigger.RequestHttpTrigger(new { graduate_classes = graduate_classes, cancel_graduate_classes = cancel_graduate_classes, schoolId = $"{school_base.id}" }, _option.Location, "graduate-change");
  202. }
  203. else
  204. {
  205. await _httpTrigger.RequestHttpTrigger(new { graduate_classes = graduate_classes, cancel_graduate_classes = cancel_graduate_classes, schoolId = $"{school_base.id}" }, _option.Location, "graduate-change");
  206. }
  207. }
  208. } catch (Exception ex) {
  209. await _dingDing.SendBotMsg($"{_option.Location},{ex.Message}\n{ex.StackTrace}", GroupNames.醍摩豆服務運維群組);
  210. }
  211. return (school_classes,graduate_classes);
  212. }
  213. public static async Task<object> DoScsApiSchool(HttpTrigger _httpTrigger,Dictionary<string,object> dict, Option _option, List<IdNameCode> ignore,
  214. string areaId,string city,string dist, AzureStorageFactory _azureStorage ,DingDing _dingDing, IWebHostEnvironment _environment
  215. )
  216. {
  217. List<ScSchool> tbschools = null;
  218. List<ScSchool> matchSchools = null;
  219. List<ScSchool> schools = null;
  220. List<ScSchool> saveschools = null;
  221. var table = _azureStorage.GetCloudTableClient().GetTableReference("ScYxpt");
  222. // 5.3.1.18根据机构ID、项目ID、子项目ID返回学校列表
  223. ( int status, string json) = await _httpTrigger.RequestHttpTrigger(dict, _option.Location, "GetSchoolList");
  224. if (status == 200)
  225. {
  226. schools = json.ToObject<List<ScSchool>>(new JsonSerializerOptions { PropertyNameCaseInsensitive = false });
  227. if (ignore.IsNotEmpty())
  228. {
  229. matchSchools = schools.FindAll(x => ignore.Select(y => y.name).Contains(x.schoolname));
  230. if (matchSchools.IsNotEmpty())
  231. {
  232. if (matchSchools.Count != ignore.Count)
  233. {
  234. var matched = matchSchools.Select(x => x.schoolname);
  235. var unmatch = ignore.Select(y => y.name).Except(matched);
  236. List<string> scschoolUnmatch = schools.Select(y => y.schoolname).Except(matched).ToList();
  237. if (scschoolUnmatch.IsNotEmpty())
  238. {
  239. return new { matched, unmatch, scschoolUnmatch };
  240. }
  241. }
  242. else
  243. {
  244. matchSchools.ForEach(x => {
  245. var exschool = ignore.Find(y => y.name.Equals(x.schoolname));
  246. if (exschool != null)
  247. {
  248. x.schoolCode = exschool.id;
  249. x.areaId = $"{areaId}";
  250. x.city = $"{city}";
  251. x.dist = $"{dist}";
  252. }
  253. });
  254. }
  255. }
  256. else
  257. {
  258. return new { unmatch = ignore, schools };
  259. }
  260. }
  261. //数据校验
  262. tbschools = await table.FindListByDict<ScSchool>(new Dictionary<string, object>() { { "PartitionKey", "ScSchool" } });
  263. if (tbschools.IsNotEmpty())
  264. {
  265. var a = tbschools.Select(y => $"{y.RowKey}").ToList();
  266. saveschools = schools.Where(x => !a.Exists(z => z.Equals($"{x.schoolid}"))).ToList();
  267. List<SchoolData> schoolDatas = new List<SchoolData>();
  268. saveschools.ForEach(x =>
  269. {
  270. x.RowKey = $"{x.schoolid}";
  271. x.PartitionKey = "ScSchool";
  272. x.areaId = $"{areaId}";
  273. x.city = $"{city}";
  274. x.dist = $"{dist}";
  275. if (string.IsNullOrEmpty(x.schoolCode))
  276. {
  277. if (string.IsNullOrEmpty(x.schoolCode))
  278. {
  279. schoolDatas.Add(new SchoolData { uid = $"{x.schoolid}", province = "四川省", city = $"{city}", name = x.schoolname });
  280. }
  281. }
  282. });
  283. schoolDatas = await SchoolService.GenerateSchoolCode(schoolDatas, _dingDing, _environment);
  284. saveschools.ForEach(x => {
  285. var schoolData = schoolDatas.Find(y => y.uid.Equals($"{x.schoolid}"));
  286. if (schoolData != null && !string.IsNullOrEmpty(schoolData.id))
  287. {
  288. x.schoolCode = schoolData.id;
  289. x.areaId = $"{areaId}";
  290. x.city = $"{city}";
  291. x.dist = $"{dist}";
  292. }
  293. });
  294. saveschools.RemoveAll(x => string.IsNullOrEmpty(x.schoolCode));
  295. saveschools.RemoveAll(x => tbschools.FindAll(z => !string.IsNullOrEmpty(z.schoolCode)).Exists(y => y.schoolCode.Equals(x.schoolCode)));
  296. saveschools = await table.SaveAll(saveschools);
  297. }
  298. else
  299. {
  300. List<SchoolData> schoolDatas = new List<SchoolData>();
  301. schools.ForEach(x =>
  302. {
  303. x.RowKey = $"{x.schoolid}";
  304. x.PartitionKey = "ScSchool";
  305. x.areaId = $"{areaId}";
  306. x.city = $"{city}";
  307. x.dist = $"{dist}";
  308. var a = ignore.Find(z => z.name.Equals(x.schoolname));
  309. if (a != null)
  310. {
  311. x.schoolCode = a.id;
  312. }
  313. else
  314. {
  315. if (string.IsNullOrEmpty(x.schoolCode))
  316. {
  317. schoolDatas.Add(new SchoolData { uid = $"{x.schoolid}", province = "四川省", city = $"{city}", name = x.schoolname });
  318. }
  319. }
  320. });
  321. schoolDatas = await SchoolService.GenerateSchoolCode(schoolDatas, _dingDing, _environment);
  322. schools.ForEach(x => {
  323. var schoolData = schoolDatas.Find(y => y.uid.Equals($"{x.schoolid}"));
  324. if (schoolData != null && !string.IsNullOrEmpty(schoolData.id))
  325. {
  326. x.schoolCode = schoolData.id;
  327. x.areaId = $"{areaId}";
  328. x.city = $"{city}";
  329. x.dist = $"{dist}";
  330. }
  331. });
  332. schools.RemoveAll(x => string.IsNullOrEmpty(x.schoolCode));
  333. saveschools = await table.SaveOrUpdateAll(schools);
  334. }
  335. }
  336. return null;
  337. }
  338. public static async Task<List<SchoolData>> GenerateSchoolCode(List<SchoolData> schools, DingDing _dingDing, IWebHostEnvironment _environment)
  339. {
  340. string path = $"{_environment.ContentRootPath}/JsonFile/Core/region.json";
  341. //var schools = jsonstr.ToObject<JsonElement>().GetProperty("schools").ToObject<List<SchoolData>>();
  342. schools = schools.Where((x, i) => schools.FindIndex(n => n.name.Equals(x.name)) == i).ToList();
  343. StreamReader streamReader = new StreamReader(new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite), Encoding.UTF8);
  344. StringBuilder stringBuilder = new StringBuilder();
  345. string text;
  346. while ((text = streamReader.ReadLine()) != null)
  347. {
  348. stringBuilder.Append(text.ToString());
  349. }
  350. streamReader.Close();
  351. string input = stringBuilder.ToString();
  352. List<Region> region = input.ToObject<List<Region>>();
  353. try
  354. {
  355. schools.ForEach(x =>
  356. {
  357. x.province = ChineseConverter.Convert(x.province, ChineseConversionDirection.TraditionalToSimplified);
  358. if (String.IsNullOrEmpty(x.city))
  359. {
  360. x.city = x.province;
  361. }
  362. else
  363. {
  364. x.city = ChineseConverter.Convert(x.city, ChineseConversionDirection.TraditionalToSimplified);
  365. }
  366. x.name = ChineseConverter.Convert(x.name, ChineseConversionDirection.TraditionalToSimplified);
  367. comeRemoveStr.ForEach(c => { x.province = x.province.Replace(c, ""); });
  368. prvcRemoveStr.ForEach(c => { x.province = x.province.Replace(c, ""); });
  369. var province = region.Find(r => r.name.Contains(x.province));
  370. string tmpprovince = x.province;
  371. string tmpcity = x.city;
  372. if (province != null)
  373. {
  374. x.province = province.name;
  375. comeRemoveStr.ForEach(c => { x.city = x.city.Replace(c, ""); });
  376. cityRemoveStr.ForEach(c => { x.city = x.city.Replace(c, ""); });
  377. nationRemoveStr.ForEach(c => { x.city = x.city.Replace(c, ""); });
  378. tmpcity = x.city;
  379. var city = province.children.Find(r => r.name.Contains(x.city));
  380. if (city == null)
  381. {
  382. city = province.children.Find(r => r.name.Contains(x.city));
  383. if (city == null)
  384. {
  385. city = province.children.SelectMany(x => x.children).ToList().Find(r => r.name.Contains(x.city));
  386. }
  387. }
  388. if (city != null)
  389. {
  390. x.city = city.name;
  391. }
  392. }
  393. string name = x.name;
  394. //去除冗余的学校信息,只保留地名和校名关键信息
  395. schemoveStr.ForEach(str =>
  396. {
  397. name = name.Replace(str, "");
  398. });
  399. string[] names = name.Split("(");
  400. if (names.Length > 1)
  401. {
  402. name = $"{names[0]}";
  403. for (int index = 1; index < names.Length; index++)
  404. {
  405. name = $"{name}{names[index].Substring(0, 1)}";
  406. var afnames = names[index].Split(")");
  407. if (afnames.Length > 1)
  408. {
  409. for (int i = 1; i < afnames.Length; i++)
  410. {
  411. name = $"{name}{afnames[i]}";
  412. }
  413. }
  414. }
  415. }
  416. names = name.Split("(");
  417. if (names.Length > 1)
  418. {
  419. name = $"{names[0]}";
  420. for (int index = 1; index < names.Length; index++)
  421. {
  422. name = $"{name}{names[index].Substring(0, 1)}";
  423. var afnames = names[index].Split(")");
  424. if (afnames.Length > 1)
  425. {
  426. for (int i = 1; i < afnames.Length; i++)
  427. {
  428. name = $"{name}{afnames[i]}";
  429. }
  430. }
  431. }
  432. }
  433. name = Regex.Replace(name, "[ \\[ \\] \\^ \\-|()【】/' {}_*×――(^)$%~!@#$…&%¥—+=<>《》!!???::•`·、。,;,.;\"‘’“”-]", " ");
  434. //检查是否有英文
  435. if (Regex.Matches(name, "[a-zA-Z]").Count > 0)
  436. {
  437. var array = Regex.Split(name, "\\s+", RegexOptions.IgnoreCase);
  438. StringBuilder tmpname = new StringBuilder();
  439. if (array.Length > 1)
  440. {
  441. foreach (var item in array)
  442. {
  443. var arr = item.Select(x => $"{x}");
  444. int index = 0;
  445. foreach (var stra in arr)
  446. {
  447. if (Regex.Matches(stra, "[a-zA-Z]").Count > 0)
  448. {
  449. if (index == 1|| index == 0)
  450. {
  451. tmpname.Append(stra);
  452. }
  453. else
  454. {
  455. }
  456. }
  457. else
  458. {
  459. tmpname.Append(stra);
  460. }
  461. index++;
  462. }
  463. }
  464. }
  465. else
  466. {
  467. var arr = name.Select(x => $"{x}");
  468. int index = 0;
  469. foreach (var stra in arr)
  470. {
  471. if (Regex.Matches(stra, "[A-Z]").Count > 0)
  472. {
  473. tmpname.Append(stra);
  474. }
  475. else if (Regex.Matches(stra, "[a-z]").Count > 0)
  476. {
  477. }
  478. else
  479. {
  480. tmpname.Append(stra);
  481. }
  482. index++;
  483. }
  484. }
  485. name = tmpname.ToString();
  486. }
  487. name = Regex.Replace(name, @"\s", "");
  488. name = name.Replace("\\", "");
  489. if (name.Length > 6)
  490. {
  491. //新区,高新,产业等非新政单位
  492. areaRemoveStr.ForEach(str =>
  493. {
  494. name = name.Replace(str.Key, str.Value);
  495. });
  496. //去除冗余的学校信息,只保留地名和校名关键信息
  497. schooRemoveStr.ForEach(str =>
  498. {
  499. name = name.Replace(str, "");
  500. });
  501. //替换民族信息词
  502. if (name.Length > 6)
  503. {
  504. nationRemoveStr.ForEach(str =>
  505. {
  506. name = name.Replace(str, "");
  507. });
  508. }
  509. //替换学校信息的副词
  510. if (name.Length > 6)
  511. {
  512. foreach (var str in schooReplaceStr)
  513. {
  514. if (name.Length <= 6)
  515. {
  516. break;
  517. }
  518. name = name.Replace(str.Key, str.Value);
  519. }
  520. }
  521. //替换省简称信息词
  522. string tmpname = name;
  523. if (name.Length > 6)
  524. {
  525. proReplaceStr.ForEach(str =>
  526. {
  527. name = name.Replace(str.Key, str.Value);
  528. });
  529. //如果替换后仍然大于6位,则取消,直接替换省份完整的词
  530. if (name.Length > 6)
  531. {
  532. name = tmpname.Replace(tmpprovince, "");
  533. }
  534. }
  535. //替换城市信息词
  536. if (name.Length > 6)
  537. {
  538. name = name.Replace(tmpcity, "");
  539. }
  540. }
  541. int len = name.Length;
  542. if (len > 6)
  543. {
  544. foreach (var str in afterSchoolRm)
  545. {
  546. if (name.Length <= 6)
  547. {
  548. break;
  549. }
  550. name = name.Replace(str, "");
  551. }
  552. }
  553. len = name.Length;
  554. if (len >= 6)
  555. {
  556. name = $"{name.Substring(0, 1)}{name.Substring(len - 5, 3)}{name.Substring(len - 2, 2)}";
  557. }
  558. //if (len <= 7 && len > 6)
  559. //{
  560. // name = name.Substring(len - 6);
  561. //}
  562. if (len <= 4)
  563. {
  564. name = $"{x.city.Substring(0, 2)}{name}";
  565. }
  566. if (len == 5)
  567. {
  568. name = $"{x.city.Substring(0, 1)}{name}";
  569. }
  570. string code = ToFirstPinYin(name);
  571. x.aname = name;
  572. x.id = code;
  573. });
  574. }
  575. catch (Exception ex)
  576. {
  577. await _dingDing.SendBotMsg($"{ex.Message}\n{ex.StackTrace}\n", GroupNames.醍摩豆服務運維群組);
  578. }
  579. string a_z = "abcdefghijklmnopqrstuvwxyz";
  580. var data = schools.GroupBy(x => x.id).Select(x => new { key = x.Key, more = x.ToList().Count > 2, count = x.ToList().Count, list = x.ToList() }).ToList();
  581. data.ForEach(x =>
  582. {
  583. if (x.count > 1)
  584. {
  585. var first = x.list.First();
  586. var same = x.list.Skip(1).Take(x.count - 1).ToList();
  587. var fs = first.aname.Select(x => x).ToArray();
  588. same.ForEach(z =>
  589. {
  590. var sp = z.aname.Select(x => x).ToArray();
  591. int len = sp.Length;
  592. if (sp.Length >= fs.Length)
  593. {
  594. len = fs.Count();
  595. }
  596. for (int index = 0; index < len; index++)
  597. {
  598. if (!$"{sp[index]}".Equals($"{fs[index]}"))
  599. {
  600. short st = ChineseChar.GetStrokeNumber(sp[index]);
  601. if (st > 0)
  602. {
  603. var ins = st % 27;
  604. var insch = a_z[ins];
  605. if (z.aname.EndsWith($"{insch}"))
  606. {
  607. ins = (st + 1) % 27;
  608. insch = a_z[ins];
  609. }
  610. z.id = z.id.Insert(index, $"{insch}").Remove(index + 1, 1);
  611. break;
  612. }
  613. }
  614. }
  615. });
  616. }
  617. });
  618. var dataz = data.SelectMany(y=>y.list).GroupBy(x => x.id).Select(x => new { key = x.Key, more = x.ToList().Count > 2, count = x.ToList().Count, list = x.ToList() }).ToList();
  619. dataz.ForEach(x =>
  620. {
  621. if (x.count > 1)
  622. {
  623. var first = x.list.First();
  624. var same = x.list.Skip(1).Take(x.count - 1).ToList();
  625. var fs = first.aname.Select(x => x).ToArray();
  626. same.ForEach(z =>
  627. {
  628. var sp = z.aname.Select(x => x).ToArray();
  629. int len = sp.Length;
  630. if (sp.Length >= fs.Length)
  631. {
  632. len = fs.Count();
  633. }
  634. for (int index = 0; index < len; index++)
  635. {
  636. if (!$"{sp[index]}".Equals($"{fs[index]}"))
  637. {
  638. var spstr = ToPinYin($"{sp[index]}" );
  639. var fsstr = ToPinYin($"{fs[index]}");
  640. var insch= spstr.Select(x => $"{x}").Except( fsstr.Select(z => $"{z}"));
  641. if (insch != null && insch.Count() > 0) {
  642. if (index > 1)
  643. {
  644. z.id = z.id.Insert(index - 1, insch.First()).Remove(index, 1);
  645. }
  646. else if (index == 1)
  647. {
  648. z.id = z.id.Insert(index, insch.First()).Remove(index - 1, 1);
  649. }
  650. }
  651. }
  652. }
  653. });
  654. }
  655. });
  656. var dataa = dataz.SelectMany(y => y.list).GroupBy(x => x.id).Select(x => new { key = x.Key, more = x.ToList().Count > 2, count = x.ToList().Count, list = x.ToList() }).ToList();
  657. Random random = new Random();
  658. List<SchoolData> list = new List<SchoolData>();
  659. dataa.ForEach(x =>
  660. {
  661. if (x.count > 1)
  662. {
  663. x.list.ForEach(y => {
  664. int r= random.Next(25);
  665. int index= random.Next(5);
  666. y.id = y.id.Insert(index - 1, $"{a_z[r]}").Remove(index, 1);
  667. });
  668. }
  669. list.AddRange(x.list);
  670. });
  671. return list;
  672. }
  673. public static string ToPinYin(string val)
  674. {
  675. StringBuilder sb = new StringBuilder();
  676. val.ToCharArray().ToList().ForEach(x =>
  677. {
  678. if (('a' <= x && x <= 'z') || ('A' <= x && x <= 'Z') || ('0' <= x && x <= '9'))
  679. {
  680. sb.Append(x);
  681. }
  682. else
  683. {
  684. try
  685. {
  686. ChineseChar cc = new ChineseChar(x);
  687. if (cc.Pinyins.Count > 0 && cc.Pinyins[0].Length > 0)
  688. {
  689. sb.Append(cc.Pinyins[0]);
  690. }
  691. }
  692. catch (Exception ex)
  693. {
  694. }
  695. }
  696. });
  697. return sb.ToString().ToLower();
  698. }
  699. public static string ToFirstPinYin(string val)
  700. {
  701. StringBuilder sb = new StringBuilder();
  702. val.ToCharArray().ToList().ForEach(x =>
  703. {
  704. if (('a' <= x && x <= 'z') || ('A' <= x && x <= 'Z') || ('0' <= x && x <= '9'))
  705. {
  706. sb.Append(x);
  707. }
  708. else
  709. {
  710. try
  711. {
  712. ChineseChar cc = new ChineseChar(x);
  713. if (cc.Pinyins.Count > 0 && cc.Pinyins[0].Length > 0)
  714. {
  715. sb.Append(cc.Pinyins[0][0]);
  716. }
  717. }
  718. catch (Exception ex)
  719. {
  720. }
  721. }
  722. });
  723. return sb.ToString().ToLower();
  724. }
  725. public class SchoolData
  726. {
  727. public string province { get; set; }
  728. public string id { get; set; }
  729. public string name { get; set; }
  730. public string city { get; set; }
  731. public string aname { get; set; }
  732. public string uid { get; set; }
  733. }
  734. public static List<string> comeRemoveStr = new List<string>() { "省", "市", "区", "州", "县", "旗", "盟", "自治" };
  735. public static List<string> prvcRemoveStr = new List<string>() { "回族", "维吾尔", "壮族", };
  736. public static List<string> cityRemoveStr = new List<string>() { "蒙古族", "地区", "壮族", "朝鲜族", "直辖" };
  737. public static List<string> nationRemoveStr = new List<string> {
  738. "维吾尔","回族", "自治", "满族", "蒙古", "壮族", "苗族" , "侗族", "瑶族",
  739. "达斡尔","鄂温克","朝鲜","畲族","土家","各族","仫佬","毛南","羌族","彝族","仡佬","布依","水族",
  740. "傣族","纳西","哈尼","拉祜","佤族","傈僳","独龙","怒族","白族","普米","固族","哈萨克","土族","撒拉","景颇","族"
  741. };
  742. public static List<string> schemoveStr = new List<string> { "第", "校区", "部", "楼", "与", "学校", "校园", };
  743. public static List<string> schooRemoveStr = new List<string>() {"省", "市", "区", "州", "县", "旗", "盟","办事处","街道", "自治",
  744. "镇","村","乡","街","路","站","馆"
  745. };
  746. public static List<string> afterSchoolRm = new List<string>() {"校","园","院", "湾","峡","沟","山","庄",
  747. "堡","江","屯","岭","溪","河","桥","营","铺","坡","寨","场","湖","巷","集","关","庙","寺","矿","塘"};
  748. public static List<KeyValuePair<string, string>> areaRemoveStr = new List<KeyValuePair<string, string>>{
  749. new KeyValuePair<string, string>("高新技术产业开发区", "高") ,
  750. new KeyValuePair<string, string>("经济技术开发区", "经") ,
  751. new KeyValuePair<string, string>("国际", "际") ,
  752. new KeyValuePair<string, string>("中国", ""),
  753. new KeyValuePair<string, string>("国家", ""),
  754. new KeyValuePair<string, string>("新区", ""),
  755. new KeyValuePair<string, string>("高新", "高"),
  756. new KeyValuePair<string, string>("园区", ""),
  757. new KeyValuePair<string, string>("产业", "产"),
  758. new KeyValuePair<string, string>("经济", "经"),
  759. new KeyValuePair<string, string>("开发", "开"),
  760. new KeyValuePair<string, string>("中心", "") ,
  761. new KeyValuePair<string, string>("集团", "") ,
  762. new KeyValuePair<string, string>("公司", "") ,
  763. new KeyValuePair<string, string>("有限", "") ,
  764. new KeyValuePair<string, string>("股份", "") ,
  765. new KeyValuePair<string, string>("服务", "") ,
  766. new KeyValuePair<string, string>("基地", "") ,
  767. new KeyValuePair<string, string>("社区", "社") ,
  768. new KeyValuePair<string, string>("职工", "") ,
  769. new KeyValuePair<string, string>("专修", ""),
  770. new KeyValuePair<string, string>("实验", "验"),
  771. new KeyValuePair<string, string>("完全", ""),
  772. new KeyValuePair<string, string>("一贯制", "") ,
  773. new KeyValuePair<string, string>("联办", ""),
  774. new KeyValuePair<string, string>("联合", ""),
  775. new KeyValuePair<string, string>("完小", "小"),
  776. new KeyValuePair<string, string>("义务", ""),
  777. new KeyValuePair<string, string>("基础", ""),
  778. new KeyValuePair<string, string>("制造", "")
  779. };
  780. public static List<KeyValuePair<string, string>> schooReplaceStr = new List<KeyValuePair<string, string>> {
  781. new KeyValuePair<string, string>("分校", "分") ,
  782. new KeyValuePair<string, string>("分园", "分") ,
  783. new KeyValuePair<string, string>("分院", "分") ,
  784. new KeyValuePair<string, string>("藏文", "藏"),
  785. new KeyValuePair<string, string>("职业技术学院", "职") ,
  786. new KeyValuePair<string, string>("创新创业", "创") ,
  787. new KeyValuePair<string, string>("就业创业", "就") ,
  788. new KeyValuePair<string, string>("应用核技术", "核") ,
  789. new KeyValuePair<string, string>("职业学院", "职") ,
  790. new KeyValuePair<string, string>("幼儿园", "幼") ,
  791. new KeyValuePair<string, string>("幼儿", "幼") ,
  792. new KeyValuePair<string, string>("小学", "小") ,
  793. new KeyValuePair<string, string>("中学", "中") ,
  794. new KeyValuePair<string, string>("中等", "中") ,
  795. new KeyValuePair<string, string>("双语", "双") ,
  796. new KeyValuePair<string, string>("初中", "初") ,
  797. new KeyValuePair<string, string>("初级", "初") ,
  798. new KeyValuePair<string, string>("广播", "广"),
  799. new KeyValuePair<string, string>("高中", "高") ,
  800. new KeyValuePair<string, string>("高级", "高") ,
  801. new KeyValuePair<string, string>("大学", "大") ,
  802. new KeyValuePair<string, string>("学院", "院") ,
  803. new KeyValuePair<string, string>("综合", "综") ,
  804. new KeyValuePair<string, string>("职业", "职") ,
  805. new KeyValuePair<string, string>("技术", "技") ,
  806. new KeyValuePair<string, string>("专科", "专") ,
  807. new KeyValuePair<string, string>("外国语", "外") ,
  808. new KeyValuePair<string, string>("九年", "九") ,
  809. new KeyValuePair<string, string>("五年", "五") ,
  810. new KeyValuePair<string, string>("年制", "制") ,
  811. new KeyValuePair<string, string>("高等", "高") ,
  812. new KeyValuePair<string, string>("院校", "院") ,
  813. new KeyValuePair<string, string>("初等", "小") ,
  814. new KeyValuePair<string, string>("附属", "附"),
  815. new KeyValuePair<string, string>("寄宿制", "寄") ,
  816. new KeyValuePair<string, string>("寄宿", "寄") ,
  817. new KeyValuePair<string, string>("卫生", "卫") ,
  818. new KeyValuePair<string, string>("创业", "创") ,
  819. new KeyValuePair<string, string>("继续", "继") ,
  820. new KeyValuePair<string, string>("开放", "开") ,
  821. new KeyValuePair<string, string>("技师", "技") ,
  822. new KeyValuePair<string, string>("成人", "成") ,
  823. new KeyValuePair<string, string>("教育", "教") ,
  824. new KeyValuePair<string, string>("科技", "科") ,
  825. new KeyValuePair<string, string>("行政", "政") ,
  826. new KeyValuePair<string, string>("生物", "生") ,
  827. new KeyValuePair<string, string>("美术", "美") ,
  828. new KeyValuePair<string, string>("设计", "设") ,
  829. new KeyValuePair<string, string>("传播", "传") ,
  830. new KeyValuePair<string, string>("计算机", "算") ,
  831. new KeyValuePair<string, string>("土木", "土") ,
  832. new KeyValuePair<string, string>("石油", "油") ,
  833. new KeyValuePair<string, string>("科普", "普") ,
  834. new KeyValuePair<string, string>("电信", "信") ,
  835. new KeyValuePair<string, string>("联合", "联") ,
  836. new KeyValuePair<string, string>("电商", "商") ,
  837. new KeyValuePair<string, string>("纺织", "织") ,
  838. new KeyValuePair<string, string>("服装", "服") ,
  839. new KeyValuePair<string, string>("新闻", "新") ,
  840. new KeyValuePair<string, string>("网络", "网") ,
  841. new KeyValuePair<string, string>("应用", "应") ,
  842. new KeyValuePair<string, string>("畜牧", "畜") ,
  843. new KeyValuePair<string, string>("兽医", "兽") ,
  844. new KeyValuePair<string, string>("师范", "师") ,
  845. new KeyValuePair<string, string>("人文", "文") ,
  846. new KeyValuePair<string, string>("创新", "创") ,
  847. new KeyValuePair<string, string>("法官", "法") ,
  848. new KeyValuePair<string, string>("邮电", "邮") ,
  849. new KeyValuePair<string, string>("文化", "文") ,
  850. new KeyValuePair<string, string>("函授", "函") ,
  851. new KeyValuePair<string, string>("科学", "科") ,
  852. new KeyValuePair<string, string>("信息", "息") ,
  853. new KeyValuePair<string, string>("水利", "利") ,
  854. new KeyValuePair<string, string>("水电", "电") ,
  855. new KeyValuePair<string, string>("电力", "力") ,
  856. new KeyValuePair<string, string>("环境", "环") ,
  857. new KeyValuePair<string, string>("建材", "材") ,
  858. new KeyValuePair<string, string>("机电", "机") ,
  859. new KeyValuePair<string, string>("航空", "空") ,
  860. new KeyValuePair<string, string>("航天", "天") ,
  861. new KeyValuePair<string, string>("警察", "警") ,
  862. new KeyValuePair<string, string>("警官", "警") ,
  863. new KeyValuePair<string, string>("财贸", "财") ,
  864. new KeyValuePair<string, string>("电子", "电") ,
  865. new KeyValuePair<string, string>("建筑", "筑") ,
  866. new KeyValuePair<string, string>("艺术", "艺") ,
  867. new KeyValuePair<string, string>("体育", "体") ,
  868. new KeyValuePair<string, string>("城市", "城") ,
  869. new KeyValuePair<string, string>("地质", "地") ,
  870. new KeyValuePair<string, string>("医药", "药") ,
  871. new KeyValuePair<string, string>("政法", "法") ,
  872. new KeyValuePair<string, string>("铁路", "铁") ,
  873. new KeyValuePair<string, string>("铁道", "道") ,
  874. new KeyValuePair<string, string>("轨道", "轨") ,
  875. new KeyValuePair<string, string>("交通", "通") ,
  876. new KeyValuePair<string, string>("医学院", "医") ,
  877. new KeyValuePair<string, string>("医学", "医") ,
  878. new KeyValuePair<string, string>("传媒", "媒") ,
  879. new KeyValuePair<string, string>("工程", "程") ,
  880. new KeyValuePair<string, string>("临床", "临") ,
  881. new KeyValuePair<string, string>("化工", "化") ,
  882. new KeyValuePair<string, string>("林业", "林") ,
  883. new KeyValuePair<string, string>("老年", "老") ,
  884. new KeyValuePair<string, string>("旅游", "游") ,
  885. new KeyValuePair<string, string>("护理", "护") ,
  886. new KeyValuePair<string, string>("贸易", "贸") ,
  887. new KeyValuePair<string, string>("中医药", "中医") ,
  888. new KeyValuePair<string, string>("资源", "源") ,
  889. new KeyValuePair<string, string>("冶金", "冶") ,
  890. new KeyValuePair<string, string>("能源", "能") ,
  891. new KeyValuePair<string, string>("汽车", "车") ,
  892. new KeyValuePair<string, string>("医科", "医") ,
  893. new KeyValuePair<string, string>("机械", "械") ,
  894. new KeyValuePair<string, string>("应用", "应") ,
  895. new KeyValuePair<string, string>("电气", "电") ,
  896. new KeyValuePair<string, string>("材料", "材") ,
  897. new KeyValuePair<string, string>("劳动", "劳") ,
  898. new KeyValuePair<string, string>("轻工", "轻") ,
  899. new KeyValuePair<string, string>("农业", "农") ,
  900. new KeyValuePair<string, string>("委员会", "委") ,
  901. new KeyValuePair<string, string>("专业", "专") ,
  902. new KeyValuePair<string, string>("广播", "广"),
  903. new KeyValuePair<string, string>("电视", "电") ,
  904. new KeyValuePair<string, string>("工商", "商") ,
  905. new KeyValuePair<string, string>("工业", "工") ,
  906. new KeyValuePair<string, string>("管理", "管") ,
  907. new KeyValuePair<string, string>("社会主义", "社") ,
  908. new KeyValuePair<string, string>("社会", "社") ,
  909. new KeyValuePair<string, string>("自动化", "自") ,
  910. };
  911. public static List<KeyValuePair<string, string>> proReplaceStr = new List<KeyValuePair<string, string>> {
  912. new KeyValuePair<string, string>("北京","京") ,
  913. new KeyValuePair<string, string>("天津","津") ,
  914. new KeyValuePair<string, string>("河北","冀") ,
  915. new KeyValuePair<string, string>("山西","晋") ,
  916. new KeyValuePair<string, string>("内蒙古","蒙") ,
  917. new KeyValuePair<string, string>("辽宁","辽") ,
  918. new KeyValuePair<string, string>("吉林","吉") ,
  919. new KeyValuePair<string, string>("黑龙江","黑") ,
  920. new KeyValuePair<string, string>("上海","沪") ,
  921. new KeyValuePair<string, string>("江苏","苏") ,
  922. new KeyValuePair<string, string>("浙江","浙") ,
  923. new KeyValuePair<string, string>("安徽","皖") ,
  924. new KeyValuePair<string, string>("福建","闽") ,
  925. new KeyValuePair<string, string>("江西","赣") ,
  926. new KeyValuePair<string, string>("山东","鲁") ,
  927. new KeyValuePair<string, string>("河南","豫") ,
  928. new KeyValuePair<string, string>("湖北","鄂") ,
  929. new KeyValuePair<string, string>("湖南","湘") ,
  930. new KeyValuePair<string, string>("广东","粤") ,
  931. new KeyValuePair<string, string>("广西","桂") ,
  932. new KeyValuePair<string, string>("海南","琼") ,
  933. new KeyValuePair<string, string>("四川","川") ,
  934. new KeyValuePair<string, string>("贵州","贵") ,
  935. new KeyValuePair<string, string>("云南","云") ,
  936. new KeyValuePair<string, string>("重庆","渝") ,
  937. new KeyValuePair<string, string>("西藏","藏") ,
  938. new KeyValuePair<string, string>("陕西","陕") ,
  939. new KeyValuePair<string, string>("甘肃","甘") ,
  940. new KeyValuePair<string, string>("青海","青") ,
  941. new KeyValuePair<string, string>("宁夏","宁") ,
  942. new KeyValuePair<string, string>("新疆","新") ,
  943. new KeyValuePair<string, string>("香港","港") ,
  944. new KeyValuePair<string, string>("澳门","澳") ,
  945. new KeyValuePair<string, string>("台湾","台")
  946. };
  947. /*
  948. [
  949. {"method":"台北市","params":{"CountryId":"TW","CityId":"30"}},1
  950. {"method":"新北市","params":{"CountryId":"TW","CityId":"01"}},1
  951. {"method":"桃园市","params":{"CountryId":"TW","CityId":"03"}},1
  952. {"method":"台中市","params":{"CountryId":"TW","CityId":"66"}},1
  953. {"method":"台南市","params":{"CountryId":"TW","CityId":"67"}},1
  954. {"method":"高雄市","params":{"CountryId":"TW","CityId":"64"}},1
  955. {"method":"基隆市","params":{"CountryId":"TW","CityId":"17"}},1
  956. {"method":"新竹市","params":{"CountryId":"TW","CityId":"18"}},1
  957. {"method":"嘉义市","params":{"CountryId":"TW","CityId":"20"}},1
  958. {"method":"新竹县","params":{"CountryId":"TW","CityId":"04"}},1
  959. {"method":"苗栗县","params":{"CountryId":"TW","CityId":"05"}},1
  960. {"method":"彰化县","params":{"CountryId":"TW","CityId":"07"}},1
  961. {"method":"南投县","params":{"CountryId":"TW","CityId":"08"}},1
  962. {"method":"云林县","params":{"CountryId":"TW","CityId":"09"}},1
  963. {"method":"嘉义县","params":{"CountryId":"TW","CityId":"10"}},1
  964. {"method":"屏东县","params":{"CountryId":"TW","CityId":"13"}},1
  965. {"method":"宜兰县","params":{"CountryId":"TW","CityId":"02"}},1
  966. {"method":"花莲县","params":{"CountryId":"TW","CityId":"15"}},1
  967. {"method":"台东县","params":{"CountryId":"TW","CityId":"14"}},1
  968. {"method":"澎湖县","params":{"CountryId":"TW","CityId":"16"}},1
  969. {"method":"金门县","params":{"CountryId":"TW","CityId":"71"}},
  970. {"method":"连江县","params":{"CountryId":"TW","CityId":"72"}}
  971. ]
  972. */
  973. }
  974. }