StuListService.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. using Azure.Cosmos;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. using System.Text.Json;
  6. using System.Threading.Tasks;
  7. using TEAMModelOS.SDK.DI;
  8. using TEAMModelOS.SDK.Extension;
  9. using TEAMModelOS.SDK;
  10. using TEAMModelOS.SDK.Models;
  11. using TEAMModelOS.SDK.Models.Cosmos.Common;
  12. namespace TEAMModelFunction
  13. {
  14. public class StuListService
  15. {
  16. public static async Task FixActivity(CosmosClient client, StuListChange stuListChange, string type)
  17. {
  18. var query = $"SELECT distinct c.owner, c.id,c.code, c.classes,c.subjects,c.progress,c.scope,c.startTime,c.school,c.creatorId,c.name,c.pk ,c.endTime FROM c join A1 in c.classes where c.pk='{type}' and A1 in('{stuListChange.listid}') ";
  19. List<MQActivity> datas = new List<MQActivity>();
  20. await foreach (var item in client.GetContainer("TEAMModelOS", "Common").GetItemQueryIterator<MQActivity>(queryText: query,
  21. requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"{type}-{stuListChange.originCode}") }))
  22. {
  23. datas.Add(item);
  24. }
  25. foreach (MQActivity activity in datas)
  26. {
  27. //已经完结的不再允许加入
  28. if (activity.progress.Equals("finish") || activity.progress.Equals("pending"))
  29. {
  30. continue;
  31. }
  32. //学生新加入名单的
  33. foreach (Students students in stuListChange.stujoin)
  34. {
  35. var stucourse = new StuActivity
  36. {
  37. id = activity.id,
  38. scode = activity.code,
  39. name = activity.name,
  40. code = $"Activity-{activity.school}-{students.id}",
  41. scope = activity.scope,
  42. school = activity.school,
  43. creatorId = activity.creatorId,
  44. pk = "Activity",
  45. type = type,
  46. subjects = activity.pk.ToLower().Equals("exam") && activity.subjects.IsNotEmpty() ? new List<string>() { activity.subjects[0].id } : new List<string>() { "" },
  47. startTime = activity.startTime,
  48. endTime = activity.endTime,
  49. blob=activity.blob,
  50. owner= activity.owner
  51. };
  52. await client.GetContainer("TEAMModelOS", "Student").UpsertItemAsync(stucourse, new PartitionKey(stucourse.code));
  53. }//tmd新加入的
  54. foreach (string tmdid in stuListChange.tmdjoin)
  55. {
  56. var stucourse = new StuActivity
  57. {
  58. id = activity.id,
  59. scode = activity.code,
  60. name = activity.name,
  61. code = $"Activity-{tmdid}",
  62. scope = activity.scope,
  63. school = activity.school,
  64. creatorId = activity.creatorId,
  65. pk = "Activity",
  66. type = type,
  67. subjects = activity.pk.ToLower().Equals("exam") && activity.subjects.IsNotEmpty() ? new List<string>() { activity.subjects[0].id } : new List<string>() { "" },
  68. startTime = activity.startTime,
  69. endTime = activity.endTime,
  70. blob = activity.blob,
  71. owner = activity.owner
  72. };
  73. await client.GetContainer("TEAMModelOS", "Teacher").UpsertItemAsync(stucourse, new PartitionKey(stucourse.code));
  74. }
  75. foreach (Students students in stuListChange.stuleave) {
  76. try {
  77. await client.GetContainer("TEAMModelOS", "Teacher").DeleteItemAsync<StuActivity>(activity.id, new PartitionKey($"Activity-{activity.school}-{students.id}"));
  78. } catch {
  79. //仅处理未写入的数据。
  80. }
  81. }
  82. foreach (string tmdid in stuListChange.tmdhleave) {
  83. try
  84. {
  85. await client.GetContainer("TEAMModelOS", "Teacher").DeleteItemAsync<StuActivity>(activity.id, new PartitionKey($"Activity-{tmdid}"));
  86. }
  87. catch
  88. {
  89. //仅处理未写入的数据。
  90. }
  91. }
  92. }
  93. }
  94. public static async Task FixStuCourse(CosmosClient client, StuListChange stuListChange)
  95. {
  96. //1.查找学校或教师的课程是否包含该名单的课程。
  97. var query = $"select distinct c.code,c.id,c.no,c.name,c.scope, c.creatorId,c.school from c join A0 in c.schedule where A0.stulist = '{stuListChange.listid}'";
  98. List<Course> courses = new List<Course>();
  99. if (stuListChange.scope.Equals("school"))
  100. {
  101. await foreach (var item in client.GetContainer("TEAMModelOS", "School").GetItemQueryIterator<Course>(queryText: query,
  102. requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"Course-{stuListChange.originCode}") }))
  103. {
  104. courses.Add(item);
  105. }
  106. }
  107. else
  108. {
  109. await foreach (var item in client.GetContainer("TEAMModelOS", "Teacher").GetItemQueryIterator<Course>(queryText: query,
  110. requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"Course-{stuListChange.originCode}") }))
  111. {
  112. courses.Add(item);
  113. }
  114. }
  115. //2.获取课程的id 并尝试添加或移除对应的学生课程记录StuCourse。
  116. foreach (var course in courses)
  117. {
  118. //学生新加入名单的
  119. foreach (Students students in stuListChange.stujoin)
  120. {
  121. var stucourse = new StuCourse
  122. {
  123. id = course.id,
  124. scode = course.code,
  125. name = course.name,
  126. code = $"StuCourse-{course.school}-{students.id}",
  127. scope = course.scope,
  128. school = course.school,
  129. creatorId = course.creatorId,
  130. pk = "StuCourse"
  131. };
  132. await client.GetContainer("TEAMModelOS", "Student").UpsertItemAsync(stucourse, new PartitionKey(stucourse.code));
  133. }
  134. //tmd新加入的
  135. foreach (string tmdid in stuListChange.tmdjoin)
  136. {
  137. var stucourse = new StuCourse
  138. {
  139. id = course.id,
  140. scode = course.code,
  141. name = course.name,
  142. code = $"StuCourse-{tmdid}",
  143. scope = course.scope,
  144. school = course.school,
  145. creatorId = course.creatorId,
  146. pk = "StuCourse"
  147. };
  148. await client.GetContainer("TEAMModelOS", "Teacher").UpsertItemAsync(stucourse, new PartitionKey(stucourse.code));
  149. }
  150. //移除名单的。 在点击相关的课程,再去二次校验是否存在,不存在则再去删除。
  151. foreach (var delStu in stuListChange.stuleave)
  152. {
  153. await client.GetContainer("TEAMModelOS", "Student").DeleteItemStreamAsync(course.id, new PartitionKey($"Course-{course.school}-{delStu.id}"));
  154. }
  155. foreach (var delTmd in stuListChange.tmdhleave)
  156. {
  157. await client.GetContainer("TEAMModelOS", "Teacher").DeleteItemStreamAsync(course.id, new PartitionKey($"Course-{delTmd}"));
  158. }
  159. }
  160. }
  161. }
  162. }