StuListService.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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,DingDing _dingDing, StuListChange stuListChange, string type)
  17. {
  18. try
  19. {
  20. 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}') ";
  21. List<MQActivity> datas = new List<MQActivity>();
  22. if (stuListChange.scope.Equals("school", StringComparison.OrdinalIgnoreCase) && !string.IsNullOrEmpty(stuListChange.school)) {
  23. await foreach (var item in client.GetContainer("TEAMModelOS", "Common").GetItemQueryIterator<MQActivity>(queryText: query,
  24. requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"{type}-{stuListChange.school}") }))
  25. {
  26. datas.Add(item);
  27. }
  28. ///还要处理该学校每个老师发布的班级的
  29. List<SchoolTeacher> teachers = new List<SchoolTeacher>();
  30. await foreach (var item in client.GetContainer("TEAMModelOS", "School").GetItemQueryIterator<SchoolTeacher>(queryText: $"SELECT c.id, c.name FROM c",
  31. requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Teacher-{stuListChange.school}") }))
  32. {
  33. teachers.Add(item);
  34. }
  35. foreach (var techer in teachers) {
  36. var queryTech = $"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 " +
  37. $" join A1 in c.classes where c.school='{stuListChange.school}' and c.pk='{type}' and A1 in('{stuListChange.listid}') ";
  38. await foreach (var item in client.GetContainer("TEAMModelOS", "Common").GetItemQueryIterator<MQActivity>(queryText: queryTech,
  39. requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"{type}-{techer.id}") }))
  40. {
  41. datas.Add(item);
  42. }
  43. }
  44. }
  45. if (stuListChange.scope.Equals("private", StringComparison.OrdinalIgnoreCase)&&!string.IsNullOrEmpty(stuListChange.creatorId))
  46. {
  47. await foreach (var item in client.GetContainer("TEAMModelOS", "Common").GetItemQueryIterator<MQActivity>(queryText: query,
  48. requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"{type}-{stuListChange.creatorId}") }))
  49. {
  50. datas.Add(item);
  51. }
  52. }
  53. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-StuListService-FixActivity\n名单发生变更 需要处理的活动\n{datas.ToJsonString()}", GroupNames.醍摩豆服務運維群組);
  54. foreach (MQActivity activity in datas)
  55. {
  56. //已经完结的不再允许加入
  57. if (activity.progress.Equals("finish") || activity.progress.Equals("pending"))
  58. {
  59. continue;
  60. }
  61. //学生新加入名单的
  62. foreach (Students students in stuListChange.stujoin)
  63. {
  64. var stucourse = new StuActivity
  65. {
  66. id = activity.id,
  67. scode = activity.code,
  68. name = activity.name,
  69. code = $"Activity-{activity.school}-{students.id}",
  70. scope = activity.scope,
  71. school = activity.school,
  72. creatorId = activity.creatorId,
  73. pk = "Activity",
  74. type = type,
  75. subjects = activity.pk.ToLower().Equals("exam") && activity.subjects.IsNotEmpty() ? new List<string>() { activity.subjects[0].id } : new List<string>() { "" },
  76. startTime = activity.startTime,
  77. endTime = activity.endTime,
  78. blob = activity.blob,
  79. owner = activity.owner
  80. };
  81. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-StuListService-FixActivity\n名单发生变更 新建活动中间表\n{stucourse.ToJsonString()}", GroupNames.醍摩豆服務運維群組);
  82. await client.GetContainer("TEAMModelOS", "Student").UpsertItemAsync(stucourse, new PartitionKey(stucourse.code));
  83. }//tmd新加入的
  84. foreach (string tmdid in stuListChange.tmdjoin)
  85. {
  86. var stucourse = new StuActivity
  87. {
  88. id = activity.id,
  89. scode = activity.code,
  90. name = activity.name,
  91. code = $"Activity-{tmdid}",
  92. scope = activity.scope,
  93. school = activity.school,
  94. creatorId = activity.creatorId,
  95. pk = "Activity",
  96. type = type,
  97. subjects = activity.pk.ToLower().Equals("exam") && activity.subjects.IsNotEmpty() ? new List<string>() { activity.subjects[0].id } : new List<string>() { "" },
  98. startTime = activity.startTime,
  99. endTime = activity.endTime,
  100. blob = activity.blob,
  101. owner = activity.owner
  102. };
  103. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-StuListService-FixActivity\n名单发生变更 新建活动中间表\n{stucourse.ToJsonString()}", GroupNames.醍摩豆服務運維群組);
  104. await client.GetContainer("TEAMModelOS", "Teacher").UpsertItemAsync(stucourse, new PartitionKey(stucourse.code));
  105. }
  106. foreach (Students students in stuListChange.stuleave)
  107. {
  108. try
  109. {
  110. await client.GetContainer("TEAMModelOS", "Teacher").DeleteItemAsync<StuActivity>(activity.id, new PartitionKey($"Activity-{activity.school}-{students.id}"));
  111. }
  112. catch (CosmosException ex)
  113. {
  114. //仅处理未写入的数据。
  115. }
  116. }
  117. foreach (string tmdid in stuListChange.tmdhleave)
  118. {
  119. try
  120. {
  121. await client.GetContainer("TEAMModelOS", "Teacher").DeleteItemAsync<StuActivity>(activity.id, new PartitionKey($"Activity-{tmdid}"));
  122. }
  123. catch(CosmosException ex )
  124. {
  125. //仅处理未写入的数据。
  126. }
  127. }
  128. }
  129. }
  130. catch(Exception ex ) {
  131. }
  132. }
  133. public static async Task FixStuCourse(CosmosClient client,DingDing _dingDing, StuListChange stuListChange)
  134. {
  135. //1.查找学校或教师的课程是否包含该名单的课程。
  136. 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}'";
  137. List<Course> courses = new List<Course>();
  138. if (stuListChange.scope.Equals("school") && !string.IsNullOrEmpty(stuListChange.school))
  139. {
  140. await foreach (var item in client.GetContainer("TEAMModelOS", "School").GetItemQueryIterator<Course>(queryText: query,
  141. requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"Course-{stuListChange.school}") }))
  142. {
  143. courses.Add(item);
  144. }
  145. }
  146. if (stuListChange.scope.Equals("private") && !string.IsNullOrEmpty(stuListChange.creatorId))
  147. {
  148. await foreach (var item in client.GetContainer("TEAMModelOS", "Teacher").GetItemQueryIterator<Course>(queryText: query,
  149. requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"Course-{stuListChange.creatorId}") }))
  150. {
  151. courses.Add(item);
  152. }
  153. }
  154. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-StuListService-FixStuCourse\n名单发生变更 需要处理的课程\n{courses.ToJsonString()}", GroupNames.醍摩豆服務運維群組);
  155. //2.获取课程的id 并尝试添加或移除对应的学生课程记录StuCourse。
  156. foreach (var course in courses)
  157. {
  158. //学生新加入名单的
  159. foreach (Students students in stuListChange.stujoin)
  160. {
  161. var stucourse = new StuCourse
  162. {
  163. id = course.id,
  164. scode = course.code,
  165. name = course.name,
  166. code = $"StuCourse-{course.school}-{students.id}",
  167. scope = course.scope,
  168. school = course.school,
  169. creatorId = course.creatorId,
  170. pk = "StuCourse"
  171. };
  172. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-StuListService-FixStuCourse\n名单发生变更 新建课程中间表\n{stucourse.ToJsonString()}", GroupNames.醍摩豆服務運維群組);
  173. await client.GetContainer("TEAMModelOS", "Student").UpsertItemAsync(stucourse, new PartitionKey(stucourse.code));
  174. }
  175. //tmd新加入的
  176. foreach (string tmdid in stuListChange.tmdjoin)
  177. {
  178. var stucourse = new StuCourse
  179. {
  180. id = course.id,
  181. scode = course.code,
  182. name = course.name,
  183. code = $"StuCourse-{tmdid}",
  184. scope = course.scope,
  185. school = course.school,
  186. creatorId = course.creatorId,
  187. pk = "StuCourse"
  188. };
  189. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-StuListService-FixStuCourse\n名单发生变更 新建课程中间表\n{stucourse.ToJsonString()}", GroupNames.醍摩豆服務運維群組);
  190. await client.GetContainer("TEAMModelOS", "Teacher").UpsertItemAsync(stucourse, new PartitionKey(stucourse.code));
  191. }
  192. //移除名单的。 在点击相关的课程,再去二次校验是否存在,不存在则再去删除。
  193. foreach (var delStu in stuListChange.stuleave)
  194. {
  195. await client.GetContainer("TEAMModelOS", "Student").DeleteItemStreamAsync(course.id, new PartitionKey($"Course-{course.school}-{delStu.id}"));
  196. }
  197. foreach (var delTmd in stuListChange.tmdhleave)
  198. {
  199. await client.GetContainer("TEAMModelOS", "Teacher").DeleteItemStreamAsync(course.id, new PartitionKey($"Course-{delTmd}"));
  200. }
  201. }
  202. }
  203. }
  204. }