SchoolService.cs 71 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445
  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, long timestamp = 0, string time =null) {
  274. //string time = "2023/05-11";
  275. DateTimeOffset date = default;
  276. if (timestamp > 1000000000) {
  277. //毫秒级
  278. if (timestamp > 1000000000000)
  279. {
  280. date = DateTimeOffset.FromUnixTimeMilliseconds(timestamp);
  281. }
  282. //秒级
  283. else {
  284. date = DateTimeOffset.FromUnixTimeSeconds(timestamp);
  285. }
  286. }
  287. if (!(!string.IsNullOrWhiteSpace(time) && DateTimeOffset.TryParse(time, out date)))
  288. {
  289. //date = DateTimeOffset.FromUnixTimeSeconds(1672506061);
  290. //date = DateTimeOffset.UtcNow;
  291. date = DateTimeOffset.FromUnixTimeSeconds(1662688000);
  292. }
  293. //年级算法
  294. //var period = school.period.Find(x => x.id.Equals(periodId));
  295. List<KeyValuePair<int, string>> yearGrades = new List<KeyValuePair<int, string>>();
  296. int Day = date.Day;
  297. int Month = date.Month;
  298. int Year = date.Year;
  299. string sm = "09";
  300. string sd = "01";
  301. int start = int.Parse($"{Year}{sm}{sd}");
  302. var se = period.semesters.Find(x => x.start == 1);
  303. if (se == null)
  304. {
  305. se = period.semesters.First();
  306. }
  307. if (se != null)
  308. {
  309. sm = se.month >= 10 ? $"{se.month}" : $"0{se.month}";
  310. sd = se.day >= 10 ? $"{se.day}" : $"0{se.day}";
  311. start = int.Parse($"{Year}{sm}{sd}");
  312. }
  313. int curr = int.Parse(date.ToString("yyyyMMdd"));
  314. //新学年开学时间大于当前时间,计算年级需要减1 20220901-20220408 > 0 则当前20220408是2021年入学的,
  315. //当前时间大于新学年开学时间,计算年级则不需要 20220901-20221203 < 0 则当前20221203是2022年入学的,
  316. //20230901-20230101 > 0 则当前20230101是2022年入学的,
  317. int dis = start - curr;
  318. //学年
  319. int studyYear= date.Year;
  320. if (dis > 0)
  321. {
  322. studyYear = Year - 1;
  323. }
  324. else
  325. {
  326. studyYear = Year;
  327. }
  328. Semester semester= null;
  329. //当前学期的下一个学期,默认加180天
  330. DateTimeOffset nextSemester = date.AddDays(180);
  331. var sortSemesters= SortSemester(period.semesters);
  332. int len = sortSemesters.Count;
  333. for (int i = 0; i < len; i++) {
  334. int next= i + 1;
  335. if (next>= len)
  336. {
  337. semester = sortSemesters[i];
  338. nextSemester = DateTimeOffset.Parse($"{studyYear+1}/{sortSemesters[0].month}/{sortSemesters[0].day}");
  339. break;
  340. }
  341. else {
  342. var fdate= DateTimeOffset.Parse($"{studyYear}/{sortSemesters[i].month}/{sortSemesters[i].day}");
  343. long lf = fdate.ToUnixTimeSeconds();
  344. var ndate = DateTimeOffset.Parse($"{studyYear}/{sortSemesters[next].month}/{sortSemesters[next].day}");
  345. long ln = ndate.ToUnixTimeSeconds();
  346. //代表有跨年
  347. if (lf > ln) {
  348. ndate = DateTimeOffset.Parse($"{studyYear+1}/{sortSemesters[next].month}/{sortSemesters[next].day}");
  349. ln = ndate.ToUnixTimeSeconds();
  350. }
  351. var cdate = DateTimeOffset.Parse($"{Year}/{Month}/{Day}");
  352. long lc = fdate.ToUnixTimeSeconds();
  353. if (lc >= lf && lc < ln) {
  354. semester = sortSemesters[i];
  355. nextSemester = DateTimeOffset.Parse($"{studyYear}/{sortSemesters[next].month}/{sortSemesters[next].day}");
  356. //nextSemester = sortSemesters[next];
  357. break;
  358. }
  359. //if (sortSemesters[i].month <=Month && sortSemesters[i].day >= Day && sortSemesters[next].month < Month && sortSemesters[next].day < Day)
  360. //{
  361. // semester = sortSemesters[i];
  362. // nextSemester = DateTimeOffset.Parse($"{studyYear}/{sortSemesters[next].month}/{sortSemesters[next].day}");
  363. // break;
  364. //}
  365. }
  366. }
  367. return (semester,studyYear,date, nextSemester);
  368. }
  369. /// <summary>
  370. /// 处理学期排序
  371. /// </summary>
  372. /// <param name="semesterList"></param>
  373. /// <returns></returns>
  374. public static List<Semester> SortSemester(List<Semester> semesterList)
  375. {
  376. int Year = DateTimeOffset.UtcNow.Year;
  377. List<KeyValuePair<int, Semester>> pairs = new List<KeyValuePair<int, Semester>>();
  378. semesterList.ForEach(se => {
  379. string sm = se.month >= 10 ? $"{se.month}" : $"0{se.month}";
  380. string sd = se.day >= 10 ? $"{se.day}" : $"0{se.day}";
  381. int order = int.Parse($"{Year}{sm}{sd}");
  382. pairs.Add(new KeyValuePair<int, Semester>(order, se));
  383. });
  384. var orderPairs = pairs.OrderBy(z => z.Key);
  385. semesterList = orderPairs.Select(z => z.Value).ToList();
  386. int startIndex = semesterList.FindIndex(z => z.start == 1);
  387. if (startIndex == -1)
  388. {
  389. //未设置学期的情况默认9月份开始的。
  390. startIndex = semesterList.FindIndex(z => z.month == 9);
  391. //如果还未找到,就以排序结果为准
  392. if (startIndex == -1)
  393. {
  394. startIndex = 0;
  395. }
  396. }
  397. if (startIndex > 0)
  398. {
  399. List<Semester> before = semesterList.Take(startIndex).ToList();
  400. List<Semester> after = semesterList.Skip(startIndex).ToList();
  401. semesterList = new List<Semester>();
  402. semesterList.AddRange(after);
  403. semesterList.AddRange(before);
  404. return semesterList;
  405. }
  406. else
  407. {
  408. return semesterList;
  409. }
  410. }
  411. /// <summary>
  412. /// 校验
  413. /// </summary>
  414. /// <param name="courseTaskInsert"></param>
  415. public static CourseTaskChanged CheckCourseTaskInsertOrChanged(string grant_type, string scope , CourseTaskChanged courseTaskInsert,string school,Period period, List<CourseBase> courseBases, List<GroupListDto> groupListDtos, List<Room> rooms,
  416. List<SchoolTeacher> schoolTeachers,List<Teacher> teachers)
  417. {
  418. if (scope.Equals("school", StringComparison.OrdinalIgnoreCase))
  419. { //学年
  420. if (courseTaskInsert.year < 2000)
  421. {
  422. courseTaskInsert.invalidCode = 17;//学年应大于2000
  423. return courseTaskInsert;
  424. }
  425. //学期
  426. var semester = period.semesters.Where(z => z.id.Equals(courseTaskInsert.semesterId));
  427. if (!semester.Any())
  428. {
  429. courseTaskInsert.invalidCode = 16;//学期不存在
  430. return courseTaskInsert;
  431. }
  432. //教师
  433. var schoolteachers = schoolTeachers.Where(z => z.id.Equals(courseTaskInsert.teacherId));
  434. if (!schoolteachers.Any())
  435. {
  436. courseTaskInsert.invalidCode = 2;//教师不存在
  437. return courseTaskInsert;
  438. }
  439. }
  440. else {
  441. //教师
  442. var teachersbase = teachers.Where(z => z.id.Equals(courseTaskInsert.teacherId));
  443. if (!teachersbase.Any())
  444. {
  445. courseTaskInsert.invalidCode = 2;//教师不存在
  446. return courseTaskInsert;
  447. }
  448. }
  449. //课程
  450. var courseBase = courseBases.Where(z => z.id.Equals(courseTaskInsert.courseId));
  451. if (!courseBase.Any())
  452. {
  453. courseTaskInsert.invalidCode = 1;//课程不存在
  454. return courseTaskInsert;
  455. }
  456. //名单
  457. var groupList = groupListDtos.Where(z => z.id.Equals(courseTaskInsert.groupId) && z.type.Equals(courseTaskInsert.type));
  458. if (!groupList.Any())
  459. {
  460. courseTaskInsert.invalidCode = 3;//名单不存在
  461. return courseTaskInsert;
  462. }
  463. else {
  464. if (scope.Equals("private", StringComparison.OrdinalIgnoreCase))
  465. {
  466. if (!groupList.First().scope.Equals("private", StringComparison.OrdinalIgnoreCase) && !string.IsNullOrWhiteSpace(school) && school.Equals(groupList.First().school))
  467. {
  468. courseTaskInsert.school = school;
  469. }
  470. }
  471. else {
  472. courseTaskInsert.school = school;
  473. }
  474. }
  475. //教室
  476. if (!string.IsNullOrWhiteSpace(courseTaskInsert.roomId))
  477. {
  478. var room = rooms.Where(z => z.id.Equals(courseTaskInsert.roomId));
  479. if (!room.Any())
  480. {
  481. courseTaskInsert.invalidCode = 4;//教室不存在
  482. return courseTaskInsert;
  483. }
  484. }
  485. if (grant_type.Equals("change-scheduleTask", StringComparison.OrdinalIgnoreCase)) {
  486. if (!string.IsNullOrWhiteSpace(courseTaskInsert.teacherIdChanged) || (!string.IsNullOrWhiteSpace(courseTaskInsert.groupIdChanged) && !string.IsNullOrWhiteSpace(courseTaskInsert.typeChanged)))
  487. {
  488. if (!string.IsNullOrWhiteSpace(courseTaskInsert.teacherIdChanged)) {
  489. if (scope.Equals("school", StringComparison.OrdinalIgnoreCase))
  490. {
  491. //教师
  492. var schoolteachers = schoolTeachers.Where(z => z.id.Equals(courseTaskInsert.teacherIdChanged));
  493. if (!schoolteachers.Any())
  494. {
  495. courseTaskInsert.invalidCode = 19;//教师不存在
  496. return courseTaskInsert;
  497. }
  498. }
  499. else {
  500. //教师
  501. var teachersbase = teachers.Where(z => z.id.Equals(courseTaskInsert.teacherIdChanged));
  502. if (!teachersbase.Any())
  503. {
  504. courseTaskInsert.invalidCode = 19;//教师不存在
  505. return courseTaskInsert;
  506. }
  507. }
  508. }
  509. if (!string.IsNullOrWhiteSpace(courseTaskInsert.groupIdChanged) && !string.IsNullOrWhiteSpace(courseTaskInsert.typeChanged)) {
  510. //名单
  511. var groupListChanged = groupListDtos.Where(z => z.id.Equals(courseTaskInsert.groupIdChanged) && z.type.Equals(courseTaskInsert.typeChanged));
  512. if (!groupListChanged.Any())
  513. {
  514. courseTaskInsert.invalidCode = 18;//名单不存在
  515. return courseTaskInsert;
  516. }
  517. else
  518. {
  519. if (scope.Equals("private", StringComparison.OrdinalIgnoreCase))
  520. {
  521. if (!groupListChanged.First().scope.Equals("private", StringComparison.OrdinalIgnoreCase) && !string.IsNullOrWhiteSpace(school) && school.Equals(groupList.First().school))
  522. {
  523. courseTaskInsert.school = school;
  524. }
  525. }
  526. else
  527. {
  528. courseTaskInsert.school = school;
  529. }
  530. }
  531. }
  532. }
  533. else {
  534. courseTaskInsert.invalidCode = 17;
  535. }
  536. }
  537. courseTaskInsert.invalidCode = 0;
  538. return courseTaskInsert;
  539. }
  540. 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) {
  541. List<Class> school_classes = new List<Class>();
  542. List<Class> graduate_classes = new List<Class>();
  543. try
  544. {
  545. var client = _azureCosmos.GetCosmosClient();
  546. //取得班级
  547. int nowYear = DateTimeOffset.UtcNow.Year;
  548. int nowMonth = DateTimeOffset.UtcNow.Month;
  549. int nowDay = DateTimeOffset.UtcNow.Day;
  550. //因为修改年级,而导致取消的
  551. List<Class> cancel_graduate_classes = new List<Class>();
  552. string sql = $"SELECT value c FROM c where (c.graduate = 0 or IS_DEFINED(c.graduate) = false) ";
  553. if (periodIds.IsNotEmpty())
  554. {
  555. sql = $"SELECT value c FROM c where c.periodId in ({string.Join(",", periodIds.Select(x => $"'{x}'"))}) ";
  556. }
  557. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<Class>
  558. //(queryText: $"SELECT value c FROM c where (c.graduate = 0 or IS_DEFINED(c.graduate) = false)",
  559. (queryText: sql,
  560. requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Class-{school_base.id}") }))
  561. {
  562. bool isgraduate = false;
  563. if (!string.IsNullOrWhiteSpace(item.periodId))
  564. {
  565. var period = school_base.period.Find(x => x.id.Equals(item.periodId));
  566. if (period != null)
  567. {
  568. var gradeCount = period.grades.Count();
  569. //2022-2016=6(待判断月份日期是否是毕业) 2022-2017=5(未毕业)
  570. if (nowYear - item.year > gradeCount)
  571. {
  572. isgraduate = true;
  573. }
  574. else if (nowYear - item.year == gradeCount)
  575. {
  576. var semester = period.semesters.Find(x => x.start == 1);
  577. if (semester != null)
  578. {
  579. if (nowMonth > semester.month)
  580. {
  581. isgraduate = true;
  582. }
  583. else if (nowMonth == semester.month)
  584. {
  585. if (nowDay >= semester.day)
  586. {
  587. isgraduate = true;
  588. }
  589. else
  590. {
  591. }
  592. }
  593. else { isgraduate = false; }
  594. }
  595. }
  596. else
  597. {
  598. isgraduate = false;
  599. }
  600. }
  601. }
  602. if (isgraduate)
  603. {
  604. graduate_classes.Add(item);
  605. }
  606. else
  607. {
  608. if (item.graduate == 1)
  609. {
  610. item.graduate = 0;
  611. cancel_graduate_classes.Add(item);
  612. }
  613. school_classes.Add(item);
  614. }
  615. }
  616. if (graduate_classes.Any() || cancel_graduate_classes.Any())
  617. {
  618. if (waite == 0)
  619. {
  620. _ = _httpTrigger.RequestHttpTrigger(new { graduate_classes = graduate_classes, cancel_graduate_classes = cancel_graduate_classes, schoolId = $"{school_base.id}" }, _option.Location, "graduate-change");
  621. }
  622. else
  623. {
  624. await _httpTrigger.RequestHttpTrigger(new { graduate_classes = graduate_classes, cancel_graduate_classes = cancel_graduate_classes, schoolId = $"{school_base.id}" }, _option.Location, "graduate-change");
  625. }
  626. }
  627. } catch (Exception ex) {
  628. await _dingDing.SendBotMsg($"{_option.Location},{ex.Message}\n{ex.StackTrace}", GroupNames.醍摩豆服務運維群組);
  629. }
  630. return (school_classes,graduate_classes);
  631. }
  632. public static async Task<object> DoScsApiSchool(HttpTrigger _httpTrigger,Dictionary<string,object> dict, Option _option, List<IdNameCode> ignore,
  633. string areaId,string city,string dist, AzureStorageFactory _azureStorage ,DingDing _dingDing, IWebHostEnvironment _environment
  634. )
  635. {
  636. List<ScSchool> tbschools = null;
  637. List<ScSchool> matchSchools = null;
  638. List<ScSchool> schools = null;
  639. List<ScSchool> saveschools = null;
  640. var table = _azureStorage.GetCloudTableClient().GetTableReference("ScYxpt");
  641. // 5.3.1.18根据机构ID、项目ID、子项目ID返回学校列表
  642. ( int status, string json) = await _httpTrigger.RequestHttpTrigger(dict, _option.Location, "GetSchoolList");
  643. if (status == 200)
  644. {
  645. schools = json.ToObject<List<ScSchool>>(new JsonSerializerOptions { PropertyNameCaseInsensitive = false });
  646. if (ignore.IsNotEmpty())
  647. {
  648. matchSchools = schools.FindAll(x => ignore.Select(y => y.name).Contains(x.schoolname));
  649. if (matchSchools.IsNotEmpty())
  650. {
  651. if (matchSchools.Count != ignore.Count)
  652. {
  653. var matched = matchSchools.Select(x => x.schoolname);
  654. var unmatch = ignore.Select(y => y.name).Except(matched);
  655. List<string> scschoolUnmatch = schools.Select(y => y.schoolname).Except(matched).ToList();
  656. if (scschoolUnmatch.IsNotEmpty())
  657. {
  658. return new { matched, unmatch, scschoolUnmatch };
  659. }
  660. }
  661. else
  662. {
  663. matchSchools.ForEach(x => {
  664. var exschool = ignore.Find(y => y.name.Equals(x.schoolname));
  665. if (exschool != null)
  666. {
  667. x.schoolCode = exschool.id;
  668. x.areaId = $"{areaId}";
  669. x.city = $"{city}";
  670. x.dist = $"{dist}";
  671. }
  672. });
  673. }
  674. }
  675. else
  676. {
  677. return new { unmatch = ignore, schools };
  678. }
  679. }
  680. //数据校验
  681. tbschools = await table.FindListByDict<ScSchool>(new Dictionary<string, object>() { { "PartitionKey", "ScSchool" } });
  682. if (tbschools.IsNotEmpty())
  683. {
  684. var a = tbschools.Select(y => $"{y.RowKey}").ToList();
  685. saveschools = schools.Where(x => !a.Exists(z => z.Equals($"{x.schoolid}"))).ToList();
  686. List<SchoolData> schoolDatas = new List<SchoolData>();
  687. saveschools.ForEach(x =>
  688. {
  689. x.RowKey = $"{x.schoolid}";
  690. x.PartitionKey = "ScSchool";
  691. x.areaId = $"{areaId}";
  692. x.city = $"{city}";
  693. x.dist = $"{dist}";
  694. if (string.IsNullOrEmpty(x.schoolCode))
  695. {
  696. if (string.IsNullOrEmpty(x.schoolCode))
  697. {
  698. schoolDatas.Add(new SchoolData { uid = $"{x.schoolid}", province = "四川省", city = $"{city}", name = x.schoolname });
  699. }
  700. }
  701. });
  702. schoolDatas = await SchoolService.GenerateSchoolCode(schoolDatas, _dingDing, _environment);
  703. saveschools.ForEach(x => {
  704. var schoolData = schoolDatas.Find(y => y.uid.Equals($"{x.schoolid}"));
  705. if (schoolData != null && !string.IsNullOrEmpty(schoolData.id))
  706. {
  707. x.schoolCode = schoolData.id;
  708. x.areaId = $"{areaId}";
  709. x.city = $"{city}";
  710. x.dist = $"{dist}";
  711. }
  712. });
  713. saveschools.RemoveAll(x => string.IsNullOrEmpty(x.schoolCode));
  714. saveschools.RemoveAll(x => tbschools.FindAll(z => !string.IsNullOrEmpty(z.schoolCode)).Exists(y => y.schoolCode.Equals(x.schoolCode)));
  715. saveschools = await table.SaveAll(saveschools);
  716. }
  717. else
  718. {
  719. List<SchoolData> schoolDatas = new List<SchoolData>();
  720. schools.ForEach(x =>
  721. {
  722. x.RowKey = $"{x.schoolid}";
  723. x.PartitionKey = "ScSchool";
  724. x.areaId = $"{areaId}";
  725. x.city = $"{city}";
  726. x.dist = $"{dist}";
  727. var a = ignore.Find(z => z.name.Equals(x.schoolname));
  728. if (a != null)
  729. {
  730. x.schoolCode = a.id;
  731. }
  732. else
  733. {
  734. if (string.IsNullOrEmpty(x.schoolCode))
  735. {
  736. schoolDatas.Add(new SchoolData { uid = $"{x.schoolid}", province = "四川省", city = $"{city}", name = x.schoolname });
  737. }
  738. }
  739. });
  740. schoolDatas = await SchoolService.GenerateSchoolCode(schoolDatas, _dingDing, _environment);
  741. schools.ForEach(x => {
  742. var schoolData = schoolDatas.Find(y => y.uid.Equals($"{x.schoolid}"));
  743. if (schoolData != null && !string.IsNullOrEmpty(schoolData.id))
  744. {
  745. x.schoolCode = schoolData.id;
  746. x.areaId = $"{areaId}";
  747. x.city = $"{city}";
  748. x.dist = $"{dist}";
  749. }
  750. });
  751. schools.RemoveAll(x => string.IsNullOrEmpty(x.schoolCode));
  752. saveschools = await table.SaveOrUpdateAll(schools);
  753. }
  754. }
  755. return null;
  756. }
  757. public static async Task<List<SchoolData>> GenerateSchoolCode(List<SchoolData> schools, DingDing _dingDing, IWebHostEnvironment _environment)
  758. {
  759. string path = $"{_environment.ContentRootPath}/JsonFile/Core/region.json";
  760. //var schools = jsonstr.ToObject<JsonElement>().GetProperty("schools").ToObject<List<SchoolData>>();
  761. schools = schools.Where((x, i) => schools.FindIndex(n => n.name.Equals(x.name)) == i).ToList();
  762. StreamReader streamReader = new StreamReader(new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite), Encoding.UTF8);
  763. StringBuilder stringBuilder = new StringBuilder();
  764. string text;
  765. while ((text = streamReader.ReadLine()) != null)
  766. {
  767. stringBuilder.Append(text.ToString());
  768. }
  769. streamReader.Close();
  770. string input = stringBuilder.ToString();
  771. List<Region> region = input.ToObject<List<Region>>();
  772. try
  773. {
  774. schools.ForEach(x =>
  775. {
  776. x.province = ChineseConverter.Convert(x.province, ChineseConversionDirection.TraditionalToSimplified);
  777. if (String.IsNullOrEmpty(x.city))
  778. {
  779. x.city = x.province;
  780. }
  781. else
  782. {
  783. x.city = ChineseConverter.Convert(x.city, ChineseConversionDirection.TraditionalToSimplified);
  784. }
  785. x.name = ChineseConverter.Convert(x.name, ChineseConversionDirection.TraditionalToSimplified);
  786. comeRemoveStr.ForEach(c => { x.province = x.province.Replace(c, ""); });
  787. prvcRemoveStr.ForEach(c => { x.province = x.province.Replace(c, ""); });
  788. var province = region.Find(r => r.name.Contains(x.province));
  789. string tmpprovince = x.province;
  790. string tmpcity = x.city;
  791. if (province != null)
  792. {
  793. x.province = province.name;
  794. comeRemoveStr.ForEach(c => { x.city = x.city.Replace(c, ""); });
  795. cityRemoveStr.ForEach(c => { x.city = x.city.Replace(c, ""); });
  796. nationRemoveStr.ForEach(c => { x.city = x.city.Replace(c, ""); });
  797. tmpcity = x.city;
  798. var city = province.children.Find(r => r.name.Contains(x.city));
  799. if (city == null)
  800. {
  801. city = province.children.Find(r => r.name.Contains(x.city));
  802. if (city == null)
  803. {
  804. city = province.children.SelectMany(x => x.children).ToList().Find(r => r.name.Contains(x.city));
  805. }
  806. }
  807. if (city != null)
  808. {
  809. x.city = city.name;
  810. }
  811. }
  812. string name = x.name;
  813. //去除冗余的学校信息,只保留地名和校名关键信息
  814. schemoveStr.ForEach(str =>
  815. {
  816. name = name.Replace(str, "");
  817. });
  818. string[] names = name.Split("(");
  819. if (names.Length > 1)
  820. {
  821. name = $"{names[0]}";
  822. for (int index = 1; index < names.Length; index++)
  823. {
  824. name = $"{name}{names[index].Substring(0, 1)}";
  825. var afnames = names[index].Split(")");
  826. if (afnames.Length > 1)
  827. {
  828. for (int i = 1; i < afnames.Length; i++)
  829. {
  830. name = $"{name}{afnames[i]}";
  831. }
  832. }
  833. }
  834. }
  835. names = name.Split("(");
  836. if (names.Length > 1)
  837. {
  838. name = $"{names[0]}";
  839. for (int index = 1; index < names.Length; index++)
  840. {
  841. name = $"{name}{names[index].Substring(0, 1)}";
  842. var afnames = names[index].Split(")");
  843. if (afnames.Length > 1)
  844. {
  845. for (int i = 1; i < afnames.Length; i++)
  846. {
  847. name = $"{name}{afnames[i]}";
  848. }
  849. }
  850. }
  851. }
  852. name = Regex.Replace(name, "[ \\[ \\] \\^ \\-|()【】/' {}_*×――(^)$%~!@#$…&%¥—+=<>《》!!???::•`·、。,;,.;\"‘’“”-]", " ");
  853. //检查是否有英文
  854. if (Regex.Matches(name, "[a-zA-Z]").Count > 0)
  855. {
  856. var array = Regex.Split(name, "\\s+", RegexOptions.IgnoreCase);
  857. StringBuilder tmpname = new StringBuilder();
  858. if (array.Length > 1)
  859. {
  860. foreach (var item in array)
  861. {
  862. var arr = item.Select(x => $"{x}");
  863. int index = 0;
  864. foreach (var stra in arr)
  865. {
  866. if (Regex.Matches(stra, "[a-zA-Z]").Count > 0)
  867. {
  868. if (index == 1|| index == 0)
  869. {
  870. tmpname.Append(stra);
  871. }
  872. else
  873. {
  874. }
  875. }
  876. else
  877. {
  878. tmpname.Append(stra);
  879. }
  880. index++;
  881. }
  882. }
  883. }
  884. else
  885. {
  886. var arr = name.Select(x => $"{x}");
  887. int index = 0;
  888. foreach (var stra in arr)
  889. {
  890. if (Regex.Matches(stra, "[A-Z]").Count > 0)
  891. {
  892. tmpname.Append(stra);
  893. }
  894. else if (Regex.Matches(stra, "[a-z]").Count > 0)
  895. {
  896. }
  897. else
  898. {
  899. tmpname.Append(stra);
  900. }
  901. index++;
  902. }
  903. }
  904. name = tmpname.ToString();
  905. }
  906. name = Regex.Replace(name, @"\s", "");
  907. name = name.Replace("\\", "");
  908. if (name.Length > 6)
  909. {
  910. //新区,高新,产业等非新政单位
  911. areaRemoveStr.ForEach(str =>
  912. {
  913. name = name.Replace(str.Key, str.Value);
  914. });
  915. //去除冗余的学校信息,只保留地名和校名关键信息
  916. schooRemoveStr.ForEach(str =>
  917. {
  918. name = name.Replace(str, "");
  919. });
  920. //替换民族信息词
  921. if (name.Length > 6)
  922. {
  923. nationRemoveStr.ForEach(str =>
  924. {
  925. name = name.Replace(str, "");
  926. });
  927. }
  928. //替换学校信息的副词
  929. if (name.Length > 6)
  930. {
  931. foreach (var str in schooReplaceStr)
  932. {
  933. if (name.Length <= 6)
  934. {
  935. break;
  936. }
  937. name = name.Replace(str.Key, str.Value);
  938. }
  939. }
  940. //替换省简称信息词
  941. string tmpname = name;
  942. if (name.Length > 6)
  943. {
  944. proReplaceStr.ForEach(str =>
  945. {
  946. name = name.Replace(str.Key, str.Value);
  947. });
  948. //如果替换后仍然大于6位,则取消,直接替换省份完整的词
  949. if (name.Length > 6)
  950. {
  951. name = tmpname.Replace(tmpprovince, "");
  952. }
  953. }
  954. //替换城市信息词
  955. if (name.Length > 6)
  956. {
  957. name = name.Replace(tmpcity, "");
  958. }
  959. }
  960. int len = name.Length;
  961. if (len > 6)
  962. {
  963. foreach (var str in afterSchoolRm)
  964. {
  965. if (name.Length <= 6)
  966. {
  967. break;
  968. }
  969. name = name.Replace(str, "");
  970. }
  971. }
  972. len = name.Length;
  973. if (len >= 6)
  974. {
  975. name = $"{name.Substring(0, 1)}{name.Substring(len - 5, 3)}{name.Substring(len - 2, 2)}";
  976. }
  977. //if (len <= 7 && len > 6)
  978. //{
  979. // name = name.Substring(len - 6);
  980. //}
  981. if (len <= 4)
  982. {
  983. name = $"{x.city.Substring(0, 2)}{name}";
  984. }
  985. if (len == 5)
  986. {
  987. name = $"{x.city.Substring(0, 1)}{name}";
  988. }
  989. string code = ToFirstPinYin(name);
  990. x.aname = name;
  991. x.id = code;
  992. });
  993. }
  994. catch (Exception ex)
  995. {
  996. await _dingDing.SendBotMsg($"{ex.Message}\n{ex.StackTrace}\n", GroupNames.醍摩豆服務運維群組);
  997. }
  998. string a_z = "abcdefghijklmnopqrstuvwxyz";
  999. 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();
  1000. data.ForEach(x =>
  1001. {
  1002. if (x.count > 1)
  1003. {
  1004. var first = x.list.First();
  1005. var same = x.list.Skip(1).Take(x.count - 1).ToList();
  1006. var fs = first.aname.Select(x => x).ToArray();
  1007. same.ForEach(z =>
  1008. {
  1009. var sp = z.aname.Select(x => x).ToArray();
  1010. int len = sp.Length;
  1011. if (sp.Length >= fs.Length)
  1012. {
  1013. len = fs.Count();
  1014. }
  1015. for (int index = 0; index < len; index++)
  1016. {
  1017. if (!$"{sp[index]}".Equals($"{fs[index]}"))
  1018. {
  1019. short st = ChineseChar.GetStrokeNumber(sp[index]);
  1020. if (st > 0)
  1021. {
  1022. var ins = st % 27;
  1023. var insch = a_z[ins];
  1024. if (z.aname.EndsWith($"{insch}"))
  1025. {
  1026. ins = (st + 1) % 27;
  1027. insch = a_z[ins];
  1028. }
  1029. z.id = z.id.Insert(index, $"{insch}").Remove(index + 1, 1);
  1030. break;
  1031. }
  1032. }
  1033. }
  1034. });
  1035. }
  1036. });
  1037. 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();
  1038. dataz.ForEach(x =>
  1039. {
  1040. if (x.count > 1)
  1041. {
  1042. var first = x.list.First();
  1043. var same = x.list.Skip(1).Take(x.count - 1).ToList();
  1044. var fs = first.aname.Select(x => x).ToArray();
  1045. same.ForEach(z =>
  1046. {
  1047. var sp = z.aname.Select(x => x).ToArray();
  1048. int len = sp.Length;
  1049. if (sp.Length >= fs.Length)
  1050. {
  1051. len = fs.Count();
  1052. }
  1053. for (int index = 0; index < len; index++)
  1054. {
  1055. if (!$"{sp[index]}".Equals($"{fs[index]}"))
  1056. {
  1057. var spstr = ToPinYin($"{sp[index]}" );
  1058. var fsstr = ToPinYin($"{fs[index]}");
  1059. var insch= spstr.Select(x => $"{x}").Except( fsstr.Select(z => $"{z}"));
  1060. if (insch != null && insch.Count() > 0) {
  1061. if (index > 1)
  1062. {
  1063. z.id = z.id.Insert(index - 1, insch.First()).Remove(index, 1);
  1064. }
  1065. else if (index == 1)
  1066. {
  1067. z.id = z.id.Insert(index, insch.First()).Remove(index - 1, 1);
  1068. }
  1069. }
  1070. }
  1071. }
  1072. });
  1073. }
  1074. });
  1075. 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();
  1076. Random random = new Random();
  1077. List<SchoolData> list = new List<SchoolData>();
  1078. dataa.ForEach(x =>
  1079. {
  1080. if (x.count > 1)
  1081. {
  1082. x.list.ForEach(y => {
  1083. int r= random.Next(25);
  1084. int index= random.Next(5);
  1085. y.id = y.id.Insert(index - 1, $"{a_z[r]}").Remove(index, 1);
  1086. });
  1087. }
  1088. list.AddRange(x.list);
  1089. });
  1090. return list;
  1091. }
  1092. public static string ToPinYin(string val)
  1093. {
  1094. StringBuilder sb = new StringBuilder();
  1095. val.ToCharArray().ToList().ForEach(x =>
  1096. {
  1097. if (('a' <= x && x <= 'z') || ('A' <= x && x <= 'Z') || ('0' <= x && x <= '9'))
  1098. {
  1099. sb.Append(x);
  1100. }
  1101. else
  1102. {
  1103. try
  1104. {
  1105. ChineseChar cc = new ChineseChar(x);
  1106. if (cc.Pinyins.Count > 0 && cc.Pinyins[0].Length > 0)
  1107. {
  1108. sb.Append(cc.Pinyins[0]);
  1109. }
  1110. }
  1111. catch (Exception ex)
  1112. {
  1113. }
  1114. }
  1115. });
  1116. return sb.ToString().ToLower();
  1117. }
  1118. public static string ToFirstPinYin(string val)
  1119. {
  1120. StringBuilder sb = new StringBuilder();
  1121. val.ToCharArray().ToList().ForEach(x =>
  1122. {
  1123. if (('a' <= x && x <= 'z') || ('A' <= x && x <= 'Z') || ('0' <= x && x <= '9'))
  1124. {
  1125. sb.Append(x);
  1126. }
  1127. else
  1128. {
  1129. try
  1130. {
  1131. ChineseChar cc = new ChineseChar(x);
  1132. if (cc.Pinyins.Count > 0 && cc.Pinyins[0].Length > 0)
  1133. {
  1134. sb.Append(cc.Pinyins[0][0]);
  1135. }
  1136. }
  1137. catch (Exception ex)
  1138. {
  1139. }
  1140. }
  1141. });
  1142. return sb.ToString().ToLower();
  1143. }
  1144. public class SchoolData
  1145. {
  1146. public string province { get; set; }
  1147. public string id { get; set; }
  1148. public string name { get; set; }
  1149. public string city { get; set; }
  1150. public string aname { get; set; }
  1151. public string uid { get; set; }
  1152. }
  1153. public static List<string> comeRemoveStr = new List<string>() { "省", "市", "区", "州", "县", "旗", "盟", "自治" };
  1154. public static List<string> prvcRemoveStr = new List<string>() { "回族", "维吾尔", "壮族", };
  1155. public static List<string> cityRemoveStr = new List<string>() { "蒙古族", "地区", "壮族", "朝鲜族", "直辖" };
  1156. public static List<string> nationRemoveStr = new List<string> {
  1157. "维吾尔","回族", "自治", "满族", "蒙古", "壮族", "苗族" , "侗族", "瑶族",
  1158. "达斡尔","鄂温克","朝鲜","畲族","土家","各族","仫佬","毛南","羌族","彝族","仡佬","布依","水族",
  1159. "傣族","纳西","哈尼","拉祜","佤族","傈僳","独龙","怒族","白族","普米","固族","哈萨克","土族","撒拉","景颇","族"
  1160. };
  1161. public static List<string> schemoveStr = new List<string> { "第", "校区", "部", "楼", "与", "学校", "校园", };
  1162. public static List<string> schooRemoveStr = new List<string>() {"省", "市", "区", "州", "县", "旗", "盟","办事处","街道", "自治",
  1163. "镇","村","乡","街","路","站","馆"
  1164. };
  1165. public static List<string> afterSchoolRm = new List<string>() {"校","园","院", "湾","峡","沟","山","庄",
  1166. "堡","江","屯","岭","溪","河","桥","营","铺","坡","寨","场","湖","巷","集","关","庙","寺","矿","塘"};
  1167. public static List<KeyValuePair<string, string>> areaRemoveStr = new List<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. new KeyValuePair<string, string>("职工", "") ,
  1188. new 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. };
  1199. public static List<KeyValuePair<string, string>> schooReplaceStr = new List<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. new KeyValuePair<string, string>("农业", "农") ,
  1319. new 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. };
  1330. public static List<KeyValuePair<string, string>> proReplaceStr = new List<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. new KeyValuePair<string, string>("云南","云") ,
  1355. new KeyValuePair<string, string>("重庆","渝") ,
  1356. new KeyValuePair<string, string>("西藏","藏") ,
  1357. new KeyValuePair<string, string>("陕西","陕") ,
  1358. new KeyValuePair<string, string>("甘肃","甘") ,
  1359. new KeyValuePair<string, string>("青海","青") ,
  1360. new KeyValuePair<string, string>("宁夏","宁") ,
  1361. new KeyValuePair<string, string>("新疆","新") ,
  1362. new KeyValuePair<string, string>("香港","港") ,
  1363. new KeyValuePair<string, string>("澳门","澳") ,
  1364. new KeyValuePair<string, string>("台湾","台")
  1365. };
  1366. /*
  1367. [
  1368. {"method":"台北市","params":{"CountryId":"TW","CityId":"30"}},1
  1369. {"method":"新北市","params":{"CountryId":"TW","CityId":"01"}},1
  1370. {"method":"桃园市","params":{"CountryId":"TW","CityId":"03"}},1
  1371. {"method":"台中市","params":{"CountryId":"TW","CityId":"66"}},1
  1372. {"method":"台南市","params":{"CountryId":"TW","CityId":"67"}},1
  1373. {"method":"高雄市","params":{"CountryId":"TW","CityId":"64"}},1
  1374. {"method":"基隆市","params":{"CountryId":"TW","CityId":"17"}},1
  1375. {"method":"新竹市","params":{"CountryId":"TW","CityId":"18"}},1
  1376. {"method":"嘉义市","params":{"CountryId":"TW","CityId":"20"}},1
  1377. {"method":"新竹县","params":{"CountryId":"TW","CityId":"04"}},1
  1378. {"method":"苗栗县","params":{"CountryId":"TW","CityId":"05"}},1
  1379. {"method":"彰化县","params":{"CountryId":"TW","CityId":"07"}},1
  1380. {"method":"南投县","params":{"CountryId":"TW","CityId":"08"}},1
  1381. {"method":"云林县","params":{"CountryId":"TW","CityId":"09"}},1
  1382. {"method":"嘉义县","params":{"CountryId":"TW","CityId":"10"}},1
  1383. {"method":"屏东县","params":{"CountryId":"TW","CityId":"13"}},1
  1384. {"method":"宜兰县","params":{"CountryId":"TW","CityId":"02"}},1
  1385. {"method":"花莲县","params":{"CountryId":"TW","CityId":"15"}},1
  1386. {"method":"台东县","params":{"CountryId":"TW","CityId":"14"}},1
  1387. {"method":"澎湖县","params":{"CountryId":"TW","CityId":"16"}},1
  1388. {"method":"金门县","params":{"CountryId":"TW","CityId":"71"}},
  1389. {"method":"连江县","params":{"CountryId":"TW","CityId":"72"}}
  1390. ]
  1391. */
  1392. }
  1393. }