SchoolService.cs 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013
  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. if (se != null)
  49. {
  50. string sm = se.month >= 10 ? $"{se.month}" : $"0{se.month}";
  51. string sd = se.day >= 10 ? $"{se.day}" : $"0{se.day}";
  52. start = int.Parse($"{Year}{sm}{sd}");
  53. }
  54. int curr = int.Parse(date.ToString("yyyyMMdd"));
  55. //新学年开学时间大于当前时间,计算年级需要减1 20220901-20220408 > 0 则当前20220408是2021年入学的,
  56. //当前时间大于新学年开学时间,计算年级则不需要 20220901-20221203 < 1 则当前20221203是2022年入学的,
  57. //20230901-20230101 > 0 则当前20230101是2022年入学的,
  58. int dis = start - curr;
  59. foreach (int year in years)
  60. {
  61. int grade;
  62. if (dis > 0)
  63. {
  64. if (Year - year > 0)
  65. {
  66. grade = (Year - year - 1) % Count.Value;
  67. }
  68. else
  69. {
  70. grade = Math.Abs((Year - year)) % Count.Value;
  71. }
  72. }
  73. else
  74. {
  75. grade = Math.Abs((Year - year)) % Count.Value;
  76. }
  77. yearGrades.Add(new KeyValuePair<int, string>(year,$"{grade}"));
  78. }
  79. }
  80. return yearGrades;
  81. }
  82. /// <summary>
  83. /// 处理学期排序
  84. /// </summary>
  85. /// <param name="semesterList"></param>
  86. /// <returns></returns>
  87. public static List<Semester> SortSemester(List<Semester> semesterList)
  88. {
  89. int Year = DateTimeOffset.UtcNow.Year;
  90. List<KeyValuePair<int, Semester>> pairs = new List<KeyValuePair<int, Semester>>();
  91. semesterList.ForEach(se => {
  92. string sm = se.month >= 10 ? $"{se.month}" : $"0{se.month}";
  93. string sd = se.day >= 10 ? $"{se.day}" : $"0{se.day}";
  94. int order = int.Parse($"{Year}{sm}{sd}");
  95. pairs.Add(new KeyValuePair<int, Semester>(order, se));
  96. });
  97. var orderPairs = pairs.OrderBy(z => z.Key);
  98. semesterList = orderPairs.Select(z => z.Value).ToList();
  99. int startIndex = semesterList.FindIndex(z => z.start == 1);
  100. if (startIndex == -1)
  101. {
  102. //未设置学期的情况默认9月份开始的。
  103. startIndex = semesterList.FindIndex(z => z.month == 9);
  104. //如果还未找到,就以排序结果为准
  105. if (startIndex == -1)
  106. {
  107. startIndex = 0;
  108. }
  109. }
  110. if (startIndex > 0)
  111. {
  112. List<Semester> before = semesterList.Take(startIndex).ToList();
  113. List<Semester> after = semesterList.Skip(startIndex).ToList();
  114. semesterList = new List<Semester>();
  115. semesterList.AddRange(after);
  116. semesterList.AddRange(before);
  117. return semesterList;
  118. }
  119. else
  120. {
  121. return semesterList;
  122. }
  123. }
  124. 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) {
  125. List<Class> school_classes = new List<Class>();
  126. List<Class> graduate_classes = new List<Class>();
  127. try
  128. {
  129. var client = _azureCosmos.GetCosmosClient();
  130. //取得班级
  131. int nowYear = DateTimeOffset.UtcNow.Year;
  132. int nowMonth = DateTimeOffset.UtcNow.Month;
  133. int nowDay = DateTimeOffset.UtcNow.Day;
  134. //因为修改年级,而导致取消的
  135. List<Class> cancel_graduate_classes = new List<Class>();
  136. string sql = $"SELECT value c FROM c where (c.graduate = 0 or IS_DEFINED(c.graduate) = false) ";
  137. if (periodIds.IsNotEmpty())
  138. {
  139. sql = $"SELECT value c FROM c where c.periodId in ({string.Join(",", periodIds.Select(x => $"'{x}'"))}) ";
  140. }
  141. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<Class>
  142. //(queryText: $"SELECT value c FROM c where (c.graduate = 0 or IS_DEFINED(c.graduate) = false)",
  143. (queryText: sql,
  144. requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Class-{school_base.id}") }))
  145. {
  146. bool isgraduate = false;
  147. if (!string.IsNullOrWhiteSpace(item.periodId))
  148. {
  149. var period = school_base.period.Find(x => x.id.Equals(item.periodId));
  150. if (period != null)
  151. {
  152. var gradeCount = period.grades.Count();
  153. //2022-2016=6(待判断月份日期是否是毕业) 2022-2017=5(未毕业)
  154. if (nowYear - item.year > gradeCount)
  155. {
  156. isgraduate = true;
  157. }
  158. else if (nowYear - item.year == gradeCount)
  159. {
  160. var semester = period.semesters.Find(x => x.start == 1);
  161. if (semester != null)
  162. {
  163. if (nowMonth > semester.month)
  164. {
  165. isgraduate = true;
  166. }
  167. else if (nowMonth == semester.month)
  168. {
  169. if (nowDay >= semester.day)
  170. {
  171. isgraduate = true;
  172. }
  173. else
  174. {
  175. }
  176. }
  177. else { isgraduate = false; }
  178. }
  179. }
  180. else
  181. {
  182. isgraduate = false;
  183. }
  184. }
  185. }
  186. if (isgraduate)
  187. {
  188. graduate_classes.Add(item);
  189. }
  190. else
  191. {
  192. if (item.graduate == 1)
  193. {
  194. item.graduate = 0;
  195. cancel_graduate_classes.Add(item);
  196. }
  197. school_classes.Add(item);
  198. }
  199. }
  200. if (graduate_classes.Any() || cancel_graduate_classes.Any())
  201. {
  202. if (waite == 0)
  203. {
  204. _ = _httpTrigger.RequestHttpTrigger(new { graduate_classes = graduate_classes, cancel_graduate_classes = cancel_graduate_classes, schoolId = $"{school_base.id}" }, _option.Location, "graduate-change");
  205. }
  206. else
  207. {
  208. await _httpTrigger.RequestHttpTrigger(new { graduate_classes = graduate_classes, cancel_graduate_classes = cancel_graduate_classes, schoolId = $"{school_base.id}" }, _option.Location, "graduate-change");
  209. }
  210. }
  211. } catch (Exception ex) {
  212. await _dingDing.SendBotMsg($"{_option.Location},{ex.Message}\n{ex.StackTrace}", GroupNames.醍摩豆服務運維群組);
  213. }
  214. return (school_classes,graduate_classes);
  215. }
  216. public static async Task<object> DoScsApiSchool(HttpTrigger _httpTrigger,Dictionary<string,object> dict, Option _option, List<IdNameCode> ignore,
  217. string areaId,string city,string dist, AzureStorageFactory _azureStorage ,DingDing _dingDing, IWebHostEnvironment _environment
  218. )
  219. {
  220. List<ScSchool> tbschools = null;
  221. List<ScSchool> matchSchools = null;
  222. List<ScSchool> schools = null;
  223. List<ScSchool> saveschools = null;
  224. var table = _azureStorage.GetCloudTableClient().GetTableReference("ScYxpt");
  225. // 5.3.1.18根据机构ID、项目ID、子项目ID返回学校列表
  226. ( int status, string json) = await _httpTrigger.RequestHttpTrigger(dict, _option.Location, "GetSchoolList");
  227. if (status == 200)
  228. {
  229. schools = json.ToObject<List<ScSchool>>(new JsonSerializerOptions { PropertyNameCaseInsensitive = false });
  230. if (ignore.IsNotEmpty())
  231. {
  232. matchSchools = schools.FindAll(x => ignore.Select(y => y.name).Contains(x.schoolname));
  233. if (matchSchools.IsNotEmpty())
  234. {
  235. if (matchSchools.Count != ignore.Count)
  236. {
  237. var matched = matchSchools.Select(x => x.schoolname);
  238. var unmatch = ignore.Select(y => y.name).Except(matched);
  239. List<string> scschoolUnmatch = schools.Select(y => y.schoolname).Except(matched).ToList();
  240. if (scschoolUnmatch.IsNotEmpty())
  241. {
  242. return new { matched, unmatch, scschoolUnmatch };
  243. }
  244. }
  245. else
  246. {
  247. matchSchools.ForEach(x => {
  248. var exschool = ignore.Find(y => y.name.Equals(x.schoolname));
  249. if (exschool != null)
  250. {
  251. x.schoolCode = exschool.id;
  252. x.areaId = $"{areaId}";
  253. x.city = $"{city}";
  254. x.dist = $"{dist}";
  255. }
  256. });
  257. }
  258. }
  259. else
  260. {
  261. return new { unmatch = ignore, schools };
  262. }
  263. }
  264. //数据校验
  265. tbschools = await table.FindListByDict<ScSchool>(new Dictionary<string, object>() { { "PartitionKey", "ScSchool" } });
  266. if (tbschools.IsNotEmpty())
  267. {
  268. var a = tbschools.Select(y => $"{y.RowKey}").ToList();
  269. saveschools = schools.Where(x => !a.Exists(z => z.Equals($"{x.schoolid}"))).ToList();
  270. List<SchoolData> schoolDatas = new List<SchoolData>();
  271. saveschools.ForEach(x =>
  272. {
  273. x.RowKey = $"{x.schoolid}";
  274. x.PartitionKey = "ScSchool";
  275. x.areaId = $"{areaId}";
  276. x.city = $"{city}";
  277. x.dist = $"{dist}";
  278. if (string.IsNullOrEmpty(x.schoolCode))
  279. {
  280. if (string.IsNullOrEmpty(x.schoolCode))
  281. {
  282. schoolDatas.Add(new SchoolData { uid = $"{x.schoolid}", province = "四川省", city = $"{city}", name = x.schoolname });
  283. }
  284. }
  285. });
  286. schoolDatas = await SchoolService.GenerateSchoolCode(schoolDatas, _dingDing, _environment);
  287. saveschools.ForEach(x => {
  288. var schoolData = schoolDatas.Find(y => y.uid.Equals($"{x.schoolid}"));
  289. if (schoolData != null && !string.IsNullOrEmpty(schoolData.id))
  290. {
  291. x.schoolCode = schoolData.id;
  292. x.areaId = $"{areaId}";
  293. x.city = $"{city}";
  294. x.dist = $"{dist}";
  295. }
  296. });
  297. saveschools.RemoveAll(x => string.IsNullOrEmpty(x.schoolCode));
  298. saveschools.RemoveAll(x => tbschools.FindAll(z => !string.IsNullOrEmpty(z.schoolCode)).Exists(y => y.schoolCode.Equals(x.schoolCode)));
  299. saveschools = await table.SaveAll(saveschools);
  300. }
  301. else
  302. {
  303. List<SchoolData> schoolDatas = new List<SchoolData>();
  304. schools.ForEach(x =>
  305. {
  306. x.RowKey = $"{x.schoolid}";
  307. x.PartitionKey = "ScSchool";
  308. x.areaId = $"{areaId}";
  309. x.city = $"{city}";
  310. x.dist = $"{dist}";
  311. var a = ignore.Find(z => z.name.Equals(x.schoolname));
  312. if (a != null)
  313. {
  314. x.schoolCode = a.id;
  315. }
  316. else
  317. {
  318. if (string.IsNullOrEmpty(x.schoolCode))
  319. {
  320. schoolDatas.Add(new SchoolData { uid = $"{x.schoolid}", province = "四川省", city = $"{city}", name = x.schoolname });
  321. }
  322. }
  323. });
  324. schoolDatas = await SchoolService.GenerateSchoolCode(schoolDatas, _dingDing, _environment);
  325. schools.ForEach(x => {
  326. var schoolData = schoolDatas.Find(y => y.uid.Equals($"{x.schoolid}"));
  327. if (schoolData != null && !string.IsNullOrEmpty(schoolData.id))
  328. {
  329. x.schoolCode = schoolData.id;
  330. x.areaId = $"{areaId}";
  331. x.city = $"{city}";
  332. x.dist = $"{dist}";
  333. }
  334. });
  335. schools.RemoveAll(x => string.IsNullOrEmpty(x.schoolCode));
  336. saveschools = await table.SaveOrUpdateAll(schools);
  337. }
  338. }
  339. return null;
  340. }
  341. public static async Task<List<SchoolData>> GenerateSchoolCode(List<SchoolData> schools, DingDing _dingDing, IWebHostEnvironment _environment)
  342. {
  343. string path = $"{_environment.ContentRootPath}/JsonFile/Core/region.json";
  344. //var schools = jsonstr.ToObject<JsonElement>().GetProperty("schools").ToObject<List<SchoolData>>();
  345. schools = schools.Where((x, i) => schools.FindIndex(n => n.name.Equals(x.name)) == i).ToList();
  346. StreamReader streamReader = new StreamReader(new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite), Encoding.UTF8);
  347. StringBuilder stringBuilder = new StringBuilder();
  348. string text;
  349. while ((text = streamReader.ReadLine()) != null)
  350. {
  351. stringBuilder.Append(text.ToString());
  352. }
  353. streamReader.Close();
  354. string input = stringBuilder.ToString();
  355. List<Region> region = input.ToObject<List<Region>>();
  356. try
  357. {
  358. schools.ForEach(x =>
  359. {
  360. x.province = ChineseConverter.Convert(x.province, ChineseConversionDirection.TraditionalToSimplified);
  361. if (String.IsNullOrEmpty(x.city))
  362. {
  363. x.city = x.province;
  364. }
  365. else
  366. {
  367. x.city = ChineseConverter.Convert(x.city, ChineseConversionDirection.TraditionalToSimplified);
  368. }
  369. x.name = ChineseConverter.Convert(x.name, ChineseConversionDirection.TraditionalToSimplified);
  370. comeRemoveStr.ForEach(c => { x.province = x.province.Replace(c, ""); });
  371. prvcRemoveStr.ForEach(c => { x.province = x.province.Replace(c, ""); });
  372. var province = region.Find(r => r.name.Contains(x.province));
  373. string tmpprovince = x.province;
  374. string tmpcity = x.city;
  375. if (province != null)
  376. {
  377. x.province = province.name;
  378. comeRemoveStr.ForEach(c => { x.city = x.city.Replace(c, ""); });
  379. cityRemoveStr.ForEach(c => { x.city = x.city.Replace(c, ""); });
  380. nationRemoveStr.ForEach(c => { x.city = x.city.Replace(c, ""); });
  381. tmpcity = x.city;
  382. var city = province.children.Find(r => r.name.Contains(x.city));
  383. if (city == null)
  384. {
  385. city = province.children.Find(r => r.name.Contains(x.city));
  386. if (city == null)
  387. {
  388. city = province.children.SelectMany(x => x.children).ToList().Find(r => r.name.Contains(x.city));
  389. }
  390. }
  391. if (city != null)
  392. {
  393. x.city = city.name;
  394. }
  395. }
  396. string name = x.name;
  397. //去除冗余的学校信息,只保留地名和校名关键信息
  398. schemoveStr.ForEach(str =>
  399. {
  400. name = name.Replace(str, "");
  401. });
  402. string[] names = name.Split("(");
  403. if (names.Length > 1)
  404. {
  405. name = $"{names[0]}";
  406. for (int index = 1; index < names.Length; index++)
  407. {
  408. name = $"{name}{names[index].Substring(0, 1)}";
  409. var afnames = names[index].Split(")");
  410. if (afnames.Length > 1)
  411. {
  412. for (int i = 1; i < afnames.Length; i++)
  413. {
  414. name = $"{name}{afnames[i]}";
  415. }
  416. }
  417. }
  418. }
  419. names = name.Split("(");
  420. if (names.Length > 1)
  421. {
  422. name = $"{names[0]}";
  423. for (int index = 1; index < names.Length; index++)
  424. {
  425. name = $"{name}{names[index].Substring(0, 1)}";
  426. var afnames = names[index].Split(")");
  427. if (afnames.Length > 1)
  428. {
  429. for (int i = 1; i < afnames.Length; i++)
  430. {
  431. name = $"{name}{afnames[i]}";
  432. }
  433. }
  434. }
  435. }
  436. name = Regex.Replace(name, "[ \\[ \\] \\^ \\-|()【】/' {}_*×――(^)$%~!@#$…&%¥—+=<>《》!!???::•`·、。,;,.;\"‘’“”-]", " ");
  437. //检查是否有英文
  438. if (Regex.Matches(name, "[a-zA-Z]").Count > 0)
  439. {
  440. var array = Regex.Split(name, "\\s+", RegexOptions.IgnoreCase);
  441. StringBuilder tmpname = new StringBuilder();
  442. if (array.Length > 1)
  443. {
  444. foreach (var item in array)
  445. {
  446. var arr = item.Select(x => $"{x}");
  447. int index = 0;
  448. foreach (var stra in arr)
  449. {
  450. if (Regex.Matches(stra, "[a-zA-Z]").Count > 0)
  451. {
  452. if (index == 1|| index == 0)
  453. {
  454. tmpname.Append(stra);
  455. }
  456. else
  457. {
  458. }
  459. }
  460. else
  461. {
  462. tmpname.Append(stra);
  463. }
  464. index++;
  465. }
  466. }
  467. }
  468. else
  469. {
  470. var arr = name.Select(x => $"{x}");
  471. int index = 0;
  472. foreach (var stra in arr)
  473. {
  474. if (Regex.Matches(stra, "[A-Z]").Count > 0)
  475. {
  476. tmpname.Append(stra);
  477. }
  478. else if (Regex.Matches(stra, "[a-z]").Count > 0)
  479. {
  480. }
  481. else
  482. {
  483. tmpname.Append(stra);
  484. }
  485. index++;
  486. }
  487. }
  488. name = tmpname.ToString();
  489. }
  490. name = Regex.Replace(name, @"\s", "");
  491. name = name.Replace("\\", "");
  492. if (name.Length > 6)
  493. {
  494. //新区,高新,产业等非新政单位
  495. areaRemoveStr.ForEach(str =>
  496. {
  497. name = name.Replace(str.Key, str.Value);
  498. });
  499. //去除冗余的学校信息,只保留地名和校名关键信息
  500. schooRemoveStr.ForEach(str =>
  501. {
  502. name = name.Replace(str, "");
  503. });
  504. //替换民族信息词
  505. if (name.Length > 6)
  506. {
  507. nationRemoveStr.ForEach(str =>
  508. {
  509. name = name.Replace(str, "");
  510. });
  511. }
  512. //替换学校信息的副词
  513. if (name.Length > 6)
  514. {
  515. foreach (var str in schooReplaceStr)
  516. {
  517. if (name.Length <= 6)
  518. {
  519. break;
  520. }
  521. name = name.Replace(str.Key, str.Value);
  522. }
  523. }
  524. //替换省简称信息词
  525. string tmpname = name;
  526. if (name.Length > 6)
  527. {
  528. proReplaceStr.ForEach(str =>
  529. {
  530. name = name.Replace(str.Key, str.Value);
  531. });
  532. //如果替换后仍然大于6位,则取消,直接替换省份完整的词
  533. if (name.Length > 6)
  534. {
  535. name = tmpname.Replace(tmpprovince, "");
  536. }
  537. }
  538. //替换城市信息词
  539. if (name.Length > 6)
  540. {
  541. name = name.Replace(tmpcity, "");
  542. }
  543. }
  544. int len = name.Length;
  545. if (len > 6)
  546. {
  547. foreach (var str in afterSchoolRm)
  548. {
  549. if (name.Length <= 6)
  550. {
  551. break;
  552. }
  553. name = name.Replace(str, "");
  554. }
  555. }
  556. len = name.Length;
  557. if (len >= 6)
  558. {
  559. name = $"{name.Substring(0, 1)}{name.Substring(len - 5, 3)}{name.Substring(len - 2, 2)}";
  560. }
  561. //if (len <= 7 && len > 6)
  562. //{
  563. // name = name.Substring(len - 6);
  564. //}
  565. if (len <= 4)
  566. {
  567. name = $"{x.city.Substring(0, 2)}{name}";
  568. }
  569. if (len == 5)
  570. {
  571. name = $"{x.city.Substring(0, 1)}{name}";
  572. }
  573. string code = ToFirstPinYin(name);
  574. x.aname = name;
  575. x.id = code;
  576. });
  577. }
  578. catch (Exception ex)
  579. {
  580. await _dingDing.SendBotMsg($"{ex.Message}\n{ex.StackTrace}\n", GroupNames.醍摩豆服務運維群組);
  581. }
  582. string a_z = "abcdefghijklmnopqrstuvwxyz";
  583. 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();
  584. data.ForEach(x =>
  585. {
  586. if (x.count > 1)
  587. {
  588. var first = x.list.First();
  589. var same = x.list.Skip(1).Take(x.count - 1).ToList();
  590. var fs = first.aname.Select(x => x).ToArray();
  591. same.ForEach(z =>
  592. {
  593. var sp = z.aname.Select(x => x).ToArray();
  594. int len = sp.Length;
  595. if (sp.Length >= fs.Length)
  596. {
  597. len = fs.Count();
  598. }
  599. for (int index = 0; index < len; index++)
  600. {
  601. if (!$"{sp[index]}".Equals($"{fs[index]}"))
  602. {
  603. short st = ChineseChar.GetStrokeNumber(sp[index]);
  604. if (st > 0)
  605. {
  606. var ins = st % 27;
  607. var insch = a_z[ins];
  608. if (z.aname.EndsWith($"{insch}"))
  609. {
  610. ins = (st + 1) % 27;
  611. insch = a_z[ins];
  612. }
  613. z.id = z.id.Insert(index, $"{insch}").Remove(index + 1, 1);
  614. break;
  615. }
  616. }
  617. }
  618. });
  619. }
  620. });
  621. 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();
  622. dataz.ForEach(x =>
  623. {
  624. if (x.count > 1)
  625. {
  626. var first = x.list.First();
  627. var same = x.list.Skip(1).Take(x.count - 1).ToList();
  628. var fs = first.aname.Select(x => x).ToArray();
  629. same.ForEach(z =>
  630. {
  631. var sp = z.aname.Select(x => x).ToArray();
  632. int len = sp.Length;
  633. if (sp.Length >= fs.Length)
  634. {
  635. len = fs.Count();
  636. }
  637. for (int index = 0; index < len; index++)
  638. {
  639. if (!$"{sp[index]}".Equals($"{fs[index]}"))
  640. {
  641. var spstr = ToPinYin($"{sp[index]}" );
  642. var fsstr = ToPinYin($"{fs[index]}");
  643. var insch= spstr.Select(x => $"{x}").Except( fsstr.Select(z => $"{z}"));
  644. if (insch != null && insch.Count() > 0) {
  645. if (index > 1)
  646. {
  647. z.id = z.id.Insert(index - 1, insch.First()).Remove(index, 1);
  648. }
  649. else if (index == 1)
  650. {
  651. z.id = z.id.Insert(index, insch.First()).Remove(index - 1, 1);
  652. }
  653. }
  654. }
  655. }
  656. });
  657. }
  658. });
  659. 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();
  660. Random random = new Random();
  661. List<SchoolData> list = new List<SchoolData>();
  662. dataa.ForEach(x =>
  663. {
  664. if (x.count > 1)
  665. {
  666. x.list.ForEach(y => {
  667. int r= random.Next(25);
  668. int index= random.Next(5);
  669. y.id = y.id.Insert(index - 1, $"{a_z[r]}").Remove(index, 1);
  670. });
  671. }
  672. list.AddRange(x.list);
  673. });
  674. return list;
  675. }
  676. public static string ToPinYin(string val)
  677. {
  678. StringBuilder sb = new StringBuilder();
  679. val.ToCharArray().ToList().ForEach(x =>
  680. {
  681. if (('a' <= x && x <= 'z') || ('A' <= x && x <= 'Z') || ('0' <= x && x <= '9'))
  682. {
  683. sb.Append(x);
  684. }
  685. else
  686. {
  687. try
  688. {
  689. ChineseChar cc = new ChineseChar(x);
  690. if (cc.Pinyins.Count > 0 && cc.Pinyins[0].Length > 0)
  691. {
  692. sb.Append(cc.Pinyins[0]);
  693. }
  694. }
  695. catch (Exception ex)
  696. {
  697. }
  698. }
  699. });
  700. return sb.ToString().ToLower();
  701. }
  702. public static string ToFirstPinYin(string val)
  703. {
  704. StringBuilder sb = new StringBuilder();
  705. val.ToCharArray().ToList().ForEach(x =>
  706. {
  707. if (('a' <= x && x <= 'z') || ('A' <= x && x <= 'Z') || ('0' <= x && x <= '9'))
  708. {
  709. sb.Append(x);
  710. }
  711. else
  712. {
  713. try
  714. {
  715. ChineseChar cc = new ChineseChar(x);
  716. if (cc.Pinyins.Count > 0 && cc.Pinyins[0].Length > 0)
  717. {
  718. sb.Append(cc.Pinyins[0][0]);
  719. }
  720. }
  721. catch (Exception ex)
  722. {
  723. }
  724. }
  725. });
  726. return sb.ToString().ToLower();
  727. }
  728. public class SchoolData
  729. {
  730. public string province { get; set; }
  731. public string id { get; set; }
  732. public string name { get; set; }
  733. public string city { get; set; }
  734. public string aname { get; set; }
  735. public string uid { get; set; }
  736. }
  737. public static List<string> comeRemoveStr = new List<string>() { "省", "市", "区", "州", "县", "旗", "盟", "自治" };
  738. public static List<string> prvcRemoveStr = new List<string>() { "回族", "维吾尔", "壮族", };
  739. public static List<string> cityRemoveStr = new List<string>() { "蒙古族", "地区", "壮族", "朝鲜族", "直辖" };
  740. public static List<string> nationRemoveStr = new List<string> {
  741. "维吾尔","回族", "自治", "满族", "蒙古", "壮族", "苗族" , "侗族", "瑶族",
  742. "达斡尔","鄂温克","朝鲜","畲族","土家","各族","仫佬","毛南","羌族","彝族","仡佬","布依","水族",
  743. "傣族","纳西","哈尼","拉祜","佤族","傈僳","独龙","怒族","白族","普米","固族","哈萨克","土族","撒拉","景颇","族"
  744. };
  745. public static List<string> schemoveStr = new List<string> { "第", "校区", "部", "楼", "与", "学校", "校园", };
  746. public static List<string> schooRemoveStr = new List<string>() {"省", "市", "区", "州", "县", "旗", "盟","办事处","街道", "自治",
  747. "镇","村","乡","街","路","站","馆"
  748. };
  749. public static List<string> afterSchoolRm = new List<string>() {"校","园","院", "湾","峡","沟","山","庄",
  750. "堡","江","屯","岭","溪","河","桥","营","铺","坡","寨","场","湖","巷","集","关","庙","寺","矿","塘"};
  751. public static List<KeyValuePair<string, string>> areaRemoveStr = new List<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. new KeyValuePair<string, string>("义务", ""),
  780. new KeyValuePair<string, string>("基础", ""),
  781. new KeyValuePair<string, string>("制造", "")
  782. };
  783. public static List<KeyValuePair<string, string>> schooReplaceStr = new List<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. new KeyValuePair<string, string>("社会主义", "社") ,
  911. new KeyValuePair<string, string>("社会", "社") ,
  912. new KeyValuePair<string, string>("自动化", "自") ,
  913. };
  914. public static List<KeyValuePair<string, string>> proReplaceStr = new List<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. new KeyValuePair<string, string>("香港","港") ,
  947. new KeyValuePair<string, string>("澳门","澳") ,
  948. new KeyValuePair<string, string>("台湾","台")
  949. };
  950. /*
  951. [
  952. {"method":"台北市","params":{"CountryId":"TW","CityId":"30"}},1
  953. {"method":"新北市","params":{"CountryId":"TW","CityId":"01"}},1
  954. {"method":"桃园市","params":{"CountryId":"TW","CityId":"03"}},1
  955. {"method":"台中市","params":{"CountryId":"TW","CityId":"66"}},1
  956. {"method":"台南市","params":{"CountryId":"TW","CityId":"67"}},1
  957. {"method":"高雄市","params":{"CountryId":"TW","CityId":"64"}},1
  958. {"method":"基隆市","params":{"CountryId":"TW","CityId":"17"}},1
  959. {"method":"新竹市","params":{"CountryId":"TW","CityId":"18"}},1
  960. {"method":"嘉义市","params":{"CountryId":"TW","CityId":"20"}},1
  961. {"method":"新竹县","params":{"CountryId":"TW","CityId":"04"}},1
  962. {"method":"苗栗县","params":{"CountryId":"TW","CityId":"05"}},1
  963. {"method":"彰化县","params":{"CountryId":"TW","CityId":"07"}},1
  964. {"method":"南投县","params":{"CountryId":"TW","CityId":"08"}},1
  965. {"method":"云林县","params":{"CountryId":"TW","CityId":"09"}},1
  966. {"method":"嘉义县","params":{"CountryId":"TW","CityId":"10"}},1
  967. {"method":"屏东县","params":{"CountryId":"TW","CityId":"13"}},1
  968. {"method":"宜兰县","params":{"CountryId":"TW","CityId":"02"}},1
  969. {"method":"花莲县","params":{"CountryId":"TW","CityId":"15"}},1
  970. {"method":"台东县","params":{"CountryId":"TW","CityId":"14"}},1
  971. {"method":"澎湖县","params":{"CountryId":"TW","CityId":"16"}},1
  972. {"method":"金门县","params":{"CountryId":"TW","CityId":"71"}},
  973. {"method":"连江县","params":{"CountryId":"TW","CityId":"72"}}
  974. ]
  975. */
  976. }
  977. }