TriggerStuActivity.cs 32 KB

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