SchoolService.cs 93 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973
  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<School> ModifySchool(School school, AzureCosmosFactory _azureCosmos) {
  30. //修补五育的科目关联
  31. var subjects = school.period.SelectMany(x => x.subjects).Where(y => (!string.IsNullOrWhiteSpace(y.bindId) &&
  32. (y.bindId.Equals("subject_painting") || y.bindId.Equals("subject_sport")
  33. || y.bindId.Equals("subject_labour") || y.bindId.Equals("subject_virtue") || y.bindId.Equals("subject_music")
  34. || y.bindId.Equals("subject_chinese") || y.bindId.Equals("subject_math") || y.bindId.Equals("subject_english")
  35. || y.bindId.Equals("subject_physics") || y.bindId.Equals("subject_chemistry") || y.bindId.Equals("subject_biology")
  36. || y.bindId.Equals("subject_politics") || y.bindId.Equals("subject_history") || y.bindId.Equals("subject_geography")
  37. ))
  38. || y.name.Contains("音乐") || y.name.Contains("美术") || y.name.Contains("劳动") || y.name.Contains("德育") || y.name.Contains("体育")
  39. || y.name.Contains("语文") || y.name.Contains("数学") || y.name.Contains("英语") || y.name.Contains("物理") || y.name.Contains("化学")
  40. || y.name.Contains("生物") || y.name.Contains("政治") || y.name.Contains("历史") || y.name.Contains("地理")
  41. );
  42. if (subjects.Any())
  43. {
  44. bool change = false;
  45. foreach (var subject in subjects)
  46. {
  47. if (string.IsNullOrWhiteSpace(subject.bindId) && subject.name.Contains("语文"))
  48. {
  49. subject.bindId="subject_chinese";
  50. change= true;
  51. }
  52. if (string.IsNullOrWhiteSpace(subject.bindId) && subject.name.Contains("数学"))
  53. {
  54. subject.bindId="subject_math";
  55. change= true;
  56. }
  57. if (string.IsNullOrWhiteSpace(subject.bindId) && subject.name.Contains("英语"))
  58. {
  59. subject.bindId="subject_english";
  60. change= true;
  61. }
  62. if (string.IsNullOrWhiteSpace(subject.bindId) && subject.name.Contains("物理"))
  63. {
  64. subject.bindId="subject_physics";
  65. change= true;
  66. }
  67. if (string.IsNullOrWhiteSpace(subject.bindId) && subject.name.Contains("化学"))
  68. {
  69. subject.bindId="subject_chemistry";
  70. change= true;
  71. }
  72. if (string.IsNullOrWhiteSpace(subject.bindId) && subject.name.Contains("生物"))
  73. {
  74. subject.bindId="subject_biology";
  75. change= true;
  76. }
  77. if (string.IsNullOrWhiteSpace(subject.bindId) && subject.name.Contains("政治"))
  78. {
  79. subject.bindId="subject_politics";
  80. change= true;
  81. }
  82. if (string.IsNullOrWhiteSpace(subject.bindId) && subject.name.Contains("历史"))
  83. {
  84. subject.bindId="subject_history";
  85. change= true;
  86. }
  87. if (string.IsNullOrWhiteSpace(subject.bindId) && subject.name.Contains("地理"))
  88. {
  89. subject.bindId="subject_geography";
  90. change= true;
  91. }
  92. if (string.IsNullOrWhiteSpace(subject.bindId) && subject.name.Contains("音乐"))
  93. {
  94. subject.bindId="subject_music";
  95. change= true;
  96. }
  97. if (string.IsNullOrWhiteSpace(subject.bindId) && subject.name.Contains("美术"))
  98. {
  99. subject.bindId="subject_painting";
  100. change= true;
  101. }
  102. if (string.IsNullOrWhiteSpace(subject.bindId) && subject.name.Contains("劳动"))
  103. {
  104. subject.bindId="subject_labour";
  105. change= true;
  106. }
  107. if (string.IsNullOrWhiteSpace(subject.bindId) && subject.name.Contains("德育"))
  108. {
  109. subject.bindId="subject_virtue";
  110. change= true;
  111. }
  112. if (string.IsNullOrWhiteSpace(subject.bindId) && subject.name.Contains("体育"))
  113. {
  114. subject.bindId="subject_sport";
  115. change= true;
  116. }
  117. }
  118. //{
  119. // List<Period> periods = new List<Period>();
  120. // var hastype_period = school.period.Where(p => !string.IsNullOrWhiteSpace(p.periodType));
  121. // if (hastype_period.Any())
  122. // {
  123. // periods.AddRange(hastype_period);
  124. // }
  125. // var nottype_period = school.period.Where(p => string.IsNullOrWhiteSpace(p.periodType));
  126. // if (nottype_period!=null && nottype_period.Count()>0)
  127. // {
  128. // foreach (var period in nottype_period)
  129. // {
  130. // if (period.name.Contains("小学"))
  131. // {
  132. // change= true;
  133. // period.periodType= "primary";
  134. // }
  135. // if (period.name.Contains("初中"))
  136. // {
  137. // change= true;
  138. // period.periodType = "junior";
  139. // }
  140. // if (period.name.Contains("高中"))
  141. // {
  142. // change= true;
  143. // period.periodType = "senior";
  144. // }
  145. // if (string.IsNullOrWhiteSpace(period.periodType) && school.period.Count == 1)
  146. // {
  147. // if (school.name.Contains("小学"))
  148. // {
  149. // change= true;
  150. // period.periodType = "primary";
  151. // }
  152. // if (school.name.Contains("初中"))
  153. // {
  154. // change= true;
  155. // period.periodType = "junior";
  156. // }
  157. // if (school.name.Contains("高中"))
  158. // {
  159. // change= true;
  160. // period.periodType = "senior";
  161. // }
  162. // }
  163. // if (!string.IsNullOrWhiteSpace(period.periodType))
  164. // {
  165. // periods.Add(period);
  166. // }
  167. // }
  168. // }
  169. //}
  170. if (change)
  171. {
  172. School schoolSelf = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.School).UpsertItemAsync<School>(school, new PartitionKey("Base"));
  173. }
  174. }
  175. return school;
  176. }
  177. public static async Task<(int code, string msg, object data)> JoinScool(string school, string id, string name, string picture, long ts,
  178. AzureCosmosFactory _azureCosmos,AzureStorageFactory _azureStorage,CoreAPIHttpService _coreAPIHttpService,DingDing _dingDing,Option _option,IConfiguration _configuration, IWebHostEnvironment _environment)
  179. {
  180. SchoolTeacher schoolTeacher = null;
  181. School schoolInfo = null;
  182. long nowTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
  183. try
  184. {
  185. if (nowTime - ts > 3600000)
  186. {
  187. return (1, "超时!", null);
  188. }
  189. var client = _azureCosmos.GetCosmosClient();
  190. try
  191. {
  192. schoolInfo = await client.GetContainer(Constant.TEAMModelOS, "School").ReadItemAsync<School>($"{school}", new PartitionKey("Base"));
  193. }
  194. catch (CosmosException ex)
  195. {
  196. if (ex.Status == 404)
  197. {
  198. return (2, "该学校不存在!", null);
  199. }
  200. }
  201. Teacher teacher = null;
  202. try
  203. {
  204. teacher = await client.GetContainer(Constant.TEAMModelOS, "Teacher").ReadItemAsync<Teacher>($"{id}", new PartitionKey("Base"));
  205. var sch = teacher.schools.Find(x => x.schoolId.Equals($"{school}"));
  206. if (sch != null)
  207. {
  208. if (sch.status.Equals("join"))
  209. {
  210. return (3, "该教师之前已经加入学校!", null);
  211. }
  212. else
  213. {
  214. sch.status = "join";
  215. sch.time = nowTime;
  216. sch.name = schoolInfo?.name;
  217. sch.picture = schoolInfo?.picture;
  218. sch.areaId = schoolInfo?.areaId;
  219. }
  220. }
  221. else
  222. {
  223. teacher.schools.Add(new Teacher.TeacherSchool { areaId = schoolInfo?.areaId, schoolId = schoolInfo.id, status = "join", name = schoolInfo.name, picture = schoolInfo.picture, time = nowTime });
  224. }
  225. if (teacher.size < 2)
  226. {
  227. teacher.size = 2;
  228. }
  229. await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "Teacher").ReplaceItemAsync<Teacher>(teacher, teacher.id, new PartitionKey($"Base"));
  230. }
  231. catch (CosmosException ex)
  232. {
  233. if (ex.Status == 404)
  234. {
  235. //如果沒有,則初始化Teacher基本資料到Cosmos
  236. teacher = new Teacher
  237. {
  238. createTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
  239. id = id,
  240. pk = "Base",
  241. code = "Base",
  242. name = name?.ToString(),
  243. // picture = picture?.ToString(),
  244. //创建账号并第一次登录IES5则默认赠送1G
  245. defaultSchool = null,
  246. size = 2,
  247. schools = new List<Teacher.TeacherSchool>() { new Teacher.TeacherSchool { areaId = schoolInfo?.areaId, schoolId = schoolInfo.id, status = "join", name = schoolInfo.name, picture = schoolInfo.picture, time = nowTime } }
  248. };
  249. var container = _azureStorage.GetBlobContainerClient(id);
  250. await container.CreateIfNotExistsAsync(PublicAccessType.None); //嘗試創建Teacher私有容器,如存在則不做任何事,保障容器一定存在
  251. await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "Teacher").CreateItemAsync<Teacher>(teacher, new PartitionKey("Base"));
  252. }
  253. }
  254. Azure.Response response = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "School").ReadItemStreamAsync(teacher.id, new PartitionKey($"Teacher-{schoolInfo.id}"));
  255. if (response.Status != 200)
  256. {
  257. schoolTeacher = new SchoolTeacher
  258. {
  259. id = teacher.id,
  260. pk = "Teacher",
  261. name = teacher.name,
  262. picture = teacher.picture,
  263. size = 0,
  264. roles = new List<string> { "teacher" },
  265. permissions = new List<string>(),
  266. status = "join",
  267. createTime = nowTime,
  268. code = $"Teacher-{schoolInfo.id}",
  269. ttl = -1
  270. };
  271. await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "School").CreateItemAsync<SchoolTeacher>(schoolTeacher, new PartitionKey($"Teacher-{schoolInfo.id}"));
  272. }
  273. else
  274. {
  275. JsonElement data = JsonDocument.Parse(response.ContentStream).RootElement;
  276. schoolTeacher = data.ToObject<SchoolTeacher>();
  277. schoolTeacher.status = "join";
  278. schoolTeacher.name = schoolInfo.name;
  279. schoolTeacher.picture = schoolInfo.picture;
  280. await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "School").ReplaceItemAsync<SchoolTeacher>(schoolTeacher, schoolTeacher.id, new PartitionKey($"Teacher-{schoolInfo.id}"));
  281. }
  282. //通知管理员
  283. string sql = "select distinct value(c.id) from c where array_contains(c.roles,'admin')";
  284. List<string> ids = new List<string>();
  285. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<string>(queryText: sql,
  286. requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"Teacher-{school}") }))
  287. {
  288. ids.Add(item);
  289. }
  290. string bizcode = "scan-join";
  291. string tsql = $"select c.id, c.name ,c.lang as code from c where c.id in ({string.Join(",", ids.Select(x => $"'{x}'"))})";
  292. List<IdNameCode> idNameCodes = new List<IdNameCode>();
  293. await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.Teacher)
  294. .GetItemQueryIterator<IdNameCode>(queryText: tsql, requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey("Base") }))
  295. {
  296. idNameCodes.Add(item);
  297. }
  298. _coreAPIHttpService.PushNotify(idNameCodes, $"{bizcode}_school", Constant.NotifyType_IES5_Management,
  299. new Dictionary<string, object> { { "tmdname", teacher.name }, { "schoolName", schoolInfo.name }, { "schoolId", $"{school}" }, { "tmdid", teacher.id } }, _option.Location, _configuration, _dingDing, _environment.ContentRootPath);
  300. }
  301. catch (Exception ae)
  302. {
  303. await _dingDing.SendBotMsg($"HiTA扫码加入学校失败!{ae.Message},{ae.StackTrace}", GroupNames.成都开发測試群組);
  304. }
  305. return (200, "ok", new { schoolTeacher.roles, schoolTeacher.status, school = $"{school}", schoolInfo.name, schoolInfo.picture });
  306. }
  307. /// <summary>
  308. /// 根据年级获取入学年
  309. /// </summary>
  310. /// <param name="semesterList"></param>
  311. /// <returns></returns>
  312. public static (List<KeyValuePair<int, int>> gradeYear, HashSet<int> years) GetYears(School school, string periodId, IEnumerable<int> grades,long timestamp=0, string time = null) {
  313. var date = DateTimeOffset.UtcNow;
  314. //2001-09-09 09:46:40
  315. bool hasData = false;
  316. if (timestamp > 1000000000)
  317. {
  318. //毫秒级
  319. if (timestamp > 1000000000000)
  320. {
  321. date = DateTimeOffset.FromUnixTimeMilliseconds(timestamp);
  322. hasData = true;
  323. }
  324. //秒级
  325. else
  326. {
  327. date = DateTimeOffset.FromUnixTimeSeconds(timestamp);
  328. hasData =true;
  329. }
  330. }
  331. if (hasData==false)
  332. {
  333. if (!string.IsNullOrWhiteSpace(time))
  334. {
  335. if (DateTimeOffset.TryParse(time, out date))
  336. {
  337. if (date.ToUnixTimeMilliseconds()<1514736000000)
  338. {
  339. date = DateTimeOffset.UtcNow;
  340. }
  341. }
  342. else
  343. {
  344. date = DateTimeOffset.UtcNow;
  345. }
  346. }
  347. else
  348. {
  349. date = DateTimeOffset.UtcNow;
  350. }
  351. }
  352. //年级算法
  353. var period = school.period.Find(x => x.id.Equals(periodId));
  354. int Day = date.Day;
  355. int Month = date.Month;
  356. int Year = date.Year;
  357. int start = int.Parse($"{Year}0901");
  358. var se = period.semesters.Find(x => x.start == 1);
  359. if (se == null)
  360. {
  361. se = period.semesters.First();
  362. }
  363. string sm = "09";
  364. string sd = "01";
  365. if (se != null)
  366. {
  367. sm = se.month >= 10 ? $"{se.month}" : $"0{se.month}";
  368. sd = se.day >= 10 ? $"{se.day}" : $"0{se.day}";
  369. start = int.Parse($"{Year}{sm}{sd}");
  370. }
  371. int curr = int.Parse(date.ToString("yyyyMMdd"));
  372. //新学年开学时间大于当前时间,计算年级需要减1 20220901-20220408 > 0 则当前20220408是2021年入学的,
  373. //当前时间大于新学年开学时间,计算年级则不需要 20220901-20221203 < 1 则当前20221203是2022年入学的,
  374. //20230901-20230101 > 0 则当前20230101是2022年入学的,
  375. int dis = start - curr;
  376. List<int > years= new List<int>();
  377. List<KeyValuePair<int, int>> gradeYear = new List<KeyValuePair<int, int>>();
  378. foreach (int grade in grades) {
  379. int year = 0;
  380. if (dis > 0)
  381. {
  382. year = Year - grade -1;
  383. }
  384. else {
  385. year= Year - grade ;
  386. }
  387. years.Add(year);
  388. gradeYear.Add(new KeyValuePair<int, int>(grade,year ));
  389. }
  390. return (gradeYear,years.ToHashSet());
  391. }
  392. /// <summary>
  393. /// 根据年份获取年级,只返回time 或当前时间以前入学的学生年级。
  394. /// </summary>
  395. /// <param name="semesterList"></param>
  396. /// <returns></returns>
  397. public static (List<KeyValuePair<int, string>> yearGrade,HashSet<string> grades) GetGrades(School school,string periodId , IEnumerable<int> years,long timestamp = 0,string time =null) {
  398. var date = DateTimeOffset.UtcNow;
  399. //2001-09-09 09:46:40
  400. bool hasData = false;
  401. if (timestamp > 1000000000)
  402. {
  403. //毫秒级
  404. if (timestamp > 1000000000000)
  405. {
  406. date = DateTimeOffset.FromUnixTimeMilliseconds(timestamp);
  407. hasData = true;
  408. }
  409. //秒级
  410. else
  411. {
  412. date = DateTimeOffset.FromUnixTimeSeconds(timestamp);
  413. hasData =true;
  414. }
  415. }
  416. if (hasData==false)
  417. {
  418. if (!string.IsNullOrWhiteSpace(time))
  419. {
  420. if (DateTimeOffset.TryParse(time, out date))
  421. {
  422. if (date.ToUnixTimeMilliseconds()<1514736000000)
  423. {
  424. date = DateTimeOffset.UtcNow;
  425. }
  426. }
  427. else
  428. {
  429. date = DateTimeOffset.UtcNow;
  430. }
  431. }
  432. else
  433. {
  434. date = DateTimeOffset.UtcNow;
  435. }
  436. }
  437. //年级算法
  438. var period = school.period.Find(x => x.id.Equals(periodId));
  439. int? Count = period?.grades?.Count;
  440. List<KeyValuePair<int, string>> yearGrades = new List<KeyValuePair<int, string>>();
  441. if (Count.HasValue)
  442. {
  443. int Day = date.Day;
  444. int Month = date.Month;
  445. int Year = date.Year;
  446. int start = int.Parse($"{Year}0901");
  447. var se = period.semesters.Find(x => x.start == 1);
  448. if (se == null)
  449. {
  450. se = period.semesters.First();
  451. }
  452. string sm = "09";
  453. string sd = "01";
  454. if (se != null)
  455. {
  456. sm = se.month >= 10 ? $"{se.month}" : $"0{se.month}";
  457. sd = se.day >= 10 ? $"{se.day}" : $"0{se.day}";
  458. start = int.Parse($"{Year}{sm}{sd}");
  459. }
  460. int curr = int.Parse(date.ToString("yyyyMMdd"));
  461. //新学年开学时间大于当前时间,计算年级需要减1 20220901-20220408 > 0 则当前20220408是2021年入学的,
  462. //当前时间大于新学年开学时间,计算年级则不需要 20220901-20221203 < 1 则当前20221203是2022年入学的,
  463. //20230901-20230101 > 0 则当前20230101是2022年入学的,
  464. int dis = start - curr;
  465. foreach (int year in years)
  466. {
  467. if (int.Parse($"{year}{sm}{sd}") < curr) {
  468. int grade;
  469. if (dis > 0)
  470. {
  471. grade = Math.Abs((Year - year - 1)) % Count.Value;
  472. }
  473. else
  474. {
  475. grade = Math.Abs((Year - year)) % Count.Value;
  476. }
  477. yearGrades.Add(new KeyValuePair<int, string>(year, $"{grade}"));
  478. }
  479. }
  480. }
  481. return (yearGrades, yearGrades.Select(z=>z.Value).ToHashSet());
  482. }
  483. public static (Semester currSemester, int studyYear, DateTimeOffset date, DateTimeOffset nextSemester) GetSemesterNew(Period period, long timestamp = 0, string time = null)
  484. {
  485. //string time = "2023/05-11";
  486. DateTimeOffset date = DateTimeOffset.Now;
  487. bool hasData = false;
  488. if (timestamp > 1000000000)
  489. {
  490. //毫秒级
  491. if (timestamp > 1000000000000)
  492. {
  493. date = DateTimeOffset.FromUnixTimeMilliseconds(timestamp);
  494. hasData = true;
  495. }
  496. //秒级
  497. else
  498. {
  499. date = DateTimeOffset.FromUnixTimeSeconds(timestamp);
  500. hasData =true;
  501. }
  502. }
  503. if (hasData==false)
  504. {
  505. if (!string.IsNullOrWhiteSpace(time))
  506. {
  507. if (DateTimeOffset.TryParse(time, out date))
  508. {
  509. if (date.ToUnixTimeMilliseconds()<1514736000000) {
  510. date = DateTimeOffset.UtcNow;
  511. }
  512. }
  513. else
  514. {
  515. date = DateTimeOffset.UtcNow;
  516. }
  517. }
  518. else
  519. {
  520. date = DateTimeOffset.UtcNow;
  521. }
  522. }
  523. int Day = date.Day;
  524. int Month = date.Month;
  525. int Year = date.Year;
  526. string sm = "09";
  527. string sd = "01";
  528. int start = int.Parse($"{Year}{sm}{sd}");
  529. var se = period.semesters.Find(x => x.start == 1);
  530. if (se == null)
  531. {
  532. se = period.semesters.First();
  533. }
  534. if (se != null)
  535. {
  536. sm = se.month >= 10 ? $"{se.month}" : $"0{se.month}";
  537. sd = se.day >= 10 ? $"{se.day}" : $"0{se.day}";
  538. start = int.Parse($"{Year}{sm}{sd}");
  539. }
  540. int curr = int.Parse(date.ToString("yyyyMMdd"));
  541. //新学年开学时间大于当前时间,计算年级需要减1 20220901-20220408 > 0 则当前20220408是2021年入学的,
  542. //当前时间大于新学年开学时间,计算年级则不需要 20220901-20221203 < 0 则当前20221203是2022年入学的,
  543. //20230901-20230101 > 0 则当前20230101是2022年入学的,
  544. int dis = start - curr;
  545. //学年
  546. int studyYear = date.Year;
  547. if (dis > 0)
  548. {
  549. studyYear = Year - 1;
  550. }
  551. else
  552. {
  553. studyYear = Year;
  554. }
  555. Semester semester = null;
  556. //当前学期的下一个学期,默认加180天
  557. DateTimeOffset nextSemester = date.AddDays(180);
  558. var sortSemesters = SortSemester(period.semesters);
  559. int len = sortSemesters.Count;
  560. for (int i = 0; i < len; i++)
  561. {
  562. int next = i + 1;
  563. if (next>= len)
  564. {
  565. semester = sortSemesters[i];
  566. nextSemester = DateTimeOffset.Parse($"{studyYear+1}/{sortSemesters[0].month}/{sortSemesters[0].day}");
  567. break;
  568. }
  569. else
  570. {
  571. var fdate = DateTimeOffset.Parse($"{studyYear}/{sortSemesters[i].month}/{sortSemesters[i].day}");
  572. long lf = fdate.ToUnixTimeSeconds();
  573. var ndate = DateTimeOffset.Parse($"{studyYear}/{sortSemesters[next].month}/{sortSemesters[next].day}");
  574. long ln = ndate.ToUnixTimeSeconds();
  575. //代表有跨年
  576. if (lf > ln)
  577. {
  578. ndate = DateTimeOffset.Parse($"{studyYear+1}/{sortSemesters[next].month}/{sortSemesters[next].day}");
  579. ln = ndate.ToUnixTimeSeconds();
  580. }
  581. var cdate = DateTimeOffset.Parse($"{Year}/{Month}/{Day}");
  582. long lc = cdate.ToUnixTimeSeconds();
  583. if (lc >= lf && lc < ln)
  584. {
  585. semester = sortSemesters[i];
  586. nextSemester = DateTimeOffset.Parse($"{studyYear+1}/{sortSemesters[next].month}/{sortSemesters[next].day}");
  587. //nextSemester = sortSemesters[next];
  588. break;
  589. }
  590. //if (sortSemesters[i].month <=Month && sortSemesters[i].day >= Day && sortSemesters[next].month < Month && sortSemesters[next].day < Day)
  591. //{
  592. // semester = sortSemesters[i];
  593. // nextSemester = DateTimeOffset.Parse($"{studyYear}/{sortSemesters[next].month}/{sortSemesters[next].day}");
  594. // break;
  595. //}
  596. }
  597. }
  598. return (semester, studyYear, date, nextSemester);
  599. }
  600. /// <summary>
  601. /// 根据指定时间或者当前时间获取学期信息
  602. /// </summary>
  603. /// <param name="semesterList"></param>
  604. /// <returns></returns>
  605. public static (Semester currSemester, int studyYear, DateTimeOffset currSemesterDate, DateTimeOffset date, DateTimeOffset nextSemester) GetSemester( Period period, long timestamp = 0, string time =null) {
  606. //string time = "2023/05-11";
  607. DateTimeOffset date = default;
  608. bool hasData=false;
  609. if (timestamp > 1000000000) {
  610. //毫秒级
  611. if (timestamp > 1000000000000)
  612. {
  613. date = DateTimeOffset.FromUnixTimeMilliseconds(timestamp);
  614. hasData=true;
  615. }
  616. //秒级
  617. else {
  618. date = DateTimeOffset.FromUnixTimeSeconds(timestamp);
  619. hasData=true;
  620. }
  621. }
  622. if (hasData==false) {
  623. if (!string.IsNullOrWhiteSpace(time))
  624. {
  625. if (DateTimeOffset.TryParse(time, out date))
  626. {
  627. if (date.ToUnixTimeMilliseconds()<1514736000000)
  628. {
  629. date = DateTimeOffset.UtcNow;
  630. }
  631. }
  632. else
  633. {
  634. date = DateTimeOffset.UtcNow;
  635. }
  636. }
  637. else
  638. {
  639. date = DateTimeOffset.UtcNow;
  640. }
  641. }
  642. //年级算法
  643. //var period = school.period.Find(x => x.id.Equals(periodId));
  644. List<KeyValuePair<int, string>> yearGrades = new List<KeyValuePair<int, string>>();
  645. int Day = date.Day;
  646. int Month = date.Month;
  647. int Year = date.Year;
  648. string sm = "09";
  649. string sd = "01";
  650. int start = int.Parse($"{Year}{sm}{sd}");
  651. var se = period.semesters.Find(x => x.start == 1);
  652. if (se == null)
  653. {
  654. se = period.semesters.First();
  655. }
  656. if (se != null)
  657. {
  658. sm = se.month >= 10 ? $"{se.month}" : $"0{se.month}";
  659. sd = se.day >= 10 ? $"{se.day}" : $"0{se.day}";
  660. start = int.Parse($"{Year}{sm}{sd}");
  661. }
  662. int curr = int.Parse(date.ToString("yyyyMMdd"));
  663. //新学年开学时间大于当前时间,计算年级需要减1 20220901-20220408 > 0 则当前20220408是2021年入学的,
  664. //当前时间大于新学年开学时间,计算年级则不需要 20220901-20221203 < 0 则当前20221203是2022年入学的,
  665. //20230901-20230101 > 0 则当前20230101是2022年入学的,
  666. int dis = start - curr;
  667. //学年
  668. int studyYear= date.Year;
  669. if (dis > 0)
  670. {
  671. studyYear = Year - 1;
  672. }
  673. else
  674. {
  675. studyYear = Year;
  676. }
  677. Semester semester= null;
  678. //当前学期的下一个学期,默认加180天
  679. DateTimeOffset nextSemester = date.AddDays(180);
  680. DateTimeOffset currSemesterDate = default;
  681. var sortSemesters= SortSemester(period.semesters);
  682. int len = sortSemesters.Count;
  683. int currSemesterYear = studyYear;
  684. for (int i = 0; i < len; i++) {
  685. int next= i + 1;
  686. if (next>= len)
  687. {
  688. semester = sortSemesters[i];
  689. nextSemester = DateTimeOffset.Parse($"{studyYear+1}/{sortSemesters[0].month}/{sortSemesters[0].day}");
  690. currSemesterDate=DateTimeOffset.Parse($"{currSemesterYear}/{semester.month}/{semester.day}");
  691. if (currSemesterDate.Subtract(nextSemester).TotalDays>0) {
  692. currSemesterDate=DateTimeOffset.Parse($"{currSemesterYear-1}/{semester.month}/{semester.day}");
  693. }
  694. break;
  695. }
  696. else {
  697. var fdate= DateTimeOffset.Parse($"{studyYear}/{sortSemesters[i].month}/{sortSemesters[i].day}");
  698. long lf = fdate.ToUnixTimeSeconds();
  699. var ndate = DateTimeOffset.Parse($"{studyYear}/{sortSemesters[next].month}/{sortSemesters[next].day}");
  700. long ln = ndate.ToUnixTimeSeconds();
  701. //代表有跨年
  702. if (lf > ln) {
  703. currSemesterYear=studyYear+1;
  704. ndate = DateTimeOffset.Parse($"{studyYear+1}/{sortSemesters[next].month}/{sortSemesters[next].day}");
  705. ln = ndate.ToUnixTimeSeconds();
  706. }
  707. var cdate = DateTimeOffset.Parse($"{Year}/{Month}/{Day}");
  708. long lc = cdate.ToUnixTimeSeconds();
  709. if (lc >= lf && lc < ln) {
  710. semester = sortSemesters[i];
  711. nextSemester = DateTimeOffset.Parse($"{studyYear+1}/{sortSemesters[next].month}/{sortSemesters[next].day}");
  712. currSemesterDate=DateTimeOffset.Parse($"{currSemesterYear}/{semester.month}/{semester.day}");
  713. if (currSemesterDate.Subtract(nextSemester).TotalDays>0)
  714. {
  715. currSemesterDate=DateTimeOffset.Parse($"{currSemesterYear-1}/{semester.month}/{semester.day}");
  716. }
  717. break;
  718. }
  719. //if (sortSemesters[i].month <=Month && sortSemesters[i].day >= Day && sortSemesters[next].month < Month && sortSemesters[next].day < Day)
  720. //{
  721. // semester = sortSemesters[i];
  722. // nextSemester = DateTimeOffset.Parse($"{studyYear}/{sortSemesters[next].month}/{sortSemesters[next].day}");
  723. // break;
  724. //}
  725. }
  726. }
  727. return (semester,studyYear, currSemesterDate, date, nextSemester);
  728. }
  729. /// <summary>
  730. /// 处理学期排序
  731. /// </summary>
  732. /// <param name="semesterList"></param>
  733. /// <returns></returns>
  734. public static List<Semester> SortSemester(List<Semester> semesterList)
  735. {
  736. int Year = DateTimeOffset.UtcNow.Year;
  737. List<KeyValuePair<int, Semester>> pairs = new List<KeyValuePair<int, Semester>>();
  738. semesterList.ForEach(se => {
  739. string sm = se.month >= 10 ? $"{se.month}" : $"0{se.month}";
  740. string sd = se.day >= 10 ? $"{se.day}" : $"0{se.day}";
  741. int order = int.Parse($"{Year}{sm}{sd}");
  742. pairs.Add(new KeyValuePair<int, Semester>(order, se));
  743. });
  744. var orderPairs = pairs.OrderBy(z => z.Key);
  745. semesterList = orderPairs.Select(z => z.Value).ToList();
  746. int startIndex = semesterList.FindIndex(z => z.start == 1);
  747. if (startIndex == -1)
  748. {
  749. //未设置学期的情况默认9月份开始的。
  750. startIndex = semesterList.FindIndex(z => z.month == 9);
  751. //如果还未找到,就以排序结果为准
  752. if (startIndex == -1)
  753. {
  754. startIndex = 0;
  755. }
  756. }
  757. if (startIndex > 0)
  758. {
  759. List<Semester> before = semesterList.Take(startIndex).ToList();
  760. List<Semester> after = semesterList.Skip(startIndex).ToList();
  761. semesterList = new List<Semester>();
  762. semesterList.AddRange(after);
  763. semesterList.AddRange(before);
  764. return semesterList;
  765. }
  766. else
  767. {
  768. return semesterList;
  769. }
  770. }
  771. /// <summary>
  772. /// 获取指定学年,指定学期的开学时间。
  773. /// </summary>
  774. /// <param name="semesterList"></param>
  775. /// <returns></returns>
  776. public static string GetOpensByStudyYearAndSemester(List<Semester> semesterList,int year ,string semesterId)
  777. {
  778. int Year = DateTimeOffset.UtcNow.Year;
  779. List<KeyValuePair<int, Semester>> pairs = new List<KeyValuePair<int, Semester>>();
  780. semesterList.ForEach(se => {
  781. string sm = se.month >= 10 ? $"{se.month}" : $"0{se.month}";
  782. string sd = se.day >= 10 ? $"{se.day}" : $"0{se.day}";
  783. int order = int.Parse($"{Year}{sm}{sd}");
  784. pairs.Add(new KeyValuePair<int, Semester>(order, se));
  785. });
  786. var orderPairs = pairs.OrderBy(z => z.Key);
  787. semesterList = orderPairs.Select(z => z.Value).ToList();
  788. int startIndex = semesterList.FindIndex(z => z.start == 1);
  789. if (startIndex == -1)
  790. {
  791. //未设置学期的情况默认9月份开始的。
  792. startIndex = semesterList.FindIndex(z => z.month == 9);
  793. //如果还未找到,就以排序结果为准
  794. if (startIndex == -1)
  795. {
  796. startIndex = 0;
  797. }
  798. }
  799. if (startIndex > 0)
  800. {
  801. List<Semester> before = semesterList.Take(startIndex).ToList();
  802. List<Semester> after = semesterList.Skip(startIndex).ToList();
  803. semesterList = new List<Semester>();
  804. semesterList.AddRange(after);
  805. semesterList.AddRange(before);
  806. }
  807. string time = string.Empty;
  808. for (int i = 0; i<semesterList.Count; i++) {
  809. if (semesterList[i].id.Equals(semesterId)) {
  810. if (i==0)
  811. {
  812. time=$"{year}-{semesterList[i].month}-{semesterList[i].day}";
  813. }
  814. else {
  815. string sm1 = semesterList[i-1].month >= 10 ? $"{semesterList[i-1].month}" : $"0{semesterList[i-1].month}";
  816. string sd1 = semesterList[i-1].day >= 10 ? $"{semesterList[i-1].day}" : $"0{semesterList[i - 1].day}";
  817. int order1 = int.Parse($"{Year}{sm1}{sd1}");
  818. string sm2 = semesterList[i].month >= 10 ? $"{semesterList[i].month}" : $"0{semesterList[i].month}";
  819. string sd2 = semesterList[i].day >= 10 ? $"{semesterList[i].day}" : $"0{semesterList[i].day}";
  820. int order2 = int.Parse($"{Year}{sm2}{sd2}");
  821. if (order2<order1)
  822. {
  823. time=$"{(year+1)}-{semesterList[i].month}-{semesterList[i].day}";
  824. }
  825. else {
  826. time=$"{year}-{semesterList[i].month}-{semesterList[i].day}";
  827. }
  828. }
  829. }
  830. }
  831. return time;
  832. }
  833. /// <summary>
  834. /// 校验
  835. /// </summary>
  836. /// <param name="courseTaskInsert"></param>
  837. public static ScheduleTaskDto CheckCourseTask(string scope, ScheduleTaskDto courseTaskInsert, string school, List<GroupListDto> groupListDtos, List<Room> rooms,
  838. List<SchoolTeacher> schoolTeachers, List<Teacher> teachers)
  839. {
  840. if (scope.Equals("school", StringComparison.OrdinalIgnoreCase))
  841. {
  842. //教师
  843. var schoolteachers = schoolTeachers.Where(z => z.id.Equals(courseTaskInsert.teacherId));
  844. if (!schoolteachers.Any())
  845. {
  846. courseTaskInsert.invalidCode = 2;//教师不存在
  847. return courseTaskInsert;
  848. }
  849. var invalidAssistants= courseTaskInsert.assistants.Except(schoolTeachers.Select(x => x.id));
  850. if (invalidAssistants != null && invalidAssistants.Any())
  851. {
  852. courseTaskInsert.invalidCode = 21;
  853. courseTaskInsert.invalidMsg = $"{string.Join(",", invalidAssistants)}";
  854. return courseTaskInsert;
  855. }
  856. }
  857. else
  858. {
  859. //教师
  860. var teachersbase = teachers.Where(z => z.id.Equals(courseTaskInsert.teacherId));
  861. if (!teachersbase.Any())
  862. {
  863. courseTaskInsert.invalidCode = 2;//教师不存在
  864. return courseTaskInsert;
  865. }
  866. var invalidAssistants = courseTaskInsert.assistants.Except(teachers.Select(x => x.id));
  867. if (invalidAssistants != null && invalidAssistants.Any())
  868. {
  869. courseTaskInsert.invalidCode = 21;
  870. courseTaskInsert.invalidMsg = $"{string.Join(",", invalidAssistants)}";
  871. return courseTaskInsert;
  872. }
  873. }
  874. //名单
  875. var groupList = groupListDtos.Where(z => z.id.Equals(courseTaskInsert.groupId) && z.type.Equals(courseTaskInsert.type));
  876. if (!groupList.Any())
  877. {
  878. courseTaskInsert.invalidCode = 3;//名单不存在
  879. return courseTaskInsert;
  880. }
  881. else
  882. {
  883. if (scope.Equals("private", StringComparison.OrdinalIgnoreCase))
  884. {
  885. if (!groupList.First().scope.Equals("private", StringComparison.OrdinalIgnoreCase) && !string.IsNullOrWhiteSpace(school) && school.Equals(groupList.First().school))
  886. {
  887. courseTaskInsert.school = school;
  888. }
  889. }
  890. else
  891. {
  892. courseTaskInsert.school = school;
  893. }
  894. }
  895. //教室
  896. if (!string.IsNullOrWhiteSpace(courseTaskInsert.roomId))
  897. {
  898. var room = rooms.Where(z => z.id.Equals(courseTaskInsert.roomId));
  899. if (!room.Any())
  900. {
  901. courseTaskInsert.invalidCode = 4;//教室不存在
  902. return courseTaskInsert;
  903. }
  904. }
  905. courseTaskInsert.invalidCode = 0;
  906. return courseTaskInsert;
  907. }
  908. /// <summary>
  909. /// 校验
  910. /// </summary>
  911. /// <param name="courseTaskInsert"></param>
  912. public static CourseTaskChanged CheckCourseTaskInsertOrChanged(string grant_type, string scope , CourseTaskChanged courseTaskInsert,string school,Period period, List<CourseBase> courseBases, List<GroupListDto> groupListDtos, List<Room> rooms,
  913. List<SchoolTeacher> schoolTeachers,List<Teacher> teachers)
  914. {
  915. if (scope.Equals("school", StringComparison.OrdinalIgnoreCase))
  916. { //学年
  917. if (courseTaskInsert.year < 2000)
  918. {
  919. courseTaskInsert.invalidCode = 17;//学年应大于2000
  920. return courseTaskInsert;
  921. }
  922. //学期
  923. var semester = period.semesters.Where(z => z.id.Equals(courseTaskInsert.semesterId));
  924. if (!semester.Any())
  925. {
  926. courseTaskInsert.invalidCode = 16;//学期不存在
  927. return courseTaskInsert;
  928. }
  929. //教师
  930. var schoolteachers = schoolTeachers.Where(z => z.id.Equals(courseTaskInsert.teacherId));
  931. if (!schoolteachers.Any())
  932. {
  933. courseTaskInsert.invalidCode = 2;//教师不存在
  934. return courseTaskInsert;
  935. }
  936. }
  937. else {
  938. //教师
  939. var teachersbase = teachers.Where(z => z.id.Equals(courseTaskInsert.teacherId));
  940. if (!teachersbase.Any())
  941. {
  942. courseTaskInsert.invalidCode = 2;//教师不存在
  943. return courseTaskInsert;
  944. }
  945. }
  946. //课程
  947. var courseBase = courseBases.Where(z => z.id.Equals(courseTaskInsert.courseId));
  948. if (!courseBase.Any())
  949. {
  950. courseTaskInsert.invalidCode = 1;//课程不存在
  951. return courseTaskInsert;
  952. }
  953. //名单
  954. var groupList = groupListDtos.Where(z => z.id.Equals(courseTaskInsert.groupId) && z.type.Equals(courseTaskInsert.type));
  955. if (!groupList.Any())
  956. {
  957. courseTaskInsert.invalidCode = 3;//名单不存在
  958. return courseTaskInsert;
  959. }
  960. else {
  961. if (scope.Equals("private", StringComparison.OrdinalIgnoreCase))
  962. {
  963. if (!groupList.First().scope.Equals("private", StringComparison.OrdinalIgnoreCase) && !string.IsNullOrWhiteSpace(school) && school.Equals(groupList.First().school))
  964. {
  965. courseTaskInsert.school = school;
  966. }
  967. }
  968. else {
  969. courseTaskInsert.school = school;
  970. }
  971. }
  972. //教室
  973. if (!string.IsNullOrWhiteSpace(courseTaskInsert.roomId))
  974. {
  975. var room = rooms.Where(z => z.id.Equals(courseTaskInsert.roomId));
  976. if (!room.Any())
  977. {
  978. courseTaskInsert.invalidCode = 4;//教室不存在
  979. return courseTaskInsert;
  980. }
  981. }
  982. if (grant_type.Equals("change-scheduleTask", StringComparison.OrdinalIgnoreCase)) {
  983. if (!string.IsNullOrWhiteSpace(courseTaskInsert.teacherIdChanged) || (!string.IsNullOrWhiteSpace(courseTaskInsert.groupIdChanged) && !string.IsNullOrWhiteSpace(courseTaskInsert.typeChanged)))
  984. {
  985. if (!string.IsNullOrWhiteSpace(courseTaskInsert.teacherIdChanged)) {
  986. if (scope.Equals("school", StringComparison.OrdinalIgnoreCase))
  987. {
  988. //教师
  989. var schoolteachers = schoolTeachers.Where(z => z.id.Equals(courseTaskInsert.teacherIdChanged));
  990. if (!schoolteachers.Any())
  991. {
  992. courseTaskInsert.invalidCode = 19;//教师不存在
  993. return courseTaskInsert;
  994. }
  995. }
  996. else {
  997. //教师
  998. var teachersbase = teachers.Where(z => z.id.Equals(courseTaskInsert.teacherIdChanged));
  999. if (!teachersbase.Any())
  1000. {
  1001. courseTaskInsert.invalidCode = 19;//教师不存在
  1002. return courseTaskInsert;
  1003. }
  1004. }
  1005. }
  1006. if (!string.IsNullOrWhiteSpace(courseTaskInsert.groupIdChanged) && !string.IsNullOrWhiteSpace(courseTaskInsert.typeChanged)) {
  1007. //名单
  1008. var groupListChanged = groupListDtos.Where(z => z.id.Equals(courseTaskInsert.groupIdChanged) && z.type.Equals(courseTaskInsert.typeChanged));
  1009. if (!groupListChanged.Any())
  1010. {
  1011. courseTaskInsert.invalidCode = 18;//名单不存在
  1012. return courseTaskInsert;
  1013. }
  1014. else
  1015. {
  1016. if (scope.Equals("private", StringComparison.OrdinalIgnoreCase))
  1017. {
  1018. if (!groupListChanged.First().scope.Equals("private", StringComparison.OrdinalIgnoreCase) && !string.IsNullOrWhiteSpace(school) && school.Equals(groupList.First().school))
  1019. {
  1020. courseTaskInsert.school = school;
  1021. }
  1022. }
  1023. else
  1024. {
  1025. courseTaskInsert.school = school;
  1026. }
  1027. }
  1028. }
  1029. }
  1030. else {
  1031. courseTaskInsert.invalidCode = 17;
  1032. }
  1033. }
  1034. courseTaskInsert.invalidCode = 0;
  1035. return courseTaskInsert;
  1036. }
  1037. 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) {
  1038. List<Class> school_classes = new List<Class>();
  1039. List<Class> graduate_classes = new List<Class>();
  1040. try
  1041. {
  1042. var client = _azureCosmos.GetCosmosClient();
  1043. //取得班级
  1044. int nowYear = DateTimeOffset.UtcNow.Year;
  1045. int nowMonth = DateTimeOffset.UtcNow.Month;
  1046. int nowDay = DateTimeOffset.UtcNow.Day;
  1047. //因为修改年级,而导致取消的
  1048. List<Class> cancel_graduate_classes = new List<Class>();
  1049. string sql = $"SELECT value c FROM c where (c.graduate = 0 or IS_DEFINED(c.graduate) = false) ";
  1050. if (periodIds.IsNotEmpty())
  1051. {
  1052. sql = $"SELECT value c FROM c where c.periodId in ({string.Join(",", periodIds.Select(x => $"'{x}'"))}) ";
  1053. }
  1054. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<Class>
  1055. //(queryText: $"SELECT value c FROM c where (c.graduate = 0 or IS_DEFINED(c.graduate) = false)",
  1056. (queryText: sql,
  1057. requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Class-{school_base.id}") }))
  1058. {
  1059. bool isgraduate = false;
  1060. if (!string.IsNullOrWhiteSpace(item.periodId))
  1061. {
  1062. var period = school_base.period.Find(x => x.id.Equals(item.periodId));
  1063. if (period != null)
  1064. {
  1065. var gradeCount = period.grades.Count();
  1066. //2022-2016=6(待判断月份日期是否是毕业) 2022-2017=5(未毕业)
  1067. if (nowYear - item.year > gradeCount)
  1068. {
  1069. isgraduate = true;
  1070. }
  1071. else if (nowYear - item.year == gradeCount)
  1072. {
  1073. var semester = period.semesters.Find(x => x.start == 1);
  1074. if (semester != null)
  1075. {
  1076. if (nowMonth > semester.month)
  1077. {
  1078. isgraduate = true;
  1079. }
  1080. else if (nowMonth == semester.month)
  1081. {
  1082. if (nowDay >= semester.day)
  1083. {
  1084. isgraduate = true;
  1085. }
  1086. else
  1087. {
  1088. }
  1089. }
  1090. else { isgraduate = false; }
  1091. }
  1092. }
  1093. else
  1094. {
  1095. isgraduate = false;
  1096. }
  1097. }
  1098. }
  1099. if (isgraduate)
  1100. {
  1101. graduate_classes.Add(item);
  1102. }
  1103. else
  1104. {
  1105. if (item.graduate == 1)
  1106. {
  1107. item.graduate = 0;
  1108. cancel_graduate_classes.Add(item);
  1109. }
  1110. school_classes.Add(item);
  1111. }
  1112. }
  1113. if (graduate_classes.Any() || cancel_graduate_classes.Any())
  1114. {
  1115. if (waite == 0)
  1116. {
  1117. _ = _httpTrigger.RequestHttpTrigger(new { graduate_classes = graduate_classes, cancel_graduate_classes = cancel_graduate_classes, schoolId = $"{school_base.id}" }, _option.Location, "graduate-change");
  1118. }
  1119. else
  1120. {
  1121. await _httpTrigger.RequestHttpTrigger(new { graduate_classes = graduate_classes, cancel_graduate_classes = cancel_graduate_classes, schoolId = $"{school_base.id}" }, _option.Location, "graduate-change");
  1122. }
  1123. }
  1124. } catch (Exception ex) {
  1125. await _dingDing.SendBotMsg($"{_option.Location},{ex.Message}\n{ex.StackTrace}", GroupNames.醍摩豆服務運維群組);
  1126. }
  1127. return (school_classes,graduate_classes);
  1128. }
  1129. public static async Task<object> DoScsApiSchool(HttpTrigger _httpTrigger,Dictionary<string,object> dict, Option _option, List<IdNameCode> ignore,
  1130. string areaId,string city,string dist, AzureStorageFactory _azureStorage ,DingDing _dingDing, IWebHostEnvironment _environment
  1131. )
  1132. {
  1133. List<ScSchool> tbschools = null;
  1134. List<ScSchool> matchSchools = null;
  1135. List<ScSchool> schools = null;
  1136. List<ScSchool> saveschools = null;
  1137. var table = _azureStorage.GetCloudTableClient().GetTableReference("ScYxpt");
  1138. // 5.3.1.18根据机构ID、项目ID、子项目ID返回学校列表
  1139. ( int status, string json) = await _httpTrigger.RequestHttpTrigger(dict, _option.Location, "GetSchoolList");
  1140. if (status == 200)
  1141. {
  1142. schools = json.ToObject<List<ScSchool>>(new JsonSerializerOptions { PropertyNameCaseInsensitive = false });
  1143. if (ignore.IsNotEmpty())
  1144. {
  1145. matchSchools = schools.FindAll(x => ignore.Select(y => y.name).Contains(x.schoolname));
  1146. if (matchSchools.IsNotEmpty())
  1147. {
  1148. if (matchSchools.Count != ignore.Count)
  1149. {
  1150. var matched = matchSchools.Select(x => x.schoolname);
  1151. var unmatch = ignore.Select(y => y.name).Except(matched);
  1152. List<string> scschoolUnmatch = schools.Select(y => y.schoolname).Except(matched).ToList();
  1153. if (scschoolUnmatch.IsNotEmpty())
  1154. {
  1155. return new { matched, unmatch, scschoolUnmatch };
  1156. }
  1157. }
  1158. else
  1159. {
  1160. matchSchools.ForEach(x => {
  1161. var exschool = ignore.Find(y => y.name.Equals(x.schoolname));
  1162. if (exschool != null)
  1163. {
  1164. x.schoolCode = exschool.id;
  1165. x.areaId = $"{areaId}";
  1166. x.city = $"{city}";
  1167. x.dist = $"{dist}";
  1168. }
  1169. });
  1170. }
  1171. }
  1172. else
  1173. {
  1174. return new { unmatch = ignore, schools };
  1175. }
  1176. }
  1177. //数据校验
  1178. tbschools = await table.FindListByDict<ScSchool>(new Dictionary<string, object>() { { "PartitionKey", "ScSchool" } });
  1179. if (tbschools.IsNotEmpty())
  1180. {
  1181. var a = tbschools.Select(y => $"{y.RowKey}").ToList();
  1182. saveschools = schools.Where(x => !a.Exists(z => z.Equals($"{x.schoolid}"))).ToList();
  1183. List<SchoolData> schoolDatas = new List<SchoolData>();
  1184. saveschools.ForEach(x =>
  1185. {
  1186. x.RowKey = $"{x.schoolid}";
  1187. x.PartitionKey = "ScSchool";
  1188. x.areaId = $"{areaId}";
  1189. x.city = $"{city}";
  1190. x.dist = $"{dist}";
  1191. if (string.IsNullOrEmpty(x.schoolCode))
  1192. {
  1193. if (string.IsNullOrEmpty(x.schoolCode))
  1194. {
  1195. schoolDatas.Add(new SchoolData { uid = $"{x.schoolid}", province = "四川省", city = $"{city}", name = x.schoolname });
  1196. }
  1197. }
  1198. });
  1199. schoolDatas = await SchoolService.GenerateSchoolCode(schoolDatas, _dingDing, _environment);
  1200. saveschools.ForEach(x => {
  1201. var schoolData = schoolDatas.Find(y => y.uid.Equals($"{x.schoolid}"));
  1202. if (schoolData != null && !string.IsNullOrEmpty(schoolData.id))
  1203. {
  1204. x.schoolCode = schoolData.id;
  1205. x.areaId = $"{areaId}";
  1206. x.city = $"{city}";
  1207. x.dist = $"{dist}";
  1208. }
  1209. });
  1210. saveschools.RemoveAll(x => string.IsNullOrEmpty(x.schoolCode));
  1211. saveschools.RemoveAll(x => tbschools.FindAll(z => !string.IsNullOrEmpty(z.schoolCode)).Exists(y => y.schoolCode.Equals(x.schoolCode)));
  1212. saveschools = await table.SaveAll(saveschools);
  1213. }
  1214. else
  1215. {
  1216. List<SchoolData> schoolDatas = new List<SchoolData>();
  1217. schools.ForEach(x =>
  1218. {
  1219. x.RowKey = $"{x.schoolid}";
  1220. x.PartitionKey = "ScSchool";
  1221. x.areaId = $"{areaId}";
  1222. x.city = $"{city}";
  1223. x.dist = $"{dist}";
  1224. var a = ignore.Find(z => z.name.Equals(x.schoolname));
  1225. if (a != null)
  1226. {
  1227. x.schoolCode = a.id;
  1228. }
  1229. else
  1230. {
  1231. if (string.IsNullOrEmpty(x.schoolCode))
  1232. {
  1233. schoolDatas.Add(new SchoolData { uid = $"{x.schoolid}", province = "四川省", city = $"{city}", name = x.schoolname });
  1234. }
  1235. }
  1236. });
  1237. schoolDatas = await SchoolService.GenerateSchoolCode(schoolDatas, _dingDing, _environment);
  1238. schools.ForEach(x => {
  1239. var schoolData = schoolDatas.Find(y => y.uid.Equals($"{x.schoolid}"));
  1240. if (schoolData != null && !string.IsNullOrEmpty(schoolData.id))
  1241. {
  1242. x.schoolCode = schoolData.id;
  1243. x.areaId = $"{areaId}";
  1244. x.city = $"{city}";
  1245. x.dist = $"{dist}";
  1246. }
  1247. });
  1248. schools.RemoveAll(x => string.IsNullOrEmpty(x.schoolCode));
  1249. saveschools = await table.SaveOrUpdateAll(schools);
  1250. }
  1251. }
  1252. return null;
  1253. }
  1254. public static async Task<List<SchoolData>> GenerateSchoolCode(List<SchoolData> schools, DingDing _dingDing, IWebHostEnvironment _environment)
  1255. {
  1256. string path = $"{_environment.ContentRootPath}/JsonFile/Core/region.json";
  1257. //var schools = jsonstr.ToObject<JsonElement>().GetProperty("schools").ToObject<List<SchoolData>>();
  1258. schools = schools.Where((x, i) => schools.FindIndex(n => n.name.Equals(x.name)) == i).ToList();
  1259. StreamReader streamReader = new StreamReader(new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite), Encoding.UTF8);
  1260. StringBuilder stringBuilder = new StringBuilder();
  1261. string text;
  1262. while ((text = streamReader.ReadLine()) != null)
  1263. {
  1264. stringBuilder.Append(text.ToString());
  1265. }
  1266. streamReader.Close();
  1267. string input = stringBuilder.ToString();
  1268. List<Region> region = input.ToObject<List<Region>>();
  1269. try
  1270. {
  1271. schools.ForEach(x =>
  1272. {
  1273. x.province = ChineseConverter.Convert(x.province, ChineseConversionDirection.TraditionalToSimplified);
  1274. if (String.IsNullOrEmpty(x.city))
  1275. {
  1276. x.city = x.province;
  1277. }
  1278. else
  1279. {
  1280. x.city = ChineseConverter.Convert(x.city, ChineseConversionDirection.TraditionalToSimplified);
  1281. }
  1282. x.name = ChineseConverter.Convert(x.name, ChineseConversionDirection.TraditionalToSimplified);
  1283. comeRemoveStr.ForEach(c => { x.province = x.province.Replace(c, ""); });
  1284. prvcRemoveStr.ForEach(c => { x.province = x.province.Replace(c, ""); });
  1285. var province = region.Find(r => r.name.Contains(x.province));
  1286. string tmpprovince = x.province;
  1287. string tmpcity = x.city;
  1288. if (province != null)
  1289. {
  1290. x.province = province.name;
  1291. comeRemoveStr.ForEach(c => { x.city = x.city.Replace(c, ""); });
  1292. cityRemoveStr.ForEach(c => { x.city = x.city.Replace(c, ""); });
  1293. nationRemoveStr.ForEach(c => { x.city = x.city.Replace(c, ""); });
  1294. tmpcity = x.city;
  1295. var city = province.children.Find(r => r.name.Contains(x.city));
  1296. if (city == null)
  1297. {
  1298. city = province.children.Find(r => r.name.Contains(x.city));
  1299. if (city == null)
  1300. {
  1301. city = province.children.SelectMany(x => x.children).ToList().Find(r => r.name.Contains(x.city));
  1302. }
  1303. }
  1304. if (city != null)
  1305. {
  1306. x.city = city.name;
  1307. }
  1308. }
  1309. string name = x.name;
  1310. //去除冗余的学校信息,只保留地名和校名关键信息
  1311. schemoveStr.ForEach(str =>
  1312. {
  1313. name = name.Replace(str, "");
  1314. });
  1315. string[] names = name.Split("(");
  1316. if (names.Length > 1)
  1317. {
  1318. name = $"{names[0]}";
  1319. for (int index = 1; index < names.Length; index++)
  1320. {
  1321. name = $"{name}{names[index].Substring(0, 1)}";
  1322. var afnames = names[index].Split(")");
  1323. if (afnames.Length > 1)
  1324. {
  1325. for (int i = 1; i < afnames.Length; i++)
  1326. {
  1327. name = $"{name}{afnames[i]}";
  1328. }
  1329. }
  1330. }
  1331. }
  1332. names = name.Split("(");
  1333. if (names.Length > 1)
  1334. {
  1335. name = $"{names[0]}";
  1336. for (int index = 1; index < names.Length; index++)
  1337. {
  1338. name = $"{name}{names[index].Substring(0, 1)}";
  1339. var afnames = names[index].Split(")");
  1340. if (afnames.Length > 1)
  1341. {
  1342. for (int i = 1; i < afnames.Length; i++)
  1343. {
  1344. name = $"{name}{afnames[i]}";
  1345. }
  1346. }
  1347. }
  1348. }
  1349. name = Regex.Replace(name, "[ \\[ \\] \\^ \\-|()【】/' {}_*×――(^)$%~!@#$…&%¥—+=<>《》!!???::•`·、。,;,.;\"‘’“”-]", " ");
  1350. //检查是否有英文
  1351. if (Regex.Matches(name, "[a-zA-Z]").Count > 0)
  1352. {
  1353. var array = Regex.Split(name, "\\s+", RegexOptions.IgnoreCase);
  1354. StringBuilder tmpname = new StringBuilder();
  1355. if (array.Length > 1)
  1356. {
  1357. foreach (var item in array)
  1358. {
  1359. var arr = item.Select(x => $"{x}");
  1360. int index = 0;
  1361. foreach (var stra in arr)
  1362. {
  1363. if (Regex.Matches(stra, "[a-zA-Z]").Count > 0)
  1364. {
  1365. if (index == 1|| index == 0)
  1366. {
  1367. tmpname.Append(stra);
  1368. }
  1369. else
  1370. {
  1371. }
  1372. }
  1373. else
  1374. {
  1375. tmpname.Append(stra);
  1376. }
  1377. index++;
  1378. }
  1379. }
  1380. }
  1381. else
  1382. {
  1383. var arr = name.Select(x => $"{x}");
  1384. int index = 0;
  1385. foreach (var stra in arr)
  1386. {
  1387. if (Regex.Matches(stra, "[A-Z]").Count > 0)
  1388. {
  1389. tmpname.Append(stra);
  1390. }
  1391. else if (Regex.Matches(stra, "[a-z]").Count > 0)
  1392. {
  1393. }
  1394. else
  1395. {
  1396. tmpname.Append(stra);
  1397. }
  1398. index++;
  1399. }
  1400. }
  1401. name = tmpname.ToString();
  1402. }
  1403. name = Regex.Replace(name, @"\s", "");
  1404. name = name.Replace("\\", "");
  1405. if (name.Length > 6)
  1406. {
  1407. //新区,高新,产业等非新政单位
  1408. areaRemoveStr.ForEach(str =>
  1409. {
  1410. name = name.Replace(str.Key, str.Value);
  1411. });
  1412. //去除冗余的学校信息,只保留地名和校名关键信息
  1413. schooRemoveStr.ForEach(str =>
  1414. {
  1415. name = name.Replace(str, "");
  1416. });
  1417. //替换民族信息词
  1418. if (name.Length > 6)
  1419. {
  1420. nationRemoveStr.ForEach(str =>
  1421. {
  1422. name = name.Replace(str, "");
  1423. });
  1424. }
  1425. //替换学校信息的副词
  1426. if (name.Length > 6)
  1427. {
  1428. foreach (var str in schooReplaceStr)
  1429. {
  1430. if (name.Length <= 6)
  1431. {
  1432. break;
  1433. }
  1434. name = name.Replace(str.Key, str.Value);
  1435. }
  1436. }
  1437. //替换省简称信息词
  1438. string tmpname = name;
  1439. if (name.Length > 6)
  1440. {
  1441. proReplaceStr.ForEach(str =>
  1442. {
  1443. name = name.Replace(str.Key, str.Value);
  1444. });
  1445. //如果替换后仍然大于6位,则取消,直接替换省份完整的词
  1446. if (name.Length > 6)
  1447. {
  1448. name = tmpname.Replace(tmpprovince, "");
  1449. }
  1450. }
  1451. //替换城市信息词
  1452. if (name.Length > 6)
  1453. {
  1454. name = name.Replace(tmpcity, "");
  1455. }
  1456. }
  1457. int len = name.Length;
  1458. if (len > 6)
  1459. {
  1460. foreach (var str in afterSchoolRm)
  1461. {
  1462. if (name.Length <= 6)
  1463. {
  1464. break;
  1465. }
  1466. name = name.Replace(str, "");
  1467. }
  1468. }
  1469. len = name.Length;
  1470. if (len >= 6)
  1471. {
  1472. name = $"{name.Substring(0, 1)}{name.Substring(len - 5, 3)}{name.Substring(len - 2, 2)}";
  1473. }
  1474. //if (len <= 7 && len > 6)
  1475. //{
  1476. // name = name.Substring(len - 6);
  1477. //}
  1478. if (len <= 4)
  1479. {
  1480. name = $"{x.city.Substring(0, 2)}{name}";
  1481. }
  1482. if (len == 5)
  1483. {
  1484. name = $"{x.city.Substring(0, 1)}{name}";
  1485. }
  1486. string code = ToFirstPinYin(name);
  1487. x.aname = name;
  1488. x.id = code;
  1489. });
  1490. }
  1491. catch (Exception ex)
  1492. {
  1493. await _dingDing.SendBotMsg($"{ex.Message}\n{ex.StackTrace}\n", GroupNames.醍摩豆服務運維群組);
  1494. }
  1495. string a_z = "abcdefghijklmnopqrstuvwxyz";
  1496. 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();
  1497. data.ForEach(x =>
  1498. {
  1499. if (x.count > 1)
  1500. {
  1501. var first = x.list.First();
  1502. var same = x.list.Skip(1).Take(x.count - 1).ToList();
  1503. var fs = first.aname.Select(x => x).ToArray();
  1504. same.ForEach(z =>
  1505. {
  1506. var sp = z.aname.Select(x => x).ToArray();
  1507. int len = sp.Length;
  1508. if (sp.Length >= fs.Length)
  1509. {
  1510. len = fs.Count();
  1511. }
  1512. for (int index = 0; index < len; index++)
  1513. {
  1514. if (!$"{sp[index]}".Equals($"{fs[index]}"))
  1515. {
  1516. short st = ChineseChar.GetStrokeNumber(sp[index]);
  1517. if (st > 0)
  1518. {
  1519. var ins = st % 27;
  1520. var insch = a_z[ins];
  1521. if (z.aname.EndsWith($"{insch}"))
  1522. {
  1523. ins = (st + 1) % 27;
  1524. insch = a_z[ins];
  1525. }
  1526. z.id = z.id.Insert(index, $"{insch}").Remove(index + 1, 1);
  1527. break;
  1528. }
  1529. }
  1530. }
  1531. });
  1532. }
  1533. });
  1534. 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();
  1535. dataz.ForEach(x =>
  1536. {
  1537. if (x.count > 1)
  1538. {
  1539. var first = x.list.First();
  1540. var same = x.list.Skip(1).Take(x.count - 1).ToList();
  1541. var fs = first.aname.Select(x => x).ToArray();
  1542. same.ForEach(z =>
  1543. {
  1544. var sp = z.aname.Select(x => x).ToArray();
  1545. int len = sp.Length;
  1546. if (sp.Length >= fs.Length)
  1547. {
  1548. len = fs.Count();
  1549. }
  1550. for (int index = 0; index < len; index++)
  1551. {
  1552. if (!$"{sp[index]}".Equals($"{fs[index]}"))
  1553. {
  1554. var spstr = ToPinYin($"{sp[index]}" );
  1555. var fsstr = ToPinYin($"{fs[index]}");
  1556. var insch= spstr.Select(x => $"{x}").Except( fsstr.Select(z => $"{z}"));
  1557. if (insch != null && insch.Count() > 0) {
  1558. if (index > 1)
  1559. {
  1560. z.id = z.id.Insert(index - 1, insch.First()).Remove(index, 1);
  1561. }
  1562. else if (index == 1)
  1563. {
  1564. z.id = z.id.Insert(index, insch.First()).Remove(index - 1, 1);
  1565. }
  1566. }
  1567. }
  1568. }
  1569. });
  1570. }
  1571. });
  1572. 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();
  1573. Random random = new Random();
  1574. List<SchoolData> list = new List<SchoolData>();
  1575. dataa.ForEach(x =>
  1576. {
  1577. if (x.count > 1)
  1578. {
  1579. x.list.ForEach(y => {
  1580. int r= random.Next(25);
  1581. int index= random.Next(5);
  1582. y.id = y.id.Insert(index - 1, $"{a_z[r]}").Remove(index, 1);
  1583. });
  1584. }
  1585. list.AddRange(x.list);
  1586. });
  1587. return list;
  1588. }
  1589. public static string ToPinYin(string val)
  1590. {
  1591. StringBuilder sb = new StringBuilder();
  1592. val.ToCharArray().ToList().ForEach(x =>
  1593. {
  1594. if (('a' <= x && x <= 'z') || ('A' <= x && x <= 'Z') || ('0' <= x && x <= '9'))
  1595. {
  1596. sb.Append(x);
  1597. }
  1598. else
  1599. {
  1600. try
  1601. {
  1602. ChineseChar cc = new ChineseChar(x);
  1603. if (cc.Pinyins.Count > 0 && cc.Pinyins[0].Length > 0)
  1604. {
  1605. sb.Append(cc.Pinyins[0]);
  1606. }
  1607. }
  1608. catch (Exception ex)
  1609. {
  1610. }
  1611. }
  1612. });
  1613. return sb.ToString().ToLower();
  1614. }
  1615. public static string ToFirstPinYin(string val)
  1616. {
  1617. StringBuilder sb = new StringBuilder();
  1618. val.ToCharArray().ToList().ForEach(x =>
  1619. {
  1620. if (('a' <= x && x <= 'z') || ('A' <= x && x <= 'Z') || ('0' <= x && x <= '9'))
  1621. {
  1622. sb.Append(x);
  1623. }
  1624. else
  1625. {
  1626. try
  1627. {
  1628. ChineseChar cc = new ChineseChar(x);
  1629. if (cc.Pinyins.Count > 0 && cc.Pinyins[0].Length > 0)
  1630. {
  1631. sb.Append(cc.Pinyins[0][0]);
  1632. }
  1633. }
  1634. catch (Exception ex)
  1635. {
  1636. }
  1637. }
  1638. });
  1639. return sb.ToString().ToLower();
  1640. }
  1641. public class SchoolData
  1642. {
  1643. public string province { get; set; }
  1644. public string id { get; set; }
  1645. public string name { get; set; }
  1646. public string city { get; set; }
  1647. public string aname { get; set; }
  1648. public string uid { get; set; }
  1649. }
  1650. public static List<string> comeRemoveStr = new List<string>() { "省", "市", "区", "州", "县", "旗", "盟", "自治" };
  1651. public static List<string> prvcRemoveStr = new List<string>() { "回族", "维吾尔", "壮族", };
  1652. public static List<string> cityRemoveStr = new List<string>() { "蒙古族", "地区", "壮族", "朝鲜族", "直辖" };
  1653. public static List<string> nationRemoveStr = new List<string> {
  1654. "维吾尔","回族", "自治", "满族", "蒙古", "壮族", "苗族" , "侗族", "瑶族",
  1655. "达斡尔","鄂温克","朝鲜","畲族","土家","各族","仫佬","毛南","羌族","彝族","仡佬","布依","水族",
  1656. "傣族","纳西","哈尼","拉祜","佤族","傈僳","独龙","怒族","白族","普米","固族","哈萨克","土族","撒拉","景颇","族"
  1657. };
  1658. public static List<string> schemoveStr = new List<string> { "第", "校区", "部", "楼", "与", "学校", "校园", };
  1659. public static List<string> schooRemoveStr = new List<string>() {"省", "市", "区", "州", "县", "旗", "盟","办事处","街道", "自治",
  1660. "镇","村","乡","街","路","站","馆"
  1661. };
  1662. public static List<string> afterSchoolRm = new List<string>() {"校","园","院", "湾","峡","沟","山","庄",
  1663. "堡","江","屯","岭","溪","河","桥","营","铺","坡","寨","场","湖","巷","集","关","庙","寺","矿","塘"};
  1664. public static List<KeyValuePair<string, string>> areaRemoveStr = new List<KeyValuePair<string, string>>{
  1665. new KeyValuePair<string, string>("高新技术产业开发区", "高") ,
  1666. new KeyValuePair<string, string>("经济技术开发区", "经") ,
  1667. new KeyValuePair<string, string>("国际", "际") ,
  1668. new KeyValuePair<string, string>("中国", ""),
  1669. new KeyValuePair<string, string>("国家", ""),
  1670. new KeyValuePair<string, string>("新区", ""),
  1671. new KeyValuePair<string, string>("高新", "高"),
  1672. new KeyValuePair<string, string>("园区", ""),
  1673. new KeyValuePair<string, string>("产业", "产"),
  1674. new KeyValuePair<string, string>("经济", "经"),
  1675. new KeyValuePair<string, string>("开发", "开"),
  1676. new KeyValuePair<string, string>("中心", "") ,
  1677. new KeyValuePair<string, string>("集团", "") ,
  1678. new KeyValuePair<string, string>("公司", "") ,
  1679. new KeyValuePair<string, string>("有限", "") ,
  1680. new KeyValuePair<string, string>("股份", "") ,
  1681. new KeyValuePair<string, string>("服务", "") ,
  1682. new KeyValuePair<string, string>("基地", "") ,
  1683. new KeyValuePair<string, string>("社区", "社") ,
  1684. new KeyValuePair<string, string>("职工", "") ,
  1685. new KeyValuePair<string, string>("专修", ""),
  1686. new KeyValuePair<string, string>("实验", "验"),
  1687. new KeyValuePair<string, string>("完全", ""),
  1688. new KeyValuePair<string, string>("一贯制", "") ,
  1689. new KeyValuePair<string, string>("联办", ""),
  1690. new KeyValuePair<string, string>("联合", ""),
  1691. new KeyValuePair<string, string>("完小", "小"),
  1692. new KeyValuePair<string, string>("义务", ""),
  1693. new KeyValuePair<string, string>("基础", ""),
  1694. new KeyValuePair<string, string>("制造", "")
  1695. };
  1696. public static List<KeyValuePair<string, string>> schooReplaceStr = new List<KeyValuePair<string, string>> {
  1697. new KeyValuePair<string, string>("分校", "分") ,
  1698. new KeyValuePair<string, string>("分园", "分") ,
  1699. new KeyValuePair<string, string>("分院", "分") ,
  1700. new KeyValuePair<string, string>("藏文", "藏"),
  1701. new KeyValuePair<string, string>("职业技术学院", "职") ,
  1702. new KeyValuePair<string, string>("创新创业", "创") ,
  1703. new KeyValuePair<string, string>("就业创业", "就") ,
  1704. new KeyValuePair<string, string>("应用核技术", "核") ,
  1705. new KeyValuePair<string, string>("职业学院", "职") ,
  1706. new KeyValuePair<string, string>("幼儿园", "幼") ,
  1707. new KeyValuePair<string, string>("幼儿", "幼") ,
  1708. new KeyValuePair<string, string>("小学", "小") ,
  1709. new KeyValuePair<string, string>("中学", "中") ,
  1710. new KeyValuePair<string, string>("中等", "中") ,
  1711. new KeyValuePair<string, string>("双语", "双") ,
  1712. new KeyValuePair<string, string>("初中", "初") ,
  1713. new KeyValuePair<string, string>("初级", "初") ,
  1714. new KeyValuePair<string, string>("广播", "广"),
  1715. new KeyValuePair<string, string>("高中", "高") ,
  1716. new KeyValuePair<string, string>("高级", "高") ,
  1717. new KeyValuePair<string, string>("大学", "大") ,
  1718. new KeyValuePair<string, string>("学院", "院") ,
  1719. new KeyValuePair<string, string>("综合", "综") ,
  1720. new KeyValuePair<string, string>("职业", "职") ,
  1721. new KeyValuePair<string, string>("技术", "技") ,
  1722. new KeyValuePair<string, string>("专科", "专") ,
  1723. new KeyValuePair<string, string>("外国语", "外") ,
  1724. new KeyValuePair<string, string>("九年", "九") ,
  1725. new KeyValuePair<string, string>("五年", "五") ,
  1726. new KeyValuePair<string, string>("年制", "制") ,
  1727. new KeyValuePair<string, string>("高等", "高") ,
  1728. new KeyValuePair<string, string>("院校", "院") ,
  1729. new KeyValuePair<string, string>("初等", "小") ,
  1730. new KeyValuePair<string, string>("附属", "附"),
  1731. new KeyValuePair<string, string>("寄宿制", "寄") ,
  1732. new KeyValuePair<string, string>("寄宿", "寄") ,
  1733. new KeyValuePair<string, string>("卫生", "卫") ,
  1734. new KeyValuePair<string, string>("创业", "创") ,
  1735. new KeyValuePair<string, string>("继续", "继") ,
  1736. new KeyValuePair<string, string>("开放", "开") ,
  1737. new KeyValuePair<string, string>("技师", "技") ,
  1738. new KeyValuePair<string, string>("成人", "成") ,
  1739. new KeyValuePair<string, string>("教育", "教") ,
  1740. new KeyValuePair<string, string>("科技", "科") ,
  1741. new KeyValuePair<string, string>("行政", "政") ,
  1742. new KeyValuePair<string, string>("生物", "生") ,
  1743. new KeyValuePair<string, string>("美术", "美") ,
  1744. new KeyValuePair<string, string>("设计", "设") ,
  1745. new KeyValuePair<string, string>("传播", "传") ,
  1746. new KeyValuePair<string, string>("计算机", "算") ,
  1747. new KeyValuePair<string, string>("土木", "土") ,
  1748. new KeyValuePair<string, string>("石油", "油") ,
  1749. new KeyValuePair<string, string>("科普", "普") ,
  1750. new KeyValuePair<string, string>("电信", "信") ,
  1751. new KeyValuePair<string, string>("联合", "联") ,
  1752. new KeyValuePair<string, string>("电商", "商") ,
  1753. new KeyValuePair<string, string>("纺织", "织") ,
  1754. new KeyValuePair<string, string>("服装", "服") ,
  1755. new KeyValuePair<string, string>("新闻", "新") ,
  1756. new KeyValuePair<string, string>("网络", "网") ,
  1757. new KeyValuePair<string, string>("应用", "应") ,
  1758. new KeyValuePair<string, string>("畜牧", "畜") ,
  1759. new KeyValuePair<string, string>("兽医", "兽") ,
  1760. new KeyValuePair<string, string>("师范", "师") ,
  1761. new KeyValuePair<string, string>("人文", "文") ,
  1762. new KeyValuePair<string, string>("创新", "创") ,
  1763. new KeyValuePair<string, string>("法官", "法") ,
  1764. new KeyValuePair<string, string>("邮电", "邮") ,
  1765. new KeyValuePair<string, string>("文化", "文") ,
  1766. new KeyValuePair<string, string>("函授", "函") ,
  1767. new KeyValuePair<string, string>("科学", "科") ,
  1768. new KeyValuePair<string, string>("信息", "息") ,
  1769. new KeyValuePair<string, string>("水利", "利") ,
  1770. new KeyValuePair<string, string>("水电", "电") ,
  1771. new KeyValuePair<string, string>("电力", "力") ,
  1772. new KeyValuePair<string, string>("环境", "环") ,
  1773. new KeyValuePair<string, string>("建材", "材") ,
  1774. new KeyValuePair<string, string>("机电", "机") ,
  1775. new KeyValuePair<string, string>("航空", "空") ,
  1776. new KeyValuePair<string, string>("航天", "天") ,
  1777. new KeyValuePair<string, string>("警察", "警") ,
  1778. new KeyValuePair<string, string>("警官", "警") ,
  1779. new KeyValuePair<string, string>("财贸", "财") ,
  1780. new KeyValuePair<string, string>("电子", "电") ,
  1781. new KeyValuePair<string, string>("建筑", "筑") ,
  1782. new KeyValuePair<string, string>("艺术", "艺") ,
  1783. new KeyValuePair<string, string>("体育", "体") ,
  1784. new KeyValuePair<string, string>("城市", "城") ,
  1785. new KeyValuePair<string, string>("地质", "地") ,
  1786. new KeyValuePair<string, string>("医药", "药") ,
  1787. new KeyValuePair<string, string>("政法", "法") ,
  1788. new KeyValuePair<string, string>("铁路", "铁") ,
  1789. new KeyValuePair<string, string>("铁道", "道") ,
  1790. new KeyValuePair<string, string>("轨道", "轨") ,
  1791. new KeyValuePair<string, string>("交通", "通") ,
  1792. new KeyValuePair<string, string>("医学院", "医") ,
  1793. new KeyValuePair<string, string>("医学", "医") ,
  1794. new KeyValuePair<string, string>("传媒", "媒") ,
  1795. new KeyValuePair<string, string>("工程", "程") ,
  1796. new KeyValuePair<string, string>("临床", "临") ,
  1797. new KeyValuePair<string, string>("化工", "化") ,
  1798. new KeyValuePair<string, string>("林业", "林") ,
  1799. new KeyValuePair<string, string>("老年", "老") ,
  1800. new KeyValuePair<string, string>("旅游", "游") ,
  1801. new KeyValuePair<string, string>("护理", "护") ,
  1802. new KeyValuePair<string, string>("贸易", "贸") ,
  1803. new KeyValuePair<string, string>("中医药", "中医") ,
  1804. new KeyValuePair<string, string>("资源", "源") ,
  1805. new KeyValuePair<string, string>("冶金", "冶") ,
  1806. new KeyValuePair<string, string>("能源", "能") ,
  1807. new KeyValuePair<string, string>("汽车", "车") ,
  1808. new KeyValuePair<string, string>("医科", "医") ,
  1809. new KeyValuePair<string, string>("机械", "械") ,
  1810. new KeyValuePair<string, string>("应用", "应") ,
  1811. new KeyValuePair<string, string>("电气", "电") ,
  1812. new KeyValuePair<string, string>("材料", "材") ,
  1813. new KeyValuePair<string, string>("劳动", "劳") ,
  1814. new KeyValuePair<string, string>("轻工", "轻") ,
  1815. new KeyValuePair<string, string>("农业", "农") ,
  1816. new KeyValuePair<string, string>("委员会", "委") ,
  1817. new KeyValuePair<string, string>("专业", "专") ,
  1818. new KeyValuePair<string, string>("广播", "广"),
  1819. new KeyValuePair<string, string>("电视", "电") ,
  1820. new KeyValuePair<string, string>("工商", "商") ,
  1821. new KeyValuePair<string, string>("工业", "工") ,
  1822. new KeyValuePair<string, string>("管理", "管") ,
  1823. new KeyValuePair<string, string>("社会主义", "社") ,
  1824. new KeyValuePair<string, string>("社会", "社") ,
  1825. new KeyValuePair<string, string>("自动化", "自") ,
  1826. };
  1827. public static List<KeyValuePair<string, string>> proReplaceStr = new List<KeyValuePair<string, string>> {
  1828. new KeyValuePair<string, string>("北京","京") ,
  1829. new KeyValuePair<string, string>("天津","津") ,
  1830. new KeyValuePair<string, string>("河北","冀") ,
  1831. new KeyValuePair<string, string>("山西","晋") ,
  1832. new KeyValuePair<string, string>("内蒙古","蒙") ,
  1833. new KeyValuePair<string, string>("辽宁","辽") ,
  1834. new KeyValuePair<string, string>("吉林","吉") ,
  1835. new KeyValuePair<string, string>("黑龙江","黑") ,
  1836. new KeyValuePair<string, string>("上海","沪") ,
  1837. new KeyValuePair<string, string>("江苏","苏") ,
  1838. new KeyValuePair<string, string>("浙江","浙") ,
  1839. new KeyValuePair<string, string>("安徽","皖") ,
  1840. new KeyValuePair<string, string>("福建","闽") ,
  1841. new KeyValuePair<string, string>("江西","赣") ,
  1842. new KeyValuePair<string, string>("山东","鲁") ,
  1843. new KeyValuePair<string, string>("河南","豫") ,
  1844. new KeyValuePair<string, string>("湖北","鄂") ,
  1845. new KeyValuePair<string, string>("湖南","湘") ,
  1846. new KeyValuePair<string, string>("广东","粤") ,
  1847. new KeyValuePair<string, string>("广西","桂") ,
  1848. new KeyValuePair<string, string>("海南","琼") ,
  1849. new KeyValuePair<string, string>("四川","川") ,
  1850. new KeyValuePair<string, string>("贵州","贵") ,
  1851. new KeyValuePair<string, string>("云南","云") ,
  1852. new KeyValuePair<string, string>("重庆","渝") ,
  1853. new KeyValuePair<string, string>("西藏","藏") ,
  1854. new KeyValuePair<string, string>("陕西","陕") ,
  1855. new KeyValuePair<string, string>("甘肃","甘") ,
  1856. new KeyValuePair<string, string>("青海","青") ,
  1857. new KeyValuePair<string, string>("宁夏","宁") ,
  1858. new KeyValuePair<string, string>("新疆","新") ,
  1859. new KeyValuePair<string, string>("香港","港") ,
  1860. new KeyValuePair<string, string>("澳门","澳") ,
  1861. new KeyValuePair<string, string>("台湾","台")
  1862. };
  1863. /*
  1864. [
  1865. {"method":"台北市","params":{"CountryId":"TW","CityId":"30"}},1
  1866. {"method":"新北市","params":{"CountryId":"TW","CityId":"01"}},1
  1867. {"method":"桃园市","params":{"CountryId":"TW","CityId":"03"}},1
  1868. {"method":"台中市","params":{"CountryId":"TW","CityId":"66"}},1
  1869. {"method":"台南市","params":{"CountryId":"TW","CityId":"67"}},1
  1870. {"method":"高雄市","params":{"CountryId":"TW","CityId":"64"}},1
  1871. {"method":"基隆市","params":{"CountryId":"TW","CityId":"17"}},1
  1872. {"method":"新竹市","params":{"CountryId":"TW","CityId":"18"}},1
  1873. {"method":"嘉义市","params":{"CountryId":"TW","CityId":"20"}},1
  1874. {"method":"新竹县","params":{"CountryId":"TW","CityId":"04"}},1
  1875. {"method":"苗栗县","params":{"CountryId":"TW","CityId":"05"}},1
  1876. {"method":"彰化县","params":{"CountryId":"TW","CityId":"07"}},1
  1877. {"method":"南投县","params":{"CountryId":"TW","CityId":"08"}},1
  1878. {"method":"云林县","params":{"CountryId":"TW","CityId":"09"}},1
  1879. {"method":"嘉义县","params":{"CountryId":"TW","CityId":"10"}},1
  1880. {"method":"屏东县","params":{"CountryId":"TW","CityId":"13"}},1
  1881. {"method":"宜兰县","params":{"CountryId":"TW","CityId":"02"}},1
  1882. {"method":"花莲县","params":{"CountryId":"TW","CityId":"15"}},1
  1883. {"method":"台东县","params":{"CountryId":"TW","CityId":"14"}},1
  1884. {"method":"澎湖县","params":{"CountryId":"TW","CityId":"16"}},1
  1885. {"method":"金门县","params":{"CountryId":"TW","CityId":"71"}},
  1886. {"method":"连江县","params":{"CountryId":"TW","CityId":"72"}}
  1887. ]
  1888. */
  1889. }
  1890. }