TriggerStuActivity.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  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. namespace TEAMModelFunction
  16. {
  17. public class TriggerStuActivity
  18. {
  19. public static async Task RefreshStuActivity(CosmosClient client, DingDing _dingDing, string id, string code)
  20. {
  21. MQActivity activity = null;
  22. try
  23. {
  24. var aactivity = await client.GetContainer("TEAMModelOS", "Common").ReadItemStreamAsync(id, new Azure.Cosmos.PartitionKey(code));
  25. using var da = await JsonDocument.ParseAsync(aactivity.ContentStream);
  26. activity = da.ToObject<MQActivity>();
  27. }
  28. catch (CosmosException ex)
  29. {
  30. }
  31. if (activity != null)
  32. {
  33. (List<TmdInfo> tmdids, List<StuInfo> students,List<ClassListInfo> classLists) = await GetStuList(client, _dingDing, activity.classes, activity.school);
  34. if (tmdids.IsNotEmpty())
  35. {
  36. foreach (TmdInfo tmdid in tmdids)
  37. {
  38. var stucourse = new StuActivity
  39. {
  40. id = activity.id,
  41. scode = activity.code,
  42. name = activity.name,
  43. code = $"Activity-{tmdid.id}",
  44. scope = activity.scope,
  45. school = activity.school,
  46. creatorId = activity.creatorId,
  47. pk = "Activity",
  48. type = activity.pk,
  49. subjects = activity.pk.ToLower().Equals("exam") && activity.subjects.IsNotEmpty() ? new List<string>() { activity.subjects[0].id } : new List<string>() { "" },
  50. startTime = activity.startTime,
  51. endTime = activity.endTime,
  52. blob = activity.blob,
  53. owner = activity.owner
  54. };
  55. await client.GetContainer("TEAMModelOS", "Teacher").UpsertItemAsync(stucourse, new PartitionKey(stucourse.code));
  56. }
  57. }
  58. if (students.IsNotEmpty())
  59. {
  60. foreach (StuInfo student in students)
  61. {
  62. var stucourse = new StuActivity
  63. {
  64. id = activity.id,
  65. scode = activity.code,
  66. name = activity.name,
  67. code = $"Activity-{activity.school}-{student.id}",
  68. scope = activity.scope,
  69. school = activity.school,
  70. creatorId = activity.creatorId,
  71. pk = "Activity",
  72. type = activity.pk,
  73. subjects = activity.pk.ToLower().Equals("exam") && activity.subjects.IsNotEmpty() ? new List<string>() { activity.subjects[0].id } : new List<string>() { "" },
  74. startTime = activity.startTime,
  75. endTime = activity.endTime,
  76. blob = activity.blob,
  77. owner = activity.owner
  78. };
  79. await client.GetContainer("TEAMModelOS", "Student").UpsertItemAsync(stucourse, new PartitionKey(stucourse.code));
  80. }
  81. }
  82. }
  83. }
  84. public static async Task<string> SaveStuActivity(CosmosClient client, DingDing _dingDing, List<StuActivity> stuActivities,List<StuActivity> tmdActivities) {
  85. try {
  86. if (stuActivities.IsNotEmpty())
  87. {
  88. foreach (var x in stuActivities)
  89. {
  90. await client.GetContainer("TEAMModelOS", "Student").UpsertItemAsync(x, new PartitionKey(x.code));
  91. }
  92. }
  93. if (tmdActivities.IsNotEmpty())
  94. {
  95. foreach (var x in tmdActivities)
  96. {
  97. await client.GetContainer("TEAMModelOS", "Teacher").UpsertItemAsync(x, new PartitionKey(x.code));
  98. }
  99. }
  100. } catch (Exception ex) {
  101. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-OS,TriggerStuActivity-SaveStuActivity\n{ex.Message}\n{ex.StackTrace}", GroupNames.醍摩豆服務運維群組);
  102. }
  103. return "";
  104. }
  105. /// <summary>
  106. /// 获取行政班接口
  107. /// </summary>
  108. /// <param name="client"></param>
  109. /// <param name="_dingDing"></param>
  110. /// <param name="classes"></param>
  111. /// <param name="school"></param>
  112. /// <returns></returns>
  113. public static async Task<List<ClassInfo>> GetClassInfo(CosmosClient client, DingDing _dingDing, List<string> classes, string school)
  114. {
  115. try {
  116. List<ClassInfo> classInfos = new List<ClassInfo>();
  117. if (classes.IsNotEmpty()) {
  118. List<string> sqlList = new List<string>();
  119. classes.ForEach(x => { sqlList.Add($" '{x}' "); });
  120. string sql = string.Join(" , ", sqlList);
  121. if (!string.IsNullOrEmpty(school))
  122. {
  123. await foreach (var item in client.GetContainer("TEAMModelOS", "School").GetItemQueryIterator<ClassInfo>(queryText: $"select c.id,c.name ,c.periodId,c.year from c where c.id in ({sql})",
  124. requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"Class-{school}") }))
  125. {
  126. classInfos.Add(item);
  127. }
  128. await foreach (var item in client.GetContainer("TEAMModelOS", "School").GetItemQueryIterator<ClassInfo>(queryText: $"select c.id,c.name from c where c.id in ({sql})",
  129. requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"StuList-{school}") }))
  130. {
  131. //item.from = "SchStuList";
  132. classInfos.Add(item);
  133. }
  134. }
  135. List<StuList> tchLists = new List<StuList>();
  136. await foreach (var item in client.GetContainer("TEAMModelOS", "Teacher").GetItemQueryIterator<ClassInfo>(queryText: $"select c.id,c.name from c where c.id in ({sql})",
  137. requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"StuList") }))
  138. {
  139. // item.from = "TchStuList";
  140. classInfos.Add(item);
  141. }
  142. }
  143. return classInfos;
  144. } catch (Exception ex) {
  145. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-OS,TriggerStuActivity-GetClassInfo\n{ex.Message}\n{ex.StackTrace}\n class{classes.ToJsonString()},{school}", GroupNames.醍摩豆服務運維群組);
  146. throw new Exception(ex.Message, ex);
  147. }
  148. }
  149. public static async Task<(List<TmdInfo> tmdinfos, List<StuInfo> students, List<ClassListInfo> classInfo)> GetStuListInStu(CosmosClient client, DingDing _dingDing, List<string> claes, string school)
  150. {
  151. try
  152. {
  153. List<string> classes = new List<string>();
  154. foreach (string ss in claes)
  155. {
  156. classes.Add(ss);
  157. }
  158. List<TmdInfo> tmdinfos = new List<TmdInfo>();
  159. List<Students> studentss = new List<Students>();
  160. List<string> tmdids = new List<string>();
  161. List<StuInfo> stuInfos = new List<StuInfo>();
  162. if (!classes.IsNotEmpty()) { return (tmdinfos, new List<StuInfo>(), null); }
  163. List<string> sqlList = new List<string>();
  164. classes.ForEach(x => { sqlList.Add($" '{x}' "); });
  165. string sql = string.Join(" , ", sqlList);
  166. List<StuList> schList = new List<StuList>();
  167. List<Student> students = new List<Student>();
  168. if (!string.IsNullOrEmpty(school))
  169. {
  170. await foreach (var item in client.GetContainer("TEAMModelOS", "School").GetItemQueryIterator<StuList>(queryText: $"select value(c) from c where c.id in ({sql})",
  171. requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"StuList-{school}") }))
  172. {
  173. schList.Add(item);
  174. }
  175. await foreach (var item in client.GetContainer("TEAMModelOS", "Student").GetItemQueryIterator<Student>(queryText: $"select value(c) from c where c.classId in ({sql})",
  176. requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"Base-{school}") }))
  177. {
  178. students.Add(item);
  179. }
  180. }
  181. List<StuList> tchLists = new List<StuList>();
  182. await foreach (var item in client.GetContainer("TEAMModelOS", "Teacher").GetItemQueryIterator<StuList>(queryText: $"select value(c) from c where c.id in ({sql})",
  183. requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"StuList") }))
  184. {
  185. tchLists.Add(item);
  186. }
  187. foreach (var x in schList)
  188. {
  189. if (x.students.IsNotEmpty())
  190. {
  191. studentss.AddRange(x.students);
  192. }
  193. if (x.tmids.IsNotEmpty())
  194. {
  195. tmdids.AddRange(x.tmids);
  196. }
  197. classes.Remove(x.id);
  198. }
  199. foreach (var x in tchLists)
  200. {
  201. if (x.students.IsNotEmpty())
  202. {
  203. studentss.AddRange(x.students);
  204. }
  205. if (x.tmids.IsNotEmpty())
  206. {
  207. tmdids.AddRange(x.tmids);
  208. }
  209. classes.Remove(x.id);
  210. }
  211. if (tmdids.IsNotEmpty())
  212. {
  213. List<TmdInfo> infos = new List<TmdInfo>();
  214. List<string> inids = new List<string>();
  215. tmdids.ForEach(x => { inids.Add($"'{x}'"); });
  216. var insql = string.Join(",", inids);
  217. var queryslt = $"SELECT value(c) FROM c where c.id in ({insql})";
  218. await foreach (var item in client.GetContainer("TEAMModelOS", "Teacher").GetItemQueryIterator<TmdInfo>(queryText: queryslt, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base") }))
  219. {
  220. infos.Add(item);
  221. }
  222. tmdinfos.AddRange(infos);
  223. }
  224. if (studentss.IsNotEmpty())
  225. {
  226. var stuGroups = studentss.GroupBy(x => x.code).ToList().Select(x => new { key = x.Key, list = x.ToList() }) ;
  227. foreach (var gp in stuGroups) {
  228. List<string> inidstus = new List<string>();
  229. gp.list.Select(x => x.id).ToList().ForEach(x => { inidstus.Add($"'{x}'"); });
  230. PartitionKey partitionKey = new PartitionKey($"Base-{gp.key.Replace("Base-", "")}");
  231. var insqlstu = string.Join(",", inidstus);
  232. var querystu = $"SELECT c.id,c.code,c.name,c.picture,c.classId,c.year,c.schoolId FROM c where c.id in ({insqlstu})";
  233. await foreach (var item in client.GetContainer("TEAMModelOS", "Student").GetItemQueryIterator<StuInfo>(queryText: querystu, requestOptions: new QueryRequestOptions() { PartitionKey= partitionKey }))
  234. {
  235. stuInfos.Add(item);
  236. }
  237. }
  238. }
  239. students.ForEach(x =>
  240. {
  241. if (!stuInfos.Select(y => y.classId).ToList().Contains(x.id))
  242. {
  243. 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 });
  244. }
  245. });
  246. List<ClassListInfo> classInfo = new List<ClassListInfo>();
  247. schList.ForEach(x => {
  248. ClassListInfo classListInfo = new ClassListInfo { id = x.id, name = x.name };
  249. if (x.students.IsNotEmpty())
  250. {
  251. x.students.ForEach(y => {
  252. var stuinfo = stuInfos.Where(z => z.id.Equals(y.id)).FirstOrDefault();
  253. if (stuinfo != null)
  254. {
  255. classListInfo.stuInfos.Add(stuinfo);
  256. }
  257. });
  258. }
  259. if (x.tmids.IsNotEmpty())
  260. {
  261. x.tmids.ForEach(y => {
  262. var tmdinfo = tmdinfos.Where(z => z.id.Equals(y)).FirstOrDefault();
  263. if (tmdinfo != null)
  264. {
  265. classListInfo.tmdInfos.Add(tmdinfo);
  266. }
  267. });
  268. }
  269. classInfo.Add(classListInfo);
  270. });
  271. tchLists.ForEach(x => {
  272. ClassListInfo classListInfo = new ClassListInfo { id = x.id, name = x.name };
  273. if (x.students.IsNotEmpty())
  274. {
  275. x.students.ForEach(y => {
  276. var stuinfo = stuInfos.Where(z => z.id.Equals(y.id)).FirstOrDefault();
  277. if (stuinfo != null)
  278. {
  279. classListInfo.stuInfos.Add(stuinfo);
  280. }
  281. });
  282. }
  283. if (x.tmids.IsNotEmpty())
  284. {
  285. x.tmids.ForEach(y => {
  286. var tmdinfo = tmdinfos.Where(z => z.id.Equals(y)).FirstOrDefault();
  287. if (tmdinfo != null)
  288. {
  289. classListInfo.tmdInfos.Add(tmdinfo);
  290. }
  291. });
  292. }
  293. classInfo.Add(classListInfo);
  294. });
  295. //var classeids= students.GroupBy(x => x.classId).Select(x => x.Key).ToList();
  296. List<ClassInfo> classInfos = await GetClassInfo(client, _dingDing, classes, school);
  297. classInfos.ForEach(x =>
  298. {
  299. ClassListInfo classListInfo = new ClassListInfo { id = x.id, name = x.name };
  300. var list = students.Where(y => y.classId .Equals(x.id)).ToList();
  301. 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 }); });
  302. classInfo.Add(classListInfo);
  303. });
  304. return (tmdinfos, stuInfos, classInfo);
  305. }
  306. catch (Exception ex)
  307. {
  308. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-OS,TriggerStuActivity-GetStuList\n{ex.Message}\n{ex.StackTrace}", GroupNames.醍摩豆服務運維群組);
  309. }
  310. return (null, null, null);
  311. }
  312. public static async Task<(List<TmdInfo> tmdinfos, List<StuInfo> students,List<ClassListInfo>classInfo)> GetStuList(CosmosClient client, DingDing _dingDing, List<string> claes, string school)
  313. {
  314. try {
  315. List<string> classes = new List<string>();
  316. foreach (string ss in claes)
  317. {
  318. classes.Add(ss);
  319. }
  320. List<TmdInfo> tmdinfos = new List<TmdInfo>();
  321. List<Students> studentss = new List<Students>();
  322. List<string> tmdids = new List<string>();
  323. List<StuInfo> stuInfos = new List<StuInfo>();
  324. if (!classes.IsNotEmpty()) { return (tmdinfos, new List<StuInfo>(),null); }
  325. List<string> sqlList = new List<string>();
  326. classes.ForEach(x => { sqlList.Add($" '{x}' "); });
  327. string sql = string.Join(" , ", sqlList);
  328. List<StuList> schList = new List<StuList>();
  329. List<Student> students = new List<Student>();
  330. if (!string.IsNullOrEmpty(school)) {
  331. await foreach (var item in client.GetContainer("TEAMModelOS", "School").GetItemQueryIterator<StuList>(queryText: $"select value(c) from c where c.id in ({sql})",
  332. requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"StuList-{school}") }))
  333. {
  334. schList.Add(item);
  335. }
  336. await foreach (var item in client.GetContainer("TEAMModelOS", "Student").GetItemQueryIterator<Student>(queryText: $"select value(c) from c where c.classId in ({sql})",
  337. requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"Base-{school}") }))
  338. {
  339. students.Add(item);
  340. }
  341. }
  342. List<StuList> tchLists = new List<StuList>();
  343. await foreach (var item in client.GetContainer("TEAMModelOS", "Teacher").GetItemQueryIterator<StuList>(queryText: $"select value(c) from c where c.id in ({sql})",
  344. requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"StuList") }))
  345. {
  346. tchLists.Add(item);
  347. }
  348. foreach (var x in schList) {
  349. if (x.students.IsNotEmpty())
  350. {
  351. studentss.AddRange(x.students);
  352. }
  353. if (x.tmids.IsNotEmpty())
  354. {
  355. tmdids.AddRange(x.tmids);
  356. }
  357. classes.Remove(x.id);
  358. }
  359. foreach (var x in tchLists)
  360. {
  361. if (x.students.IsNotEmpty()) {
  362. studentss.AddRange(x.students);
  363. }
  364. if (x.tmids.IsNotEmpty())
  365. {
  366. tmdids.AddRange(x.tmids);
  367. }
  368. classes.Remove(x.id);
  369. }
  370. if (tmdids.IsNotEmpty()) {
  371. List<TmdInfo> infos = new List<TmdInfo>();
  372. List<string> inids = new List<string>();
  373. tmdids.ForEach(x => { inids.Add($"'{x}'"); });
  374. var insql = string.Join(",", inids);
  375. var queryslt = $"SELECT value(c) FROM c where c.id in ({insql})";
  376. await foreach (var item in client.GetContainer("TEAMModelOS", "Teacher").GetItemQueryIterator<TmdInfo>(queryText: queryslt, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base") }))
  377. {
  378. infos.Add(item);
  379. }
  380. tmdinfos.AddRange(infos);
  381. }
  382. if (studentss.IsNotEmpty()) {
  383. List<string> inidstus = new List<string>();
  384. foreach (Students stu in studentss) {
  385. var querystu = $"SELECT c.id,c.code,c.name,c.picture,c.classId,c.year,c.schoolId FROM c where c.id = '{stu.id}'";
  386. await foreach (var item in client.GetContainer("TEAMModelOS", "Student").GetItemQueryIterator<StuInfo>(queryText: querystu, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"{stu.code}") }))
  387. {
  388. stuInfos.Add(item);
  389. }
  390. }
  391. /*studentss.Select(x => x.id).ToList().ForEach(x => { inidstus.Add($"'{x}'"); });
  392. var insqlstu = string.Join(",", inidstus);
  393. var querystu = $"SELECT c.id,c.code,c.name,c.picture,c.classId,c.year,c.schoolId FROM c where c.id in ({insqlstu})";
  394. await foreach (var item in client.GetContainer("TEAMModelOS", "Student").GetItemQueryIterator<StuInfo>(queryText: querystu, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base-{school}") }))
  395. {
  396. stuInfos.Add(item);
  397. }*/
  398. }
  399. students.ForEach(x =>
  400. {
  401. if (!stuInfos.Select(y => y.classId).ToList().Contains(x.id)) {
  402. 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 });
  403. }
  404. });
  405. List<ClassListInfo> classInfo = new List<ClassListInfo>();
  406. schList.ForEach(x => {
  407. ClassListInfo classListInfo = new ClassListInfo {id=x.id,name=x.name };
  408. if (x.students.IsNotEmpty()) {
  409. x.students.ForEach(y => {
  410. var stuinfo= stuInfos.Where(z => z.id.Equals(y.id)).FirstOrDefault();
  411. if (stuinfo != null) {
  412. classListInfo.stuInfos.Add(stuinfo);
  413. }
  414. });
  415. }
  416. if (x.tmids.IsNotEmpty())
  417. {
  418. x.tmids.ForEach(y => {
  419. var tmdinfo = tmdinfos.Where(z => z.id.Equals(y)).FirstOrDefault();
  420. if (tmdinfo != null)
  421. {
  422. classListInfo.tmdInfos.Add(tmdinfo);
  423. }
  424. });
  425. }
  426. classInfo.Add(classListInfo);
  427. });
  428. tchLists.ForEach(x => {
  429. ClassListInfo classListInfo = new ClassListInfo { id = x.id, name = x.name };
  430. if (x.students.IsNotEmpty())
  431. {
  432. x.students.ForEach(y => {
  433. var stuinfo = stuInfos.Where(z => z.id.Equals(y.id)).FirstOrDefault();
  434. if (stuinfo != null)
  435. {
  436. classListInfo.stuInfos.Add(stuinfo);
  437. }
  438. });
  439. }
  440. if (x.tmids.IsNotEmpty())
  441. {
  442. x.tmids.ForEach(y => {
  443. var tmdinfo = tmdinfos.Where(z => z.id.Equals(y)).FirstOrDefault();
  444. if (tmdinfo != null)
  445. {
  446. classListInfo.tmdInfos.Add(tmdinfo);
  447. }
  448. });
  449. }
  450. classInfo.Add(classListInfo);
  451. });
  452. //var classeids= students.GroupBy(x => x.classId).Select(x => x.Key).ToList();
  453. List<ClassInfo> classInfos= await GetClassInfo(client, _dingDing, classes, school);
  454. classInfos.ForEach(x =>
  455. {
  456. ClassListInfo classListInfo = new ClassListInfo { id = x.id, name = x.name };
  457. var list= students.Where(y => y.classId .Equals(x.id)).ToList();
  458. 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 }); });
  459. classInfo.Add(classListInfo);
  460. });
  461. return (tmdinfos, stuInfos, classInfo) ;
  462. }
  463. catch (Exception ex)
  464. {
  465. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-OS,TriggerStuActivity-GetStuList\n{ex.Message}\n{ex.StackTrace}", GroupNames.醍摩豆服務運維群組);
  466. }
  467. return (null, null,null);
  468. }
  469. }
  470. public class ClassListInfo
  471. {
  472. public string id { get; set; }
  473. public string name { get; set; }
  474. // public string from { get; set; }
  475. public List<StuInfo> stuInfos { get; set; } = new List<StuInfo>();
  476. public List<TmdInfo> tmdInfos { get; set; } = new List<TmdInfo>();
  477. }
  478. }