SchoolService.cs 70 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434
  1. using Azure.Cosmos;
  2. using Azure.Storage.Blobs.Models;
  3. using DocumentFormat.OpenXml.Bibliography;
  4. using DocumentFormat.OpenXml.Drawing.Charts;
  5. using HTEXLib.COMM.Helpers;
  6. using Microsoft.AspNetCore.Hosting;
  7. using Microsoft.Extensions.Configuration;
  8. using Microsoft.Extensions.Hosting;
  9. using Microsoft.International.Converters.PinYinConverter;
  10. using Microsoft.International.Converters.TraditionalChineseToSimplifiedConverter;
  11. using System;
  12. using System.Collections.Generic;
  13. using System.Configuration;
  14. using System.IO;
  15. using System.Linq;
  16. using System.Text;
  17. using System.Text.Json;
  18. using System.Text.RegularExpressions;
  19. using System.Threading.Tasks;
  20. using TEAMModelOS.Models;
  21. using TEAMModelOS.SDK.DI;
  22. using TEAMModelOS.SDK.Extension;
  23. using TEAMModelOS.SDK.Models;
  24. using Period = TEAMModelOS.SDK.Models.Period;
  25. namespace TEAMModelOS.SDK
  26. {
  27. public class SchoolService
  28. {
  29. public static async Task<(int code, string msg, object data)> JoinScool(string school, string id, string name, string picture, long ts,
  30. AzureCosmosFactory _azureCosmos,AzureStorageFactory _azureStorage,CoreAPIHttpService _coreAPIHttpService,DingDing _dingDing,Option _option,IConfiguration _configuration, IWebHostEnvironment _environment)
  31. {
  32. SchoolTeacher schoolTeacher = null;
  33. School schoolInfo = null;
  34. long nowTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
  35. try
  36. {
  37. if (nowTime - ts > 3600000)
  38. {
  39. return (1, "超时!", null);
  40. }
  41. var client = _azureCosmos.GetCosmosClient();
  42. try
  43. {
  44. schoolInfo = await client.GetContainer(Constant.TEAMModelOS, "School").ReadItemAsync<School>($"{school}", new PartitionKey("Base"));
  45. }
  46. catch (CosmosException ex)
  47. {
  48. if (ex.Status == 404)
  49. {
  50. return (2, "该学校不存在!", null);
  51. }
  52. }
  53. Teacher teacher = null;
  54. try
  55. {
  56. teacher = await client.GetContainer(Constant.TEAMModelOS, "Teacher").ReadItemAsync<Teacher>($"{id}", new PartitionKey("Base"));
  57. var sch = teacher.schools.Find(x => x.schoolId.Equals($"{school}"));
  58. if (sch != null)
  59. {
  60. if (sch.status.Equals("join"))
  61. {
  62. return (3, "该教师之前已经加入学校!", null);
  63. }
  64. else
  65. {
  66. sch.status = "join";
  67. sch.time = nowTime;
  68. sch.name = schoolInfo?.name;
  69. sch.picture = schoolInfo?.picture;
  70. sch.areaId = schoolInfo?.areaId;
  71. }
  72. }
  73. else
  74. {
  75. teacher.schools.Add(new Teacher.TeacherSchool { areaId = schoolInfo?.areaId, schoolId = schoolInfo.id, status = "join", name = schoolInfo.name, picture = schoolInfo.picture, time = nowTime });
  76. }
  77. if (teacher.size < 2)
  78. {
  79. teacher.size = 2;
  80. }
  81. await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "Teacher").ReplaceItemAsync<Teacher>(teacher, teacher.id, new PartitionKey($"Base"));
  82. }
  83. catch (CosmosException ex)
  84. {
  85. if (ex.Status == 404)
  86. {
  87. //如果沒有,則初始化Teacher基本資料到Cosmos
  88. teacher = new Teacher
  89. {
  90. createTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
  91. id = id,
  92. pk = "Base",
  93. code = "Base",
  94. name = name?.ToString(),
  95. // picture = picture?.ToString(),
  96. //创建账号并第一次登录IES5则默认赠送1G
  97. defaultSchool = null,
  98. size = 2,
  99. schools = new List<Teacher.TeacherSchool>() { new Teacher.TeacherSchool { areaId = schoolInfo?.areaId, schoolId = schoolInfo.id, status = "join", name = schoolInfo.name, picture = schoolInfo.picture, time = nowTime } }
  100. };
  101. var container = _azureStorage.GetBlobContainerClient(id);
  102. await container.CreateIfNotExistsAsync(PublicAccessType.None); //嘗試創建Teacher私有容器,如存在則不做任何事,保障容器一定存在
  103. await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "Teacher").CreateItemAsync<Teacher>(teacher, new PartitionKey("Base"));
  104. }
  105. }
  106. Azure.Response response = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "School").ReadItemStreamAsync(teacher.id, new PartitionKey($"Teacher-{schoolInfo.id}"));
  107. if (response.Status != 200)
  108. {
  109. schoolTeacher = new SchoolTeacher
  110. {
  111. id = teacher.id,
  112. pk = "Teacher",
  113. name = teacher.name,
  114. picture = teacher.picture,
  115. size = 0,
  116. roles = new List<string> { "teacher" },
  117. permissions = new List<string>(),
  118. status = "join",
  119. createTime = nowTime,
  120. code = $"Teacher-{schoolInfo.id}",
  121. ttl = -1
  122. };
  123. await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "School").CreateItemAsync<SchoolTeacher>(schoolTeacher, new PartitionKey($"Teacher-{schoolInfo.id}"));
  124. }
  125. else
  126. {
  127. JsonElement data = JsonDocument.Parse(response.ContentStream).RootElement;
  128. schoolTeacher = data.ToObject<SchoolTeacher>();
  129. schoolTeacher.status = "join";
  130. schoolTeacher.name = schoolInfo.name;
  131. schoolTeacher.picture = schoolInfo.picture;
  132. await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "School").ReplaceItemAsync<SchoolTeacher>(schoolTeacher, schoolTeacher.id, new PartitionKey($"Teacher-{schoolInfo.id}"));
  133. }
  134. //通知管理员
  135. string sql = "select distinct value(c.id) from c where array_contains(c.roles,'admin')";
  136. List<string> ids = new List<string>();
  137. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<string>(queryText: sql,
  138. requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"Teacher-{school}") }))
  139. {
  140. ids.Add(item);
  141. }
  142. string bizcode = "scan-join";
  143. string tsql = $"select c.id, c.name ,c.lang as code from c where c.id in ({string.Join(",", ids.Select(x => $"'{x}'"))})";
  144. List<IdNameCode> idNameCodes = new List<IdNameCode>();
  145. await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.Teacher)
  146. .GetItemQueryIterator<IdNameCode>(queryText: tsql, requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey("Base") }))
  147. {
  148. idNameCodes.Add(item);
  149. }
  150. _coreAPIHttpService.PushNotify(idNameCodes, $"{bizcode}_school", Constant.NotifyType_IES5_Management,
  151. new Dictionary<string, object> { { "tmdname", teacher.name }, { "schoolName", schoolInfo.name }, { "schoolId", $"{school}" }, { "tmdid", teacher.id } }, _option.Location, _configuration, _dingDing, _environment.ContentRootPath);
  152. }
  153. catch (Exception ae)
  154. {
  155. await _dingDing.SendBotMsg($"HiTA扫码加入学校失败!{ae.Message},{ae.StackTrace}", GroupNames.成都开发測試群組);
  156. }
  157. return (200, "ok", new { schoolTeacher.roles, schoolTeacher.status, school = $"{school}", schoolInfo.name, schoolInfo.picture });
  158. }
  159. /// <summary>
  160. /// 根据年级获取入学年
  161. /// </summary>
  162. /// <param name="semesterList"></param>
  163. /// <returns></returns>
  164. public static (List<KeyValuePair<int, int>> gradeYear, HashSet<int> years) GetYears(School school, string periodId, IEnumerable<int> grades, long time = 0) {
  165. var date = DateTimeOffset.UtcNow;
  166. //2001-09-09 09:46:40
  167. if (time > 1000000000000)
  168. {
  169. date = DateTimeOffset.FromUnixTimeMilliseconds(time);
  170. }
  171. //年级算法
  172. var period = school.period.Find(x => x.id.Equals(periodId));
  173. int Day = date.Day;
  174. int Month = date.Month;
  175. int Year = date.Year;
  176. int start = int.Parse($"{Year}0901");
  177. var se = period.semesters.Find(x => x.start == 1);
  178. if (se == null)
  179. {
  180. se = period.semesters.First();
  181. }
  182. string sm = "09";
  183. string sd = "01";
  184. if (se != null)
  185. {
  186. sm = se.month >= 10 ? $"{se.month}" : $"0{se.month}";
  187. sd = se.day >= 10 ? $"{se.day}" : $"0{se.day}";
  188. start = int.Parse($"{Year}{sm}{sd}");
  189. }
  190. int curr = int.Parse(date.ToString("yyyyMMdd"));
  191. //新学年开学时间大于当前时间,计算年级需要减1 20220901-20220408 > 0 则当前20220408是2021年入学的,
  192. //当前时间大于新学年开学时间,计算年级则不需要 20220901-20221203 < 1 则当前20221203是2022年入学的,
  193. //20230901-20230101 > 0 则当前20230101是2022年入学的,
  194. int dis = start - curr;
  195. List<int > years= new List<int>();
  196. List<KeyValuePair<int, int>> gradeYear = new List<KeyValuePair<int, int>>();
  197. foreach (int grade in grades) {
  198. int year = 0;
  199. if (dis > 0)
  200. {
  201. year = Year - grade -1;
  202. }
  203. else {
  204. year= Year - grade ;
  205. }
  206. years.Add(year);
  207. gradeYear.Add(new KeyValuePair<int, int>(grade,year ));
  208. }
  209. return (gradeYear,years.ToHashSet());
  210. }
  211. /// <summary>
  212. /// 根据年份获取年级,只返回time 或当前时间以前入学的学生年级。
  213. /// </summary>
  214. /// <param name="semesterList"></param>
  215. /// <returns></returns>
  216. public static (List<KeyValuePair<int, string>> yearGrade,HashSet<string> grades) GetGrades(School school,string periodId , IEnumerable<int> years,long time =0) {
  217. var date = DateTimeOffset.UtcNow;
  218. //2001-09-09 09:46:40
  219. if (time > 1000000000000) {
  220. date = DateTimeOffset.FromUnixTimeMilliseconds(time);
  221. }
  222. //年级算法
  223. var period = school.period.Find(x => x.id.Equals(periodId));
  224. int? Count = period?.grades?.Count;
  225. List<KeyValuePair<int, string>> yearGrades = new List<KeyValuePair<int, string>>();
  226. if (Count.HasValue)
  227. {
  228. int Day = date.Day;
  229. int Month = date.Month;
  230. int Year = date.Year;
  231. int start = int.Parse($"{Year}0901");
  232. var se = period.semesters.Find(x => x.start == 1);
  233. if (se == null)
  234. {
  235. se = period.semesters.First();
  236. }
  237. string sm = "09";
  238. string sd = "01";
  239. if (se != null)
  240. {
  241. sm = se.month >= 10 ? $"{se.month}" : $"0{se.month}";
  242. sd = se.day >= 10 ? $"{se.day}" : $"0{se.day}";
  243. start = int.Parse($"{Year}{sm}{sd}");
  244. }
  245. int curr = int.Parse(date.ToString("yyyyMMdd"));
  246. //新学年开学时间大于当前时间,计算年级需要减1 20220901-20220408 > 0 则当前20220408是2021年入学的,
  247. //当前时间大于新学年开学时间,计算年级则不需要 20220901-20221203 < 1 则当前20221203是2022年入学的,
  248. //20230901-20230101 > 0 则当前20230101是2022年入学的,
  249. int dis = start - curr;
  250. foreach (int year in years)
  251. {
  252. if (int.Parse($"{year}{sm}{sd}") < curr) {
  253. int grade;
  254. if (dis > 0)
  255. {
  256. grade = Math.Abs((Year - year - 1)) % Count.Value;
  257. }
  258. else
  259. {
  260. grade = Math.Abs((Year - year)) % Count.Value;
  261. }
  262. yearGrades.Add(new KeyValuePair<int, string>(year, $"{grade}"));
  263. }
  264. }
  265. }
  266. return (yearGrades, yearGrades.Select(z=>z.Value).ToHashSet());
  267. }
  268. /// <summary>
  269. /// 根据指定时间或者当前时间获取学期信息
  270. /// </summary>
  271. /// <param name="semesterList"></param>
  272. /// <returns></returns>
  273. public static (Semester currSemester, int studyYear, DateTimeOffset date, DateTimeOffset nextSemester) GetSemester( Period period, string time =null) {
  274. //string time = "2023/05-11";
  275. DateTimeOffset date = default;
  276. if (!(!string.IsNullOrWhiteSpace(time) && DateTimeOffset.TryParse(time, out date)))
  277. {
  278. //date = DateTimeOffset.FromUnixTimeSeconds(1672506061);
  279. //date = DateTimeOffset.UtcNow;
  280. date = DateTimeOffset.FromUnixTimeSeconds(1662688000);
  281. }
  282. //年级算法
  283. //var period = school.period.Find(x => x.id.Equals(periodId));
  284. List<KeyValuePair<int, string>> yearGrades = new List<KeyValuePair<int, string>>();
  285. int Day = date.Day;
  286. int Month = date.Month;
  287. int Year = date.Year;
  288. string sm = "09";
  289. string sd = "01";
  290. int start = int.Parse($"{Year}{sm}{sd}");
  291. var se = period.semesters.Find(x => x.start == 1);
  292. if (se == null)
  293. {
  294. se = period.semesters.First();
  295. }
  296. if (se != null)
  297. {
  298. sm = se.month >= 10 ? $"{se.month}" : $"0{se.month}";
  299. sd = se.day >= 10 ? $"{se.day}" : $"0{se.day}";
  300. start = int.Parse($"{Year}{sm}{sd}");
  301. }
  302. int curr = int.Parse(date.ToString("yyyyMMdd"));
  303. //新学年开学时间大于当前时间,计算年级需要减1 20220901-20220408 > 0 则当前20220408是2021年入学的,
  304. //当前时间大于新学年开学时间,计算年级则不需要 20220901-20221203 < 0 则当前20221203是2022年入学的,
  305. //20230901-20230101 > 0 则当前20230101是2022年入学的,
  306. int dis = start - curr;
  307. //学年
  308. int studyYear= date.Year;
  309. if (dis > 0)
  310. {
  311. studyYear = Year - 1;
  312. }
  313. else
  314. {
  315. studyYear = Year;
  316. }
  317. Semester semester= null;
  318. //当前学期的下一个学期,默认加180天
  319. DateTimeOffset nextSemester = date.AddDays(180);
  320. var sortSemesters= SortSemester(period.semesters);
  321. int len = sortSemesters.Count;
  322. for (int i = 0; i < len; i++) {
  323. int next= i + 1;
  324. if (next>= len)
  325. {
  326. semester = sortSemesters[i];
  327. nextSemester = DateTimeOffset.Parse($"{studyYear+1}/{sortSemesters[0].month}/{sortSemesters[0].day}");
  328. break;
  329. }
  330. else {
  331. var fdate= DateTimeOffset.Parse($"{studyYear}/{sortSemesters[i].month}/{sortSemesters[i].day}");
  332. long lf = fdate.ToUnixTimeSeconds();
  333. var ndate = DateTimeOffset.Parse($"{studyYear}/{sortSemesters[next].month}/{sortSemesters[next].day}");
  334. long ln = ndate.ToUnixTimeSeconds();
  335. //代表有跨年
  336. if (lf > ln) {
  337. ndate = DateTimeOffset.Parse($"{studyYear+1}/{sortSemesters[next].month}/{sortSemesters[next].day}");
  338. ln = ndate.ToUnixTimeSeconds();
  339. }
  340. var cdate = DateTimeOffset.Parse($"{Year}/{Month}/{Day}");
  341. long lc = fdate.ToUnixTimeSeconds();
  342. if (lc >= lf && lc < ln) {
  343. semester = sortSemesters[i];
  344. nextSemester = DateTimeOffset.Parse($"{studyYear}/{sortSemesters[next].month}/{sortSemesters[next].day}");
  345. //nextSemester = sortSemesters[next];
  346. break;
  347. }
  348. //if (sortSemesters[i].month <=Month && sortSemesters[i].day >= Day && sortSemesters[next].month < Month && sortSemesters[next].day < Day)
  349. //{
  350. // semester = sortSemesters[i];
  351. // nextSemester = DateTimeOffset.Parse($"{studyYear}/{sortSemesters[next].month}/{sortSemesters[next].day}");
  352. // break;
  353. //}
  354. }
  355. }
  356. return (semester,studyYear,date, nextSemester);
  357. }
  358. /// <summary>
  359. /// 处理学期排序
  360. /// </summary>
  361. /// <param name="semesterList"></param>
  362. /// <returns></returns>
  363. public static List<Semester> SortSemester(List<Semester> semesterList)
  364. {
  365. int Year = DateTimeOffset.UtcNow.Year;
  366. List<KeyValuePair<int, Semester>> pairs = new List<KeyValuePair<int, Semester>>();
  367. semesterList.ForEach(se => {
  368. string sm = se.month >= 10 ? $"{se.month}" : $"0{se.month}";
  369. string sd = se.day >= 10 ? $"{se.day}" : $"0{se.day}";
  370. int order = int.Parse($"{Year}{sm}{sd}");
  371. pairs.Add(new KeyValuePair<int, Semester>(order, se));
  372. });
  373. var orderPairs = pairs.OrderBy(z => z.Key);
  374. semesterList = orderPairs.Select(z => z.Value).ToList();
  375. int startIndex = semesterList.FindIndex(z => z.start == 1);
  376. if (startIndex == -1)
  377. {
  378. //未设置学期的情况默认9月份开始的。
  379. startIndex = semesterList.FindIndex(z => z.month == 9);
  380. //如果还未找到,就以排序结果为准
  381. if (startIndex == -1)
  382. {
  383. startIndex = 0;
  384. }
  385. }
  386. if (startIndex > 0)
  387. {
  388. List<Semester> before = semesterList.Take(startIndex).ToList();
  389. List<Semester> after = semesterList.Skip(startIndex).ToList();
  390. semesterList = new List<Semester>();
  391. semesterList.AddRange(after);
  392. semesterList.AddRange(before);
  393. return semesterList;
  394. }
  395. else
  396. {
  397. return semesterList;
  398. }
  399. }
  400. /// <summary>
  401. /// 校验
  402. /// </summary>
  403. /// <param name="courseTaskInsert"></param>
  404. public static CourseTaskChanged CheckCourseTaskInsertOrChanged(string grant_type, string scope , CourseTaskChanged courseTaskInsert,string school,Period period, List<CourseBase> courseBases, List<GroupListDto> groupListDtos, List<Room> rooms,
  405. List<SchoolTeacher> schoolTeachers,List<Teacher> teachers)
  406. {
  407. if (scope.Equals("school", StringComparison.OrdinalIgnoreCase))
  408. { //学年
  409. if (courseTaskInsert.year < 2000)
  410. {
  411. courseTaskInsert.invalidCode = 17;//学年应大于2000
  412. return courseTaskInsert;
  413. }
  414. //学期
  415. var semester = period.semesters.Where(z => z.id.Equals(courseTaskInsert.semesterId));
  416. if (!semester.Any())
  417. {
  418. courseTaskInsert.invalidCode = 16;//学期不存在
  419. return courseTaskInsert;
  420. }
  421. //教师
  422. var schoolteachers = schoolTeachers.Where(z => z.id.Equals(courseTaskInsert.teacherId));
  423. if (!schoolteachers.Any())
  424. {
  425. courseTaskInsert.invalidCode = 2;//教师不存在
  426. return courseTaskInsert;
  427. }
  428. }
  429. else {
  430. //教师
  431. var teachersbase = teachers.Where(z => z.id.Equals(courseTaskInsert.teacherId));
  432. if (!teachersbase.Any())
  433. {
  434. courseTaskInsert.invalidCode = 2;//教师不存在
  435. return courseTaskInsert;
  436. }
  437. }
  438. //课程
  439. var courseBase = courseBases.Where(z => z.id.Equals(courseTaskInsert.courseId));
  440. if (!courseBase.Any())
  441. {
  442. courseTaskInsert.invalidCode = 1;//课程不存在
  443. return courseTaskInsert;
  444. }
  445. //名单
  446. var groupList = groupListDtos.Where(z => z.id.Equals(courseTaskInsert.groupId) && z.type.Equals(courseTaskInsert.type));
  447. if (!groupList.Any())
  448. {
  449. courseTaskInsert.invalidCode = 3;//名单不存在
  450. return courseTaskInsert;
  451. }
  452. else {
  453. if (scope.Equals("private", StringComparison.OrdinalIgnoreCase))
  454. {
  455. if (!groupList.First().scope.Equals("private", StringComparison.OrdinalIgnoreCase) && !string.IsNullOrWhiteSpace(school) && school.Equals(groupList.First().school))
  456. {
  457. courseTaskInsert.school = school;
  458. }
  459. }
  460. else {
  461. courseTaskInsert.school = school;
  462. }
  463. }
  464. //教室
  465. if (!string.IsNullOrWhiteSpace(courseTaskInsert.roomId))
  466. {
  467. var room = rooms.Where(z => z.id.Equals(courseTaskInsert.roomId));
  468. if (!room.Any())
  469. {
  470. courseTaskInsert.invalidCode = 4;//教室不存在
  471. return courseTaskInsert;
  472. }
  473. }
  474. if (grant_type.Equals("change-scheduleTask", StringComparison.OrdinalIgnoreCase)) {
  475. if (!string.IsNullOrWhiteSpace(courseTaskInsert.teacherIdChanged) || (!string.IsNullOrWhiteSpace(courseTaskInsert.groupIdChanged) && !string.IsNullOrWhiteSpace(courseTaskInsert.typeChanged)))
  476. {
  477. if (!string.IsNullOrWhiteSpace(courseTaskInsert.teacherIdChanged)) {
  478. if (scope.Equals("school", StringComparison.OrdinalIgnoreCase))
  479. {
  480. //教师
  481. var schoolteachers = schoolTeachers.Where(z => z.id.Equals(courseTaskInsert.teacherIdChanged));
  482. if (!schoolteachers.Any())
  483. {
  484. courseTaskInsert.invalidCode = 19;//教师不存在
  485. return courseTaskInsert;
  486. }
  487. }
  488. else {
  489. //教师
  490. var teachersbase = teachers.Where(z => z.id.Equals(courseTaskInsert.teacherIdChanged));
  491. if (!teachersbase.Any())
  492. {
  493. courseTaskInsert.invalidCode = 19;//教师不存在
  494. return courseTaskInsert;
  495. }
  496. }
  497. }
  498. if (!string.IsNullOrWhiteSpace(courseTaskInsert.groupIdChanged) && !string.IsNullOrWhiteSpace(courseTaskInsert.typeChanged)) {
  499. //名单
  500. var groupListChanged = groupListDtos.Where(z => z.id.Equals(courseTaskInsert.groupIdChanged) && z.type.Equals(courseTaskInsert.typeChanged));
  501. if (!groupListChanged.Any())
  502. {
  503. courseTaskInsert.invalidCode = 18;//名单不存在
  504. return courseTaskInsert;
  505. }
  506. else
  507. {
  508. if (scope.Equals("private", StringComparison.OrdinalIgnoreCase))
  509. {
  510. if (!groupListChanged.First().scope.Equals("private", StringComparison.OrdinalIgnoreCase) && !string.IsNullOrWhiteSpace(school) && school.Equals(groupList.First().school))
  511. {
  512. courseTaskInsert.school = school;
  513. }
  514. }
  515. else
  516. {
  517. courseTaskInsert.school = school;
  518. }
  519. }
  520. }
  521. }
  522. else {
  523. courseTaskInsert.invalidCode = 17;
  524. }
  525. }
  526. courseTaskInsert.invalidCode = 0;
  527. return courseTaskInsert;
  528. }
  529. 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) {
  530. List<Class> school_classes = new List<Class>();
  531. List<Class> graduate_classes = new List<Class>();
  532. try
  533. {
  534. var client = _azureCosmos.GetCosmosClient();
  535. //取得班级
  536. int nowYear = DateTimeOffset.UtcNow.Year;
  537. int nowMonth = DateTimeOffset.UtcNow.Month;
  538. int nowDay = DateTimeOffset.UtcNow.Day;
  539. //因为修改年级,而导致取消的
  540. List<Class> cancel_graduate_classes = new List<Class>();
  541. string sql = $"SELECT value c FROM c where (c.graduate = 0 or IS_DEFINED(c.graduate) = false) ";
  542. if (periodIds.IsNotEmpty())
  543. {
  544. sql = $"SELECT value c FROM c where c.periodId in ({string.Join(",", periodIds.Select(x => $"'{x}'"))}) ";
  545. }
  546. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<Class>
  547. //(queryText: $"SELECT value c FROM c where (c.graduate = 0 or IS_DEFINED(c.graduate) = false)",
  548. (queryText: sql,
  549. requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Class-{school_base.id}") }))
  550. {
  551. bool isgraduate = false;
  552. if (!string.IsNullOrWhiteSpace(item.periodId))
  553. {
  554. var period = school_base.period.Find(x => x.id.Equals(item.periodId));
  555. if (period != null)
  556. {
  557. var gradeCount = period.grades.Count();
  558. //2022-2016=6(待判断月份日期是否是毕业) 2022-2017=5(未毕业)
  559. if (nowYear - item.year > gradeCount)
  560. {
  561. isgraduate = true;
  562. }
  563. else if (nowYear - item.year == gradeCount)
  564. {
  565. var semester = period.semesters.Find(x => x.start == 1);
  566. if (semester != null)
  567. {
  568. if (nowMonth > semester.month)
  569. {
  570. isgraduate = true;
  571. }
  572. else if (nowMonth == semester.month)
  573. {
  574. if (nowDay >= semester.day)
  575. {
  576. isgraduate = true;
  577. }
  578. else
  579. {
  580. }
  581. }
  582. else { isgraduate = false; }
  583. }
  584. }
  585. else
  586. {
  587. isgraduate = false;
  588. }
  589. }
  590. }
  591. if (isgraduate)
  592. {
  593. graduate_classes.Add(item);
  594. }
  595. else
  596. {
  597. if (item.graduate == 1)
  598. {
  599. item.graduate = 0;
  600. cancel_graduate_classes.Add(item);
  601. }
  602. school_classes.Add(item);
  603. }
  604. }
  605. if (graduate_classes.Any() || cancel_graduate_classes.Any())
  606. {
  607. if (waite == 0)
  608. {
  609. _ = _httpTrigger.RequestHttpTrigger(new { graduate_classes = graduate_classes, cancel_graduate_classes = cancel_graduate_classes, schoolId = $"{school_base.id}" }, _option.Location, "graduate-change");
  610. }
  611. else
  612. {
  613. await _httpTrigger.RequestHttpTrigger(new { graduate_classes = graduate_classes, cancel_graduate_classes = cancel_graduate_classes, schoolId = $"{school_base.id}" }, _option.Location, "graduate-change");
  614. }
  615. }
  616. } catch (Exception ex) {
  617. await _dingDing.SendBotMsg($"{_option.Location},{ex.Message}\n{ex.StackTrace}", GroupNames.醍摩豆服務運維群組);
  618. }
  619. return (school_classes,graduate_classes);
  620. }
  621. public static async Task<object> DoScsApiSchool(HttpTrigger _httpTrigger,Dictionary<string,object> dict, Option _option, List<IdNameCode> ignore,
  622. string areaId,string city,string dist, AzureStorageFactory _azureStorage ,DingDing _dingDing, IWebHostEnvironment _environment
  623. )
  624. {
  625. List<ScSchool> tbschools = null;
  626. List<ScSchool> matchSchools = null;
  627. List<ScSchool> schools = null;
  628. List<ScSchool> saveschools = null;
  629. var table = _azureStorage.GetCloudTableClient().GetTableReference("ScYxpt");
  630. // 5.3.1.18根据机构ID、项目ID、子项目ID返回学校列表
  631. ( int status, string json) = await _httpTrigger.RequestHttpTrigger(dict, _option.Location, "GetSchoolList");
  632. if (status == 200)
  633. {
  634. schools = json.ToObject<List<ScSchool>>(new JsonSerializerOptions { PropertyNameCaseInsensitive = false });
  635. if (ignore.IsNotEmpty())
  636. {
  637. matchSchools = schools.FindAll(x => ignore.Select(y => y.name).Contains(x.schoolname));
  638. if (matchSchools.IsNotEmpty())
  639. {
  640. if (matchSchools.Count != ignore.Count)
  641. {
  642. var matched = matchSchools.Select(x => x.schoolname);
  643. var unmatch = ignore.Select(y => y.name).Except(matched);
  644. List<string> scschoolUnmatch = schools.Select(y => y.schoolname).Except(matched).ToList();
  645. if (scschoolUnmatch.IsNotEmpty())
  646. {
  647. return new { matched, unmatch, scschoolUnmatch };
  648. }
  649. }
  650. else
  651. {
  652. matchSchools.ForEach(x => {
  653. var exschool = ignore.Find(y => y.name.Equals(x.schoolname));
  654. if (exschool != null)
  655. {
  656. x.schoolCode = exschool.id;
  657. x.areaId = $"{areaId}";
  658. x.city = $"{city}";
  659. x.dist = $"{dist}";
  660. }
  661. });
  662. }
  663. }
  664. else
  665. {
  666. return new { unmatch = ignore, schools };
  667. }
  668. }
  669. //数据校验
  670. tbschools = await table.FindListByDict<ScSchool>(new Dictionary<string, object>() { { "PartitionKey", "ScSchool" } });
  671. if (tbschools.IsNotEmpty())
  672. {
  673. var a = tbschools.Select(y => $"{y.RowKey}").ToList();
  674. saveschools = schools.Where(x => !a.Exists(z => z.Equals($"{x.schoolid}"))).ToList();
  675. List<SchoolData> schoolDatas = new List<SchoolData>();
  676. saveschools.ForEach(x =>
  677. {
  678. x.RowKey = $"{x.schoolid}";
  679. x.PartitionKey = "ScSchool";
  680. x.areaId = $"{areaId}";
  681. x.city = $"{city}";
  682. x.dist = $"{dist}";
  683. if (string.IsNullOrEmpty(x.schoolCode))
  684. {
  685. if (string.IsNullOrEmpty(x.schoolCode))
  686. {
  687. schoolDatas.Add(new SchoolData { uid = $"{x.schoolid}", province = "四川省", city = $"{city}", name = x.schoolname });
  688. }
  689. }
  690. });
  691. schoolDatas = await SchoolService.GenerateSchoolCode(schoolDatas, _dingDing, _environment);
  692. saveschools.ForEach(x => {
  693. var schoolData = schoolDatas.Find(y => y.uid.Equals($"{x.schoolid}"));
  694. if (schoolData != null && !string.IsNullOrEmpty(schoolData.id))
  695. {
  696. x.schoolCode = schoolData.id;
  697. x.areaId = $"{areaId}";
  698. x.city = $"{city}";
  699. x.dist = $"{dist}";
  700. }
  701. });
  702. saveschools.RemoveAll(x => string.IsNullOrEmpty(x.schoolCode));
  703. saveschools.RemoveAll(x => tbschools.FindAll(z => !string.IsNullOrEmpty(z.schoolCode)).Exists(y => y.schoolCode.Equals(x.schoolCode)));
  704. saveschools = await table.SaveAll(saveschools);
  705. }
  706. else
  707. {
  708. List<SchoolData> schoolDatas = new List<SchoolData>();
  709. schools.ForEach(x =>
  710. {
  711. x.RowKey = $"{x.schoolid}";
  712. x.PartitionKey = "ScSchool";
  713. x.areaId = $"{areaId}";
  714. x.city = $"{city}";
  715. x.dist = $"{dist}";
  716. var a = ignore.Find(z => z.name.Equals(x.schoolname));
  717. if (a != null)
  718. {
  719. x.schoolCode = a.id;
  720. }
  721. else
  722. {
  723. if (string.IsNullOrEmpty(x.schoolCode))
  724. {
  725. schoolDatas.Add(new SchoolData { uid = $"{x.schoolid}", province = "四川省", city = $"{city}", name = x.schoolname });
  726. }
  727. }
  728. });
  729. schoolDatas = await SchoolService.GenerateSchoolCode(schoolDatas, _dingDing, _environment);
  730. schools.ForEach(x => {
  731. var schoolData = schoolDatas.Find(y => y.uid.Equals($"{x.schoolid}"));
  732. if (schoolData != null && !string.IsNullOrEmpty(schoolData.id))
  733. {
  734. x.schoolCode = schoolData.id;
  735. x.areaId = $"{areaId}";
  736. x.city = $"{city}";
  737. x.dist = $"{dist}";
  738. }
  739. });
  740. schools.RemoveAll(x => string.IsNullOrEmpty(x.schoolCode));
  741. saveschools = await table.SaveOrUpdateAll(schools);
  742. }
  743. }
  744. return null;
  745. }
  746. public static async Task<List<SchoolData>> GenerateSchoolCode(List<SchoolData> schools, DingDing _dingDing, IWebHostEnvironment _environment)
  747. {
  748. string path = $"{_environment.ContentRootPath}/JsonFile/Core/region.json";
  749. //var schools = jsonstr.ToObject<JsonElement>().GetProperty("schools").ToObject<List<SchoolData>>();
  750. schools = schools.Where((x, i) => schools.FindIndex(n => n.name.Equals(x.name)) == i).ToList();
  751. StreamReader streamReader = new StreamReader(new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite), Encoding.UTF8);
  752. StringBuilder stringBuilder = new StringBuilder();
  753. string text;
  754. while ((text = streamReader.ReadLine()) != null)
  755. {
  756. stringBuilder.Append(text.ToString());
  757. }
  758. streamReader.Close();
  759. string input = stringBuilder.ToString();
  760. List<Region> region = input.ToObject<List<Region>>();
  761. try
  762. {
  763. schools.ForEach(x =>
  764. {
  765. x.province = ChineseConverter.Convert(x.province, ChineseConversionDirection.TraditionalToSimplified);
  766. if (String.IsNullOrEmpty(x.city))
  767. {
  768. x.city = x.province;
  769. }
  770. else
  771. {
  772. x.city = ChineseConverter.Convert(x.city, ChineseConversionDirection.TraditionalToSimplified);
  773. }
  774. x.name = ChineseConverter.Convert(x.name, ChineseConversionDirection.TraditionalToSimplified);
  775. comeRemoveStr.ForEach(c => { x.province = x.province.Replace(c, ""); });
  776. prvcRemoveStr.ForEach(c => { x.province = x.province.Replace(c, ""); });
  777. var province = region.Find(r => r.name.Contains(x.province));
  778. string tmpprovince = x.province;
  779. string tmpcity = x.city;
  780. if (province != null)
  781. {
  782. x.province = province.name;
  783. comeRemoveStr.ForEach(c => { x.city = x.city.Replace(c, ""); });
  784. cityRemoveStr.ForEach(c => { x.city = x.city.Replace(c, ""); });
  785. nationRemoveStr.ForEach(c => { x.city = x.city.Replace(c, ""); });
  786. tmpcity = x.city;
  787. var city = province.children.Find(r => r.name.Contains(x.city));
  788. if (city == null)
  789. {
  790. city = province.children.Find(r => r.name.Contains(x.city));
  791. if (city == null)
  792. {
  793. city = province.children.SelectMany(x => x.children).ToList().Find(r => r.name.Contains(x.city));
  794. }
  795. }
  796. if (city != null)
  797. {
  798. x.city = city.name;
  799. }
  800. }
  801. string name = x.name;
  802. //去除冗余的学校信息,只保留地名和校名关键信息
  803. schemoveStr.ForEach(str =>
  804. {
  805. name = name.Replace(str, "");
  806. });
  807. string[] names = name.Split("(");
  808. if (names.Length > 1)
  809. {
  810. name = $"{names[0]}";
  811. for (int index = 1; index < names.Length; index++)
  812. {
  813. name = $"{name}{names[index].Substring(0, 1)}";
  814. var afnames = names[index].Split(")");
  815. if (afnames.Length > 1)
  816. {
  817. for (int i = 1; i < afnames.Length; i++)
  818. {
  819. name = $"{name}{afnames[i]}";
  820. }
  821. }
  822. }
  823. }
  824. names = name.Split("(");
  825. if (names.Length > 1)
  826. {
  827. name = $"{names[0]}";
  828. for (int index = 1; index < names.Length; index++)
  829. {
  830. name = $"{name}{names[index].Substring(0, 1)}";
  831. var afnames = names[index].Split(")");
  832. if (afnames.Length > 1)
  833. {
  834. for (int i = 1; i < afnames.Length; i++)
  835. {
  836. name = $"{name}{afnames[i]}";
  837. }
  838. }
  839. }
  840. }
  841. name = Regex.Replace(name, "[ \\[ \\] \\^ \\-|()【】/' {}_*×――(^)$%~!@#$…&%¥—+=<>《》!!???::•`·、。,;,.;\"‘’“”-]", " ");
  842. //检查是否有英文
  843. if (Regex.Matches(name, "[a-zA-Z]").Count > 0)
  844. {
  845. var array = Regex.Split(name, "\\s+", RegexOptions.IgnoreCase);
  846. StringBuilder tmpname = new StringBuilder();
  847. if (array.Length > 1)
  848. {
  849. foreach (var item in array)
  850. {
  851. var arr = item.Select(x => $"{x}");
  852. int index = 0;
  853. foreach (var stra in arr)
  854. {
  855. if (Regex.Matches(stra, "[a-zA-Z]").Count > 0)
  856. {
  857. if (index == 1|| index == 0)
  858. {
  859. tmpname.Append(stra);
  860. }
  861. else
  862. {
  863. }
  864. }
  865. else
  866. {
  867. tmpname.Append(stra);
  868. }
  869. index++;
  870. }
  871. }
  872. }
  873. else
  874. {
  875. var arr = name.Select(x => $"{x}");
  876. int index = 0;
  877. foreach (var stra in arr)
  878. {
  879. if (Regex.Matches(stra, "[A-Z]").Count > 0)
  880. {
  881. tmpname.Append(stra);
  882. }
  883. else if (Regex.Matches(stra, "[a-z]").Count > 0)
  884. {
  885. }
  886. else
  887. {
  888. tmpname.Append(stra);
  889. }
  890. index++;
  891. }
  892. }
  893. name = tmpname.ToString();
  894. }
  895. name = Regex.Replace(name, @"\s", "");
  896. name = name.Replace("\\", "");
  897. if (name.Length > 6)
  898. {
  899. //新区,高新,产业等非新政单位
  900. areaRemoveStr.ForEach(str =>
  901. {
  902. name = name.Replace(str.Key, str.Value);
  903. });
  904. //去除冗余的学校信息,只保留地名和校名关键信息
  905. schooRemoveStr.ForEach(str =>
  906. {
  907. name = name.Replace(str, "");
  908. });
  909. //替换民族信息词
  910. if (name.Length > 6)
  911. {
  912. nationRemoveStr.ForEach(str =>
  913. {
  914. name = name.Replace(str, "");
  915. });
  916. }
  917. //替换学校信息的副词
  918. if (name.Length > 6)
  919. {
  920. foreach (var str in schooReplaceStr)
  921. {
  922. if (name.Length <= 6)
  923. {
  924. break;
  925. }
  926. name = name.Replace(str.Key, str.Value);
  927. }
  928. }
  929. //替换省简称信息词
  930. string tmpname = name;
  931. if (name.Length > 6)
  932. {
  933. proReplaceStr.ForEach(str =>
  934. {
  935. name = name.Replace(str.Key, str.Value);
  936. });
  937. //如果替换后仍然大于6位,则取消,直接替换省份完整的词
  938. if (name.Length > 6)
  939. {
  940. name = tmpname.Replace(tmpprovince, "");
  941. }
  942. }
  943. //替换城市信息词
  944. if (name.Length > 6)
  945. {
  946. name = name.Replace(tmpcity, "");
  947. }
  948. }
  949. int len = name.Length;
  950. if (len > 6)
  951. {
  952. foreach (var str in afterSchoolRm)
  953. {
  954. if (name.Length <= 6)
  955. {
  956. break;
  957. }
  958. name = name.Replace(str, "");
  959. }
  960. }
  961. len = name.Length;
  962. if (len >= 6)
  963. {
  964. name = $"{name.Substring(0, 1)}{name.Substring(len - 5, 3)}{name.Substring(len - 2, 2)}";
  965. }
  966. //if (len <= 7 && len > 6)
  967. //{
  968. // name = name.Substring(len - 6);
  969. //}
  970. if (len <= 4)
  971. {
  972. name = $"{x.city.Substring(0, 2)}{name}";
  973. }
  974. if (len == 5)
  975. {
  976. name = $"{x.city.Substring(0, 1)}{name}";
  977. }
  978. string code = ToFirstPinYin(name);
  979. x.aname = name;
  980. x.id = code;
  981. });
  982. }
  983. catch (Exception ex)
  984. {
  985. await _dingDing.SendBotMsg($"{ex.Message}\n{ex.StackTrace}\n", GroupNames.醍摩豆服務運維群組);
  986. }
  987. string a_z = "abcdefghijklmnopqrstuvwxyz";
  988. 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();
  989. data.ForEach(x =>
  990. {
  991. if (x.count > 1)
  992. {
  993. var first = x.list.First();
  994. var same = x.list.Skip(1).Take(x.count - 1).ToList();
  995. var fs = first.aname.Select(x => x).ToArray();
  996. same.ForEach(z =>
  997. {
  998. var sp = z.aname.Select(x => x).ToArray();
  999. int len = sp.Length;
  1000. if (sp.Length >= fs.Length)
  1001. {
  1002. len = fs.Count();
  1003. }
  1004. for (int index = 0; index < len; index++)
  1005. {
  1006. if (!$"{sp[index]}".Equals($"{fs[index]}"))
  1007. {
  1008. short st = ChineseChar.GetStrokeNumber(sp[index]);
  1009. if (st > 0)
  1010. {
  1011. var ins = st % 27;
  1012. var insch = a_z[ins];
  1013. if (z.aname.EndsWith($"{insch}"))
  1014. {
  1015. ins = (st + 1) % 27;
  1016. insch = a_z[ins];
  1017. }
  1018. z.id = z.id.Insert(index, $"{insch}").Remove(index + 1, 1);
  1019. break;
  1020. }
  1021. }
  1022. }
  1023. });
  1024. }
  1025. });
  1026. 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();
  1027. dataz.ForEach(x =>
  1028. {
  1029. if (x.count > 1)
  1030. {
  1031. var first = x.list.First();
  1032. var same = x.list.Skip(1).Take(x.count - 1).ToList();
  1033. var fs = first.aname.Select(x => x).ToArray();
  1034. same.ForEach(z =>
  1035. {
  1036. var sp = z.aname.Select(x => x).ToArray();
  1037. int len = sp.Length;
  1038. if (sp.Length >= fs.Length)
  1039. {
  1040. len = fs.Count();
  1041. }
  1042. for (int index = 0; index < len; index++)
  1043. {
  1044. if (!$"{sp[index]}".Equals($"{fs[index]}"))
  1045. {
  1046. var spstr = ToPinYin($"{sp[index]}" );
  1047. var fsstr = ToPinYin($"{fs[index]}");
  1048. var insch= spstr.Select(x => $"{x}").Except( fsstr.Select(z => $"{z}"));
  1049. if (insch != null && insch.Count() > 0) {
  1050. if (index > 1)
  1051. {
  1052. z.id = z.id.Insert(index - 1, insch.First()).Remove(index, 1);
  1053. }
  1054. else if (index == 1)
  1055. {
  1056. z.id = z.id.Insert(index, insch.First()).Remove(index - 1, 1);
  1057. }
  1058. }
  1059. }
  1060. }
  1061. });
  1062. }
  1063. });
  1064. 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();
  1065. Random random = new Random();
  1066. List<SchoolData> list = new List<SchoolData>();
  1067. dataa.ForEach(x =>
  1068. {
  1069. if (x.count > 1)
  1070. {
  1071. x.list.ForEach(y => {
  1072. int r= random.Next(25);
  1073. int index= random.Next(5);
  1074. y.id = y.id.Insert(index - 1, $"{a_z[r]}").Remove(index, 1);
  1075. });
  1076. }
  1077. list.AddRange(x.list);
  1078. });
  1079. return list;
  1080. }
  1081. public static string ToPinYin(string val)
  1082. {
  1083. StringBuilder sb = new StringBuilder();
  1084. val.ToCharArray().ToList().ForEach(x =>
  1085. {
  1086. if (('a' <= x && x <= 'z') || ('A' <= x && x <= 'Z') || ('0' <= x && x <= '9'))
  1087. {
  1088. sb.Append(x);
  1089. }
  1090. else
  1091. {
  1092. try
  1093. {
  1094. ChineseChar cc = new ChineseChar(x);
  1095. if (cc.Pinyins.Count > 0 && cc.Pinyins[0].Length > 0)
  1096. {
  1097. sb.Append(cc.Pinyins[0]);
  1098. }
  1099. }
  1100. catch (Exception ex)
  1101. {
  1102. }
  1103. }
  1104. });
  1105. return sb.ToString().ToLower();
  1106. }
  1107. public static string ToFirstPinYin(string val)
  1108. {
  1109. StringBuilder sb = new StringBuilder();
  1110. val.ToCharArray().ToList().ForEach(x =>
  1111. {
  1112. if (('a' <= x && x <= 'z') || ('A' <= x && x <= 'Z') || ('0' <= x && x <= '9'))
  1113. {
  1114. sb.Append(x);
  1115. }
  1116. else
  1117. {
  1118. try
  1119. {
  1120. ChineseChar cc = new ChineseChar(x);
  1121. if (cc.Pinyins.Count > 0 && cc.Pinyins[0].Length > 0)
  1122. {
  1123. sb.Append(cc.Pinyins[0][0]);
  1124. }
  1125. }
  1126. catch (Exception ex)
  1127. {
  1128. }
  1129. }
  1130. });
  1131. return sb.ToString().ToLower();
  1132. }
  1133. public class SchoolData
  1134. {
  1135. public string province { get; set; }
  1136. public string id { get; set; }
  1137. public string name { get; set; }
  1138. public string city { get; set; }
  1139. public string aname { get; set; }
  1140. public string uid { get; set; }
  1141. }
  1142. public static List<string> comeRemoveStr = new List<string>() { "省", "市", "区", "州", "县", "旗", "盟", "自治" };
  1143. public static List<string> prvcRemoveStr = new List<string>() { "回族", "维吾尔", "壮族", };
  1144. public static List<string> cityRemoveStr = new List<string>() { "蒙古族", "地区", "壮族", "朝鲜族", "直辖" };
  1145. public static List<string> nationRemoveStr = new List<string> {
  1146. "维吾尔","回族", "自治", "满族", "蒙古", "壮族", "苗族" , "侗族", "瑶族",
  1147. "达斡尔","鄂温克","朝鲜","畲族","土家","各族","仫佬","毛南","羌族","彝族","仡佬","布依","水族",
  1148. "傣族","纳西","哈尼","拉祜","佤族","傈僳","独龙","怒族","白族","普米","固族","哈萨克","土族","撒拉","景颇","族"
  1149. };
  1150. public static List<string> schemoveStr = new List<string> { "第", "校区", "部", "楼", "与", "学校", "校园", };
  1151. public static List<string> schooRemoveStr = new List<string>() {"省", "市", "区", "州", "县", "旗", "盟","办事处","街道", "自治",
  1152. "镇","村","乡","街","路","站","馆"
  1153. };
  1154. public static List<string> afterSchoolRm = new List<string>() {"校","园","院", "湾","峡","沟","山","庄",
  1155. "堡","江","屯","岭","溪","河","桥","营","铺","坡","寨","场","湖","巷","集","关","庙","寺","矿","塘"};
  1156. public static List<KeyValuePair<string, string>> areaRemoveStr = new List<KeyValuePair<string, string>>{
  1157. new KeyValuePair<string, string>("高新技术产业开发区", "高") ,
  1158. new KeyValuePair<string, string>("经济技术开发区", "经") ,
  1159. new KeyValuePair<string, string>("国际", "际") ,
  1160. new KeyValuePair<string, string>("中国", ""),
  1161. new KeyValuePair<string, string>("国家", ""),
  1162. new KeyValuePair<string, string>("新区", ""),
  1163. new KeyValuePair<string, string>("高新", "高"),
  1164. new KeyValuePair<string, string>("园区", ""),
  1165. new KeyValuePair<string, string>("产业", "产"),
  1166. new KeyValuePair<string, string>("经济", "经"),
  1167. new KeyValuePair<string, string>("开发", "开"),
  1168. new KeyValuePair<string, string>("中心", "") ,
  1169. new KeyValuePair<string, string>("集团", "") ,
  1170. new KeyValuePair<string, string>("公司", "") ,
  1171. new KeyValuePair<string, string>("有限", "") ,
  1172. new KeyValuePair<string, string>("股份", "") ,
  1173. new KeyValuePair<string, string>("服务", "") ,
  1174. new KeyValuePair<string, string>("基地", "") ,
  1175. new KeyValuePair<string, string>("社区", "社") ,
  1176. new KeyValuePair<string, string>("职工", "") ,
  1177. new KeyValuePair<string, string>("专修", ""),
  1178. new KeyValuePair<string, string>("实验", "验"),
  1179. new KeyValuePair<string, string>("完全", ""),
  1180. new KeyValuePair<string, string>("一贯制", "") ,
  1181. new KeyValuePair<string, string>("联办", ""),
  1182. new KeyValuePair<string, string>("联合", ""),
  1183. new KeyValuePair<string, string>("完小", "小"),
  1184. new KeyValuePair<string, string>("义务", ""),
  1185. new KeyValuePair<string, string>("基础", ""),
  1186. new KeyValuePair<string, string>("制造", "")
  1187. };
  1188. public static List<KeyValuePair<string, string>> schooReplaceStr = new List<KeyValuePair<string, string>> {
  1189. new KeyValuePair<string, string>("分校", "分") ,
  1190. new KeyValuePair<string, string>("分园", "分") ,
  1191. new KeyValuePair<string, string>("分院", "分") ,
  1192. new KeyValuePair<string, string>("藏文", "藏"),
  1193. new KeyValuePair<string, string>("职业技术学院", "职") ,
  1194. new KeyValuePair<string, string>("创新创业", "创") ,
  1195. new KeyValuePair<string, string>("就业创业", "就") ,
  1196. new KeyValuePair<string, string>("应用核技术", "核") ,
  1197. new KeyValuePair<string, string>("职业学院", "职") ,
  1198. new KeyValuePair<string, string>("幼儿园", "幼") ,
  1199. new KeyValuePair<string, string>("幼儿", "幼") ,
  1200. new KeyValuePair<string, string>("小学", "小") ,
  1201. new KeyValuePair<string, string>("中学", "中") ,
  1202. new KeyValuePair<string, string>("中等", "中") ,
  1203. new KeyValuePair<string, string>("双语", "双") ,
  1204. new KeyValuePair<string, string>("初中", "初") ,
  1205. new KeyValuePair<string, string>("初级", "初") ,
  1206. new KeyValuePair<string, string>("广播", "广"),
  1207. new KeyValuePair<string, string>("高中", "高") ,
  1208. new KeyValuePair<string, string>("高级", "高") ,
  1209. new KeyValuePair<string, string>("大学", "大") ,
  1210. new KeyValuePair<string, string>("学院", "院") ,
  1211. new KeyValuePair<string, string>("综合", "综") ,
  1212. new KeyValuePair<string, string>("职业", "职") ,
  1213. new KeyValuePair<string, string>("技术", "技") ,
  1214. new KeyValuePair<string, string>("专科", "专") ,
  1215. new KeyValuePair<string, string>("外国语", "外") ,
  1216. new KeyValuePair<string, string>("九年", "九") ,
  1217. new KeyValuePair<string, string>("五年", "五") ,
  1218. new KeyValuePair<string, string>("年制", "制") ,
  1219. new KeyValuePair<string, string>("高等", "高") ,
  1220. new KeyValuePair<string, string>("院校", "院") ,
  1221. new KeyValuePair<string, string>("初等", "小") ,
  1222. new KeyValuePair<string, string>("附属", "附"),
  1223. new KeyValuePair<string, string>("寄宿制", "寄") ,
  1224. new KeyValuePair<string, string>("寄宿", "寄") ,
  1225. new KeyValuePair<string, string>("卫生", "卫") ,
  1226. new KeyValuePair<string, string>("创业", "创") ,
  1227. new KeyValuePair<string, string>("继续", "继") ,
  1228. new KeyValuePair<string, string>("开放", "开") ,
  1229. new KeyValuePair<string, string>("技师", "技") ,
  1230. new KeyValuePair<string, string>("成人", "成") ,
  1231. new KeyValuePair<string, string>("教育", "教") ,
  1232. new KeyValuePair<string, string>("科技", "科") ,
  1233. new KeyValuePair<string, string>("行政", "政") ,
  1234. new KeyValuePair<string, string>("生物", "生") ,
  1235. new KeyValuePair<string, string>("美术", "美") ,
  1236. new KeyValuePair<string, string>("设计", "设") ,
  1237. new KeyValuePair<string, string>("传播", "传") ,
  1238. new KeyValuePair<string, string>("计算机", "算") ,
  1239. new KeyValuePair<string, string>("土木", "土") ,
  1240. new KeyValuePair<string, string>("石油", "油") ,
  1241. new KeyValuePair<string, string>("科普", "普") ,
  1242. new KeyValuePair<string, string>("电信", "信") ,
  1243. new KeyValuePair<string, string>("联合", "联") ,
  1244. new KeyValuePair<string, string>("电商", "商") ,
  1245. new KeyValuePair<string, string>("纺织", "织") ,
  1246. new KeyValuePair<string, string>("服装", "服") ,
  1247. new KeyValuePair<string, string>("新闻", "新") ,
  1248. new KeyValuePair<string, string>("网络", "网") ,
  1249. new KeyValuePair<string, string>("应用", "应") ,
  1250. new KeyValuePair<string, string>("畜牧", "畜") ,
  1251. new KeyValuePair<string, string>("兽医", "兽") ,
  1252. new KeyValuePair<string, string>("师范", "师") ,
  1253. new KeyValuePair<string, string>("人文", "文") ,
  1254. new KeyValuePair<string, string>("创新", "创") ,
  1255. new KeyValuePair<string, string>("法官", "法") ,
  1256. new KeyValuePair<string, string>("邮电", "邮") ,
  1257. new KeyValuePair<string, string>("文化", "文") ,
  1258. new KeyValuePair<string, string>("函授", "函") ,
  1259. new KeyValuePair<string, string>("科学", "科") ,
  1260. new KeyValuePair<string, string>("信息", "息") ,
  1261. new KeyValuePair<string, string>("水利", "利") ,
  1262. new KeyValuePair<string, string>("水电", "电") ,
  1263. new KeyValuePair<string, string>("电力", "力") ,
  1264. new KeyValuePair<string, string>("环境", "环") ,
  1265. new KeyValuePair<string, string>("建材", "材") ,
  1266. new KeyValuePair<string, string>("机电", "机") ,
  1267. new KeyValuePair<string, string>("航空", "空") ,
  1268. new KeyValuePair<string, string>("航天", "天") ,
  1269. new KeyValuePair<string, string>("警察", "警") ,
  1270. new KeyValuePair<string, string>("警官", "警") ,
  1271. new KeyValuePair<string, string>("财贸", "财") ,
  1272. new KeyValuePair<string, string>("电子", "电") ,
  1273. new KeyValuePair<string, string>("建筑", "筑") ,
  1274. new KeyValuePair<string, string>("艺术", "艺") ,
  1275. new KeyValuePair<string, string>("体育", "体") ,
  1276. new KeyValuePair<string, string>("城市", "城") ,
  1277. new KeyValuePair<string, string>("地质", "地") ,
  1278. new KeyValuePair<string, string>("医药", "药") ,
  1279. new KeyValuePair<string, string>("政法", "法") ,
  1280. new KeyValuePair<string, string>("铁路", "铁") ,
  1281. new KeyValuePair<string, string>("铁道", "道") ,
  1282. new KeyValuePair<string, string>("轨道", "轨") ,
  1283. new KeyValuePair<string, string>("交通", "通") ,
  1284. new KeyValuePair<string, string>("医学院", "医") ,
  1285. new KeyValuePair<string, string>("医学", "医") ,
  1286. new KeyValuePair<string, string>("传媒", "媒") ,
  1287. new KeyValuePair<string, string>("工程", "程") ,
  1288. new KeyValuePair<string, string>("临床", "临") ,
  1289. new KeyValuePair<string, string>("化工", "化") ,
  1290. new KeyValuePair<string, string>("林业", "林") ,
  1291. new KeyValuePair<string, string>("老年", "老") ,
  1292. new KeyValuePair<string, string>("旅游", "游") ,
  1293. new KeyValuePair<string, string>("护理", "护") ,
  1294. new KeyValuePair<string, string>("贸易", "贸") ,
  1295. new KeyValuePair<string, string>("中医药", "中医") ,
  1296. new KeyValuePair<string, string>("资源", "源") ,
  1297. new KeyValuePair<string, string>("冶金", "冶") ,
  1298. new KeyValuePair<string, string>("能源", "能") ,
  1299. new KeyValuePair<string, string>("汽车", "车") ,
  1300. new KeyValuePair<string, string>("医科", "医") ,
  1301. new KeyValuePair<string, string>("机械", "械") ,
  1302. new KeyValuePair<string, string>("应用", "应") ,
  1303. new KeyValuePair<string, string>("电气", "电") ,
  1304. new KeyValuePair<string, string>("材料", "材") ,
  1305. new KeyValuePair<string, string>("劳动", "劳") ,
  1306. new KeyValuePair<string, string>("轻工", "轻") ,
  1307. new KeyValuePair<string, string>("农业", "农") ,
  1308. new KeyValuePair<string, string>("委员会", "委") ,
  1309. new KeyValuePair<string, string>("专业", "专") ,
  1310. new KeyValuePair<string, string>("广播", "广"),
  1311. new KeyValuePair<string, string>("电视", "电") ,
  1312. new KeyValuePair<string, string>("工商", "商") ,
  1313. new KeyValuePair<string, string>("工业", "工") ,
  1314. new KeyValuePair<string, string>("管理", "管") ,
  1315. new KeyValuePair<string, string>("社会主义", "社") ,
  1316. new KeyValuePair<string, string>("社会", "社") ,
  1317. new KeyValuePair<string, string>("自动化", "自") ,
  1318. };
  1319. public static List<KeyValuePair<string, string>> proReplaceStr = new List<KeyValuePair<string, string>> {
  1320. new KeyValuePair<string, string>("北京","京") ,
  1321. new KeyValuePair<string, string>("天津","津") ,
  1322. new KeyValuePair<string, string>("河北","冀") ,
  1323. new KeyValuePair<string, string>("山西","晋") ,
  1324. new KeyValuePair<string, string>("内蒙古","蒙") ,
  1325. new KeyValuePair<string, string>("辽宁","辽") ,
  1326. new KeyValuePair<string, string>("吉林","吉") ,
  1327. new KeyValuePair<string, string>("黑龙江","黑") ,
  1328. new KeyValuePair<string, string>("上海","沪") ,
  1329. new KeyValuePair<string, string>("江苏","苏") ,
  1330. new KeyValuePair<string, string>("浙江","浙") ,
  1331. new KeyValuePair<string, string>("安徽","皖") ,
  1332. new KeyValuePair<string, string>("福建","闽") ,
  1333. new KeyValuePair<string, string>("江西","赣") ,
  1334. new KeyValuePair<string, string>("山东","鲁") ,
  1335. new KeyValuePair<string, string>("河南","豫") ,
  1336. new KeyValuePair<string, string>("湖北","鄂") ,
  1337. new KeyValuePair<string, string>("湖南","湘") ,
  1338. new KeyValuePair<string, string>("广东","粤") ,
  1339. new KeyValuePair<string, string>("广西","桂") ,
  1340. new KeyValuePair<string, string>("海南","琼") ,
  1341. new KeyValuePair<string, string>("四川","川") ,
  1342. new KeyValuePair<string, string>("贵州","贵") ,
  1343. new KeyValuePair<string, string>("云南","云") ,
  1344. new KeyValuePair<string, string>("重庆","渝") ,
  1345. new KeyValuePair<string, string>("西藏","藏") ,
  1346. new KeyValuePair<string, string>("陕西","陕") ,
  1347. new KeyValuePair<string, string>("甘肃","甘") ,
  1348. new KeyValuePair<string, string>("青海","青") ,
  1349. new KeyValuePair<string, string>("宁夏","宁") ,
  1350. new KeyValuePair<string, string>("新疆","新") ,
  1351. new KeyValuePair<string, string>("香港","港") ,
  1352. new KeyValuePair<string, string>("澳门","澳") ,
  1353. new KeyValuePair<string, string>("台湾","台")
  1354. };
  1355. /*
  1356. [
  1357. {"method":"台北市","params":{"CountryId":"TW","CityId":"30"}},1
  1358. {"method":"新北市","params":{"CountryId":"TW","CityId":"01"}},1
  1359. {"method":"桃园市","params":{"CountryId":"TW","CityId":"03"}},1
  1360. {"method":"台中市","params":{"CountryId":"TW","CityId":"66"}},1
  1361. {"method":"台南市","params":{"CountryId":"TW","CityId":"67"}},1
  1362. {"method":"高雄市","params":{"CountryId":"TW","CityId":"64"}},1
  1363. {"method":"基隆市","params":{"CountryId":"TW","CityId":"17"}},1
  1364. {"method":"新竹市","params":{"CountryId":"TW","CityId":"18"}},1
  1365. {"method":"嘉义市","params":{"CountryId":"TW","CityId":"20"}},1
  1366. {"method":"新竹县","params":{"CountryId":"TW","CityId":"04"}},1
  1367. {"method":"苗栗县","params":{"CountryId":"TW","CityId":"05"}},1
  1368. {"method":"彰化县","params":{"CountryId":"TW","CityId":"07"}},1
  1369. {"method":"南投县","params":{"CountryId":"TW","CityId":"08"}},1
  1370. {"method":"云林县","params":{"CountryId":"TW","CityId":"09"}},1
  1371. {"method":"嘉义县","params":{"CountryId":"TW","CityId":"10"}},1
  1372. {"method":"屏东县","params":{"CountryId":"TW","CityId":"13"}},1
  1373. {"method":"宜兰县","params":{"CountryId":"TW","CityId":"02"}},1
  1374. {"method":"花莲县","params":{"CountryId":"TW","CityId":"15"}},1
  1375. {"method":"台东县","params":{"CountryId":"TW","CityId":"14"}},1
  1376. {"method":"澎湖县","params":{"CountryId":"TW","CityId":"16"}},1
  1377. {"method":"金门县","params":{"CountryId":"TW","CityId":"71"}},
  1378. {"method":"连江县","params":{"CountryId":"TW","CityId":"72"}}
  1379. ]
  1380. */
  1381. }
  1382. }