TriggerStuActivity.cs 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  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<TmdInfo> tmdinfos = new List<TmdInfo>();
  352. List<ClassListInfo> classInfo = new List<ClassListInfo>();
  353. if (claes.Count == 1 && claes.First().Equals("default"))
  354. {
  355. List<TmdInfo> infos = new List<TmdInfo>();
  356. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryIterator<TmdInfo>(queryText: $"SELECT value(c) FROM c ", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base") }))
  357. {
  358. infos.Add(item);
  359. }
  360. tmdinfos.AddRange(infos);
  361. }
  362. else {
  363. List<string> classes = new List<string>();
  364. foreach (string ss in claes)
  365. {
  366. classes.Add(ss);
  367. }
  368. List<string> tmdids = new List<string>();
  369. List<StuInfo> stuInfos = new List<StuInfo>();
  370. if (!classes.IsNotEmpty()) { return (tmdinfos, null); }
  371. List<string> sqlList = new List<string>();
  372. classes.ForEach(x => { sqlList.Add($" '{x}' "); });
  373. string sql = string.Join(" , ", sqlList);
  374. List<TchList> tchLists = new List<TchList>();
  375. if (!string.IsNullOrEmpty(school))
  376. {
  377. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<TchList>(queryText: $"select value(c) from c where c.id in ({sql})",
  378. requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"TchList-{school}") }))
  379. {
  380. tchLists.Add(item);
  381. }
  382. }
  383. foreach (var x in tchLists)
  384. {
  385. if (x.teachers.IsNotEmpty())
  386. {
  387. tmdids.AddRange(x.teachers);
  388. }
  389. classes.Remove(x.id);
  390. }
  391. if (tmdids.IsNotEmpty())
  392. {
  393. List<TmdInfo> infos = new List<TmdInfo>();
  394. List<string> inids = new List<string>();
  395. tmdids.ForEach(x => { inids.Add($"'{x}'"); });
  396. var insql = string.Join(",", inids);
  397. var queryslt = $"SELECT value(c) FROM c where c.id in ({insql})";
  398. //合并代码/
  399. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryIterator<TmdInfo>(queryText: queryslt, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base") }))
  400. {
  401. infos.Add(item);
  402. }
  403. tmdinfos.AddRange(infos);
  404. }
  405. tchLists.ForEach(x => {
  406. ClassListInfo classListInfo = new ClassListInfo { id = x.id, name = x.name };
  407. if (x.teachers.IsNotEmpty())
  408. {
  409. x.teachers.ForEach(y => {
  410. var tmdinfo = tmdinfos.Where(z => z.id.Equals(y)).FirstOrDefault();
  411. if (tmdinfo != null)
  412. {
  413. classListInfo.tmdInfos.Add(tmdinfo);
  414. }
  415. });
  416. }
  417. classInfo.Add(classListInfo);
  418. });
  419. tchLists.ForEach(x => {
  420. ClassListInfo classListInfo = new ClassListInfo { id = x.id, name = x.name };
  421. if (x.teachers.IsNotEmpty())
  422. {
  423. x.teachers.ForEach(y => {
  424. var tmdinfo = tmdinfos.Where(z => z.id.Equals(y)).FirstOrDefault();
  425. if (tmdinfo != null)
  426. {
  427. classListInfo.tmdInfos.Add(tmdinfo);
  428. }
  429. });
  430. }
  431. classInfo.Add(classListInfo);
  432. });
  433. //var classeids= students.GroupBy(x => x.classId).Select(x => x.Key).ToList();
  434. List<ClassInfo> classInfos = await GetClassInfo(client, _dingDing, classes, school);
  435. classInfos.ForEach(x =>
  436. {
  437. ClassListInfo classListInfo = new ClassListInfo { id = x.id, name = x.name };
  438. classInfo.Add(classListInfo);
  439. });
  440. }
  441. return (tmdinfos, classInfo);
  442. }
  443. catch (Exception ex)
  444. {
  445. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-OS,TriggerStuActivity-GetStuList\n{ex.Message}\n{ex.StackTrace}", GroupNames.醍摩豆服務運維群組);
  446. }
  447. return (null, null);
  448. }
  449. public static async Task<(List<TmdInfo> tmdinfos, List<StuInfo> students, List<ClassListInfo> classInfo)> GetStuList(CosmosClient client, DingDing _dingDing, List<string> claes, string school)
  450. {
  451. try
  452. {
  453. List<string> classes = new List<string>();
  454. foreach (string ss in claes)
  455. {
  456. classes.Add(ss);
  457. }
  458. List<TmdInfo> tmdinfos = new List<TmdInfo>();
  459. List<Students> studentss = new List<Students>();
  460. List<string> tmdids = new List<string>();
  461. List<StuInfo> stuInfos = new List<StuInfo>();
  462. if (!classes.IsNotEmpty()) { return (tmdinfos, new List<StuInfo>(), null); }
  463. List<string> sqlList = new List<string>();
  464. classes.ForEach(x => { sqlList.Add($" '{x}' "); });
  465. string sql = string.Join(" , ", sqlList);
  466. List<StuList> schList = new List<StuList>();
  467. List<Student> students = new List<Student>();
  468. if (!string.IsNullOrEmpty(school))
  469. {
  470. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<StuList>(queryText: $"select value(c) from c where c.id in ({sql})",
  471. requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"StuList-{school}") }))
  472. {
  473. schList.Add(item);
  474. }
  475. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Student").GetItemQueryIterator<Student>(queryText: $"select value(c) from c where c.classId in ({sql})",
  476. requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"Base-{school}") }))
  477. {
  478. students.Add(item);
  479. }
  480. }
  481. List<StuList> tchLists = new List<StuList>();
  482. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryIterator<StuList>(queryText: $"select value(c) from c where c.id in ({sql})",
  483. requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"StuList") }))
  484. {
  485. tchLists.Add(item);
  486. }
  487. foreach (var x in schList)
  488. {
  489. if (x.students.IsNotEmpty())
  490. {
  491. studentss.AddRange(x.students);
  492. }
  493. if (x.tmids.IsNotEmpty())
  494. {
  495. tmdids.AddRange(x.tmids);
  496. }
  497. classes.Remove(x.id);
  498. }
  499. foreach (var x in tchLists)
  500. {
  501. if (x.students.IsNotEmpty())
  502. {
  503. studentss.AddRange(x.students);
  504. }
  505. if (x.tmids.IsNotEmpty())
  506. {
  507. tmdids.AddRange(x.tmids);
  508. }
  509. classes.Remove(x.id);
  510. }
  511. if (tmdids.IsNotEmpty())
  512. {
  513. List<TmdInfo> infos = new List<TmdInfo>();
  514. List<string> inids = new List<string>();
  515. tmdids.ForEach(x => { inids.Add($"'{x}'"); });
  516. var insql = string.Join(",", inids);
  517. var queryslt = $"SELECT value(c) FROM c where c.id in ({insql})";
  518. //合并代码/
  519. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Student").GetItemQueryIterator<TmdInfo>(queryText: queryslt, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base") }))
  520. {
  521. infos.Add(item);
  522. }
  523. tmdinfos.AddRange(infos);
  524. }
  525. if (studentss.IsNotEmpty())
  526. {
  527. List<string> inidstus = new List<string>();
  528. foreach (Students stu in studentss)
  529. {
  530. var querystu = $"SELECT c.id,c.code,c.name,c.picture,c.classId,c.year,c.schoolId FROM c where c.id = '{stu.id}'";
  531. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Student").GetItemQueryIterator<StuInfo>(queryText: querystu, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"{stu.code}") }))
  532. {
  533. stuInfos.Add(item);
  534. }
  535. }
  536. /*studentss.Select(x => x.id).ToList().ForEach(x => { inidstus.Add($"'{x}'"); });
  537. var insqlstu = string.Join(",", inidstus);
  538. var querystu = $"SELECT c.id,c.code,c.name,c.picture,c.classId,c.year,c.schoolId FROM c where c.id in ({insqlstu})";
  539. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Student").GetItemQueryIterator<StuInfo>(queryText: querystu, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base-{school}") }))
  540. {
  541. stuInfos.Add(item);
  542. }*/
  543. }
  544. students.ForEach(x =>
  545. {
  546. if (!stuInfos.Select(y => y.classId).ToList().Contains(x.id))
  547. {
  548. 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 });
  549. }
  550. });
  551. List<ClassListInfo> classInfo = new List<ClassListInfo>();
  552. schList.ForEach(x => {
  553. ClassListInfo classListInfo = new ClassListInfo { id = x.id, name = x.name };
  554. if (x.students.IsNotEmpty())
  555. {
  556. x.students.ForEach(y => {
  557. var stuinfo = stuInfos.Where(z => z.id.Equals(y.id)).FirstOrDefault();
  558. if (stuinfo != null)
  559. {
  560. classListInfo.stuInfos.Add(stuinfo);
  561. }
  562. });
  563. }
  564. if (x.tmids.IsNotEmpty())
  565. {
  566. x.tmids.ForEach(y => {
  567. var tmdinfo = tmdinfos.Where(z => z.id.Equals(y)).FirstOrDefault();
  568. if (tmdinfo != null)
  569. {
  570. classListInfo.tmdInfos.Add(tmdinfo);
  571. }
  572. });
  573. }
  574. classInfo.Add(classListInfo);
  575. });
  576. tchLists.ForEach(x => {
  577. ClassListInfo classListInfo = new ClassListInfo { id = x.id, name = x.name };
  578. if (x.students.IsNotEmpty())
  579. {
  580. x.students.ForEach(y => {
  581. var stuinfo = stuInfos.Where(z => z.id.Equals(y.id)).FirstOrDefault();
  582. if (stuinfo != null)
  583. {
  584. classListInfo.stuInfos.Add(stuinfo);
  585. }
  586. });
  587. }
  588. if (x.tmids.IsNotEmpty())
  589. {
  590. x.tmids.ForEach(y => {
  591. var tmdinfo = tmdinfos.Where(z => z.id.Equals(y)).FirstOrDefault();
  592. if (tmdinfo != null)
  593. {
  594. classListInfo.tmdInfos.Add(tmdinfo);
  595. }
  596. });
  597. }
  598. classInfo.Add(classListInfo);
  599. });
  600. //var classeids= students.GroupBy(x => x.classId).Select(x => x.Key).ToList();
  601. List<ClassInfo> classInfos = await GetClassInfo(client, _dingDing, classes, school);
  602. classInfos.ForEach(x =>
  603. {
  604. ClassListInfo classListInfo = new ClassListInfo { id = x.id, name = x.name };
  605. var list = students.Where(y => y.classId.Equals(x.id)).ToList();
  606. 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 }); });
  607. classInfo.Add(classListInfo);
  608. });
  609. return (tmdinfos, stuInfos, classInfo);
  610. }
  611. catch (Exception ex)
  612. {
  613. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-OS,TriggerStuActivity-GetStuList\n{ex.Message}\n{ex.StackTrace}", GroupNames.醍摩豆服務運維群組);
  614. }
  615. return (null, null, null);
  616. }
  617. }
  618. public class ClassListInfo
  619. {
  620. public string id { get; set; }
  621. public string name { get; set; }
  622. // public string from { get; set; }
  623. public List<StuInfo> stuInfos { get; set; } = new List<StuInfo>();
  624. public List<TmdInfo> tmdInfos { get; set; } = new List<TmdInfo>();
  625. }
  626. }