TriggerStuActivity.cs 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  1. using Azure;
  2. using Azure.Cosmos;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Text.Json;
  9. using System.Threading.Tasks;
  10. using TEAMModelOS.SDK.DI;
  11. using TEAMModelOS.SDK.Extension;
  12. using TEAMModelOS.SDK;
  13. using TEAMModelOS.SDK.Models;
  14. using TEAMModelOS.SDK.Models.Cosmos.Common;
  15. using TEAMModelOS.SDK.Models.Service;
  16. using HTEXLib.COMM.Helpers;
  17. namespace TEAMModelOS.SDK
  18. {
  19. public class TriggerStuActivity
  20. {
  21. public static async Task RefreshStuActivity(CosmosClient client, DingDing _dingDing, string id, string code)
  22. {
  23. MQActivity activity = null;
  24. try
  25. {
  26. var aactivity = await client.GetContainer(Constant.TEAMModelOS, "Common").ReadItemStreamAsync(id, new Azure.Cosmos.PartitionKey(code));
  27. using var da = await JsonDocument.ParseAsync(aactivity.ContentStream);
  28. activity = da.ToObject<MQActivity>();
  29. }
  30. catch (CosmosException )
  31. {
  32. activity=null;
  33. }
  34. if (activity != null)
  35. {
  36. List<string> classes = ExamService.getClasses(activity.classes, activity.stuLists);
  37. (List<TmdInfo> tmdids, List<StuInfo> students,List<ClassListInfo> classLists) = await GetStuList(client, _dingDing, classes, activity.school);
  38. if (tmdids.IsNotEmpty())
  39. {
  40. foreach (TmdInfo tmdid in tmdids)
  41. {
  42. var stucourse = new StuActivity
  43. {
  44. id = activity.id,
  45. scode = activity.code,
  46. name = activity.name,
  47. code = $"Activity-{tmdid.id}",
  48. source = activity.source,
  49. scope = activity.scope,
  50. school = activity.school,
  51. creatorId = activity.creatorId,
  52. pk = "Activity",
  53. type = activity.pk,
  54. subjects = activity.pk.ToLower().Equals("exam") && activity.subjects.IsNotEmpty() ? new List<string>() { activity.subjects[0].id } : new List<string>() { "" },
  55. startTime = activity.startTime,
  56. endTime = activity.endTime,
  57. blob = activity.blob,
  58. owner = activity.owner,
  59. createTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
  60. taskStatus = -1,
  61. classIds = classes
  62. };
  63. await client.GetContainer(Constant.TEAMModelOS, "Student").UpsertItemAsync(stucourse, new PartitionKey(stucourse.code));
  64. }
  65. }
  66. if (students.IsNotEmpty())
  67. {
  68. foreach (StuInfo student in students)
  69. {
  70. var stucourse = new StuActivity
  71. {
  72. id = activity.id,
  73. scode = activity.code,
  74. name = activity.name,
  75. code = $"Activity-{activity.school}-{student.id}",
  76. scope = activity.scope,
  77. source = activity.source,
  78. school = activity.school,
  79. creatorId = activity.creatorId,
  80. pk = "Activity",
  81. type = activity.pk,
  82. subjects = activity.pk.ToLower().Equals("exam") && activity.subjects.IsNotEmpty() ? new List<string>() { activity.subjects[0].id } : new List<string>() { "" },
  83. startTime = activity.startTime,
  84. endTime = activity.endTime,
  85. blob = activity.blob,
  86. owner = activity.owner,
  87. createTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
  88. taskStatus = -1,
  89. classIds = classes
  90. };
  91. await client.GetContainer(Constant.TEAMModelOS, "Student").UpsertItemAsync(stucourse, new PartitionKey(stucourse.code));
  92. }
  93. }
  94. }
  95. }
  96. public static async Task<string> SaveStuActivity(CosmosClient client, DingDing _dingDing, List<StuActivity> stuActivities,List<StuActivity> tmdActivities, List<StuActivity> tchActivities) {
  97. try {
  98. if (stuActivities.IsNotEmpty())
  99. {
  100. foreach (var x in stuActivities)
  101. {
  102. await client.GetContainer(Constant.TEAMModelOS, "Student").UpsertItemAsync(x, new PartitionKey(x.code));
  103. }
  104. }
  105. if (tmdActivities.IsNotEmpty())
  106. {
  107. foreach (var x in tmdActivities)
  108. {
  109. await client.GetContainer(Constant.TEAMModelOS, "Student").UpsertItemAsync(x, new PartitionKey(x.code));
  110. }
  111. }
  112. if (tchActivities.IsNotEmpty())
  113. {
  114. foreach (var x in tchActivities)
  115. {
  116. await client.GetContainer(Constant.TEAMModelOS, "Teacher").UpsertItemAsync(x, new PartitionKey(x.code));
  117. }
  118. }
  119. } catch (Exception ex) {
  120. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-OS,TriggerStuActivity-SaveStuActivity\n{ex.Message}\n{ex.StackTrace}", GroupNames.醍摩豆服務運維群組);
  121. }
  122. return "";
  123. }
  124. /// <summary>
  125. /// 获取行政班接口
  126. /// </summary>
  127. /// <param name="client"></param>
  128. /// <param name="_dingDing"></param>
  129. /// <param name="classes"></param>
  130. /// <param name="school"></param>
  131. /// <returns></returns>
  132. public static async Task<List<ClassInfo>> GetClassInfo(CosmosClient client, DingDing _dingDing, List<string> classes, string school)
  133. {
  134. try {
  135. List<ClassInfo> classInfos = new List<ClassInfo>();
  136. if (classes.IsNotEmpty()) {
  137. List<string> sqlList = new List<string>();
  138. classes.ForEach(x => { sqlList.Add($" '{x}' "); });
  139. string sql = string.Join(" , ", sqlList);
  140. if (!string.IsNullOrEmpty(school))
  141. {
  142. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<ClassInfo>(queryText: $"select c.id,c.name ,c.periodId,c.year from c where c.id in ({sql})",
  143. requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"Class-{school}") }))
  144. {
  145. classInfos.Add(item);
  146. }
  147. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<ClassInfo>(queryText: $"select c.id,c.name from c where c.id in ({sql})",
  148. requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"StuList-{school}") }))
  149. {
  150. //item.from = "SchStuList";
  151. classInfos.Add(item);
  152. }
  153. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<ClassInfo>(queryText: $"select c.id,c.name from c where c.id in ({sql})",
  154. requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"TchList-{school}") }))
  155. {
  156. //item.from = "SchStuList";
  157. classInfos.Add(item);
  158. }
  159. }
  160. List<StuList> tchLists = new List<StuList>();
  161. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryIterator<ClassInfo>(queryText: $"select c.id,c.name from c where c.id in ({sql})",
  162. requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"StuList") }))
  163. {
  164. // item.from = "TchStuList";
  165. classInfos.Add(item);
  166. }
  167. classInfos = classInfos.GroupBy(c => c.id).Select(s => s.First()).ToList();
  168. }
  169. return classInfos;
  170. } catch (Exception ex) {
  171. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-OS,TriggerStuActivity-GetClassInfo\n{ex.Message}\n{ex.StackTrace}\n class{classes.ToJsonString()},{school}", GroupNames.醍摩豆服務運維群組);
  172. throw new Exception(ex.Message, ex);
  173. }
  174. }
  175. public static async Task<(List<TmdInfo> tmdinfos, List<StuInfo> students, List<ClassListInfo> classInfo)> GetStuListInStu(CosmosClient client, DingDing _dingDing, List<string> claes, string school)
  176. {
  177. try
  178. {
  179. List<string> classes = new List<string>();
  180. foreach (string ss in claes)
  181. {
  182. classes.Add(ss);
  183. }
  184. List<TmdInfo> tmdinfos = new List<TmdInfo>();
  185. List<Students> studentss = new List<Students>();
  186. List<string> tmdids = new List<string>();
  187. List<StuInfo> stuInfos = new List<StuInfo>();
  188. if (!classes.IsNotEmpty()) { return (tmdinfos, new List<StuInfo>(), null); }
  189. List<string> sqlList = new List<string>();
  190. classes.ForEach(x => { sqlList.Add($" '{x}' "); });
  191. string sql = string.Join(" , ", sqlList);
  192. List<StuList> schList = new List<StuList>();
  193. List<Student> students = new List<Student>();
  194. if (!string.IsNullOrEmpty(school))
  195. {
  196. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<StuList>(queryText: $"select value(c) from c where c.id in ({sql})",
  197. requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"StuList-{school}") }))
  198. {
  199. schList.Add(item);
  200. }
  201. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Student").GetItemQueryIterator<Student>(queryText: $"select value(c) from c where c.classId in ({sql})",
  202. requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"Base-{school}") }))
  203. {
  204. students.Add(item);
  205. }
  206. }
  207. List<StuList> tchLists = new List<StuList>();
  208. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryIterator<StuList>(queryText: $"select value(c) from c where c.id in ({sql})",
  209. requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"StuList") }))
  210. {
  211. tchLists.Add(item);
  212. }
  213. foreach (var x in schList)
  214. {
  215. if (x.students.IsNotEmpty())
  216. {
  217. studentss.AddRange(x.students);
  218. }
  219. if (x.tmids.IsNotEmpty())
  220. {
  221. tmdids.AddRange(x.tmids);
  222. }
  223. classes.Remove(x.id);
  224. }
  225. foreach (var x in tchLists)
  226. {
  227. if (x.students.IsNotEmpty())
  228. {
  229. studentss.AddRange(x.students);
  230. }
  231. if (x.tmids.IsNotEmpty())
  232. {
  233. tmdids.AddRange(x.tmids);
  234. }
  235. classes.Remove(x.id);
  236. }
  237. if (tmdids.IsNotEmpty())
  238. {
  239. List<TmdInfo> infos = new List<TmdInfo>();
  240. List<string> inids = new List<string>();
  241. tmdids.ForEach(x => { inids.Add($"'{x}'"); });
  242. var insql = string.Join(",", inids);
  243. var queryslt = $"SELECT value(c) FROM c where c.id in ({insql})";
  244. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Student").GetItemQueryIterator<TmdInfo>(queryText: queryslt, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base") }))
  245. {
  246. infos.Add(item);
  247. tmdids.Remove(item.id);
  248. }
  249. if (tmdids.IsNotEmpty()) {
  250. inids = new List<string>();
  251. tmdids.ForEach(x => { inids.Add($"'{x}'"); });
  252. insql = string.Join(",", inids);
  253. queryslt = $"SELECT value(c) FROM c where c.id in ({insql})";
  254. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryIterator<TmdInfo>(queryText: queryslt, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base") }))
  255. {
  256. infos.Add(item);
  257. }
  258. }
  259. tmdinfos.AddRange(infos);
  260. }
  261. if (studentss.IsNotEmpty())
  262. {
  263. var stuGroups = studentss.GroupBy(x => x.code).ToList().Select(x => new { key = x.Key, list = x.ToList() }) ;
  264. foreach (var gp in stuGroups) {
  265. List<string> inidstus = new List<string>();
  266. gp.list.Select(x => x.id).ToList().ForEach(x => { inidstus.Add($"'{x}'"); });
  267. PartitionKey partitionKey = new PartitionKey($"Base-{gp.key.Replace("Base-", "")}");
  268. var insqlstu = string.Join(",", inidstus);
  269. var querystu = $"SELECT c.id,c.code,c.name,c.picture,c.classId,c.groupId,c.groupName,c.year,c.schoolId FROM c where c.id in ({insqlstu})";
  270. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Student").GetItemQueryIterator<StuInfo>(queryText: querystu, requestOptions: new QueryRequestOptions() { PartitionKey= partitionKey }))
  271. {
  272. stuInfos.Add(item);
  273. }
  274. }
  275. }
  276. students.ForEach(x =>
  277. {
  278. if (!stuInfos.Select(y => y.classId).ToList().Contains(x.id))
  279. {
  280. stuInfos.Add(new StuInfo { id = x.id, code = x.code, schoolId = x.schoolId, classId = x.classId, name = x.name, picture = x.picture, year = x.year,groupId = x.groupId,groupName=x.groupName,no=x.no });
  281. }
  282. });
  283. List<ClassListInfo> classInfo = new List<ClassListInfo>();
  284. schList.ForEach(x => {
  285. ClassListInfo classListInfo = new ClassListInfo { id = x.id, name = x.name };
  286. if (x.students.IsNotEmpty())
  287. {
  288. x.students.ForEach(y => {
  289. var stuinfo = stuInfos.Where(z => z.id.Equals(y.id)).FirstOrDefault();
  290. if (stuinfo != null)
  291. {
  292. classListInfo.stuInfos.Add(stuinfo);
  293. }
  294. });
  295. }
  296. if (x.tmids.IsNotEmpty())
  297. {
  298. x.tmids.ForEach(y => {
  299. var tmdinfo = tmdinfos.Where(z => z.id.Equals(y)).FirstOrDefault();
  300. if (tmdinfo != null)
  301. {
  302. classListInfo.tmdInfos.Add(tmdinfo);
  303. }
  304. });
  305. }
  306. classInfo.Add(classListInfo);
  307. });
  308. tchLists.ForEach(x => {
  309. ClassListInfo classListInfo = new ClassListInfo { id = x.id, name = x.name };
  310. if (x.students.IsNotEmpty())
  311. {
  312. x.students.ForEach(y => {
  313. var stuinfo = stuInfos.Where(z => z.id.Equals(y.id)).FirstOrDefault();
  314. if (stuinfo != null)
  315. {
  316. classListInfo.stuInfos.Add(stuinfo);
  317. }
  318. });
  319. }
  320. if (x.tmids.IsNotEmpty())
  321. {
  322. x.tmids.ForEach(y => {
  323. var tmdinfo = tmdinfos.Where(z => z.id.Equals(y)).FirstOrDefault();
  324. if (tmdinfo != null)
  325. {
  326. classListInfo.tmdInfos.Add(tmdinfo);
  327. }
  328. });
  329. }
  330. classInfo.Add(classListInfo);
  331. });
  332. //var classeids= students.GroupBy(x => x.classId).Select(x => x.Key).ToList();
  333. List<ClassInfo> classInfos = await GetClassInfo(client, _dingDing, classes, school);
  334. classInfos.ForEach(x =>
  335. {
  336. ClassListInfo classListInfo = new ClassListInfo { id = x.id, name = x.name };
  337. var list = students.Where(y => y.classId .Equals(x.id)).ToList();
  338. list.ForEach(z => { classListInfo.stuInfos.Add(new StuInfo { id = z.id, code = z.code, schoolId = z.schoolId, classId = z.classId, name = z.name, picture = z.picture, year = z.year,groupId=z.groupId,groupName=z.groupName,no=z.no }); });
  339. classInfo.Add(classListInfo);
  340. });
  341. return (tmdinfos, stuInfos, classInfo);
  342. }
  343. catch (Exception ex)
  344. {
  345. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-OS,TriggerStuActivity-GetStuList\n{ex.Message}\n{ex.StackTrace}", GroupNames.醍摩豆服務運維群組);
  346. }
  347. return (null, null, null);
  348. }
  349. public static async Task<(List<TmdInfo> tmdinfos, List<ClassListInfo> classInfo)> GetTchList(CosmosClient client, DingDing _dingDing, List<string> claes, string school)
  350. {
  351. try
  352. {
  353. List<TmdInfo> tmdinfos = new List<TmdInfo>();
  354. List<ClassListInfo> classInfo = new List<ClassListInfo>();
  355. if (claes.Count == 1 && claes.First().Equals("default"))
  356. {
  357. List<TmdInfo> infos = new List<TmdInfo>();
  358. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<TmdInfo>(queryText: $"SELECT value(c) FROM c ", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Teacher-{school}") }))
  359. {
  360. infos.Add(item);
  361. }
  362. tmdinfos.AddRange(infos);
  363. classInfo.Add(new ClassListInfo { id = "default",name= "default", tmdInfos = infos });
  364. }
  365. else {
  366. List<string> classes = new List<string>();
  367. foreach (string ss in claes)
  368. {
  369. classes.Add(ss);
  370. }
  371. List<string> tmdids = new List<string>();
  372. List<StuInfo> stuInfos = new List<StuInfo>();
  373. if (!classes.IsNotEmpty()) { return (tmdinfos, null); }
  374. List<string> sqlList = new List<string>();
  375. classes.ForEach(x => { sqlList.Add($" '{x}' "); });
  376. string sql = string.Join(" , ", sqlList);
  377. List<TchList> tchLists = new List<TchList>();
  378. if (!string.IsNullOrEmpty(school))
  379. {
  380. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<TchList>(queryText: $"select value(c) from c where c.id in ({sql})",
  381. requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"TchList-{school}") }))
  382. {
  383. tchLists.Add(item);
  384. }
  385. }
  386. foreach (var x in tchLists)
  387. {
  388. if (x.teachers.IsNotEmpty())
  389. {
  390. tmdids.AddRange(x.teachers);
  391. }
  392. classes.Remove(x.id);
  393. }
  394. if (tmdids.IsNotEmpty())
  395. {
  396. List<TmdInfo> infos = new List<TmdInfo>();
  397. List<string> inids = new List<string>();
  398. tmdids.ForEach(x => { inids.Add($"'{x}'"); });
  399. var insql = string.Join(",", inids);
  400. var queryslt = $"SELECT value(c) FROM c where c.id in ({insql})";
  401. //合并代码/
  402. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryIterator<TmdInfo>(queryText: queryslt, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base") }))
  403. {
  404. infos.Add(item);
  405. }
  406. tmdinfos.AddRange(infos);
  407. }
  408. tchLists.ForEach(x => {
  409. ClassListInfo classListInfo = new ClassListInfo { id = x.id, name = x.name };
  410. if (x.teachers.IsNotEmpty())
  411. {
  412. x.teachers.ForEach(y => {
  413. var tmdinfo = tmdinfos.Where(z => z.id.Equals(y)).FirstOrDefault();
  414. if (tmdinfo != null)
  415. {
  416. classListInfo.tmdInfos.Add(tmdinfo);
  417. }
  418. });
  419. }
  420. classInfo.Add(classListInfo);
  421. });
  422. }
  423. return (tmdinfos, classInfo);
  424. }
  425. catch (Exception ex)
  426. {
  427. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-OS,TriggerStuActivity-GetStuList\n{ex.Message}\n{ex.StackTrace}", GroupNames.醍摩豆服務運維群組);
  428. }
  429. return (null, null);
  430. }
  431. public static async Task<(List<TmdInfo> tmdinfos, List<StuInfo> students, List<ClassListInfo> classInfo)> GetStuList(CosmosClient client, DingDing _dingDing, List<string> claes, string school)
  432. {
  433. try
  434. {
  435. List<string> classes = new List<string>();
  436. foreach (string ss in claes)
  437. {
  438. classes.Add(ss);
  439. }
  440. List<TmdInfo> tmdinfos = new List<TmdInfo>();
  441. List<Students> studentss = new List<Students>();
  442. List<string> tmdids = new List<string>();
  443. List<StuInfo> stuInfos = new List<StuInfo>();
  444. if (!classes.IsNotEmpty()) { return (tmdinfos, new List<StuInfo>(), null); }
  445. List<string> sqlList = new List<string>();
  446. classes.ForEach(x => { sqlList.Add($" '{x}' "); });
  447. string sql = string.Join(" , ", sqlList);
  448. List<StuList> schList = new List<StuList>();
  449. List<Student> students = new List<Student>();
  450. if (!string.IsNullOrEmpty(school))
  451. {
  452. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<StuList>(queryText: $"select value(c) from c where c.id in ({sql})",
  453. requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"StuList-{school}") }))
  454. {
  455. schList.Add(item);
  456. }
  457. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Student").GetItemQueryIterator<Student>(queryText: $"select value(c) from c where c.classId in ({sql})",
  458. requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"Base-{school}") }))
  459. {
  460. students.Add(item);
  461. }
  462. }
  463. List<StuList> tchLists = new List<StuList>();
  464. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryIterator<StuList>(queryText: $"select value(c) from c where c.id in ({sql})",
  465. requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"StuList") }))
  466. {
  467. tchLists.Add(item);
  468. }
  469. foreach (var x in schList)
  470. {
  471. if (x.students.IsNotEmpty())
  472. {
  473. studentss.AddRange(x.students);
  474. }
  475. if (x.tmids.IsNotEmpty())
  476. {
  477. tmdids.AddRange(x.tmids);
  478. }
  479. classes.Remove(x.id);
  480. }
  481. foreach (var x in tchLists)
  482. {
  483. if (x.students.IsNotEmpty())
  484. {
  485. studentss.AddRange(x.students);
  486. }
  487. if (x.tmids.IsNotEmpty())
  488. {
  489. tmdids.AddRange(x.tmids);
  490. }
  491. classes.Remove(x.id);
  492. }
  493. if (tmdids.IsNotEmpty())
  494. {
  495. List<TmdInfo> infos = new List<TmdInfo>();
  496. List<string> inids = new List<string>();
  497. tmdids.ForEach(x => { inids.Add($"'{x}'"); });
  498. var insql = string.Join(",", inids);
  499. var queryslt = $"SELECT value(c) FROM c where c.id in ({insql})";
  500. //合并代码/
  501. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Student").GetItemQueryIterator<TmdInfo>(queryText: queryslt, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base") }))
  502. {
  503. infos.Add(item);
  504. }
  505. tmdinfos.AddRange(infos);
  506. }
  507. if (studentss.IsNotEmpty())
  508. {
  509. List<string> inidstus = new List<string>();
  510. foreach (Students stu in studentss)
  511. {
  512. var querystu = $"SELECT c.id,c.code,c.name,c.picture,c.classId,c.year,c.schoolId FROM c where c.id = '{stu.id}'";
  513. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Student").GetItemQueryIterator<StuInfo>(queryText: querystu, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"{stu.code}") }))
  514. {
  515. stuInfos.Add(item);
  516. }
  517. }
  518. /*studentss.Select(x => x.id).ToList().ForEach(x => { inidstus.Add($"'{x}'"); });
  519. var insqlstu = string.Join(",", inidstus);
  520. var querystu = $"SELECT c.id,c.code,c.name,c.picture,c.classId,c.year,c.schoolId FROM c where c.id in ({insqlstu})";
  521. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Student").GetItemQueryIterator<StuInfo>(queryText: querystu, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base-{school}") }))
  522. {
  523. stuInfos.Add(item);
  524. }*/
  525. }
  526. students.ForEach(x =>
  527. {
  528. if (!stuInfos.Select(y => y.classId).ToList().Contains(x.id))
  529. {
  530. stuInfos.Add(new StuInfo { id = x.id, code = x.code, schoolId = x.schoolId, classId = x.classId, name = x.name, picture = x.picture, year = x.year, groupId = x.groupId, groupName = x.groupName, no = x.no });
  531. }
  532. });
  533. List<ClassListInfo> classInfo = new List<ClassListInfo>();
  534. schList.ForEach(x => {
  535. ClassListInfo classListInfo = new ClassListInfo { id = x.id, name = x.name };
  536. if (x.students.IsNotEmpty())
  537. {
  538. x.students.ForEach(y => {
  539. var stuinfo = stuInfos.Where(z => z.id.Equals(y.id)).FirstOrDefault();
  540. if (stuinfo != null)
  541. {
  542. classListInfo.stuInfos.Add(stuinfo);
  543. }
  544. });
  545. }
  546. if (x.tmids.IsNotEmpty())
  547. {
  548. x.tmids.ForEach(y => {
  549. var tmdinfo = tmdinfos.Where(z => z.id.Equals(y)).FirstOrDefault();
  550. if (tmdinfo != null)
  551. {
  552. classListInfo.tmdInfos.Add(tmdinfo);
  553. }
  554. });
  555. }
  556. classInfo.Add(classListInfo);
  557. });
  558. tchLists.ForEach(x => {
  559. ClassListInfo classListInfo = new ClassListInfo { id = x.id, name = x.name };
  560. if (x.students.IsNotEmpty())
  561. {
  562. x.students.ForEach(y => {
  563. var stuinfo = stuInfos.Where(z => z.id.Equals(y.id)).FirstOrDefault();
  564. if (stuinfo != null)
  565. {
  566. classListInfo.stuInfos.Add(stuinfo);
  567. }
  568. });
  569. }
  570. if (x.tmids.IsNotEmpty())
  571. {
  572. x.tmids.ForEach(y => {
  573. var tmdinfo = tmdinfos.Where(z => z.id.Equals(y)).FirstOrDefault();
  574. if (tmdinfo != null)
  575. {
  576. classListInfo.tmdInfos.Add(tmdinfo);
  577. }
  578. });
  579. }
  580. classInfo.Add(classListInfo);
  581. });
  582. //var classeids= students.GroupBy(x => x.classId).Select(x => x.Key).ToList();
  583. List<ClassInfo> classInfos = await GetClassInfo(client, _dingDing, classes, school);
  584. classInfos.ForEach(x =>
  585. {
  586. ClassListInfo classListInfo = new ClassListInfo { id = x.id, name = x.name };
  587. var list = students.Where(y => y.classId.Equals(x.id)).ToList();
  588. list.ForEach(z => { classListInfo.stuInfos.Add(new StuInfo { id = z.id, code = z.code, schoolId = z.schoolId, classId = z.classId, name = z.name, picture = z.picture, year = z.year, groupId = z.groupId, groupName = z.groupName, no = z.no }); });
  589. classInfo.Add(classListInfo);
  590. });
  591. return (tmdinfos, stuInfos, classInfo);
  592. }
  593. catch (Exception ex)
  594. {
  595. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-OS,TriggerStuActivity-GetStuList\n{ex.Message}\n{ex.StackTrace}", GroupNames.醍摩豆服務運維群組);
  596. }
  597. return (null, null, null);
  598. }
  599. }
  600. public class ClassListInfo
  601. {
  602. public string id { get; set; }
  603. public string name { get; set; }
  604. // public string from { get; set; }
  605. public List<StuInfo> stuInfos { get; set; } = new List<StuInfo>();
  606. public List<TmdInfo> tmdInfos { get; set; } = new List<TmdInfo>();
  607. }
  608. }