TriggerStuActivity.cs 33 KB

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