TriggerStuActivity.cs 30 KB

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